Skip to content

Commit

Permalink
Calculate head/tail indicies directly
Browse files Browse the repository at this point in the history
  • Loading branch information
natebeauregard committed Jan 16, 2025
1 parent 8c1f5ad commit cfd469d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 19 deletions.
12 changes: 7 additions & 5 deletions yarn-project/p2p/src/mem_pools/tx_pool/aztec_kv_tx_pool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,26 @@ describe('KV TX pool', () => {
const tx2 = mockTx(2);
const tx3 = mockTx(3);
const tx4 = mockTx(4);
await txPool.addTxs([tx1, tx2, tx3, tx4]);
const tx5 = mockTx(5);
await txPool.addTxs([tx1, tx2, tx3, tx4, tx5]);

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

// delete another tx and assert that the first tx is purged and the new tx is archived
// delete a single tx and assert that the first tx is purged and the new tx is archived
await txPool.deleteTxs([tx3.getTxHash()]);
expect(txPool.getArchivedTxByHash(tx1.getTxHash())).toBeUndefined();
expect(txPool.getArchivedTxByHash(tx2.getTxHash())).toEqual(tx2);
expect(txPool.getArchivedTxByHash(tx3.getTxHash())).toEqual(tx3);

// delete another tx and assert that the second tx is purged and the new tx is archived
await txPool.deleteTxs([tx4.getTxHash()]);
// delete multiple txs and assert that the old txs are purged and the new txs are archived
await txPool.deleteTxs([tx4.getTxHash(), tx5.getTxHash()]);
expect(txPool.getArchivedTxByHash(tx1.getTxHash())).toBeUndefined();
expect(txPool.getArchivedTxByHash(tx2.getTxHash())).toBeUndefined();
expect(txPool.getArchivedTxByHash(tx3.getTxHash())).toEqual(tx3);
expect(txPool.getArchivedTxByHash(tx3.getTxHash())).toBeUndefined();
expect(txPool.getArchivedTxByHash(tx4.getTxHash())).toEqual(tx4);
expect(txPool.getArchivedTxByHash(tx5.getTxHash())).toEqual(tx5);
});
});
19 changes: 5 additions & 14 deletions yarn-project/p2p/src/mem_pools/tx_pool/aztec_kv_tx_pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Tx, TxHash } from '@aztec/circuit-types';
import { type TxAddedToPoolStats } from '@aztec/circuit-types/stats';
import { ClientIvcProof } from '@aztec/circuits.js';
import { type Logger, createLogger } from '@aztec/foundation/log';
import { type AztecKVStore, type AztecMap, type AztecMultiMap, type AztecSingleton } from '@aztec/kv-store';
import { type AztecKVStore, type AztecMap, type AztecMultiMap } from '@aztec/kv-store';
import { type TelemetryClient, getTelemetryClient } from '@aztec/telemetry-client';

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

/** Index of the most recently inserted archived tx. */
#archivedTxHead: AztecSingleton<number>;

/** Index of the oldest archived tx. */
#archivedTxTail: AztecSingleton<number>;

/** Number of txs to archive. */
#archivedTxLimit: number;

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

this.#archivedTxs = archive.openMap('archivedTxs');
this.#archivedTxIndices = archive.openMap('archivedTxIndices');
this.#archivedTxHead = archive.openSingleton('archivedTxHead');
this.#archivedTxTail = archive.openSingleton('archivedTxTail');
this.#archivedTxLimit = archivedTxLimit;

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

for (const tx of txs) {
while (headIdx - tailIdx >= this.#archivedTxLimit) {
Expand All @@ -299,9 +293,6 @@ export class AztecKVTxPool implements TxPool {
void this.#archivedTxIndices.set(headIdx, txHash);
headIdx++;
}

void this.#archivedTxHead.set(headIdx);
void this.#archivedTxTail.set(tailIdx);
});
}
}

0 comments on commit cfd469d

Please sign in to comment.