Skip to content

Commit 8c1f5ad

Browse files
Archive txs in a single DB write
1 parent 8f57a30 commit 8c1f5ad

File tree

2 files changed

+41
-32
lines changed

2 files changed

+41
-32
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ describe('KV TX pool', () => {
1919
const tx1 = mockTx(1);
2020
const tx2 = mockTx(2);
2121
const tx3 = mockTx(3);
22-
await txPool.addTxs([tx1, tx2, tx3]);
22+
const tx4 = mockTx(4);
23+
await txPool.addTxs([tx1, tx2, tx3, tx4]);
2324

2425
// delete two txs and assert that they are properly archived
2526
await txPool.deleteTxs([tx1.getTxHash(), tx2.getTxHash()]);
@@ -31,5 +32,12 @@ describe('KV TX pool', () => {
3132
expect(txPool.getArchivedTxByHash(tx1.getTxHash())).toBeUndefined();
3233
expect(txPool.getArchivedTxByHash(tx2.getTxHash())).toEqual(tx2);
3334
expect(txPool.getArchivedTxByHash(tx3.getTxHash())).toEqual(tx3);
35+
36+
// delete another tx and assert that the second tx is purged and the new tx is archived
37+
await txPool.deleteTxs([tx4.getTxHash()]);
38+
expect(txPool.getArchivedTxByHash(tx1.getTxHash())).toBeUndefined();
39+
expect(txPool.getArchivedTxByHash(tx2.getTxHash())).toBeUndefined();
40+
expect(txPool.getArchivedTxByHash(tx3.getTxHash())).toEqual(tx3);
41+
expect(txPool.getArchivedTxByHash(tx4.getTxHash())).toEqual(tx4);
3442
});
3543
});

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

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,14 @@ export class AztecKVTxPool implements TxPool {
207207
/**
208208
* Deletes transactions from the pool. Tx hashes that are not present are ignored.
209209
* @param txHashes - An array of tx hashes to be removed from the tx pool.
210-
* @returns The number of transactions that was deleted from the pool.
210+
* @returns Empty promise.
211211
*/
212212
public deleteTxs(txHashes: TxHash[]): Promise<void> {
213213
let pendingDeleted = 0;
214214
let minedDeleted = 0;
215215

216-
const archiveTxs: Promise<void>[] = [];
217-
const poolTxs = this.#store.transaction(() => {
216+
const deletedTxs: Tx[] = [];
217+
const poolDbTx = this.#store.transaction(() => {
218218
for (const hash of txHashes) {
219219
const key = hash.toString();
220220
const tx = this.getTxByHash(hash);
@@ -231,7 +231,7 @@ export class AztecKVTxPool implements TxPool {
231231
}
232232

233233
if (this.#archivedTxLimit) {
234-
archiveTxs.push(this.archiveTx(tx));
234+
deletedTxs.push(tx);
235235
}
236236

237237
void this.#txs.delete(key);
@@ -243,12 +243,7 @@ export class AztecKVTxPool implements TxPool {
243243
this.#metrics.recordRemovedObjects(minedDeleted, 'mined');
244244
});
245245

246-
return poolTxs.then(() =>
247-
archiveTxs.reduce(
248-
(archiveTx, remainingArchiveTxs) => archiveTx.then(() => remainingArchiveTxs),
249-
Promise.resolve(),
250-
),
251-
);
246+
return this.#archivedTxLimit ? poolDbTx.then(() => this.archiveTxs(deletedTxs)) : poolDbTx;
252247
}
253248

254249
/**
@@ -272,35 +267,41 @@ export class AztecKVTxPool implements TxPool {
272267
}
273268

274269
/**
275-
* Archives a tx for future reference. The number of archived txs is limited by the specified archivedTxLimit.
276-
* @param tx - The transaction to archive.
270+
* Archives a list of txs for future reference. The number of archived txs is limited by the specified archivedTxLimit.
271+
* @param txs - The list of transactions to archive.
272+
* @returns Empty promise.
277273
*/
278-
private archiveTx(tx: Tx): Promise<void> {
274+
private archiveTxs(txs: Tx[]): Promise<void> {
279275
return this.#archive.transaction(() => {
280276
let headIdx = this.#archivedTxHead.get() ?? 0;
281277
let tailIdx = this.#archivedTxTail.get() ?? 0;
282278

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);
279+
for (const tx of txs) {
280+
while (headIdx - tailIdx >= this.#archivedTxLimit) {
281+
const txHash = this.#archivedTxIndices.get(tailIdx);
282+
if (txHash) {
283+
void this.#archivedTxs.delete(txHash);
284+
void this.#archivedTxIndices.delete(tailIdx);
285+
}
286+
tailIdx++;
288287
}
289-
void this.#archivedTxTail.set(++tailIdx);
288+
289+
const archivedTx: Tx = new Tx(
290+
tx.data,
291+
ClientIvcProof.empty(),
292+
tx.unencryptedLogs,
293+
tx.contractClassLogs,
294+
tx.enqueuedPublicFunctionCalls,
295+
tx.publicTeardownFunctionCall,
296+
);
297+
const txHash = tx.getTxHash().toString();
298+
void this.#archivedTxs.set(txHash, archivedTx.toBuffer());
299+
void this.#archivedTxIndices.set(headIdx, txHash);
300+
headIdx++;
290301
}
291302

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);
303+
void this.#archivedTxHead.set(headIdx);
304+
void this.#archivedTxTail.set(tailIdx);
304305
});
305306
}
306307
}

0 commit comments

Comments
 (0)