Skip to content

Commit abc4623

Browse files
authored
fix: remove all txs from a failed epoch (#13771)
Fixes #13723 To be reverted in the future, tracked here: #13770
1 parent 7e72420 commit abc4623

File tree

3 files changed

+61
-14
lines changed

3 files changed

+61
-14
lines changed

yarn-project/end-to-end/src/e2e_block_building.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,9 @@ describe('e2e_block_building', () => {
529529
});
530530
});
531531

532-
describe('reorgs', () => {
532+
// Due to #13723, this test is disabled.
533+
// TODO: bring back once fixed: #13770
534+
describe.skip('reorgs', () => {
533535
let contract: StatefulTestContract;
534536
let cheatCodes: CheatCodes;
535537
let ownerAddress: AztecAddress;

yarn-project/p2p/src/client/p2p_client.test.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,35 @@ describe('In-Memory P2P Client', () => {
189189
});
190190

191191
describe('Chain prunes', () => {
192+
it('deletes transactions mined in pruned blocks', async () => {
193+
client = new P2PClient(P2PClientType.Full, kvStore, blockSource, mempools, p2pService, {
194+
keepProvenTxsInPoolFor: 10,
195+
});
196+
blockSource.setProvenBlockNumber(0);
197+
await client.start();
198+
199+
// Create two transactions:
200+
// 1. A transaction mined in block 95 (which will be pruned)
201+
// 2. A transaction mined in block 90 (which will remain)
202+
const txMinedInPrunedBlock = await mockTx();
203+
const txMinedInKeptBlock = await mockTx();
204+
205+
// Mock the mined transactions
206+
txPool.getMinedTxHashes.mockResolvedValue([
207+
[await txMinedInPrunedBlock.getTxHash(), 95],
208+
[await txMinedInKeptBlock.getTxHash(), 90],
209+
]);
210+
211+
txPool.getAllTxs.mockResolvedValue([txMinedInPrunedBlock, txMinedInKeptBlock]);
212+
213+
// Prune the chain back to block 90
214+
blockSource.removeBlocks(10);
215+
await client.sync();
216+
217+
// Verify only the transaction mined in the pruned block is deleted
218+
expect(txPool.deleteTxs).toHaveBeenCalledWith([await txMinedInPrunedBlock.getTxHash()]);
219+
await client.stop();
220+
});
192221
it('moves the tips on a chain reorg', async () => {
193222
blockSource.setProvenBlockNumber(0);
194223
await client.start();
@@ -246,7 +275,9 @@ describe('In-Memory P2P Client', () => {
246275
await client.stop();
247276
});
248277

249-
it('moves mined and valid txs back to the pending set', async () => {
278+
// NOTE: skipping as we currently delete all mined txs within the epoch when pruning
279+
// TODO: bring back once fixed: #13770
280+
it.skip('moves mined and valid txs back to the pending set', async () => {
250281
client = new P2PClient(P2PClientType.Full, kvStore, blockSource, mempools, p2pService, {
251282
keepProvenTxsInPoolFor: 10,
252283
});

yarn-project/p2p/src/client/p2p_client.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -752,36 +752,50 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
752752
* @param latestBlock - The block number the chain was pruned to.
753753
*/
754754
private async handlePruneL2Blocks(latestBlock: number): Promise<void> {
755-
const txsToDelete: TxHash[] = [];
755+
// NOTE: temporary fix for alphanet, deleting ALL txs that were in the epoch from the pool #13723
756+
// TODO: undo once fixed: #13770
757+
const txsToDelete = new Set<TxHash>();
758+
const minedTxs = await this.txPool.getMinedTxHashes();
759+
for (const [txHash, blockNumber] of minedTxs) {
760+
if (blockNumber > latestBlock) {
761+
txsToDelete.add(txHash);
762+
}
763+
}
764+
765+
// Find transactions that reference pruned blocks in their historical header
756766
for (const tx of await this.txPool.getAllTxs()) {
757767
// every tx that's been generated against a block that has now been pruned is no longer valid
758768
if (tx.data.constants.historicalHeader.globalVariables.blockNumber.toNumber() > latestBlock) {
759-
txsToDelete.push(await tx.getTxHash());
769+
const txHash = await tx.getTxHash();
770+
txsToDelete.add(txHash);
760771
}
761772
}
762773

763774
this.log.info(
764775
`Detected chain prune. Removing invalid txs count=${
765-
txsToDelete.length
776+
txsToDelete.size
766777
} newLatestBlock=${latestBlock} previousLatestBlock=${await this.getSyncedLatestBlockNum()}`,
767778
);
768779

769780
// delete invalid txs (both pending and mined)
770-
await this.txPool.deleteTxs(txsToDelete);
781+
await this.txPool.deleteTxs(Array.from(txsToDelete));
771782

772783
// everything left in the mined set was built against a block on the proven chain so its still valid
773784
// move back to pending the txs that were reorged out of the chain
774785
// NOTE: we can't move _all_ txs back to pending because the tx pool could keep hold of mined txs for longer
775786
// (see this.keepProvenTxsFor)
776-
const txsToMoveToPending: TxHash[] = [];
777-
for (const [txHash, blockNumber] of await this.txPool.getMinedTxHashes()) {
778-
if (blockNumber > latestBlock) {
779-
txsToMoveToPending.push(txHash);
780-
}
781-
}
782787

783-
this.log.info(`Moving ${txsToMoveToPending.length} mined txs back to pending`);
784-
await this.txPool.markMinedAsPending(txsToMoveToPending);
788+
// NOTE: given current fix for alphanet, the code below is redundant as all these txs will be deleted.
789+
// TODO: bring back once fixed: #13770
790+
// const txsToMoveToPending: TxHash[] = [];
791+
// for (const [txHash, blockNumber] of minedTxs) {
792+
// if (blockNumber > latestBlock) {
793+
// txsToMoveToPending.push(txHash);
794+
// }
795+
// }
796+
797+
// this.log.info(`Moving ${txsToMoveToPending.length} mined txs back to pending`);
798+
// await this.txPool.markMinedAsPending(txsToMoveToPending);
785799

786800
await this.synchedLatestBlockNumber.set(latestBlock);
787801
// no need to update block hashes, as they will be updated as new blocks are added

0 commit comments

Comments
 (0)