Fix checkConnections, also check more aggressively

This commit is contained in:
Sosthene 2025-09-10 11:30:51 +02:00
parent 0d934e7b6e
commit 5fc485e233
2 changed files with 18 additions and 16 deletions

View File

@ -376,6 +376,8 @@ export async function registerAllListeners() {
} }
const state = services.getStateFromId(process, stateId); const state = services.getStateFromId(process, stateId);
await services.checkConnections(process, stateId);
let res: Record<string, any> = {}; let res: Record<string, any> = {};
if (state) { if (state) {
// Decrypt all the data we have the key for // Decrypt all the data we have the key for

View File

@ -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 // 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 // We will take the roles from the last state, wheter it's commited or not
public async checkConnections(process: Process): Promise<void> { public async checkConnections(process: Process, stateId: string | null = null): Promise<void> {
if (process.states.length < 2) { if (process.states.length < 2) {
throw new Error('Process doesn\'t have any state yet'); throw new Error('Process doesn\'t have any state yet');
} }
let roles = process.states[process.states.length - 2].roles; let roles: Record<string, RoleDefinition> | 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) { if (!roles) {
throw new Error('No roles found'); throw new Error('No roles found');
} else {
console.log('roles found', roles);
} }
let members: Set<Member> = new Set(); let members: Set<Member> = new Set();
for (const role of Object.values(roles!)) { for (const role of Object.values(roles!)) {
@ -255,8 +258,7 @@ export default class Services {
for (const address of sp_addresses) { 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 // For now, we ignore our own device address, although there might be use cases for having a secret with ourselves
if (address === myAddress) continue; if (address === myAddress) continue;
const sharedSecret = await this.getSecretForAddress(address); if (await this.getSecretForAddress(address) === null) {
if (!sharedSecret) {
unconnectedAddresses.add(address); unconnectedAddresses.add(address);
} }
} }
@ -420,7 +422,8 @@ export default class Services {
); );
if (result.updated_process) { 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); return(result);
} else { } else {
throw new Error('Empty updated_process in createProcessReturn'); throw new Error('Empty updated_process in createProcessReturn');
@ -448,7 +451,7 @@ export default class Services {
try { try {
const result = this.sdkClient.update_process(process, encodedPrivateData, roles, encodedPublicData, this.getAllMembers()); const result = this.sdkClient.update_process(process, encodedPrivateData, roles, encodedPublicData, this.getAllMembers());
if (result.updated_process) { if (result.updated_process) {
await this.checkConnections(result.updated_process); await this.checkConnections(result.updated_process.current_process);
return(result); return(result);
} else { } else {
throw new Error('Empty updated_process in updateProcessReturn'); throw new Error('Empty updated_process in updateProcessReturn');
@ -462,15 +465,11 @@ export default class Services {
const process = await this.getProcess(processId); const process = await this.getProcess(processId);
if (!process) { if (!process) {
throw new Error('Unknown process'); throw new Error('Unknown process');
} else {
await this.checkConnections(process);
} }
try { try {
const result = this.sdkClient.create_update_message(process, stateId, this.getAllMembers()); return 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');
}
} catch (e) { } catch (e) {
throw new Error(`Failed to create prd update: ${e}`); throw new Error(`Failed to create prd update: ${e}`);
} }
@ -496,7 +495,7 @@ export default class Services {
try { try {
const result = this.sdkClient.validate_state(process, stateId, this.getAllMembers()); const result = this.sdkClient.validate_state(process, stateId, this.getAllMembers());
if (result.updated_process) { if (result.updated_process) {
await this.checkConnections(result.updated_process); await this.checkConnections(result.updated_process.current_process);
return(result); return(result);
} else { } else {
throw new Error('Empty updated_process in approveChangeReturn'); throw new Error('Empty updated_process in approveChangeReturn');
@ -1462,6 +1461,7 @@ export default class Services {
} }
if (newStates.length != 0) { if (newStates.length != 0) {
await this.checkConnections(existing);
await this.requestDataFromPeers(processId, newStates, newRoles); await this.requestDataFromPeers(processId, newStates, newRoles);
} }
// Otherwise we're probably just in the initial loading at page initialization // Otherwise we're probably just in the initial loading at page initialization