@aztec/p2p 0.0.1-commit.c7c42ec → 0.0.1-commit.f295ac2

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/interface.d.ts +18 -5
  2. package/dest/client/interface.d.ts.map +1 -1
  3. package/dest/client/p2p_client.d.ts +10 -13
  4. package/dest/client/p2p_client.d.ts.map +1 -1
  5. package/dest/client/p2p_client.js +449 -118
  6. package/dest/config.js +2 -2
  7. package/dest/mem_pools/attestation_pool/attestation_pool.d.ts +61 -42
  8. package/dest/mem_pools/attestation_pool/attestation_pool.d.ts.map +1 -1
  9. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.d.ts +1 -1
  10. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.d.ts.map +1 -1
  11. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.js +237 -263
  12. package/dest/mem_pools/attestation_pool/kv_attestation_pool.d.ts +21 -18
  13. package/dest/mem_pools/attestation_pool/kv_attestation_pool.d.ts.map +1 -1
  14. package/dest/mem_pools/attestation_pool/kv_attestation_pool.js +113 -108
  15. package/dest/mem_pools/attestation_pool/memory_attestation_pool.d.ts +17 -16
  16. package/dest/mem_pools/attestation_pool/memory_attestation_pool.d.ts.map +1 -1
  17. package/dest/mem_pools/attestation_pool/memory_attestation_pool.js +89 -128
  18. package/dest/mem_pools/attestation_pool/mocks.d.ts +9 -6
  19. package/dest/mem_pools/attestation_pool/mocks.d.ts.map +1 -1
  20. package/dest/mem_pools/attestation_pool/mocks.js +16 -12
  21. package/dest/mem_pools/instrumentation.d.ts +1 -1
  22. package/dest/mem_pools/instrumentation.d.ts.map +1 -1
  23. package/dest/mem_pools/instrumentation.js +4 -13
  24. package/dest/mem_pools/tx_pool/aztec_kv_tx_pool.d.ts +13 -8
  25. package/dest/mem_pools/tx_pool/aztec_kv_tx_pool.d.ts.map +1 -1
  26. package/dest/mem_pools/tx_pool/aztec_kv_tx_pool.js +91 -50
  27. package/dest/mem_pools/tx_pool/eviction/eviction_manager.d.ts +17 -4
  28. package/dest/mem_pools/tx_pool/eviction/eviction_manager.d.ts.map +1 -1
  29. package/dest/mem_pools/tx_pool/eviction/eviction_manager.js +59 -3
  30. package/dest/mem_pools/tx_pool/eviction/eviction_strategy.d.ts +77 -4
  31. package/dest/mem_pools/tx_pool/eviction/eviction_strategy.d.ts.map +1 -1
  32. package/dest/mem_pools/tx_pool/eviction/eviction_strategy.js +47 -0
  33. package/dest/mem_pools/tx_pool/eviction/fee_payer_balance_eviction_rule.d.ts +16 -0
  34. package/dest/mem_pools/tx_pool/eviction/fee_payer_balance_eviction_rule.d.ts.map +1 -0
  35. package/dest/mem_pools/tx_pool/eviction/fee_payer_balance_eviction_rule.js +115 -0
  36. package/dest/mem_pools/tx_pool/eviction/invalid_txs_after_mining_rule.d.ts +2 -2
  37. package/dest/mem_pools/tx_pool/eviction/invalid_txs_after_mining_rule.d.ts.map +1 -1
  38. package/dest/mem_pools/tx_pool/eviction/invalid_txs_after_reorg_rule.d.ts +2 -2
  39. package/dest/mem_pools/tx_pool/eviction/invalid_txs_after_reorg_rule.d.ts.map +1 -1
  40. package/dest/mem_pools/tx_pool/eviction/low_priority_eviction_rule.d.ts +2 -2
  41. package/dest/mem_pools/tx_pool/eviction/low_priority_eviction_rule.d.ts.map +1 -1
  42. package/dest/mem_pools/tx_pool/eviction/nullifier_conflict_pre_add_rule.d.ts +25 -0
  43. package/dest/mem_pools/tx_pool/eviction/nullifier_conflict_pre_add_rule.d.ts.map +1 -0
  44. package/dest/mem_pools/tx_pool/eviction/nullifier_conflict_pre_add_rule.js +57 -0
  45. package/dest/msg_validators/attestation_validator/attestation_validator.d.ts +4 -4
  46. package/dest/msg_validators/attestation_validator/attestation_validator.d.ts.map +1 -1
  47. package/dest/msg_validators/attestation_validator/attestation_validator.js +12 -10
  48. package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.d.ts +5 -5
  49. package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.d.ts.map +1 -1
  50. package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.js +7 -10
  51. package/dest/msg_validators/index.d.ts +2 -2
  52. package/dest/msg_validators/index.d.ts.map +1 -1
  53. package/dest/msg_validators/index.js +1 -1
  54. package/dest/msg_validators/proposal_validator/block_proposal_validator.d.ts +9 -0
  55. package/dest/msg_validators/proposal_validator/block_proposal_validator.d.ts.map +1 -0
  56. package/dest/msg_validators/proposal_validator/block_proposal_validator.js +6 -0
  57. package/dest/msg_validators/proposal_validator/checkpoint_proposal_validator.d.ts +9 -0
  58. package/dest/msg_validators/proposal_validator/checkpoint_proposal_validator.d.ts.map +1 -0
  59. package/dest/msg_validators/proposal_validator/checkpoint_proposal_validator.js +6 -0
  60. package/dest/msg_validators/proposal_validator/index.d.ts +4 -0
  61. package/dest/msg_validators/proposal_validator/index.d.ts.map +1 -0
  62. package/dest/msg_validators/proposal_validator/index.js +3 -0
  63. package/dest/msg_validators/proposal_validator/proposal_validator.d.ts +13 -0
  64. package/dest/msg_validators/proposal_validator/proposal_validator.d.ts.map +1 -0
  65. package/dest/msg_validators/{block_proposal_validator/block_proposal_validator.js → proposal_validator/proposal_validator.js} +19 -21
  66. package/dest/msg_validators/proposal_validator/proposal_validator_test_suite.d.ts +23 -0
  67. package/dest/msg_validators/proposal_validator/proposal_validator_test_suite.d.ts.map +1 -0
  68. package/dest/msg_validators/proposal_validator/proposal_validator_test_suite.js +183 -0
  69. package/dest/msg_validators/tx_validator/data_validator.d.ts +1 -1
  70. package/dest/msg_validators/tx_validator/data_validator.d.ts.map +1 -1
  71. package/dest/msg_validators/tx_validator/fee_payer_balance.d.ts +10 -0
  72. package/dest/msg_validators/tx_validator/fee_payer_balance.d.ts.map +1 -0
  73. package/dest/msg_validators/tx_validator/fee_payer_balance.js +20 -0
  74. package/dest/msg_validators/tx_validator/gas_validator.d.ts +1 -1
  75. package/dest/msg_validators/tx_validator/gas_validator.d.ts.map +1 -1
  76. package/dest/msg_validators/tx_validator/gas_validator.js +8 -14
  77. package/dest/msg_validators/tx_validator/metadata_validator.d.ts +1 -1
  78. package/dest/msg_validators/tx_validator/metadata_validator.d.ts.map +1 -1
  79. package/dest/msg_validators/tx_validator/timestamp_validator.d.ts +1 -1
  80. package/dest/msg_validators/tx_validator/timestamp_validator.d.ts.map +1 -1
  81. package/dest/services/dummy_service.d.ts +6 -2
  82. package/dest/services/dummy_service.d.ts.map +1 -1
  83. package/dest/services/dummy_service.js +3 -0
  84. package/dest/services/encoding.d.ts +1 -1
  85. package/dest/services/encoding.d.ts.map +1 -1
  86. package/dest/services/encoding.js +4 -2
  87. package/dest/services/libp2p/instrumentation.d.ts +1 -1
  88. package/dest/services/libp2p/instrumentation.d.ts.map +1 -1
  89. package/dest/services/libp2p/instrumentation.js +20 -73
  90. package/dest/services/libp2p/libp2p_service.d.ts +27 -10
  91. package/dest/services/libp2p/libp2p_service.d.ts.map +1 -1
  92. package/dest/services/libp2p/libp2p_service.js +696 -137
  93. package/dest/services/peer-manager/metrics.d.ts +1 -1
  94. package/dest/services/peer-manager/metrics.d.ts.map +1 -1
  95. package/dest/services/peer-manager/metrics.js +6 -26
  96. package/dest/services/peer-manager/peer_manager.d.ts +2 -2
  97. package/dest/services/peer-manager/peer_manager.d.ts.map +1 -1
  98. package/dest/services/peer-manager/peer_manager.js +0 -10
  99. package/dest/services/peer-manager/peer_scoring.d.ts +1 -1
  100. package/dest/services/peer-manager/peer_scoring.d.ts.map +1 -1
  101. package/dest/services/peer-manager/peer_scoring.js +2 -5
  102. package/dest/services/reqresp/connection-sampler/connection_sampler.d.ts +1 -1
  103. package/dest/services/reqresp/connection-sampler/connection_sampler.d.ts.map +1 -1
  104. package/dest/services/reqresp/constants.d.ts +12 -0
  105. package/dest/services/reqresp/constants.d.ts.map +1 -0
  106. package/dest/services/reqresp/constants.js +7 -0
  107. package/dest/services/reqresp/interface.d.ts +3 -3
  108. package/dest/services/reqresp/interface.d.ts.map +1 -1
  109. package/dest/services/reqresp/interface.js +2 -2
  110. package/dest/services/reqresp/metrics.d.ts +1 -1
  111. package/dest/services/reqresp/metrics.d.ts.map +1 -1
  112. package/dest/services/reqresp/metrics.js +5 -21
  113. package/dest/services/reqresp/protocols/block_txs/bitvector.d.ts +1 -1
  114. package/dest/services/reqresp/protocols/block_txs/bitvector.d.ts.map +1 -1
  115. package/dest/services/reqresp/protocols/block_txs/bitvector.js +7 -0
  116. package/dest/services/reqresp/protocols/status.d.ts +1 -1
  117. package/dest/services/reqresp/protocols/status.d.ts.map +1 -1
  118. package/dest/services/reqresp/protocols/status.js +4 -1
  119. package/dest/services/reqresp/reqresp.js +402 -24
  120. package/dest/services/service.d.ts +16 -3
  121. package/dest/services/service.d.ts.map +1 -1
  122. package/dest/services/tx_collection/instrumentation.d.ts +1 -1
  123. package/dest/services/tx_collection/instrumentation.d.ts.map +1 -1
  124. package/dest/services/tx_collection/instrumentation.js +4 -14
  125. package/dest/services/tx_provider_instrumentation.d.ts +1 -1
  126. package/dest/services/tx_provider_instrumentation.d.ts.map +1 -1
  127. package/dest/services/tx_provider_instrumentation.js +6 -19
  128. package/dest/testbench/p2p_client_testbench_worker.js +27 -12
  129. package/dest/testbench/worker_client_manager.d.ts +1 -1
  130. package/dest/testbench/worker_client_manager.d.ts.map +1 -1
  131. package/dest/testbench/worker_client_manager.js +6 -1
  132. package/package.json +16 -16
  133. package/src/client/interface.ts +19 -4
  134. package/src/client/p2p_client.ts +78 -128
  135. package/src/config.ts +2 -2
  136. package/src/mem_pools/attestation_pool/attestation_pool.ts +68 -41
  137. package/src/mem_pools/attestation_pool/attestation_pool_test_suite.ts +239 -287
  138. package/src/mem_pools/attestation_pool/kv_attestation_pool.ts +162 -140
  139. package/src/mem_pools/attestation_pool/memory_attestation_pool.ts +141 -164
  140. package/src/mem_pools/attestation_pool/mocks.ts +19 -13
  141. package/src/mem_pools/instrumentation.ts +9 -18
  142. package/src/mem_pools/tx_pool/README.md +28 -13
  143. package/src/mem_pools/tx_pool/aztec_kv_tx_pool.ts +128 -73
  144. package/src/mem_pools/tx_pool/eviction/eviction_manager.ts +64 -4
  145. package/src/mem_pools/tx_pool/eviction/eviction_strategy.ts +117 -3
  146. package/src/mem_pools/tx_pool/eviction/fee_payer_balance_eviction_rule.ts +159 -0
  147. package/src/mem_pools/tx_pool/eviction/nullifier_conflict_pre_add_rule.ts +75 -0
  148. package/src/msg_validators/attestation_validator/attestation_validator.ts +16 -13
  149. package/src/msg_validators/attestation_validator/fisherman_attestation_validator.ts +9 -12
  150. package/src/msg_validators/index.ts +1 -1
  151. package/src/msg_validators/proposal_validator/block_proposal_validator.ts +10 -0
  152. package/src/msg_validators/proposal_validator/checkpoint_proposal_validator.ts +13 -0
  153. package/src/msg_validators/proposal_validator/index.ts +3 -0
  154. package/src/msg_validators/{block_proposal_validator/block_proposal_validator.ts → proposal_validator/proposal_validator.ts} +23 -28
  155. package/src/msg_validators/proposal_validator/proposal_validator_test_suite.ts +206 -0
  156. package/src/msg_validators/tx_validator/data_validator.ts +12 -4
  157. package/src/msg_validators/tx_validator/fee_payer_balance.ts +40 -0
  158. package/src/msg_validators/tx_validator/gas_validator.ts +8 -25
  159. package/src/msg_validators/tx_validator/metadata_validator.ts +12 -4
  160. package/src/msg_validators/tx_validator/timestamp_validator.ts +3 -1
  161. package/src/services/dummy_service.ts +6 -0
  162. package/src/services/encoding.ts +3 -1
  163. package/src/services/libp2p/instrumentation.ts +19 -73
  164. package/src/services/libp2p/libp2p_service.ts +326 -102
  165. package/src/services/peer-manager/metrics.ts +5 -26
  166. package/src/services/peer-manager/peer_manager.ts +1 -2
  167. package/src/services/peer-manager/peer_scoring.ts +1 -5
  168. package/src/services/reqresp/connection-sampler/connection_sampler.ts +3 -1
  169. package/src/services/reqresp/constants.ts +14 -0
  170. package/src/services/reqresp/interface.ts +2 -2
  171. package/src/services/reqresp/metrics.ts +7 -23
  172. package/src/services/reqresp/protocols/block_txs/bitvector.ts +9 -0
  173. package/src/services/reqresp/protocols/status.ts +7 -4
  174. package/src/services/service.ts +19 -4
  175. package/src/services/tx_collection/instrumentation.ts +4 -21
  176. package/src/services/tx_provider_instrumentation.ts +11 -24
  177. package/src/testbench/p2p_client_testbench_worker.ts +35 -12
  178. package/src/testbench/worker_client_manager.ts +6 -1
  179. package/dest/mem_pools/tx_pool/eviction/insufficient_fee_payer_balance_rule.d.ts +0 -15
  180. package/dest/mem_pools/tx_pool/eviction/insufficient_fee_payer_balance_rule.d.ts.map +0 -1
  181. package/dest/mem_pools/tx_pool/eviction/insufficient_fee_payer_balance_rule.js +0 -88
  182. package/dest/msg_validators/block_proposal_validator/block_proposal_validator.d.ts +0 -12
  183. package/dest/msg_validators/block_proposal_validator/block_proposal_validator.d.ts.map +0 -1
  184. package/dest/msg_validators/block_proposal_validator/index.d.ts +0 -2
  185. package/dest/msg_validators/block_proposal_validator/index.d.ts.map +0 -1
  186. package/dest/msg_validators/block_proposal_validator/index.js +0 -1
  187. package/src/mem_pools/tx_pool/eviction/insufficient_fee_payer_balance_rule.ts +0 -108
  188. package/src/msg_validators/block_proposal_validator/index.ts +0 -1
