From c8c94be15949e3c59422ac49bebaf25d567bd22f Mon Sep 17 00:00:00 2001 From: Brian Botha Date: Wed, 10 Aug 2022 13:17:45 +1000 Subject: [PATCH] fix: cleaning up `withTransactionF` usage --- src/acl/ACL.ts | 40 ++++++---------- src/agent/service/nodesChainDataGet.ts | 2 +- .../service/nodesClosestLocalNodesGet.ts | 5 +- src/agent/service/nodesCrossSignClaim.ts | 2 +- .../service/nodesHolePunchMessageSend.ts | 2 +- src/agent/service/notificationsSend.ts | 6 +-- src/agent/service/vaultsGitInfoGet.ts | 2 +- src/agent/service/vaultsGitPackGet.ts | 2 +- src/agent/service/vaultsScan.ts | 2 +- src/client/service/agentLockAll.ts | 4 +- .../service/gestaltsActionsGetByIdentity.ts | 2 +- .../service/gestaltsActionsGetByNode.ts | 2 +- .../service/gestaltsActionsSetByIdentity.ts | 2 +- .../service/gestaltsActionsSetByNode.ts | 2 +- .../service/gestaltsActionsUnsetByIdentity.ts | 2 +- .../service/gestaltsActionsUnsetByNode.ts | 2 +- .../service/gestaltsGestaltGetByIdentity.ts | 2 +- .../service/gestaltsGestaltGetByNode.ts | 2 +- src/client/service/gestaltsGestaltList.ts | 2 +- src/client/service/identitiesClaim.ts | 2 +- src/client/service/identitiesTokenDelete.ts | 2 +- src/client/service/identitiesTokenGet.ts | 2 +- src/client/service/identitiesTokenPut.ts | 2 +- src/client/service/nodesAdd.ts | 2 +- src/client/service/notificationsClear.ts | 2 +- src/client/service/notificationsRead.ts | 2 +- src/client/service/vaultsCreate.ts | 2 +- src/client/service/vaultsList.ts | 2 +- src/gestalts/GestaltGraph.ts | 46 +++++++------------ src/identities/IdentitiesManager.ts | 16 ++----- src/nodes/NodeGraph.ts | 22 ++++----- src/nodes/NodeManager.ts | 4 +- src/notifications/NotificationsManager.ts | 14 +++--- src/sessions/SessionManager.ts | 17 ++----- src/sigchain/Sigchain.ts | 26 ++++------- src/vaults/VaultInternal.ts | 14 +++--- src/vaults/VaultManager.ts | 36 +++++++-------- 37 files changed, 118 insertions(+), 180 deletions(-) diff --git a/src/acl/ACL.ts b/src/acl/ACL.ts index db9a61d7b7..c66dee09c1 100644 --- a/src/acl/ACL.ts +++ b/src/acl/ACL.ts @@ -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'; @@ -91,13 +90,6 @@ class ACL { this.logger.info(`Destroyed ${this.constructor.name}`); } - @ready(new aclErrors.ErrorACLNotRunning()) - public async withTransactionF( - f: (tran: DBTransaction) => Promise, - ): Promise { - return withF([this.db.transaction()], ([tran]) => f(tran)); - } - @ready(new aclErrors.ErrorACLNotRunning()) public async sameNodePerm( nodeId1: NodeId, @@ -105,7 +97,7 @@ class ACL { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.sameNodePerm(nodeId1, nodeId2, tran), ); } @@ -130,7 +122,7 @@ class ACL { tran?: DBTransaction, ): Promise>> { if (tran == null) { - return this.withTransactionF(async (tran) => this.getNodePerms(tran)); + return this.db.withTransactionF((tran) => this.getNodePerms(tran)); } const permIds: Record> = {}; for await (const [keyPath, value] of tran.iterator([ @@ -171,7 +163,7 @@ class ACL { tran?: DBTransaction, ): Promise>> { if (tran == null) { - return this.withTransactionF(async (tran) => this.getVaultPerms(tran)); + return this.db.withTransactionF((tran) => this.getVaultPerms(tran)); } const vaultPerms: Record> = {}; for await (const [keyPath, nodeIds] of tran.iterator>( @@ -226,9 +218,7 @@ class ACL { tran?: DBTransaction, ): Promise { 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()], @@ -255,7 +245,7 @@ class ACL { tran?: DBTransaction, ): Promise> { if (tran == null) { - return this.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.getVaultPerm(vaultId, tran), ); } @@ -311,7 +301,7 @@ class ACL { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.setNodeAction(nodeId, action, tran), ); } @@ -357,7 +347,7 @@ class ACL { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.unsetNodeAction(nodeId, action, tran), ); } @@ -384,7 +374,7 @@ class ACL { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.setVaultAction(vaultId, nodeId, action, tran), ); } @@ -428,7 +418,7 @@ class ACL { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.unsetVaultAction(vaultId, nodeId, action, tran), ); } @@ -470,7 +460,7 @@ class ACL { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.setNodesPerm(nodeIds, perm, tran), ); } @@ -525,7 +515,7 @@ class ACL { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.setNodePerm(nodeId, perm, tran), ); } @@ -566,7 +556,7 @@ class ACL { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.unsetNodePerm(nodeId, tran), ); } @@ -598,7 +588,7 @@ class ACL { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.unsetVaultPerms(vaultId, tran), ); } @@ -638,7 +628,7 @@ class ACL { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.joinNodePerm(nodeId, nodeIdsJoin, perm, tran), ); } @@ -694,7 +684,7 @@ class ACL { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.joinVaultPerms(vaultId, vaultIdsJoin, tran), ); } diff --git a/src/agent/service/nodesChainDataGet.ts b/src/agent/service/nodesChainDataGet.ts index 10175c7063..97a5375fb2 100644 --- a/src/agent/service/nodesChainDataGet.ts +++ b/src/agent/service/nodesChainDataGet.ts @@ -26,7 +26,7 @@ function nodesChainDataGet({ ): Promise => { 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 diff --git a/src/agent/service/nodesClosestLocalNodesGet.ts b/src/agent/service/nodesClosestLocalNodesGet.ts index 4c987667d0..12cb6e0667 100644 --- a/src/agent/service/nodesClosestLocalNodesGet.ts +++ b/src/agent/service/nodesClosestLocalNodesGet.ts @@ -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(); diff --git a/src/agent/service/nodesCrossSignClaim.ts b/src/agent/service/nodesCrossSignClaim.ts index 2c9793ba08..0e0e5a53e1 100644 --- a/src/agent/service/nodesCrossSignClaim.ts +++ b/src/agent/service/nodesCrossSignClaim.ts @@ -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) { diff --git a/src/agent/service/nodesHolePunchMessageSend.ts b/src/agent/service/nodesHolePunchMessageSend.ts index c610d7428f..65738e89c3 100644 --- a/src/agent/service/nodesHolePunchMessageSend.ts +++ b/src/agent/service/nodesHolePunchMessageSend.ts @@ -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(), diff --git a/src/agent/service/notificationsSend.ts b/src/agent/service/notificationsSend.ts index cd1b43c762..d192f1905f 100644 --- a/src/agent/service/notificationsSend.ts +++ b/src/agent/service/notificationsSend.ts @@ -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; diff --git a/src/agent/service/vaultsGitInfoGet.ts b/src/agent/service/vaultsGitInfoGet.ts index 0fb18c96ad..4c614203a8 100644 --- a/src/agent/service/vaultsGitInfoGet.ts +++ b/src/agent/service/vaultsGitInfoGet.ts @@ -34,7 +34,7 @@ function vaultsGitInfoGet({ ): Promise => { 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, diff --git a/src/agent/service/vaultsGitPackGet.ts b/src/agent/service/vaultsGitPackGet.ts index f8aa5dc3d8..f388f98558 100644 --- a/src/agent/service/vaultsGitPackGet.ts +++ b/src/agent/service/vaultsGitPackGet.ts @@ -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, diff --git a/src/agent/service/vaultsScan.ts b/src/agent/service/vaultsScan.ts index f827191085..2fbd498610 100644 --- a/src/agent/service/vaultsScan.ts +++ b/src/agent/service/vaultsScan.ts @@ -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, diff --git a/src/client/service/agentLockAll.ts b/src/client/service/agentLockAll.ts index 2c2c7505ed..da90e23a53 100644 --- a/src/client/service/agentLockAll.ts +++ b/src/client/service/agentLockAll.ts @@ -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) { diff --git a/src/client/service/gestaltsActionsGetByIdentity.ts b/src/client/service/gestaltsActionsGetByIdentity.ts index 3375ed15df..0b7d7c0390 100644 --- a/src/client/service/gestaltsActionsGetByIdentity.ts +++ b/src/client/service/gestaltsActionsGetByIdentity.ts @@ -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) { diff --git a/src/client/service/gestaltsActionsGetByNode.ts b/src/client/service/gestaltsActionsGetByNode.ts index ea0e4298d4..b221186ecc 100644 --- a/src/client/service/gestaltsActionsGetByNode.ts +++ b/src/client/service/gestaltsActionsGetByNode.ts @@ -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) { diff --git a/src/client/service/gestaltsActionsSetByIdentity.ts b/src/client/service/gestaltsActionsSetByIdentity.ts index b60d3aa84c..1944e1b678 100644 --- a/src/client/service/gestaltsActionsSetByIdentity.ts +++ b/src/client/service/gestaltsActionsSetByIdentity.ts @@ -56,7 +56,7 @@ function gestaltsActionsSetByIdentity({ identityId: call.request.getIdentity()?.getIdentityId(), }, ); - await db.withTransactionF(async (tran) => + await db.withTransactionF((tran) => gestaltGraph.setGestaltActionByIdentity( providerId, identityId, diff --git a/src/client/service/gestaltsActionsSetByNode.ts b/src/client/service/gestaltsActionsSetByNode.ts index 187c634a7e..b2009e98c7 100644 --- a/src/client/service/gestaltsActionsSetByNode.ts +++ b/src/client/service/gestaltsActionsSetByNode.ts @@ -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); diff --git a/src/client/service/gestaltsActionsUnsetByIdentity.ts b/src/client/service/gestaltsActionsUnsetByIdentity.ts index b2467bee59..d224c50532 100644 --- a/src/client/service/gestaltsActionsUnsetByIdentity.ts +++ b/src/client/service/gestaltsActionsUnsetByIdentity.ts @@ -56,7 +56,7 @@ function gestaltsActionsUnsetByIdentity({ identityId: call.request.getIdentity()?.getIdentityId(), }, ); - await db.withTransactionF(async (tran) => + await db.withTransactionF((tran) => gestaltGraph.unsetGestaltActionByIdentity( providerId, identityId, diff --git a/src/client/service/gestaltsActionsUnsetByNode.ts b/src/client/service/gestaltsActionsUnsetByNode.ts index bc39dc569b..fc2fa56709 100644 --- a/src/client/service/gestaltsActionsUnsetByNode.ts +++ b/src/client/service/gestaltsActionsUnsetByNode.ts @@ -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); diff --git a/src/client/service/gestaltsGestaltGetByIdentity.ts b/src/client/service/gestaltsGestaltGetByIdentity.ts index 8768ad1364..5c96467a09 100644 --- a/src/client/service/gestaltsGestaltGetByIdentity.ts +++ b/src/client/service/gestaltsGestaltGetByIdentity.ts @@ -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) { diff --git a/src/client/service/gestaltsGestaltGetByNode.ts b/src/client/service/gestaltsGestaltGetByNode.ts index 207859fb5e..f5677758dc 100644 --- a/src/client/service/gestaltsGestaltGetByNode.ts +++ b/src/client/service/gestaltsGestaltGetByNode.ts @@ -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) { diff --git a/src/client/service/gestaltsGestaltList.ts b/src/client/service/gestaltsGestaltList.ts index d07fb9f32f..62c25c5705 100644 --- a/src/client/service/gestaltsGestaltList.ts +++ b/src/client/service/gestaltsGestaltList.ts @@ -28,7 +28,7 @@ function gestaltsGestaltList({ try { const metadata = await authenticate(call.metadata); call.sendMetadata(metadata); - const certs: Array = await db.withTransactionF(async (tran) => + const certs: Array = await db.withTransactionF((tran) => gestaltGraph.getGestalts(tran), ); for (const cert of certs) { diff --git a/src/client/service/identitiesClaim.ts b/src/client/service/identitiesClaim.ts index 6677c77d46..952cd77ae3 100644 --- a/src/client/service/identitiesClaim.ts +++ b/src/client/service/identitiesClaim.ts @@ -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', diff --git a/src/client/service/identitiesTokenDelete.ts b/src/client/service/identitiesTokenDelete.ts index 2b4a78b9b9..da0bbaa20f 100644 --- a/src/client/service/identitiesTokenDelete.ts +++ b/src/client/service/identitiesTokenDelete.ts @@ -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); diff --git a/src/client/service/identitiesTokenGet.ts b/src/client/service/identitiesTokenGet.ts index c829da281b..3a25c1b069 100644 --- a/src/client/service/identitiesTokenGet.ts +++ b/src/client/service/identitiesTokenGet.ts @@ -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)); diff --git a/src/client/service/identitiesTokenPut.ts b/src/client/service/identitiesTokenPut.ts index b7ae0139f5..4ce1588382 100644 --- a/src/client/service/identitiesTokenPut.ts +++ b/src/client/service/identitiesTokenPut.ts @@ -53,7 +53,7 @@ function identitiesTokenPut({ identityId: call.request.getProvider()?.getIdentityId(), }, ); - await db.withTransactionF(async (tran) => + await db.withTransactionF((tran) => identitiesManager.putToken( providerId, identityId, diff --git a/src/client/service/nodesAdd.ts b/src/client/service/nodesAdd.ts index 92de5581d4..87b356b7f3 100644 --- a/src/client/service/nodesAdd.ts +++ b/src/client/service/nodesAdd.ts @@ -72,7 +72,7 @@ function nodesAdd({ ); } - await db.withTransactionF(async (tran) => + await db.withTransactionF((tran) => nodeManager.setNode( nodeId, { diff --git a/src/client/service/notificationsClear.ts b/src/client/service/notificationsClear.ts index ebcea2af08..e26b24cb44 100644 --- a/src/client/service/notificationsClear.ts +++ b/src/client/service/notificationsClear.ts @@ -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); diff --git a/src/client/service/notificationsRead.ts b/src/client/service/notificationsRead.ts index f706b5bd26..4e790f7fa0 100644 --- a/src/client/service/notificationsRead.ts +++ b/src/client/service/notificationsRead.ts @@ -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, diff --git a/src/client/service/vaultsCreate.ts b/src/client/service/vaultsCreate.ts index df7c6cfac4..26617a6650 100644 --- a/src/client/service/vaultsCreate.ts +++ b/src/client/service/vaultsCreate.ts @@ -31,7 +31,7 @@ function vaultsCreate({ try { const metadata = await authenticate(call.metadata); call.sendMetadata(metadata); - vaultId = await db.withTransactionF(async (tran) => + vaultId = await db.withTransactionF((tran) => vaultManager.createVault(call.request.getNameOrId() as VaultName, tran), ); response.setNameOrId(vaultsUtils.encodeVaultId(vaultId)); diff --git a/src/client/service/vaultsList.ts b/src/client/service/vaultsList.ts index c7d3da737b..3fbbdadd52 100644 --- a/src/client/service/vaultsList.ts +++ b/src/client/service/vaultsList.ts @@ -27,7 +27,7 @@ function vaultsList({ try { const metadata = await authenticate(call.metadata); call.sendMetadata(metadata); - const vaults = await db.withTransactionF(async (tran) => + const vaults = await db.withTransactionF((tran) => vaultManager.listVaults(tran), ); for await (const [vaultName, vaultId] of vaults) { diff --git a/src/gestalts/GestaltGraph.ts b/src/gestalts/GestaltGraph.ts index f61b234955..e9f688ca29 100644 --- a/src/gestalts/GestaltGraph.ts +++ b/src/gestalts/GestaltGraph.ts @@ -17,7 +17,6 @@ import { CreateDestroyStartStop, ready, } from '@matrixai/async-init/dist/CreateDestroyStartStop'; -import { withF } from '@matrixai/resources'; import * as gestaltsUtils from './utils'; import * as gestaltsErrors from './errors'; import * as aclUtils from '../acl/utils'; @@ -90,17 +89,10 @@ class GestaltGraph { this.logger.info(`Destroyed ${this.constructor.name}`); } - @ready(new gestaltsErrors.ErrorGestaltsGraphNotRunning()) - public async withTransactionF( - f: (tran: DBTransaction) => Promise, - ): Promise { - return withF([this.db.transaction()], ([tran]) => f(tran)); - } - @ready(new gestaltsErrors.ErrorGestaltsGraphNotRunning()) public async getGestalts(tran?: DBTransaction): Promise> { if (tran == null) { - return this.withTransactionF(async (tran) => this.getGestalts(tran)); + return this.db.withTransactionF((tran) => this.getGestalts(tran)); } const unvisited: Map = new Map(); for await (const [k, gKs] of tran.iterator( @@ -164,7 +156,7 @@ class GestaltGraph { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.getGestaltByNode(nodeId, tran), ); } @@ -179,7 +171,7 @@ class GestaltGraph { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.getGestaltByIdentity(providerId, identityId, tran), ); } @@ -193,7 +185,7 @@ class GestaltGraph { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.setIdentity(identityInfo, tran), ); } @@ -222,7 +214,7 @@ class GestaltGraph { tran?: DBTransaction, ) { if (tran == null) { - return this.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.unsetIdentity(providerId, identityId, tran), ); } @@ -267,9 +259,7 @@ class GestaltGraph { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.withTransactionF(async (tran) => - this.setNode(nodeInfo, tran), - ); + return this.db.withTransactionF((tran) => this.setNode(nodeInfo, tran)); } const nodeKey = gestaltsUtils.keyFromNode( nodesUtils.decodeNodeId(nodeInfo.id)!, @@ -307,9 +297,7 @@ class GestaltGraph { @ready(new gestaltsErrors.ErrorGestaltsGraphNotRunning()) public async unsetNode(nodeId: NodeId, tran?: DBTransaction): Promise { if (tran == null) { - return this.withTransactionF(async (tran) => - this.unsetNode(nodeId, tran), - ); + return this.db.withTransactionF((tran) => this.unsetNode(nodeId, tran)); } const nodeKey = gestaltsUtils.keyFromNode(nodeId); const nodeKeyPath = [ @@ -356,7 +344,7 @@ class GestaltGraph { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.linkNodeAndIdentity(nodeInfo, identityInfo, tran), ); } @@ -502,7 +490,7 @@ class GestaltGraph { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.linkNodeAndNode(nodeInfo1, nodeInfo2, tran), ); } @@ -621,7 +609,7 @@ class GestaltGraph { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.unlinkNodeAndIdentity(nodeId, providerId, identityId, tran), ); } @@ -676,7 +664,7 @@ class GestaltGraph { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.unlinkNodeAndNode(nodeId1, nodeId2, tran), ); } @@ -729,7 +717,7 @@ class GestaltGraph { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.getGestaltActionsByNode(nodeId, tran), ); } @@ -755,7 +743,7 @@ class GestaltGraph { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.getGestaltActionsByIdentity(providerId, identityId, tran), ); } @@ -796,7 +784,7 @@ class GestaltGraph { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.setGestaltActionByNode(nodeId, action, tran), ); } @@ -819,7 +807,7 @@ class GestaltGraph { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.setGestaltActionByIdentity(providerId, identityId, action, tran), ); } @@ -855,7 +843,7 @@ class GestaltGraph { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.unsetGestaltActionByNode(nodeId, action, tran), ); } @@ -878,7 +866,7 @@ class GestaltGraph { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.unsetGestaltActionByIdentity(providerId, identityId, action, tran), ); } diff --git a/src/identities/IdentitiesManager.ts b/src/identities/IdentitiesManager.ts index f4e42dc38e..2f1e98adfb 100644 --- a/src/identities/IdentitiesManager.ts +++ b/src/identities/IdentitiesManager.ts @@ -11,7 +11,6 @@ import { CreateDestroyStartStop, ready, } from '@matrixai/async-init/dist/CreateDestroyStartStop'; -import { withF } from '@matrixai/resources'; import * as identitiesErrors from './errors'; interface IdentitiesManager extends CreateDestroyStartStop {} @@ -74,13 +73,6 @@ class IdentitiesManager { this.logger.info(`Destroyed ${this.constructor.name}`); } - @ready(new identitiesErrors.ErrorIdentitiesManagerNotRunning()) - public async withTransactionF( - f: (tran: DBTransaction) => Promise, - ): Promise { - return withF([this.db.transaction()], ([tran]) => f(tran)); - } - @ready(new identitiesErrors.ErrorIdentitiesManagerNotRunning()) public getProviders(): Record { return Object.fromEntries(this.providers); @@ -116,7 +108,7 @@ class IdentitiesManager { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.getTokens(providerId, tran), ); } @@ -138,7 +130,7 @@ class IdentitiesManager { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.getToken(providerId, identityId, tran), ); } @@ -161,7 +153,7 @@ class IdentitiesManager { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.putToken(providerId, identityId, tokenData, tran), ); } @@ -181,7 +173,7 @@ class IdentitiesManager { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.delToken(providerId, identityId, tran), ); } diff --git a/src/nodes/NodeGraph.ts b/src/nodes/NodeGraph.ts index e37140138c..738aaeacbf 100644 --- a/src/nodes/NodeGraph.ts +++ b/src/nodes/NodeGraph.ts @@ -157,9 +157,7 @@ class NodeGraph { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.db.withTransactionF(async (tran) => - this.getNode(nodeId, tran), - ); + return this.db.withTransactionF((tran) => this.getNode(nodeId, tran)); } const [bucketIndex] = this.bucketIndex(nodeId); @@ -217,7 +215,7 @@ class NodeGraph { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.db.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.setNode(nodeId, nodeAddress, tran), ); } @@ -266,7 +264,7 @@ class NodeGraph { tran?: DBTransaction, ): Promise> { if (tran == null) { - return this.db.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.getOldestNode(bucketIndex, limit, tran), ); } @@ -286,9 +284,7 @@ class NodeGraph { @ready(new nodesErrors.ErrorNodeGraphNotRunning()) public async unsetNode(nodeId: NodeId, tran?: DBTransaction): Promise { if (tran == null) { - return this.db.withTransactionF(async (tran) => - this.unsetNode(nodeId, tran), - ); + return this.db.withTransactionF((tran) => this.unsetNode(nodeId, tran)); } const [bucketIndex, bucketKey] = this.bucketIndex(nodeId); @@ -324,7 +320,7 @@ class NodeGraph { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.db.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.getBucket(bucketIndex, sort, order, tran), ); } @@ -499,7 +495,7 @@ class NodeGraph { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.db.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.resetBuckets(nodeIdOwn, tran), ); } @@ -598,7 +594,7 @@ class NodeGraph { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.db.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.getBucketMeta(bucketIndex, tran), ); } @@ -629,7 +625,7 @@ class NodeGraph { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.db.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.getBucketMetaProp(bucketIndex, key, tran), ); } @@ -676,7 +672,7 @@ class NodeGraph { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.db.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.getClosestNodes(nodeId, limit, tran), ); } diff --git a/src/nodes/NodeManager.ts b/src/nodes/NodeManager.ts index 0609d45a74..aa0740ee52 100644 --- a/src/nodes/NodeManager.ts +++ b/src/nodes/NodeManager.ts @@ -233,7 +233,7 @@ class NodeManager { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.db.withTransactionF(async (tran) => { + return this.db.withTransactionF((tran) => { return this.claimNode(targetNodeId, tran); }); } @@ -418,7 +418,7 @@ class NodeManager { } if (tran == null) { - return this.db.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.setNode(nodeId, nodeAddress, block, force, timeout, tran), ); } diff --git a/src/notifications/NotificationsManager.ts b/src/notifications/NotificationsManager.ts index 9b58945990..f616431216 100644 --- a/src/notifications/NotificationsManager.ts +++ b/src/notifications/NotificationsManager.ts @@ -162,9 +162,9 @@ class NotificationsManager { public async destroy() { this.logger.info(`Destroying ${this.constructor.name}`); - await this.db.withTransactionF(async (tran) => { - await tran.clear(this.notificationsDbPath); - }); + await this.db.withTransactionF((tran) => + tran.clear(this.notificationsDbPath), + ); this.logger.info(`Destroyed ${this.constructor.name}`); } @@ -218,7 +218,7 @@ class NotificationsManager { ): Promise { const messageCountPath = [...this.notificationsDbPath, MESSAGE_COUNT_KEY]; if (tran == null) { - return this.withTransactionF(messageCountPath, async (tran) => + return this.withTransactionF(messageCountPath, (tran) => this.receiveNotification(notification, tran), ); } @@ -269,7 +269,7 @@ class NotificationsManager { tran?: DBTransaction; } = {}): Promise> { if (tran == null) { - return this.withTransactionF(async (tran) => + return this.withTransactionF((tran) => this.readNotifications({ unread, number, order, tran }), ); } @@ -309,7 +309,7 @@ class NotificationsManager { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.withTransactionF(async (tran) => + return this.withTransactionF((tran) => this.findGestaltInvite(fromNode, tran), ); } @@ -331,7 +331,7 @@ class NotificationsManager { public async clearNotifications(tran?: DBTransaction): Promise { const messageCountPath = [...this.notificationsDbPath, MESSAGE_COUNT_KEY]; if (tran == null) { - return this.withTransactionF(messageCountPath, async (tran) => + return this.withTransactionF(messageCountPath, (tran) => this.clearNotifications(tran), ); } diff --git a/src/sessions/SessionManager.ts b/src/sessions/SessionManager.ts index 4a9f1607be..160080f349 100644 --- a/src/sessions/SessionManager.ts +++ b/src/sessions/SessionManager.ts @@ -98,17 +98,10 @@ class SessionManager { this.logger.info(`Destroyed ${this.constructor.name}`); } - @ready(new sessionsErrors.ErrorSessionManagerNotRunning()) - public async withTransactionF( - f: (tran: DBTransaction) => Promise, - ): Promise { - return withF([this.db.transaction()], ([tran]) => f(tran)); - } - @ready(new sessionsErrors.ErrorSessionManagerNotRunning()) public async resetKey(tran?: DBTransaction): Promise { if (tran == null) { - return this.withTransactionF(async (tran) => this.resetKey(tran)); + return this.db.withTransactionF((tran) => this.resetKey(tran)); } const key = await this.generateKey(this.keyBits); await tran.put([...this.sessionsDbPath, 'key'], key, true); @@ -125,9 +118,7 @@ class SessionManager { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.withTransactionF(async (tran) => - this.createToken(expiry, tran), - ); + return this.db.withTransactionF((tran) => this.createToken(expiry, tran)); } const payload = { iss: nodesUtils.encodeNodeId(this.keyManager.getNodeId()), @@ -144,9 +135,7 @@ class SessionManager { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.withTransactionF(async (tran) => - this.verifyToken(token, tran), - ); + return this.db.withTransactionF((tran) => this.verifyToken(token, tran)); } const key = await tran.get([...this.sessionsDbPath, 'key'], true); const result = await sessionsUtils.verifySessionToken(token, key!); diff --git a/src/sigchain/Sigchain.ts b/src/sigchain/Sigchain.ts index d69e30c579..45c0c999e3 100644 --- a/src/sigchain/Sigchain.ts +++ b/src/sigchain/Sigchain.ts @@ -186,10 +186,8 @@ class Sigchain { this.sequenceNumberKey, ]; if (tran == null) { - return this.withTransactionF( - claimIdPath, - sequenceNumberPath, - async (tran) => this.addClaim(claimData, tran), + return this.withTransactionF(claimIdPath, sequenceNumberPath, (tran) => + this.addClaim(claimData, tran), ); } const prevSequenceNumber = await this.getSequenceNumber(tran); @@ -225,10 +223,8 @@ class Sigchain { this.sequenceNumberKey, ]; if (tran == null) { - return this.withTransactionF( - claimIdPath, - sequenceNumberPath, - async (tran) => this.addExistingClaim(claim, tran), + return this.withTransactionF(claimIdPath, sequenceNumberPath, (tran) => + this.addExistingClaim(claim, tran), ); } const decodedClaim = claimsUtils.decodeClaim(claim); @@ -259,7 +255,7 @@ class Sigchain { this.sequenceNumberKey, ]; if (tran == null) { - return this.withTransactionF(sequenceNumberPath, async (tran) => + return this.withTransactionF(sequenceNumberPath, (tran) => this.createIntermediaryClaim(claimData, tran), ); } @@ -283,7 +279,7 @@ class Sigchain { @ready(new sigchainErrors.ErrorSigchainNotRunning()) public async getChainData(tran?: DBTransaction): Promise { if (tran == null) { - return this.withTransactionF(async (tran) => this.getChainData(tran)); + return this.withTransactionF((tran) => this.getChainData(tran)); } const chainData: ChainDataEncoded = {}; const readIterator = tran.iterator( @@ -312,9 +308,7 @@ class Sigchain { tran?: DBTransaction, ): Promise> { if (tran == null) { - return this.withTransactionF(async (tran) => - this.getClaims(claimType, tran), - ); + return this.withTransactionF((tran) => this.getClaims(claimType, tran)); } const relevantClaims: Array = []; const readIterator = tran.iterator( @@ -380,9 +374,7 @@ class Sigchain { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.withTransactionF(async (tran) => - this.getClaim(claimId, tran), - ); + return this.withTransactionF((tran) => this.getClaim(claimId, tran)); } const claim = await tran.get([ ...this.sigchainClaimsDbPath, @@ -399,7 +391,7 @@ class Sigchain { tran?: DBTransaction, ): Promise> { if (tran == null) { - return this.withTransactionF(async (tran) => this.getSeqMap(tran)); + return this.withTransactionF((tran) => this.getSeqMap(tran)); } const map: Record = {}; const claimStream = tran.iterator(this.sigchainClaimsDbPath, { diff --git a/src/vaults/VaultInternal.ts b/src/vaults/VaultInternal.ts index 0061d9185b..d354d59b15 100644 --- a/src/vaults/VaultInternal.ts +++ b/src/vaults/VaultInternal.ts @@ -68,7 +68,7 @@ class VaultInternal { tran?: DBTransaction; }): Promise { if (tran == null) { - return await db.withTransactionF(async (tran) => + return await db.withTransactionF((tran) => this.createVaultInternal({ vaultId, vaultName, @@ -122,7 +122,7 @@ class VaultInternal { tran?: DBTransaction; }): Promise { if (tran == null) { - return await db.withTransactionF(async (tran) => + return await db.withTransactionF((tran) => this.cloneVaultInternal({ targetNodeId, targetVaultNameOrId, @@ -266,7 +266,7 @@ class VaultInternal { tran?: DBTransaction; } = {}): Promise { if (tran == null) { - return await this.db.withTransactionF(async (tran) => + return await this.db.withTransactionF((tran) => this.start_(fresh, tran, vaultName), ); } @@ -328,9 +328,7 @@ class VaultInternal { public async destroy(tran?: DBTransaction): Promise { if (tran == null) { - return await this.db.withTransactionF(async (tran) => - this.destroy_(tran), - ); + return await this.db.withTransactionF((tran) => this.destroy_(tran)); } return await this.destroy_(tran); } @@ -444,7 +442,7 @@ class VaultInternal { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.db.withTransactionF(async (tran) => this.writeF(f, tran)); + return this.db.withTransactionF((tran) => this.writeF(f, tran)); } // This should really be an internal property @@ -537,7 +535,7 @@ class VaultInternal { tran?: DBTransaction; }): Promise { if (tran == null) { - return this.db.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.pullVault({ nodeConnectionManager, pullNodeId, diff --git a/src/vaults/VaultManager.ts b/src/vaults/VaultManager.ts index 6bd32803d1..eb757beaec 100644 --- a/src/vaults/VaultManager.ts +++ b/src/vaults/VaultManager.ts @@ -268,7 +268,7 @@ class VaultManager { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.db.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.createVault(vaultName, tran), ); } @@ -322,7 +322,7 @@ class VaultManager { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.db.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.getVaultMeta(vaultId, tran), ); } @@ -362,7 +362,7 @@ class VaultManager { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.db.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.destroyVault(vaultId, tran), ); } @@ -396,9 +396,7 @@ class VaultManager { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.db.withTransactionF(async (tran) => - this.closeVault(vaultId, tran), - ); + return this.db.withTransactionF((tran) => this.closeVault(vaultId, tran)); } if ((await this.getVaultName(vaultId, tran)) == null) { @@ -419,7 +417,7 @@ class VaultManager { @ready(new vaultsErrors.ErrorVaultManagerNotRunning()) public async listVaults(tran?: DBTransaction): Promise { if (tran == null) { - return this.db.withTransactionF(async (tran) => this.listVaults(tran)); + return this.db.withTransactionF((tran) => this.listVaults(tran)); } const vaults: VaultList = new Map(); @@ -444,7 +442,7 @@ class VaultManager { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.db.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.renameVault(vaultId, newVaultName, tran), ); } @@ -488,7 +486,7 @@ class VaultManager { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.db.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.getVaultId(vaultName, tran), ); } @@ -512,7 +510,7 @@ class VaultManager { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.db.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.getVaultName(vaultId, tran), ); } @@ -529,7 +527,7 @@ class VaultManager { tran?: DBTransaction, ): Promise> { if (tran == null) { - return this.db.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.getVaultPermission(vaultId, tran), ); } @@ -554,7 +552,7 @@ class VaultManager { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.db.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.shareVault(vaultId, nodeId, tran), ); } @@ -588,7 +586,7 @@ class VaultManager { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.db.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.unshareVault(vaultId, nodeId, tran), ); } @@ -611,7 +609,7 @@ class VaultManager { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.db.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.cloneVault(nodeId, vaultNameOrId, tran), ); } @@ -697,7 +695,7 @@ class VaultManager { tran?: DBTransaction; }): Promise { if (tran == null) { - return this.db.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.pullVault({ vaultId, pullNodeId, pullVaultNameOrId, tran }), ); } @@ -767,7 +765,7 @@ class VaultManager { tran?: DBTransaction, ): Promise<[PassThrough, PassThrough]> { if (tran == null) { - return this.db.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.handlePackRequest(vaultId, body, tran), ); } @@ -913,9 +911,7 @@ class VaultManager { tran: DBTransaction, ): Promise { if (tran == null) { - return this.db.withTransactionF(async (tran) => - this.getVault(vaultId, tran), - ); + return this.db.withTransactionF((tran) => this.getVault(vaultId, tran)); } const vaultIdString = vaultId.toString() as VaultIdString; @@ -955,7 +951,7 @@ class VaultManager { tran?: DBTransaction, ): Promise { if (tran == null) { - return this.db.withTransactionF(async (tran) => + return this.db.withTransactionF((tran) => this.withVaults(vaultIds, f, tran), ); }