Skip to content

Commit

Permalink
fix: cleaning up withTransactionF usage
Browse files Browse the repository at this point in the history
  • Loading branch information
tegefaulkes committed Aug 10, 2022
1 parent 213fdfd commit c8c94be
Show file tree
Hide file tree
Showing 37 changed files with 118 additions and 180 deletions.
40 changes: 15 additions & 25 deletions src/acl/ACL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
CreateDestroyStartStop,
ready,
} from '@matrixai/async-init/dist/CreateDestroyStartStop';
import { withF } from '@matrixai/resources';
import * as aclUtils from './utils';
import * as aclErrors from './errors';

Expand Down Expand Up @@ -91,21 +90,14 @@ class ACL {
this.logger.info(`Destroyed ${this.constructor.name}`);
}

@ready(new aclErrors.ErrorACLNotRunning())
public async withTransactionF<T>(
f: (tran: DBTransaction) => Promise<T>,
): Promise<T> {
return withF([this.db.transaction()], ([tran]) => f(tran));
}

@ready(new aclErrors.ErrorACLNotRunning())
public async sameNodePerm(
nodeId1: NodeId,
nodeId2: NodeId,
tran?: DBTransaction,
): Promise<boolean> {
if (tran == null) {
return this.withTransactionF(async (tran) =>
return this.db.withTransactionF((tran) =>
this.sameNodePerm(nodeId1, nodeId2, tran),
);
}
Expand All @@ -130,7 +122,7 @@ class ACL {
tran?: DBTransaction,
): Promise<Array<Record<NodeId, Permission>>> {
if (tran == null) {
return this.withTransactionF(async (tran) => this.getNodePerms(tran));
return this.db.withTransactionF((tran) => this.getNodePerms(tran));
}
const permIds: Record<PermissionIdString, Record<NodeId, Permission>> = {};
for await (const [keyPath, value] of tran.iterator([
Expand Down Expand Up @@ -171,7 +163,7 @@ class ACL {
tran?: DBTransaction,
): Promise<Record<VaultId, Record<NodeId, Permission>>> {
if (tran == null) {
return this.withTransactionF(async (tran) => this.getVaultPerms(tran));
return this.db.withTransactionF((tran) => this.getVaultPerms(tran));
}
const vaultPerms: Record<VaultId, Record<NodeId, Permission>> = {};
for await (const [keyPath, nodeIds] of tran.iterator<Record<NodeId, null>>(
Expand Down Expand Up @@ -226,9 +218,7 @@ class ACL {
tran?: DBTransaction,
): Promise<Permission | undefined> {
if (tran == null) {
return this.withTransactionF(async (tran) =>
this.getNodePerm(nodeId, tran),
);
return this.db.withTransactionF((tran) => this.getNodePerm(nodeId, tran));
}
const permId = await tran.get(
[...this.aclNodesDbPath, nodeId.toBuffer()],
Expand All @@ -255,7 +245,7 @@ class ACL {
tran?: DBTransaction,
): Promise<Record<NodeId, Permission>> {
if (tran == null) {
return this.withTransactionF(async (tran) =>
return this.db.withTransactionF((tran) =>
this.getVaultPerm(vaultId, tran),
);
}
Expand Down Expand Up @@ -311,7 +301,7 @@ class ACL {
tran?: DBTransaction,
): Promise<void> {
if (tran == null) {
return this.withTransactionF(async (tran) =>
return this.db.withTransactionF((tran) =>
this.setNodeAction(nodeId, action, tran),
);
}
Expand Down Expand Up @@ -357,7 +347,7 @@ class ACL {
tran?: DBTransaction,
): Promise<void> {
if (tran == null) {
return this.withTransactionF(async (tran) =>
return this.db.withTransactionF((tran) =>
this.unsetNodeAction(nodeId, action, tran),
);
}
Expand All @@ -384,7 +374,7 @@ class ACL {
tran?: DBTransaction,
): Promise<void> {
if (tran == null) {
return this.withTransactionF(async (tran) =>
return this.db.withTransactionF((tran) =>
this.setVaultAction(vaultId, nodeId, action, tran),
);
}
Expand Down Expand Up @@ -428,7 +418,7 @@ class ACL {
tran?: DBTransaction,
): Promise<void> {
if (tran == null) {
return this.withTransactionF(async (tran) =>
return this.db.withTransactionF((tran) =>
this.unsetVaultAction(vaultId, nodeId, action, tran),
);
}
Expand Down Expand Up @@ -470,7 +460,7 @@ class ACL {
tran?: DBTransaction,
): Promise<void> {
if (tran == null) {
return this.withTransactionF(async (tran) =>
return this.db.withTransactionF((tran) =>
this.setNodesPerm(nodeIds, perm, tran),
);
}
Expand Down Expand Up @@ -525,7 +515,7 @@ class ACL {
tran?: DBTransaction,
): Promise<void> {
if (tran == null) {
return this.withTransactionF(async (tran) =>
return this.db.withTransactionF((tran) =>
this.setNodePerm(nodeId, perm, tran),
);
}
Expand Down Expand Up @@ -566,7 +556,7 @@ class ACL {
tran?: DBTransaction,
): Promise<void> {
if (tran == null) {
return this.withTransactionF(async (tran) =>
return this.db.withTransactionF((tran) =>
this.unsetNodePerm(nodeId, tran),
);
}
Expand Down Expand Up @@ -598,7 +588,7 @@ class ACL {
tran?: DBTransaction,
): Promise<void> {
if (tran == null) {
return this.withTransactionF(async (tran) =>
return this.db.withTransactionF((tran) =>
this.unsetVaultPerms(vaultId, tran),
);
}
Expand Down Expand Up @@ -638,7 +628,7 @@ class ACL {
tran?: DBTransaction,
): Promise<void> {
if (tran == null) {
return this.withTransactionF(async (tran) =>
return this.db.withTransactionF((tran) =>
this.joinNodePerm(nodeId, nodeIdsJoin, perm, tran),
);
}
Expand Down Expand Up @@ -694,7 +684,7 @@ class ACL {
tran?: DBTransaction,
): Promise<void> {
if (tran == null) {
return this.withTransactionF(async (tran) =>
return this.db.withTransactionF((tran) =>
this.joinVaultPerms(vaultId, vaultIdsJoin, tran),
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/agent/service/nodesChainDataGet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function nodesChainDataGet({
): Promise<void> => {
try {
const response = new nodesPB.ChainData();
const chainData = await db.withTransactionF(async (tran) =>
const chainData = await db.withTransactionF((tran) =>
sigchain.getChainData(tran),
);
// Iterate through each claim in the chain, and serialize for transport
Expand Down
5 changes: 2 additions & 3 deletions src/agent/service/nodesClosestLocalNodesGet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ function nodesClosestLocalNodesGet({
},
);
// Get all local nodes that are closest to the target node from the request
const closestNodes = await db.withTransactionF(
async (tran) =>
await nodeGraph.getClosestNodes(nodeId, undefined, tran),
const closestNodes = await db.withTransactionF((tran) =>
nodeGraph.getClosestNodes(nodeId, undefined, tran),
);
for (const [nodeId, nodeData] of closestNodes) {
const addressMessage = new nodesPB.Address();
Expand Down
2 changes: 1 addition & 1 deletion src/agent/service/nodesCrossSignClaim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function nodesCrossSignClaim({
true,
);
try {
await db.withTransactionF(async (tran) => {
await db.withTransactionF((tran) => {
const readStatus = await genClaims.read();
// If nothing to read, end and destroy
if (readStatus.done) {
Expand Down
2 changes: 1 addition & 1 deletion src/agent/service/nodesHolePunchMessageSend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function nodesHolePunchMessageSend({
// Firstly, check if this node is the desired node
// If so, then we want to make this node start sending hole punching packets
// back to the source node.
await db.withTransactionF(async (tran) => {
await db.withTransactionF((tran) => {
if (keyManager.getNodeId().equals(targetId)) {
const [host, port] = networkUtils.parseAddress(
call.request.getProxyAddress(),
Expand Down
6 changes: 3 additions & 3 deletions src/agent/service/notificationsSend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ function notificationsSend({
try {
const jwt = call.request.getContent();
const notification = await notificationsUtils.verifyAndDecodeNotif(jwt);
await db.withTransactionF(async (tran) => {
await notificationsManager.receiveNotification(notification, tran);
});
await db.withTransactionF((tran) =>
notificationsManager.receiveNotification(notification, tran),
);
const response = new utilsPB.EmptyMessage();
callback(null, response);
return;
Expand Down
2 changes: 1 addition & 1 deletion src/agent/service/vaultsGitInfoGet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function vaultsGitInfoGet({
): Promise<void> => {
const genWritable = grpcUtils.generatorWritable(call, true);
try {
await db.withTransactionF(async (tran) => {
await db.withTransactionF((tran) => {
const vaultIdFromName = await vaultManager.getVaultId(
call.request.getVault()?.getNameOrId() as VaultName,
tran,
Expand Down
2 changes: 1 addition & 1 deletion src/agent/service/vaultsGitPackGet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function vaultsGitPackGet({
const nodeId = connectionInfo.remoteNodeId;
const nodeIdEncoded = nodesUtils.encodeNodeId(nodeId);
const nameOrId = meta.get('vaultNameOrId').pop()!.toString();
await db.withTransactionF(async (tran) => {
await db.withTransactionF((tran) => {
const vaultIdFromName = await vaultManager.getVaultId(
nameOrId as VaultName,
tran,
Expand Down
2 changes: 1 addition & 1 deletion src/agent/service/vaultsScan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function vaultsScan({
}
const nodeId = connectionInfo.remoteNodeId;
try {
await db.withTransactionF(async (tran) => {
await db.withTransactionF((tran) => {
const listResponse = vaultManager.handleScanVaults(nodeId, tran);
for await (const {
vaultId,
Expand Down
4 changes: 1 addition & 3 deletions src/client/service/agentLockAll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ function agentLockAll({
const response = new utilsPB.EmptyMessage();
const metadata = await authenticate(call.metadata);
call.sendMetadata(metadata);
await db.withTransactionF(
async (tran) => await sessionManager.resetKey(tran),
);
await db.withTransactionF((tran) => sessionManager.resetKey(tran));
callback(null, response);
return;
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion src/client/service/gestaltsActionsGetByIdentity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function gestaltsActionsGetByIdentity({
},
);

const result = await db.withTransactionF(async (tran) =>
const result = await db.withTransactionF((tran) =>
gestaltGraph.getGestaltActionsByIdentity(providerId, identityId, tran),
);
if (result == null) {
Expand Down
2 changes: 1 addition & 1 deletion src/client/service/gestaltsActionsGetByNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function gestaltsActionsGetByNode({
nodeId: call.request.getNodeId(),
},
);
const result = await db.withTransactionF(async (tran) =>
const result = await db.withTransactionF((tran) =>
gestaltGraph.getGestaltActionsByNode(nodeId, tran),
);
if (result == null) {
Expand Down
2 changes: 1 addition & 1 deletion src/client/service/gestaltsActionsSetByIdentity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function gestaltsActionsSetByIdentity({
identityId: call.request.getIdentity()?.getIdentityId(),
},
);
await db.withTransactionF(async (tran) =>
await db.withTransactionF((tran) =>
gestaltGraph.setGestaltActionByIdentity(
providerId,
identityId,
Expand Down
2 changes: 1 addition & 1 deletion src/client/service/gestaltsActionsSetByNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function gestaltsActionsSetByNode({
action: call.request.getAction(),
},
);
await db.withTransactionF(async (tran) =>
await db.withTransactionF((tran) =>
gestaltGraph.setGestaltActionByNode(nodeId, action, tran),
);
callback(null, response);
Expand Down
2 changes: 1 addition & 1 deletion src/client/service/gestaltsActionsUnsetByIdentity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function gestaltsActionsUnsetByIdentity({
identityId: call.request.getIdentity()?.getIdentityId(),
},
);
await db.withTransactionF(async (tran) =>
await db.withTransactionF((tran) =>
gestaltGraph.unsetGestaltActionByIdentity(
providerId,
identityId,
Expand Down
2 changes: 1 addition & 1 deletion src/client/service/gestaltsActionsUnsetByNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function gestaltsActionsUnsetByNode({
action: call.request.getAction(),
},
);
await db.withTransactionF(async (tran) =>
await db.withTransactionF((tran) =>
gestaltGraph.unsetGestaltActionByNode(nodeId, action, tran),
);
callback(null, response);
Expand Down
2 changes: 1 addition & 1 deletion src/client/service/gestaltsGestaltGetByIdentity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function gestaltsGestaltGetByIdentity({
identityId: call.request.getIdentityId(),
},
);
const gestalt = await db.withTransactionF(async (tran) =>
const gestalt = await db.withTransactionF((tran) =>
gestaltGraph.getGestaltByIdentity(providerId, identityId, tran),
);
if (gestalt != null) {
Expand Down
2 changes: 1 addition & 1 deletion src/client/service/gestaltsGestaltGetByNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function gestaltsGestaltGetByNode({
nodeId: call.request.getNodeId(),
},
);
const gestalt = await db.withTransactionF(async (tran) =>
const gestalt = await db.withTransactionF((tran) =>
gestaltGraph.getGestaltByNode(nodeId, tran),
);
if (gestalt != null) {
Expand Down
2 changes: 1 addition & 1 deletion src/client/service/gestaltsGestaltList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function gestaltsGestaltList({
try {
const metadata = await authenticate(call.metadata);
call.sendMetadata(metadata);
const certs: Array<Gestalt> = await db.withTransactionF(async (tran) =>
const certs: Array<Gestalt> = await db.withTransactionF((tran) =>
gestaltGraph.getGestalts(tran),
);
for (const cert of certs) {
Expand Down
2 changes: 1 addition & 1 deletion src/client/service/identitiesClaim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function identitiesClaim({
throw new identitiesErrors.ErrorProviderUnauthenticated();
}
// Create identity claim on our node
const [, claim] = await db.withTransactionF(async (tran) =>
const [, claim] = await db.withTransactionF((tran) =>
sigchain.addClaim(
{
type: 'identity',
Expand Down
2 changes: 1 addition & 1 deletion src/client/service/identitiesTokenDelete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function identitiesTokenDelete({
identityId: call.request.getIdentityId(),
},
);
await db.withTransactionF(async (tran) =>
await db.withTransactionF((tran) =>
identitiesManager.delToken(providerId, identityId, tran),
);
callback(null, response);
Expand Down
2 changes: 1 addition & 1 deletion src/client/service/identitiesTokenGet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function identitiesTokenGet({
identityId: call.request.getIdentityId(),
},
);
const tokens = await db.withTransactionF(async (tran) =>
const tokens = await db.withTransactionF((tran) =>
identitiesManager.getToken(providerId, identityId, tran),
);
response.setToken(JSON.stringify(tokens));
Expand Down
2 changes: 1 addition & 1 deletion src/client/service/identitiesTokenPut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function identitiesTokenPut({
identityId: call.request.getProvider()?.getIdentityId(),
},
);
await db.withTransactionF(async (tran) =>
await db.withTransactionF((tran) =>
identitiesManager.putToken(
providerId,
identityId,
Expand Down
2 changes: 1 addition & 1 deletion src/client/service/nodesAdd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function nodesAdd({
);
}

await db.withTransactionF(async (tran) =>
await db.withTransactionF((tran) =>
nodeManager.setNode(
nodeId,
{
Expand Down
2 changes: 1 addition & 1 deletion src/client/service/notificationsClear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function notificationsClear({
const response = new utilsPB.EmptyMessage();
const metadata = await authenticate(call.metadata);
call.sendMetadata(metadata);
await db.withTransactionF(async (tran) =>
await db.withTransactionF((tran) =>
notificationsManager.clearNotifications(tran),
);
callback(null, response);
Expand Down
2 changes: 1 addition & 1 deletion src/client/service/notificationsRead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function notificationsRead({
} else {
number = parseInt(numberField);
}
const notifications = await db.withTransactionF(async (tran) =>
const notifications = await db.withTransactionF((tran) =>
notificationsManager.readNotifications({
unread,
number,
Expand Down
Loading

0 comments on commit c8c94be

Please sign in to comment.