@aztec/p2p 0.0.1-commit.e310a4c8 → 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 +17 -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 +25 -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
@@ -1,6 +1,414 @@
1
+ import { Fr } from '@aztec/foundation/curves/bn254';
2
+ import { toArray } from '@aztec/foundation/iterable';
3
+ import { createLogger } from '@aztec/foundation/log';
4
+ import { BlockProposal, CheckpointAttestation, CheckpointProposal } from '@aztec/stdlib/p2p';
5
+ import { getTelemetryClient } from '@aztec/telemetry-client';
6
+ import { PoolInstrumentation, PoolName } from '../instrumentation.js';
7
+ export const MAX_PROPOSALS_PER_SLOT = 5;
8
+ export const MAX_PROPOSALS_PER_POSITION = 3;
9
+ export const ATTESTATION_CAP_BUFFER = 10;
1
10
  /**
2
- * An Attestation Pool contains attestations collected by a validator
11
+ * Pool for storing attestations and proposals collected by a validator.
3
12
  *
4
- * Attestations that are observed via the p2p network are stored for requests
13
+ * Attestations and proposals observed via the p2p network are stored for requests
5
14
  * from the validator to produce a block, or to serve to other peers.
6
- */ export { };
15
+ */ export class AttestationPool {
16
+ store;
17
+ log;
18
+ metrics;
19
+ // Checkpoint attestations from attestation key (slot-proposalId-signer) to serialized CheckpointAttestation
20
+ // Keys are lexicographically sortable allowing range queries by slot or by (slot, proposalId)
21
+ checkpointAttestations;
22
+ // Checkpoint proposals from proposal archive to serialized CheckpointProposal
23
+ checkpointProposals;
24
+ // Checkpoint proposals indexed by slot for querying all proposals in a slot
25
+ // Key: slot number, Value: proposal archive strings
26
+ checkpointProposalsForSlot;
27
+ // Block proposals from proposal archive to serialized BlockProposal
28
+ blockProposals;
29
+ // Block proposals indexed by slot and index-within-checkpoint for duplicate detection
30
+ // Key: (slot << 10) | indexWithinCheckpoint, Value: archive string
31
+ blockProposalsForSlotAndIndex;
32
+ constructor(store, telemetry = getTelemetryClient(), log = createLogger('aztec:attestation_pool')){
33
+ this.store = store;
34
+ this.log = log;
35
+ this.poolStats = async ()=>{
36
+ return {
37
+ itemCount: await this.checkpointAttestations.sizeAsync()
38
+ };
39
+ };
40
+ // Initialize block proposal storage
41
+ this.blockProposals = store.openMap('proposals');
42
+ this.blockProposalsForSlotAndIndex = store.openMultiMap('block_proposals_for_slot_and_index');
43
+ // Initialize checkpoint attestations storage
44
+ this.checkpointAttestations = store.openMap('checkpoint_attestations');
45
+ // Initialize checkpoint proposal storage
46
+ this.checkpointProposals = store.openMap('checkpoint_proposals');
47
+ this.checkpointProposalsForSlot = store.openMultiMap('checkpoint_proposals_for_slot');
48
+ this.metrics = new PoolInstrumentation(telemetry, PoolName.ATTESTATION_POOL, this.poolStats);
49
+ }
50
+ poolStats;
51
+ /** Returns whether the pool is empty. */ async isEmpty() {
52
+ for await (const _ of this.checkpointAttestations.entriesAsync()){
53
+ return false;
54
+ }
55
+ for await (const _ of this.blockProposals.entriesAsync()){
56
+ return false;
57
+ }
58
+ return true;
59
+ }
60
+ getProposalKey(slot, proposalId) {
61
+ const slotStr = typeof slot === 'string' ? slot : new Fr(slot).toString();
62
+ const proposalIdStr = typeof proposalId === 'string' ? proposalId : Buffer.isBuffer(proposalId) ? Fr.fromBuffer(proposalId).toString() : proposalId.toString();
63
+ return `${slotStr}-${proposalIdStr}`;
64
+ }
65
+ getAttestationKey(slot, proposalId, address) {
66
+ return `${this.getProposalKey(slot, proposalId)}-${address}`;
67
+ }
68
+ /** Returns range bounds for querying all attestations for a given slot. */ getAttestationKeyRangeForSlot(slot) {
69
+ const slotStr = new Fr(slot).toString();
70
+ return {
71
+ start: `${slotStr}-`,
72
+ end: `${slotStr}-Z`
73
+ }; // 'Z' sorts after any hex character
74
+ }
75
+ /** Returns range bounds for querying all attestations for a given (slot, proposalId). */ getAttestationKeyRangeForProposal(slot, proposalId) {
76
+ const proposalKey = this.getProposalKey(slot, proposalId);
77
+ return {
78
+ start: `${proposalKey}-`,
79
+ end: `${proposalKey}-Z`
80
+ };
81
+ }
82
+ /** Number of bits reserved for indexWithinCheckpoint in position keys. */ static INDEX_BITS = 10;
83
+ /** Maximum indexWithinCheckpoint value (2^10 - 1 = 1023). */ static MAX_INDEX = (1 << AttestationPool.INDEX_BITS) - 1;
84
+ /** Creates a position key for block proposals: (slot << 10) | indexWithinCheckpoint. */ getBlockPositionKey(slot, indexWithinCheckpoint) {
85
+ if (indexWithinCheckpoint > AttestationPool.MAX_INDEX) {
86
+ throw new Error(`Value for indexWithinCheckpoint ${indexWithinCheckpoint} exceeds maximum ${AttestationPool.MAX_INDEX}`);
87
+ }
88
+ return slot << AttestationPool.INDEX_BITS | indexWithinCheckpoint;
89
+ }
90
+ /**
91
+ * Attempts to add a block proposal to the pool.
92
+ *
93
+ * This method performs validation and addition in a single call:
94
+ * - Checks if the proposal already exists (returns alreadyExists: true if so)
95
+ * - Checks if the position has reached the proposal cap (returns added: false if so)
96
+ * - Adds the proposal if validation passes
97
+ *
98
+ * @param blockProposal - The block proposal to add
99
+ * @returns Result indicating whether the proposal was added and duplicate detection info
100
+ */ async tryAddBlockProposal(blockProposal) {
101
+ return await this.store.transactionAsync(async ()=>{
102
+ const proposalId = blockProposal.archive.toString();
103
+ // Check if already exists
104
+ const alreadyExists = await this.blockProposals.hasAsync(proposalId);
105
+ if (alreadyExists) {
106
+ const totalForPosition = await this.getBlockProposalCountForPosition(blockProposal.slotNumber, blockProposal.indexWithinCheckpoint);
107
+ return {
108
+ added: false,
109
+ alreadyExists: true,
110
+ totalForPosition
111
+ };
112
+ }
113
+ // Get current count for position and check cap, do not add if exceeded
114
+ const totalForPosition = await this.getBlockProposalCountForPosition(blockProposal.slotNumber, blockProposal.indexWithinCheckpoint);
115
+ if (totalForPosition >= MAX_PROPOSALS_PER_POSITION) {
116
+ return {
117
+ added: false,
118
+ alreadyExists: false,
119
+ totalForPosition
120
+ };
121
+ }
122
+ // Add the proposal
123
+ await this.addBlockProposal(blockProposal);
124
+ this.log.debug(`Added block proposal for slot ${blockProposal.slotNumber} and index ${blockProposal.indexWithinCheckpoint}`, {
125
+ proposalId,
126
+ slotNumber: blockProposal.slotNumber,
127
+ indexWithinCheckpoint: blockProposal.indexWithinCheckpoint
128
+ });
129
+ return {
130
+ added: true,
131
+ alreadyExists: false,
132
+ totalForPosition: totalForPosition + 1
133
+ };
134
+ });
135
+ }
136
+ /** Gets the count of block proposals for a given position (slot, indexWithinCheckpoint). */ getBlockProposalCountForPosition(slot, indexWithinCheckpoint) {
137
+ const positionKey = this.getBlockPositionKey(slot, indexWithinCheckpoint);
138
+ return this.blockProposalsForSlotAndIndex.getValueCountAsync(positionKey);
139
+ }
140
+ /** Internal method - must be called within a transaction. */ async addBlockProposal(blockProposal) {
141
+ const proposalId = blockProposal.archive.toString();
142
+ // Strip signedTxs before storing to avoid persisting full tx data
143
+ await this.blockProposals.set(proposalId, blockProposal.withoutSignedTxs().toBuffer());
144
+ // Index by slot and position for duplicate detection
145
+ const positionKey = this.getBlockPositionKey(blockProposal.slotNumber, blockProposal.indexWithinCheckpoint);
146
+ await this.blockProposalsForSlotAndIndex.set(positionKey, proposalId);
147
+ }
148
+ /**
149
+ * Get block proposal by its ID.
150
+ *
151
+ * @param id - The ID of the block proposal to retrieve. The ID is proposal.payload.archive
152
+ *
153
+ * @return The block proposal if it exists, otherwise undefined.
154
+ */ async getBlockProposal(id) {
155
+ const buffer = await this.blockProposals.getAsync(id);
156
+ try {
157
+ if (buffer && buffer.length > 0) {
158
+ return BlockProposal.fromBuffer(buffer);
159
+ }
160
+ } catch {
161
+ return undefined;
162
+ }
163
+ return undefined;
164
+ }
165
+ /**
166
+ * Attempts to add a checkpoint proposal to the pool.
167
+ *
168
+ * This method performs validation and addition in a single call:
169
+ * - Checks if the proposal already exists (returns alreadyExists: true if so)
170
+ * - Checks if the slot has reached the proposal cap (returns added: false if so)
171
+ * - Adds the proposal if validation passes
172
+ *
173
+ * Note: This method only handles the CheckpointProposalCore. If the original
174
+ * CheckpointProposal contains a lastBlock, the caller should extract it via
175
+ * getBlockProposal() and add it separately via tryAddBlockProposal().
176
+ *
177
+ * @param proposal - The checkpoint proposal core to add
178
+ * @returns Result indicating whether the proposal was added and duplicate detection info
179
+ */ async tryAddCheckpointProposal(proposal) {
180
+ return await this.store.transactionAsync(async ()=>{
181
+ const proposalId = proposal.archive.toString();
182
+ // Check if already exists
183
+ const alreadyExists = await this.checkpointProposals.hasAsync(proposalId);
184
+ if (alreadyExists) {
185
+ const totalForPosition = await this.checkpointProposalsForSlot.getValueCountAsync(proposal.slotNumber);
186
+ return {
187
+ added: false,
188
+ alreadyExists: true,
189
+ totalForPosition
190
+ };
191
+ }
192
+ // Get current count for slot and check cap
193
+ const totalForPosition = await this.checkpointProposalsForSlot.getValueCountAsync(proposal.slotNumber);
194
+ if (totalForPosition >= MAX_PROPOSALS_PER_SLOT) {
195
+ return {
196
+ added: false,
197
+ alreadyExists: false,
198
+ totalForPosition
199
+ };
200
+ }
201
+ // Add the proposal if cap not exceeded
202
+ await this.addCheckpointProposal(proposal);
203
+ this.log.debug(`Added checkpoint proposal for slot ${proposal.slotNumber}`, {
204
+ proposalId,
205
+ slotNumber: proposal.slotNumber
206
+ });
207
+ return {
208
+ added: true,
209
+ alreadyExists: false,
210
+ totalForPosition: totalForPosition + 1
211
+ };
212
+ });
213
+ }
214
+ /** Internal method - must be called within a transaction. */ async addCheckpointProposal(proposal) {
215
+ const slotKey = proposal.slotNumber;
216
+ const proposalId = proposal.archive.toString();
217
+ await this.checkpointProposalsForSlot.set(slotKey, proposalId);
218
+ await this.checkpointProposals.set(proposalId, proposal.toBuffer());
219
+ }
220
+ /**
221
+ * Get checkpoint proposal by its ID.
222
+ *
223
+ * Returns a CheckpointProposalCore (without lastBlock info) since the lastBlock
224
+ * is extracted and stored separately as a BlockProposal when added.
225
+ *
226
+ * @param id - The ID of the checkpoint proposal to retrieve (proposal.archive)
227
+ * @return The checkpoint proposal core if it exists, otherwise undefined.
228
+ */ async getCheckpointProposal(id) {
229
+ const buffer = await this.checkpointProposals.getAsync(id);
230
+ try {
231
+ if (buffer && buffer.length > 0) {
232
+ return CheckpointProposal.fromBuffer(buffer);
233
+ }
234
+ } catch {
235
+ return undefined;
236
+ }
237
+ return undefined;
238
+ }
239
+ /**
240
+ * Adds own checkpoint attestations to the pool.
241
+ * Skips validations on number of checkpoint attestations stored for the given slot.
242
+ */ async addOwnCheckpointAttestations(attestations) {
243
+ await this.store.transactionAsync(async ()=>{
244
+ for (const attestation of attestations){
245
+ const slotNumber = attestation.payload.header.slotNumber;
246
+ const proposalId = attestation.archive;
247
+ const sender = attestation.getSender();
248
+ // Skip attestations with invalid signatures
249
+ if (!sender) {
250
+ this.log.warn(`Skipping own checkpoint attestation with invalid signature for slot ${slotNumber}`, {
251
+ signature: attestation.signature.toString(),
252
+ slotNumber,
253
+ proposalId
254
+ });
255
+ continue;
256
+ }
257
+ const address = sender.toString();
258
+ await this.checkpointAttestations.set(this.getAttestationKey(slotNumber, proposalId, address), attestation.toBuffer());
259
+ this.log.debug(`Added own checkpoint attestation for slot ${slotNumber} from ${address}`, {
260
+ signature: attestation.signature.toString(),
261
+ slotNumber,
262
+ address,
263
+ proposalId
264
+ });
265
+ }
266
+ });
267
+ }
268
+ /**
269
+ * Get all checkpoint attestations for a given slot.
270
+ *
271
+ * @param slot - The slot to query
272
+ * @return CheckpointAttestations
273
+ */ async getCheckpointAttestationsForSlot(slot) {
274
+ const range = this.getAttestationKeyRangeForSlot(slot);
275
+ const attestations = [];
276
+ for await (const [_, buf] of this.checkpointAttestations.entriesAsync(range)){
277
+ attestations.push(CheckpointAttestation.fromBuffer(buf));
278
+ }
279
+ return attestations;
280
+ }
281
+ /**
282
+ * Get checkpoint attestations for slot and given proposal.
283
+ *
284
+ * @param slot - The slot to query
285
+ * @param proposalId - The proposal to query
286
+ * @return CheckpointAttestations
287
+ */ async getCheckpointAttestationsForSlotAndProposal(slot, proposalId) {
288
+ const range = this.getAttestationKeyRangeForProposal(slot, proposalId);
289
+ const attestations = [];
290
+ for await (const [_, buf] of this.checkpointAttestations.entriesAsync(range)){
291
+ attestations.push(CheckpointAttestation.fromBuffer(buf));
292
+ }
293
+ return attestations;
294
+ }
295
+ /**
296
+ * Delete all pool data (attestations, proposals) older than the given slot.
297
+ *
298
+ * @param oldestSlot - The oldest slot to keep.
299
+ */ async deleteOlderThan(oldestSlot) {
300
+ let numberOfAttestations = 0;
301
+ let numberOfCheckpointProposals = 0;
302
+ let numberOfBlockProposals = 0;
303
+ await this.store.transactionAsync(async ()=>{
304
+ // Delete checkpoint attestations with slot < oldestSlot
305
+ // Attestation keys start with Fr(slot).toString(), so we use end bound of Fr(oldestSlot).toString()
306
+ const attestationEndKey = new Fr(oldestSlot).toString();
307
+ for await (const key of this.checkpointAttestations.keysAsync({
308
+ end: attestationEndKey
309
+ })){
310
+ await this.checkpointAttestations.delete(key);
311
+ numberOfAttestations++;
312
+ }
313
+ // Delete checkpoint proposals for slots < oldestSlot, using checkpointProposalsForSlot as index
314
+ for await (const slot of this.checkpointProposalsForSlot.keysAsync({
315
+ end: oldestSlot
316
+ })){
317
+ const proposalIds = await toArray(this.checkpointProposalsForSlot.getValuesAsync(slot));
318
+ for (const proposalId of proposalIds){
319
+ await this.checkpointProposals.delete(proposalId);
320
+ numberOfCheckpointProposals++;
321
+ }
322
+ await this.checkpointProposalsForSlot.delete(slot);
323
+ }
324
+ // Delete block proposals for slots < oldestSlot, using blockProposalsForSlotAndIndex as index
325
+ // Key format: (slot << INDEX_BITS) | indexWithinCheckpoint
326
+ const blockPositionEndKey = oldestSlot << AttestationPool.INDEX_BITS;
327
+ for await (const positionKey of this.blockProposalsForSlotAndIndex.keysAsync({
328
+ end: blockPositionEndKey
329
+ })){
330
+ const proposalIds = await toArray(this.blockProposalsForSlotAndIndex.getValuesAsync(positionKey));
331
+ for (const proposalId of proposalIds){
332
+ await this.blockProposals.delete(proposalId);
333
+ numberOfBlockProposals++;
334
+ }
335
+ await this.blockProposalsForSlotAndIndex.delete(positionKey);
336
+ }
337
+ });
338
+ this.log.verbose(`Deleted old pool data`, {
339
+ oldestSlot,
340
+ numberOfAttestations,
341
+ numberOfCheckpointProposals,
342
+ numberOfBlockProposals
343
+ });
344
+ }
345
+ /**
346
+ * Attempts to add a checkpoint attestation to the pool.
347
+ *
348
+ * This method performs validation and addition in a single call:
349
+ * - Checks if the attestation already exists (returns alreadyExists: true if so)
350
+ * - Checks if the (slot, proposalId) has reached the attestation cap (returns added: false if so)
351
+ * - Adds the attestation if validation passes
352
+ *
353
+ * @param attestation - The checkpoint attestation to add
354
+ * @param committeeSize - Committee size for the attestation's slot
355
+ * @returns Result indicating whether the attestation was added and existence info
356
+ */ async tryAddCheckpointAttestation(attestation, committeeSize) {
357
+ const slotNumber = attestation.payload.header.slotNumber;
358
+ const proposalId = attestation.archive.toString();
359
+ const sender = attestation.getSender();
360
+ if (!sender) {
361
+ return {
362
+ added: false,
363
+ alreadyExists: false,
364
+ totalForPosition: 0
365
+ };
366
+ }
367
+ return await this.store.transactionAsync(async ()=>{
368
+ const key = this.getAttestationKey(slotNumber, proposalId, sender.toString());
369
+ const alreadyExists = await this.checkpointAttestations.hasAsync(key);
370
+ if (alreadyExists) {
371
+ const total = await this.getAttestationCount(slotNumber, proposalId);
372
+ return {
373
+ added: false,
374
+ alreadyExists: true,
375
+ totalForPosition: total
376
+ };
377
+ }
378
+ const limit = committeeSize + ATTESTATION_CAP_BUFFER;
379
+ const currentCount = await this.getAttestationCount(slotNumber, proposalId);
380
+ if (currentCount >= limit) {
381
+ return {
382
+ added: false,
383
+ alreadyExists: false,
384
+ totalForPosition: currentCount
385
+ };
386
+ }
387
+ await this.checkpointAttestations.set(key, attestation.toBuffer());
388
+ this.log.debug(`Added checkpoint attestation for slot ${slotNumber} from ${sender.toString()}`, {
389
+ signature: attestation.signature.toString(),
390
+ slotNumber,
391
+ address: sender.toString(),
392
+ proposalId
393
+ });
394
+ return {
395
+ added: true,
396
+ alreadyExists: false,
397
+ totalForPosition: currentCount + 1
398
+ };
399
+ });
400
+ }
401
+ /** Gets the count of attestations for a given (slot, proposalId). */ async getAttestationCount(slot, proposalId) {
402
+ const range = this.getAttestationKeyRangeForProposal(slot, proposalId);
403
+ let count = 0;
404
+ for await (const _ of this.checkpointAttestations.keysAsync(range)){
405
+ count++;
406
+ }
407
+ return count;
408
+ }
409
+ }
410
+ /** Creates an AttestationPool backed by a temporary store for testing. */ export async function createTestAttestationPool(telemetry) {
411
+ const { openTmpStore } = await import('@aztec/kv-store/lmdb-v2');
412
+ const store = await openTmpStore('test-attestation-pool');
413
+ return new AttestationPool(store, telemetry);
414
+ }
@@ -1,3 +1,3 @@
1
- import type { AttestationPool } from './attestation_pool.js';
1
+ import { type AttestationPool } from './attestation_pool.js';
2
2
  export declare function describeAttestationPool(getAttestationPool: () => AttestationPool): void;
