From 13dbaaa7cc5f5e51012008e9c198ca31d78ed638 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Iv=C3=A1n=20Vieitez=20Parra?= <3857362+corrideat@users.noreply.github.com> Date: Tue, 23 Jan 2024 22:59:07 +0100 Subject: [PATCH] Remove await for actions in identity contract --- frontend/controller/actions/group.js | 7 +++++-- frontend/controller/actions/identity.js | 21 +++++++++++++++++++-- frontend/model/contracts/identity.js | 6 ++++-- frontend/model/contracts/manifests.json | 4 ++-- shared/domains/chelonia/chelonia.js | 3 +-- shared/domains/chelonia/utils.js | 2 +- 6 files changed, 32 insertions(+), 11 deletions(-) diff --git a/frontend/controller/actions/group.js b/frontend/controller/actions/group.js index 8fa4b5f530..2c46a44c14 100644 --- a/frontend/controller/actions/group.js +++ b/frontend/controller/actions/group.js @@ -262,7 +262,10 @@ export default (sbp('sbp/selectors/register', { // JOINING_GROUP, so that we process leave actions and don't interfere // with the leaving process (otherwise, the side-effects will prevent // us from fully leaving). - await sbp('chelonia/contract/wait', params.contractID) + await Promise.all([ + sbp('chelonia/contract/wait', params.originatingContractID), + sbp('chelonia/contract/wait', params.contractID) + ]) sbp('okTurtles.data/set', 'JOINING_GROUP-' + params.contractID, true) try { const { loggedIn } = sbp('state/vuex/state') @@ -369,7 +372,7 @@ export default (sbp('sbp/selectors/register', { postpublish: null } }).catch((e) => { - console.error(`[gi.actions/group/join] Error while sending key request for ${params.contractID}:`, e) + console.error(`[gi.actions/group/join] Error while sending key request for ${params.contractID}:`, e?.message || e, e) throw e }) diff --git a/frontend/controller/actions/identity.js b/frontend/controller/actions/identity.js index 401a1cf667..319899cf6e 100644 --- a/frontend/controller/actions/identity.js +++ b/frontend/controller/actions/identity.js @@ -456,9 +456,26 @@ export default (sbp('sbp/selectors/register', { try { const state = sbp('state/vuex/state') console.info('logging out, waiting for any events to finish...') + // wait for any pending operations to finish before calling state/vuex/save + // This includes, in order: + // 1. Actions to be sent (in the `encrypted-action` queue) + // 2. (In reset) Actions that haven't been published yet (the + // `publish:${contractID}` queues) + // 3. (In reset) Processing of any action (waiting on all the contract + // queues), including their side-effects (the `${contractID}` queues) + // 4. (In reset handler) Outgoing actions from side-effects (again, in + // the `encrypted-action` queue) + await sbp('okTurtles.eventQueue/queueEvent', 'encrypted-action', () => {}) + // reset will wait until we have processed any remaining actions await sbp('chelonia/reset', async () => { - // wait for any pending sync operations to finish before saving, - // including those that side-effects might have sent + // some of the actions that reset waited for might have side-effects + // that send actions + // we wait for those as well (the duplication in this case is + // intended) -- see 4. above + // The intent of this is to wait for all the current actions to be + // sent and then wait until any actions that are a side-effect are sent + // TODO: We might not need this second await and 1-3 could be fine (i.e., + // we could avoid waiting on these 2nd layer of actions) await sbp('okTurtles.eventQueue/queueEvent', 'encrypted-action', () => {}) // See comment below for 'gi.db/settings/delete' await sbp('state/vuex/save') diff --git a/frontend/model/contracts/identity.js b/frontend/model/contracts/identity.js index e0131a4bcf..99b705d568 100644 --- a/frontend/model/contracts/identity.js +++ b/frontend/model/contracts/identity.js @@ -193,7 +193,7 @@ sbp('chelonia/defineContract', { // a deadlock. if (!inviteSecretId) return - return sbp('gi.actions/group/join', { + sbp('gi.actions/group/join', { originatingContractID: contractID, originatingContractName: 'gi.contracts/identity', contractID: data.groupContractID, @@ -202,6 +202,8 @@ sbp('chelonia/defineContract', { signingKeyId: inviteSecretId, innerSigningKeyId: sbp('chelonia/contract/currentKeyIdByName', state, 'csk'), encryptionKeyId: sbp('chelonia/contract/currentKeyIdByName', state, 'cek') + }).catch(e => { + console.error(`[gi.contracts/identity/joinGroup/sideEffect] Error sending gi.actions/group/join action for group ${data.groupContractID}`, e) }) }).catch(e => { console.error(`[gi.contracts/identity/joinGroup/sideEffect] Error at queueInvocation group ${data.groupContractID}`, e) @@ -243,7 +245,7 @@ sbp('chelonia/defineContract', { contractID: groupContractID, data: {} }).catch(e => { - console.error(`[gi.contracts/identity/leaveGroup/sideEffect] Error sending /removeOurselvs action to group ${data.groupContractID}`, e) + console.error(`[gi.contracts/identity/leaveGroup/sideEffect] Error sending /removeOurselves action to group ${data.groupContractID}`, e) }) } diff --git a/frontend/model/contracts/manifests.json b/frontend/model/contracts/manifests.json index 316299d976..cba93fddd1 100644 --- a/frontend/model/contracts/manifests.json +++ b/frontend/model/contracts/manifests.json @@ -1,7 +1,7 @@ { "manifests": { "gi.contracts/chatroom": "21XWnNMgR144Bw53R99pzoqhdg7n6VdEE1o4PWdoN5mdrGQSTT", - "gi.contracts/group": "21XWnNRJJiwLyc5Ldgo5eTaDaJ8zSp74pYFB9JV2yCDjGa3gvA", - "gi.contracts/identity": "21XWnNWbBF1cz7VSRKffrmppQuPNncZdbmbN2Zf7bdmRSMgGCx" + "gi.contracts/group": "21XWnNUSm3U7wAEnPxnNDhocjPa235sAzMtLare3cfFkasN1ks", + "gi.contracts/identity": "21XWnNLTgLyFrGkbGbXCMtbCtB2apx88qXLDBhPiew2PzWtDCP" } } diff --git a/shared/domains/chelonia/chelonia.js b/shared/domains/chelonia/chelonia.js index a3e26fe93d..452a309e07 100644 --- a/shared/domains/chelonia/chelonia.js +++ b/shared/domains/chelonia/chelonia.js @@ -680,9 +680,8 @@ export default (sbp('sbp/selectors/register', { 'chelonia/contract/sync': function (contractIDs: string | string[], params?: { force?: boolean, deferredRemove?: boolean }): Promise<*> { const listOfIds = typeof contractIDs === 'string' ? [contractIDs] : contractIDs const forcedSync = !!params?.force - const rootState = sbp(this.config.stateSelector) return Promise.all(listOfIds.map(contractID => { - if (!forcedSync && has(rootState.contracts, contractID)) { + if (!forcedSync && this.subscriptionSet.has(contractID)) { if (params?.deferredRemove) { this.removeCount[contractID] = (this.removeCount[contractID] || 0) + 1 } diff --git a/shared/domains/chelonia/utils.js b/shared/domains/chelonia/utils.js index 97933b614a..ed13ba0b70 100644 --- a/shared/domains/chelonia/utils.js +++ b/shared/domains/chelonia/utils.js @@ -123,7 +123,7 @@ export const validateKeyPermissions = (state: Object, signingKeyId: string, opT: ) ) ) { - console.error(`Signing key ${signingKey.id} is missing permissions for operation ${opT}`) + console.error(`Signing key ${signingKeyId} is missing permissions for operation ${opT}`) return false }