From 3e6d38656d773cde85615f6572b4c72b31ce74c8 Mon Sep 17 00:00:00 2001 From: Mononaut Date: Thu, 11 Apr 2024 08:09:28 +0000 Subject: [PATCH] Add sequence number to mempool block updates --- backend/src/api/websocket-handler.ts | 12 ++++++++++++ frontend/src/app/services/state.service.ts | 1 + frontend/src/app/services/websocket.service.ts | 9 ++++++++- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/backend/src/api/websocket-handler.ts b/backend/src/api/websocket-handler.ts index 621f2996a..d4ff7efe3 100644 --- a/backend/src/api/websocket-handler.ts +++ b/backend/src/api/websocket-handler.ts @@ -54,6 +54,7 @@ class WebsocketHandler { private socketData: { [key: string]: string } = {}; private serializedInitData: string = '{}'; private lastRbfSummary: ReplacementInfo[] | null = null; + private mempoolSequence: number = 0; constructor() { } @@ -317,6 +318,7 @@ class WebsocketHandler { const mBlocksWithTransactions = mempoolBlocks.getMempoolBlocksWithTransactions(); response['projected-block-transactions'] = JSON.stringify({ index: index, + sequence: this.mempoolSequence, blockTransactions: (mBlocksWithTransactions[index]?.transactions || []).map(mempoolBlocks.compressTx), }); } else { @@ -602,6 +604,10 @@ class WebsocketHandler { const addressCache = this.makeAddressCache(newTransactions); const removedAddressCache = this.makeAddressCache(deletedTransactions); + if (memPool.isInSync()) { + this.mempoolSequence++; + } + // TODO - Fix indentation after PR is merged for (const server of this.webSocketServers) { server.clients.forEach(async (client) => { @@ -825,6 +831,7 @@ class WebsocketHandler { if (mBlockDeltas[index]) { response['projected-block-transactions'] = getCachedResponse(`projected-block-transactions-${index}`, { index: index, + sequence: this.mempoolSequence, delta: mBlockDeltas[index], }); } @@ -993,6 +1000,9 @@ class WebsocketHandler { return responseCache[key]; } + if (memPool.isInSync()) { + this.mempoolSequence++; + } // TODO - Fix indentation after PR is merged for (const server of this.webSocketServers) { @@ -1162,11 +1172,13 @@ class WebsocketHandler { if (mBlockDeltas[index].added.length > (mBlocksWithTransactions[index]?.transactions.length / 2)) { response['projected-block-transactions'] = getCachedResponse(`projected-block-transactions-full-${index}`, { index: index, + sequence: this.mempoolSequence, blockTransactions: mBlocksWithTransactions[index].transactions.map(mempoolBlocks.compressTx), }); } else { response['projected-block-transactions'] = getCachedResponse(`projected-block-transactions-delta-${index}`, { index: index, + sequence: this.mempoolSequence, delta: mBlockDeltas[index], }); } diff --git a/frontend/src/app/services/state.service.ts b/frontend/src/app/services/state.service.ts index 6d99d278a..174388b7e 100644 --- a/frontend/src/app/services/state.service.ts +++ b/frontend/src/app/services/state.service.ts @@ -96,6 +96,7 @@ export class StateService { env: Env; latestBlockHeight = -1; blocks: BlockExtended[] = []; + mempoolSequence: number; backend$ = new BehaviorSubject<'esplora' | 'electrum' | 'none'>('esplora'); networkChanged$ = new ReplaySubject(1); diff --git a/frontend/src/app/services/websocket.service.ts b/frontend/src/app/services/websocket.service.ts index de5170cbe..414f60bc5 100644 --- a/frontend/src/app/services/websocket.service.ts +++ b/frontend/src/app/services/websocket.service.ts @@ -400,9 +400,16 @@ export class WebsocketService { if (response['projected-block-transactions']) { if (response['projected-block-transactions'].index == this.trackingMempoolBlock) { if (response['projected-block-transactions'].blockTransactions) { + this.stateService.mempoolSequence = response['projected-block-transactions'].sequence; this.stateService.mempoolBlockTransactions$.next(response['projected-block-transactions'].blockTransactions.map(uncompressTx)); } else if (response['projected-block-transactions'].delta) { - this.stateService.mempoolBlockDelta$.next(uncompressDeltaChange(response['projected-block-transactions'].delta)); + if (this.stateService.mempoolSequence && response['projected-block-transactions'].sequence !== this.stateService.mempoolSequence + 1) { + this.stateService.mempoolSequence = 0; + this.startTrackMempoolBlock(this.trackingMempoolBlock, true); + } else { + this.stateService.mempoolSequence = response['projected-block-transactions'].sequence; + this.stateService.mempoolBlockDelta$.next(uncompressDeltaChange(response['projected-block-transactions'].delta)); + } } } }