@aztec/p2p 4.0.0-nightly.20260111 → 4.0.0-nightly.20260113

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 (86) 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 +9 -12
  4. package/dest/client/p2p_client.d.ts.map +1 -1
  5. package/dest/client/p2p_client.js +59 -103
  6. package/dest/mem_pools/attestation_pool/attestation_pool.d.ts +61 -42
  7. package/dest/mem_pools/attestation_pool/attestation_pool.d.ts.map +1 -1
  8. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.d.ts +1 -1
  9. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.d.ts.map +1 -1
  10. package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.js +225 -262
  11. package/dest/mem_pools/attestation_pool/kv_attestation_pool.d.ts +21 -18
  12. package/dest/mem_pools/attestation_pool/kv_attestation_pool.d.ts.map +1 -1
  13. package/dest/mem_pools/attestation_pool/kv_attestation_pool.js +113 -108
  14. package/dest/mem_pools/attestation_pool/memory_attestation_pool.d.ts +17 -16
  15. package/dest/mem_pools/attestation_pool/memory_attestation_pool.d.ts.map +1 -1
  16. package/dest/mem_pools/attestation_pool/memory_attestation_pool.js +89 -128
  17. package/dest/mem_pools/attestation_pool/mocks.d.ts +7 -6
  18. package/dest/mem_pools/attestation_pool/mocks.d.ts.map +1 -1
  19. package/dest/mem_pools/attestation_pool/mocks.js +9 -8
  20. package/dest/msg_validators/attestation_validator/attestation_validator.d.ts +4 -4
  21. package/dest/msg_validators/attestation_validator/attestation_validator.d.ts.map +1 -1
  22. package/dest/msg_validators/attestation_validator/attestation_validator.js +12 -10
  23. package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.d.ts +5 -5
  24. package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.d.ts.map +1 -1
  25. package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.js +5 -5
  26. package/dest/msg_validators/index.d.ts +2 -2
  27. package/dest/msg_validators/index.d.ts.map +1 -1
  28. package/dest/msg_validators/index.js +1 -1
  29. package/dest/msg_validators/proposal_validator/block_proposal_validator.d.ts +9 -0
  30. package/dest/msg_validators/proposal_validator/block_proposal_validator.d.ts.map +1 -0
  31. package/dest/msg_validators/proposal_validator/block_proposal_validator.js +6 -0
  32. package/dest/msg_validators/proposal_validator/checkpoint_proposal_validator.d.ts +9 -0
  33. package/dest/msg_validators/proposal_validator/checkpoint_proposal_validator.d.ts.map +1 -0
  34. package/dest/msg_validators/proposal_validator/checkpoint_proposal_validator.js +6 -0
  35. package/dest/msg_validators/proposal_validator/index.d.ts +4 -0
  36. package/dest/msg_validators/proposal_validator/index.d.ts.map +1 -0
  37. package/dest/msg_validators/proposal_validator/index.js +3 -0
  38. package/dest/msg_validators/proposal_validator/proposal_validator.d.ts +13 -0
  39. package/dest/msg_validators/proposal_validator/proposal_validator.d.ts.map +1 -0
  40. package/dest/msg_validators/{block_proposal_validator/block_proposal_validator.js → proposal_validator/proposal_validator.js} +19 -21
  41. package/dest/msg_validators/proposal_validator/proposal_validator_test_suite.d.ts +23 -0
  42. package/dest/msg_validators/proposal_validator/proposal_validator_test_suite.d.ts.map +1 -0
  43. package/dest/msg_validators/proposal_validator/proposal_validator_test_suite.js +183 -0
  44. package/dest/services/dummy_service.d.ts +6 -2
  45. package/dest/services/dummy_service.d.ts.map +1 -1
  46. package/dest/services/dummy_service.js +3 -0
  47. package/dest/services/encoding.d.ts +1 -1
  48. package/dest/services/encoding.d.ts.map +1 -1
  49. package/dest/services/encoding.js +4 -2
  50. package/dest/services/libp2p/libp2p_service.d.ts +26 -10
  51. package/dest/services/libp2p/libp2p_service.d.ts.map +1 -1
  52. package/dest/services/libp2p/libp2p_service.js +218 -88
  53. package/dest/services/service.d.ts +16 -3
  54. package/dest/services/service.d.ts.map +1 -1
  55. package/dest/testbench/p2p_client_testbench_worker.js +25 -11
  56. package/dest/testbench/worker_client_manager.d.ts +1 -1
  57. package/dest/testbench/worker_client_manager.d.ts.map +1 -1
  58. package/dest/testbench/worker_client_manager.js +6 -1
  59. package/package.json +14 -14
  60. package/src/client/interface.ts +19 -4
  61. package/src/client/p2p_client.ts +69 -110
  62. package/src/mem_pools/attestation_pool/attestation_pool.ts +68 -41
  63. package/src/mem_pools/attestation_pool/attestation_pool_test_suite.ts +231 -287
  64. package/src/mem_pools/attestation_pool/kv_attestation_pool.ts +162 -140
  65. package/src/mem_pools/attestation_pool/memory_attestation_pool.ts +141 -164
  66. package/src/mem_pools/attestation_pool/mocks.ts +13 -9
  67. package/src/msg_validators/attestation_validator/attestation_validator.ts +16 -13
  68. package/src/msg_validators/attestation_validator/fisherman_attestation_validator.ts +7 -7
  69. package/src/msg_validators/index.ts +1 -1
  70. package/src/msg_validators/proposal_validator/block_proposal_validator.ts +10 -0
  71. package/src/msg_validators/proposal_validator/checkpoint_proposal_validator.ts +13 -0
  72. package/src/msg_validators/proposal_validator/index.ts +3 -0
  73. package/src/msg_validators/{block_proposal_validator/block_proposal_validator.ts → proposal_validator/proposal_validator.ts} +23 -28
  74. package/src/msg_validators/proposal_validator/proposal_validator_test_suite.ts +206 -0
  75. package/src/services/dummy_service.ts +6 -0
  76. package/src/services/encoding.ts +3 -1
  77. package/src/services/libp2p/libp2p_service.ts +258 -94
  78. package/src/services/service.ts +19 -4
  79. package/src/testbench/p2p_client_testbench_worker.ts +34 -11
  80. package/src/testbench/worker_client_manager.ts +6 -1
  81. package/dest/msg_validators/block_proposal_validator/block_proposal_validator.d.ts +0 -12
  82. package/dest/msg_validators/block_proposal_validator/block_proposal_validator.d.ts.map +0 -1
  83. package/dest/msg_validators/block_proposal_validator/index.d.ts +0 -2
  84. package/dest/msg_validators/block_proposal_validator/index.d.ts.map +0 -1
  85. package/dest/msg_validators/block_proposal_validator/index.js +0 -1
  86. package/src/msg_validators/block_proposal_validator/index.ts +0 -1