3
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYXR0ZXN0YXRpb25fcG9vbF90ZXN0X3N1aXRlLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvbWVtX3Bvb2xzL2F0dGVzdGF0aW9uX3Bvb2wvYXR0ZXN0YXRpb25fcG9vbF90ZXN0X3N1aXRlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQVlBLE9BQU8sS0FBSyxFQUFFLGVBQWUsRUFBRSxNQUFNLHVCQUF1QixDQUFDO0FBTTdELHdCQUFnQix1QkFBdUIsQ0FBQyxrQkFBa0IsRUFBRSxNQUFNLGVBQWUsUUEwV2hGIn0=
3
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYXR0ZXN0YXRpb25fcG9vbF90ZXN0X3N1aXRlLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvbWVtX3Bvb2xzL2F0dGVzdGF0aW9uX3Bvb2wvYXR0ZXN0YXRpb25fcG9vbF90ZXN0X3N1aXRlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQVlBLE9BQU8sRUFBRSxLQUFLLGVBQWUsRUFBc0QsTUFBTSx1QkFBdUIsQ0FBQztBQUtqSCx3QkFBZ0IsdUJBQXVCLENBQUMsa0JBQWtCLEVBQUUsTUFBTSxlQUFlLFFBMnJCaEYifQ==
@@ -1 +1 @@
1
- {"version":3,"file":"attestation_pool_test_suite.d.ts","sourceRoot":"","sources":["../../../src/mem_pools/attestation_pool/attestation_pool_test_suite.ts"],"names":[],"mappings":"AAYA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAM7D,wBAAgB,uBAAuB,CAAC,kBAAkB,EAAE,MAAM,eAAe,QA0WhF"}
1
+ {"version":3,"file":"attestation_pool_test_suite.d.ts","sourceRoot":"","sources":["../../../src/mem_pools/attestation_pool/attestation_pool_test_suite.ts"],"names":[],"mappings":"AAYA,OAAO,EAAE,KAAK,eAAe,EAAsD,MAAM,uBAAuB,CAAC;AAKjH,wBAAgB,uBAAuB,CAAC,kBAAkB,EAAE,MAAM,eAAe,QA2rBhF"}