From 5fc485e233391d00ab1e56aa365f9abb587019fa Mon Sep 17 00:00:00 2001 From: Sosthene Date: Wed, 10 Sep 2025 11:30:51 +0200 Subject: [PATCH] Fix checkConnections, also check more aggressively --- src/router.ts | 2 ++ src/services/service.ts | 32 ++++++++++++++++---------------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/router.ts b/src/router.ts index ba3c2c4..c8dca76 100755 --- a/src/router.ts +++ b/src/router.ts @@ -376,6 +376,8 @@ export async function registerAllListeners() { } const state = services.getStateFromId(process, stateId); + await services.checkConnections(process, stateId); + let res: Record = {}; if (state) { // Decrypt all the data we have the key for diff --git a/src/services/service.ts b/src/services/service.ts index da90389..9d9a225 100755 --- a/src/services/service.ts +++ b/src/services/service.ts @@ -210,15 +210,18 @@ export default class Services { // If we're updating a process, we must call that after update especially if roles are part of it // We will take the roles from the last state, wheter it's commited or not - public async checkConnections(process: Process): Promise { + public async checkConnections(process: Process, stateId: string | null = null): Promise { if (process.states.length < 2) { throw new Error('Process doesn\'t have any state yet'); } - let roles = process.states[process.states.length - 2].roles; + let roles: Record | null = null; + if (!stateId) { + roles = process.states[process.states.length - 2].roles; + } else { + roles = process.states.find(state => state.state_id === stateId)?.roles || null; + } if (!roles) { throw new Error('No roles found'); - } else { - console.log('roles found', roles); } let members: Set = new Set(); for (const role of Object.values(roles!)) { @@ -255,8 +258,7 @@ export default class Services { for (const address of sp_addresses) { // For now, we ignore our own device address, although there might be use cases for having a secret with ourselves if (address === myAddress) continue; - const sharedSecret = await this.getSecretForAddress(address); - if (!sharedSecret) { + if (await this.getSecretForAddress(address) === null) { unconnectedAddresses.add(address); } } @@ -420,7 +422,8 @@ export default class Services { ); if (result.updated_process) { - await this.checkConnections(result.updated_process); + console.log('created process:', result.updated_process); + await this.checkConnections(result.updated_process.current_process); return(result); } else { throw new Error('Empty updated_process in createProcessReturn'); @@ -448,7 +451,7 @@ export default class Services { try { const result = this.sdkClient.update_process(process, encodedPrivateData, roles, encodedPublicData, this.getAllMembers()); if (result.updated_process) { - await this.checkConnections(result.updated_process); + await this.checkConnections(result.updated_process.current_process); return(result); } else { throw new Error('Empty updated_process in updateProcessReturn'); @@ -462,15 +465,11 @@ export default class Services { const process = await this.getProcess(processId); if (!process) { throw new Error('Unknown process'); + } else { + await this.checkConnections(process); } try { - const result = this.sdkClient.create_update_message(process, stateId, this.getAllMembers()); - if (result.updated_process) { - await this.checkConnections(result.updated_process); - return(result); - } else { - throw new Error('Empty updated_process in createPrdUpdateReturn'); - } + return this.sdkClient.create_update_message(process, stateId, this.getAllMembers()); } catch (e) { throw new Error(`Failed to create prd update: ${e}`); } @@ -496,7 +495,7 @@ export default class Services { try { const result = this.sdkClient.validate_state(process, stateId, this.getAllMembers()); if (result.updated_process) { - await this.checkConnections(result.updated_process); + await this.checkConnections(result.updated_process.current_process); return(result); } else { throw new Error('Empty updated_process in approveChangeReturn'); @@ -1462,6 +1461,7 @@ export default class Services { } if (newStates.length != 0) { + await this.checkConnections(existing); await this.requestDataFromPeers(processId, newStates, newRoles); } // Otherwise we're probably just in the initial loading at page initialization