@aztec/p2p 0.0.1-commit.fce3e4f → 0.0.1-commit.fffb133c
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dest/client/factory.d.ts +2 -2
- package/dest/client/factory.d.ts.map +1 -1
- package/dest/client/factory.js +2 -3
- package/dest/client/interface.d.ts +18 -5
- package/dest/client/interface.d.ts.map +1 -1
- package/dest/client/p2p_client.d.ts +16 -19
- package/dest/client/p2p_client.d.ts.map +1 -1
- package/dest/client/p2p_client.js +460 -127
- package/dest/config.d.ts +4 -7
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +10 -13
- package/dest/mem_pools/attestation_pool/attestation_pool.d.ts +61 -42
- package/dest/mem_pools/attestation_pool/attestation_pool.d.ts.map +1 -1
- package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.d.ts +1 -1
- package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.d.ts.map +1 -1
- package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.js +239 -265
- package/dest/mem_pools/attestation_pool/kv_attestation_pool.d.ts +21 -18
- package/dest/mem_pools/attestation_pool/kv_attestation_pool.d.ts.map +1 -1
- package/dest/mem_pools/attestation_pool/kv_attestation_pool.js +114 -109
- package/dest/mem_pools/attestation_pool/memory_attestation_pool.d.ts +17 -16
- package/dest/mem_pools/attestation_pool/memory_attestation_pool.d.ts.map +1 -1
- package/dest/mem_pools/attestation_pool/memory_attestation_pool.js +89 -128
- package/dest/mem_pools/attestation_pool/mocks.d.ts +11 -8
- package/dest/mem_pools/attestation_pool/mocks.d.ts.map +1 -1
- package/dest/mem_pools/attestation_pool/mocks.js +17 -13
- package/dest/mem_pools/instrumentation.d.ts +7 -1
- package/dest/mem_pools/instrumentation.d.ts.map +1 -1
- package/dest/mem_pools/instrumentation.js +30 -12
- package/dest/mem_pools/interface.d.ts +3 -4
- package/dest/mem_pools/interface.d.ts.map +1 -1
- package/dest/mem_pools/tx_pool/aztec_kv_tx_pool.d.ts +37 -27
- package/dest/mem_pools/tx_pool/aztec_kv_tx_pool.d.ts.map +1 -1
- package/dest/mem_pools/tx_pool/aztec_kv_tx_pool.js +314 -335
- package/dest/mem_pools/tx_pool/eviction/eviction_manager.d.ts +32 -0
- package/dest/mem_pools/tx_pool/eviction/eviction_manager.d.ts.map +1 -0
- package/dest/mem_pools/tx_pool/eviction/eviction_manager.js +112 -0
- package/dest/mem_pools/tx_pool/eviction/eviction_strategy.d.ts +157 -0
- package/dest/mem_pools/tx_pool/eviction/eviction_strategy.d.ts.map +1 -0
- package/dest/mem_pools/tx_pool/eviction/eviction_strategy.js +52 -0
- package/dest/mem_pools/tx_pool/eviction/fee_payer_balance_eviction_rule.d.ts +16 -0
- package/dest/mem_pools/tx_pool/eviction/fee_payer_balance_eviction_rule.d.ts.map +1 -0
- package/dest/mem_pools/tx_pool/eviction/fee_payer_balance_eviction_rule.js +122 -0
- package/dest/mem_pools/tx_pool/eviction/invalid_txs_after_mining_rule.d.ts +17 -0
- package/dest/mem_pools/tx_pool/eviction/invalid_txs_after_mining_rule.d.ts.map +1 -0
- package/dest/mem_pools/tx_pool/eviction/invalid_txs_after_mining_rule.js +84 -0
- package/dest/mem_pools/tx_pool/eviction/invalid_txs_after_reorg_rule.d.ts +19 -0
- package/dest/mem_pools/tx_pool/eviction/invalid_txs_after_reorg_rule.d.ts.map +1 -0
- package/dest/mem_pools/tx_pool/eviction/invalid_txs_after_reorg_rule.js +78 -0
- package/dest/mem_pools/tx_pool/eviction/low_priority_eviction_rule.d.ts +26 -0
- package/dest/mem_pools/tx_pool/eviction/low_priority_eviction_rule.d.ts.map +1 -0
- package/dest/mem_pools/tx_pool/eviction/low_priority_eviction_rule.js +84 -0
- package/dest/mem_pools/tx_pool/eviction/nullifier_conflict_pre_add_rule.d.ts +25 -0
- package/dest/mem_pools/tx_pool/eviction/nullifier_conflict_pre_add_rule.d.ts.map +1 -0
- package/dest/mem_pools/tx_pool/eviction/nullifier_conflict_pre_add_rule.js +57 -0
- package/dest/mem_pools/tx_pool/index.d.ts +1 -2
- package/dest/mem_pools/tx_pool/index.d.ts.map +1 -1
- package/dest/mem_pools/tx_pool/index.js +0 -1
- package/dest/mem_pools/tx_pool/priority.d.ts +5 -1
- package/dest/mem_pools/tx_pool/priority.d.ts.map +1 -1
- package/dest/mem_pools/tx_pool/priority.js +6 -1
- package/dest/mem_pools/tx_pool/tx_pool.d.ts +11 -6
- package/dest/mem_pools/tx_pool/tx_pool.d.ts.map +1 -1
- package/dest/mem_pools/tx_pool/tx_pool_test_suite.d.ts +1 -1
- package/dest/mem_pools/tx_pool/tx_pool_test_suite.d.ts.map +1 -1
- package/dest/mem_pools/tx_pool/tx_pool_test_suite.js +30 -24
- package/dest/msg_validators/attestation_validator/attestation_validator.d.ts +4 -4
- package/dest/msg_validators/attestation_validator/attestation_validator.d.ts.map +1 -1
- package/dest/msg_validators/attestation_validator/attestation_validator.js +52 -19
- package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.d.ts +5 -5
- package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.d.ts.map +1 -1
- package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.js +18 -14
- package/dest/msg_validators/clock_tolerance.d.ts +21 -0
- package/dest/msg_validators/clock_tolerance.d.ts.map +1 -0
- package/dest/msg_validators/clock_tolerance.js +37 -0
- package/dest/msg_validators/index.d.ts +2 -2
- package/dest/msg_validators/index.d.ts.map +1 -1
- package/dest/msg_validators/index.js +1 -1
- package/dest/msg_validators/proposal_validator/block_proposal_validator.d.ts +9 -0
- package/dest/msg_validators/proposal_validator/block_proposal_validator.d.ts.map +1 -0
- package/dest/msg_validators/proposal_validator/block_proposal_validator.js +6 -0
- package/dest/msg_validators/proposal_validator/checkpoint_proposal_validator.d.ts +9 -0
- package/dest/msg_validators/proposal_validator/checkpoint_proposal_validator.d.ts.map +1 -0
- package/dest/msg_validators/proposal_validator/checkpoint_proposal_validator.js +6 -0
- package/dest/msg_validators/proposal_validator/index.d.ts +4 -0
- package/dest/msg_validators/proposal_validator/index.d.ts.map +1 -0
- package/dest/msg_validators/proposal_validator/index.js +3 -0
- package/dest/msg_validators/proposal_validator/proposal_validator.d.ts +13 -0
- package/dest/msg_validators/proposal_validator/proposal_validator.d.ts.map +1 -0
- package/dest/msg_validators/proposal_validator/proposal_validator.js +104 -0
- package/dest/msg_validators/proposal_validator/proposal_validator_test_suite.d.ts +23 -0
- package/dest/msg_validators/proposal_validator/proposal_validator_test_suite.d.ts.map +1 -0
- package/dest/msg_validators/proposal_validator/proposal_validator_test_suite.js +212 -0
- package/dest/msg_validators/tx_validator/archive_cache.d.ts +2 -2
- package/dest/msg_validators/tx_validator/archive_cache.d.ts.map +1 -1
- package/dest/msg_validators/tx_validator/block_header_validator.d.ts +2 -2
- package/dest/msg_validators/tx_validator/block_header_validator.d.ts.map +1 -1
- package/dest/msg_validators/tx_validator/block_header_validator.js +1 -1
- package/dest/msg_validators/tx_validator/data_validator.d.ts +1 -1
- package/dest/msg_validators/tx_validator/data_validator.d.ts.map +1 -1
- package/dest/msg_validators/tx_validator/factory.d.ts +4 -3
- package/dest/msg_validators/tx_validator/factory.d.ts.map +1 -1
- package/dest/msg_validators/tx_validator/factory.js +1 -1
- package/dest/msg_validators/tx_validator/fee_payer_balance.d.ts +10 -0
- package/dest/msg_validators/tx_validator/fee_payer_balance.d.ts.map +1 -0
- package/dest/msg_validators/tx_validator/fee_payer_balance.js +20 -0
- package/dest/msg_validators/tx_validator/gas_validator.d.ts +1 -1
- package/dest/msg_validators/tx_validator/gas_validator.d.ts.map +1 -1
- package/dest/msg_validators/tx_validator/gas_validator.js +8 -14
- package/dest/msg_validators/tx_validator/index.d.ts +2 -1
- package/dest/msg_validators/tx_validator/index.d.ts.map +1 -1
- package/dest/msg_validators/tx_validator/index.js +1 -0
- package/dest/msg_validators/tx_validator/metadata_validator.d.ts +2 -2
- package/dest/msg_validators/tx_validator/metadata_validator.d.ts.map +1 -1
- package/dest/msg_validators/tx_validator/size_validator.d.ts +6 -0
- package/dest/msg_validators/tx_validator/size_validator.d.ts.map +1 -0
- package/dest/msg_validators/tx_validator/size_validator.js +20 -0
- package/dest/msg_validators/tx_validator/test_utils.d.ts +2 -2
- package/dest/msg_validators/tx_validator/test_utils.d.ts.map +1 -1
- package/dest/msg_validators/tx_validator/timestamp_validator.d.ts +3 -2
- package/dest/msg_validators/tx_validator/timestamp_validator.d.ts.map +1 -1
- package/dest/services/dummy_service.d.ts +6 -2
- package/dest/services/dummy_service.d.ts.map +1 -1
- package/dest/services/dummy_service.js +3 -0
- package/dest/services/encoding.d.ts +1 -1
- package/dest/services/encoding.d.ts.map +1 -1
- package/dest/services/encoding.js +7 -6
- package/dest/services/libp2p/instrumentation.d.ts +1 -1
- package/dest/services/libp2p/instrumentation.d.ts.map +1 -1
- package/dest/services/libp2p/instrumentation.js +20 -73
- package/dest/services/libp2p/libp2p_service.d.ts +31 -14
- package/dest/services/libp2p/libp2p_service.d.ts.map +1 -1
- package/dest/services/libp2p/libp2p_service.js +722 -168
- package/dest/services/peer-manager/metrics.d.ts +6 -1
- package/dest/services/peer-manager/metrics.d.ts.map +1 -1
- package/dest/services/peer-manager/metrics.js +18 -21
- package/dest/services/peer-manager/peer_manager.d.ts +2 -2
- package/dest/services/peer-manager/peer_manager.d.ts.map +1 -1
- package/dest/services/peer-manager/peer_manager.js +4 -12
- package/dest/services/peer-manager/peer_scoring.d.ts +1 -1
- package/dest/services/peer-manager/peer_scoring.d.ts.map +1 -1
- package/dest/services/peer-manager/peer_scoring.js +2 -5
- package/dest/services/reqresp/connection-sampler/connection_sampler.d.ts +1 -1
- package/dest/services/reqresp/connection-sampler/connection_sampler.d.ts.map +1 -1
- package/dest/services/reqresp/constants.d.ts +12 -0
- package/dest/services/reqresp/constants.d.ts.map +1 -0
- package/dest/services/reqresp/constants.js +7 -0
- package/dest/services/reqresp/interface.d.ts +2 -2
- package/dest/services/reqresp/interface.d.ts.map +1 -1
- package/dest/services/reqresp/interface.js +1 -1
- package/dest/services/reqresp/metrics.d.ts +1 -1
- package/dest/services/reqresp/metrics.d.ts.map +1 -1
- package/dest/services/reqresp/metrics.js +5 -21
- package/dest/services/reqresp/protocols/auth.d.ts +2 -2
- package/dest/services/reqresp/protocols/auth.d.ts.map +1 -1
- package/dest/services/reqresp/protocols/auth.js +2 -2
- package/dest/services/reqresp/protocols/block.d.ts +1 -1
- package/dest/services/reqresp/protocols/block.d.ts.map +1 -1
- package/dest/services/reqresp/protocols/block.js +3 -2
- package/dest/services/reqresp/protocols/block_txs/bitvector.d.ts +1 -1
- package/dest/services/reqresp/protocols/block_txs/bitvector.d.ts.map +1 -1
- package/dest/services/reqresp/protocols/block_txs/bitvector.js +7 -0
- package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.d.ts +2 -2
- package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.d.ts.map +1 -1
- package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.js +1 -1
- package/dest/services/reqresp/protocols/status.d.ts +5 -4
- package/dest/services/reqresp/protocols/status.d.ts.map +1 -1
- package/dest/services/reqresp/protocols/status.js +7 -3
- package/dest/services/reqresp/protocols/tx.d.ts +2 -3
- package/dest/services/reqresp/protocols/tx.d.ts.map +1 -1
- package/dest/services/reqresp/reqresp.js +402 -24
- package/dest/services/service.d.ts +16 -3
- package/dest/services/service.d.ts.map +1 -1
- package/dest/services/tx_collection/config.js +1 -1
- package/dest/services/tx_collection/fast_tx_collection.d.ts +4 -3
- package/dest/services/tx_collection/fast_tx_collection.d.ts.map +1 -1
- package/dest/services/tx_collection/instrumentation.d.ts +1 -1
- package/dest/services/tx_collection/instrumentation.d.ts.map +1 -1
- package/dest/services/tx_collection/instrumentation.js +4 -14
- package/dest/services/tx_collection/slow_tx_collection.d.ts +4 -3
- package/dest/services/tx_collection/slow_tx_collection.d.ts.map +1 -1
- package/dest/services/tx_collection/tx_collection.d.ts +7 -6
- package/dest/services/tx_collection/tx_collection.d.ts.map +1 -1
- package/dest/services/tx_provider.d.ts +4 -2
- package/dest/services/tx_provider.d.ts.map +1 -1
- package/dest/services/tx_provider.js +11 -2
- package/dest/services/tx_provider_instrumentation.d.ts +5 -2
- package/dest/services/tx_provider_instrumentation.d.ts.map +1 -1
- package/dest/services/tx_provider_instrumentation.js +13 -13
- package/dest/test-helpers/mock-tx-helpers.js +1 -1
- package/dest/test-helpers/reqresp-nodes.d.ts +2 -2
- package/dest/test-helpers/reqresp-nodes.d.ts.map +1 -1
- package/dest/testbench/p2p_client_testbench_worker.js +31 -17
- package/dest/testbench/worker_client_manager.d.ts +1 -1
- package/dest/testbench/worker_client_manager.d.ts.map +1 -1
- package/dest/testbench/worker_client_manager.js +6 -1
- package/package.json +18 -18
- package/src/client/factory.ts +5 -10
- package/src/client/interface.ts +19 -4
- package/src/client/p2p_client.ts +101 -154
- package/src/config.ts +12 -18
- package/src/mem_pools/attestation_pool/attestation_pool.ts +68 -41
- package/src/mem_pools/attestation_pool/attestation_pool_test_suite.ts +241 -289
- package/src/mem_pools/attestation_pool/kv_attestation_pool.ts +163 -141
- package/src/mem_pools/attestation_pool/memory_attestation_pool.ts +141 -164
- package/src/mem_pools/attestation_pool/mocks.ts +21 -15
- package/src/mem_pools/instrumentation.ts +38 -14
- package/src/mem_pools/interface.ts +2 -4
- package/src/mem_pools/tx_pool/README.md +270 -0
- package/src/mem_pools/tx_pool/aztec_kv_tx_pool.ts +367 -371
- package/src/mem_pools/tx_pool/eviction/eviction_manager.ts +132 -0
- package/src/mem_pools/tx_pool/eviction/eviction_strategy.ts +208 -0
- package/src/mem_pools/tx_pool/eviction/fee_payer_balance_eviction_rule.ts +162 -0
- package/src/mem_pools/tx_pool/eviction/invalid_txs_after_mining_rule.ts +104 -0
- package/src/mem_pools/tx_pool/eviction/invalid_txs_after_reorg_rule.ts +93 -0
- package/src/mem_pools/tx_pool/eviction/low_priority_eviction_rule.ts +106 -0
- package/src/mem_pools/tx_pool/eviction/nullifier_conflict_pre_add_rule.ts +75 -0
- package/src/mem_pools/tx_pool/index.ts +0 -1
- package/src/mem_pools/tx_pool/priority.ts +8 -1
- package/src/mem_pools/tx_pool/tx_pool.ts +11 -5
- package/src/mem_pools/tx_pool/tx_pool_test_suite.ts +23 -17
- package/src/msg_validators/attestation_validator/attestation_validator.ts +37 -22
- package/src/msg_validators/attestation_validator/fisherman_attestation_validator.ts +13 -16
- package/src/msg_validators/clock_tolerance.ts +51 -0
- package/src/msg_validators/index.ts +1 -1
- package/src/msg_validators/proposal_validator/block_proposal_validator.ts +10 -0
- package/src/msg_validators/proposal_validator/checkpoint_proposal_validator.ts +13 -0
- package/src/msg_validators/proposal_validator/index.ts +3 -0
- package/src/msg_validators/proposal_validator/proposal_validator.ts +92 -0
- package/src/msg_validators/proposal_validator/proposal_validator_test_suite.ts +230 -0
- package/src/msg_validators/tx_validator/archive_cache.ts +1 -1
- package/src/msg_validators/tx_validator/block_header_validator.ts +4 -2
- package/src/msg_validators/tx_validator/data_validator.ts +12 -4
- package/src/msg_validators/tx_validator/factory.ts +3 -2
- package/src/msg_validators/tx_validator/fee_payer_balance.ts +40 -0
- package/src/msg_validators/tx_validator/gas_validator.ts +8 -25
- package/src/msg_validators/tx_validator/index.ts +1 -0
- package/src/msg_validators/tx_validator/metadata_validator.ts +13 -5
- package/src/msg_validators/tx_validator/size_validator.ts +18 -0
- package/src/msg_validators/tx_validator/test_utils.ts +1 -1
- package/src/msg_validators/tx_validator/timestamp_validator.ts +5 -2
- package/src/services/dummy_service.ts +6 -0
- package/src/services/encoding.ts +6 -5
- package/src/services/libp2p/instrumentation.ts +19 -73
- package/src/services/libp2p/libp2p_service.ts +369 -138
- package/src/services/peer-manager/metrics.ts +22 -21
- package/src/services/peer-manager/peer_manager.ts +5 -4
- package/src/services/peer-manager/peer_scoring.ts +1 -5
- package/src/services/reqresp/connection-sampler/connection_sampler.ts +3 -1
- package/src/services/reqresp/constants.ts +14 -0
- package/src/services/reqresp/interface.ts +1 -1
- package/src/services/reqresp/metrics.ts +7 -23
- package/src/services/reqresp/protocols/auth.ts +2 -2
- package/src/services/reqresp/protocols/block.ts +3 -2
- package/src/services/reqresp/protocols/block_txs/bitvector.ts +9 -0
- package/src/services/reqresp/protocols/block_txs/block_txs_reqresp.ts +1 -1
- package/src/services/reqresp/protocols/status.ts +16 -12
- package/src/services/reqresp/protocols/tx.ts +1 -2
- package/src/services/service.ts +19 -4
- package/src/services/tx_collection/config.ts +1 -1
- package/src/services/tx_collection/fast_tx_collection.ts +3 -2
- package/src/services/tx_collection/instrumentation.ts +4 -21
- package/src/services/tx_collection/slow_tx_collection.ts +3 -3
- package/src/services/tx_collection/tx_collection.ts +6 -5
- package/src/services/tx_provider.ts +19 -3
- package/src/services/tx_provider_instrumentation.ts +18 -14
- package/src/test-helpers/mock-pubsub.ts +1 -1
- package/src/test-helpers/mock-tx-helpers.ts +1 -1
- package/src/test-helpers/reqresp-nodes.ts +1 -1
- package/src/testbench/p2p_client_testbench_worker.ts +42 -22
- package/src/testbench/worker_client_manager.ts +6 -1
- package/dest/mem_pools/tx_pool/memory_tx_pool.d.ts +0 -80
- package/dest/mem_pools/tx_pool/memory_tx_pool.d.ts.map +0 -1
- package/dest/mem_pools/tx_pool/memory_tx_pool.js +0 -238
- package/dest/msg_validators/block_proposal_validator/block_proposal_validator.d.ts +0 -12
- package/dest/msg_validators/block_proposal_validator/block_proposal_validator.d.ts.map +0 -1
- package/dest/msg_validators/block_proposal_validator/block_proposal_validator.js +0 -82
- package/dest/msg_validators/block_proposal_validator/index.d.ts +0 -2
- package/dest/msg_validators/block_proposal_validator/index.d.ts.map +0 -1
- package/dest/msg_validators/block_proposal_validator/index.js +0 -1
- package/src/mem_pools/tx_pool/memory_tx_pool.ts +0 -283
- package/src/msg_validators/block_proposal_validator/block_proposal_validator.ts +0 -97
- package/src/msg_validators/block_proposal_validator/index.ts +0 -1
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { findIndexInSortedArray, insertIntoSortedArray } from '@aztec/foundation/array';
|
|
2
|
+
import type { BlockNumber } from '@aztec/foundation/branded-types';
|
|
3
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
4
|
+
import { createLogger } from '@aztec/foundation/log';
|
|
5
|
+
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
6
|
+
import { BlockHeader, Tx, TxHash } from '@aztec/stdlib/tx';
|
|
7
|
+
|
|
8
|
+
import type { TxPoolOptions } from '../tx_pool.js';
|
|
9
|
+
import {
|
|
10
|
+
type EvictionContext,
|
|
11
|
+
EvictionEvent,
|
|
12
|
+
type EvictionRule,
|
|
13
|
+
type PreAddEvictionResult,
|
|
14
|
+
type PreAddEvictionRule,
|
|
15
|
+
type PreAddPoolAccess,
|
|
16
|
+
type TxPoolOperations,
|
|
17
|
+
} from './eviction_strategy.js';
|
|
18
|
+
|
|
19
|
+
export class EvictionManager {
|
|
20
|
+
private rules: EvictionRule[] = [];
|
|
21
|
+
|
|
22
|
+
/** Pre-add eviction rules (run inside addTxs transaction) */
|
|
23
|
+
private preAddRules: PreAddEvictionRule[] = [];
|
|
24
|
+
|
|
25
|
+
constructor(
|
|
26
|
+
private txPool: TxPoolOperations,
|
|
27
|
+
private log = createLogger('p2p:mempool:tx_pool:eviction_manager'),
|
|
28
|
+
) {}
|
|
29
|
+
|
|
30
|
+
public async evictAfterNewTxs(newTxs: TxHash[], feePayers: AztecAddress[]): Promise<void> {
|
|
31
|
+
const ctx: EvictionContext = {
|
|
32
|
+
event: EvictionEvent.TXS_ADDED,
|
|
33
|
+
newTxs,
|
|
34
|
+
feePayers,
|
|
35
|
+
};
|
|
36
|
+
await this.runEvictionRules(ctx);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public async evictAfterNewBlock(
|
|
40
|
+
block: BlockHeader,
|
|
41
|
+
newNullifiers: Fr[],
|
|
42
|
+
minedFeePayers: AztecAddress[],
|
|
43
|
+
): Promise<void> {
|
|
44
|
+
const ctx: EvictionContext = {
|
|
45
|
+
event: EvictionEvent.BLOCK_MINED,
|
|
46
|
+
block,
|
|
47
|
+
newNullifiers,
|
|
48
|
+
feePayers: minedFeePayers,
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
await this.runEvictionRules(ctx);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public async evictAfterChainPrune(blockNumber: BlockNumber): Promise<void> {
|
|
55
|
+
const ctx: EvictionContext = {
|
|
56
|
+
event: EvictionEvent.CHAIN_PRUNED,
|
|
57
|
+
blockNumber,
|
|
58
|
+
};
|
|
59
|
+
await this.runEvictionRules(ctx);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Runs pre-add eviction rules to determine if an incoming tx should be added
|
|
64
|
+
* and which existing txs should be evicted.
|
|
65
|
+
* Called from inside the addTxs database transaction for atomicity.
|
|
66
|
+
*
|
|
67
|
+
* @param tx - The incoming transaction
|
|
68
|
+
* @param poolAccess - Read-only access to pool state
|
|
69
|
+
* @returns Combined result from all pre-add rules
|
|
70
|
+
*/
|
|
71
|
+
public async runPreAddRules(tx: Tx, poolAccess: PreAddPoolAccess): Promise<PreAddEvictionResult> {
|
|
72
|
+
const allTxHashesToEvict: TxHash[] = [];
|
|
73
|
+
const cmpTxHash = (a: TxHash, b: TxHash) => Fr.cmp(a.hash, b.hash);
|
|
74
|
+
|
|
75
|
+
for (const rule of this.preAddRules) {
|
|
76
|
+
try {
|
|
77
|
+
const result = await rule.check(tx, poolAccess);
|
|
78
|
+
|
|
79
|
+
if (result.shouldReject) {
|
|
80
|
+
return { shouldReject: true, txHashesToEvict: [], reason: result.reason };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
for (const txHashToEvict of result.txHashesToEvict) {
|
|
84
|
+
// Only add if not already present (dedup)
|
|
85
|
+
if (findIndexInSortedArray(allTxHashesToEvict, txHashToEvict, cmpTxHash) === -1) {
|
|
86
|
+
insertIntoSortedArray(allTxHashesToEvict, txHashToEvict, cmpTxHash);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
} catch (err) {
|
|
90
|
+
this.log.warn(`Pre-add eviction rule ${rule.name} unexpected error: ${String(err)}`, {
|
|
91
|
+
err,
|
|
92
|
+
preAddRule: rule.name,
|
|
93
|
+
});
|
|
94
|
+
// On error, reject the tx to be safe
|
|
95
|
+
return { shouldReject: true, txHashesToEvict: [], reason: `rule error: ${String(err)}` };
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return { shouldReject: false, txHashesToEvict: allTxHashesToEvict };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
public registerRule(rule: EvictionRule) {
|
|
103
|
+
this.rules.push(rule);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
public registerPreAddRule(rule: PreAddEvictionRule) {
|
|
107
|
+
this.preAddRules.push(rule);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
public updateConfig(config: TxPoolOptions): void {
|
|
111
|
+
for (const rule of this.rules) {
|
|
112
|
+
rule.updateConfig(config);
|
|
113
|
+
}
|
|
114
|
+
for (const rule of this.preAddRules) {
|
|
115
|
+
rule.updateConfig?.(config);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
private async runEvictionRules(ctx: EvictionContext): Promise<void> {
|
|
120
|
+
for (const rule of this.rules) {
|
|
121
|
+
try {
|
|
122
|
+
await rule.evict(ctx, this.txPool);
|
|
123
|
+
} catch (err) {
|
|
124
|
+
this.log.warn(`Eviction rule ${rule.name} unexpected error: ${String(err)}`, {
|
|
125
|
+
err,
|
|
126
|
+
evictionRule: rule.name,
|
|
127
|
+
evictionEvent: ctx.event,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import type { BlockNumber } from '@aztec/foundation/branded-types';
|
|
2
|
+
import { Buffer32 } from '@aztec/foundation/buffer';
|
|
3
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
4
|
+
import { ProtocolContractAddress } from '@aztec/protocol-contracts';
|
|
5
|
+
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
6
|
+
import { type BlockHeader, type Tx, TxHash } from '@aztec/stdlib/tx';
|
|
7
|
+
|
|
8
|
+
import { getFeePayerBalanceDelta } from '../../../msg_validators/tx_validator/fee_payer_balance.js';
|
|
9
|
+
import { getTxPriorityFee } from '../priority.js';
|
|
10
|
+
import type { TxPoolOptions } from '../tx_pool.js';
|
|
11
|
+
|
|
12
|
+
export const EvictionEvent = {
|
|
13
|
+
TXS_ADDED: 'txs_added',
|
|
14
|
+
BLOCK_MINED: 'block_mined',
|
|
15
|
+
CHAIN_PRUNED: 'chain_pruned',
|
|
16
|
+
} as const;
|
|
17
|
+
|
|
18
|
+
type EvictionEvent = (typeof EvictionEvent)[keyof typeof EvictionEvent];
|
|
19
|
+
|
|
20
|
+
export type EvictionContext =
|
|
21
|
+
| {
|
|
22
|
+
event: typeof EvictionEvent.TXS_ADDED;
|
|
23
|
+
newTxs: TxHash[];
|
|
24
|
+
feePayers: AztecAddress[];
|
|
25
|
+
}
|
|
26
|
+
| {
|
|
27
|
+
event: typeof EvictionEvent.CHAIN_PRUNED;
|
|
28
|
+
blockNumber: BlockNumber;
|
|
29
|
+
}
|
|
30
|
+
| {
|
|
31
|
+
event: typeof EvictionEvent.BLOCK_MINED;
|
|
32
|
+
block: BlockHeader;
|
|
33
|
+
newNullifiers: Fr[];
|
|
34
|
+
feePayers: AztecAddress[];
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Result of an eviction operation
|
|
39
|
+
*/
|
|
40
|
+
export interface EvictionResult {
|
|
41
|
+
readonly txsEvicted: TxHash[];
|
|
42
|
+
readonly reason: string;
|
|
43
|
+
readonly success: boolean;
|
|
44
|
+
readonly error?: Error;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Information about a pending transaction
|
|
49
|
+
*/
|
|
50
|
+
export interface PendingTxInfo {
|
|
51
|
+
txHash: TxHash;
|
|
52
|
+
blockHash: Fr;
|
|
53
|
+
isEvictable: boolean;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Information about a transaction that references a specific block
|
|
58
|
+
*/
|
|
59
|
+
export interface TxBlockReference {
|
|
60
|
+
txHash: TxHash;
|
|
61
|
+
blockHash: Fr;
|
|
62
|
+
isEvictable: boolean;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Operations that eviction strategies can perform on the pool
|
|
67
|
+
*/
|
|
68
|
+
export interface TxPoolOperations {
|
|
69
|
+
getTxByHash(txHash: TxHash): Promise<Tx | undefined>;
|
|
70
|
+
getPendingTxInfos(): Promise<PendingTxInfo[]>;
|
|
71
|
+
getPendingTxsReferencingBlocks(blockHashes: Fr[]): Promise<TxBlockReference[]>;
|
|
72
|
+
getPendingFeePayers(): Promise<AztecAddress[]>;
|
|
73
|
+
getFeePayerTxInfos(feePayer: AztecAddress): AsyncIterable<FeePayerTxInfo>;
|
|
74
|
+
/** Cheap count of current pending transactions. */
|
|
75
|
+
getPendingTxCount(): Promise<number>;
|
|
76
|
+
/**
|
|
77
|
+
* Returns up to `limit` lowest-priority evictable pending tx hashes.
|
|
78
|
+
* Ordering should be from lowest priority upwards.
|
|
79
|
+
*/
|
|
80
|
+
getLowestPriorityEvictable(limit: number): Promise<TxHash[]>;
|
|
81
|
+
deleteTxs(txHashes: TxHash[], opts?: { permanently?: boolean }): Promise<void>;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Strategy interface for different eviction behaviors
|
|
86
|
+
*/
|
|
87
|
+
export interface EvictionRule {
|
|
88
|
+
readonly name: string;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Performs the eviction logic
|
|
92
|
+
*/
|
|
93
|
+
evict(context: EvictionContext, txPool: TxPoolOperations): Promise<EvictionResult>;
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Updates the configuration for this eviction rule.
|
|
97
|
+
* Rules should ignore config options that don't apply to them.
|
|
98
|
+
*/
|
|
99
|
+
updateConfig(config: TxPoolOptions): void;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Balance-related information about a transaction for a fee payer.
|
|
104
|
+
*/
|
|
105
|
+
export class FeePayerTxInfo {
|
|
106
|
+
txHash: TxHash;
|
|
107
|
+
priority: bigint;
|
|
108
|
+
feeLimit: bigint;
|
|
109
|
+
claimAmount: bigint;
|
|
110
|
+
isEvictable: boolean;
|
|
111
|
+
|
|
112
|
+
constructor(fields: {
|
|
113
|
+
txHash: TxHash;
|
|
114
|
+
priority: bigint;
|
|
115
|
+
feeLimit: bigint;
|
|
116
|
+
claimAmount: bigint;
|
|
117
|
+
isEvictable: boolean;
|
|
118
|
+
}) {
|
|
119
|
+
this.txHash = fields.txHash;
|
|
120
|
+
this.priority = fields.priority;
|
|
121
|
+
this.feeLimit = fields.feeLimit;
|
|
122
|
+
this.claimAmount = fields.claimAmount;
|
|
123
|
+
this.isEvictable = fields.isEvictable;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
static async encode(tx: Tx, txHash: string | TxHash): Promise<Buffer> {
|
|
127
|
+
const { feeLimit, claimAmount } = await getFeePayerBalanceDelta(tx, ProtocolContractAddress.FeeJuice);
|
|
128
|
+
const priority = Buffer32.fromBigInt(getTxPriorityFee(tx)).toBuffer();
|
|
129
|
+
const hashBuffer = (typeof txHash === 'string' ? TxHash.fromString(txHash) : txHash).toBuffer();
|
|
130
|
+
const feeLimitBuffer = Buffer32.fromBigInt(feeLimit).toBuffer();
|
|
131
|
+
const claimAmountBuffer = Buffer32.fromBigInt(claimAmount).toBuffer();
|
|
132
|
+
return Buffer.concat([priority, hashBuffer, feeLimitBuffer, claimAmountBuffer]);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
static decode(value: Buffer, isEvictable = true): FeePayerTxInfo {
|
|
136
|
+
const priority = Buffer32.fromBuffer(value.subarray(0, Buffer32.SIZE)).toBigInt();
|
|
137
|
+
const hashOffset = Buffer32.SIZE;
|
|
138
|
+
const feeLimitOffset = hashOffset + TxHash.SIZE;
|
|
139
|
+
const claimOffset = feeLimitOffset + Buffer32.SIZE;
|
|
140
|
+
|
|
141
|
+
return new FeePayerTxInfo({
|
|
142
|
+
txHash: TxHash.fromBuffer(value.subarray(hashOffset, feeLimitOffset)),
|
|
143
|
+
priority,
|
|
144
|
+
feeLimit: Buffer32.fromBuffer(value.subarray(feeLimitOffset, claimOffset)).toBigInt(),
|
|
145
|
+
claimAmount: Buffer32.fromBuffer(value.subarray(claimOffset, claimOffset + Buffer32.SIZE)).toBigInt(),
|
|
146
|
+
isEvictable,
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Read-only access to pool state for pre-add eviction checks.
|
|
153
|
+
* Passed to pre-add rules during the addTxs transaction.
|
|
154
|
+
*/
|
|
155
|
+
export interface PreAddPoolAccess {
|
|
156
|
+
/**
|
|
157
|
+
* Get the pending tx hash that uses a specific nullifier, if any.
|
|
158
|
+
* Returns undefined if no pending tx uses this nullifier.
|
|
159
|
+
*/
|
|
160
|
+
getTxHashByNullifier(nullifier: Fr): Promise<TxHash | undefined>;
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Get a pending transaction by its hash.
|
|
164
|
+
*/
|
|
165
|
+
getPendingTxByHash(hash: TxHash): Promise<Tx | undefined>;
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Get the priority string for a transaction (for fee comparison).
|
|
169
|
+
*/
|
|
170
|
+
getTxPriority(tx: Tx): string;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Result of a pre-add eviction check for a single transaction.
|
|
175
|
+
*/
|
|
176
|
+
export interface PreAddEvictionResult {
|
|
177
|
+
/** Whether the incoming tx should be rejected */
|
|
178
|
+
readonly shouldReject: boolean;
|
|
179
|
+
/** Sorted array of existing tx hashes that should be evicted if this tx is added */
|
|
180
|
+
readonly txHashesToEvict: TxHash[];
|
|
181
|
+
/** Optional reason for rejection */
|
|
182
|
+
readonly reason?: string;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Strategy interface for pre-add eviction rules.
|
|
187
|
+
* These run inside the addTxs transaction before a tx is added,
|
|
188
|
+
* deciding whether to evict existing txs or reject the incoming tx.
|
|
189
|
+
*/
|
|
190
|
+
export interface PreAddEvictionRule {
|
|
191
|
+
readonly name: string;
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Check if incoming tx should be added and which existing txs to evict.
|
|
195
|
+
* Called inside the addTxs database transaction for atomicity.
|
|
196
|
+
*
|
|
197
|
+
* @param tx - The incoming transaction to check
|
|
198
|
+
* @param poolAccess - Read-only access to current pool state
|
|
199
|
+
* @returns Result indicating whether to reject and what to evict
|
|
200
|
+
*/
|
|
201
|
+
check(tx: Tx, poolAccess: PreAddPoolAccess): Promise<PreAddEvictionResult>;
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Updates the configuration for this rule.
|
|
205
|
+
* Rules should ignore config options that don't apply to them.
|
|
206
|
+
*/
|
|
207
|
+
updateConfig?(config: TxPoolOptions): void;
|
|
208
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { createLogger } from '@aztec/foundation/log';
|
|
2
|
+
import { ProtocolContractAddress } from '@aztec/protocol-contracts';
|
|
3
|
+
import { computeFeePayerBalanceStorageSlot } from '@aztec/protocol-contracts/fee-juice';
|
|
4
|
+
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
5
|
+
import type { WorldStateSynchronizer } from '@aztec/stdlib/interfaces/server';
|
|
6
|
+
import { DatabasePublicStateSource, type MerkleTreeReadOperations } from '@aztec/stdlib/trees';
|
|
7
|
+
import type { TxHash } from '@aztec/stdlib/tx';
|
|
8
|
+
|
|
9
|
+
import type { TxPoolOptions } from '../tx_pool.js';
|
|
10
|
+
import {
|
|
11
|
+
type EvictionContext,
|
|
12
|
+
EvictionEvent,
|
|
13
|
+
type EvictionResult,
|
|
14
|
+
type EvictionRule,
|
|
15
|
+
type FeePayerTxInfo,
|
|
16
|
+
type TxPoolOperations,
|
|
17
|
+
} from './eviction_strategy.js';
|
|
18
|
+
|
|
19
|
+
export class FeePayerBalanceEvictionRule implements EvictionRule {
|
|
20
|
+
public readonly name = 'FeePayerBalanceEviction';
|
|
21
|
+
public readonly reason = 'fee_payer_balance';
|
|
22
|
+
|
|
23
|
+
private log = createLogger('p2p:mempool:tx_pool:fee_payer_balance_eviction_rule');
|
|
24
|
+
|
|
25
|
+
constructor(private worldState: WorldStateSynchronizer) {}
|
|
26
|
+
|
|
27
|
+
async evict(context: EvictionContext, txPool: TxPoolOperations): Promise<EvictionResult> {
|
|
28
|
+
try {
|
|
29
|
+
if (context.event === EvictionEvent.TXS_ADDED) {
|
|
30
|
+
return await this.evictForFeePayers(context.feePayers, this.worldState.getCommitted(), txPool);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (context.event === EvictionEvent.BLOCK_MINED) {
|
|
34
|
+
const blockNumber = context.block.getBlockNumber();
|
|
35
|
+
// Ensure world state is synced to this block before accessing the snapshot.
|
|
36
|
+
// This handles the race where a block is added to the archiver
|
|
37
|
+
// but the world state hasn't synced it yet.
|
|
38
|
+
await this.worldState.syncImmediate(blockNumber);
|
|
39
|
+
return await this.evictForFeePayers(context.feePayers, this.worldState.getSnapshot(blockNumber), txPool);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// TODO: fix this edge-case
|
|
43
|
+
// This can lead to a race condition if we are catching up in the p2p client.
|
|
44
|
+
// Let's say we have 3 txs for the same fee payer, which get mined in blocks 1, 2, 3.
|
|
45
|
+
// Tx1 consumes fee juice, tx2 increases it, tx3 consumes it again. We see block1 with tx1 first, run this rule, and evict tx3.
|
|
46
|
+
// But tx3 was valid (due to tx2) and mined on block3. And we have just removed from the mempool a tx we needed for proving/reexec.
|
|
47
|
+
//
|
|
48
|
+
// NOTE: this will happen only in case of that lower-priority-fee tx entered in e.g. block 2, and we have higher-priority-fee tx in block 3
|
|
49
|
+
// (simply because that was the timing of these txs). But, in case of higher-priority-fee txs being in block 2, the tx3 won't be evicted
|
|
50
|
+
// -----
|
|
51
|
+
// Proposed fix: evict only if node is synched
|
|
52
|
+
if (context.event === EvictionEvent.CHAIN_PRUNED) {
|
|
53
|
+
// Ensure world state is synced to this block before accessing the snapshot.
|
|
54
|
+
await this.worldState.syncImmediate(context.blockNumber);
|
|
55
|
+
const feePayers = await txPool.getPendingFeePayers();
|
|
56
|
+
return await this.evictForFeePayers(feePayers, this.worldState.getSnapshot(context.blockNumber), txPool);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
reason: this.reason,
|
|
61
|
+
success: true,
|
|
62
|
+
txsEvicted: [],
|
|
63
|
+
};
|
|
64
|
+
} catch (err) {
|
|
65
|
+
this.log.error('Failed to evict txs due to fee payer balance', { err });
|
|
66
|
+
return {
|
|
67
|
+
reason: this.reason,
|
|
68
|
+
success: false,
|
|
69
|
+
txsEvicted: [],
|
|
70
|
+
error: new Error('Failed to evict txs due to fee payer balance', { cause: err }),
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
updateConfig(_config: TxPoolOptions): void {}
|
|
76
|
+
|
|
77
|
+
private async evictForFeePayers(
|
|
78
|
+
feePayers: Array<AztecAddress>, // assumed to be unique
|
|
79
|
+
db: MerkleTreeReadOperations,
|
|
80
|
+
txPool: TxPoolOperations,
|
|
81
|
+
): Promise<EvictionResult> {
|
|
82
|
+
const publicStateSource = this.createPublicStateSource(db);
|
|
83
|
+
|
|
84
|
+
const txsToEvict = (
|
|
85
|
+
await Promise.all(feePayers.map(feePayer => this.getEvictionsForFeePayer(feePayer, publicStateSource, txPool)))
|
|
86
|
+
).flat();
|
|
87
|
+
|
|
88
|
+
if (txsToEvict.length > 0) {
|
|
89
|
+
await txPool.deleteTxs(txsToEvict);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return {
|
|
93
|
+
reason: this.reason,
|
|
94
|
+
success: true,
|
|
95
|
+
txsEvicted: txsToEvict,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
private async getEvictionsForFeePayer(
|
|
100
|
+
feePayer: AztecAddress,
|
|
101
|
+
publicStateSource: DatabasePublicStateSource,
|
|
102
|
+
txPool: TxPoolOperations,
|
|
103
|
+
): Promise<TxHash[]> {
|
|
104
|
+
const initialBalance = (
|
|
105
|
+
await publicStateSource.storageRead(
|
|
106
|
+
ProtocolContractAddress.FeeJuice,
|
|
107
|
+
//TODO: cache this LRU-style
|
|
108
|
+
await computeFeePayerBalanceStorageSlot(feePayer),
|
|
109
|
+
)
|
|
110
|
+
).toBigInt();
|
|
111
|
+
|
|
112
|
+
const txs: FeePayerTxInfo[] = [];
|
|
113
|
+
for await (const entry of txPool.getFeePayerTxInfos(feePayer)) {
|
|
114
|
+
txs.push(entry);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (txs.length === 0) {
|
|
118
|
+
return [];
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const txsToEvict: TxHash[] = [];
|
|
122
|
+
let balance = initialBalance;
|
|
123
|
+
let hasNonEvictableOverBalance = false;
|
|
124
|
+
|
|
125
|
+
// Evaluate balance in priority order so later claims cannot fund earlier spends.
|
|
126
|
+
// This sorts so that higher priority txs come first.
|
|
127
|
+
txs.sort((a, b) => {
|
|
128
|
+
if (a.priority === b.priority) {
|
|
129
|
+
return a.txHash.toBigInt() >= b.txHash.toBigInt() ? -1 : 1;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return a.priority > b.priority ? -1 : 1;
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
for (const tx of txs) {
|
|
136
|
+
const available = balance + tx.claimAmount;
|
|
137
|
+
if (available >= tx.feeLimit) {
|
|
138
|
+
balance = available - tx.feeLimit;
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (tx.isEvictable) {
|
|
143
|
+
txsToEvict.push(tx.txHash);
|
|
144
|
+
} else {
|
|
145
|
+
hasNonEvictableOverBalance = true;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (hasNonEvictableOverBalance) {
|
|
150
|
+
this.log.verbose('Fee payer balance cannot be satisfied due to non-evictable txs', {
|
|
151
|
+
feePayer: feePayer.toString(),
|
|
152
|
+
balance: initialBalance,
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return txsToEvict;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
private createPublicStateSource(db: MerkleTreeReadOperations): DatabasePublicStateSource {
|
|
160
|
+
return new DatabasePublicStateSource(db);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { createLogger } from '@aztec/foundation/log';
|
|
2
|
+
import type { TxHash } from '@aztec/stdlib/tx';
|
|
3
|
+
|
|
4
|
+
import type { TxPoolOptions } from '../tx_pool.js';
|
|
5
|
+
import {
|
|
6
|
+
type EvictionContext,
|
|
7
|
+
EvictionEvent,
|
|
8
|
+
type EvictionResult,
|
|
9
|
+
type EvictionRule,
|
|
10
|
+
type TxPoolOperations,
|
|
11
|
+
} from './eviction_strategy.js';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Eviction rule that removes invalid transactions after a block is mined.
|
|
15
|
+
* Only triggers on BLOCK_MINED events.
|
|
16
|
+
*
|
|
17
|
+
* Eviction criteria includes:
|
|
18
|
+
* - Transactions with nullifiers that are already included in the mined block
|
|
19
|
+
* - Transactions with an expiration timestamp less than or equal to the mined block timestamp
|
|
20
|
+
*/
|
|
21
|
+
export class InvalidTxsAfterMiningRule implements EvictionRule {
|
|
22
|
+
public readonly name = 'InvalidTxsAfterMining';
|
|
23
|
+
|
|
24
|
+
private log = createLogger('p2p:mempool:tx_pool:invalid_txs_after_mining_rule');
|
|
25
|
+
|
|
26
|
+
async evict(context: EvictionContext, txPool: TxPoolOperations): Promise<EvictionResult> {
|
|
27
|
+
if (context.event !== EvictionEvent.BLOCK_MINED) {
|
|
28
|
+
return {
|
|
29
|
+
reason: 'block_mined_invalid_txs',
|
|
30
|
+
success: true,
|
|
31
|
+
txsEvicted: [],
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (!context.block || !context.newNullifiers) {
|
|
36
|
+
this.log.warn('Invalid context for block mined eviction', { context });
|
|
37
|
+
return {
|
|
38
|
+
reason: 'block_mined_invalid_txs',
|
|
39
|
+
success: false,
|
|
40
|
+
txsEvicted: [],
|
|
41
|
+
error: new Error('Invalid block mined context'),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
try {
|
|
46
|
+
const { timestamp } = context.block.globalVariables;
|
|
47
|
+
|
|
48
|
+
const txsToEvict: TxHash[] = [];
|
|
49
|
+
const pendingTxs = await txPool.getPendingTxInfos();
|
|
50
|
+
const minedNullifiers = new Set(context.newNullifiers.map(n => n.toString()));
|
|
51
|
+
|
|
52
|
+
for (const { txHash, isEvictable } of pendingTxs) {
|
|
53
|
+
if (!isEvictable) {
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const tx = await txPool.getTxByHash(txHash);
|
|
58
|
+
if (!tx) {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Evict pending txs that share nullifiers with mined txs
|
|
63
|
+
const txNullifiers = tx.data.getNonEmptyNullifiers();
|
|
64
|
+
if (txNullifiers.some(nullifier => minedNullifiers.has(nullifier.toString()))) {
|
|
65
|
+
this.log.verbose(`Evicting tx ${txHash} from pool due to a duplicate nullifier with a mined tx`);
|
|
66
|
+
txsToEvict.push(txHash);
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Evict pending txs with an expiration timestamp less than or equal to the mined block timestamp
|
|
71
|
+
const includeByTimestamp = tx.data.includeByTimestamp;
|
|
72
|
+
if (includeByTimestamp <= timestamp) {
|
|
73
|
+
this.log.verbose(
|
|
74
|
+
`Evicting tx ${txHash} from pool due to the tx being expired (includeByTimestamp: ${includeByTimestamp}, mined block timestamp: ${timestamp})`,
|
|
75
|
+
);
|
|
76
|
+
txsToEvict.push(txHash);
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (txsToEvict.length > 0) {
|
|
82
|
+
await txPool.deleteTxs(txsToEvict);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
this.log.debug(`Evicted ${txsToEvict.length} invalid txs after block mined`);
|
|
86
|
+
|
|
87
|
+
return {
|
|
88
|
+
reason: 'block_mined_invalid_txs',
|
|
89
|
+
success: true,
|
|
90
|
+
txsEvicted: txsToEvict,
|
|
91
|
+
};
|
|
92
|
+
} catch (err) {
|
|
93
|
+
this.log.error('Failed to evict invalid transactions after mining', { err });
|
|
94
|
+
return {
|
|
95
|
+
reason: 'block_mined_invalid_txs',
|
|
96
|
+
success: false,
|
|
97
|
+
txsEvicted: [],
|
|
98
|
+
error: new Error('Failed to evict invalid txs after mining', { cause: err }),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
updateConfig(_config: TxPoolOptions): void {}
|
|
104
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { findIndexInSortedArray, insertIntoSortedArray } from '@aztec/foundation/array';
|
|
2
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
3
|
+
import { createLogger } from '@aztec/foundation/log';
|
|
4
|
+
import type { WorldStateSynchronizer } from '@aztec/stdlib/interfaces/server';
|
|
5
|
+
import { MerkleTreeId } from '@aztec/stdlib/trees';
|
|
6
|
+
import type { TxHash } from '@aztec/stdlib/tx';
|
|
7
|
+
|
|
8
|
+
import type { TxPoolOptions } from '../tx_pool.js';
|
|
9
|
+
import {
|
|
10
|
+
type EvictionContext,
|
|
11
|
+
EvictionEvent,
|
|
12
|
+
type EvictionResult,
|
|
13
|
+
type EvictionRule,
|
|
14
|
+
type TxPoolOperations,
|
|
15
|
+
} from './eviction_strategy.js';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Eviction rule that removes invalid transactions after a blockchain reorganization.
|
|
19
|
+
* Only triggers on CHAIN_PRUNED events.
|
|
20
|
+
*
|
|
21
|
+
* Eviction criteria includes:
|
|
22
|
+
* - Transactions that reference pruned block hashes (invalid by definition)
|
|
23
|
+
*/
|
|
24
|
+
export class InvalidTxsAfterReorgRule implements EvictionRule {
|
|
25
|
+
public readonly name = 'InvalidTxsAfterReorg';
|
|
26
|
+
|
|
27
|
+
private log = createLogger('p2p:mempool:tx_pool:invalid_txs_after_reorg_rule');
|
|
28
|
+
|
|
29
|
+
public constructor(private worldState: WorldStateSynchronizer) {}
|
|
30
|
+
|
|
31
|
+
async evict(context: EvictionContext, txPool: TxPoolOperations): Promise<EvictionResult> {
|
|
32
|
+
if (context.event !== EvictionEvent.CHAIN_PRUNED) {
|
|
33
|
+
return {
|
|
34
|
+
reason: 'reorg_invalid_txs',
|
|
35
|
+
success: true,
|
|
36
|
+
txsEvicted: [],
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
try {
|
|
41
|
+
const candidateTxs = (await txPool.getPendingTxInfos()).filter(({ isEvictable }) => isEvictable);
|
|
42
|
+
|
|
43
|
+
// Deduplicate block hashes to reduce redundant DB lookups (many txs may share the same blockHash).
|
|
44
|
+
const uniqueBlockHashes: Fr[] = [];
|
|
45
|
+
candidateTxs.forEach(({ blockHash }) => {
|
|
46
|
+
insertIntoSortedArray(uniqueBlockHashes, blockHash, Fr.cmp, false);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// Ensure world state is synced to this block before accessing the snapshot.
|
|
50
|
+
await this.worldState.syncImmediate(context.blockNumber);
|
|
51
|
+
const db = this.worldState.getSnapshot(context.blockNumber);
|
|
52
|
+
const blocksFromDb = await db.findLeafIndices(MerkleTreeId.ARCHIVE, uniqueBlockHashes);
|
|
53
|
+
|
|
54
|
+
// Identify txs whose blockHash is not found in the archive (pruned)
|
|
55
|
+
const txsToEvict: TxHash[] = [];
|
|
56
|
+
for (const tx of candidateTxs) {
|
|
57
|
+
const idx = findIndexInSortedArray(uniqueBlockHashes, tx.blockHash, Fr.cmp);
|
|
58
|
+
const blockPruned = idx === -1 || blocksFromDb[idx] === undefined;
|
|
59
|
+
if (blockPruned) {
|
|
60
|
+
txsToEvict.push(tx.txHash);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (txsToEvict.length > 0) {
|
|
65
|
+
this.log.verbose(`Evicting ${txsToEvict.length} txs from pool due to referencing pruned blocks`);
|
|
66
|
+
await txPool.deleteTxs(txsToEvict);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const keptCount = candidateTxs.length - txsToEvict.length;
|
|
70
|
+
if (keptCount > 0) {
|
|
71
|
+
this.log.verbose(`Kept ${keptCount} txs that did not reference pruned blocks`);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
this.log.debug(`Evicted ${txsToEvict.length} invalid txs after reorg`);
|
|
75
|
+
|
|
76
|
+
return {
|
|
77
|
+
reason: 'reorg_invalid_txs',
|
|
78
|
+
success: true,
|
|
79
|
+
txsEvicted: txsToEvict,
|
|
80
|
+
};
|
|
81
|
+
} catch (err) {
|
|
82
|
+
this.log.error('Failed to evict invalid transactions after reorg', { err });
|
|
83
|
+
return {
|
|
84
|
+
reason: 'reorg_invalid_txs',
|
|
85
|
+
success: false,
|
|
86
|
+
txsEvicted: [],
|
|
87
|
+
error: new Error('Failed to evict invalid txs after reorg', { cause: err }),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
updateConfig(_config: TxPoolOptions): void {}
|
|
93
|
+
}
|