@@ -0,0 +1,159 @@
1
+ import { createLogger } from '@aztec/foundation/log';
2
+ import { ProtocolContractAddress } from '@aztec/protocol-contracts';
3
+ import { computeFeePayerBalanceStorageSlot } from '@aztec/protocol-contracts/fee-juice';
4
+ import type { AztecAddress } from '@aztec/stdlib/aztec-address';
5
+ import type { ReadonlyWorldStateAccess } from '@aztec/stdlib/interfaces/server';
6
+ import { DatabasePublicStateSource, type MerkleTreeReadOperations } from '@aztec/stdlib/trees';
7
+ import type { TxHash } from '@aztec/stdlib/tx';
8
+
9
+ import type { TxPoolOptions } from '../tx_pool.js';
10
+ import {
11
+ type EvictionContext,
12
+ EvictionEvent,
13
+ type EvictionResult,
14
+ type EvictionRule,
15
+ type FeePayerTxInfo,
16
+ type TxPoolOperations,
17
+ } from './eviction_strategy.js';
18
+
19
+ export class FeePayerBalanceEvictionRule implements EvictionRule {
20
+ public readonly name = 'FeePayerBalanceEviction';
21
+ public readonly reason = 'fee_payer_balance';
22
+
23
+ private log = createLogger('p2p:mempool:tx_pool:fee_payer_balance_eviction_rule');
24
+
25
+ constructor(private worldState: ReadonlyWorldStateAccess) {}
26
+
27
+ async evict(context: EvictionContext, txPool: TxPoolOperations): Promise<EvictionResult> {
28
+ try {
29
+ if (context.event === EvictionEvent.TXS_ADDED) {
30
+ return await this.evictForFeePayers(context.feePayers, this.worldState.getCommitted(), txPool);
31
+ }
32
+
33
+ if (context.event === EvictionEvent.BLOCK_MINED) {
34
+ return await this.evictForFeePayers(
35
+ context.feePayers,
36
+ this.worldState.getSnapshot(context.block.getBlockNumber()),
37
+ txPool,
38
+ );
39
+ }
40
+
41
+ // TODO: fix this edge-case
42
+ // This can lead to a race condition if we are catching up in the p2p client.
43
+ // Let's say we have 3 txs for the same fee payer, which get mined in blocks 1, 2, 3.
44
+ // Tx1 consumes fee juice, tx2 increases it, tx3 consumes it again. We see block1 with tx1 first, run this rule, and evict tx3.
45
+ // But tx3 was valid (due to tx2) and mined on block3. And we have just removed from the mempool a tx we needed for proving/reexec.
46
+ //
47
+ // NOTE: this will happen only in case of that lower-priority-fee tx entered in e.g. block 2, and we have higher-priority-fee tx in block 3
48
+ // (simply because that was the timing of these txs). But, in case of higher-priority-fee txs being in block 2, the tx3 won't be evicted
49
+ // -----
50
+ // Proposed fix: evict only if node is synched
51
+ if (context.event === EvictionEvent.CHAIN_PRUNED) {
52
+ const feePayers = await txPool.getPendingFeePayers();
53
+ return await this.evictForFeePayers(feePayers, this.worldState.getSnapshot(context.blockNumber), txPool);
54
+ }
55
+
56
+ return {
57
+ reason: this.reason,
58
+ success: true,
59
+ txsEvicted: [],
60
+ };
61
+ } catch (err) {
62
+ this.log.error('Failed to evict txs due to fee payer balance', { err });
63
+ return {
64
+ reason: this.reason,
65
+ success: false,
66
+ txsEvicted: [],
67
+ error: new Error('Failed to evict txs due to fee payer balance', { cause: err }),
68
+ };
69
+ }
70
+ }
71
+
72
+ updateConfig(_config: TxPoolOptions): void {}
73
+
74
+ private async evictForFeePayers(
75
+ feePayers: Array<AztecAddress>, // assumed to be unique
76
+ db: MerkleTreeReadOperations,
77
+ txPool: TxPoolOperations,
78
+ ): Promise<EvictionResult> {
79
+ const publicStateSource = this.createPublicStateSource(db);
80
+
81
+ const txsToEvict = (
82
+ await Promise.all(feePayers.map(feePayer => this.getEvictionsForFeePayer(feePayer, publicStateSource, txPool)))
83
+ ).flat();
84
+
85
+ if (txsToEvict.length > 0) {
86
+ await txPool.deleteTxs(txsToEvict);
87
+ }
88
+
89
+ return {
90
+ reason: this.reason,
91
+ success: true,
92
+ txsEvicted: txsToEvict,
93
+ };
94
+ }
95
+
96
+ private async getEvictionsForFeePayer(
97
+ feePayer: AztecAddress,
98
+ publicStateSource: DatabasePublicStateSource,
99
+ txPool: TxPoolOperations,
100
+ ): Promise<TxHash[]> {
101
+ const initialBalance = (
102
+ await publicStateSource.storageRead(
103
+ ProtocolContractAddress.FeeJuice,
104
+ //TODO: cache this LRU-style
105
+ await computeFeePayerBalanceStorageSlot(feePayer),
106
+ )
107
+ ).toBigInt();
108
+
109
+ const txs: FeePayerTxInfo[] = [];
110
+ for await (const entry of txPool.getFeePayerTxInfos(feePayer)) {
111
+ txs.push(entry);
112
+ }
113
+
114
+ if (txs.length === 0) {
115
+ return [];
116
+ }
117
+
118
+ const txsToEvict: TxHash[] = [];
119
+ let balance = initialBalance;
120
+ let hasNonEvictableOverBalance = false;
121
+
122
+ // Evaluate balance in priority order so later claims cannot fund earlier spends.
123
+ // This sorts so that higher priority txs come first.
124
+ txs.sort((a, b) => {
125
+ if (a.priority === b.priority) {
126
+ return a.txHash.toBigInt() >= b.txHash.toBigInt() ? -1 : 1;
127
+ }
128
+
129
+ return a.priority > b.priority ? -1 : 1;
130
+ });
131
+
132
+ for (const tx of txs) {
133
+ const available = balance + tx.claimAmount;
134
+ if (available >= tx.feeLimit) {
135
+ balance = available - tx.feeLimit;
136
+ continue;
137
+ }
138
+
139
+ if (tx.isEvictable) {
140
+ txsToEvict.push(tx.txHash);
141
+ } else {
142
+ hasNonEvictableOverBalance = true;
143
+ }
144
+ }
145
+
146
+ if (hasNonEvictableOverBalance) {
147
+ this.log.verbose('Fee payer balance cannot be satisfied due to non-evictable txs', {
148
+ feePayer: feePayer.toString(),
149
+ balance: initialBalance,
150
+ });
151
+ }
152
+
153
+ return txsToEvict;
154
+ }
155
+
156
+ private createPublicStateSource(db: MerkleTreeReadOperations): DatabasePublicStateSource {
157
+ return new DatabasePublicStateSource(db);
158
+ }
159
+ }
@@ -0,0 +1,75 @@
1
+ import { findIndexInSortedArray, insertIntoSortedArray } from '@aztec/foundation/array';
2
+ import { Fr } from '@aztec/foundation/curves/bn254';
3
+ import { createLogger } from '@aztec/foundation/log';
4
+ import { type Tx, TxHash } from '@aztec/stdlib/tx';
5
+
6
+ import type { PreAddEvictionResult, PreAddEvictionRule, PreAddPoolAccess } from './eviction_strategy.js';
7
+
8
+ const cmpTxHash = (a: TxHash, b: TxHash) => Fr.cmp(a.hash, b.hash);
9
+
10
+ /**
11
+ * Pre-add eviction rule that checks for nullifier conflicts between incoming and existing transactions.
12
+ *
13
+ * When an incoming tx shares nullifiers with existing pending txs:
14
+ * - If the incoming tx has strictly higher priority fee, evict all conflicting txs
15
+ * - If any conflicting tx has equal or higher priority fee, reject the incoming tx
16
+ *
17
+ * This prevents nullifier spam attacks where an attacker floods the mempool with
18
+ * transactions spending the same nullifiers.
19
+ */
20
+ export class NullifierConflictPreAddRule implements PreAddEvictionRule {
21
+ public readonly name = 'NullifierConflictPreAdd';
22
+
23
+ private log = createLogger('p2p:mempool:tx_pool:nullifier_conflict_pre_add_rule');
24
+
25
+ /**
26
+ * Check if the incoming transaction conflicts with existing transactions via nullifiers.
27
+ *
28
+ * @param tx - The incoming transaction
29
+ * @param poolAccess - Read-only access to pool state
30
+ * @returns Result with rejection status and txs to evict
31
+ */
32
+ async check(tx: Tx, poolAccess: PreAddPoolAccess): Promise<PreAddEvictionResult> {
33
+ const txHash = tx.getTxHash();
34
+ const nullifiers = tx.data.getNonEmptyNullifiers();
35
+ const txHashesToEvict: TxHash[] = [];
36
+ const incomingPriority = poolAccess.getTxPriority(tx);
37
+
38
+ for (const nullifier of nullifiers) {
39
+ const conflictingHash = await poolAccess.getTxHashByNullifier(nullifier);
40
+
41
+ if (
42
+ !conflictingHash ||
43
+ conflictingHash.equals(txHash) ||
44
+ findIndexInSortedArray(txHashesToEvict, conflictingHash, cmpTxHash) !== -1
45
+ ) {
46
+ continue;
47
+ }
48
+
49
+ // Get the conflicting tx's priority
50
+ const conflictingTx = await poolAccess.getPendingTxByHash(conflictingHash);
51
+ if (!conflictingTx) {
52
+ continue;
53
+ }
54
+
55
+ const conflictingPriority = poolAccess.getTxPriority(conflictingTx);
56
+
57
+ // If incoming tx has strictly higher priority, mark for eviction
58
+ // Otherwise, reject incoming tx (ties go to existing tx)
59
+ if (incomingPriority > conflictingPriority) {
60
+ insertIntoSortedArray(txHashesToEvict, conflictingHash, cmpTxHash);
61
+ } else {
62
+ this.log.debug(
63
+ `Rejecting tx ${txHash.toString()}: nullifier conflict with ${conflictingHash.toString()} which has higher or equal fee`,
64
+ );
65
+ return {
66
+ shouldReject: true,
67
+ txHashesToEvict: [],
68
+ reason: `nullifier conflict with ${conflictingHash.toString()}`,
69
+ };
70
+ }
71
+ }
72
+
73
+ return { shouldReject: false, txHashesToEvict };
74
+ }
75
+ }
@@ -1,33 +1,34 @@
1
1
  import type { EpochCacheInterface } from '@aztec/epoch-cache';
