From 2026232b15354c8a4e5af799c6508420e5eb1178 Mon Sep 17 00:00:00 2001 From: Thura Moe Myint Date: Tue, 21 Jan 2025 04:56:28 +0700 Subject: [PATCH] SHARD-1137: Fix getTxTimestampBinary bucket size (#367) * SHARD-1137: Fix getTxTimestampBinary size * SHARD-1137: Fix getTxTimestampBinary size * SHARD-1137: Fix getTxTimestampBinary size --- src/config/server.ts | 1 + src/shardus/shardus-types.ts | 1 + src/state-manager/TransactionConsensus.ts | 21 ++++++++++++++++++++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/config/server.ts b/src/config/server.ts index 66215fc8f..32834ba73 100644 --- a/src/config/server.ts +++ b/src/config/server.ts @@ -175,6 +175,7 @@ const SERVER_CONFIG: StrictServerConfiguration = { networkTransactionsToProcessPerCycle: 20, getTxTimestampTimeoutOffset: 0, dropNGTByGossipEnabled: false, + timestampCacheFixSize: 10000 }, ip: { externalIp: '0.0.0.0', diff --git a/src/shardus/shardus-types.ts b/src/shardus/shardus-types.ts index 4d991a2d4..c773ad3c6 100644 --- a/src/shardus/shardus-types.ts +++ b/src/shardus/shardus-types.ts @@ -986,6 +986,7 @@ export interface ServerConfiguration { getTxTimestampTimeoutOffset?: number // default timeout is 5 seconds so this can be used to add or subtract time from that /** allow dropping NGTs by hitting a single node's endpoint and the drop mesage being sent to other nodes by gossip */ dropNGTByGossipEnabled: boolean + timestampCacheFixSize: number } /** Server IP configuration */ ip?: { diff --git a/src/state-manager/TransactionConsensus.ts b/src/state-manager/TransactionConsensus.ts index 89cf7ee85..f53efb9a8 100644 --- a/src/state-manager/TransactionConsensus.ts +++ b/src/state-manager/TransactionConsensus.ts @@ -1811,7 +1811,21 @@ class TransactionConsenus { nestedCountersInstance.countEvent('consensus', 'get_tx_timestamp found tx timestamp in cacheById') return tsReceipt } - + // Ensure the cycleCounter is within the acceptable range relative to lastest cycle number. If the absolute difference is greater than 1, return null to indicate an invalid state. + if (Math.abs(cycleCounter - CycleChain.newest.counter) > 1 ){ + return null + } + if (this.txTimestampCache.size >= Context.config.p2p.timestampCacheFixSize) { + const oldestCycleCounter = [...this.txTimestampCache.keys()][0] + const txMap = this.txTimestampCache.get(oldestCycleCounter); + if (txMap && txMap.size > 0) { + const oldestTxId = [...txMap.keys()][0] + txMap.delete(oldestTxId) + } + if (txMap.size === 0) { + this.txTimestampCache.delete(oldestCycleCounter); + } + } const tsReceipt: TimestampReceipt = { txId, cycleMarker, @@ -1830,6 +1844,11 @@ class TransactionConsenus { // cache to txId map this.txTimestampCache.get(signedTsReceipt.cycleCounter).set(txId, signedTsReceipt) if (Context.config.p2p.timestampCacheFix) { + if (this.txTimestampCacheByTxId.size >= Context.config.p2p.timestampCacheFixSize) { + const oldestTxId = [...this.txTimestampCacheByTxId.keys()][0] + this.txTimestampCacheByTxId.delete(oldestTxId) + this.seenTimestampRequests.delete(oldestTxId) + } // eslint-disable-next-line security/detect-object-injection this.txTimestampCacheByTxId.set(txId, signedTsReceipt) this.seenTimestampRequests.add(txId)