@aztec/p2p 0.0.1-commit.87a0206 → 0.0.1-commit.8f9871590

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 (213) 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 +42 -8
  4. package/dest/client/interface.d.ts +30 -15
  5. package/dest/client/interface.d.ts.map +1 -1
  6. package/dest/client/p2p_client.d.ts +33 -35
  7. package/dest/client/p2p_client.d.ts.map +1 -1
  8. package/dest/client/p2p_client.js +107 -137
  9. package/dest/config.d.ts +22 -4
  10. package/dest/config.d.ts.map +1 -1
  11. package/dest/config.js +16 -1
  12. package/dest/index.d.ts +2 -1
  13. package/dest/index.d.ts.map +1 -1
  14. package/dest/index.js +1 -0
  15. package/dest/mem_pools/attestation_pool/attestation_pool.d.ts +21 -12
  16. package/dest/mem_pools/attestation_pool/attestation_pool.d.ts.map +1 -1
  17. package/dest/mem_pools/attestation_pool/attestation_pool.js +67 -37
  18. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.d.ts +1 -1
  19. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.d.ts.map +1 -1
  20. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.js +53 -53
  21. package/dest/mem_pools/attestation_pool/index.d.ts +2 -2
  22. package/dest/mem_pools/attestation_pool/index.d.ts.map +1 -1
  23. package/dest/mem_pools/attestation_pool/index.js +1 -1
  24. package/dest/mem_pools/attestation_pool/mocks.d.ts +2 -2
  25. package/dest/mem_pools/attestation_pool/mocks.d.ts.map +1 -1
  26. package/dest/mem_pools/attestation_pool/mocks.js +2 -2
  27. package/dest/mem_pools/index.d.ts +2 -1
  28. package/dest/mem_pools/index.d.ts.map +1 -1
  29. package/dest/mem_pools/interface.d.ts +3 -3
  30. package/dest/mem_pools/interface.d.ts.map +1 -1
  31. package/dest/mem_pools/tx_pool/eviction/invalid_txs_after_mining_rule.js +3 -3
  32. package/dest/mem_pools/tx_pool_v2/deleted_pool.d.ts +102 -0
  33. package/dest/mem_pools/tx_pool_v2/deleted_pool.d.ts.map +1 -0
  34. package/dest/mem_pools/tx_pool_v2/deleted_pool.js +242 -0
  35. package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_eviction_rule.d.ts +1 -1
  36. package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_eviction_rule.d.ts.map +1 -1
  37. package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_eviction_rule.js +3 -0
  38. package/dest/mem_pools/tx_pool_v2/eviction/invalid_txs_after_mining_rule.js +5 -3
  39. package/dest/mem_pools/tx_pool_v2/eviction/invalid_txs_after_reorg_rule.js +3 -1
  40. package/dest/mem_pools/tx_pool_v2/eviction/low_priority_eviction_rule.d.ts +1 -1
  41. package/dest/mem_pools/tx_pool_v2/eviction/low_priority_eviction_rule.d.ts.map +1 -1
  42. package/dest/mem_pools/tx_pool_v2/eviction/low_priority_eviction_rule.js +3 -1
  43. package/dest/mem_pools/tx_pool_v2/index.d.ts +2 -1
  44. package/dest/mem_pools/tx_pool_v2/index.d.ts.map +1 -1
  45. package/dest/mem_pools/tx_pool_v2/index.js +1 -0
  46. package/dest/mem_pools/tx_pool_v2/interfaces.d.ts +7 -3
  47. package/dest/mem_pools/tx_pool_v2/interfaces.d.ts.map +1 -1
  48. package/dest/mem_pools/tx_pool_v2/interfaces.js +2 -1
  49. package/dest/mem_pools/tx_pool_v2/tx_metadata.d.ts +28 -4
  50. package/dest/mem_pools/tx_pool_v2/tx_metadata.d.ts.map +1 -1
  51. package/dest/mem_pools/tx_pool_v2/tx_metadata.js +37 -4
  52. package/dest/mem_pools/tx_pool_v2/tx_pool_indices.d.ts +8 -2
  53. package/dest/mem_pools/tx_pool_v2/tx_pool_indices.d.ts.map +1 -1
  54. package/dest/mem_pools/tx_pool_v2/tx_pool_indices.js +15 -2
  55. package/dest/mem_pools/tx_pool_v2/tx_pool_v2.d.ts +4 -2
  56. package/dest/mem_pools/tx_pool_v2/tx_pool_v2.d.ts.map +1 -1
  57. package/dest/mem_pools/tx_pool_v2/tx_pool_v2.js +6 -2
  58. package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.d.ts +4 -2
  59. package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.d.ts.map +1 -1
  60. package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.js +110 -78
  61. package/dest/msg_validators/tx_validator/aggregate_tx_validator.d.ts +3 -3
  62. package/dest/msg_validators/tx_validator/aggregate_tx_validator.d.ts.map +1 -1
  63. package/dest/msg_validators/tx_validator/block_header_validator.d.ts +16 -3
  64. package/dest/msg_validators/tx_validator/block_header_validator.d.ts.map +1 -1
  65. package/dest/msg_validators/tx_validator/block_header_validator.js +1 -1
  66. package/dest/msg_validators/tx_validator/double_spend_validator.d.ts +13 -3
  67. package/dest/msg_validators/tx_validator/double_spend_validator.d.ts.map +1 -1
  68. package/dest/msg_validators/tx_validator/double_spend_validator.js +4 -4
  69. package/dest/msg_validators/tx_validator/timestamp_validator.d.ts +20 -4
  70. package/dest/msg_validators/tx_validator/timestamp_validator.d.ts.map +1 -1
  71. package/dest/msg_validators/tx_validator/timestamp_validator.js +6 -6
  72. package/dest/services/dummy_service.d.ts +6 -2
  73. package/dest/services/dummy_service.d.ts.map +1 -1
  74. package/dest/services/dummy_service.js +3 -0
  75. package/dest/services/encoding.d.ts +2 -2
  76. package/dest/services/encoding.d.ts.map +1 -1
  77. package/dest/services/encoding.js +2 -2
  78. package/dest/services/gossipsub/index.d.ts +3 -0
  79. package/dest/services/gossipsub/index.d.ts.map +1 -0
  80. package/dest/services/gossipsub/index.js +2 -0
  81. package/dest/services/gossipsub/scoring.d.ts +21 -3
  82. package/dest/services/gossipsub/scoring.d.ts.map +1 -1
  83. package/dest/services/gossipsub/scoring.js +24 -7
  84. package/dest/services/gossipsub/topic_score_params.d.ts +161 -0
  85. package/dest/services/gossipsub/topic_score_params.d.ts.map +1 -0
  86. package/dest/services/gossipsub/topic_score_params.js +324 -0
  87. package/dest/services/libp2p/libp2p_service.d.ts +11 -3
  88. package/dest/services/libp2p/libp2p_service.d.ts.map +1 -1
  89. package/dest/services/libp2p/libp2p_service.js +91 -67
  90. package/dest/services/peer-manager/peer_scoring.d.ts +1 -1
  91. package/dest/services/peer-manager/peer_scoring.d.ts.map +1 -1
  92. package/dest/services/peer-manager/peer_scoring.js +25 -2
  93. package/dest/services/reqresp/interface.d.ts +10 -1
  94. package/dest/services/reqresp/interface.d.ts.map +1 -1
  95. package/dest/services/reqresp/interface.js +15 -1
  96. package/dest/services/reqresp/protocols/block_txs/block_txs_handler.d.ts +3 -3
  97. package/dest/services/reqresp/protocols/block_txs/block_txs_handler.d.ts.map +1 -1
  98. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.d.ts +7 -1
  99. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.d.ts.map +1 -1
  100. package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.js +15 -0
  101. package/dest/services/reqresp/protocols/tx.d.ts +7 -1
  102. package/dest/services/reqresp/protocols/tx.d.ts.map +1 -1
  103. package/dest/services/reqresp/protocols/tx.js +20 -0
  104. package/dest/services/reqresp/reqresp.d.ts +1 -1
  105. package/dest/services/reqresp/reqresp.d.ts.map +1 -1
  106. package/dest/services/reqresp/reqresp.js +11 -4
  107. package/dest/services/service.d.ts +18 -1
  108. package/dest/services/service.d.ts.map +1 -1
  109. package/dest/services/tx_collection/config.d.ts +19 -1
  110. package/dest/services/tx_collection/config.d.ts.map +1 -1
  111. package/dest/services/tx_collection/config.js +46 -0
  112. package/dest/services/tx_collection/fast_tx_collection.d.ts +3 -1
  113. package/dest/services/tx_collection/fast_tx_collection.d.ts.map +1 -1
  114. package/dest/services/tx_collection/fast_tx_collection.js +17 -3
  115. package/dest/services/tx_collection/file_store_tx_collection.d.ts +53 -0
  116. package/dest/services/tx_collection/file_store_tx_collection.d.ts.map +1 -0
  117. package/dest/services/tx_collection/file_store_tx_collection.js +165 -0
  118. package/dest/services/tx_collection/file_store_tx_source.d.ts +28 -0
  119. package/dest/services/tx_collection/file_store_tx_source.d.ts.map +1 -0
  120. package/dest/services/tx_collection/file_store_tx_source.js +59 -0
  121. package/dest/services/tx_collection/index.d.ts +2 -1
  122. package/dest/services/tx_collection/index.d.ts.map +1 -1
  123. package/dest/services/tx_collection/index.js +1 -0
  124. package/dest/services/tx_collection/slow_tx_collection.d.ts +6 -2
  125. package/dest/services/tx_collection/slow_tx_collection.d.ts.map +1 -1
  126. package/dest/services/tx_collection/slow_tx_collection.js +55 -23
  127. package/dest/services/tx_collection/tx_collection.d.ts +19 -7
  128. package/dest/services/tx_collection/tx_collection.d.ts.map +1 -1
  129. package/dest/services/tx_collection/tx_collection.js +75 -3
  130. package/dest/services/tx_collection/tx_collection_sink.d.ts +15 -6
  131. package/dest/services/tx_collection/tx_collection_sink.d.ts.map +1 -1
  132. package/dest/services/tx_collection/tx_collection_sink.js +13 -7
  133. package/dest/services/tx_file_store/config.d.ts +1 -3
  134. package/dest/services/tx_file_store/config.d.ts.map +1 -1
  135. package/dest/services/tx_file_store/config.js +0 -4
  136. package/dest/services/tx_file_store/tx_file_store.d.ts +4 -3
  137. package/dest/services/tx_file_store/tx_file_store.d.ts.map +1 -1
  138. package/dest/services/tx_file_store/tx_file_store.js +8 -5
  139. package/dest/services/tx_provider.d.ts +3 -3
  140. package/dest/services/tx_provider.d.ts.map +1 -1
  141. package/dest/services/tx_provider.js +5 -4
  142. package/dest/test-helpers/make-test-p2p-clients.d.ts +3 -3
  143. package/dest/test-helpers/make-test-p2p-clients.d.ts.map +1 -1
  144. package/dest/test-helpers/mock-pubsub.d.ts +27 -1
  145. package/dest/test-helpers/mock-pubsub.d.ts.map +1 -1
  146. package/dest/test-helpers/mock-pubsub.js +97 -2
  147. package/dest/test-helpers/reqresp-nodes.d.ts +1 -1
  148. package/dest/test-helpers/reqresp-nodes.d.ts.map +1 -1
  149. package/dest/test-helpers/reqresp-nodes.js +2 -1
  150. package/dest/test-helpers/testbench-utils.d.ts +32 -24
  151. package/dest/test-helpers/testbench-utils.d.ts.map +1 -1
  152. package/dest/test-helpers/testbench-utils.js +103 -36
  153. package/dest/testbench/p2p_client_testbench_worker.js +3 -3
  154. package/package.json +14 -14
  155. package/src/client/factory.ts +78 -9
  156. package/src/client/interface.ts +32 -13
  157. package/src/client/p2p_client.ts +135 -157
  158. package/src/config.ts +34 -2
  159. package/src/index.ts +1 -0
  160. package/src/mem_pools/attestation_pool/attestation_pool.ts +95 -44
  161. package/src/mem_pools/attestation_pool/attestation_pool_test_suite.ts +57 -53
  162. package/src/mem_pools/attestation_pool/index.ts +3 -3
  163. package/src/mem_pools/attestation_pool/mocks.ts +2 -1
  164. package/src/mem_pools/index.ts +3 -0
  165. package/src/mem_pools/interface.ts +2 -2
  166. package/src/mem_pools/tx_pool/README.md +1 -1
  167. package/src/mem_pools/tx_pool/eviction/invalid_txs_after_mining_rule.ts +3 -3
  168. package/src/mem_pools/tx_pool_v2/README.md +76 -10
  169. package/src/mem_pools/tx_pool_v2/deleted_pool.ts +310 -0
  170. package/src/mem_pools/tx_pool_v2/eviction/fee_payer_balance_eviction_rule.ts +3 -0
  171. package/src/mem_pools/tx_pool_v2/eviction/invalid_txs_after_mining_rule.ts +3 -3
  172. package/src/mem_pools/tx_pool_v2/eviction/invalid_txs_after_reorg_rule.ts +1 -1
  173. package/src/mem_pools/tx_pool_v2/eviction/low_priority_eviction_rule.ts +3 -1
  174. package/src/mem_pools/tx_pool_v2/index.ts +1 -0
  175. package/src/mem_pools/tx_pool_v2/interfaces.ts +8 -2
  176. package/src/mem_pools/tx_pool_v2/tx_metadata.ts +57 -7
  177. package/src/mem_pools/tx_pool_v2/tx_pool_indices.ts +18 -2
  178. package/src/mem_pools/tx_pool_v2/tx_pool_v2.ts +7 -1
  179. package/src/mem_pools/tx_pool_v2/tx_pool_v2_impl.ts +122 -80
  180. package/src/msg_validators/tx_validator/aggregate_tx_validator.ts +2 -2
  181. package/src/msg_validators/tx_validator/block_header_validator.ts +15 -3
  182. package/src/msg_validators/tx_validator/double_spend_validator.ts +11 -6
  183. package/src/msg_validators/tx_validator/timestamp_validator.ts +23 -18
  184. package/src/services/dummy_service.ts +6 -0
  185. package/src/services/encoding.ts +2 -2
  186. package/src/services/gossipsub/README.md +626 -0
  187. package/src/services/gossipsub/index.ts +2 -0
  188. package/src/services/gossipsub/scoring.ts +29 -5
  189. package/src/services/gossipsub/topic_score_params.ts +451 -0
  190. package/src/services/libp2p/libp2p_service.ts +91 -70
  191. package/src/services/peer-manager/peer_scoring.ts +25 -0
  192. package/src/services/reqresp/interface.ts +26 -1
  193. package/src/services/reqresp/protocols/block_txs/block_txs_handler.ts +2 -2
  194. package/src/services/reqresp/protocols/block_txs/block_txs_reqresp.ts +17 -0
  195. package/src/services/reqresp/protocols/tx.ts +22 -0
  196. package/src/services/reqresp/reqresp.ts +13 -3
  197. package/src/services/service.ts +20 -0
  198. package/src/services/tx_collection/config.ts +68 -0
  199. package/src/services/tx_collection/fast_tx_collection.ts +14 -2
  200. package/src/services/tx_collection/file_store_tx_collection.ts +198 -0
  201. package/src/services/tx_collection/file_store_tx_source.ts +73 -0
  202. package/src/services/tx_collection/index.ts +1 -0
  203. package/src/services/tx_collection/slow_tx_collection.ts +64 -30
  204. package/src/services/tx_collection/tx_collection.ts +109 -13
  205. package/src/services/tx_collection/tx_collection_sink.ts +17 -7
  206. package/src/services/tx_file_store/config.ts +0 -6
  207. package/src/services/tx_file_store/tx_file_store.ts +9 -7
  208. package/src/services/tx_provider.ts +8 -7
  209. package/src/test-helpers/make-test-p2p-clients.ts +3 -3
  210. package/src/test-helpers/mock-pubsub.ts +133 -3
  211. package/src/test-helpers/reqresp-nodes.ts +2 -1
  212. package/src/test-helpers/testbench-utils.ts +117 -40
  213. package/src/testbench/p2p_client_testbench_worker.ts +3 -3