2
2
  import { NoCommitteeError } from '@aztec/ethereum/contracts';
3
3
  import { type Logger, createLogger } from '@aztec/foundation/log';
4
- import { type BlockAttestation, type P2PValidator, PeerErrorSeverity } from '@aztec/stdlib/p2p';
4
+ import { type CheckpointAttestation, type P2PValidator, PeerErrorSeverity } from '@aztec/stdlib/p2p';
5
5
 
6
- export class AttestationValidator implements P2PValidator<BlockAttestation> {
6
+ export class CheckpointAttestationValidator implements P2PValidator<CheckpointAttestation> {
7
7
  protected epochCache: EpochCacheInterface;
8
8
  protected logger: Logger;
9
9
 
10
10
  constructor(epochCache: EpochCacheInterface) {
11
11
  this.epochCache = epochCache;
12
- this.logger = createLogger('p2p:attestation-validator');
12
+ this.logger = createLogger('p2p:checkpoint-attestation-validator');
13
13
  }
14
14
 
15
- async validate(message: BlockAttestation): Promise<PeerErrorSeverity | undefined> {
15
+ async validate(message: CheckpointAttestation): Promise<PeerErrorSeverity | undefined> {
16
16
  const slotNumber = message.payload.header.slotNumber;
17
17
 
18
18
  try {
19
- const { currentProposer, nextProposer, currentSlot, nextSlot } =
20
- await this.epochCache.getProposerAttesterAddressInCurrentOrNextSlot();
19
+ const { currentSlot, nextSlot } = await this.epochCache.getProposerAttesterAddressInCurrentOrNextSlot();
21
20
 
22
21
  if (slotNumber !== currentSlot && slotNumber !== nextSlot) {
23
- this.logger.warn(`Attestation slot ${slotNumber} is not current (${currentSlot}) or next (${nextSlot}) slot`);
22
+ this.logger.warn(
23
+ `Checkpoint attestation slot ${slotNumber} is not current (${currentSlot}) or next (${nextSlot}) slot`,
24
+ );
24
25
  return PeerErrorSeverity.HighToleranceError;
25
26
  }
26
27
 
27
28
  // Verify the signature is valid
28
29
  const attester = message.getSender();
29
30
  if (attester === undefined) {
30
- this.logger.warn(`Invalid signature in attestation for slot ${slotNumber}`);
31
+ this.logger.warn(`Invalid signature in checkpoint attestation for slot ${slotNumber}`);
31
32
  return PeerErrorSeverity.LowToleranceError;
32
33
  }
33
34
 
@@ -37,20 +38,22 @@ export class AttestationValidator implements P2PValidator<BlockAttestation> {
37
38
  return PeerErrorSeverity.HighToleranceError;
38
39
  }
39
40
 
40
- // Verify the proposer signature matches the expected proposer for this slot
41
+ // Verify the proposer signature matches the expected proposer for the attestation's slot
42
+ // We look up the proposer for the specific slot rather than using currentSlot/nextSlot
43
+ // since timing differences could cause mismatches
41
44
  const proposer = message.getProposer();
42
- const expectedProposer = slotNumber === currentSlot ? currentProposer : nextProposer;
45
+ const expectedProposer = await this.epochCache.getProposerAttesterAddressInSlot(slotNumber);
43
46
  if (!expectedProposer) {
44
47
  this.logger.warn(`No proposer defined for slot ${slotNumber}`);
45
48
  return PeerErrorSeverity.HighToleranceError;
46
49
  }
47
50
  if (!proposer) {
48
- this.logger.warn(`Invalid proposer signature in attestation for slot ${slotNumber}`);
51
+ this.logger.warn(`Invalid proposer signature in checkpoint attestation for slot ${slotNumber}`);
49
52
  return PeerErrorSeverity.LowToleranceError;
50
53
  }
51
54
  if (!proposer.equals(expectedProposer)) {
52
55
  this.logger.warn(
53
- `Proposer signature mismatch in attestation. ` +
56
+ `Proposer signature mismatch in checkpoint attestation. ` +
54
57
  `Expected ${expectedProposer?.toString() ?? 'none'} but got ${proposer.toString()} for slot ${slotNumber}`,
55
58
  );
56
59
  return PeerErrorSeverity.HighToleranceError;
@@ -60,7 +63,7 @@ export class AttestationValidator implements P2PValidator<BlockAttestation> {
60
63
  } catch (e) {
61
64
  // People shouldn't be sending us attestations if the committee doesn't exist
62
65
  if (e instanceof NoCommitteeError) {
63
- this.logger.warn(`No committee exists for attestation for slot ${slotNumber}`);
66
+ this.logger.warn(`No committee exists for checkpoint attestation for slot ${slotNumber}`);
64
67
  return PeerErrorSeverity.LowToleranceError;
65
68
  }
66
69
  throw e;
@@ -1,9 +1,9 @@
1
1
  import type { EpochCacheInterface } from '@aztec/epoch-cache';
2
- import { type BlockAttestation, PeerErrorSeverity } from '@aztec/stdlib/p2p';
3
- import { Attributes, Metrics, type TelemetryClient, ValueType } from '@aztec/telemetry-client';
2
+ import { type CheckpointAttestation, PeerErrorSeverity } from '@aztec/stdlib/p2p';
3
+ import { Attributes, Metrics, type TelemetryClient } from '@aztec/telemetry-client';
4
4
 
5
5
  import type { AttestationPool } from '../../mem_pools/attestation_pool/attestation_pool.js';
6
- import { AttestationValidator } from './attestation_validator.js';
6
+ import { CheckpointAttestationValidator } from './attestation_validator.js';
7
7
 
8
8
  /**
9
9
  * FishermanAttestationValidator extends the base AttestationValidator to add
@@ -13,7 +13,7 @@ import { AttestationValidator } from './attestation_validator.js';
13
13
  * handled by LibP2PService based on the fishermanMode config to ensure a better
14
14
  * view of the network.
15
15
  */
16
- export class FishermanAttestationValidator extends AttestationValidator {
16
+ export class FishermanAttestationValidator extends CheckpointAttestationValidator {
17
17
  private invalidAttestationCounter;
18
18
 
19
19
  constructor(
@@ -25,13 +25,10 @@ export class FishermanAttestationValidator extends AttestationValidator {
25
25
  this.logger = this.logger.createChild('[FISHERMAN]');
26
26
 
27
27
  const meter = telemetryClient.getMeter('FishermanAttestationValidator');
28
- this.invalidAttestationCounter = meter.createUpDownCounter(Metrics.VALIDATOR_INVALID_ATTESTATION_RECEIVED_COUNT, {
29
- description: 'The number of invalid attestations received',
30
- valueType: ValueType.INT,
31
- });
28
+ this.invalidAttestationCounter = meter.createUpDownCounter(Metrics.VALIDATOR_INVALID_ATTESTATION_RECEIVED_COUNT);
32
29
  }
33
30
 
34
- override async validate(message: BlockAttestation): Promise<PeerErrorSeverity | undefined> {
31
+ override async validate(message: CheckpointAttestation): Promise<PeerErrorSeverity | undefined> {
35
32
  // First run the standard validation
36
33
  const baseValidationResult = await super.validate(message);
37
34
  if (baseValidationResult !== undefined) {
@@ -52,11 +49,11 @@ export class FishermanAttestationValidator extends AttestationValidator {
52
49
  }
53
50
 
54
51
  const proposalId = message.archive.toString();
55
- const proposal = await this.attestationPool.getBlockProposal(proposalId);
52
+ const proposal = await this.attestationPool.getCheckpointProposal(proposalId);
56
53
 
57
54
  if (proposal) {
58
55
  // Compare the attestation payload with the proposal payload
59
- if (!message.payload.equals(proposal.payload)) {
56
+ if (!message.payload.equals(proposal)) {
60
57
  this.logger.error(
61
58
  `Attestation payload mismatch for slot ${slotNumberBigInt}! ` +
62
59
  `Attester ${attester.toString()} signed different data than the proposal.`,
@@ -66,7 +63,7 @@ export class FishermanAttestationValidator extends AttestationValidator {
66
63
  proposer: proposer.toString(),
67
64
  proposalArchive: proposal.archive.toString(),
68
65
  attestationArchive: message.archive.toString(),
69
- proposalHeader: proposal.payload.header.hash().toString(),
66
+ proposalHeader: proposal.checkpointHeader.hash().toString(),
70
67
  attestationHeader: message.payload.header.hash().toString(),
71
68
  },
72
69
  );
@@ -1,3 +1,3 @@
1
1
  export * from './tx_validator/index.js';
2
- export * from './block_proposal_validator/index.js';
2
+ export * from './proposal_validator/index.js';
3
3
  export * from './attestation_validator/index.js';
@@ -0,0 +1,10 @@
1
+ import type { EpochCacheInterface } from '@aztec/epoch-cache';
2
+ import type { BlockProposal, P2PValidator } from '@aztec/stdlib/p2p';
3
+
4
+ import { ProposalValidator } from '../proposal_validator/proposal_validator.js';
5
+
6
+ export class BlockProposalValidator extends ProposalValidator<BlockProposal> implements P2PValidator<BlockProposal> {
7
+ constructor(epochCache: EpochCacheInterface, opts: { txsPermitted: boolean }) {
8
+ super(epochCache, opts, 'p2p:block_proposal_validator');
9
+ }
10
+ }
@@ -0,0 +1,13 @@
1
+ import type { EpochCacheInterface } from '@aztec/epoch-cache';
2
+ import type { CheckpointProposal, P2PValidator } from '@aztec/stdlib/p2p';
3
+
4
+ import { ProposalValidator } from '../proposal_validator/proposal_validator.js';
5
+
6
+ export class CheckpointProposalValidator
7
+ extends ProposalValidator<CheckpointProposal>
8
+ implements P2PValidator<CheckpointProposal>
9
+ {
10
+ constructor(epochCache: EpochCacheInterface, opts: { txsPermitted: boolean }) {
11
+ super(epochCache, opts, 'p2p:checkpoint_proposal_validator');
12
+ }
13
+ }
@@ -0,0 +1,3 @@
1
+ export * from './block_proposal_validator.js';
2
+ export * from './checkpoint_proposal_validator.js';
3
+ export * from './proposal_validator.js';
@@ -1,63 +1,60 @@
1
1
  import type { EpochCacheInterface } from '@aztec/epoch-cache';
2
2
  import { NoCommitteeError } from '@aztec/ethereum/contracts';
3
3
  import { type Logger, createLogger } from '@aztec/foundation/log';
4
- import { type BlockProposal, type P2PValidator, PeerErrorSeverity } from '@aztec/stdlib/p2p';
4
+ import { BlockProposal, CheckpointProposal, PeerErrorSeverity } from '@aztec/stdlib/p2p';
5
5
 
6
- export class BlockProposalValidator implements P2PValidator<BlockProposal> {
7
- private epochCache: EpochCacheInterface;
8
- private logger: Logger;
9
- private txsPermitted: boolean;
6
+ export abstract class ProposalValidator<TProposal extends BlockProposal | CheckpointProposal> {
7
+ protected epochCache: EpochCacheInterface;
8
+ protected logger: Logger;
9
+ protected txsPermitted: boolean;
10
10
 
11
- constructor(epochCache: EpochCacheInterface, opts: { txsPermitted: boolean }) {
11
+ constructor(epochCache: EpochCacheInterface, opts: { txsPermitted: boolean }, loggerName: string) {
12
12
  this.epochCache = epochCache;
13
13
  this.txsPermitted = opts.txsPermitted;
14
- this.logger = createLogger('p2p:block_proposal_validator');
14
+ this.logger = createLogger(loggerName);
15
15
  }
16
16
 
17
- async validate(block: BlockProposal): Promise<PeerErrorSeverity | undefined> {
17
+ public async validate(proposal: TProposal): Promise<PeerErrorSeverity | undefined> {
18
18
  try {
19
- // Check signature validity first - invalid signatures are a high-severity issue
20
- const proposer = block.getSender();
19
+ // Signature validity
20
+ const proposer = proposal.getSender();
21
21
  if (!proposer) {
22
- this.logger.debug(`Penalizing peer for block proposal with invalid signature`);
22
+ this.logger.debug(`Penalizing peer for proposal with invalid signature`);
23
23
  return PeerErrorSeverity.MidToleranceError;
24
24
  }
25
25
 
26
- // Check if transactions are permitted when the proposal contains transaction hashes
27
- const embeddedTxCount = block.txs?.length ?? 0;
28
- if (!this.txsPermitted && (block.txHashes.length > 0 || embeddedTxCount > 0)) {
26
+ // Transactions permitted check
27
+ const embeddedTxCount = proposal.txs?.length ?? 0;
28
+ if (!this.txsPermitted && (proposal.txHashes.length > 0 || embeddedTxCount > 0)) {
29
29
  this.logger.debug(
30
- `Penalizing peer for block proposal with ${block.txHashes.length} transaction(s) when transactions are not permitted`,
30
+ `Penalizing peer for proposal with ${proposal.txHashes.length} transaction(s) when transactions are not permitted`,
31
31
  );
32
32
  return PeerErrorSeverity.MidToleranceError;
33
33
  }
34
34
 
35
- // If there are embedded txs, they must be listed in txHashes; if there are no txHashes, there must be no txs
36
- const hashSet = new Set(block.txHashes.map(h => h.toString()));
35
+ // Embedded txs must be listed in txHashes
36
+ const hashSet = new Set(proposal.txHashes.map(h => h.toString()));
37
37
  const missingTxHashes =
38
38
  embeddedTxCount > 0
39
- ? block.txs!.filter(tx => !hashSet.has(tx.getTxHash().toString())).map(tx => tx.getTxHash().toString())
39
+ ? proposal.txs!.filter(tx => !hashSet.has(tx.getTxHash().toString())).map(tx => tx.getTxHash().toString())
40
40
  : [];
41
41
  if (embeddedTxCount > 0 && missingTxHashes.length > 0) {
42
42
  this.logger.warn('Penalizing peer for embedded transaction(s) not included in txHashes', {
43
43
  embeddedTxCount,
44
- txHashesLength: block.txHashes.length,
44
+ txHashesLength: proposal.txHashes.length,
45
45
  missingTxHashes,
46
46
  });
47
47
  return PeerErrorSeverity.MidToleranceError;
48
48
  }
49
49
 
50
+ // Slot and proposer checks
50
51
  const { currentProposer, nextProposer, currentSlot, nextSlot } =
51
52
  await this.epochCache.getProposerAttesterAddressInCurrentOrNextSlot();
52
-
53
- // Check that the attestation is for the current or next slot
54
- const slotNumber = block.payload.header.slotNumber;
53
+ const slotNumber = proposal.slotNumber;
55
54
  if (slotNumber !== currentSlot && slotNumber !== nextSlot) {
56
55
  this.logger.debug(`Penalizing peer for invalid slot number ${slotNumber}`, { currentSlot, nextSlot });
57
56
  return PeerErrorSeverity.HighToleranceError;
58
57
  }
59
-
60
- // Check that the block proposal is from the current or next proposer
61
58
  if (slotNumber === currentSlot && currentProposer !== undefined && !proposer.equals(currentProposer)) {
62
59
  this.logger.debug(`Penalizing peer for invalid proposer for current slot ${slotNumber}`, {
63
60
  currentProposer,
@@ -66,7 +63,6 @@ export class BlockProposalValidator implements P2PValidator<BlockProposal> {
66
63
  });
67
64
  return PeerErrorSeverity.MidToleranceError;
68
65
  }
69
-
70
66
  if (slotNumber === nextSlot && nextProposer !== undefined && !proposer.equals(nextProposer)) {
71
67
  this.logger.debug(`Penalizing peer for invalid proposer for next slot ${slotNumber}`, {
72
68
  currentProposer,
@@ -77,8 +73,8 @@ export class BlockProposalValidator implements P2PValidator<BlockProposal> {
77
73
  }
78
74
 
79
75
  // Validate tx hashes for all txs embedded in the proposal
80
- if (!(await Promise.all(block.txs?.map(tx => tx.validateTxHash()) ?? [])).every(v => v)) {
81
- this.logger.warn(`Penalizing peer for invalid tx hashes in block proposal`, {
76
+ if (!(await Promise.all(proposal.txs?.map(tx => tx.validateTxHash()) ?? [])).every(v => v)) {
77
+ this.logger.warn(`Penalizing peer for invalid tx hashes in proposal`, {
82
78
  proposer,
83
79
  slotNumber,
84
80
  });
@@ -87,7 +83,6 @@ export class BlockProposalValidator implements P2PValidator<BlockProposal> {
87
83
 
88
84
  return undefined;
89
85
  } catch (e) {
90
- // People shouldn't be sending us block proposals if the committee doesn't exist
91
86
  if (e instanceof NoCommitteeError) {
92
87
  return PeerErrorSeverity.LowToleranceError;
93
88
  }