@@ -2,16 +2,9 @@ import { Tx, TxHash } from '@aztec/circuit-types';
2
2
import { type TxAddedToPoolStats } from '@aztec/circuit-types/stats' ;
3
3
import { ClientIvcProof } from '@aztec/circuits.js' ;
4
4
import { type Logger , createLogger } from '@aztec/foundation/log' ;
5
- import {
6
- type AztecKVStore ,
7
- type AztecMap ,
8
- type AztecMapWithSize ,
9
- type AztecMultiMap ,
10
- type AztecSingleton ,
11
- } from '@aztec/kv-store' ;
5
+ import { type AztecKVStore , type AztecMap , type AztecMultiMap , type AztecSingleton } from '@aztec/kv-store' ;
12
6
import { type TelemetryClient , getTelemetryClient } from '@aztec/telemetry-client' ;
13
7
14
- import { getP2PConfigFromEnv } from '../../config.js' ;
15
8
import { PoolInstrumentation , PoolName } from '../instrumentation.js' ;
16
9
import { getPendingTxPriority } from './priority.js' ;
17
10
import { type TxPool } from './tx_pool.js' ;
@@ -31,8 +24,11 @@ export class AztecKVTxPool implements TxPool {
31
24
/** Index from tx priority (stored as hex) to its tx hash, filtered by pending txs. */
32
25
#pendingTxPriorityToHash: AztecMultiMap < string , string > ;
33
26
27
+ /** KV store for archived txs. */
28
+ #archive: AztecKVStore ;
29
+
34
30
/** Archived txs map for future lookup. */
35
- #archivedTxs: AztecMapWithSize < string , Buffer > ;
31
+ #archivedTxs: AztecMap < string , Buffer > ;
36
32
37
33
/** Indexes of the archived txs by insertion order. */
38
34
#archivedTxIndices: AztecMap < number , string > ;
@@ -52,26 +48,30 @@ export class AztecKVTxPool implements TxPool {
52
48
53
49
/**
54
50
* Class constructor for KV TxPool. Initiates our transaction pool as an AztecMap.
55
- * @param store - A KV store.
51
+ * @param store - A KV store for live txs in the pool.
52
+ * @param archive - A KV store for archived txs.
53
+ * @param telemetry - A telemetry client.
56
54
* @param log - A logger.
57
55
*/
58
56
constructor (
59
57
store : AztecKVStore ,
58
+ archive : AztecKVStore ,
60
59
telemetry : TelemetryClient = getTelemetryClient ( ) ,
61
- archivedTxLimit = getP2PConfigFromEnv ( ) . archivedTxLimit ,
60
+ archivedTxLimit : number = 0 ,
62
61
log = createLogger ( 'p2p:tx_pool' ) ,
63
62
) {
64
63
this . #txs = store . openMap ( 'txs' ) ;
65
64
this . #minedTxHashToBlock = store . openMap ( 'txHashToBlockMined' ) ;
66
65
this . #pendingTxPriorityToHash = store . openMultiMap ( 'pendingTxFeeToHash' ) ;
67
66
68
- this . #archivedTxs = store . openMapWithSize ( 'archivedTxs' ) ;
69
- this . #archivedTxIndices = store . openMap ( 'archivedTxIndicies ' ) ;
70
- this . #archivedTxHead = store . openSingleton ( 'archivedTxHead' ) ;
71
- this . #archivedTxTail = store . openSingleton ( 'archivedTxTail' ) ;
67
+ this . #archivedTxs = archive . openMap ( 'archivedTxs' ) ;
68
+ this . #archivedTxIndices = archive . openMap ( 'archivedTxIndices ' ) ;
69
+ this . #archivedTxHead = archive . openSingleton ( 'archivedTxHead' ) ;
70
+ this . #archivedTxTail = archive . openSingleton ( 'archivedTxTail' ) ;
72
71
this . #archivedTxLimit = archivedTxLimit ;
73
72
74
73
this . #store = store ;
74
+ this . #archive = archive ;
75
75
this . #log = log ;
76
76
this . #metrics = new PoolInstrumentation ( telemetry , PoolName . TX_POOL , ( ) => store . estimateSize ( ) ) ;
77
77
}
@@ -198,10 +198,6 @@ export class AztecKVTxPool implements TxPool {
198
198
void this . #pendingTxPriorityToHash. set ( getPendingTxPriority ( tx ) , key ) ;
199
199
this . #metrics. recordSize ( tx ) ;
200
200
}
201
-
202
- if ( this . #archivedTxLimit) {
203
- void this . archiveTx ( tx ) ;
204
- }
205
201
}
206
202
207
203
this . #metrics. recordAddedObjects ( pendingCount , 'pending' ) ;
@@ -217,7 +213,8 @@ export class AztecKVTxPool implements TxPool {
217
213
let pendingDeleted = 0 ;
218
214
let minedDeleted = 0 ;
219
215
220
- return this . #store. transaction ( ( ) => {
216
+ const archiveTxs : Promise < void > [ ] = [ ] ;
217
+ const poolTxs = this . #store. transaction ( ( ) => {
221
218
for ( const hash of txHashes ) {
222
219
const key = hash . toString ( ) ;
223
220
const tx = this . getTxByHash ( hash ) ;
@@ -233,6 +230,10 @@ export class AztecKVTxPool implements TxPool {
233
230
pendingDeleted ++ ;
234
231
}
235
232
233
+ if ( this . #archivedTxLimit) {
234
+ archiveTxs . push ( this . archiveTx ( tx ) ) ;
235
+ }
236
+
236
237
void this . #txs. delete ( key ) ;
237
238
void this . #minedTxHashToBlock. delete ( key ) ;
238
239
}
@@ -241,6 +242,13 @@ export class AztecKVTxPool implements TxPool {
241
242
this . #metrics. recordRemovedObjects ( pendingDeleted , 'pending' ) ;
242
243
this . #metrics. recordRemovedObjects ( minedDeleted , 'mined' ) ;
243
244
} ) ;
245
+
246
+ return poolTxs . then ( ( ) =>
247
+ archiveTxs . reduce (
248
+ ( archiveTx , remainingArchiveTxs ) => archiveTx . then ( ( ) => remainingArchiveTxs ) ,
249
+ Promise . resolve ( ) ,
250
+ ) ,
251
+ ) ;
244
252
}
245
253
246
254
/**
@@ -264,32 +272,35 @@ export class AztecKVTxPool implements TxPool {
264
272
}
265
273
266
274
/**
267
- * Archive a tx for future reference.
275
+ * Archives a tx for future reference. The number of archived txs is limited by the specified archivedTxLimit .
268
276
* @param tx - The transaction to archive.
269
277
*/
270
- private archiveTx ( tx : Tx ) {
271
- while ( this . #archivedTxs. size ( ) >= this . #archivedTxLimit) {
272
- const tailIdx = this . #archivedTxTail. get ( ) ?? 0 ;
273
- const txHash = this . #archivedTxIndices. get ( tailIdx ) ;
274
- if ( txHash ) {
275
- void this . #archivedTxs. delete ( txHash ) ;
276
- void this . #archivedTxIndices. delete ( tailIdx ) ;
278
+ private archiveTx ( tx : Tx ) : Promise < void > {
279
+ return this . #archive. transaction ( ( ) => {
280
+ let headIdx = this . #archivedTxHead. get ( ) ?? 0 ;
281
+ let tailIdx = this . #archivedTxTail. get ( ) ?? 0 ;
282
+
283
+ while ( headIdx - tailIdx >= this . #archivedTxLimit) {
284
+ const txHash = this . #archivedTxIndices. get ( tailIdx ) ;
285
+ if ( txHash ) {
286
+ void this . #archivedTxs. delete ( txHash ) ;
287
+ void this . #archivedTxIndices. delete ( tailIdx ) ;
288
+ }
289
+ void this . #archivedTxTail. set ( ++ tailIdx ) ;
277
290
}
278
- void this . #archivedTxTail. set ( tailIdx + 1 ) ;
279
- }
280
291
281
- const archivedTx : Tx = new Tx (
282
- tx . data ,
283
- ClientIvcProof . empty ( ) ,
284
- tx . unencryptedLogs ,
285
- tx . contractClassLogs ,
286
- tx . enqueuedPublicFunctionCalls ,
287
- tx . publicTeardownFunctionCall ,
288
- ) ;
289
- const txHash = tx . getTxHash ( ) . toString ( ) ;
290
- void this . #archivedTxs. set ( txHash , archivedTx . toBuffer ( ) ) ;
291
- const headIdx = this . #archivedTxHead . get ( ) ?? 0 ;
292
- void this . #archivedTxIndices . set ( headIdx , txHash ) ;
293
- void this . #archivedTxHead . set ( headIdx + 1 ) ;
292
+ const archivedTx : Tx = new Tx (
293
+ tx . data ,
294
+ ClientIvcProof . empty ( ) ,
295
+ tx . unencryptedLogs ,
296
+ tx . contractClassLogs ,
297
+ tx . enqueuedPublicFunctionCalls ,
298
+ tx . publicTeardownFunctionCall ,
299
+ ) ;
300
+ const txHash = tx . getTxHash ( ) . toString ( ) ;
301
+ void this . #archivedTxs. set ( txHash , archivedTx . toBuffer ( ) ) ;
302
+ void this . #archivedTxIndices . set ( headIdx , txHash ) ;
303
+ void this . #archivedTxHead . set ( ++ headIdx ) ;
304
+ } ) ;
294
305
}
295
306
}
0 commit comments