package/src/config.ts CHANGED
@@ -13,7 +13,13 @@ import { Fr } from '@aztec/foundation/curves/bn254';
13
13
  import { type DataStoreConfig, dataConfigMappings } from '@aztec/kv-store/config';
14
14
  import { FunctionSelector } from '@aztec/stdlib/abi/function-selector';
15
15
  import { AztecAddress } from '@aztec/stdlib/aztec-address';
16
- import { type AllowedElement, type ChainConfig, chainConfigMappings } from '@aztec/stdlib/config';
16
+ import {
17
+ type AllowedElement,
18
+ type ChainConfig,
19
+ type SequencerConfig,
20
+ chainConfigMappings,
21
+ sharedSequencerConfigMappings,
22
+ } from '@aztec/stdlib/config';
17
23
 
18
24
  import {
19
25
  type BatchTxRequesterConfig,
@@ -31,13 +37,17 @@ export interface P2PConfig
31
37
  BatchTxRequesterConfig,
32
38
  ChainConfig,
33
39
  TxCollectionConfig,
34
- TxFileStoreConfig {
40
+ TxFileStoreConfig,
41
+ Pick<SequencerConfig, 'blockDurationMs'> {
35
42
  /** A flag dictating whether the P2P subsystem should be enabled. */
36
43
  p2pEnabled: boolean;
37
44
 
38
45
  /** The frequency in which to check for new L2 blocks. */
39
46
  blockCheckIntervalMS: number;
40
47
 
48
+ /** The frequency in which to check for new L2 slots. */
49
+ slotCheckIntervalMS: number;
50
+
41
51
  /** The number of blocks to fetch in a single batch. */
42
52
  blockRequestBatchSize: number;
43
53
 
@@ -177,6 +187,12 @@ export interface P2PConfig
177
187
 
178
188
  /** Whether to run in fisherman mode: validates all proposals and attestations but does not broadcast attestations or participate in consensus */
179
189
  fishermanMode: boolean;
190
+
191
+ /** Broadcast block proposals even when a conflicting proposal for the same slot already exists in the pool (for testing purposes only). */
192
+ broadcastEquivocatedProposals?: boolean;
193
+
194
+ /** Minimum age (ms) a transaction must have been in the pool before it's eligible for block building. */
195
+ minTxPoolAgeMs: number;
180
196
  }
181
197
 
182
198
  export const DEFAULT_P2P_PORT = 40400;
@@ -197,6 +213,11 @@ export const p2pConfigMappings: ConfigMappingsType<P2PConfig> = {
197
213
  description: 'The frequency in which to check for new L2 blocks.',
198
214
  ...numberConfigHelper(100),
199
215
  },
216
+ slotCheckIntervalMS: {
217
+ env: 'P2P_SLOT_CHECK_INTERVAL_MS',
218
+ description: 'The frequency in which to check for new L2 slots.',
219
+ ...numberConfigHelper(1000),
220
+ },
200
221
  debugDisableColocationPenalty: {
201
222
  env: 'DEBUG_P2P_DISABLE_COLOCATION_PENALTY',
202
223
  description: 'DEBUG: Disable colocation penalty - NEVER set to true in production',
@@ -441,6 +462,17 @@ export const p2pConfigMappings: ConfigMappingsType<P2PConfig> = {
441
462
  'Whether to run in fisherman mode: validates all proposals and attestations but does not broadcast attestations or participate in consensus.',
442
463
  ...booleanConfigHelper(false),
443
464
  },
465
+ broadcastEquivocatedProposals: {
466
+ description:
467
+ 'Broadcast block proposals even when a conflicting proposal for the same slot already exists in the pool (for testing purposes only).',
468
+ ...booleanConfigHelper(false),
469
+ },
470
+ minTxPoolAgeMs: {
471
+ env: 'P2P_MIN_TX_POOL_AGE_MS',
472
+ description: 'Minimum age (ms) a transaction must have been in the pool before it is eligible for block building.',
473
+ ...numberConfigHelper(2_000),
474
+ },
475
+ ...sharedSequencerConfigMappings,
444
476
  ...p2pReqRespConfigMappings,
445
477
  ...batchTxRequesterConfigMappings,
446
478
  ...chainConfigMappings,
package/src/index.ts CHANGED
@@ -7,5 +7,6 @@ export * from './enr/index.js';
7
7
  export * from './config.js';
8
8
  export * from './mem_pools/attestation_pool/index.js';
9
9
  export * from './mem_pools/tx_pool/index.js';
10
+ export * from './mem_pools/tx_pool_v2/index.js';
10
11
  export * from './msg_validators/index.js';
11
12
  export * from './services/index.js';
@@ -19,13 +19,17 @@ export type TryAddResult = {
19
19
  added: boolean;
20
20
  /** Whether the exact item already existed */
21
21
  alreadyExists: boolean;
22
- /** Total items for this position - used for duplicate detection */
23
- totalForPosition: number;
22
+ /** Count of items for the position. Meaning varies by method:
23
+ * - tryAddBlockProposal: proposals at (slot, indexWithinCheckpoint)
24
+ * - tryAddCheckpointProposal: proposals at slot
25
+ * - tryAddCheckpointAttestation: attestations by this signer for this slot */
26
+ count: number;
24
27
  };
25
28
 
26
- export const MAX_PROPOSALS_PER_SLOT = 5;
27
- export const MAX_PROPOSALS_PER_POSITION = 3;
28
- export const ATTESTATION_CAP_BUFFER = 10;
29
+ export const MAX_CHECKPOINT_PROPOSALS_PER_SLOT = 5;
30
+ export const MAX_BLOCK_PROPOSALS_PER_POSITION = 3;
31
+ /** Maximum attestations a single signer can make per slot before being rejected. */
32
+ export const MAX_ATTESTATIONS_PER_SLOT_AND_SIGNER = 3;
29
33
 
30
34
  /** Public API interface for attestation pools. Used for typing mocks and test implementations. */
31
35
  export type AttestationPoolApi = Pick<
@@ -39,6 +43,7 @@ export type AttestationPoolApi = Pick<
39
43
  | 'deleteOlderThan'
40
44
  | 'getCheckpointAttestationsForSlot'
41
45
  | 'getCheckpointAttestationsForSlotAndProposal'
46
+ | 'hasBlockProposalsForSlot'
42
47
  | 'isEmpty'
43
48
  >;
44
49
 
@@ -69,6 +74,10 @@ export class AttestationPool {
69
74
  // Key: (slot << 10) | indexWithinCheckpoint, Value: archive string
70
75
  private blockProposalsForSlotAndIndex: AztecAsyncMultiMap<number, string>;
71
76
 
77
+ // Checkpoint attestations indexed by (slot, signer) for tracking attestations per (slot, signer) for duplicate detection
78
+ // Key: `${Fr(slot).toString()}-${signerAddress}` string (padded for lexicographic ordering), Value: `proposalId` strings
79
+ private checkpointAttestationsPerSlotAndSigner: AztecAsyncMultiMap<string, string>;
80
+
72
81
  constructor(
73
82
  private store: AztecAsyncKVStore,
74
83
  telemetry: TelemetryClient = getTelemetryClient(),
@@ -80,6 +89,7 @@ export class AttestationPool {
80
89
 
81
90
  // Initialize checkpoint attestations storage
82
91
  this.checkpointAttestations = store.openMap('checkpoint_attestations');
92
+ this.checkpointAttestationsPerSlotAndSigner = store.openMultiMap('checkpoint_attestations_per_slot_and_signer');
83
93
 
84
94
  // Initialize checkpoint proposal storage
85
95
  this.checkpointProposals = store.openMap('checkpoint_proposals');
@@ -133,6 +143,12 @@ export class AttestationPool {
133
143
  return { start: `${proposalKey}-`, end: `${proposalKey}-Z` };
134
144
  }
135
145
 
146
+ /** Creates a key for the per-signer-per-slot attestation index. Uses padded slot for lexicographic ordering. */
147
+ private getSlotSignerKey(slot: SlotNumber, signerAddress: string): string {
148
+ const slotStr = new Fr(slot).toString();
149
+ return `${slotStr}-${signerAddress}`;
150
+ }
151
+
136
152
  /** Number of bits reserved for indexWithinCheckpoint in position keys. */
137
153
  private static readonly INDEX_BITS = 10;
138
154
  /** Maximum indexWithinCheckpoint value (2^10 - 1 = 1023). */
@@ -166,21 +182,21 @@ export class AttestationPool {
166
182
  // Check if already exists
167
183
  const alreadyExists = await this.blockProposals.hasAsync(proposalId);
168
184
  if (alreadyExists) {
169
- const totalForPosition = await this.getBlockProposalCountForPosition(
185
+ const count = await this.getBlockProposalCountForPosition(
170
186
  blockProposal.slotNumber,
171
187
  blockProposal.indexWithinCheckpoint,
172
188
  );
173
- return { added: false, alreadyExists: true, totalForPosition };
189
+ return { added: false, alreadyExists: true, count };
174
190
  }
175
191
 
176
192
  // Get current count for position and check cap, do not add if exceeded
177
- const totalForPosition = await this.getBlockProposalCountForPosition(
193
+ const count = await this.getBlockProposalCountForPosition(
178
194
  blockProposal.slotNumber,
179
195
  blockProposal.indexWithinCheckpoint,
180
196
  );
181
197
 
182
- if (totalForPosition >= MAX_PROPOSALS_PER_POSITION) {
183
- return { added: false, alreadyExists: false, totalForPosition };
198
+ if (count >= MAX_BLOCK_PROPOSALS_PER_POSITION) {
199
+ return { added: false, alreadyExists: false, count };
184
200
  }
185
201
 
186
202
  // Add the proposal
@@ -195,7 +211,7 @@ export class AttestationPool {
195
211
  },
196
212
  );
197
213
 
198
- return { added: true, alreadyExists: false, totalForPosition: totalForPosition + 1 };
214
+ return { added: true, alreadyExists: false, count: count + 1 };
199
215
  });
200
216
  }
201
217
 
@@ -239,6 +255,13 @@ export class AttestationPool {
239
255
  return undefined;
240
256
  }
241
257
 
258
+ /** Checks if any block proposals exist for a given slot (at index 0). */
259
+ public async hasBlockProposalsForSlot(slot: SlotNumber): Promise<boolean> {
260
+ const positionKey = this.getBlockPositionKey(slot, 0);
261
+ const count = await this.blockProposalsForSlotAndIndex.getValueCountAsync(positionKey);
262
+ return count > 0;
263
+ }
264
+
242
265
  /**
243
266
  * Attempts to add a checkpoint proposal to the pool.
244
267
  *
@@ -261,14 +284,14 @@ export class AttestationPool {
261
284
  // Check if already exists
262
285
  const alreadyExists = await this.checkpointProposals.hasAsync(proposalId);
263
286
  if (alreadyExists) {
264
- const totalForPosition = await this.checkpointProposalsForSlot.getValueCountAsync(proposal.slotNumber);
265
- return { added: false, alreadyExists: true, totalForPosition };
287
+ const count = await this.checkpointProposalsForSlot.getValueCountAsync(proposal.slotNumber);
288
+ return { added: false, alreadyExists: true, count };
266
289
  }
267
290
 
268
291
  // Get current count for slot and check cap
269
- const totalForPosition = await this.checkpointProposalsForSlot.getValueCountAsync(proposal.slotNumber);
270
- if (totalForPosition >= MAX_PROPOSALS_PER_SLOT) {
271
- return { added: false, alreadyExists: false, totalForPosition };
292
+ const count = await this.checkpointProposalsForSlot.getValueCountAsync(proposal.slotNumber);
293
+ if (count >= MAX_CHECKPOINT_PROPOSALS_PER_SLOT) {
294
+ return { added: false, alreadyExists: false, count };
272
295
  }
273
296
 
274
297
  // Add the proposal if cap not exceeded
@@ -279,7 +302,7 @@ export class AttestationPool {
279
302
  slotNumber: proposal.slotNumber,
280
303
  });
281
304
 
282
- return { added: true, alreadyExists: false, totalForPosition: totalForPosition + 1 };
305
+ return { added: true, alreadyExists: false, count: count + 1 };
283
306
  });
284
307
  }
285
308
 
@@ -409,6 +432,14 @@ export class AttestationPool {
409
432
  numberOfAttestations++;
410
433
  }
411
434
 
435
+ // Clean up per-signer-per-slot index. Keys are formatted as `${Fr(slot).toString()}-${signerAddress}`.
436
+ // Since Fr pads to fixed-width hex, Fr(oldestSlot) is lexicographically greater than any key with
437
+ // a smaller slot (even with the signer suffix), so using it as the exclusive end bound is correct.
438
+ const slotSignerEndKey = new Fr(oldestSlot).toString();
439
+ for await (const key of this.checkpointAttestationsPerSlotAndSigner.keysAsync({ end: slotSignerEndKey })) {
440
+ await this.checkpointAttestationsPerSlotAndSigner.delete(key);
441
+ }
442
+
412
443
  // Delete checkpoint proposals for slots < oldestSlot, using checkpointProposalsForSlot as index
413
444
  for await (const slot of this.checkpointProposalsForSlot.keysAsync({ end: oldestSlot })) {
414
445
  const proposalIds = await toArray(this.checkpointProposalsForSlot.getValuesAsync(slot));
@@ -445,61 +476,81 @@ export class AttestationPool {
445
476
  *
446
477
  * This method performs validation and addition in a single call:
447
478
  * - Checks if the attestation already exists (returns alreadyExists: true if so)
448
- * - Checks if the (slot, proposalId) has reached the attestation cap (returns added: false if so)
479
+ * - Checks if this signer has reached the per-signer attestation cap for this slot
449
480
  * - Adds the attestation if validation passes
450
481
  *
451
482
  * @param attestation - The checkpoint attestation to add
452
- * @param committeeSize - Committee size for the attestation's slot
453
- * @returns Result indicating whether the attestation was added and existence info
483
+ * @returns Result indicating whether the attestation was added, existence info, and count of
484
+ * attestations by this signer for this slot (for equivocation detection)
454
485
  */
455
- public async tryAddCheckpointAttestation(
456
- attestation: CheckpointAttestation,
457
- committeeSize: number,
458
- ): Promise<TryAddResult> {
486
+ public async tryAddCheckpointAttestation(attestation: CheckpointAttestation): Promise<TryAddResult> {
459
487
  const slotNumber = attestation.payload.header.slotNumber;
460
488
  const proposalId = attestation.archive.toString();
461
489
  const sender = attestation.getSender();
462
490
 
463
491
  if (!sender) {
464
- return { added: false, alreadyExists: false, totalForPosition: 0 };
492
+ return { added: false, alreadyExists: false, count: 0 };
465
493
  }
466
494
 
495
+ const signerAddress = sender.toString();
496
+
467
497
  return await this.store.transactionAsync(async () => {
468
- const key = this.getAttestationKey(slotNumber, proposalId, sender.toString());
498
+ const key = this.getAttestationKey(slotNumber, proposalId, signerAddress);
469
499
  const alreadyExists = await this.checkpointAttestations.hasAsync(key);
470
500
 
501
+ // Get count of attestations by this signer for this slot (for duplicate detection)
502
+ const signerAttestationCount = await this.getSignerAttestationCountForSlot(slotNumber, signerAddress);
503
+
471
504
  if (alreadyExists) {
472
- const total = await this.getAttestationCount(slotNumber, proposalId);
473
- return { added: false, alreadyExists: true, totalForPosition: total };
505
+ return {
506
+ added: false,
507
+ alreadyExists: true,
508
+ count: signerAttestationCount,
509
+ };
474
510
  }
475
511
 
476
- const limit = committeeSize + ATTESTATION_CAP_BUFFER;
477
- const currentCount = await this.getAttestationCount(slotNumber, proposalId);
478
-
479
- if (currentCount >= limit) {
480
- return { added: false, alreadyExists: false, totalForPosition: currentCount };
512
+ // Check if this signer has exceeded the per-signer cap for this slot
513
+ if (signerAttestationCount >= MAX_ATTESTATIONS_PER_SLOT_AND_SIGNER) {
514
+ this.log.debug(`Rejecting attestation: signer ${signerAddress} exceeded per-slot cap for slot ${slotNumber}`, {
515
+ slotNumber,
516
+ signerAddress,
517
+ proposalId,
518
+ signerAttestationCount,
519
+ });
520
+ return {
521
+ added: false,
522
+ alreadyExists: false,
523
+ count: signerAttestationCount,
524
+ };
481
525
  }
482
526
 
527
+ // Add the attestation
483
528
  await this.checkpointAttestations.set(key, attestation.toBuffer());
484
529
 
485
- this.log.debug(`Added checkpoint attestation for slot ${slotNumber} from ${sender.toString()}`, {
530
+ // Track this attestation in the per-signer-per-slot index for duplicate detection
531
+ const slotSignerKey = this.getSlotSignerKey(slotNumber, signerAddress);
532
+ await this.checkpointAttestationsPerSlotAndSigner.set(slotSignerKey, proposalId);
533
+
534
+ this.log.debug(`Added checkpoint attestation for slot ${slotNumber} from ${signerAddress}`, {
486
535
  signature: attestation.signature.toString(),
487
536
  slotNumber,
488
- address: sender.toString(),
537
+ address: signerAddress,
489
538
  proposalId,
490
539
  });
491
- return { added: true, alreadyExists: false, totalForPosition: currentCount + 1 };
540
+
541
+ // Return the new count
542
+ return {
543
+ added: true,
544
+ alreadyExists: false,
545
+ count: signerAttestationCount + 1,
546
+ };
492
547
  });
493
548
  }
494
549
 
495
- /** Gets the count of attestations for a given (slot, proposalId). */
496
- private async getAttestationCount(slot: SlotNumber, proposalId: string): Promise<number> {
497
- const range = this.getAttestationKeyRangeForProposal(slot, proposalId);
498
- let count = 0;
499
- for await (const _ of this.checkpointAttestations.keysAsync(range)) {
500
- count++;
501
- }
502
- return count;
550
+ /** Gets the count of attestations by a specific signer for a given slot. */
551
+ private async getSignerAttestationCountForSlot(slot: SlotNumber, signerAddress: string): Promise<number> {
552
+ const slotSignerKey = this.getSlotSignerKey(slot, signerAddress);
553
+ return await this.checkpointAttestationsPerSlotAndSigner.getValueCountAsync(slotSignerKey);
503
554
  }
504
555
  }
505
556