Skip to content

Commit cfd469d

Browse files
Calculate head/tail indicies directly
1 parent 8c1f5ad commit cfd469d

File tree

2 files changed

+12
-19
lines changed

2 files changed

+12
-19
lines changed

yarn-project/p2p/src/mem_pools/tx_pool/aztec_kv_tx_pool.test.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,26 @@ describe('KV TX pool', () => {
2020
const tx2 = mockTx(2);
2121
const tx3 = mockTx(3);
2222
const tx4 = mockTx(4);
23-
await txPool.addTxs([tx1, tx2, tx3, tx4]);
23+
const tx5 = mockTx(5);
24+
await txPool.addTxs([tx1, tx2, tx3, tx4, tx5]);
2425

2526
// delete two txs and assert that they are properly archived
2627
await txPool.deleteTxs([tx1.getTxHash(), tx2.getTxHash()]);
2728
expect(txPool.getArchivedTxByHash(tx1.getTxHash())).toEqual(tx1);
2829
expect(txPool.getArchivedTxByHash(tx2.getTxHash())).toEqual(tx2);
2930

30-
// delete another tx and assert that the first tx is purged and the new tx is archived
31+
// delete a single tx and assert that the first tx is purged and the new tx is archived
3132
await txPool.deleteTxs([tx3.getTxHash()]);
3233
expect(txPool.getArchivedTxByHash(tx1.getTxHash())).toBeUndefined();
3334
expect(txPool.getArchivedTxByHash(tx2.getTxHash())).toEqual(tx2);
3435
expect(txPool.getArchivedTxByHash(tx3.getTxHash())).toEqual(tx3);
3536

36-
// delete another tx and assert that the second tx is purged and the new tx is archived
37-
await txPool.deleteTxs([tx4.getTxHash()]);
37+
// delete multiple txs and assert that the old txs are purged and the new txs are archived
38+
await txPool.deleteTxs([tx4.getTxHash(), tx5.getTxHash()]);
3839
expect(txPool.getArchivedTxByHash(tx1.getTxHash())).toBeUndefined();
3940
expect(txPool.getArchivedTxByHash(tx2.getTxHash())).toBeUndefined();
40-
expect(txPool.getArchivedTxByHash(tx3.getTxHash())).toEqual(tx3);
41+
expect(txPool.getArchivedTxByHash(tx3.getTxHash())).toBeUndefined();
4142
expect(txPool.getArchivedTxByHash(tx4.getTxHash())).toEqual(tx4);
43+
expect(txPool.getArchivedTxByHash(tx5.getTxHash())).toEqual(tx5);
4244
});
4345
});

yarn-project/p2p/src/mem_pools/tx_pool/aztec_kv_tx_pool.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Tx, TxHash } from '@aztec/circuit-types';
22
import { type TxAddedToPoolStats } from '@aztec/circuit-types/stats';
33
import { ClientIvcProof } from '@aztec/circuits.js';
44
import { type Logger, createLogger } from '@aztec/foundation/log';
5-
import { type AztecKVStore, type AztecMap, type AztecMultiMap, type AztecSingleton } from '@aztec/kv-store';
5+
import { type AztecKVStore, type AztecMap, type AztecMultiMap } from '@aztec/kv-store';
66
import { type TelemetryClient, getTelemetryClient } from '@aztec/telemetry-client';
77

88
import { PoolInstrumentation, PoolName } from '../instrumentation.js';
@@ -33,12 +33,6 @@ export class AztecKVTxPool implements TxPool {
3333
/** Indexes of the archived txs by insertion order. */
3434
#archivedTxIndices: AztecMap<number, string>;
3535

36-
/** Index of the most recently inserted archived tx. */
37-
#archivedTxHead: AztecSingleton<number>;
38-
39-
/** Index of the oldest archived tx. */
40-
#archivedTxTail: AztecSingleton<number>;
41-
4236
/** Number of txs to archive. */
4337
#archivedTxLimit: number;
4438

@@ -51,6 +45,7 @@ export class AztecKVTxPool implements TxPool {
5145
* @param store - A KV store for live txs in the pool.
5246
* @param archive - A KV store for archived txs.
5347
* @param telemetry - A telemetry client.
48+
* @param archivedTxLimit - The number of txs to archive.
5449
* @param log - A logger.
5550
*/
5651
constructor(
@@ -66,8 +61,6 @@ export class AztecKVTxPool implements TxPool {
6661

6762
this.#archivedTxs = archive.openMap('archivedTxs');
6863
this.#archivedTxIndices = archive.openMap('archivedTxIndices');
69-
this.#archivedTxHead = archive.openSingleton('archivedTxHead');
70-
this.#archivedTxTail = archive.openSingleton('archivedTxTail');
7164
this.#archivedTxLimit = archivedTxLimit;
7265

7366
this.#store = store;
@@ -273,8 +266,9 @@ export class AztecKVTxPool implements TxPool {
273266
*/
274267
private archiveTxs(txs: Tx[]): Promise<void> {
275268
return this.#archive.transaction(() => {
276-
let headIdx = this.#archivedTxHead.get() ?? 0;
277-
let tailIdx = this.#archivedTxTail.get() ?? 0;
269+
// calcualte the head and tail indices of the archived txs by insertion order.
270+
let headIdx = (this.#archivedTxIndices.entries({ limit: 1, reverse: true }).next().value?.[0] ?? -1) + 1;
271+
let tailIdx = this.#archivedTxIndices.entries({ limit: 1 }).next().value?.[0] ?? 0;
278272

279273
for (const tx of txs) {
280274
while (headIdx - tailIdx >= this.#archivedTxLimit) {
@@ -299,9 +293,6 @@ export class AztecKVTxPool implements TxPool {
299293
void this.#archivedTxIndices.set(headIdx, txHash);
300294
headIdx++;
301295
}
302-
303-
void this.#archivedTxHead.set(headIdx);
304-
void this.#archivedTxTail.set(tailIdx);
305296
});
306297
}
307298
}

0 commit comments

Comments
 (0)