@@ -1,71 +1,120 @@
1
1
  import type { SlotNumber } from '@aztec/foundation/branded-types';
2
2
  import { createLogger } from '@aztec/foundation/log';
3
- import type { BlockAttestation, BlockProposal } from '@aztec/stdlib/p2p';
3
+ import type {
4
+ BlockProposal,
5
+ CheckpointAttestation,
6
+ CheckpointProposal,
7
+ CheckpointProposalCore,
8
+ } from '@aztec/stdlib/p2p';
4
9
  import { type TelemetryClient, getTelemetryClient } from '@aztec/telemetry-client';
5
10
 
11
+ import { ProposalSlotCapExceededError } from '../../errors/attestation-pool.error.js';
6
12
  import { PoolInstrumentation, PoolName, type PoolStatsCallback } from '../instrumentation.js';
7
13
  import type { AttestationPool } from './attestation_pool.js';
8
14
  import { ATTESTATION_CAP_BUFFER, MAX_PROPOSALS_PER_SLOT } from './kv_attestation_pool.js';
9
15
 
10
16
  export class InMemoryAttestationPool implements AttestationPool {
11
- private metrics: PoolInstrumentation<BlockAttestation>;
17
+ private metrics: PoolInstrumentation<CheckpointAttestation>;
12
18
 
19
+ private proposals: Map<string, BlockProposal>;
20
+
21
+ // Checkpoint attestations
13
22
  // eslint-disable-next-line aztec-custom/no-non-primitive-in-collections
14
- private attestations: Map<
23
+ private checkpointAttestations: Map<
15
24
  /*slot=*/ SlotNumber,
16
- Map</*proposalId*/ string, Map</*address=*/ string, BlockAttestation>>
25
+ Map</*proposalId*/ string, Map</*address=*/ string, CheckpointAttestation>>
17
26
  >;
18
- private proposals: Map<string, BlockProposal>;
27
+ private checkpointProposals: Map<string, CheckpointProposalCore>;
19
28
 
20
29
  constructor(
21
30
  telemetry: TelemetryClient = getTelemetryClient(),
22
31
  private log = createLogger('p2p:attestation_pool'),
23
32
  ) {
24
- this.attestations = new Map();
25
33
  this.proposals = new Map();
34
+ this.checkpointAttestations = new Map();
35
+ this.checkpointProposals = new Map();
26
36
  this.metrics = new PoolInstrumentation(telemetry, PoolName.ATTESTATION_POOL, this.poolStats);
27
37
  }
28
38
 
29
39
  private poolStats: PoolStatsCallback = () => {
30
40
  return Promise.resolve({
31
- itemCount: this.attestations.size,
41
+ itemCount: this.checkpointAttestations.size,
32
42
  });
33
43
  };
34
44
 
35
45
  public isEmpty(): Promise<boolean> {
36
- return Promise.resolve(this.attestations.size === 0);
46
+ return Promise.resolve(this.checkpointAttestations.size === 0 && this.proposals.size === 0);
37
47
  }
38
48
 
39
- public getAttestationsForSlot(slot: SlotNumber): Promise<BlockAttestation[]> {
40
- return Promise.resolve(
41
- Array.from(this.attestations.get(slot)?.values() ?? []).flatMap(proposalAttestationMap =>
42
- Array.from(proposalAttestationMap.values()),
43
- ),
44
- );
49
+ public addBlockProposal(blockProposal: BlockProposal): Promise<void> {
50
+ // Strip signedTxs before storing to avoid holding full tx data in memory
51
+ this.proposals.set(blockProposal.archive.toString(), blockProposal.withoutSignedTxs());
52
+ return Promise.resolve();
45
53
  }
46
54
 
47
- public getAttestationsForSlotAndProposal(slot: SlotNumber, proposalId: string): Promise<BlockAttestation[]> {
48
- const slotAttestationMap = this.attestations.get(slot);
49
- if (slotAttestationMap) {
50
- const proposalAttestationMap = slotAttestationMap.get(proposalId);
51
- if (proposalAttestationMap) {
52
- return Promise.resolve(Array.from(proposalAttestationMap.values()));
53
- }
55
+ public getBlockProposal(id: string): Promise<BlockProposal | undefined> {
56
+ return Promise.resolve(this.proposals.get(id));
57
+ }
58
+
59
+ public hasBlockProposal(idOrProposal: string | BlockProposal): Promise<boolean> {
60
+ const id = typeof idOrProposal === 'string' ? idOrProposal : idOrProposal.archive.toString();
61
+ return Promise.resolve(this.proposals.has(id));
62
+ }
63
+
64
+ public canAddProposal(_block: BlockProposal): Promise<boolean> {
65
+ // TODO(palla/mbps): See when to allow
66
+ return Promise.resolve(true);
67
+ }
68
+
69
+ // Checkpoint attestation methods
70
+
71
+ public async addCheckpointProposal(proposal: CheckpointProposal): Promise<void> {
72
+ if (!(await this.canAddCheckpointProposal(proposal))) {
73
+ throw new ProposalSlotCapExceededError(
74
+ `Maximum checkpoint proposals per slot reached: slot=${proposal.slotNumber} cap=${MAX_PROPOSALS_PER_SLOT} proposal=${proposal.archive.toString()}`,
75
+ );
54
76
  }
55
- return Promise.resolve([]);
77
+
78
+ // Extract and validate the block proposal if present
79
+ const blockProposal = proposal.getBlockProposal();
80
+ if (blockProposal && !(await this.canAddProposal(blockProposal))) {
81
+ throw new ProposalSlotCapExceededError(
82
+ `Maximum block proposals per slot reached when extracting from checkpoint: slot=${proposal.slotNumber} proposal=${blockProposal.archive.toString()}`,
83
+ );
84
+ }
85
+
86
+ const slotProposalMapping = getCheckpointSlotOrDefault(this.checkpointAttestations, proposal.slotNumber);
87
+ slotProposalMapping.set(proposal.archive.toString(), new Map<string, CheckpointAttestation>());
88
+
89
+ // Store the checkpoint proposal as core (without lastBlock) to avoid duplication
90
+ this.checkpointProposals.set(proposal.archive.toString(), proposal.toCore());
91
+
92
+ // Store the extracted block proposal separately
93
+ if (blockProposal) {
94
+ this.proposals.set(blockProposal.archive.toString(), blockProposal.withoutSignedTxs());
95
+ }
96
+
97
+ return Promise.resolve();
98
+ }
99
+
100
+ public getCheckpointProposal(id: string): Promise<CheckpointProposalCore | undefined> {
101
+ return Promise.resolve(this.checkpointProposals.get(id));
102
+ }
103
+
104
+ public hasCheckpointProposal(idOrProposal: string | CheckpointProposal): Promise<boolean> {
105
+ const id = typeof idOrProposal === 'string' ? idOrProposal : idOrProposal.archive.toString();
106
+ return Promise.resolve(this.checkpointProposals.has(id));
56
107
  }
57
108
 
58
- public addAttestations(attestations: BlockAttestation[]): Promise<void> {
109
+ public addCheckpointAttestations(attestations: CheckpointAttestation[]): Promise<void> {
59
110
  for (const attestation of attestations) {
60
- // Perf: order and group by slot before insertion
61
111
  const slotNumber = attestation.payload.header.slotNumber;
62
-
63
112
  const proposalId = attestation.archive.toString();
64
113
  const sender = attestation.getSender();
65
114
 
66
115
  // Skip attestations with invalid signatures
67
116
  if (!sender) {
68
- this.log.warn(`Skipping attestation with invalid signature for slot ${slotNumber}`, {
117
+ this.log.warn(`Skipping checkpoint attestation with invalid signature for slot ${slotNumber}`, {
69
118
  signature: attestation.signature.toString(),
70
119
  slotNumber,
71
120
  proposalId,
@@ -73,11 +122,11 @@ export class InMemoryAttestationPool implements AttestationPool {
73
122
  continue;
74
123
  }
75
124
 
76
- const slotAttestationMap = getSlotOrDefault(this.attestations, slotNumber);
77
- const proposalAttestationMap = getProposalOrDefault(slotAttestationMap, proposalId);
125
+ const slotAttestationMap = getCheckpointSlotOrDefault(this.checkpointAttestations, slotNumber);
126
+ const proposalAttestationMap = getCheckpointProposalOrDefault(slotAttestationMap, proposalId);
78
127
  proposalAttestationMap.set(sender.toString(), attestation);
79
128
 
80
- this.log.verbose(`Added attestation for slot ${slotNumber} from ${sender}`, {
129
+ this.log.verbose(`Added checkpoint attestation for slot ${slotNumber} from ${sender}`, {
81
130
  signature: attestation.signature.toString(),
82
131
  slotNumber,
83
132
  address: sender,
@@ -88,25 +137,32 @@ export class InMemoryAttestationPool implements AttestationPool {
88
137
  return Promise.resolve();
89
138
  }
90
139
 
91
- #getNumberOfAttestationsInSlot(slot: SlotNumber): number {
92
- let total = 0;
93
- const slotAttestationMap = getSlotOrDefault(this.attestations, slot);
140
+ public getCheckpointAttestationsForSlot(slot: SlotNumber): Promise<CheckpointAttestation[]> {
141
+ return Promise.resolve(
142
+ Array.from(this.checkpointAttestations.get(slot)?.values() ?? []).flatMap(proposalAttestationMap =>
143
+ Array.from(proposalAttestationMap.values()),
144
+ ),
145
+ );
146
+ }
94
147
 
148
+ public getCheckpointAttestationsForSlotAndProposal(
149
+ slot: SlotNumber,
150
+ proposalId: string,
151
+ ): Promise<CheckpointAttestation[]> {
152
+ const slotAttestationMap = this.checkpointAttestations.get(slot);
95
153
  if (slotAttestationMap) {
96
- for (const proposalAttestationMap of slotAttestationMap.values() ?? []) {
97
- total += proposalAttestationMap.size;
154
+ const proposalAttestationMap = slotAttestationMap.get(proposalId);
155
+ if (proposalAttestationMap) {
156
+ return Promise.resolve(Array.from(proposalAttestationMap.values()));
98
157
  }
99
158
  }
100
- return total;
159
+ return Promise.resolve([]);
101
160
  }
102
161
 
103
- public async deleteAttestationsOlderThan(oldestSlot: SlotNumber): Promise<void> {
162
+ public deleteCheckpointAttestationsOlderThan(oldestSlot: SlotNumber): Promise<void> {
104
163
  const olderThan = [];
105
164
 
106
- // Entries are iterated in insertion order, so we can break as soon as we find a slot that is older than the oldestSlot.
107
- // Note: this will only prune correctly if attestations are added in order of rising slot, it is important that we do not allow
108
- // insertion of attestations that are old. #(https://github.com/AztecProtocol/aztec-packages/issues/10322)
109
- const slots = this.attestations.keys();
165
+ const slots = this.checkpointAttestations.keys();
110
166
  for (const slot of slots) {
111
167
  if (slot < oldestSlot) {
112
168
  olderThan.push(slot);
@@ -116,70 +172,51 @@ export class InMemoryAttestationPool implements AttestationPool {
116
172
  }
117
173
 
118
174
  for (const oldSlot of olderThan) {
119
- await this.deleteAttestationsForSlot(oldSlot);
175
+ const proposalIds = this.checkpointAttestations.get(oldSlot)?.keys();
176
+ proposalIds?.forEach(proposalId => this.checkpointProposals.delete(proposalId));
177
+ this.checkpointAttestations.delete(oldSlot);
120
178
  }
121
179
  return Promise.resolve();
122
180
  }
123
181
 
124
- public deleteAttestationsForSlot(slot: SlotNumber): Promise<void> {
125
- // We count the number of attestations we are removing
126
- const numberOfAttestations = this.#getNumberOfAttestationsInSlot(slot);
127
- const proposalIdsToDelete = this.attestations.get(slot)?.keys();
128
- let proposalIdsToDeleteCount = 0;
129
- proposalIdsToDelete?.forEach(proposalId => {
130
- this.proposals.delete(proposalId);
131
- proposalIdsToDeleteCount++;
132
- });
133
-
134
- this.attestations.delete(slot);
135
- this.log.verbose(
136
- `Removed ${numberOfAttestations} attestations and ${proposalIdsToDeleteCount} proposals for slot ${slot}`,
137
- );
138
-
139
- return Promise.resolve();
182
+ public hasReachedCheckpointProposalCap(slot: SlotNumber): Promise<boolean> {
183
+ const slotAttestationMap = this.checkpointAttestations.get(slot);
184
+ const proposalCount = slotAttestationMap?.size ?? 0;
185
+ return Promise.resolve(proposalCount >= MAX_PROPOSALS_PER_SLOT);
140
186
  }
141
187
 
142
- public deleteAttestationsForSlotAndProposal(slot: SlotNumber, proposalId: string): Promise<void> {
143
- const slotAttestationMap = getSlotOrDefault(this.attestations, slot);
144
- if (slotAttestationMap) {
145
- if (slotAttestationMap.has(proposalId)) {
146
- const numberOfAttestations = slotAttestationMap.get(proposalId)?.size ?? 0;
147
-
148
- slotAttestationMap.delete(proposalId);
149
-
150
- this.log.verbose(`Removed ${numberOfAttestations} attestations for slot ${slot} and proposal ${proposalId}`);
151
- }
152
- }
188
+ public hasReachedCheckpointAttestationCap(
189
+ slot: SlotNumber,
190
+ proposalId: string,
191
+ committeeSize: number,
192
+ ): Promise<boolean> {
193
+ const limit = committeeSize + ATTESTATION_CAP_BUFFER;
194
+ const count = this.checkpointAttestations.get(slot)?.get(proposalId)?.size ?? 0;
195
+ return Promise.resolve(limit <= 0 || count >= limit);
196
+ }
153
197
 
154
- this.proposals.delete(proposalId);
155
- return Promise.resolve();
198
+ public async canAddCheckpointProposal(proposal: CheckpointProposal): Promise<boolean> {
199
+ return (
200
+ this.checkpointProposals.has(proposal.archive.toString()) ||
201
+ !(await this.hasReachedCheckpointProposalCap(proposal.slotNumber))
202
+ );
156
203
  }
157
204
 
158
- public deleteAttestations(attestations: BlockAttestation[]): Promise<void> {
159
- for (const attestation of attestations) {
160
- const slotNumber = attestation.payload.header.slotNumber;
161
- const slotAttestationMap = this.attestations.get(slotNumber);
162
- if (slotAttestationMap) {
163
- const proposalId = attestation.archive.toString();
164
- const proposalAttestationMap = getProposalOrDefault(slotAttestationMap, proposalId);
165
- if (proposalAttestationMap) {
166
- const sender = attestation.getSender();
167
-
168
- // Skip attestations with invalid signatures
169
- if (!sender) {
170
- this.log.warn(`Skipping deletion of attestation with invalid signature for slot ${slotNumber}`);
171
- continue;
172
- }
173
-
174
- proposalAttestationMap.delete(sender.toString());
175
- this.log.debug(`Deleted attestation for slot ${slotNumber} from ${sender}`);
176
- }
177
- }
178
- }
179
- return Promise.resolve();
205
+ public async canAddCheckpointAttestation(
206
+ attestation: CheckpointAttestation,
207
+ committeeSize: number,
208
+ ): Promise<boolean> {
209
+ const sender = attestation.getSender();
210
+ const slot = attestation.payload.header.slotNumber;
211
+ const pid = attestation.archive.toString();
212
+ return (
213
+ !!sender &&
214
+ ((this.checkpointAttestations.get(slot)?.get(pid)?.has(sender.toString()) ?? false) ||
215
+ !(await this.hasReachedCheckpointAttestationCap(slot, pid, committeeSize)))
216
+ );
180
217
  }
181
218
 
182
- public hasAttestation(attestation: BlockAttestation): Promise<boolean> {
219
+ public hasCheckpointAttestation(attestation: CheckpointAttestation): Promise<boolean> {
183
220
  const slotNumber = attestation.payload.header.slotNumber;
184
221
  const proposalId = attestation.archive.toString();
185
222
  const sender = attestation.getSender();
@@ -189,7 +226,7 @@ export class InMemoryAttestationPool implements AttestationPool {
189
226
  return Promise.resolve(false);
190
227
  }
191
228
 
192
- const slotAttestationMap = this.attestations.get(slotNumber);
229
+ const slotAttestationMap = this.checkpointAttestations.get(slotNumber);
193
230
  if (!slotAttestationMap) {
194
231
  return Promise.resolve(false);
195
232
  }
@@ -201,87 +238,27 @@ export class InMemoryAttestationPool implements AttestationPool {
201
238
 
202
239
  return Promise.resolve(proposalAttestationMap.has(sender.toString()));
203
240
  }
204
-
205
- public addBlockProposal(blockProposal: BlockProposal): Promise<void> {
206
- // We initialize slot-proposal mapping if it does not exist
207
- // This is important to ensure we can delete this proposal if there were not attestations for it
208
- const slotProposalMapping = getSlotOrDefault(this.attestations, blockProposal.slotNumber);
209
- slotProposalMapping.set(blockProposal.payload.archive.toString(), new Map<string, BlockAttestation>());
210
-
211
- this.proposals.set(blockProposal.payload.archive.toString(), blockProposal);
212
- return Promise.resolve();
213
- }
214
-
215
- public getBlockProposal(id: string): Promise<BlockProposal | undefined> {
216
- return Promise.resolve(this.proposals.get(id));
217
- }
218
-
219
- public hasBlockProposal(idOrProposal: string | BlockProposal): Promise<boolean> {
220
- const id = typeof idOrProposal === 'string' ? idOrProposal : idOrProposal.payload.archive.toString();
221
- return Promise.resolve(this.proposals.has(id));
222
- }
223
-
224
- public hasReachedProposalCap(slot: SlotNumber): Promise<boolean> {
225
- const slotAttestationMap = this.attestations.get(slot);
226
- const proposalCount = slotAttestationMap?.size ?? 0;
227
- return Promise.resolve(proposalCount >= MAX_PROPOSALS_PER_SLOT);
228
- }
229
-
230
- public hasReachedAttestationCap(slot: SlotNumber, proposalId: string, committeeSize: number): Promise<boolean> {
231
- const limit = committeeSize + ATTESTATION_CAP_BUFFER;
232
- const count = this.attestations.get(slot)?.get(proposalId)?.size ?? 0;
233
- return Promise.resolve(limit <= 0 || count >= limit);
234
- }
235
-
236
- public async canAddProposal(block: BlockProposal): Promise<boolean> {
237
- return this.proposals.has(block.archive.toString()) || !(await this.hasReachedProposalCap(block.slotNumber));
238
- }
239
-
240
- public async canAddAttestation(attestation: BlockAttestation, committeeSize: number): Promise<boolean> {
241
- const sender = attestation.getSender();
242
- const slot = attestation.payload.header.slotNumber;
243
- const pid = attestation.archive.toString();
244
- return (
245
- !!sender &&
246
- ((this.attestations.get(slot)?.get(pid)?.has(sender.toString()) ?? false) ||
247
- !(await this.hasReachedAttestationCap(slot, pid, committeeSize)))
248
- );
249
- }
250
241
  }
251
242
 
252
- /**
253
- * Get Slot or Default
254
- *
255
- * Fetch the slot mapping, if it does not exist, then create a mapping and return it
256
- * @param map - The map to fetch from
257
- * @param slot - The slot to fetch
258
- * @returns The slot mapping
259
- */
260
- function getSlotOrDefault(
243
+ // Checkpoint attestation helper functions
244
+
245
+ function getCheckpointSlotOrDefault(
261
246
  // eslint-disable-next-line aztec-custom/no-non-primitive-in-collections
262
- map: Map<SlotNumber, Map<string, Map<string, BlockAttestation>>>,
247
+ map: Map<SlotNumber, Map<string, Map<string, CheckpointAttestation>>>,
263
248
  slot: SlotNumber,
264
- ): Map<string, Map<string, BlockAttestation>> {
249
+ ): Map<string, Map<string, CheckpointAttestation>> {
265
250
  if (!map.has(slot)) {
266
- map.set(slot, new Map<string, Map<string, BlockAttestation>>());
251
+ map.set(slot, new Map<string, Map<string, CheckpointAttestation>>());
267
252
  }
268
253
  return map.get(slot)!;
269
254
  }
270
255
 
271
- /**
272
- * Get Proposal or Default
273
- *
274
- * Fetch the proposal mapping, if it does not exist, then create a mapping and return it
275
- * @param map - The map to fetch from
276
- * @param proposalId - The proposal id to fetch
277
- * @returns The proposal mapping
278
- */
279
- function getProposalOrDefault(
280
- map: Map<string, Map<string, BlockAttestation>>,
256
+ function getCheckpointProposalOrDefault(
257
+ map: Map<string, Map<string, CheckpointAttestation>>,
281
258
  proposalId: string,
282
- ): Map<string, BlockAttestation> {
259
+ ): Map<string, CheckpointAttestation> {
283
260
  if (!map.has(proposalId)) {
284
- map.set(proposalId, new Map<string, BlockAttestation>());
261
+ map.set(proposalId, new Map<string, CheckpointAttestation>());
285
262
  }
286
263
  return map.get(proposalId)!;
287
264
  }
@@ -1,7 +1,7 @@
1
1
  import type { Secp256k1Signer } from '@aztec/foundation/crypto/secp256k1-signer';
2
2
  import { Fr } from '@aztec/foundation/curves/bn254';
3
3
  import {
4
- BlockAttestation,
4
+ CheckpointAttestation,
5
5
  ConsensusPayload,
6
6
  SignatureDomainSeparator,
7
7
  getHashedSignaturePayloadEthSignedMessage,
@@ -20,26 +20,30 @@ export const generateAccount = (): LocalAccount => {
20
20
  return privateKeyToAccount(privateKey);
21
21
  };
22
22
 
23
- /** Mock Attestation
23
+ /** Mock Checkpoint Attestation
24
24
  *
25
- * @param signer A viem signer to create a signature
25
+ * @param signer A Secp256k1Signer to create a signature
26
26
  * @param slot The slot number the attestation is for
27
- * @returns A Block Attestation
27
+ * @param archive The archive root (defaults to random)
28
+ * @returns A Checkpoint Attestation
28
29
  */
29
- export const mockAttestation = (
30
+ export const mockCheckpointAttestation = (
30
31
  signer: Secp256k1Signer,
31
32
  slot: number = 0,
32
33
  archive: Fr = Fr.random(),
33
- ): BlockAttestation => {
34
+ ): CheckpointAttestation => {
34
35
  // Use arbitrary numbers for all other than slot
35
36
  const header = makeL2BlockHeader(1, 2, slot);
36
37
  const payload = new ConsensusPayload(header.toCheckpointHeader(), archive);
37
38
 
38
- const attestationHash = getHashedSignaturePayloadEthSignedMessage(payload, SignatureDomainSeparator.blockAttestation);
39
+ const attestationHash = getHashedSignaturePayloadEthSignedMessage(
40
+ payload,
41
+ SignatureDomainSeparator.checkpointAttestation,
42
+ );
39
43
  const attestationSignature = signer.sign(attestationHash);
40
44
 
41
- const proposalHash = getHashedSignaturePayloadEthSignedMessage(payload, SignatureDomainSeparator.blockProposal);
45
+ const proposalHash = getHashedSignaturePayloadEthSignedMessage(payload, SignatureDomainSeparator.checkpointProposal);
42
46
  const proposerSignature = signer.sign(proposalHash);
43
47
 
44
- return new BlockAttestation(payload, attestationSignature, proposerSignature);
48
+ return new CheckpointAttestation(payload, attestationSignature, proposerSignature);
45
49
  };
@@ -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';
2
+ import { type CheckpointAttestation, PeerErrorSeverity } from '@aztec/stdlib/p2p';
3
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(
@@ -28,7 +28,7 @@ export class FishermanAttestationValidator extends AttestationValidator {
28
28
  this.invalidAttestationCounter = meter.createUpDownCounter(Metrics.VALIDATOR_INVALID_ATTESTATION_RECEIVED_COUNT);
29
29
  }
30
30
 
31
- override async validate(message: BlockAttestation): Promise<PeerErrorSeverity | undefined> {
31
+ override async validate(message: CheckpointAttestation): Promise<PeerErrorSeverity | undefined> {
32
32
  // First run the standard validation
33
33
  const baseValidationResult = await super.validate(message);
34
34
  if (baseValidationResult !== undefined) {
@@ -49,11 +49,11 @@ export class FishermanAttestationValidator extends AttestationValidator {
49
49
  }
50
50
 
51
51
  const proposalId = message.archive.toString();
52
- const proposal = await this.attestationPool.getBlockProposal(proposalId);
52
+ const proposal = await this.attestationPool.getCheckpointProposal(proposalId);
53
53
 
54
54
  if (proposal) {
55
55
  // Compare the attestation payload with the proposal payload
56
- if (!message.payload.equals(proposal.payload)) {
56
+ if (!message.payload.equals(proposal)) {
57
57
  this.logger.error(
58
58
  `Attestation payload mismatch for slot ${slotNumberBigInt}! ` +
59
59
  `Attester ${attester.toString()} signed different data than the proposal.`,
@@ -63,7 +63,7 @@ export class FishermanAttestationValidator extends AttestationValidator {
63
63
  proposer: proposer.toString(),
64
64
  proposalArchive: proposal.archive.toString(),
65
65
  attestationArchive: message.archive.toString(),
66
- proposalHeader: proposal.payload.header.hash().toString(),
66
+ proposalHeader: proposal.checkpointHeader.hash().toString(),
67
67
  attestationHeader: message.payload.header.hash().toString(),
68
68
  },
69
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';