@aztec/p2p 0.0.1-commit.2e2504e2 → 0.0.1-commit.2eb6648a
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 +3 -3
- package/dest/client/factory.d.ts.map +1 -1
- package/dest/client/factory.js +2 -2
- package/dest/client/interface.d.ts +9 -2
- package/dest/client/interface.d.ts.map +1 -1
- package/dest/client/p2p_client.d.ts +4 -3
- package/dest/client/p2p_client.d.ts.map +1 -1
- package/dest/client/p2p_client.js +11 -5
- package/dest/client/test/tx_proposal_collector/proposal_tx_collector_worker.js +1 -1
- package/dest/config.d.ts +1 -1
- package/dest/mem_pools/attestation_pool/attestation_pool.d.ts +94 -87
- package/dest/mem_pools/attestation_pool/attestation_pool.d.ts.map +1 -1
- package/dest/mem_pools/attestation_pool/attestation_pool.js +411 -3
- package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.d.ts +2 -2
- 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 +351 -85
- package/dest/mem_pools/attestation_pool/index.d.ts +2 -3
- package/dest/mem_pools/attestation_pool/index.d.ts.map +1 -1
- package/dest/mem_pools/attestation_pool/index.js +1 -2
- package/dest/mem_pools/index.d.ts +2 -2
- package/dest/mem_pools/index.d.ts.map +1 -1
- package/dest/mem_pools/index.js +1 -1
- package/dest/mem_pools/interface.d.ts +3 -3
- package/dest/mem_pools/interface.d.ts.map +1 -1
- package/dest/mem_pools/tx_pool_v2/interfaces.d.ts +5 -1
- package/dest/mem_pools/tx_pool_v2/interfaces.d.ts.map +1 -1
- package/dest/mem_pools/tx_pool_v2/tx_metadata.d.ts +1 -1
- package/dest/mem_pools/tx_pool_v2/tx_metadata.d.ts.map +1 -1
- package/dest/mem_pools/tx_pool_v2/tx_metadata.js +2 -1
- package/dest/mem_pools/tx_pool_v2/tx_pool_indices.d.ts +99 -0
- package/dest/mem_pools/tx_pool_v2/tx_pool_indices.d.ts.map +1 -0
- package/dest/mem_pools/tx_pool_v2/tx_pool_indices.js +332 -0
- package/dest/mem_pools/tx_pool_v2/tx_pool_v2.d.ts +1 -1
- package/dest/mem_pools/tx_pool_v2/tx_pool_v2.d.ts.map +1 -1
- package/dest/mem_pools/tx_pool_v2/tx_pool_v2.js +6 -0
- package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.d.ts +1 -1
- package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.d.ts.map +1 -1
- package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.js +193 -486
- package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.d.ts +3 -3
- package/dest/msg_validators/attestation_validator/fisherman_attestation_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/libp2p/libp2p_service.d.ts +74 -33
- package/dest/services/libp2p/libp2p_service.d.ts.map +1 -1
- package/dest/services/libp2p/libp2p_service.js +299 -228
- package/dest/services/reqresp/batch-tx-requester/batch_tx_requester.d.ts +4 -4
- package/dest/services/reqresp/batch-tx-requester/batch_tx_requester.d.ts.map +1 -1
- package/dest/services/reqresp/batch-tx-requester/batch_tx_requester.js +8 -8
- package/dest/services/reqresp/protocols/block_txs/block_txs_handler.d.ts +6 -4
- package/dest/services/reqresp/protocols/block_txs/block_txs_handler.d.ts.map +1 -1
- package/dest/services/reqresp/protocols/block_txs/block_txs_handler.js +16 -11
- package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.d.ts +15 -10
- 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 +12 -11
- package/dest/services/service.d.ts +18 -1
- package/dest/services/service.d.ts.map +1 -1
- package/dest/services/tx_collection/config.d.ts +3 -3
- package/dest/services/tx_collection/config.js +3 -3
- package/dest/services/tx_collection/fast_tx_collection.d.ts +4 -5
- package/dest/services/tx_collection/fast_tx_collection.d.ts.map +1 -1
- package/dest/services/tx_collection/fast_tx_collection.js +10 -14
- package/dest/services/tx_collection/index.d.ts +1 -1
- package/dest/services/tx_collection/proposal_tx_collector.d.ts +12 -12
- package/dest/services/tx_collection/proposal_tx_collector.d.ts.map +1 -1
- package/dest/services/tx_collection/proposal_tx_collector.js +4 -5
- package/dest/test-helpers/testbench-utils.d.ts +10 -16
- package/dest/test-helpers/testbench-utils.d.ts.map +1 -1
- package/dest/test-helpers/testbench-utils.js +32 -30
- package/dest/testbench/p2p_client_testbench_worker.js +1 -1
- package/package.json +14 -14
- package/src/client/factory.ts +3 -4
- package/src/client/interface.ts +13 -1
- package/src/client/p2p_client.ts +20 -8
- package/src/client/test/tx_proposal_collector/proposal_tx_collector_worker.ts +1 -1
- package/src/mem_pools/attestation_pool/attestation_pool.ts +444 -90
- package/src/mem_pools/attestation_pool/attestation_pool_test_suite.ts +436 -100
- package/src/mem_pools/attestation_pool/index.ts +9 -2
- package/src/mem_pools/index.ts +1 -1
- package/src/mem_pools/interface.ts +2 -2
- package/src/mem_pools/tx_pool_v2/README.md +28 -7
- package/src/mem_pools/tx_pool_v2/interfaces.ts +2 -0
- package/src/mem_pools/tx_pool_v2/tx_metadata.ts +2 -1
- package/src/mem_pools/tx_pool_v2/tx_pool_indices.ts +417 -0
- package/src/mem_pools/tx_pool_v2/tx_pool_v2.ts +3 -0
- package/src/mem_pools/tx_pool_v2/tx_pool_v2_impl.ts +185 -568
- package/src/msg_validators/attestation_validator/fisherman_attestation_validator.ts +2 -2
- package/src/services/dummy_service.ts +6 -0
- package/src/services/libp2p/libp2p_service.ts +304 -230
- package/src/services/reqresp/batch-tx-requester/README.md +7 -7
- package/src/services/reqresp/batch-tx-requester/batch_tx_requester.ts +11 -11
- package/src/services/reqresp/protocols/block_txs/block_txs_handler.ts +22 -13
- package/src/services/reqresp/protocols/block_txs/block_txs_reqresp.ts +21 -15
- package/src/services/service.ts +20 -0
- package/src/services/tx_collection/config.ts +6 -6
- package/src/services/tx_collection/fast_tx_collection.ts +14 -24
- package/src/services/tx_collection/index.ts +1 -1
- package/src/services/tx_collection/proposal_tx_collector.ts +12 -14
- package/src/test-helpers/testbench-utils.ts +18 -39
- package/src/testbench/p2p_client_testbench_worker.ts +1 -1
- package/dest/mem_pools/attestation_pool/kv_attestation_pool.d.ts +0 -40
- package/dest/mem_pools/attestation_pool/kv_attestation_pool.d.ts.map +0 -1
- package/dest/mem_pools/attestation_pool/kv_attestation_pool.js +0 -218
- package/dest/mem_pools/attestation_pool/memory_attestation_pool.d.ts +0 -31
- package/dest/mem_pools/attestation_pool/memory_attestation_pool.d.ts.map +0 -1
- package/dest/mem_pools/attestation_pool/memory_attestation_pool.js +0 -180
- package/src/mem_pools/attestation_pool/kv_attestation_pool.ts +0 -320
- package/src/mem_pools/attestation_pool/memory_attestation_pool.ts +0 -264
|
@@ -1,2 +1,9 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
1
|
+
export {
|
|
2
|
+
AttestationPool,
|
|
3
|
+
type AttestationPoolApi,
|
|
4
|
+
type TryAddResult,
|
|
5
|
+
createTestAttestationPool,
|
|
6
|
+
MAX_PROPOSALS_PER_SLOT,
|
|
7
|
+
MAX_PROPOSALS_PER_POSITION,
|
|
8
|
+
ATTESTATION_CAP_BUFFER,
|
|
9
|
+
} from './attestation_pool.js';
|
package/src/mem_pools/index.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { type
|
|
1
|
+
export { AttestationPool, type AttestationPoolApi } from './attestation_pool/attestation_pool.js';
|
|
2
2
|
export { type MemPools } from './interface.js';
|
|
3
3
|
export { type TxPool } from './tx_pool/tx_pool.js';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { AttestationPoolApi } from './attestation_pool/attestation_pool.js';
|
|
2
2
|
import type { TxPool } from './tx_pool/tx_pool.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -6,5 +6,5 @@ import type { TxPool } from './tx_pool/tx_pool.js';
|
|
|
6
6
|
*/
|
|
7
7
|
export type MemPools = {
|
|
8
8
|
txPool: TxPool;
|
|
9
|
-
attestationPool:
|
|
9
|
+
attestationPool: AttestationPoolApi;
|
|
10
10
|
};
|
|
@@ -56,13 +56,34 @@ Core implementation containing:
|
|
|
56
56
|
|
|
57
57
|
Lightweight metadata stored alongside each transaction:
|
|
58
58
|
- `txHash`: Transaction identifier
|
|
59
|
-
- `
|
|
60
|
-
- `priorityFee`: For priority ordering
|
|
59
|
+
- `anchorBlockHeaderHash`: Hash of the anchor block header
|
|
60
|
+
- `priorityFee`: For priority ordering and challenges
|
|
61
61
|
- `feePayer`: For balance-based eviction
|
|
62
|
+
- `claimAmount`: Fee payer's claim from bridging
|
|
63
|
+
- `feeLimit`: Maximum fee the tx can pay
|
|
62
64
|
- `nullifiers`: For conflict detection
|
|
63
|
-
- `
|
|
64
|
-
- `
|
|
65
|
-
|
|
65
|
+
- `includeByTimestamp`: Expiration timestamp
|
|
66
|
+
- `minedL2BlockId`: Set when mined (undefined otherwise)
|
|
67
|
+
|
|
68
|
+
State is derived by TxPoolIndices:
|
|
69
|
+
- `mined` if `minedL2BlockId` is set
|
|
70
|
+
- `protected` if in protection map
|
|
71
|
+
- `pending` otherwise
|
|
72
|
+
|
|
73
|
+
## Architecture: Pre-add vs Post-event Rules
|
|
74
|
+
|
|
75
|
+
**Pre-add rules** (run during `addPendingTxs`):
|
|
76
|
+
- Used for external transactions entering the pool
|
|
77
|
+
- Can reject the incoming tx entirely
|
|
78
|
+
- Can evict lower-priority existing txs to make room
|
|
79
|
+
- Rules: NullifierConflictRule, FeePayerBalancePreAddRule, LowPriorityPreAddRule
|
|
80
|
+
|
|
81
|
+
**Post-event rules** (run after state transitions):
|
|
82
|
+
- Used for internal state changes (block mined, reorg, slot change)
|
|
83
|
+
- Only evict txs already in the pool
|
|
84
|
+
- Rules: InvalidTxsAfterMiningRule, InvalidTxsAfterReorgRule, FeePayerBalanceEvictionRule, LowPriorityEvictionRule
|
|
85
|
+
|
|
86
|
+
This design choice means restored txs (from protected/mined states) use post-event rules only, because they were already validated on initial submission.
|
|
66
87
|
|
|
67
88
|
## Eviction Rules
|
|
68
89
|
|
|
@@ -85,8 +106,8 @@ Run after events to clean up the pool:
|
|
|
85
106
|
| Rule | Trigger | Purpose |
|
|
86
107
|
|------|---------|---------|
|
|
87
108
|
| `LowPriorityEvictionRule` | `txs_added` | Evicts lowest priority txs when pool exceeds limit. |
|
|
88
|
-
| `FeePayerBalanceEvictionRule` | `block_mined` | Evicts txs when fee payer balance
|
|
89
|
-
| `InvalidTxsAfterMiningRule` | `block_mined` | Evicts pending txs with nullifiers
|
|
109
|
+
| `FeePayerBalanceEvictionRule` | `txs_added`, `block_mined`, `chain_pruned` | Evicts txs when fee payer has insufficient balance. |
|
|
110
|
+
| `InvalidTxsAfterMiningRule` | `block_mined` | Evicts pending txs with: (1) nullifiers in mined block, (2) expired timestamp. |
|
|
90
111
|
| `InvalidTxsAfterReorgRule` | `chain_pruned` | Evicts txs with invalid anchor blocks after reorg. |
|
|
91
112
|
|
|
92
113
|
## Usage
|
|
@@ -25,6 +25,8 @@ export type AddTxsResult = {
|
|
|
25
25
|
export type TxPoolV2Events = {
|
|
26
26
|
/** Emitted when transactions are successfully added to the pool */
|
|
27
27
|
'txs-added': (args: { txs: Tx[]; source?: string }) => void;
|
|
28
|
+
/** Emitted when transactions are removed from the pool */
|
|
29
|
+
'txs-removed': (args: { txHashes: TxHash[] }) => void;
|
|
28
30
|
};
|
|
29
31
|
|
|
30
32
|
/**
|
|
@@ -145,7 +145,8 @@ export function checkNullifierConflict(
|
|
|
145
145
|
|
|
146
146
|
// If incoming tx has strictly higher priority, mark for eviction
|
|
147
147
|
// Otherwise, ignore incoming tx (ties go to existing tx)
|
|
148
|
-
|
|
148
|
+
// Use comparePriority for deterministic ordering (includes txHash as tiebreaker)
|
|
149
|
+
if (comparePriority(incomingMeta, conflictingMeta) > 0) {
|
|
149
150
|
txHashesToEvict.push(conflictingHashStr);
|
|
150
151
|
} else {
|
|
151
152
|
return {
|
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
import { SlotNumber } from '@aztec/foundation/branded-types';
|
|
2
|
+
import type { L2BlockId } from '@aztec/stdlib/block';
|
|
3
|
+
|
|
4
|
+
import { type TxMetaData, type TxState, compareFee, compareTxHash } from './tx_metadata.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Manages in-memory indices for the transaction pool.
|
|
8
|
+
*
|
|
9
|
+
* Tracks transaction metadata and maintains several indices for efficient querying:
|
|
10
|
+
* - Metadata by txHash (primary store)
|
|
11
|
+
* - Nullifier to txHash mapping (pending txs only)
|
|
12
|
+
* - Fee payer to txHashes mapping (pending txs only)
|
|
13
|
+
* - Priority-ordered pending txs
|
|
14
|
+
* - Protected transaction tracking
|
|
15
|
+
*
|
|
16
|
+
* Key invariant: Only pending txs appear in nullifier/feePayer/priority indices.
|
|
17
|
+
*/
|
|
18
|
+
export class TxPoolIndices {
|
|
19
|
+
/** Primary metadata store: txHash -> TxMetaData */
|
|
20
|
+
#metadata: Map<string, TxMetaData> = new Map();
|
|
21
|
+
/** Nullifier to txHash index (pending txs only) */
|
|
22
|
+
#nullifierToTxHash: Map<string, string> = new Map();
|
|
23
|
+
/** Fee payer to txHashes index (pending txs only) */
|
|
24
|
+
#feePayerToTxHashes: Map<string, Set<string>> = new Map();
|
|
25
|
+
/** Pending txHashes grouped by priority fee */
|
|
26
|
+
#pendingByPriority: Map<bigint, Set<string>> = new Map();
|
|
27
|
+
/** Protected transactions: txHash -> slotNumber */
|
|
28
|
+
#protectedTransactions: Map<string, SlotNumber> = new Map();
|
|
29
|
+
|
|
30
|
+
// ============================================================================
|
|
31
|
+
// STATE QUERIES
|
|
32
|
+
// ============================================================================
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Derives the transaction state from its metadata and protection status.
|
|
36
|
+
* A transaction is:
|
|
37
|
+
* - 'mined' if it has a minedL2BlockId
|
|
38
|
+
* - 'protected' if it's in the protectedTransactions map (but not mined)
|
|
39
|
+
* - 'pending' otherwise
|
|
40
|
+
*/
|
|
41
|
+
getTxState(meta: TxMetaData): TxState {
|
|
42
|
+
if (meta.minedL2BlockId !== undefined) {
|
|
43
|
+
return 'mined';
|
|
44
|
+
} else if (this.#protectedTransactions.has(meta.txHash)) {
|
|
45
|
+
return 'protected';
|
|
46
|
+
} else {
|
|
47
|
+
return 'pending';
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
getMetadata(txHash: string): TxMetaData | undefined {
|
|
52
|
+
return this.#metadata.get(txHash);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
has(txHash: string): boolean {
|
|
56
|
+
return this.#metadata.has(txHash);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
isEmpty(): boolean {
|
|
60
|
+
return this.#metadata.size === 0;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
getTxCount(): number {
|
|
64
|
+
return this.#metadata.size;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// ============================================================================
|
|
68
|
+
// ITERATION
|
|
69
|
+
// ============================================================================
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Iterates pending transaction hashes in priority order.
|
|
73
|
+
* @param order - 'desc' for highest priority first, 'asc' for lowest priority first
|
|
74
|
+
*/
|
|
75
|
+
*iteratePendingByPriority(order: 'asc' | 'desc'): Generator<string> {
|
|
76
|
+
// Use compareFee from tx_metadata, swap args for descending order
|
|
77
|
+
const feeCompareFn = order === 'desc' ? (a: bigint, b: bigint) => compareFee(b, a) : compareFee;
|
|
78
|
+
const hashCompareFn = order === 'desc' ? (a: string, b: string) => compareTxHash(b, a) : compareTxHash;
|
|
79
|
+
|
|
80
|
+
const sortedFees = [...this.#pendingByPriority.keys()].sort(feeCompareFn);
|
|
81
|
+
|
|
82
|
+
for (const fee of sortedFees) {
|
|
83
|
+
const hashesAtFee = this.#pendingByPriority.get(fee)!;
|
|
84
|
+
// Use compareTxHash from tx_metadata, swap args for descending order
|
|
85
|
+
const sortedHashes = [...hashesAtFee].sort(hashCompareFn);
|
|
86
|
+
for (const hash of sortedHashes) {
|
|
87
|
+
yield hash;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Iterates all metadata entries */
|
|
93
|
+
*iterateMetadata(): Generator<[string, TxMetaData]> {
|
|
94
|
+
yield* this.#metadata;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// ============================================================================
|
|
98
|
+
// INDEX MODIFICATIONS
|
|
99
|
+
// ============================================================================
|
|
100
|
+
|
|
101
|
+
/** Adds a new pending transaction to all indices */
|
|
102
|
+
addPending(meta: TxMetaData): void {
|
|
103
|
+
this.#metadata.set(meta.txHash, meta);
|
|
104
|
+
this.#addToPendingIndices(meta);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Adds a new protected transaction (not added to pending indices) */
|
|
108
|
+
addProtected(meta: TxMetaData, slot: SlotNumber): void {
|
|
109
|
+
this.#metadata.set(meta.txHash, meta);
|
|
110
|
+
this.#protectedTransactions.set(meta.txHash, slot);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Adds a new mined transaction (not added to pending indices) */
|
|
114
|
+
addMined(meta: TxMetaData): void {
|
|
115
|
+
this.#metadata.set(meta.txHash, meta);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Marks an existing transaction as mined and removes from pending indices */
|
|
119
|
+
markAsMined(meta: TxMetaData, blockId: L2BlockId): void {
|
|
120
|
+
meta.minedL2BlockId = blockId;
|
|
121
|
+
// Safe to call unconditionally - removeFromPendingIndices is idempotent
|
|
122
|
+
this.#removeFromPendingIndices(meta);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Clears the mined status from a transaction */
|
|
126
|
+
markAsUnmined(meta: TxMetaData): void {
|
|
127
|
+
meta.minedL2BlockId = undefined;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Updates protection status for an existing transaction.
|
|
132
|
+
* Removes from pending indices if transitioning from pending to protected.
|
|
133
|
+
*/
|
|
134
|
+
updateProtection(txHash: string, slotNumber: SlotNumber): void {
|
|
135
|
+
const currentSlot = this.#protectedTransactions.get(txHash);
|
|
136
|
+
|
|
137
|
+
// Only update if not already protected at an equal or later slot
|
|
138
|
+
if (currentSlot !== undefined && currentSlot >= slotNumber) {
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Remove from pending indices if transitioning from pending to protected
|
|
143
|
+
if (currentSlot === undefined) {
|
|
144
|
+
const meta = this.#metadata.get(txHash);
|
|
145
|
+
if (meta) {
|
|
146
|
+
this.#removeFromPendingIndices(meta);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
this.#protectedTransactions.set(txHash, slotNumber);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Sets protection for a txHash that may not have metadata yet */
|
|
154
|
+
setProtection(txHash: string, slotNumber: SlotNumber): void {
|
|
155
|
+
this.#protectedTransactions.set(txHash, slotNumber);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** Gets the protection slot for a txHash, if protected */
|
|
159
|
+
getProtectionSlot(txHash: string): SlotNumber | undefined {
|
|
160
|
+
return this.#protectedTransactions.get(txHash);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Removes protection from tx hashes */
|
|
164
|
+
clearProtection(txHashes: string[]): void {
|
|
165
|
+
for (const txHash of txHashes) {
|
|
166
|
+
this.#protectedTransactions.delete(txHash);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Removes a transaction from all indices */
|
|
171
|
+
remove(txHash: string): void {
|
|
172
|
+
const meta = this.#metadata.get(txHash);
|
|
173
|
+
if (!meta) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
this.#metadata.delete(txHash);
|
|
178
|
+
this.#protectedTransactions.delete(txHash);
|
|
179
|
+
this.#removeFromPendingIndices(meta);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** Removes a transaction from pending indices only (not metadata) */
|
|
183
|
+
removeFromPendingIndices(meta: TxMetaData): void {
|
|
184
|
+
this.#removeFromPendingIndices(meta);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** Adds a transaction to pending indices (used during conflict resolution) */
|
|
188
|
+
addToPendingIndices(meta: TxMetaData): void {
|
|
189
|
+
this.#addToPendingIndices(meta);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// ============================================================================
|
|
193
|
+
// QUERIES FOR EVICTION RULES
|
|
194
|
+
// ============================================================================
|
|
195
|
+
|
|
196
|
+
/** Gets all pending transactions for a given fee payer */
|
|
197
|
+
getFeePayerPendingTxs(feePayer: string): TxMetaData[] {
|
|
198
|
+
const txHashes = this.#feePayerToTxHashes.get(feePayer);
|
|
199
|
+
if (!txHashes) {
|
|
200
|
+
return [];
|
|
201
|
+
}
|
|
202
|
+
const result: TxMetaData[] = [];
|
|
203
|
+
for (const txHash of txHashes) {
|
|
204
|
+
const meta = this.#metadata.get(txHash);
|
|
205
|
+
if (meta && this.getTxState(meta) === 'pending') {
|
|
206
|
+
result.push(meta);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
return result;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** Gets the count of pending transactions */
|
|
213
|
+
getPendingTxCount(): number {
|
|
214
|
+
let count = 0;
|
|
215
|
+
for (const hashes of this.#pendingByPriority.values()) {
|
|
216
|
+
count += hashes.size;
|
|
217
|
+
}
|
|
218
|
+
return count;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** Gets the lowest priority pending transaction hashes (up to limit) */
|
|
222
|
+
getLowestPriorityPending(limit: number): string[] {
|
|
223
|
+
if (limit <= 0) {
|
|
224
|
+
return [];
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const result: string[] = [];
|
|
228
|
+
for (const hash of this.iteratePendingByPriority('asc')) {
|
|
229
|
+
result.push(hash);
|
|
230
|
+
if (result.length >= limit) {
|
|
231
|
+
break;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
return result;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** Gets the lowest priority pending transaction */
|
|
238
|
+
getLowestPriorityPendingTx(): TxMetaData | undefined {
|
|
239
|
+
for (const txHash of this.iteratePendingByPriority('asc')) {
|
|
240
|
+
const meta = this.#metadata.get(txHash);
|
|
241
|
+
if (meta) {
|
|
242
|
+
return meta;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return undefined;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** Gets all pending transactions */
|
|
249
|
+
getPendingTxs(): TxMetaData[] {
|
|
250
|
+
const result: TxMetaData[] = [];
|
|
251
|
+
for (const hashSet of this.#pendingByPriority.values()) {
|
|
252
|
+
for (const txHash of hashSet) {
|
|
253
|
+
const meta = this.#metadata.get(txHash);
|
|
254
|
+
if (meta) {
|
|
255
|
+
result.push(meta);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return result;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/** Gets all fee payers with pending transactions */
|
|
263
|
+
getPendingFeePayers(): string[] {
|
|
264
|
+
return Array.from(this.#feePayerToTxHashes.keys());
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/** Gets the txHash that uses a given nullifier (pending txs only) */
|
|
268
|
+
getTxHashByNullifier(nullifier: string): string | undefined {
|
|
269
|
+
return this.#nullifierToTxHash.get(nullifier);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** Gets txHashes for a fee payer */
|
|
273
|
+
getTxHashesByFeePayer(feePayer: string): Set<string> | undefined {
|
|
274
|
+
return this.#feePayerToTxHashes.get(feePayer);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// ============================================================================
|
|
278
|
+
// FIND/FILTER OPERATIONS
|
|
279
|
+
// ============================================================================
|
|
280
|
+
|
|
281
|
+
/** Finds all transactions mined in blocks after the given block number */
|
|
282
|
+
findTxsMinedAfter(blockNumber: number): TxMetaData[] {
|
|
283
|
+
const result: TxMetaData[] = [];
|
|
284
|
+
for (const meta of this.#metadata.values()) {
|
|
285
|
+
if (meta.minedL2BlockId !== undefined && meta.minedL2BlockId.number > blockNumber) {
|
|
286
|
+
result.push(meta);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
return result;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/** Finds tx hashes mined at or before the given block number */
|
|
293
|
+
findTxsMinedAtOrBefore(blockNumber: number): string[] {
|
|
294
|
+
const result: string[] = [];
|
|
295
|
+
for (const [txHash, meta] of this.#metadata) {
|
|
296
|
+
if (meta.minedL2BlockId !== undefined && meta.minedL2BlockId.number <= blockNumber) {
|
|
297
|
+
result.push(txHash);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
return result;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/** Finds protected tx hashes from slots earlier than the given slot number */
|
|
304
|
+
findExpiredProtectedTxs(slotNumber: SlotNumber): string[] {
|
|
305
|
+
const result: string[] = [];
|
|
306
|
+
for (const [txHash, protectedSlot] of this.#protectedTransactions) {
|
|
307
|
+
if (protectedSlot < slotNumber) {
|
|
308
|
+
result.push(txHash);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
return result;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/** Filters out transactions that are currently protected */
|
|
315
|
+
filterUnprotected(txs: TxMetaData[]): TxMetaData[] {
|
|
316
|
+
return txs.filter(meta => !this.#protectedTransactions.has(meta.txHash));
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/** Filters to transactions that have metadata and are not mined */
|
|
320
|
+
filterRestorable(txHashes: string[]): TxMetaData[] {
|
|
321
|
+
const result: TxMetaData[] = [];
|
|
322
|
+
for (const txHash of txHashes) {
|
|
323
|
+
const meta = this.#metadata.get(txHash);
|
|
324
|
+
if (meta && meta.minedL2BlockId === undefined) {
|
|
325
|
+
result.push(meta);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
return result;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// ============================================================================
|
|
332
|
+
// METRICS
|
|
333
|
+
// ============================================================================
|
|
334
|
+
|
|
335
|
+
/** Counts transactions by state */
|
|
336
|
+
countTxs(): { pending: number; protected: number; mined: number } {
|
|
337
|
+
let pending = 0;
|
|
338
|
+
let protected_ = 0;
|
|
339
|
+
let mined = 0;
|
|
340
|
+
|
|
341
|
+
for (const meta of this.#metadata.values()) {
|
|
342
|
+
const state = this.getTxState(meta);
|
|
343
|
+
if (state === 'pending') {
|
|
344
|
+
pending++;
|
|
345
|
+
} else if (state === 'protected') {
|
|
346
|
+
protected_++;
|
|
347
|
+
} else if (state === 'mined') {
|
|
348
|
+
mined++;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
return { pending, protected: protected_, mined };
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/** Gets all mined transactions with their block IDs */
|
|
356
|
+
getMinedTxs(): [string, L2BlockId][] {
|
|
357
|
+
const result: [string, L2BlockId][] = [];
|
|
358
|
+
for (const [txHash, meta] of this.#metadata) {
|
|
359
|
+
if (meta.minedL2BlockId !== undefined) {
|
|
360
|
+
result.push([txHash, meta.minedL2BlockId]);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
return result;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// ============================================================================
|
|
367
|
+
// PRIVATE HELPERS
|
|
368
|
+
// ============================================================================
|
|
369
|
+
|
|
370
|
+
#addToPendingIndices(meta: TxMetaData): void {
|
|
371
|
+
// Add to nullifier index
|
|
372
|
+
for (const nullifier of meta.nullifiers) {
|
|
373
|
+
this.#nullifierToTxHash.set(nullifier, meta.txHash);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
// Add to fee payer index
|
|
377
|
+
let feePayerSet = this.#feePayerToTxHashes.get(meta.feePayer);
|
|
378
|
+
if (!feePayerSet) {
|
|
379
|
+
feePayerSet = new Set();
|
|
380
|
+
this.#feePayerToTxHashes.set(meta.feePayer, feePayerSet);
|
|
381
|
+
}
|
|
382
|
+
feePayerSet.add(meta.txHash);
|
|
383
|
+
|
|
384
|
+
// Add to priority bucket
|
|
385
|
+
let prioritySet = this.#pendingByPriority.get(meta.priorityFee);
|
|
386
|
+
if (!prioritySet) {
|
|
387
|
+
prioritySet = new Set();
|
|
388
|
+
this.#pendingByPriority.set(meta.priorityFee, prioritySet);
|
|
389
|
+
}
|
|
390
|
+
prioritySet.add(meta.txHash);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
#removeFromPendingIndices(meta: TxMetaData): void {
|
|
394
|
+
// Remove from nullifier index
|
|
395
|
+
for (const nullifier of meta.nullifiers) {
|
|
396
|
+
this.#nullifierToTxHash.delete(nullifier);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
// Remove from fee payer index
|
|
400
|
+
const feePayerSet = this.#feePayerToTxHashes.get(meta.feePayer);
|
|
401
|
+
if (feePayerSet) {
|
|
402
|
+
feePayerSet.delete(meta.txHash);
|
|
403
|
+
if (feePayerSet.size === 0) {
|
|
404
|
+
this.#feePayerToTxHashes.delete(meta.feePayer);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
// Remove from priority map
|
|
409
|
+
const hashSet = this.#pendingByPriority.get(meta.priorityFee);
|
|
410
|
+
if (hashSet) {
|
|
411
|
+
hashSet.delete(meta.txHash);
|
|
412
|
+
if (hashSet.size === 0) {
|
|
413
|
+
this.#pendingByPriority.delete(meta.priorityFee);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
}
|
|
@@ -52,6 +52,9 @@ export class AztecKVTxPoolV2 extends (EventEmitter as new () => TypedEventEmitte
|
|
|
52
52
|
},
|
|
53
53
|
onTxsRemoved: (txHashes: string[] | bigint[]) => {
|
|
54
54
|
this.#metrics?.transactionsRemoved(txHashes);
|
|
55
|
+
// Convert to TxHash objects for the event
|
|
56
|
+
const hashes = txHashes.map(h => (typeof h === 'string' ? TxHash.fromString(h) : TxHash.fromBigInt(h)));
|
|
57
|
+
this.emit('txs-removed', { txHashes: hashes });
|
|
55
58
|
},
|
|
56
59
|
};
|
|
57
60
|
|