From 8e9d7f0c76b35296e26a6c5d34b8ee2983490f36 Mon Sep 17 00:00:00 2001 From: Sosthene Date: Tue, 12 Aug 2025 12:52:14 +0200 Subject: [PATCH] remove wasm processes cache --- src/services/service.ts | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/src/services/service.ts b/src/services/service.ts index d225bdc..7e1617a 100755 --- a/src/services/service.ts +++ b/src/services/service.ts @@ -541,10 +541,40 @@ export default class Services { // await this.saveCipherTxToDb(parsedTx) } - async parseNewTx(tx: string) { + async parseNewTx(newTxMsg: string) { + const parsedMsg: NewTxMessage = JSON.parse(newTxMsg); + if (parsedMsg.error !== null) { + console.error('Received error in new tx message:', parsedMsg.error); + return; + } + const membersList = this.getAllMembers(); try { - const parsedTx = this.sdkClient.parse_new_tx(tx, 0, membersList); + // Does the transaction spend the tip of a process? + const prevouts = this.sdkClient.get_prevouts(parsedMsg.transaction); + console.log('prevouts:', prevouts); + for (const process of Object.values(this.processesCache)) { + const tip = process.states[process.states.length - 1].commited_in; + if (prevouts.includes(tip)) { + const processId = process.states[0].commited_in; + const newTip = this.sdkClient.get_txid(parsedMsg.transaction); + console.log('Transaction', newTip, 'spends the tip of process', processId); + // We take the data out of the output + const newStateId = this.sdkClient.get_new_state_id(parsedMsg.transaction); + console.log('newStateId:', newStateId); + // We update the relevant process + const updatedProcess = this.sdkClient.process_commit_new_state(process, newStateId, newTip); + this.processesCache[processId] = updatedProcess; + console.log('updatedProcess:', updatedProcess); + break; + } + } + } catch (e) { + console.error('Failed to parse new tx for commitments:', e); + } + + try { + const parsedTx = this.sdkClient.parse_new_tx(parsedMsg.transaction, 0, membersList); if (parsedTx) { try { await this.handleApiReturn(parsedTx); @@ -555,7 +585,7 @@ export default class Services { } } } catch (e) { - console.trace(e); + console.debug(e); } } @@ -1060,7 +1090,7 @@ export default class Services { await this.restoreProcessesFromDB(); } - // Restore process in wasm with persistent storage + // Restore processes cache from persistent storage public async restoreProcessesFromDB() { const db = await Database.getInstance(); try { @@ -1068,7 +1098,6 @@ export default class Services { if (processes && Object.keys(processes).length != 0) { console.log(`Restoring ${Object.keys(processes).length} processes`); this.processesCache = processes; - this.sdkClient.set_process_cache(processes); } else { console.log('No processes to restore!'); }