@aztec/p2p 0.0.1-commit.e3c1de76 → 0.0.1-commit.e558bd1c

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.
Files changed (188) hide show
  1. package/dest/client/factory.d.ts +3 -3
  2. package/dest/client/factory.d.ts.map +1 -1
  3. package/dest/client/factory.js +5 -3
  4. package/dest/client/interface.d.ts +9 -2
  5. package/dest/client/interface.d.ts.map +1 -1
  6. package/dest/client/p2p_client.d.ts +7 -4
  7. package/dest/client/p2p_client.d.ts.map +1 -1
  8. package/dest/client/p2p_client.js +22 -7
  9. package/dest/client/test/tx_proposal_collector/proposal_tx_collector_worker.js +1 -1
  10. package/dest/config.d.ts +9 -3
  11. package/dest/config.d.ts.map +1 -1
  12. package/dest/config.js +3 -1
  13. package/dest/mem_pools/attestation_pool/attestation_pool.d.ts +94 -87
  14. package/dest/mem_pools/attestation_pool/attestation_pool.d.ts.map +1 -1
  15. package/dest/mem_pools/attestation_pool/attestation_pool.js +411 -3
  16. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.d.ts +2 -2
  17. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.d.ts.map +1 -1
  18. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.js +351 -85
  19. package/dest/mem_pools/attestation_pool/index.d.ts +2 -3
  20. package/dest/mem_pools/attestation_pool/index.d.ts.map +1 -1
  21. package/dest/mem_pools/attestation_pool/index.js +1 -2
  22. package/dest/mem_pools/index.d.ts +2 -2
  23. package/dest/mem_pools/index.d.ts.map +1 -1
  24. package/dest/mem_pools/index.js +1 -1
  25. package/dest/mem_pools/interface.d.ts +3 -3
  26. package/dest/mem_pools/interface.d.ts.map +1 -1
  27. package/dest/mem_pools/tx_pool_v2/archive/index.d.ts +2 -0
  28. package/dest/mem_pools/tx_pool_v2/archive/index.d.ts.map +1 -0
  29. package/dest/mem_pools/tx_pool_v2/archive/index.js +1 -0
  30. package/dest/mem_pools/tx_pool_v2/archive/tx_archive.d.ts +43 -0
  31. package/dest/mem_pools/tx_pool_v2/archive/tx_archive.d.ts.map +1 -0
  32. package/dest/mem_pools/tx_pool_v2/archive/tx_archive.js +103 -0
  33. package/dest/mem_pools/tx_pool_v2/eviction/eviction_manager.d.ts +47 -0
  34. package/dest/mem_pools/tx_pool_v2/eviction/eviction_manager.d.ts.map +1 -0
  35. package/dest/mem_pools/tx_pool_v2/eviction/eviction_manager.js +119 -0
  36. package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_eviction_rule.d.ts +17 -0
  37. package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_eviction_rule.d.ts.map +1 -0
  38. package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_eviction_rule.js +90 -0
  39. package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_pre_add_rule.d.ts +19 -0
  40. package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_pre_add_rule.d.ts.map +1 -0
  41. package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_pre_add_rule.js +89 -0
  42. package/dest/mem_pools/tx_pool_v2/eviction/index.d.ts +10 -0
  43. package/dest/mem_pools/tx_pool_v2/eviction/index.d.ts.map +1 -0
  44. package/dest/mem_pools/tx_pool_v2/eviction/index.js +11 -0
  45. package/dest/mem_pools/tx_pool_v2/eviction/interfaces.d.ts +131 -0
  46. package/dest/mem_pools/tx_pool_v2/eviction/interfaces.d.ts.map +1 -0
  47. package/dest/mem_pools/tx_pool_v2/eviction/interfaces.js +17 -0
  48. package/dest/mem_pools/tx_pool_v2/eviction/invalid_txs_after_mining_rule.d.ts +15 -0
  49. package/dest/mem_pools/tx_pool_v2/eviction/invalid_txs_after_mining_rule.d.ts.map +1 -0
  50. package/dest/mem_pools/tx_pool_v2/eviction/invalid_txs_after_mining_rule.js +63 -0
  51. package/dest/mem_pools/tx_pool_v2/eviction/invalid_txs_after_reorg_rule.d.ts +17 -0
  52. package/dest/mem_pools/tx_pool_v2/eviction/invalid_txs_after_reorg_rule.d.ts.map +1 -0
  53. package/dest/mem_pools/tx_pool_v2/eviction/invalid_txs_after_reorg_rule.js +91 -0
  54. package/dest/mem_pools/tx_pool_v2/eviction/low_priority_eviction_rule.d.ts +16 -0
  55. package/dest/mem_pools/tx_pool_v2/eviction/low_priority_eviction_rule.d.ts.map +1 -0
  56. package/dest/mem_pools/tx_pool_v2/eviction/low_priority_eviction_rule.js +70 -0
  57. package/dest/mem_pools/tx_pool_v2/eviction/low_priority_pre_add_rule.d.ts +20 -0
  58. package/dest/mem_pools/tx_pool_v2/eviction/low_priority_pre_add_rule.d.ts.map +1 -0
  59. package/dest/mem_pools/tx_pool_v2/eviction/low_priority_pre_add_rule.js +63 -0
  60. package/dest/mem_pools/tx_pool_v2/eviction/nullifier_conflict_rule.d.ts +15 -0
  61. package/dest/mem_pools/tx_pool_v2/eviction/nullifier_conflict_rule.d.ts.map +1 -0
  62. package/dest/mem_pools/tx_pool_v2/eviction/nullifier_conflict_rule.js +19 -0
  63. package/dest/mem_pools/tx_pool_v2/index.d.ts +5 -0
  64. package/dest/mem_pools/tx_pool_v2/index.d.ts.map +1 -0
  65. package/dest/mem_pools/tx_pool_v2/index.js +4 -0
  66. package/dest/mem_pools/tx_pool_v2/interfaces.d.ts +197 -0
  67. package/dest/mem_pools/tx_pool_v2/interfaces.d.ts.map +1 -0
  68. package/dest/mem_pools/tx_pool_v2/interfaces.js +6 -0
  69. package/dest/mem_pools/tx_pool_v2/tx_metadata.d.ts +71 -0
  70. package/dest/mem_pools/tx_pool_v2/tx_metadata.d.ts.map +1 -0
  71. package/dest/mem_pools/tx_pool_v2/tx_metadata.js +95 -0
  72. package/dest/mem_pools/tx_pool_v2/tx_pool_bench_metrics.d.ts +26 -0
  73. package/dest/mem_pools/tx_pool_v2/tx_pool_bench_metrics.d.ts.map +1 -0
  74. package/dest/mem_pools/tx_pool_v2/tx_pool_bench_metrics.js +70 -0
  75. package/dest/mem_pools/tx_pool_v2/tx_pool_indices.d.ts +99 -0
  76. package/dest/mem_pools/tx_pool_v2/tx_pool_indices.d.ts.map +1 -0
  77. package/dest/mem_pools/tx_pool_v2/tx_pool_indices.js +332 -0
  78. package/dest/mem_pools/tx_pool_v2/tx_pool_v2.d.ts +55 -0
  79. package/dest/mem_pools/tx_pool_v2/tx_pool_v2.d.ts.map +1 -0
  80. package/dest/mem_pools/tx_pool_v2/tx_pool_v2.js +156 -0
  81. package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.d.ts +69 -0
  82. package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.d.ts.map +1 -0
  83. package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.js +748 -0
  84. package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.d.ts +3 -3
  85. package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.d.ts.map +1 -1
  86. package/dest/services/dummy_service.d.ts +6 -2
  87. package/dest/services/dummy_service.d.ts.map +1 -1
  88. package/dest/services/dummy_service.js +3 -0
  89. package/dest/services/index.d.ts +2 -1
  90. package/dest/services/index.d.ts.map +1 -1
  91. package/dest/services/index.js +1 -0
  92. package/dest/services/libp2p/libp2p_service.d.ts +74 -33
  93. package/dest/services/libp2p/libp2p_service.d.ts.map +1 -1
  94. package/dest/services/libp2p/libp2p_service.js +299 -228
  95. package/dest/services/reqresp/batch-tx-requester/batch_tx_requester.d.ts +4 -4
  96. package/dest/services/reqresp/batch-tx-requester/batch_tx_requester.d.ts.map +1 -1
  97. package/dest/services/reqresp/batch-tx-requester/batch_tx_requester.js +8 -8
  98. package/dest/services/reqresp/protocols/block_txs/block_txs_handler.d.ts +6 -4
  99. package/dest/services/reqresp/protocols/block_txs/block_txs_handler.d.ts.map +1 -1
  100. package/dest/services/reqresp/protocols/block_txs/block_txs_handler.js +16 -11
  101. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.d.ts +15 -10
  102. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.d.ts.map +1 -1
  103. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.js +12 -11
  104. package/dest/services/service.d.ts +18 -1
  105. package/dest/services/service.d.ts.map +1 -1
  106. package/dest/services/tx_collection/config.d.ts +3 -3
  107. package/dest/services/tx_collection/config.js +3 -3
  108. package/dest/services/tx_collection/fast_tx_collection.d.ts +4 -5
  109. package/dest/services/tx_collection/fast_tx_collection.d.ts.map +1 -1
  110. package/dest/services/tx_collection/fast_tx_collection.js +10 -14
  111. package/dest/services/tx_collection/index.d.ts +1 -1
  112. package/dest/services/tx_collection/proposal_tx_collector.d.ts +12 -12
  113. package/dest/services/tx_collection/proposal_tx_collector.d.ts.map +1 -1
  114. package/dest/services/tx_collection/proposal_tx_collector.js +4 -5
  115. package/dest/services/tx_file_store/config.d.ts +18 -0
  116. package/dest/services/tx_file_store/config.d.ts.map +1 -0
  117. package/dest/services/tx_file_store/config.js +26 -0
  118. package/dest/services/tx_file_store/index.d.ts +4 -0
  119. package/dest/services/tx_file_store/index.d.ts.map +1 -0
  120. package/dest/services/tx_file_store/index.js +3 -0
  121. package/dest/services/tx_file_store/instrumentation.d.ts +15 -0
  122. package/dest/services/tx_file_store/instrumentation.d.ts.map +1 -0
  123. package/dest/services/tx_file_store/instrumentation.js +29 -0
  124. package/dest/services/tx_file_store/tx_file_store.d.ts +47 -0
  125. package/dest/services/tx_file_store/tx_file_store.d.ts.map +1 -0
  126. package/dest/services/tx_file_store/tx_file_store.js +149 -0
  127. package/dest/test-helpers/testbench-utils.d.ts +10 -16
  128. package/dest/test-helpers/testbench-utils.d.ts.map +1 -1
  129. package/dest/test-helpers/testbench-utils.js +32 -30
  130. package/dest/testbench/p2p_client_testbench_worker.js +1 -1
  131. package/package.json +14 -14
  132. package/src/client/factory.ts +7 -4
  133. package/src/client/interface.ts +13 -1
  134. package/src/client/p2p_client.ts +30 -8
  135. package/src/client/test/tx_proposal_collector/proposal_tx_collector_worker.ts +1 -1
  136. package/src/config.ts +8 -1
  137. package/src/mem_pools/attestation_pool/attestation_pool.ts +444 -90
  138. package/src/mem_pools/attestation_pool/attestation_pool_test_suite.ts +436 -100
  139. package/src/mem_pools/attestation_pool/index.ts +9 -2
  140. package/src/mem_pools/index.ts +1 -1
  141. package/src/mem_pools/interface.ts +2 -2
  142. package/src/mem_pools/tx_pool_v2/README.md +209 -0
  143. package/src/mem_pools/tx_pool_v2/archive/index.ts +1 -0
  144. package/src/mem_pools/tx_pool_v2/archive/tx_archive.ts +120 -0
  145. package/src/mem_pools/tx_pool_v2/eviction/eviction_manager.ts +147 -0
  146. package/src/mem_pools/tx_pool_v2/eviction/fee_payer_balance_eviction_rule.ts +118 -0
  147. package/src/mem_pools/tx_pool_v2/eviction/fee_payer_balance_pre_add_rule.ts +111 -0
  148. package/src/mem_pools/tx_pool_v2/eviction/index.ts +23 -0
  149. package/src/mem_pools/tx_pool_v2/eviction/interfaces.ts +164 -0
  150. package/src/mem_pools/tx_pool_v2/eviction/invalid_txs_after_mining_rule.ts +74 -0
  151. package/src/mem_pools/tx_pool_v2/eviction/invalid_txs_after_reorg_rule.ts +101 -0
  152. package/src/mem_pools/tx_pool_v2/eviction/low_priority_eviction_rule.ts +86 -0
  153. package/src/mem_pools/tx_pool_v2/eviction/low_priority_pre_add_rule.ts +72 -0
  154. package/src/mem_pools/tx_pool_v2/eviction/nullifier_conflict_rule.ts +31 -0
  155. package/src/mem_pools/tx_pool_v2/index.ts +11 -0
  156. package/src/mem_pools/tx_pool_v2/interfaces.ts +227 -0
  157. package/src/mem_pools/tx_pool_v2/tx_metadata.ts +161 -0
  158. package/src/mem_pools/tx_pool_v2/tx_pool_bench_metrics.ts +77 -0
  159. package/src/mem_pools/tx_pool_v2/tx_pool_indices.ts +417 -0
  160. package/src/mem_pools/tx_pool_v2/tx_pool_v2.ts +212 -0
  161. package/src/mem_pools/tx_pool_v2/tx_pool_v2_impl.ts +882 -0
  162. package/src/msg_validators/attestation_validator/fisherman_attestation_validator.ts +2 -2
  163. package/src/services/dummy_service.ts +6 -0
  164. package/src/services/index.ts +1 -0
  165. package/src/services/libp2p/libp2p_service.ts +304 -230
  166. package/src/services/reqresp/batch-tx-requester/README.md +7 -7
  167. package/src/services/reqresp/batch-tx-requester/batch_tx_requester.ts +11 -11
  168. package/src/services/reqresp/protocols/block_txs/block_txs_handler.ts +22 -13
  169. package/src/services/reqresp/protocols/block_txs/block_txs_reqresp.ts +21 -15
  170. package/src/services/service.ts +20 -0
  171. package/src/services/tx_collection/config.ts +6 -6
  172. package/src/services/tx_collection/fast_tx_collection.ts +14 -24
  173. package/src/services/tx_collection/index.ts +1 -1
  174. package/src/services/tx_collection/proposal_tx_collector.ts +12 -14
  175. package/src/services/tx_file_store/config.ts +43 -0
  176. package/src/services/tx_file_store/index.ts +3 -0
  177. package/src/services/tx_file_store/instrumentation.ts +36 -0
  178. package/src/services/tx_file_store/tx_file_store.ts +173 -0
  179. package/src/test-helpers/testbench-utils.ts +18 -39
  180. package/src/testbench/p2p_client_testbench_worker.ts +1 -1
  181. package/dest/mem_pools/attestation_pool/kv_attestation_pool.d.ts +0 -40
  182. package/dest/mem_pools/attestation_pool/kv_attestation_pool.d.ts.map +0 -1
  183. package/dest/mem_pools/attestation_pool/kv_attestation_pool.js +0 -218
  184. package/dest/mem_pools/attestation_pool/memory_attestation_pool.d.ts +0 -31
  185. package/dest/mem_pools/attestation_pool/memory_attestation_pool.d.ts.map +0 -1
  186. package/dest/mem_pools/attestation_pool/memory_attestation_pool.js +0 -180
  187. package/src/mem_pools/attestation_pool/kv_attestation_pool.ts +0 -320
  188. package/src/mem_pools/attestation_pool/memory_attestation_pool.ts +0 -264
