@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
|
@@ -1,39 +1,42 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { insertIntoSortedArray } from '@aztec/foundation/array';
|
|
2
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
2
3
|
import { toArray } from '@aztec/foundation/iterable';
|
|
3
4
|
import { createLogger } from '@aztec/foundation/log';
|
|
4
|
-
import {
|
|
5
|
-
import { GasFees } from '@aztec/stdlib/gas';
|
|
5
|
+
import { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
6
6
|
import { ChonkProof } from '@aztec/stdlib/proofs';
|
|
7
|
-
import { DatabasePublicStateSource } from '@aztec/stdlib/trees';
|
|
8
7
|
import { Tx, TxHash } from '@aztec/stdlib/tx';
|
|
9
8
|
import { getTelemetryClient } from '@aztec/telemetry-client';
|
|
10
9
|
import assert from 'assert';
|
|
11
10
|
import EventEmitter from 'node:events';
|
|
12
11
|
import { ArchiveCache } from '../../msg_validators/tx_validator/archive_cache.js';
|
|
13
|
-
import { GasTxValidator } from '../../msg_validators/tx_validator/gas_validator.js';
|
|
14
12
|
import { PoolInstrumentation, PoolName } from '../instrumentation.js';
|
|
13
|
+
import { EvictionManager } from './eviction/eviction_manager.js';
|
|
14
|
+
import { FeePayerTxInfo } from './eviction/eviction_strategy.js';
|
|
15
|
+
import { FeePayerBalanceEvictionRule } from './eviction/fee_payer_balance_eviction_rule.js';
|
|
16
|
+
import { InvalidTxsAfterMiningRule } from './eviction/invalid_txs_after_mining_rule.js';
|
|
17
|
+
import { InvalidTxsAfterReorgRule } from './eviction/invalid_txs_after_reorg_rule.js';
|
|
18
|
+
import { LowPriorityEvictionRule } from './eviction/low_priority_eviction_rule.js';
|
|
19
|
+
import { NullifierConflictPreAddRule } from './eviction/nullifier_conflict_pre_add_rule.js';
|
|
15
20
|
import { getPendingTxPriority } from './priority.js';
|
|
16
21
|
/**
|
|
17
22
|
* KV implementation of the Transaction Pool.
|
|
18
23
|
*/ export class AztecKVTxPool extends EventEmitter {
|
|
19
24
|
#store;
|
|
20
25
|
/** Our tx pool, stored as a Map, with K: tx hash and V: the transaction. */ #txs;
|
|
21
|
-
/**
|
|
22
|
-
/** The tx evicion logic will kick after pool size is greater than maxTxPoolSize * txPoolOverflowFactor */ txPoolOverflowFactor = 1;
|
|
26
|
+
/** Holds the historical block for each tx */ #pendingTxHashToHistoricalBlockHeaderHash;
|
|
23
27
|
/** Index from tx hash to the block number in which they were mined, filtered by mined txs. */ #minedTxHashToBlock;
|
|
24
28
|
/** Index from tx priority (stored as hex) to its tx hash, filtered by pending txs. */ #pendingTxPriorityToHash;
|
|
25
|
-
/** Index from tx hash to its tx size (in bytes), filtered by pending txs. */ #pendingTxHashToSize;
|
|
26
|
-
/** Index from tx hash to its header hash, filtered by pending txs. */ #pendingTxHashToHeaderHash;
|
|
27
29
|
/** Map from tx hash to the block number it was originally mined in (for soft-deleted txs). */ #deletedMinedTxHashes;
|
|
28
30
|
/** MultiMap from block number to deleted mined tx hashes for efficient cleanup. */ #blockToDeletedMinedTxHash;
|
|
29
|
-
|
|
30
|
-
|
|
31
|
+
#historicalHeaderToTxHash;
|
|
32
|
+
#feePayerToBalanceEntry;
|
|
33
|
+
/** Index from nullifier to pending tx hash */ #pendingNullifierToTxHash;
|
|
31
34
|
/** In-memory set of txs that should not be evicted from the pool. */ #nonEvictableTxs;
|
|
32
35
|
/** KV store for archived txs. */ #archive;
|
|
33
36
|
/** Archived txs map for future lookup. */ #archivedTxs;
|
|
34
37
|
/** Indexes of the archived txs by insertion order. */ #archivedTxIndices;
|
|
35
38
|
/** Number of txs to archive. */ #archivedTxLimit = 0;
|
|
36
|
-
|
|
39
|
+
#evictionManager;
|
|
37
40
|
#log;
|
|
38
41
|
#metrics;
|
|
39
42
|
/**
|
|
@@ -43,25 +46,33 @@ import { getPendingTxPriority } from './priority.js';
|
|
|
43
46
|
* @param telemetry - A telemetry client.
|
|
44
47
|
* @param archivedTxLimit - The number of txs to archive.
|
|
45
48
|
* @param log - A logger.
|
|
46
|
-
*/ constructor(store, archive,
|
|
49
|
+
*/ constructor(store, archive, worldState, telemetry = getTelemetryClient(), config = {}, log = createLogger('p2p:tx_pool')){
|
|
47
50
|
super();
|
|
48
51
|
this.#log = log;
|
|
52
|
+
this.#evictionManager = new EvictionManager(this);
|
|
53
|
+
this.#evictionManager.registerRule(new InvalidTxsAfterMiningRule());
|
|
54
|
+
this.#evictionManager.registerRule(new InvalidTxsAfterReorgRule(worldState));
|
|
55
|
+
this.#evictionManager.registerRule(new FeePayerBalanceEvictionRule(worldState));
|
|
56
|
+
this.#evictionManager.registerRule(new LowPriorityEvictionRule({
|
|
57
|
+
//NOTE: 0 effectively disables low priority eviction
|
|
58
|
+
maxPoolSize: config.maxPendingTxCount ?? 0
|
|
59
|
+
}));
|
|
60
|
+
this.#evictionManager.registerPreAddRule(new NullifierConflictPreAddRule());
|
|
49
61
|
this.updateConfig(config);
|
|
50
62
|
this.#txs = store.openMap('txs');
|
|
51
63
|
this.#minedTxHashToBlock = store.openMap('txHashToBlockMined');
|
|
52
64
|
this.#pendingTxPriorityToHash = store.openMultiMap('pendingTxFeeToHash');
|
|
53
|
-
this.#pendingTxHashToSize = store.openMap('pendingTxHashToSize');
|
|
54
|
-
this.#pendingTxHashToHeaderHash = store.openMap('pendingTxHashToHeaderHash');
|
|
55
|
-
this.#pendingTxSize = store.openSingleton('pendingTxSize');
|
|
56
65
|
this.#deletedMinedTxHashes = store.openMap('deletedMinedTxHashes');
|
|
57
66
|
this.#blockToDeletedMinedTxHash = store.openMultiMap('blockToDeletedMinedTxHash');
|
|
58
|
-
this.#
|
|
67
|
+
this.#pendingTxHashToHistoricalBlockHeaderHash = store.openMap('txHistoricalBlock');
|
|
68
|
+
this.#historicalHeaderToTxHash = store.openMultiMap('historicalHeaderToPendingTxHash');
|
|
69
|
+
this.#feePayerToBalanceEntry = store.openMultiMap('feePayerToBalanceEntry');
|
|
70
|
+
this.#pendingNullifierToTxHash = store.openMap('pendingNullifierToTxHash');
|
|
59
71
|
this.#nonEvictableTxs = new Set();
|
|
60
72
|
this.#archivedTxs = archive.openMap('archivedTxs');
|
|
61
73
|
this.#archivedTxIndices = archive.openMap('archivedTxIndices');
|
|
62
74
|
this.#store = store;
|
|
63
75
|
this.#archive = archive;
|
|
64
|
-
this.#worldStateSynchronizer = worldStateSynchronizer;
|
|
65
76
|
this.#metrics = new PoolInstrumentation(telemetry, PoolName.TX_POOL, this.countTxs, ()=>store.estimateSize());
|
|
66
77
|
}
|
|
67
78
|
countTxs = async ()=>{
|
|
@@ -92,59 +103,68 @@ import { getPendingTxPriority } from './priority.js';
|
|
|
92
103
|
if (txHashes.length === 0) {
|
|
93
104
|
return Promise.resolve();
|
|
94
105
|
}
|
|
95
|
-
const
|
|
96
|
-
const
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
await this
|
|
106
|
+
const uniqueMinedNullifiers = [];
|
|
107
|
+
const uniqueMinedFeePayers = [];
|
|
108
|
+
try {
|
|
109
|
+
await this.#store.transactionAsync(async ()=>{
|
|
110
|
+
for (const hash of txHashes){
|
|
111
|
+
const key = hash.toString();
|
|
112
|
+
await this.#minedTxHashToBlock.set(key, blockHeader.globalVariables.blockNumber);
|
|
113
|
+
const tx = await this.getTxByHash(hash);
|
|
114
|
+
if (tx) {
|
|
115
|
+
const nullifiers = tx.data.getNonEmptyNullifiers();
|
|
116
|
+
nullifiers.forEach((nullifier)=>insertIntoSortedArray(uniqueMinedNullifiers, nullifier, Fr.cmp, false));
|
|
117
|
+
insertIntoSortedArray(uniqueMinedFeePayers, tx.data.feePayer, (a, b)=>a.toField().cmp(b.toField()), false);
|
|
118
|
+
await this.removePendingTxIndicesInDbTx(tx, key);
|
|
119
|
+
}
|
|
120
|
+
// If this tx was previously soft-deleted, remove it from the deleted sets
|
|
121
|
+
if (await this.#deletedMinedTxHashes.hasAsync(key)) {
|
|
122
|
+
const originalBlock = await this.#deletedMinedTxHashes.getAsync(key);
|
|
123
|
+
await this.#deletedMinedTxHashes.delete(key);
|
|
124
|
+
// Remove from block-to-hash mapping
|
|
125
|
+
if (originalBlock !== undefined) {
|
|
126
|
+
await this.#blockToDeletedMinedTxHash.deleteValue(originalBlock, key);
|
|
127
|
+
}
|
|
108
128
|
}
|
|
109
129
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
await this.#pendingTxSize.set(pendingTxSize);
|
|
121
|
-
await this.evictInvalidTxsAfterMining(txHashes, blockHeader, minedNullifiers, minedFeePayers);
|
|
122
|
-
});
|
|
123
|
-
// We update this after the transaction above. This ensures that the non-evictable transactions are not evicted
|
|
124
|
-
// until any that have been mined are marked as such.
|
|
125
|
-
// The non-evictable set is not considered when evicting transactions that are invalid after a block is mined.
|
|
126
|
-
this.#nonEvictableTxs.clear();
|
|
130
|
+
});
|
|
131
|
+
await this.#evictionManager.evictAfterNewBlock(blockHeader, uniqueMinedNullifiers, uniqueMinedFeePayers);
|
|
132
|
+
this.#metrics.transactionsRemoved(txHashes.map((hash)=>hash.toBigInt()));
|
|
133
|
+
} catch (err) {
|
|
134
|
+
this.#log.warn('Unexpected error when marking txs as mined', {
|
|
135
|
+
err
|
|
136
|
+
});
|
|
137
|
+
}
|
|
127
138
|
}
|
|
128
|
-
async markMinedAsPending(txHashes) {
|
|
139
|
+
async markMinedAsPending(txHashes, latestBlock) {
|
|
129
140
|
if (txHashes.length === 0) {
|
|
130
141
|
return Promise.resolve();
|
|
131
142
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
143
|
+
try {
|
|
144
|
+
await this.#store.transactionAsync(async ()=>{
|
|
145
|
+
for (const hash of txHashes){
|
|
146
|
+
const key = hash.toString();
|
|
147
|
+
await this.#minedTxHashToBlock.delete(key);
|
|
148
|
+
// Clear soft-delete metadata if this tx was previously soft-deleted,
|
|
149
|
+
// so cleanupDeletedMinedTxs won't later hard-delete it while it's pending
|
|
150
|
+
const deletedBlock = await this.#deletedMinedTxHashes.getAsync(key);
|
|
151
|
+
if (deletedBlock !== undefined) {
|
|
152
|
+
await this.#deletedMinedTxHashes.delete(key);
|
|
153
|
+
await this.#blockToDeletedMinedTxHash.deleteValue(deletedBlock, key);
|
|
154
|
+
}
|
|
155
|
+
// Rehydrate the tx in the in-memory pending txs mapping
|
|
156
|
+
const tx = await this.getTxByHash(hash);
|
|
157
|
+
if (tx) {
|
|
158
|
+
await this.addPendingTxIndicesInDbTx(tx, key);
|
|
159
|
+
}
|
|
142
160
|
}
|
|
143
|
-
}
|
|
144
|
-
await this.#
|
|
145
|
-
})
|
|
146
|
-
|
|
147
|
-
|
|
161
|
+
});
|
|
162
|
+
await this.#evictionManager.evictAfterChainPrune(latestBlock);
|
|
163
|
+
} catch (err) {
|
|
164
|
+
this.#log.warn('Unexpected error when marking mined txs as pending', {
|
|
165
|
+
err
|
|
166
|
+
});
|
|
167
|
+
}
|
|
148
168
|
}
|
|
149
169
|
async getPendingTxHashes() {
|
|
150
170
|
const vals = await toArray(this.#pendingTxPriorityToHash.valuesAsync({
|
|
@@ -152,36 +172,6 @@ import { getPendingTxPriority } from './priority.js';
|
|
|
152
172
|
}));
|
|
153
173
|
return vals.map(TxHash.fromString);
|
|
154
174
|
}
|
|
155
|
-
async getMinedTxHashes() {
|
|
156
|
-
const vals = await toArray(this.#minedTxHashToBlock.entriesAsync());
|
|
157
|
-
return vals.map(([txHash, blockNumber])=>[
|
|
158
|
-
TxHash.fromString(txHash),
|
|
159
|
-
blockNumber
|
|
160
|
-
]);
|
|
161
|
-
}
|
|
162
|
-
async getPendingTxCount() {
|
|
163
|
-
return await this.#pendingTxHashToHeaderHash.sizeAsync() ?? 0;
|
|
164
|
-
}
|
|
165
|
-
async getMinedTxCount() {
|
|
166
|
-
return await this.#minedTxHashToBlock.sizeAsync() ?? 0;
|
|
167
|
-
}
|
|
168
|
-
async getTxStatus(txHash) {
|
|
169
|
-
const key = txHash.toString();
|
|
170
|
-
const [isMined, isKnown, isDeleted] = await Promise.all([
|
|
171
|
-
this.#minedTxHashToBlock.hasAsync(key),
|
|
172
|
-
this.#txs.hasAsync(key),
|
|
173
|
-
this.#deletedMinedTxHashes.hasAsync(key)
|
|
174
|
-
]);
|
|
175
|
-
if (isDeleted) {
|
|
176
|
-
return 'deleted';
|
|
177
|
-
} else if (isMined) {
|
|
178
|
-
return 'mined';
|
|
179
|
-
} else if (isKnown) {
|
|
180
|
-
return 'pending';
|
|
181
|
-
} else {
|
|
182
|
-
return undefined;
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
175
|
/**
|
|
186
176
|
* Checks if a transaction exists in the pool and returns it.
|
|
187
177
|
* @param txHash - The generated tx hash.
|
|
@@ -213,39 +203,70 @@ import { getPendingTxPriority } from './priority.js';
|
|
|
213
203
|
}
|
|
214
204
|
/**
|
|
215
205
|
* Adds a list of transactions to the pool. Duplicates are ignored.
|
|
206
|
+
* Handles nullifier deduplication: if an incoming tx has a nullifier conflict with
|
|
207
|
+
* existing pending txs, it will either replace them (if higher fee) or be rejected.
|
|
216
208
|
* @param txs - An array of txs to be added to the pool.
|
|
217
|
-
* @returns
|
|
209
|
+
* @returns count of added transactions
|
|
218
210
|
*/ async addTxs(txs, opts = {}) {
|
|
211
|
+
if (txs.length === 0) {
|
|
212
|
+
return Promise.resolve(0);
|
|
213
|
+
}
|
|
219
214
|
const addedTxs = [];
|
|
215
|
+
const uniqueFeePayers = [];
|
|
216
|
+
const replacedTxHashes = [];
|
|
220
217
|
const hashesAndStats = txs.map((tx)=>({
|
|
221
218
|
txHash: tx.getTxHash(),
|
|
222
219
|
txStats: tx.getStats()
|
|
223
220
|
}));
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
this.#
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
221
|
+
try {
|
|
222
|
+
await this.#store.transactionAsync(async ()=>{
|
|
223
|
+
for(let i = 0; i < txs.length; i++){
|
|
224
|
+
const tx = txs[i];
|
|
225
|
+
const { txHash, txStats } = hashesAndStats[i];
|
|
226
|
+
const key = txHash.toString();
|
|
227
|
+
if (await this.#txs.hasAsync(key)) {
|
|
228
|
+
this.#log.debug(`Tx ${key} already exists in the pool`);
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
const poolAccess = this.getPreAddPoolAccess();
|
|
232
|
+
const { shouldReject, txHashesToEvict } = await this.#evictionManager.runPreAddRules(tx, poolAccess);
|
|
233
|
+
if (shouldReject) {
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
for (const txHashToEvict of txHashesToEvict){
|
|
237
|
+
const txToDelete = await this.getTxByHash(txHashToEvict);
|
|
238
|
+
if (txToDelete) {
|
|
239
|
+
const evictedKey = txHashToEvict.toString();
|
|
240
|
+
await this.deletePendingTxInDbTx(txToDelete, evictedKey);
|
|
241
|
+
replacedTxHashes.push(txHashToEvict);
|
|
242
|
+
this.#log.verbose(`Evicted tx ${evictedKey} due to higher-fee tx ${key}`);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
this.#log.verbose(`Adding tx ${key} to pool`, {
|
|
246
|
+
eventName: 'tx-added-to-pool',
|
|
247
|
+
...txStats
|
|
248
|
+
});
|
|
249
|
+
await this.#txs.set(key, tx.toBuffer());
|
|
250
|
+
addedTxs.push(tx);
|
|
251
|
+
insertIntoSortedArray(uniqueFeePayers, tx.data.feePayer, (a, b)=>a.toField().cmp(b.toField()), false);
|
|
252
|
+
await this.#pendingTxHashToHistoricalBlockHeaderHash.set(key, (await tx.data.constants.anchorBlockHeader.hash()).toString());
|
|
253
|
+
if (!await this.#minedTxHashToBlock.hasAsync(key)) {
|
|
254
|
+
await this.addPendingTxIndicesInDbTx(tx, key);
|
|
255
|
+
this.#metrics.recordSize(tx);
|
|
256
|
+
}
|
|
243
257
|
}
|
|
244
|
-
})
|
|
245
|
-
await this.#
|
|
246
|
-
|
|
247
|
-
|
|
258
|
+
});
|
|
259
|
+
await this.#evictionManager.evictAfterNewTxs(addedTxs.map(({ txHash })=>txHash), uniqueFeePayers);
|
|
260
|
+
} catch (err) {
|
|
261
|
+
this.#log.warn('Unexpected error when adding txs', {
|
|
262
|
+
err
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
if (replacedTxHashes.length > 0) {
|
|
266
|
+
this.#metrics.transactionsRemoved(replacedTxHashes.map((hash)=>hash.toBigInt()));
|
|
267
|
+
}
|
|
248
268
|
if (addedTxs.length > 0) {
|
|
269
|
+
this.#metrics.transactionsAdded(addedTxs);
|
|
249
270
|
this.emit('txs-added', {
|
|
250
271
|
...opts,
|
|
251
272
|
txs: addedTxs
|
|
@@ -258,51 +279,58 @@ import { getPendingTxPriority } from './priority.js';
|
|
|
258
279
|
* Mined transactions are soft-deleted with a timestamp, pending transactions are permanently deleted.
|
|
259
280
|
* @param txHashes - An array of tx hashes to be deleted from the tx pool.
|
|
260
281
|
* @returns Empty promise.
|
|
261
|
-
*/ deleteTxs(txHashes, opts
|
|
282
|
+
*/ deleteTxs(txHashes, opts) {
|
|
262
283
|
if (txHashes.length === 0) {
|
|
263
284
|
return Promise.resolve();
|
|
264
285
|
}
|
|
265
286
|
const deletedTxs = [];
|
|
266
287
|
const poolDbTx = this.#store.transactionAsync(async ()=>{
|
|
267
|
-
let pendingTxSize = await this.#pendingTxSize.getAsync() ?? 0;
|
|
268
288
|
for (const hash of txHashes){
|
|
269
289
|
const key = hash.toString();
|
|
270
290
|
const tx = await this.getTxByHash(hash);
|
|
271
|
-
if (tx) {
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
await this.#blockToDeletedMinedTxHash.set(minedBlockNumber, key);
|
|
284
|
-
}
|
|
285
|
-
} else {
|
|
286
|
-
// Permanently delete pending transactions
|
|
287
|
-
this.#log.trace(`Deleting pending tx ${key} from pool`);
|
|
288
|
-
pendingTxSize -= tx.getSize();
|
|
289
|
-
await this.removePendingTxIndices(tx, key);
|
|
290
|
-
await this.#txs.delete(key);
|
|
291
|
-
}
|
|
292
|
-
if (!opts.eviction && this.#archivedTxLimit) {
|
|
291
|
+
if (!tx) {
|
|
292
|
+
this.#log.trace(`Skipping deletion of missing tx ${key} from pool`);
|
|
293
|
+
continue;
|
|
294
|
+
}
|
|
295
|
+
const minedBlockNumber = await this.#minedTxHashToBlock.getAsync(key);
|
|
296
|
+
const txIsPending = minedBlockNumber === undefined;
|
|
297
|
+
if (txIsPending) {
|
|
298
|
+
await this.deletePendingTxInDbTx(tx, key);
|
|
299
|
+
} else {
|
|
300
|
+
await this.deleteMinedTx(key, minedBlockNumber, opts?.permanently ?? false);
|
|
301
|
+
const shouldArchiveTx = this.#archivedTxLimit && !opts?.permanently;
|
|
302
|
+
if (shouldArchiveTx) {
|
|
293
303
|
deletedTxs.push(tx);
|
|
294
304
|
}
|
|
295
|
-
} else {
|
|
296
|
-
this.#log.trace(`Skipping deletion of missing tx ${key} from pool`);
|
|
297
305
|
}
|
|
298
306
|
}
|
|
299
|
-
await this.#pendingTxSize.set(pendingTxSize);
|
|
300
307
|
});
|
|
308
|
+
this.#metrics.transactionsRemoved(txHashes.map((hash)=>hash.toBigInt()));
|
|
301
309
|
this.#log.debug(`Deleted ${txHashes.length} txs from pool`, {
|
|
302
310
|
txHashes
|
|
303
311
|
});
|
|
304
312
|
return this.#archivedTxLimit ? poolDbTx.then(()=>this.archiveTxs(deletedTxs)) : poolDbTx;
|
|
305
313
|
}
|
|
314
|
+
async deleteMinedTx(txHash, minedBlockNumber, permanently) {
|
|
315
|
+
await this.#minedTxHashToBlock.delete(txHash);
|
|
316
|
+
if (permanently) {
|
|
317
|
+
this.#log.trace(`Deleting mined tx ${txHash} from pool`);
|
|
318
|
+
await this.#txs.delete(txHash);
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
// Soft-delete mined transactions: remove from mined set but keep in storage
|
|
322
|
+
this.#log.trace(`Soft-deleting mined tx ${txHash} from pool`);
|
|
323
|
+
await this.#deletedMinedTxHashes.set(txHash, minedBlockNumber);
|
|
324
|
+
await this.#blockToDeletedMinedTxHash.set(minedBlockNumber, txHash);
|
|
325
|
+
}
|
|
326
|
+
// Assumes being called within a DB transaction
|
|
327
|
+
async deletePendingTxInDbTx(tx, txHash) {
|
|
328
|
+
// We always permanently delete pending transactions
|
|
329
|
+
this.#log.trace(`Deleting pending tx ${txHash} from pool`);
|
|
330
|
+
await this.removePendingTxIndicesInDbTx(tx, txHash);
|
|
331
|
+
await this.#txs.delete(txHash);
|
|
332
|
+
await this.#pendingTxHashToHistoricalBlockHeaderHash.delete(txHash);
|
|
333
|
+
}
|
|
306
334
|
/**
|
|
307
335
|
* Gets all the transactions stored in the pool.
|
|
308
336
|
* @returns Array of tx objects in the order they were added to the pool.
|
|
@@ -317,45 +345,116 @@ import { getPendingTxPriority } from './priority.js';
|
|
|
317
345
|
const vals = await toArray(this.#txs.keysAsync());
|
|
318
346
|
return vals.map((x)=>TxHash.fromString(x));
|
|
319
347
|
}
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
348
|
+
async getPendingTxInfos() {
|
|
349
|
+
const vals = await toArray(this.#pendingTxPriorityToHash.valuesAsync());
|
|
350
|
+
const results = await Promise.all(vals.map((val)=>this.getPendingTxInfo(TxHash.fromString(val))));
|
|
351
|
+
return results.filter((info)=>info !== undefined);
|
|
352
|
+
}
|
|
353
|
+
async getPendingTxInfo(txHash) {
|
|
354
|
+
let historicalBlockHash = await this.#pendingTxHashToHistoricalBlockHeaderHash.getAsync(txHash.toString());
|
|
355
|
+
// Not all tx might have this index created.
|
|
356
|
+
if (!historicalBlockHash) {
|
|
357
|
+
const tx = await this.getTxByHash(txHash);
|
|
358
|
+
if (!tx) {
|
|
359
|
+
this.#log.warn(`PendingTxInfo:tx ${txHash} not found`);
|
|
360
|
+
return undefined;
|
|
330
361
|
}
|
|
362
|
+
historicalBlockHash = (await tx.data.constants.anchorBlockHeader.hash()).toString();
|
|
363
|
+
await this.#pendingTxHashToHistoricalBlockHeaderHash.set(txHash.toString(), historicalBlockHash);
|
|
331
364
|
}
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
this.#
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
365
|
+
return {
|
|
366
|
+
txHash,
|
|
367
|
+
blockHash: Fr.fromString(historicalBlockHash),
|
|
368
|
+
isEvictable: !this.#nonEvictableTxs.has(txHash.toString())
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
async getPendingTxsReferencingBlocks(blockHashes) {
|
|
372
|
+
const result = [];
|
|
373
|
+
for (const blockHash of blockHashes){
|
|
374
|
+
const chunk = await toArray(this.#historicalHeaderToTxHash.getValuesAsync(blockHash.toString()));
|
|
375
|
+
result.push(...chunk.map((txHash)=>({
|
|
376
|
+
txHash: TxHash.fromString(txHash),
|
|
377
|
+
blockHash,
|
|
378
|
+
isEvictable: !this.#nonEvictableTxs.has(txHash)
|
|
379
|
+
})));
|
|
339
380
|
}
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
381
|
+
return result;
|
|
382
|
+
}
|
|
383
|
+
async getPendingFeePayers() {
|
|
384
|
+
const feePayers = [];
|
|
385
|
+
for await (const feePayer of this.#feePayerToBalanceEntry.keysAsync()){
|
|
386
|
+
const address = AztecAddress.fromString(feePayer);
|
|
387
|
+
insertIntoSortedArray(feePayers, address, (a, b)=>a.toField().cmp(b.toField()), false);
|
|
388
|
+
}
|
|
389
|
+
return feePayers;
|
|
390
|
+
}
|
|
391
|
+
async *getFeePayerTxInfos(feePayer) {
|
|
392
|
+
for await (const value of this.#feePayerToBalanceEntry.getValuesAsync(feePayer.toString())){
|
|
393
|
+
const info = FeePayerTxInfo.decode(value);
|
|
394
|
+
info.isEvictable = !this.#nonEvictableTxs.has(info.txHash.toString());
|
|
395
|
+
yield info;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
async getMinedTxHashes() {
|
|
399
|
+
const vals = await toArray(this.#minedTxHashToBlock.entriesAsync());
|
|
400
|
+
return vals.map(([txHash, blockNumber])=>[
|
|
401
|
+
TxHash.fromString(txHash),
|
|
402
|
+
blockNumber
|
|
403
|
+
]);
|
|
404
|
+
}
|
|
405
|
+
async getPendingTxCount() {
|
|
406
|
+
return await this.#pendingTxPriorityToHash.sizeAsync() ?? 0;
|
|
407
|
+
}
|
|
408
|
+
async getMinedTxCount() {
|
|
409
|
+
return await this.#minedTxHashToBlock.sizeAsync() ?? 0;
|
|
410
|
+
}
|
|
411
|
+
async getTxStatus(txHash) {
|
|
412
|
+
const key = txHash.toString();
|
|
413
|
+
const [isMined, isKnown, isDeleted] = await Promise.all([
|
|
414
|
+
this.#minedTxHashToBlock.hasAsync(key),
|
|
415
|
+
this.#txs.hasAsync(key),
|
|
416
|
+
this.#deletedMinedTxHashes.hasAsync(key)
|
|
417
|
+
]);
|
|
418
|
+
if (isDeleted) {
|
|
419
|
+
return 'deleted';
|
|
420
|
+
} else if (isMined) {
|
|
421
|
+
return 'mined';
|
|
422
|
+
} else if (isKnown) {
|
|
423
|
+
return 'pending';
|
|
424
|
+
} else {
|
|
425
|
+
return undefined;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
updateConfig(cfg) {
|
|
429
|
+
if (typeof cfg.archivedTxLimit === 'number') {
|
|
430
|
+
assert(cfg.archivedTxLimit >= 0, 'archivedTxLimit must be greater or equal to 0');
|
|
431
|
+
this.#archivedTxLimit = cfg.archivedTxLimit;
|
|
432
|
+
}
|
|
433
|
+
if (this.#evictionManager) {
|
|
434
|
+
this.#evictionManager.updateConfig(cfg);
|
|
343
435
|
}
|
|
344
|
-
// deletedMinedCleanupThresholdMs is no longer used in block-based cleanup
|
|
345
436
|
}
|
|
346
437
|
markTxsAsNonEvictable(txHashes) {
|
|
347
438
|
txHashes.forEach((txHash)=>this.#nonEvictableTxs.add(txHash.toString()));
|
|
348
439
|
return Promise.resolve();
|
|
349
440
|
}
|
|
441
|
+
clearNonEvictableTxs() {
|
|
442
|
+
// Clear the non-evictable set after completing the DB updates above.
|
|
443
|
+
// This ensures pinned (non-evictable) txs are protected while we mark mined txs,
|
|
444
|
+
// but they won't remain pinned indefinitely across blocks. Note that eviction rules
|
|
445
|
+
// (including post-mining invalidation) respect the non-evictable flag while it is set.
|
|
446
|
+
this.#nonEvictableTxs.clear();
|
|
447
|
+
return Promise.resolve();
|
|
448
|
+
}
|
|
350
449
|
/**
|
|
351
450
|
* Permanently deletes deleted mined transactions from blocks up to and including the specified block number.
|
|
352
451
|
* @param blockNumber - Block number threshold. Deleted mined txs from this block or earlier will be permanently deleted.
|
|
353
452
|
* @returns The number of transactions permanently deleted.
|
|
354
453
|
*/ async cleanupDeletedMinedTxs(blockNumber) {
|
|
355
454
|
let deletedCount = 0;
|
|
356
|
-
const txHashesToDelete = [];
|
|
357
|
-
const blocksToDelete = [];
|
|
358
455
|
await this.#store.transactionAsync(async ()=>{
|
|
456
|
+
const txHashesToDelete = [];
|
|
457
|
+
const blocksToDelete = [];
|
|
359
458
|
// Iterate through all entries and check block numbers
|
|
360
459
|
for await (const [block, txHash] of this.#blockToDeletedMinedTxHash.entriesAsync()){
|
|
361
460
|
if (block <= blockNumber) {
|
|
@@ -369,6 +468,7 @@ import { getPendingTxPriority } from './priority.js';
|
|
|
369
468
|
deletedCount++;
|
|
370
469
|
}
|
|
371
470
|
}
|
|
471
|
+
this.#metrics.transactionsRemoved(txHashesToDelete);
|
|
372
472
|
// Clean up block-to-hash mapping - delete all values for each block
|
|
373
473
|
for (const block of blocksToDelete){
|
|
374
474
|
const txHashesForBlock = await toArray(this.#blockToDeletedMinedTxHash.getValuesAsync(block));
|
|
@@ -383,13 +483,6 @@ import { getPendingTxPriority } from './priority.js';
|
|
|
383
483
|
return deletedCount;
|
|
384
484
|
}
|
|
385
485
|
/**
|
|
386
|
-
* Creates a GasTxValidator instance.
|
|
387
|
-
* @param db - DB for the validator to use
|
|
388
|
-
* @returns A GasTxValidator instance
|
|
389
|
-
*/ createGasTxValidator(db) {
|
|
390
|
-
return new GasTxValidator(new DatabasePublicStateSource(db), ProtocolContractAddress.FeeJuice, GasFees.empty());
|
|
391
|
-
}
|
|
392
|
-
/**
|
|
393
486
|
* Creates an ArchiveCache instance.
|
|
394
487
|
* @param db - DB for the cache to use
|
|
395
488
|
* @returns An ArchiveCache instance
|
|
@@ -397,36 +490,17 @@ import { getPendingTxPriority } from './priority.js';
|
|
|
397
490
|
return new ArchiveCache(db);
|
|
398
491
|
}
|
|
399
492
|
/**
|
|
400
|
-
* Checks if a cached transaction exists in the in-memory pending tx pool and returns it.
|
|
401
|
-
* Otherwise, it checks the tx pool, updates the pending tx pool, and returns the tx.
|
|
402
|
-
* @param txHash - The generated tx hash.
|
|
403
|
-
* @returns The transaction, if found, 'undefined' otherwise.
|
|
404
|
-
*/ async getPendingTxByHash(txHash) {
|
|
405
|
-
let key;
|
|
406
|
-
if (typeof txHash === 'string') {
|
|
407
|
-
key = txHash;
|
|
408
|
-
txHash = TxHash.fromString(txHash);
|
|
409
|
-
} else {
|
|
410
|
-
key = txHash.toString();
|
|
411
|
-
}
|
|
412
|
-
if (this.#pendingTxs.has(key)) {
|
|
413
|
-
return this.#pendingTxs.get(key);
|
|
414
|
-
}
|
|
415
|
-
const tx = await this.getTxByHash(txHash);
|
|
416
|
-
if (tx) {
|
|
417
|
-
this.#pendingTxs.set(key, tx);
|
|
418
|
-
return tx;
|
|
419
|
-
}
|
|
420
|
-
return undefined;
|
|
421
|
-
}
|
|
422
|
-
/**
|
|
423
493
|
* Archives a list of txs for future reference. The number of archived txs is limited by the specified archivedTxLimit.
|
|
494
|
+
* Note: Pending txs should not be archived, only finalized txs
|
|
424
495
|
* @param txs - The list of transactions to archive.
|
|
425
496
|
* @returns Empty promise.
|
|
426
497
|
*/ async archiveTxs(txs) {
|
|
427
498
|
if (txs.length === 0) {
|
|
428
499
|
return;
|
|
429
500
|
}
|
|
501
|
+
if (this.#archivedTxLimit === 0) {
|
|
502
|
+
return;
|
|
503
|
+
}
|
|
430
504
|
try {
|
|
431
505
|
const txHashes = await Promise.all(txs.map((tx)=>tx.getTxHash()));
|
|
432
506
|
await this.#archive.transactionAsync(async ()=>{
|
|
@@ -465,153 +539,58 @@ import { getPendingTxPriority } from './priority.js';
|
|
|
465
539
|
});
|
|
466
540
|
}
|
|
467
541
|
}
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
numLowPriorityTxsEvicted: 0,
|
|
478
|
-
numNewTxsEvicted: 0
|
|
479
|
-
};
|
|
480
|
-
}
|
|
481
|
-
let numNewTxsEvicted = 0;
|
|
482
|
-
const txsToEvict = [];
|
|
483
|
-
let pendingTxsSize = await this.#pendingTxSize.getAsync() ?? 0;
|
|
484
|
-
if (pendingTxsSize > this.#maxTxPoolSize * this.txPoolOverflowFactor) {
|
|
485
|
-
for await (const txHash of this.#pendingTxPriorityToHash.valuesAsync()){
|
|
486
|
-
if (this.#nonEvictableTxs.has(txHash.toString())) {
|
|
487
|
-
continue;
|
|
488
|
-
}
|
|
489
|
-
const txSize = await this.#pendingTxHashToSize.getAsync(txHash.toString()) ?? (await this.getPendingTxByHash(txHash))?.getSize();
|
|
490
|
-
this.#log.verbose(`Evicting tx ${txHash} from pool due to low priority to satisfy max tx size limit`, {
|
|
491
|
-
txHash,
|
|
492
|
-
txSize
|
|
493
|
-
});
|
|
494
|
-
txsToEvict.push(TxHash.fromString(txHash));
|
|
495
|
-
if (txSize) {
|
|
496
|
-
pendingTxsSize -= txSize;
|
|
497
|
-
if (pendingTxsSize <= this.#maxTxPoolSize) {
|
|
498
|
-
break;
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
numNewTxsEvicted += newTxHashes.filter((txHash)=>txsToEvict.includes(txHash)).length;
|
|
503
|
-
}
|
|
504
|
-
if (txsToEvict.length > 0) {
|
|
505
|
-
await this.deleteTxs(txsToEvict, {
|
|
506
|
-
eviction: true
|
|
507
|
-
});
|
|
542
|
+
// Assumes being called within a DB transaction
|
|
543
|
+
async addPendingTxIndicesInDbTx(tx, txHash) {
|
|
544
|
+
await this.#pendingTxPriorityToHash.set(getPendingTxPriority(tx), txHash);
|
|
545
|
+
await this.#historicalHeaderToTxHash.set((await tx.data.constants.anchorBlockHeader.hash()).toString(), txHash);
|
|
546
|
+
await this.#feePayerToBalanceEntry.set(tx.data.feePayer.toString(), await FeePayerTxInfo.encode(tx, txHash));
|
|
547
|
+
// Add nullifier entries for conflict detection
|
|
548
|
+
const nullifiers = tx.data.getNonEmptyNullifiers();
|
|
549
|
+
for (const nullifier of nullifiers){
|
|
550
|
+
await this.#pendingNullifierToTxHash.set(nullifier.toString(), txHash);
|
|
508
551
|
}
|
|
509
|
-
return {
|
|
510
|
-
numLowPriorityTxsEvicted: txsToEvict.length,
|
|
511
|
-
numNewTxsEvicted
|
|
512
|
-
};
|
|
513
552
|
}
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
* @returns The total number of txs evicted from the pool.
|
|
524
|
-
*/ async evictInvalidTxsAfterMining(minedTxHashes, blockHeader, minedNullifiers, minedFeePayers) {
|
|
525
|
-
if (minedTxHashes.length === 0) {
|
|
526
|
-
return 0;
|
|
527
|
-
}
|
|
528
|
-
const { blockNumber, timestamp } = blockHeader.globalVariables;
|
|
529
|
-
// Wait for world state to be synced to at least the mined block number
|
|
530
|
-
await this.#worldStateSynchronizer.syncImmediate(blockNumber);
|
|
531
|
-
const db = this.#worldStateSynchronizer.getCommitted();
|
|
532
|
-
const gasTxValidator = this.createGasTxValidator(db);
|
|
533
|
-
const txsToEvict = [];
|
|
534
|
-
for await (const txHash of this.#pendingTxPriorityToHash.valuesAsync()){
|
|
535
|
-
const tx = await this.getPendingTxByHash(txHash);
|
|
536
|
-
if (!tx) {
|
|
537
|
-
continue;
|
|
538
|
-
}
|
|
539
|
-
// Evict pending txs that share nullifiers with mined txs
|
|
540
|
-
const txNullifiers = tx.data.getNonEmptyNullifiers();
|
|
541
|
-
if (txNullifiers.some((nullifier)=>minedNullifiers.has(nullifier.toString()))) {
|
|
542
|
-
this.#log.verbose(`Evicting tx ${txHash} from pool due to a duplicate nullifier with a mined tx`);
|
|
543
|
-
txsToEvict.push(TxHash.fromString(txHash));
|
|
544
|
-
continue;
|
|
545
|
-
}
|
|
546
|
-
// Evict pending txs with an insufficient fee payer balance
|
|
547
|
-
if (minedFeePayers.has(tx.data.feePayer.toString()) && (await gasTxValidator.validateTxFee(tx)).result === 'invalid') {
|
|
548
|
-
this.#log.verbose(`Evicting tx ${txHash} from pool due to an insufficient fee payer balance`);
|
|
549
|
-
txsToEvict.push(TxHash.fromString(txHash));
|
|
550
|
-
continue;
|
|
551
|
-
}
|
|
552
|
-
// Evict pending txs with an expiration timestamp less than or equal to the mined block timestamp
|
|
553
|
-
const includeByTimestamp = tx.data.includeByTimestamp;
|
|
554
|
-
if (includeByTimestamp <= timestamp) {
|
|
555
|
-
this.#log.verbose(`Evicting tx ${txHash} from pool due to the tx being expired (includeByTimestamp: ${includeByTimestamp}, mined block timestamp: ${timestamp})`);
|
|
556
|
-
txsToEvict.push(TxHash.fromString(txHash));
|
|
557
|
-
continue;
|
|
558
|
-
}
|
|
559
|
-
}
|
|
560
|
-
if (txsToEvict.length > 0) {
|
|
561
|
-
await this.deleteTxs(txsToEvict, {
|
|
562
|
-
eviction: true
|
|
563
|
-
});
|
|
553
|
+
// Assumes being called within a DB transaction
|
|
554
|
+
async removePendingTxIndicesInDbTx(tx, txHash) {
|
|
555
|
+
await this.#pendingTxPriorityToHash.deleteValue(getPendingTxPriority(tx), txHash);
|
|
556
|
+
await this.#historicalHeaderToTxHash.deleteValue((await tx.data.constants.anchorBlockHeader.hash()).toString(), txHash);
|
|
557
|
+
await this.#feePayerToBalanceEntry.deleteValue(tx.data.feePayer.toString(), await FeePayerTxInfo.encode(tx, txHash));
|
|
558
|
+
// Remove nullifier entries
|
|
559
|
+
const nullifiers = tx.data.getNonEmptyNullifiers();
|
|
560
|
+
for (const nullifier of nullifiers){
|
|
561
|
+
await this.#pendingNullifierToTxHash.delete(nullifier.toString());
|
|
564
562
|
}
|
|
565
|
-
return txsToEvict.length;
|
|
566
563
|
}
|
|
567
564
|
/**
|
|
568
|
-
*
|
|
569
|
-
*
|
|
570
|
-
|
|
571
|
-
* @returns The total number of txs evicted from the pool.
|
|
572
|
-
*/ async evictInvalidTxsAfterReorg(txHashes) {
|
|
573
|
-
if (txHashes.length === 0) {
|
|
574
|
-
return 0;
|
|
575
|
-
}
|
|
576
|
-
await this.#worldStateSynchronizer.syncImmediate();
|
|
577
|
-
const db = this.#worldStateSynchronizer.getCommitted();
|
|
578
|
-
const archiveCache = this.createArchiveCache(db);
|
|
579
|
-
const gasTxValidator = this.createGasTxValidator(db);
|
|
565
|
+
* Returns up to `limit` lowest-priority evictable pending tx hashes without hydrating transactions.
|
|
566
|
+
* Iterates the priority index in ascending order and skips non-evictable txs.
|
|
567
|
+
*/ async getLowestPriorityEvictable(limit) {
|
|
580
568
|
const txsToEvict = [];
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
const [index] = await archiveCache.getArchiveIndices([
|
|
587
|
-
Fr.fromString(headerHash)
|
|
588
|
-
]);
|
|
589
|
-
if (index === undefined) {
|
|
590
|
-
this.#log.verbose(`Evicting tx ${txHash} from pool due to an invalid archive root`);
|
|
591
|
-
txsToEvict.push(TxHash.fromString(txHash));
|
|
569
|
+
if (limit <= 0) {
|
|
570
|
+
return txsToEvict;
|
|
571
|
+
}
|
|
572
|
+
for await (const txHashStr of this.#pendingTxPriorityToHash.valuesAsync()){
|
|
573
|
+
if (this.#nonEvictableTxs.has(txHashStr)) {
|
|
592
574
|
continue;
|
|
593
575
|
}
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
576
|
+
txsToEvict.push(TxHash.fromString(txHashStr));
|
|
577
|
+
if (txsToEvict.length >= limit) {
|
|
578
|
+
break;
|
|
597
579
|
}
|
|
598
580
|
}
|
|
599
|
-
|
|
600
|
-
await this.deleteTxs(txsToEvict, {
|
|
601
|
-
eviction: true
|
|
602
|
-
});
|
|
603
|
-
}
|
|
604
|
-
return txsToEvict.length;
|
|
581
|
+
return txsToEvict;
|
|
605
582
|
}
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
583
|
+
/**
|
|
584
|
+
* Creates a PreAddPoolAccess object for use by pre-add eviction rules.
|
|
585
|
+
* Provides read-only access to pool state during addTxs transaction.
|
|
586
|
+
*/ getPreAddPoolAccess() {
|
|
587
|
+
return {
|
|
588
|
+
getTxHashByNullifier: async (nullifier)=>{
|
|
589
|
+
const hashStr = await this.#pendingNullifierToTxHash.getAsync(nullifier.toString());
|
|
590
|
+
return hashStr ? TxHash.fromString(hashStr) : undefined;
|
|
591
|
+
},
|
|
592
|
+
getPendingTxByHash: this.getTxByHash.bind(this),
|
|
593
|
+
getTxPriority: getPendingTxPriority
|
|
594
|
+
};
|
|
616
595
|
}
|
|
617
596
|
}
|