@aztec/p2p 0.0.1-commit.9b94fc1 → 0.0.1-commit.c7c42ec
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/p2p_client.d.ts +7 -7
- package/dest/client/p2p_client.d.ts.map +1 -1
- package/dest/client/p2p_client.js +30 -28
- package/dest/config.d.ts +4 -7
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +8 -11
- package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.js +2 -2
- package/dest/mem_pools/attestation_pool/kv_attestation_pool.js +1 -1
- package/dest/mem_pools/attestation_pool/mocks.d.ts +3 -3
- package/dest/mem_pools/attestation_pool/mocks.d.ts.map +1 -1
- package/dest/mem_pools/attestation_pool/mocks.js +1 -1
- 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 +29 -2
- 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 +30 -25
- 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 +262 -324
- package/dest/mem_pools/tx_pool/eviction/eviction_manager.d.ts +18 -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 +56 -0
- package/dest/mem_pools/tx_pool/eviction/eviction_strategy.d.ts +83 -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 +5 -0
- package/dest/mem_pools/tx_pool/eviction/insufficient_fee_payer_balance_rule.d.ts +15 -0
- package/dest/mem_pools/tx_pool/eviction/insufficient_fee_payer_balance_rule.d.ts.map +1 -0
- package/dest/mem_pools/tx_pool/eviction/insufficient_fee_payer_balance_rule.js +88 -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 +76 -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/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.js +1 -1
- package/dest/msg_validators/block_proposal_validator/block_proposal_validator.js +1 -1
- 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/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/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/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/encoding.js +1 -1
- package/dest/services/libp2p/libp2p_service.d.ts +4 -4
- package/dest/services/libp2p/libp2p_service.d.ts.map +1 -1
- package/dest/services/libp2p/libp2p_service.js +14 -19
- 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 +17 -0
- package/dest/services/peer-manager/peer_manager.d.ts +1 -1
- package/dest/services/peer-manager/peer_manager.d.ts.map +1 -1
- package/dest/services/peer-manager/peer_manager.js +4 -2
- 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/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/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 +4 -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/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/slow_tx_collection.d.ts +6 -5
- package/dest/services/tx_collection/slow_tx_collection.d.ts.map +1 -1
- package/dest/services/tx_collection/tx_collection.d.ts +11 -10
- package/dest/services/tx_collection/tx_collection.d.ts.map +1 -1
- package/dest/services/tx_provider.d.ts +6 -4
- 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 +14 -1
- 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 +1 -0
- package/package.json +16 -16
- package/src/client/factory.ts +5 -10
- package/src/client/p2p_client.ts +52 -55
- package/src/config.ts +10 -16
- package/src/mem_pools/attestation_pool/attestation_pool_test_suite.ts +2 -2
- package/src/mem_pools/attestation_pool/kv_attestation_pool.ts +1 -1
- package/src/mem_pools/attestation_pool/mocks.ts +2 -2
- package/src/mem_pools/instrumentation.ts +33 -0
- package/src/mem_pools/interface.ts +2 -4
- package/src/mem_pools/tx_pool/README.md +255 -0
- package/src/mem_pools/tx_pool/aztec_kv_tx_pool.ts +314 -373
- package/src/mem_pools/tx_pool/eviction/eviction_manager.ts +71 -0
- package/src/mem_pools/tx_pool/eviction/eviction_strategy.ts +93 -0
- package/src/mem_pools/tx_pool/eviction/insufficient_fee_payer_balance_rule.ts +108 -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 +91 -0
- package/src/mem_pools/tx_pool/eviction/low_priority_eviction_rule.ts +106 -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 +1 -1
- package/src/msg_validators/block_proposal_validator/block_proposal_validator.ts +1 -1
- package/src/msg_validators/tx_validator/archive_cache.ts +1 -1
- package/src/msg_validators/tx_validator/block_header_validator.ts +1 -1
- package/src/msg_validators/tx_validator/factory.ts +3 -2
- package/src/msg_validators/tx_validator/metadata_validator.ts +1 -1
- package/src/msg_validators/tx_validator/test_utils.ts +1 -1
- package/src/msg_validators/tx_validator/timestamp_validator.ts +2 -1
- package/src/services/encoding.ts +1 -1
- package/src/services/libp2p/libp2p_service.ts +23 -28
- package/src/services/peer-manager/metrics.ts +22 -0
- package/src/services/peer-manager/peer_manager.ts +4 -2
- package/src/services/reqresp/interface.ts +1 -1
- 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/block_txs_reqresp.ts +1 -1
- package/src/services/reqresp/protocols/status.ts +9 -8
- package/src/services/reqresp/protocols/tx.ts +1 -2
- 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/slow_tx_collection.ts +5 -5
- package/src/services/tx_collection/tx_collection.ts +10 -9
- package/src/services/tx_provider.ts +21 -5
- package/src/services/tx_provider_instrumentation.ts +19 -2
- 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 +2 -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/src/mem_pools/tx_pool/memory_tx_pool.ts +0 -283
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { EpochNumber, SlotNumber } from '@aztec/foundation/branded-types';
|
|
1
|
+
import { BlockNumber, EpochNumber, SlotNumber } from '@aztec/foundation/branded-types';
|
|
2
2
|
import { chunk } from '@aztec/foundation/collection';
|
|
3
3
|
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
4
4
|
import { boundInclusive } from '@aztec/foundation/number';
|
|
5
5
|
import { RunningPromise } from '@aztec/foundation/promise';
|
|
6
6
|
import { DateProvider } from '@aztec/foundation/timer';
|
|
7
|
-
import type {
|
|
7
|
+
import type { L2BlockNew } from '@aztec/stdlib/block';
|
|
8
8
|
import { type L1RollupConstants, getEpochAtSlot, getTimestampRangeForEpoch } from '@aztec/stdlib/epoch-helpers';
|
|
9
9
|
import { type Tx, TxHash } from '@aztec/stdlib/tx';
|
|
10
10
|
|
|
@@ -76,7 +76,7 @@ export class SlowTxCollection {
|
|
|
76
76
|
}
|
|
77
77
|
|
|
78
78
|
/** Starts collecting the given tx hashes for the given L2Block in the slow loop */
|
|
79
|
-
public startCollecting(block:
|
|
79
|
+
public startCollecting(block: L2BlockNew, txHashes: TxHash[]) {
|
|
80
80
|
const slot = block.header.getSlot();
|
|
81
81
|
const deadline = this.getDeadlineForSlot(slot);
|
|
82
82
|
if (+deadline < this.dateProvider.now()) {
|
|
@@ -203,7 +203,7 @@ export class SlowTxCollection {
|
|
|
203
203
|
* Stop collecting all txs for blocks less than or requal to the block number specified.
|
|
204
204
|
* To be called when we no longer care about gathering txs up to a certain block, eg when they become proven or finalized.
|
|
205
205
|
*/
|
|
206
|
-
public stopCollectingForBlocksUpTo(blockNumber:
|
|
206
|
+
public stopCollectingForBlocksUpTo(blockNumber: BlockNumber): void {
|
|
207
207
|
for (const [txHash, info] of this.missingTxs.entries()) {
|
|
208
208
|
if (info.blockNumber <= blockNumber) {
|
|
209
209
|
this.missingTxs.delete(txHash);
|
|
@@ -215,7 +215,7 @@ export class SlowTxCollection {
|
|
|
215
215
|
* Stop collecting all txs for blocks greater than the block number specified.
|
|
216
216
|
* To be called when there is a chain prune and previously mined txs are no longer relevant.
|
|
217
217
|
*/
|
|
218
|
-
public stopCollectingForBlocksAfter(blockNumber:
|
|
218
|
+
public stopCollectingForBlocksAfter(blockNumber: BlockNumber): void {
|
|
219
219
|
for (const [txHash, info] of this.missingTxs.entries()) {
|
|
220
220
|
if (info.blockNumber > blockNumber) {
|
|
221
221
|
this.missingTxs.delete(txHash);
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
import { BlockNumber } from '@aztec/foundation/branded-types';
|
|
1
2
|
import { compactArray } from '@aztec/foundation/collection';
|
|
2
3
|
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
3
4
|
import { type PromiseWithResolvers, RunningPromise } from '@aztec/foundation/promise';
|
|
4
5
|
import { DateProvider } from '@aztec/foundation/timer';
|
|
5
|
-
import type {
|
|
6
|
+
import type { L2BlockInfo, L2BlockNew } from '@aztec/stdlib/block';
|
|
6
7
|
import type { L1RollupConstants } from '@aztec/stdlib/epoch-helpers';
|
|
7
8
|
import type { BlockProposal } from '@aztec/stdlib/p2p';
|
|
8
9
|
import { Tx, TxHash } from '@aztec/stdlib/tx';
|
|
@@ -21,11 +22,11 @@ import type { TxSource } from './tx_source.js';
|
|
|
21
22
|
|
|
22
23
|
export type CollectionMethod = 'fast-req-resp' | 'fast-node-rpc' | 'slow-req-resp' | 'slow-node-rpc';
|
|
23
24
|
|
|
24
|
-
export type MissingTxInfo = { blockNumber:
|
|
25
|
+
export type MissingTxInfo = { blockNumber: BlockNumber; deadline: Date; readyForReqResp: boolean };
|
|
25
26
|
|
|
26
27
|
export type FastCollectionRequestInput =
|
|
27
|
-
| { type: 'block'; block:
|
|
28
|
-
| { type: 'proposal'; blockProposal: BlockProposal; blockNumber:
|
|
28
|
+
| { type: 'block'; block: L2BlockNew }
|
|
29
|
+
| { type: 'proposal'; blockProposal: BlockProposal; blockNumber: BlockNumber };
|
|
29
30
|
|
|
30
31
|
export type FastCollectionRequest = FastCollectionRequestInput & {
|
|
31
32
|
missingTxHashes: Set<string>;
|
|
@@ -145,14 +146,14 @@ export class TxCollection {
|
|
|
145
146
|
}
|
|
146
147
|
|
|
147
148
|
/** Starts collecting the given tx hashes for the given L2Block in the slow loop */
|
|
148
|
-
public startCollecting(block:
|
|
149
|
+
public startCollecting(block: L2BlockNew, txHashes: TxHash[]) {
|
|
149
150
|
return this.slowCollection.startCollecting(block, txHashes);
|
|
150
151
|
}
|
|
151
152
|
|
|
152
153
|
/** Collects the set of txs for the given block proposal as fast as possible */
|
|
153
154
|
public collectFastForProposal(
|
|
154
155
|
blockProposal: BlockProposal,
|
|
155
|
-
blockNumber:
|
|
156
|
+
blockNumber: BlockNumber,
|
|
156
157
|
txHashes: TxHash[] | string[],
|
|
157
158
|
opts: { deadline: Date; pinnedPeer?: PeerId },
|
|
158
159
|
) {
|
|
@@ -161,7 +162,7 @@ export class TxCollection {
|
|
|
161
162
|
|
|
162
163
|
/** Collects the set of txs for the given mined block as fast as possible */
|
|
163
164
|
public collectFastForBlock(
|
|
164
|
-
block:
|
|
165
|
+
block: L2BlockNew,
|
|
165
166
|
txHashes: TxHash[] | string[],
|
|
166
167
|
opts: { deadline: Date; pinnedPeer?: PeerId },
|
|
167
168
|
) {
|
|
@@ -187,7 +188,7 @@ export class TxCollection {
|
|
|
187
188
|
* Stop collecting all txs for blocks less than or requal to the block number specified.
|
|
188
189
|
* To be called when we no longer care about gathering txs up to a certain block, eg when they become proven or finalized.
|
|
189
190
|
*/
|
|
190
|
-
public stopCollectingForBlocksUpTo(blockNumber:
|
|
191
|
+
public stopCollectingForBlocksUpTo(blockNumber: BlockNumber): void {
|
|
191
192
|
this.slowCollection.stopCollectingForBlocksUpTo(blockNumber);
|
|
192
193
|
this.fastCollection.stopCollectingForBlocksUpTo(blockNumber);
|
|
193
194
|
}
|
|
@@ -196,7 +197,7 @@ export class TxCollection {
|
|
|
196
197
|
* Stop collecting all txs for blocks greater than the block number specified.
|
|
197
198
|
* To be called when there is a chain prune and previously mined txs are no longer relevant.
|
|
198
199
|
*/
|
|
199
|
-
public stopCollectingForBlocksAfter(blockNumber:
|
|
200
|
+
public stopCollectingForBlocksAfter(blockNumber: BlockNumber): void {
|
|
200
201
|
this.slowCollection.stopCollectingForBlocksAfter(blockNumber);
|
|
201
202
|
this.fastCollection.stopCollectingForBlocksAfter(blockNumber);
|
|
202
203
|
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import { BlockNumber } from '@aztec/foundation/branded-types';
|
|
1
2
|
import { compactArray } from '@aztec/foundation/collection';
|
|
2
3
|
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
3
4
|
import { elapsed } from '@aztec/foundation/timer';
|
|
4
|
-
import type {
|
|
5
|
+
import type { L2BlockInfo, L2BlockNew } from '@aztec/stdlib/block';
|
|
5
6
|
import type { ITxProvider } from '@aztec/stdlib/interfaces/server';
|
|
6
7
|
import type { BlockProposal } from '@aztec/stdlib/p2p';
|
|
7
8
|
import { Tx, TxHash } from '@aztec/stdlib/tx';
|
|
@@ -55,7 +56,7 @@ export class TxProvider implements ITxProvider {
|
|
|
55
56
|
/** Gathers txs from the tx pool, proposal body, remote rpc nodes, and reqresp. */
|
|
56
57
|
public getTxsForBlockProposal(
|
|
57
58
|
blockProposal: BlockProposal,
|
|
58
|
-
blockNumber:
|
|
59
|
+
blockNumber: BlockNumber,
|
|
59
60
|
opts: { pinnedPeer: PeerId | undefined; deadline: Date },
|
|
60
61
|
): Promise<{ txs: Tx[]; missingTxs: TxHash[] }> {
|
|
61
62
|
return this.getOrderedTxsFromAllSources(
|
|
@@ -67,7 +68,7 @@ export class TxProvider implements ITxProvider {
|
|
|
67
68
|
}
|
|
68
69
|
|
|
69
70
|
/** Gathers txs from the tx pool, remote rpc nodes, and reqresp. */
|
|
70
|
-
public getTxsForBlock(block:
|
|
71
|
+
public getTxsForBlock(block: L2BlockNew, opts: { deadline: Date }): Promise<{ txs: Tx[]; missingTxs: TxHash[] }> {
|
|
71
72
|
return this.getOrderedTxsFromAllSources(
|
|
72
73
|
{ type: 'block', block },
|
|
73
74
|
block.toBlockInfo(),
|
|
@@ -136,6 +137,7 @@ export class TxProvider implements ITxProvider {
|
|
|
136
137
|
);
|
|
137
138
|
|
|
138
139
|
if (missingTxHashes.size === 0) {
|
|
140
|
+
this.instrumentation.incTxsFromP2P(0, txHashes.length);
|
|
139
141
|
return { txsFromMempool };
|
|
140
142
|
}
|
|
141
143
|
|
|
@@ -154,24 +156,26 @@ export class TxProvider implements ITxProvider {
|
|
|
154
156
|
|
|
155
157
|
if (missingTxHashes.size === 0) {
|
|
156
158
|
await this.processProposalTxs(txsFromProposal);
|
|
159
|
+
this.instrumentation.incTxsFromP2P(0, txHashes.length);
|
|
157
160
|
return { txsFromMempool, txsFromProposal };
|
|
158
161
|
}
|
|
159
162
|
|
|
160
163
|
// Start tx collection from the network if needed, while we validate the txs taken from the proposal in parallel
|
|
161
164
|
const [txsFromNetwork] = await Promise.all([
|
|
162
|
-
this.
|
|
165
|
+
this.collectFromP2P(request, [...missingTxHashes], opts),
|
|
163
166
|
this.processProposalTxs(txsFromProposal),
|
|
164
167
|
] as const);
|
|
165
168
|
|
|
166
169
|
if (txsFromNetwork.length > 0) {
|
|
167
170
|
txsFromNetwork.forEach(tx => missingTxHashes.delete(tx.txHash.toString()));
|
|
168
|
-
this.instrumentation.incTxsFromP2P(txsFromNetwork.length);
|
|
169
171
|
this.log.debug(
|
|
170
172
|
`Retrieved ${txsFromNetwork.length} txs from network for block proposal (${missingTxHashes.size} pending)`,
|
|
171
173
|
{ ...blockInfo, missingTxHashes: [...missingTxHashes] },
|
|
172
174
|
);
|
|
173
175
|
}
|
|
174
176
|
|
|
177
|
+
this.instrumentation.incTxsFromP2P(txsFromNetwork.length, txHashes.length);
|
|
178
|
+
|
|
175
179
|
if (missingTxHashes.size === 0) {
|
|
176
180
|
return { txsFromNetwork, txsFromMempool, txsFromProposal };
|
|
177
181
|
}
|
|
@@ -199,6 +203,18 @@ export class TxProvider implements ITxProvider {
|
|
|
199
203
|
};
|
|
200
204
|
}
|
|
201
205
|
|
|
206
|
+
private async collectFromP2P(
|
|
207
|
+
input: FastCollectionRequestInput,
|
|
208
|
+
txHashes: TxHash[] | string[],
|
|
209
|
+
opts: { deadline: Date; pinnedPeer?: PeerId },
|
|
210
|
+
): Promise<Tx[]> {
|
|
211
|
+
const requestedAt = Date.now();
|
|
212
|
+
const result = await this.txCollection.collectFastFor(input, txHashes, opts);
|
|
213
|
+
const requestProcessedAt = Date.now();
|
|
214
|
+
this.instrumentation.recordTxsRequestDelay(requestProcessedAt - requestedAt);
|
|
215
|
+
return result;
|
|
216
|
+
}
|
|
217
|
+
|
|
202
218
|
private extractFromProposal(proposal: BlockProposal | undefined, missingTxHashes: string[]): Tx[] {
|
|
203
219
|
if (!proposal) {
|
|
204
220
|
return [];
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Metrics, type TelemetryClient, type UpDownCounter } from '@aztec/telemetry-client';
|
|
1
|
+
import { type Histogram, Metrics, type TelemetryClient, type UpDownCounter } from '@aztec/telemetry-client';
|
|
2
2
|
|
|
3
3
|
export class TxProviderInstrumentation {
|
|
4
4
|
private txFromProposalCount: UpDownCounter;
|
|
@@ -6,6 +6,9 @@ export class TxProviderInstrumentation {
|
|
|
6
6
|
private txFromP2PCount: UpDownCounter;
|
|
7
7
|
private missingTxsCount: UpDownCounter;
|
|
8
8
|
|
|
9
|
+
private fractionOfTxsRequestedFromP2P: Histogram;
|
|
10
|
+
private txsRequestDelay: Histogram;
|
|
11
|
+
|
|
9
12
|
constructor(client: TelemetryClient, name: string) {
|
|
10
13
|
const meter = client.getMeter(name);
|
|
11
14
|
|
|
@@ -24,6 +27,15 @@ export class TxProviderInstrumentation {
|
|
|
24
27
|
this.missingTxsCount = meter.createUpDownCounter(Metrics.TX_PROVIDER_MISSING_TXS_COUNT, {
|
|
25
28
|
description: 'The number of txs not found anywhere',
|
|
26
29
|
});
|
|
30
|
+
|
|
31
|
+
this.fractionOfTxsRequestedFromP2P = meter.createHistogram(Metrics.TX_PROVIDER_P2P_TXS_REQUESTED_FRACTION, {
|
|
32
|
+
description: 'The fraction of transaction requested from peers',
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
this.txsRequestDelay = meter.createHistogram(Metrics.TX_PROVIDER_P2P_TXS_REQUEST_DELAY, {
|
|
36
|
+
unit: 'ms',
|
|
37
|
+
description: 'The time it took to request missing transactions from p2p',
|
|
38
|
+
});
|
|
27
39
|
}
|
|
28
40
|
|
|
29
41
|
incTxsFromProposals(count: number) {
|
|
@@ -34,8 +46,13 @@ export class TxProviderInstrumentation {
|
|
|
34
46
|
this.txFromMempoolCount.add(count);
|
|
35
47
|
}
|
|
36
48
|
|
|
37
|
-
incTxsFromP2P(count: number) {
|
|
49
|
+
incTxsFromP2P(count: number, total: number) {
|
|
38
50
|
this.txFromP2PCount.add(count);
|
|
51
|
+
this.fractionOfTxsRequestedFromP2P.record(count / total);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
recordTxsRequestDelay(delay: number) {
|
|
55
|
+
this.txsRequestDelay.record(delay);
|
|
39
56
|
}
|
|
40
57
|
|
|
41
58
|
incMissingTxs(count: number) {
|
|
@@ -39,7 +39,7 @@ export function getMockPubSubP2PServiceFactory<T extends P2PClientType>(
|
|
|
39
39
|
peerId: PeerId,
|
|
40
40
|
deps: {
|
|
41
41
|
packageVersion: string;
|
|
42
|
-
mempools: MemPools
|
|
42
|
+
mempools: MemPools;
|
|
43
43
|
l2BlockSource: L2BlockSource & ContractDataSource;
|
|
44
44
|
epochCache: EpochCacheInterface;
|
|
45
45
|
proofVerifier: ClientProtocolCircuitVerifier;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Fr } from '@aztec/foundation/
|
|
1
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
2
2
|
import { getVKTreeRoot } from '@aztec/noir-protocol-circuits-types/vk-tree';
|
|
3
3
|
import { protocolContractsHash } from '@aztec/protocol-contracts';
|
|
4
4
|
import { mockTx } from '@aztec/stdlib/testing';
|
|
@@ -112,7 +112,7 @@ export async function createTestLibP2PService<T extends P2PClientType>(
|
|
|
112
112
|
archiver: L2BlockSource & ContractDataSource,
|
|
113
113
|
worldStateSynchronizer: WorldStateSynchronizer,
|
|
114
114
|
epochCache: EpochCache,
|
|
115
|
-
mempools: MemPools
|
|
115
|
+
mempools: MemPools,
|
|
116
116
|
telemetry: TelemetryClient,
|
|
117
117
|
port: number = 0,
|
|
118
118
|
peerId?: PeerId,
|
|
@@ -56,6 +56,7 @@ function mockTxPool(): TxPool {
|
|
|
56
56
|
hasTx: () => Promise.resolve(false),
|
|
57
57
|
updateConfig: () => {},
|
|
58
58
|
markTxsAsNonEvictable: () => Promise.resolve(),
|
|
59
|
+
clearNonEvictableTxs: () => Promise.resolve(),
|
|
59
60
|
cleanupDeletedMinedTxs: () => Promise.resolve(0),
|
|
60
61
|
};
|
|
61
62
|
return Object.assign(new EventEmitter(), pool);
|
|
@@ -126,7 +127,7 @@ class TestLibP2PService<T extends P2PClientType = P2PClientType.Full> extends Li
|
|
|
126
127
|
peerDiscoveryService: PeerDiscoveryService,
|
|
127
128
|
reqresp: ReqResp,
|
|
128
129
|
peerManager: PeerManager,
|
|
129
|
-
mempools: MemPools
|
|
130
|
+
mempools: MemPools,
|
|
130
131
|
archiver: L2BlockSource & ContractDataSource,
|
|
131
132
|
epochCache: EpochCacheInterface,
|
|
132
133
|
proofVerifier: ClientProtocolCircuitVerifier,
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import type { TypedEventEmitter } from '@aztec/foundation/types';
|
|
2
|
-
import { BlockHeader, Tx, TxHash } from '@aztec/stdlib/tx';
|
|
3
|
-
import { type TelemetryClient } from '@aztec/telemetry-client';
|
|
4
|
-
import type { TxPool, TxPoolEvents, TxPoolOptions } from './tx_pool.js';
|
|
5
|
-
declare const InMemoryTxPool_base: new () => TypedEventEmitter<TxPoolEvents>;
|
|
6
|
-
/**
|
|
7
|
-
* In-memory implementation of the Transaction Pool.
|
|
8
|
-
*/
|
|
9
|
-
export declare class InMemoryTxPool extends InMemoryTxPool_base implements TxPool {
|
|
10
|
-
private log;
|
|
11
|
-
/**
|
|
12
|
-
* Our tx pool, stored as a Map in-memory, with K: tx hash and V: the transaction.
|
|
13
|
-
*/
|
|
14
|
-
private txs;
|
|
15
|
-
private minedTxs;
|
|
16
|
-
private pendingTxs;
|
|
17
|
-
private deletedMinedTxHashes;
|
|
18
|
-
private blockToDeletedMinedTxHash;
|
|
19
|
-
private metrics;
|
|
20
|
-
/**
|
|
21
|
-
* Class constructor for in-memory TxPool. Initiates our transaction pool as a JS Map.
|
|
22
|
-
* @param log - A logger.
|
|
23
|
-
*/
|
|
24
|
-
constructor(telemetry?: TelemetryClient, log?: import("@aztec/foundation/log").Logger);
|
|
25
|
-
private countTx;
|
|
26
|
-
isEmpty(): Promise<boolean>;
|
|
27
|
-
markAsMined(txHashes: TxHash[], blockHeader: BlockHeader): Promise<void>;
|
|
28
|
-
markMinedAsPending(txHashes: TxHash[]): Promise<void>;
|
|
29
|
-
getPendingTxHashes(): Promise<TxHash[]>;
|
|
30
|
-
getMinedTxHashes(): Promise<[TxHash, number][]>;
|
|
31
|
-
getPendingTxCount(): Promise<number>;
|
|
32
|
-
getTxStatus(txHash: TxHash): Promise<'pending' | 'mined' | 'deleted' | undefined>;
|
|
33
|
-
/**
|
|
34
|
-
* Checks if a transaction exists in the pool and returns it.
|
|
35
|
-
* @param txHash - The generated tx hash.
|
|
36
|
-
* @returns The transaction, if found, 'undefined' otherwise.
|
|
37
|
-
*/
|
|
38
|
-
getTxByHash(txHash: TxHash): Promise<Tx | undefined>;
|
|
39
|
-
getTxsByHash(txHashes: TxHash[]): Promise<(Tx | undefined)[]>;
|
|
40
|
-
hasTxs(txHashes: TxHash[]): Promise<boolean[]>;
|
|
41
|
-
hasTx(txHash: TxHash): Promise<boolean>;
|
|
42
|
-
getArchivedTxByHash(): Promise<Tx | undefined>;
|
|
43
|
-
/**
|
|
44
|
-
* Adds a list of transactions to the pool. Duplicates are ignored.
|
|
45
|
-
* @param txs - An array of txs to be added to the pool.
|
|
46
|
-
* @returns Empty promise.
|
|
47
|
-
*/
|
|
48
|
-
addTxs(txs: Tx[], opts?: {
|
|
49
|
-
source?: string;
|
|
50
|
-
}): Promise<number>;
|
|
51
|
-
/**
|
|
52
|
-
* Deletes transactions from the pool. Tx hashes that are not present are ignored.
|
|
53
|
-
* Mined transactions are soft-deleted with a timestamp, pending transactions are permanently deleted.
|
|
54
|
-
* @param txHashes - An array of tx hashes to be deleted from the tx pool.
|
|
55
|
-
* @returns Empty promise.
|
|
56
|
-
*/
|
|
57
|
-
deleteTxs(txHashes: TxHash[], opts?: {
|
|
58
|
-
permanently?: boolean;
|
|
59
|
-
}): Promise<void>;
|
|
60
|
-
/**
|
|
61
|
-
* Gets all the transactions stored in the pool.
|
|
62
|
-
* @returns Array of tx objects in the order they were added to the pool.
|
|
63
|
-
*/
|
|
64
|
-
getAllTxs(): Promise<Tx[]>;
|
|
65
|
-
/**
|
|
66
|
-
* Gets the hashes of all transactions currently in the tx pool.
|
|
67
|
-
* @returns An array of transaction hashes found in the tx pool.
|
|
68
|
-
*/
|
|
69
|
-
getAllTxHashes(): Promise<TxHash[]>;
|
|
70
|
-
updateConfig(_config: TxPoolOptions): void;
|
|
71
|
-
markTxsAsNonEvictable(_: TxHash[]): Promise<void>;
|
|
72
|
-
/**
|
|
73
|
-
* Permanently deletes deleted mined transactions from blocks up to and including the specified block number.
|
|
74
|
-
* @param blockNumber - Block number threshold. Deleted mined txs from this block or earlier will be permanently deleted.
|
|
75
|
-
* @returns The number of transactions permanently deleted.
|
|
76
|
-
*/
|
|
77
|
-
cleanupDeletedMinedTxs(blockNumber: number): Promise<number>;
|
|
78
|
-
}
|
|
79
|
-
export {};
|
|
80
|
-
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibWVtb3J5X3R4X3Bvb2wuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3NyYy9tZW1fcG9vbHMvdHhfcG9vbC9tZW1vcnlfdHhfcG9vbC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFDQSxPQUFPLEtBQUssRUFBRSxpQkFBaUIsRUFBRSxNQUFNLHlCQUF5QixDQUFDO0FBRWpFLE9BQU8sRUFBRSxXQUFXLEVBQUUsRUFBRSxFQUFFLE1BQU0sRUFBRSxNQUFNLGtCQUFrQixDQUFDO0FBQzNELE9BQU8sRUFBRSxLQUFLLGVBQWUsRUFBc0IsTUFBTSx5QkFBeUIsQ0FBQztBQU1uRixPQUFPLEtBQUssRUFBRSxNQUFNLEVBQUUsWUFBWSxFQUFFLGFBQWEsRUFBRSxNQUFNLGNBQWMsQ0FBQzs7QUFFeEU7O0dBRUc7QUFDSCxxQkFBYSxjQUFlLFNBQVEsbUJBQTRELFlBQVcsTUFBTTtJQWtCN0csT0FBTyxDQUFDLEdBQUc7SUFqQmI7O09BRUc7SUFDSCxPQUFPLENBQUMsR0FBRyxDQUFrQjtJQUM3QixPQUFPLENBQUMsUUFBUSxDQUFzQjtJQUN0QyxPQUFPLENBQUMsVUFBVSxDQUFjO0lBQ2hDLE9BQU8sQ0FBQyxvQkFBb0IsQ0FBc0I7SUFDbEQsT0FBTyxDQUFDLHlCQUF5QixDQUEyQjtJQUU1RCxPQUFPLENBQUMsT0FBTyxDQUEwQjtJQUV6Qzs7O09BR0c7SUFDSCxZQUNFLFNBQVMsR0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GAAG,SAAS,CAAC,CAG1D;IAED,YAAY,CAAC,QAAQ,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,CAAC,EAAE,GAAG,SAAS,CAAC,EAAE,CAAC,CAE5D;IACD,MAAM,CAAC,QAAQ,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC,CAE7C;IAEK,KAAK,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAG5C;IAEM,mBAAmB,IAAI,OAAO,CAAC,EAAE,GAAG,SAAS,CAAC,CAEpD;IAED;;;;OAIG;IACI,MAAM,CAAC,GAAG,EAAE,EAAE,EAAE,EAAE,IAAI,GAAE;QAAE,MAAM,CAAC,EAAE,MAAM,CAAA;KAAO,GAAG,OAAO,CAAC,MAAM,CAAC,CAwBxE;IAED;;;;;OAKG;IACI,SAAS,CAAC,QAAQ,EAAE,MAAM,EAAE,EAAE,IAAI,CAAC,EAAE;QAAE,WAAW,CAAC,EAAE,OAAO,CAAA;KAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CA2BpF;IAED;;;OAGG;IACI,SAAS,IAAI,OAAO,CAAC,EAAE,EAAE,CAAC,CAEhC;IAED;;;OAGG;IACI,cAAc,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC,CAEzC;IAED,YAAY,CAAC,OAAO,EAAE,aAAa,GAAG,IAAI,CAAG;IAE7C,qBAAqB,CAAC,CAAC,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAEhD;IAED;;;;OAIG;IACI,sBAAsB,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CA0BlE;CACF"}
|
|
@@ -1,238 +0,0 @@
|
|
|
1
|
-
import { createLogger } from '@aztec/foundation/log';
|
|
2
|
-
import { Tx, TxHash } from '@aztec/stdlib/tx';
|
|
3
|
-
import { getTelemetryClient } from '@aztec/telemetry-client';
|
|
4
|
-
import EventEmitter from 'node:events';
|
|
5
|
-
import { PoolInstrumentation, PoolName } from '../instrumentation.js';
|
|
6
|
-
import { getPendingTxPriority } from './priority.js';
|
|
7
|
-
/**
|
|
8
|
-
* In-memory implementation of the Transaction Pool.
|
|
9
|
-
*/ export class InMemoryTxPool extends EventEmitter {
|
|
10
|
-
log;
|
|
11
|
-
/**
|
|
12
|
-
* Our tx pool, stored as a Map in-memory, with K: tx hash and V: the transaction.
|
|
13
|
-
*/ txs;
|
|
14
|
-
minedTxs;
|
|
15
|
-
pendingTxs;
|
|
16
|
-
deletedMinedTxHashes;
|
|
17
|
-
blockToDeletedMinedTxHash;
|
|
18
|
-
metrics;
|
|
19
|
-
/**
|
|
20
|
-
* Class constructor for in-memory TxPool. Initiates our transaction pool as a JS Map.
|
|
21
|
-
* @param log - A logger.
|
|
22
|
-
*/ constructor(telemetry = getTelemetryClient(), log = createLogger('p2p:tx_pool')){
|
|
23
|
-
super(), this.log = log, this.countTx = ()=>{
|
|
24
|
-
return Promise.resolve({
|
|
25
|
-
itemCount: {
|
|
26
|
-
mined: this.minedTxs.size,
|
|
27
|
-
pending: this.pendingTxs.size
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
};
|
|
31
|
-
this.txs = new Map();
|
|
32
|
-
this.minedTxs = new Map();
|
|
33
|
-
this.pendingTxs = new Set();
|
|
34
|
-
this.deletedMinedTxHashes = new Map();
|
|
35
|
-
this.blockToDeletedMinedTxHash = new Map();
|
|
36
|
-
this.metrics = new PoolInstrumentation(telemetry, PoolName.TX_POOL, this.countTx);
|
|
37
|
-
}
|
|
38
|
-
countTx;
|
|
39
|
-
isEmpty() {
|
|
40
|
-
return Promise.resolve(this.txs.size === 0);
|
|
41
|
-
}
|
|
42
|
-
markAsMined(txHashes, blockHeader) {
|
|
43
|
-
const keys = txHashes.map((x)=>x.toBigInt());
|
|
44
|
-
for (const key of keys){
|
|
45
|
-
// If this tx was previously soft-deleted, remove it from the deleted sets
|
|
46
|
-
if (this.deletedMinedTxHashes.has(key)) {
|
|
47
|
-
const originalBlock = this.deletedMinedTxHashes.get(key);
|
|
48
|
-
this.deletedMinedTxHashes.delete(key);
|
|
49
|
-
// Remove from block-to-hash mapping
|
|
50
|
-
const txHashesForBlock = this.blockToDeletedMinedTxHash.get(originalBlock);
|
|
51
|
-
if (txHashesForBlock) {
|
|
52
|
-
txHashesForBlock.delete(key);
|
|
53
|
-
if (txHashesForBlock.size === 0) {
|
|
54
|
-
this.blockToDeletedMinedTxHash.delete(originalBlock);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
this.minedTxs.set(key, blockHeader.globalVariables.blockNumber);
|
|
59
|
-
this.pendingTxs.delete(key);
|
|
60
|
-
}
|
|
61
|
-
return Promise.resolve();
|
|
62
|
-
}
|
|
63
|
-
markMinedAsPending(txHashes) {
|
|
64
|
-
if (txHashes.length === 0) {
|
|
65
|
-
return Promise.resolve();
|
|
66
|
-
}
|
|
67
|
-
const keys = txHashes.map((x)=>x.toBigInt());
|
|
68
|
-
for (const key of keys){
|
|
69
|
-
this.minedTxs.delete(key);
|
|
70
|
-
// only add back to the pending set if we have the tx object
|
|
71
|
-
if (this.txs.has(key)) {
|
|
72
|
-
this.pendingTxs.add(key);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
return Promise.resolve();
|
|
76
|
-
}
|
|
77
|
-
async getPendingTxHashes() {
|
|
78
|
-
const txs = (await this.getAllTxs()).sort((tx1, tx2)=>-getPendingTxPriority(tx1).localeCompare(getPendingTxPriority(tx2)));
|
|
79
|
-
const txHashes = await Promise.all(txs.map((tx)=>tx.getTxHash()));
|
|
80
|
-
// No need to check deleted since pending txs are never soft-deleted
|
|
81
|
-
return txHashes.filter((txHash)=>{
|
|
82
|
-
const key = txHash.toBigInt();
|
|
83
|
-
return this.pendingTxs.has(key);
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
getMinedTxHashes() {
|
|
87
|
-
return Promise.resolve(Array.from(this.minedTxs.entries()).map(([txHash, blockNumber])=>[
|
|
88
|
-
TxHash.fromBigInt(txHash),
|
|
89
|
-
blockNumber
|
|
90
|
-
]));
|
|
91
|
-
}
|
|
92
|
-
getPendingTxCount() {
|
|
93
|
-
// Soft-deleted transactions are always mined, never pending
|
|
94
|
-
return Promise.resolve(this.pendingTxs.size);
|
|
95
|
-
}
|
|
96
|
-
getTxStatus(txHash) {
|
|
97
|
-
const key = txHash.toBigInt();
|
|
98
|
-
if (this.deletedMinedTxHashes.has(key)) {
|
|
99
|
-
return Promise.resolve('deleted');
|
|
100
|
-
}
|
|
101
|
-
if (this.minedTxs.has(key)) {
|
|
102
|
-
return Promise.resolve('mined');
|
|
103
|
-
}
|
|
104
|
-
if (this.pendingTxs.has(key)) {
|
|
105
|
-
return Promise.resolve('pending');
|
|
106
|
-
}
|
|
107
|
-
return Promise.resolve(undefined);
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* Checks if a transaction exists in the pool and returns it.
|
|
111
|
-
* @param txHash - The generated tx hash.
|
|
112
|
-
* @returns The transaction, if found, 'undefined' otherwise.
|
|
113
|
-
*/ getTxByHash(txHash) {
|
|
114
|
-
const result = this.txs.get(txHash.toBigInt());
|
|
115
|
-
return Promise.resolve(result === undefined ? undefined : Tx.clone(result));
|
|
116
|
-
}
|
|
117
|
-
getTxsByHash(txHashes) {
|
|
118
|
-
return Promise.all(txHashes.map((txHash)=>this.getTxByHash(txHash)));
|
|
119
|
-
}
|
|
120
|
-
hasTxs(txHashes) {
|
|
121
|
-
return Promise.resolve(txHashes.map((txHash)=>this.txs.has(txHash.toBigInt())));
|
|
122
|
-
}
|
|
123
|
-
async hasTx(txHash) {
|
|
124
|
-
const result = await this.hasTxs([
|
|
125
|
-
txHash
|
|
126
|
-
]);
|
|
127
|
-
return result[0];
|
|
128
|
-
}
|
|
129
|
-
getArchivedTxByHash() {
|
|
130
|
-
return Promise.resolve(undefined);
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Adds a list of transactions to the pool. Duplicates are ignored.
|
|
134
|
-
* @param txs - An array of txs to be added to the pool.
|
|
135
|
-
* @returns Empty promise.
|
|
136
|
-
*/ addTxs(txs, opts = {}) {
|
|
137
|
-
const added = [];
|
|
138
|
-
for (const tx of txs){
|
|
139
|
-
const txHash = tx.getTxHash();
|
|
140
|
-
this.log.verbose(`Adding tx ${txHash.toString()} to pool`, {
|
|
141
|
-
eventName: 'tx-added-to-pool',
|
|
142
|
-
...tx.getStats()
|
|
143
|
-
});
|
|
144
|
-
const key = txHash.toBigInt();
|
|
145
|
-
if (!this.txs.has(key)) {
|
|
146
|
-
added.push(tx);
|
|
147
|
-
this.txs.set(key, tx);
|
|
148
|
-
}
|
|
149
|
-
if (!this.minedTxs.has(key)) {
|
|
150
|
-
this.metrics.recordSize(tx);
|
|
151
|
-
this.pendingTxs.add(key);
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
if (added.length > 0) {
|
|
155
|
-
this.emit('txs-added', {
|
|
156
|
-
...opts,
|
|
157
|
-
txs: added
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
return Promise.resolve(added.length);
|
|
161
|
-
}
|
|
162
|
-
/**
|
|
163
|
-
* Deletes transactions from the pool. Tx hashes that are not present are ignored.
|
|
164
|
-
* Mined transactions are soft-deleted with a timestamp, pending transactions are permanently deleted.
|
|
165
|
-
* @param txHashes - An array of tx hashes to be deleted from the tx pool.
|
|
166
|
-
* @returns Empty promise.
|
|
167
|
-
*/ deleteTxs(txHashes, opts) {
|
|
168
|
-
for (const txHash of txHashes){
|
|
169
|
-
const key = txHash.toBigInt();
|
|
170
|
-
if (this.txs.has(key)) {
|
|
171
|
-
if (this.minedTxs.has(key)) {
|
|
172
|
-
const blockNumber = this.minedTxs.get(key);
|
|
173
|
-
this.minedTxs.delete(key);
|
|
174
|
-
// Soft-delete mined transactions: remove from mined set but keep in storage
|
|
175
|
-
if (opts?.permanently) {
|
|
176
|
-
// Permanently delete mined transactions if specified
|
|
177
|
-
this.txs.delete(key);
|
|
178
|
-
} else {
|
|
179
|
-
this.deletedMinedTxHashes.set(key, blockNumber);
|
|
180
|
-
if (!this.blockToDeletedMinedTxHash.has(blockNumber)) {
|
|
181
|
-
this.blockToDeletedMinedTxHash.set(blockNumber, new Set());
|
|
182
|
-
}
|
|
183
|
-
this.blockToDeletedMinedTxHash.get(blockNumber).add(key);
|
|
184
|
-
}
|
|
185
|
-
} else {
|
|
186
|
-
// Permanently delete pending transactions
|
|
187
|
-
this.txs.delete(key);
|
|
188
|
-
this.pendingTxs.delete(key);
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
return Promise.resolve();
|
|
193
|
-
}
|
|
194
|
-
/**
|
|
195
|
-
* Gets all the transactions stored in the pool.
|
|
196
|
-
* @returns Array of tx objects in the order they were added to the pool.
|
|
197
|
-
*/ getAllTxs() {
|
|
198
|
-
return Promise.resolve(Array.from(this.txs.values()).map((x)=>Tx.clone(x)));
|
|
199
|
-
}
|
|
200
|
-
/**
|
|
201
|
-
* Gets the hashes of all transactions currently in the tx pool.
|
|
202
|
-
* @returns An array of transaction hashes found in the tx pool.
|
|
203
|
-
*/ getAllTxHashes() {
|
|
204
|
-
return Promise.resolve(Array.from(this.txs.keys()).map((x)=>TxHash.fromBigInt(x)));
|
|
205
|
-
}
|
|
206
|
-
updateConfig(_config) {}
|
|
207
|
-
markTxsAsNonEvictable(_) {
|
|
208
|
-
return Promise.resolve();
|
|
209
|
-
}
|
|
210
|
-
/**
|
|
211
|
-
* Permanently deletes deleted mined transactions from blocks up to and including the specified block number.
|
|
212
|
-
* @param blockNumber - Block number threshold. Deleted mined txs from this block or earlier will be permanently deleted.
|
|
213
|
-
* @returns The number of transactions permanently deleted.
|
|
214
|
-
*/ cleanupDeletedMinedTxs(blockNumber) {
|
|
215
|
-
let deletedCount = 0;
|
|
216
|
-
const blocksToDelete = [];
|
|
217
|
-
// Find all blocks up to the specified block number
|
|
218
|
-
for (const [block, txHashes] of this.blockToDeletedMinedTxHash.entries()){
|
|
219
|
-
if (block <= blockNumber) {
|
|
220
|
-
// Permanently delete all transactions from this block
|
|
221
|
-
for (const txHash of txHashes){
|
|
222
|
-
this.txs.delete(txHash);
|
|
223
|
-
this.deletedMinedTxHashes.delete(txHash);
|
|
224
|
-
deletedCount++;
|
|
225
|
-
}
|
|
226
|
-
blocksToDelete.push(block);
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
// Clean up block-to-hash mapping
|
|
230
|
-
for (const block of blocksToDelete){
|
|
231
|
-
this.blockToDeletedMinedTxHash.delete(block);
|
|
232
|
-
}
|
|
233
|
-
if (deletedCount > 0) {
|
|
234
|
-
this.log.debug(`Permanently deleted ${deletedCount} deleted mined txs from blocks up to ${blockNumber}`);
|
|
235
|
-
}
|
|
236
|
-
return Promise.resolve(deletedCount);
|
|
237
|
-
}
|
|
238
|
-
}
|