@@ -0,0 +1,227 @@
1
+ import type { SlotNumber } from '@aztec/foundation/branded-types';
2
+ import type { TypedEventEmitter } from '@aztec/foundation/types';
3
+ import type { L2Block, L2BlockId, L2BlockSource } from '@aztec/stdlib/block';
4
+ import type { WorldStateSynchronizer } from '@aztec/stdlib/interfaces/server';
5
+ import type { BlockHeader, Tx, TxHash, TxValidator } from '@aztec/stdlib/tx';
6
+
7
+ import type { TxMetaData, TxState } from './tx_metadata.js';
8
+
9
+ /**
10
+ * Result of adding transactions to the pending pool.
11
+ * Categorizes transactions by their outcome.
12
+ */
13
+ export type AddTxsResult = {
14
+ /** Transactions successfully added to the pool */
15
+ accepted: TxHash[];
16
+ /** Transactions ignored because they're valid but undesirable (e.g., duplicate, lower priority nullifier conflict) */
17
+ ignored: TxHash[];
18
+ /** Transactions rejected because they failed validation (e.g., invalid proof, expired timestamp) */
19
+ rejected: TxHash[];
20
+ };
21
+
22
+ /**
23
+ * Events emitted by TxPoolV2.
24
+ */
25
+ export type TxPoolV2Events = {
26
+ /** Emitted when transactions are successfully added to the pool */
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;
30
+ };
31
+
32
+ /**
33
+ * Configuration options for TxPoolV2.
34
+ */
35
+ export type TxPoolV2Config = {
36
+ /** Maximum number of pending transactions before low-priority eviction */
37
+ maxPendingTxCount: number;
38
+ /** Maximum number of archived transactions to retain (0 = disabled) */
39
+ archivedTxLimit: number;
40
+ };
41
+
42
+ /**
43
+ * Default configuration values for TxPoolV2.
44
+ */
45
+ export const DEFAULT_TX_POOL_V2_CONFIG: TxPoolV2Config = {
46
+ maxPendingTxCount: 0, // 0 = disabled
47
+ archivedTxLimit: 0, // 0 = disabled
48
+ };
49
+
50
+ /**
51
+ * Dependencies required by TxPoolV2.
52
+ */
53
+ export type TxPoolV2Dependencies = {
54
+ /** Block source (Archiver) for checking mined status and verifying pruned blocks */
55
+ l2BlockSource: L2BlockSource;
56
+ /** World state synchronizer for validating transactions after chain prunes */
57
+ worldStateSynchronizer: WorldStateSynchronizer;
58
+ /** Validator for transactions entering the pending pool */
59
+ pendingTxValidator: TxValidator<Tx>;
60
+ };
61
+
62
+ /**
63
+ * Read-only access to pool state for pre-add checks.
64
+ * Used by eviction rules to inspect pool state during transaction addition.
65
+ */
66
+ export interface PoolReadAccess {
67
+ /** Get metadata for a transaction by its hash (as string) */
68
+ getMetadata(txHash: string): TxMetaData | undefined;
69
+ /** Get the transaction hash that uses a specific nullifier (as string) */
70
+ getTxHashByNullifier(nullifier: string): string | undefined;
71
+ /** Get all transaction hashes for a fee payer (as string) */
72
+ getTxHashesByFeePayer(feePayer: string): Set<string> | undefined;
73
+ /** Get the current pending transaction count */
74
+ getPendingTxCount(): number;
75
+ }
76
+
77
+ /**
78
+ *
79
+ * The pool manages transactions through a state machine:
80
+ * - Pending: Transaction is awaiting inclusion in a block
81
+ * - Protected: Transaction is being considered for a block proposal
82
+ * - Mined: Transaction has been included in a block
83
+ * - Deleted: Transaction has been removed from the pool
84
+ *
85
+ * All state-mutating operations are serialized through a handler queue
86
+ * to prevent race conditions.
87
+ */
88
+ export interface TxPoolV2 extends TypedEventEmitter<TxPoolV2Events> {
89
+ // === Core Operations ===
90
+
91
+ /**
92
+ * Adds transactions to the pending pool with challenge and validation.
93
+ * Handles nullifier conflicts via the challenge mechanism.
94
+ * @param txs - Transactions to add
95
+ * @param opts - Optional metadata (e.g., source for logging)
96
+ * @returns Result categorizing each transaction as accepted, rejected, or ignored
97
+ */
98
+ addPendingTxs(txs: Tx[], opts?: { source?: string }): Promise<AddTxsResult>;
99
+
100
+ /**
101
+ * Checks if a transaction can be added without modifying the pool.
102
+ * Performs the same validation as addPendingTxs but doesn't persist changes.
103
+ * @param tx - Transaction to check
104
+ * @returns Result: 'accepted', 'ignored' (if already in pool or undesirable), or 'rejected' (if validation fails)
105
+ */
106
+ canAddPendingTx(tx: Tx): Promise<'accepted' | 'ignored' | 'rejected'>;
107
+
108
+ /**
109
+ * Adds transactions as immediately protected for a given slot.
110
+ * Used when receiving transactions from a block proposal we're validating.
111
+ * @param txs - Transactions to add as protected
112
+ * @param block - Block header providing slot context
113
+ * @param opts - Optional metadata (e.g., source for logging)
114
+ */
115
+ addProtectedTxs(txs: Tx[], block: BlockHeader, opts?: { source?: string }): Promise<void>;
116
+
117
+ /**
118
+ * Protects existing transactions by hash for a given slot.
119
+ * Returns hashes of transactions that weren't found in the pool.
120
+ * Records unknown hashes for automatic protection when received via gossip.
121
+ * @param txHashes - Hashes of transactions to protect
122
+ * @param block - Block header providing slot context
123
+ * @returns Hashes of transactions not found in the pool
124
+ */
125
+ protectTxs(txHashes: TxHash[], block: BlockHeader): Promise<TxHash[]>;
126
+
127
+ /**
128
+ * Adds transactions as already mined.
129
+ * Used by prover nodes fetching transactions via request/response.
130
+ * @param txs - Transactions to add as mined
131
+ * @param block - Block header the transactions were mined in
132
+ * @param opts - Optional metadata (e.g., source for logging)
133
+ */
134
+ addMinedTxs(txs: Tx[], block: BlockHeader, opts?: { source?: string }): Promise<void>;
135
+
136
+ // === State Transition Handlers ===
137
+
138
+ /**
139
+ * Handles a mined block - marks transactions as mined and evicts conflicting pending txs.
140
+ * Uses nullifiers directly from the block to evict pending transactions with conflicts.
141
+ * @param block - The complete mined block
142
+ */
143
+ handleMinedBlock(block: L2Block): Promise<void>;
144
+
145
+ /**
146
+ * Prepares the pool for a new slot.
147
+ * Unprotects transactions from earlier slots and validates them before
148
+ * returning to pending state.
149
+ * @param slotNumber - The slot number to prepare for
150
+ */
151
+ prepareForSlot(slotNumber: SlotNumber): Promise<void>;
152
+
153
+ /**
154
+ * Handles pruned blocks during a reorg.
155
+ * Un-mines all transactions mined in blocks beyond the given latest block
156
+ * and validates them before returning to pending.
157
+ * @param latestBlock - The latest valid block ID after the prune
158
+ */
159
+ handlePrunedBlocks(latestBlock: L2BlockId): Promise<void>;
160
+
161
+ /**
162
+ * Handles failed transaction execution.
163
+ * Deletes transactions that failed during block building.
164
+ * @param txHashes - Hashes of transactions that failed
165
+ */
166
+ handleFailedExecution(txHashes: TxHash[]): Promise<void>;
167
+
168
+ /**
169
+ * Handles a finalized block.
170
+ * Permanently deletes mined transactions and optionally archives them.
171
+ * @param block - Header of the finalized block
172
+ */
173
+ handleFinalizedBlock(block: BlockHeader): Promise<void>;
174
+
175
+ /** Gets a transaction by its hash */
176
+ getTxByHash(txHash: TxHash): Promise<Tx | undefined>;
177
+
178
+ /** Gets multiple transactions by their hashes */
179
+ getTxsByHash(txHashes: TxHash[]): Promise<(Tx | undefined)[]>;
180
+
181
+ /** Checks if transactions exist in the pool */
182
+ hasTxs(txHashes: TxHash[]): Promise<boolean[]>;
183
+
184
+ /** Gets the status of a transaction */
185
+ getTxStatus(txHash: TxHash): Promise<TxState | 'deleted' | undefined>;
186
+
187
+ /** Gets pending transaction hashes sorted by priority (highest first) */
188
+ getPendingTxHashes(): Promise<TxHash[]>;
189
+
190
+ /** Gets the count of pending transactions */
191
+ getPendingTxCount(): Promise<number>;
192
+
193
+ /** Gets mined transaction hashes with their block IDs */
194
+ getMinedTxHashes(): Promise<[TxHash, L2BlockId][]>;
195
+
196
+ /** Gets the count of mined transactions */
197
+ getMinedTxCount(): Promise<number>;
198
+
199
+ /** Checks if the pool is empty */
200
+ isEmpty(): Promise<boolean>;
201
+
202
+ /** Gets an archived transaction by its hash */
203
+ getArchivedTxByHash(txHash: TxHash): Promise<Tx | undefined>;
204
+
205
+ /** Gets the lowest priority pending transactions */
206
+ getLowestPriorityPending(limit: number): Promise<TxHash[]>;
207
+
208
+ // === Configuration ===
209
+
210
+ /** Updates the pool configuration */
211
+ updateConfig(config: Partial<TxPoolV2Config>): Promise<void>;
212
+
213
+ // === Lifecycle ===
214
+
215
+ /**
216
+ * Starts the pool and initializes state from persistence.
217
+ * Must be called before other operations.
218
+ * - Reads all transactions from the database
219
+ * - Checks each against the Archiver to determine mined status
220
+ * - Validates all non-mined transactions
221
+ * - Populates in-memory indices
222
+ */
223
+ start(): Promise<void>;
224
+
225
+ /** Stops the pool and releases resources */
226
+ stop(): Promise<void>;
227
+ }
@@ -0,0 +1,161 @@
1
+ import { Fr } from '@aztec/foundation/curves/bn254';
2
+ import { ProtocolContractAddress } from '@aztec/protocol-contracts';
3
+ import type { L2BlockId } from '@aztec/stdlib/block';
4
+ import type { Tx } from '@aztec/stdlib/tx';
5
+
6
+ import { getFeePayerBalanceDelta } from '../../msg_validators/tx_validator/fee_payer_balance.js';
7
+ import { getTxPriorityFee } from '../tx_pool/priority.js';
8
+ import type { PreAddResult } from './eviction/interfaces.js';
9
+
10
+ /**
11
+ * Lightweight in-memory representation of a transaction.
12
+ * Stored for every tx in the pool to enable efficient queries and challenges
13
+ * without deserializing full transaction data.
14
+ *
15
+ * Uses strings for txHash and nullifiers to enable fast Map/Set lookups
16
+ * without repeated .toString() conversions.
17
+ */
18
+ export type TxMetaData = {
19
+ /** The transaction hash as hex string */
20
+ readonly txHash: string;
21
+
22
+ /** Block ID (number and hash) in which the transaction was mined (undefined if not mined) */
23
+ minedL2BlockId?: L2BlockId;
24
+
25
+ /** Hash of the block header the transaction uses as its anchor (hex string) */
26
+ readonly anchorBlockHeaderHash: string;
27
+
28
+ /** The total priority fee (used for ordering and challenges) */
29
+ readonly priorityFee: bigint;
30
+
31
+ /** The fee payer address as hex string */
32
+ readonly feePayer: string;
33
+
34
+ /** The claim amount for the fee payer */
35
+ readonly claimAmount: bigint;
36
+
37
+ /** The fee limit */
38
+ readonly feeLimit: bigint;
39
+
40
+ /** Non-empty nullifiers emitted by the transaction (hex strings) */
41
+ readonly nullifiers: readonly string[];
42
+
43
+ /** Timestamp by which the transaction must be included (for expiration checks) */
44
+ readonly includeByTimestamp: bigint;
45
+ };
46
+
47
+ /** Transaction state derived from TxMetaData fields and pool protection status */
48
+ export type TxState = 'pending' | 'protected' | 'mined';
49
+
50
+ /**
51
+ * Builds TxMetaData from a full Tx object.
52
+ * Extracts all relevant fields for efficient in-memory storage and querying.
53
+ */
54
+ export async function buildTxMetaData(tx: Tx): Promise<TxMetaData> {
55
+ const txHash = tx.getTxHash().toString();
56
+ const anchorBlockHeaderHash = (await tx.data.constants.anchorBlockHeader.hash()).toString();
57
+ const priorityFee = getTxPriorityFee(tx);
58
+ const feePayer = tx.data.feePayer.toString();
59
+ const nullifiers = tx.data.getNonEmptyNullifiers().map(n => n.toString());
60
+ const includeByTimestamp = tx.data.includeByTimestamp;
61
+
62
+ const { feeLimit, claimAmount } = await getFeePayerBalanceDelta(tx, ProtocolContractAddress.FeeJuice);
63
+
64
+ return {
65
+ txHash,
66
+ anchorBlockHeaderHash,
67
+ priorityFee,
68
+ feePayer,
69
+ claimAmount,
70
+ feeLimit,
71
+ nullifiers,
72
+ includeByTimestamp,
73
+ };
74
+ }
75
+
76
+ /** Minimal fields required for priority comparison. */
77
+ type PriorityComparable = Pick<TxMetaData, 'txHash' | 'priorityFee'>;
78
+
79
+ /**
80
+ * Compares two priority fees in ascending order.
81
+ * Returns negative if a < b, positive if a > b, 0 if equal.
82
+ */
83
+ export function compareFee(a: bigint, b: bigint): number {
84
+ return a < b ? -1 : a > b ? 1 : 0;
85
+ }
86
+
87
+ /**
88
+ * Compares two tx hashes in ascending order.
89
+ * Uses field element comparison for deterministic ordering.
90
+ * Returns negative if a < b, positive if a > b, 0 if equal.
91
+ */
92
+ export function compareTxHash(a: string, b: string): number {
93
+ const fieldA = Fr.fromHexString(a);
94
+ const fieldB = Fr.fromHexString(b);
95
+ return fieldA.cmp(fieldB);
96
+ }
97
+
98
+ /**
99
+ * Compares two transactions by priority fee, with txHash as tiebreaker.
100
+ * Returns negative if a < b, positive if a > b, 0 if equal.
101
+ * Use with sort() for ascending order, or negate/reverse for descending.
102
+ */
103
+ export function comparePriority(a: PriorityComparable, b: PriorityComparable): number {
104
+ const feeComparison = compareFee(a.priorityFee, b.priorityFee);
105
+ if (feeComparison !== 0) {
106
+ return feeComparison;
107
+ }
108
+ return compareTxHash(a.txHash, b.txHash);
109
+ }
110
+
111
+ /**
112
+ * Checks for nullifier conflicts between an incoming transaction and existing pool state.
113
+ *
114
+ * When the incoming tx shares nullifiers with existing pending txs:
115
+ * - If the incoming tx has strictly higher priority, mark conflicting txs for eviction
116
+ * - If any conflicting tx has equal or higher priority, ignore the incoming tx
117
+ *
118
+ * @param incomingMeta - Metadata for the incoming transaction
119
+ * @param getTxHashByNullifier - Accessor to find which tx uses a nullifier
120
+ * @param getMetadata - Accessor to get metadata for a tx hash
121
+ */
122
+ export function checkNullifierConflict(
123
+ incomingMeta: TxMetaData,
124
+ getTxHashByNullifier: (nullifier: string) => string | undefined,
125
+ getMetadata: (txHash: string) => TxMetaData | undefined,
126
+ ): PreAddResult {
127
+ const txHashesToEvict: string[] = [];
128
+
129
+ for (const nullifier of incomingMeta.nullifiers) {
130
+ const conflictingHashStr = getTxHashByNullifier(nullifier);
131
+
132
+ if (!conflictingHashStr || conflictingHashStr === incomingMeta.txHash) {
133
+ continue;
134
+ }
135
+
136
+ // Skip if already marked for eviction
137
+ if (txHashesToEvict.includes(conflictingHashStr)) {
138
+ continue;
139
+ }
140
+
141
+ const conflictingMeta = getMetadata(conflictingHashStr);
142
+ if (!conflictingMeta) {
143
+ continue;
144
+ }
145
+
146
+ // If incoming tx has strictly higher priority, mark for eviction
147
+ // Otherwise, ignore incoming tx (ties go to existing tx)
148
+ // Use comparePriority for deterministic ordering (includes txHash as tiebreaker)
149
+ if (comparePriority(incomingMeta, conflictingMeta) > 0) {
150
+ txHashesToEvict.push(conflictingHashStr);
151
+ } else {
152
+ return {
153
+ shouldIgnore: true,
154
+ txHashesToEvict: [],
155
+ reason: `nullifier conflict with ${conflictingHashStr}`,
156
+ };
157
+ }
158
+ }
159
+
160
+ return { shouldIgnore: false, txHashesToEvict };
161
+ }
@@ -0,0 +1,77 @@
1
+ /**
2
+ * Metrics types for tx pool benchmarks.
3
+ */
4
+
5
+ export enum TxPoolOperation {
6
+ ADD_PENDING_TXS = 'addPendingTxs',
7
+ CAN_ADD_PENDING_TX = 'canAddPendingTx',
8
+ GET_PENDING_TX_HASHES = 'getPendingTxHashes',
9
+ GET_TX_BY_HASH = 'getTxByHash',
10
+ GET_TXS_BY_HASH = 'getTxsByHash',
11
+ HAS_TXS = 'hasTxs',
12
+ HANDLE_MINED_BLOCK = 'handleMinedBlock',
13
+ PREPARE_FOR_SLOT = 'prepareForSlot',
14
+ HANDLE_PRUNED_BLOCKS = 'handlePrunedBlocks',
15
+ GET_LOWEST_PRIORITY_PENDING = 'getLowestPriorityPending',
16
+ HYDRATE_FROM_DATABASE = 'hydrateFromDatabase',
17
+ }
18
+
19
+ type OperationMetric = {
20
+ operation: TxPoolOperation;
21
+ poolSize: number;
22
+ batchSize: number;
23
+ value: number;
24
+ };
25
+
26
+ /**
27
+ * Collects and formats tx pool benchmark metrics.
28
+ */
29
+ export class TxPoolBenchMetrics {
30
+ private metrics: OperationMetric[] = [];
31
+
32
+ public addMetric(operation: TxPoolOperation, poolSize: number, batchSize: number, value: number) {
33
+ this.metrics.push({ operation, poolSize, batchSize, value });
34
+ }
35
+
36
+ public toPrettyString(): string {
37
+ let pretty = 'TxPool Benchmark Metrics:\n';
38
+ pretty += '='.repeat(60) + '\n';
39
+
40
+ // Group by operation
41
+ const byOperation = new Map<TxPoolOperation, OperationMetric[]>();
42
+ for (const metric of this.metrics) {
43
+ if (!byOperation.has(metric.operation)) {
44
+ byOperation.set(metric.operation, []);
45
+ }
46
+ byOperation.get(metric.operation)!.push(metric);
47
+ }
48
+
49
+ for (const [operation, opMetrics] of byOperation) {
50
+ pretty += `\n${operation}:\n`;
51
+ for (const metric of opMetrics) {
52
+ const poolSizeStr = metric.poolSize > 0 ? `pool=${metric.poolSize}` : '';
53
+ const batchSizeStr = metric.batchSize > 0 ? `batch=${metric.batchSize}` : '';
54
+ const params = [poolSizeStr, batchSizeStr].filter(Boolean).join(', ');
55
+ pretty += ` ${params ? `(${params})` : ''}: ${metric.value.toFixed(3)} ms\n`;
56
+ }
57
+ }
58
+
59
+ return pretty;
60
+ }
61
+
62
+ public toGithubActionBenchmarkJSON(indent = 2): string {
63
+ const data = this.metrics.map(metric => {
64
+ const poolSizeStr = metric.poolSize > 0 ? `${metric.poolSize} txs in pool` : '';
65
+ const batchSizeStr = metric.batchSize > 0 ? `batch of ${metric.batchSize}` : '';
66
+ const params = [poolSizeStr, batchSizeStr].filter(Boolean).join('/');
67
+
68
+ return {
69
+ name: `TxPool/${metric.operation}${params ? `/${params}` : ''}`,
70
+ value: metric.value,
71
+ unit: 'ms',
72
+ };
73
+ });
74
+
75
+ return JSON.stringify(data, null, indent);
76
+ }
77
+ }