@aztec/validator-client 4.0.0-nightly.20260112 → 4.0.0-nightly.20260114

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 (42) hide show
  1. package/README.md +256 -0
  2. package/dest/block_proposal_handler.d.ts +22 -10
  3. package/dest/block_proposal_handler.d.ts.map +1 -1
  4. package/dest/block_proposal_handler.js +347 -76
  5. package/dest/checkpoint_builder.d.ts +70 -0
  6. package/dest/checkpoint_builder.d.ts.map +1 -0
  7. package/dest/checkpoint_builder.js +156 -0
  8. package/dest/config.d.ts +1 -1
  9. package/dest/config.d.ts.map +1 -1
  10. package/dest/config.js +10 -5
  11. package/dest/duties/validation_service.d.ts +26 -10
  12. package/dest/duties/validation_service.d.ts.map +1 -1
  13. package/dest/duties/validation_service.js +51 -21
  14. package/dest/factory.d.ts +10 -7
  15. package/dest/factory.d.ts.map +1 -1
  16. package/dest/factory.js +2 -2
  17. package/dest/index.d.ts +3 -1
  18. package/dest/index.d.ts.map +1 -1
  19. package/dest/index.js +2 -0
  20. package/dest/tx_validator/index.d.ts +3 -0
  21. package/dest/tx_validator/index.d.ts.map +1 -0
  22. package/dest/tx_validator/index.js +2 -0
  23. package/dest/tx_validator/nullifier_cache.d.ts +14 -0
  24. package/dest/tx_validator/nullifier_cache.d.ts.map +1 -0
  25. package/dest/tx_validator/nullifier_cache.js +24 -0
  26. package/dest/tx_validator/tx_validator_factory.d.ts +18 -0
  27. package/dest/tx_validator/tx_validator_factory.d.ts.map +1 -0
  28. package/dest/tx_validator/tx_validator_factory.js +53 -0
  29. package/dest/validator.d.ts +39 -15
  30. package/dest/validator.d.ts.map +1 -1
  31. package/dest/validator.js +318 -449
  32. package/package.json +16 -12
  33. package/src/block_proposal_handler.ts +273 -43
  34. package/src/checkpoint_builder.ts +276 -0
  35. package/src/config.ts +10 -5
  36. package/src/duties/validation_service.ts +79 -25
  37. package/src/factory.ts +14 -8
  38. package/src/index.ts +2 -0
  39. package/src/tx_validator/index.ts +2 -0
  40. package/src/tx_validator/nullifier_cache.ts +30 -0
  41. package/src/tx_validator/tx_validator_factory.ts +133 -0
  42. package/src/validator.ts +424 -94
package/dest/validator.js CHANGED
@@ -1,385 +1,16 @@
1
- function applyDecs2203RFactory() {
2
- function createAddInitializerMethod(initializers, decoratorFinishedRef) {
3
- return function addInitializer(initializer) {
4
- assertNotFinished(decoratorFinishedRef, "addInitializer");
5
- assertCallable(initializer, "An initializer");
6
- initializers.push(initializer);
7
- };
8
- }
9
- function memberDec(dec, name, desc, initializers, kind, isStatic, isPrivate, metadata, value) {
10
- var kindStr;
11
- switch(kind){
12
- case 1:
13
- kindStr = "accessor";
14
- break;
15
- case 2:
16
- kindStr = "method";
17
- break;
18
- case 3:
19
- kindStr = "getter";
20
- break;
21
- case 4:
22
- kindStr = "setter";
23
- break;
24
- default:
25
- kindStr = "field";
26
- }
27
- var ctx = {
28
- kind: kindStr,
29
- name: isPrivate ? "#" + name : name,
30
- static: isStatic,
31
- private: isPrivate,
32
- metadata: metadata
33
- };
34
- var decoratorFinishedRef = {
35
- v: false
36
- };
37
- ctx.addInitializer = createAddInitializerMethod(initializers, decoratorFinishedRef);
38
- var get, set;
39
- if (kind === 0) {
40
- if (isPrivate) {
41
- get = desc.get;
42
- set = desc.set;
43
- } else {
44
- get = function() {
45
- return this[name];
46
- };
47
- set = function(v) {
48
- this[name] = v;
49
- };
50
- }
51
- } else if (kind === 2) {
52
- get = function() {
53
- return desc.value;
54
- };
55
- } else {
56
- if (kind === 1 || kind === 3) {
57
- get = function() {
58
- return desc.get.call(this);
59
- };
60
- }
61
- if (kind === 1 || kind === 4) {
62
- set = function(v) {
63
- desc.set.call(this, v);
64
- };
65
- }
66
- }
67
- ctx.access = get && set ? {
68
- get: get,
69
- set: set
70
- } : get ? {
71
- get: get
72
- } : {
73
- set: set
74
- };
75
- try {
76
- return dec(value, ctx);
77
- } finally{
78
- decoratorFinishedRef.v = true;
79
- }
80
- }
81
- function assertNotFinished(decoratorFinishedRef, fnName) {
82
- if (decoratorFinishedRef.v) {
83
- throw new Error("attempted to call " + fnName + " after decoration was finished");
84
- }
85
- }
86
- function assertCallable(fn, hint) {
87
- if (typeof fn !== "function") {
88
- throw new TypeError(hint + " must be a function");
89
- }
90
- }
91
- function assertValidReturnValue(kind, value) {
92
- var type = typeof value;
93
- if (kind === 1) {
94
- if (type !== "object" || value === null) {
95
- throw new TypeError("accessor decorators must return an object with get, set, or init properties or void 0");
96
- }
97
- if (value.get !== undefined) {
98
- assertCallable(value.get, "accessor.get");
99
- }
100
- if (value.set !== undefined) {
101
- assertCallable(value.set, "accessor.set");
102
- }
103
- if (value.init !== undefined) {
104
- assertCallable(value.init, "accessor.init");
105
- }
106
- } else if (type !== "function") {
107
- var hint;
108
- if (kind === 0) {
109
- hint = "field";
110
- } else if (kind === 10) {
111
- hint = "class";
112
- } else {
113
- hint = "method";
114
- }
115
- throw new TypeError(hint + " decorators must return a function or void 0");
116
- }
117
- }
118
- function applyMemberDec(ret, base, decInfo, name, kind, isStatic, isPrivate, initializers, metadata) {
119
- var decs = decInfo[0];
120
- var desc, init, value;
121
- if (isPrivate) {
122
- if (kind === 0 || kind === 1) {
123
- desc = {
124
- get: decInfo[3],
125
- set: decInfo[4]
126
- };
127
- } else if (kind === 3) {
128
- desc = {
129
- get: decInfo[3]
130
- };
131
- } else if (kind === 4) {
132
- desc = {
133
- set: decInfo[3]
134
- };
135
- } else {
136
- desc = {
137
- value: decInfo[3]
138
- };
139
- }
140
- } else if (kind !== 0) {
141
- desc = Object.getOwnPropertyDescriptor(base, name);
142
- }
143
- if (kind === 1) {
144
- value = {
145
- get: desc.get,
146
- set: desc.set
147
- };
148
- } else if (kind === 2) {
149
- value = desc.value;
150
- } else if (kind === 3) {
151
- value = desc.get;
152
- } else if (kind === 4) {
153
- value = desc.set;
154
- }
155
- var newValue, get, set;
156
- if (typeof decs === "function") {
157
- newValue = memberDec(decs, name, desc, initializers, kind, isStatic, isPrivate, metadata, value);
158
- if (newValue !== void 0) {
159
- assertValidReturnValue(kind, newValue);
160
- if (kind === 0) {
161
- init = newValue;
162
- } else if (kind === 1) {
163
- init = newValue.init;
164
- get = newValue.get || value.get;
165
- set = newValue.set || value.set;
166
- value = {
167
- get: get,
168
- set: set
169
- };
170
- } else {
171
- value = newValue;
172
- }
173
- }
174
- } else {
175
- for(var i = decs.length - 1; i >= 0; i--){
176
- var dec = decs[i];
177
- newValue = memberDec(dec, name, desc, initializers, kind, isStatic, isPrivate, metadata, value);
178
- if (newValue !== void 0) {
179
- assertValidReturnValue(kind, newValue);
180
- var newInit;
181
- if (kind === 0) {
182
- newInit = newValue;
183
- } else if (kind === 1) {
184
- newInit = newValue.init;
185
- get = newValue.get || value.get;
186
- set = newValue.set || value.set;
187
- value = {
188
- get: get,
189
- set: set
190
- };
191
- } else {
192
- value = newValue;
193
- }
194
- if (newInit !== void 0) {
195
- if (init === void 0) {
196
- init = newInit;
197
- } else if (typeof init === "function") {
198
- init = [
199
- init,
200
- newInit
201
- ];
202
- } else {
203
- init.push(newInit);
204
- }
205
- }
206
- }
207
- }
208
- }
209
- if (kind === 0 || kind === 1) {
210
- if (init === void 0) {
211
- init = function(instance, init) {
212
- return init;
213
- };
214
- } else if (typeof init !== "function") {
215
- var ownInitializers = init;
216
- init = function(instance, init) {
217
- var value = init;
218
- for(var i = 0; i < ownInitializers.length; i++){
219
- value = ownInitializers[i].call(instance, value);
220
- }
221
- return value;
222
- };
223
- } else {
224
- var originalInitializer = init;
225
- init = function(instance, init) {
226
- return originalInitializer.call(instance, init);
227
- };
228
- }
229
- ret.push(init);
230
- }
231
- if (kind !== 0) {
232
- if (kind === 1) {
233
- desc.get = value.get;
234
- desc.set = value.set;
235
- } else if (kind === 2) {
236
- desc.value = value;
237
- } else if (kind === 3) {
238
- desc.get = value;
239
- } else if (kind === 4) {
240
- desc.set = value;
241
- }
242
- if (isPrivate) {
243
- if (kind === 1) {
244
- ret.push(function(instance, args) {
245
- return value.get.call(instance, args);
246
- });
247
- ret.push(function(instance, args) {
248
- return value.set.call(instance, args);
249
- });
250
- } else if (kind === 2) {
251
- ret.push(value);
252
- } else {
253
- ret.push(function(instance, args) {
254
- return value.call(instance, args);
255
- });
256
- }
257
- } else {
258
- Object.defineProperty(base, name, desc);
259
- }
260
- }
261
- }
262
- function applyMemberDecs(Class, decInfos, metadata) {
263
- var ret = [];
264
- var protoInitializers;
265
- var staticInitializers;
266
- var existingProtoNonFields = new Map();
267
- var existingStaticNonFields = new Map();
268
- for(var i = 0; i < decInfos.length; i++){
269
- var decInfo = decInfos[i];
270
- if (!Array.isArray(decInfo)) continue;
271
- var kind = decInfo[1];
272
- var name = decInfo[2];
273
- var isPrivate = decInfo.length > 3;
274
- var isStatic = kind >= 5;
275
- var base;
276
- var initializers;
277
- if (isStatic) {
278
- base = Class;
279
- kind = kind - 5;
280
- staticInitializers = staticInitializers || [];
281
- initializers = staticInitializers;
282
- } else {
283
- base = Class.prototype;
284
- protoInitializers = protoInitializers || [];
285
- initializers = protoInitializers;
286
- }
287
- if (kind !== 0 && !isPrivate) {
288
- var existingNonFields = isStatic ? existingStaticNonFields : existingProtoNonFields;
289
- var existingKind = existingNonFields.get(name) || 0;
290
- if (existingKind === true || existingKind === 3 && kind !== 4 || existingKind === 4 && kind !== 3) {
291
- throw new Error("Attempted to decorate a public method/accessor that has the same name as a previously decorated public method/accessor. This is not currently supported by the decorators plugin. Property name was: " + name);
292
- } else if (!existingKind && kind > 2) {
293
- existingNonFields.set(name, kind);
294
- } else {
295
- existingNonFields.set(name, true);
296
- }
297
- }
298
- applyMemberDec(ret, base, decInfo, name, kind, isStatic, isPrivate, initializers, metadata);
299
- }
300
- pushInitializers(ret, protoInitializers);
301
- pushInitializers(ret, staticInitializers);
302
- return ret;
303
- }
304
- function pushInitializers(ret, initializers) {
305
- if (initializers) {
306
- ret.push(function(instance) {
307
- for(var i = 0; i < initializers.length; i++){
308
- initializers[i].call(instance);
309
- }
310
- return instance;
311
- });
312
- }
313
- }
314
- function applyClassDecs(targetClass, classDecs, metadata) {
315
- if (classDecs.length > 0) {
316
- var initializers = [];
317
- var newClass = targetClass;
318
- var name = targetClass.name;
319
- for(var i = classDecs.length - 1; i >= 0; i--){
320
- var decoratorFinishedRef = {
321
- v: false
322
- };
323
- try {
324
- var nextNewClass = classDecs[i](newClass, {
325
- kind: "class",
326
- name: name,
327
- addInitializer: createAddInitializerMethod(initializers, decoratorFinishedRef),
328
- metadata
329
- });
330
- } finally{
331
- decoratorFinishedRef.v = true;
332
- }
333
- if (nextNewClass !== undefined) {
334
- assertValidReturnValue(10, nextNewClass);
335
- newClass = nextNewClass;
336
- }
337
- }
338
- return [
339
- defineMetadata(newClass, metadata),
340
- function() {
341
- for(var i = 0; i < initializers.length; i++){
342
- initializers[i].call(newClass);
343
- }
344
- }
345
- ];
346
- }
347
- }
348
- function defineMetadata(Class, metadata) {
349
- return Object.defineProperty(Class, Symbol.metadata || Symbol.for("Symbol.metadata"), {
350
- configurable: true,
351
- enumerable: true,
352
- value: metadata
353
- });
354
- }
355
- return function applyDecs2203R(targetClass, memberDecs, classDecs, parentClass) {
356
- if (parentClass !== void 0) {
357
- var parentMetadata = parentClass[Symbol.metadata || Symbol.for("Symbol.metadata")];
358
- }
359
- var metadata = Object.create(parentMetadata === void 0 ? null : parentMetadata);
360
- var e = applyMemberDecs(targetClass, memberDecs, metadata);
361
- if (!classDecs.length) defineMetadata(targetClass, metadata);
362
- return {
363
- e: e,
364
- get c () {
365
- return applyClassDecs(targetClass, classDecs, metadata);
366
- }
367
- };
368
- };
369
- }
370
- function _apply_decs_2203_r(targetClass, memberDecs, classDecs, parentClass) {
371
- return (_apply_decs_2203_r = applyDecs2203RFactory())(targetClass, memberDecs, classDecs, parentClass);
372
- }
373
- var _dec, _initProto;
374
1
  import { getBlobsPerL1Block } from '@aztec/blob-lib';
2
+ import { BlockNumber } from '@aztec/foundation/branded-types';
3
+ import { TimeoutError } from '@aztec/foundation/error';
375
4
  import { createLogger } from '@aztec/foundation/log';
5
+ import { retryUntil } from '@aztec/foundation/retry';
376
6
  import { RunningPromise } from '@aztec/foundation/running-promise';
377
7
  import { sleep } from '@aztec/foundation/sleep';
378
8
  import { DateProvider } from '@aztec/foundation/timer';
379
9
  import { AuthRequest, AuthResponse, BlockProposalValidator, ReqRespSubProtocol } from '@aztec/p2p';
380
10
  import { OffenseType, WANT_TO_SLASH_EVENT } from '@aztec/slasher';
11
+ import { getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
381
12
  import { AttestationTimeoutError } from '@aztec/stdlib/validators';
382
- import { Attributes, getTelemetryClient, trackSpan } from '@aztec/telemetry-client';
13
+ import { getTelemetryClient } from '@aztec/telemetry-client';
383
14
  import { EventEmitter } from 'events';
384
15
  import { BlockProposalHandler } from './block_proposal_handler.js';
385
16
  import { ValidationService } from './duties/validation_service.js';
@@ -393,10 +24,6 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
393
24
  'state_mismatch',
394
25
  'failed_txs'
395
26
  ];
396
- _dec = trackSpan('validator.attestToProposal', (proposal, proposalSender)=>({
397
- [Attributes.BLOCK_HASH]: proposal.payload.header.hash.toString(),
398
- [Attributes.PEER_ID]: proposalSender.toString()
399
- }));
400
27
  /**
401
28
  * Validator Client
402
29
  */ export class ValidatorClient extends EventEmitter {
@@ -404,18 +31,13 @@ _dec = trackSpan('validator.attestToProposal', (proposal, proposalSender)=>({
404
31
  epochCache;
405
32
  p2pClient;
406
33
  blockProposalHandler;
34
+ blockSource;
35
+ checkpointsBuilder;
36
+ worldState;
37
+ l1ToL2MessageSource;
407
38
  config;
408
39
  blobClient;
409
40
  dateProvider;
410
- static{
411
- ({ e: [_initProto] } = _apply_decs_2203_r(this, [
412
- [
413
- _dec,
414
- 2,
415
- "attestToProposal"
416
- ]
417
- ], []));
418
- }
419
41
  tracer;
420
42
  validationService;
421
43
  metrics;
@@ -427,8 +49,12 @@ _dec = trackSpan('validator.attestToProposal', (proposal, proposalSender)=>({
427
49
  lastEpochForCommitteeUpdateLoop;
428
50
  epochCacheUpdateLoop;
429
51
  proposersOfInvalidBlocks;
430
- constructor(keyStore, epochCache, p2pClient, blockProposalHandler, config, blobClient, dateProvider = new DateProvider(), telemetry = getTelemetryClient(), log = createLogger('validator')){
431
- super(), this.keyStore = keyStore, this.epochCache = epochCache, this.p2pClient = p2pClient, this.blockProposalHandler = blockProposalHandler, this.config = config, this.blobClient = blobClient, this.dateProvider = dateProvider, this.hasRegisteredHandlers = (_initProto(this), false), this.proposersOfInvalidBlocks = new Set();
52
+ // TODO(palla/mbps): Remove this once checkpoint validation is stable and we can validate all blocks properly.
53
+ // Tracks slots for which we have successfully validated a block proposal, so we can attest to checkpoint proposals for those slots.
54
+ // eslint-disable-next-line aztec-custom/no-non-primitive-in-collections
55
+ validatedBlockSlots;
56
+ constructor(keyStore, epochCache, p2pClient, blockProposalHandler, blockSource, checkpointsBuilder, worldState, l1ToL2MessageSource, config, blobClient, dateProvider = new DateProvider(), telemetry = getTelemetryClient(), log = createLogger('validator')){
57
+ super(), this.keyStore = keyStore, this.epochCache = epochCache, this.p2pClient = p2pClient, this.blockProposalHandler = blockProposalHandler, this.blockSource = blockSource, this.checkpointsBuilder = checkpointsBuilder, this.worldState = worldState, this.l1ToL2MessageSource = l1ToL2MessageSource, this.config = config, this.blobClient = blobClient, this.dateProvider = dateProvider, this.hasRegisteredHandlers = false, this.proposersOfInvalidBlocks = new Set(), this.validatedBlockSlots = new Set();
432
58
  // Create child logger with fisherman prefix if in fisherman mode
433
59
  this.log = config.fishermanMode ? log.createChild('[FISHERMAN]') : log;
434
60
  this.tracer = telemetry.getTracer('Validator');
@@ -482,13 +108,13 @@ _dec = trackSpan('validator.attestToProposal', (proposal, proposalSender)=>({
482
108
  this.log.error(`Error updating epoch committee`, err);
483
109
  }
484
110
  }
485
- static new(config, blockBuilder, epochCache, p2pClient, blockSource, l1ToL2MessageSource, txProvider, keyStoreManager, blobClient, dateProvider = new DateProvider(), telemetry = getTelemetryClient()) {
111
+ static new(config, checkpointsBuilder, worldState, epochCache, p2pClient, blockSource, l1ToL2MessageSource, txProvider, keyStoreManager, blobClient, dateProvider = new DateProvider(), telemetry = getTelemetryClient()) {
486
112
  const metrics = new ValidatorMetrics(telemetry);
487
113
  const blockProposalValidator = new BlockProposalValidator(epochCache, {
488
114
  txsPermitted: !config.disableTransactions
489
115
  });
490
- const blockProposalHandler = new BlockProposalHandler(blockBuilder, blockSource, l1ToL2MessageSource, txProvider, blockProposalValidator, config, metrics, dateProvider, telemetry);
491
- const validator = new ValidatorClient(NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager), epochCache, p2pClient, blockProposalHandler, config, blobClient, dateProvider, telemetry);
116
+ const blockProposalHandler = new BlockProposalHandler(checkpointsBuilder, worldState, blockSource, l1ToL2MessageSource, txProvider, blockProposalValidator, epochCache, config, metrics, dateProvider, telemetry);
117
+ const validator = new ValidatorClient(NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager), epochCache, p2pClient, blockProposalHandler, blockSource, checkpointsBuilder, worldState, l1ToL2MessageSource, config, blobClient, dateProvider, telemetry);
492
118
  return validator;
493
119
  }
494
120
  getValidatorAddresses() {
@@ -497,10 +123,6 @@ _dec = trackSpan('validator.attestToProposal', (proposal, proposalSender)=>({
497
123
  getBlockProposalHandler() {
498
124
  return this.blockProposalHandler;
499
125
  }
500
- // Proxy method for backwards compatibility with tests
501
- reExecuteTransactions(proposal, blockNumber, txs, l1ToL2Messages) {
502
- return this.blockProposalHandler.reexecuteTransactions(proposal, blockNumber, txs, l1ToL2Messages);
503
- }
504
126
  signWithAddress(addr, msg) {
505
127
  return this.keyStore.signTypedDataWithAddress(addr, msg);
506
128
  }
@@ -541,41 +163,50 @@ _dec = trackSpan('validator.attestToProposal', (proposal, proposalSender)=>({
541
163
  if (!this.hasRegisteredHandlers) {
542
164
  this.hasRegisteredHandlers = true;
543
165
  this.log.debug(`Registering validator handlers for p2p client`);
544
- const handler = (block, proposalSender)=>this.attestToProposal(block, proposalSender);
545
- this.p2pClient.registerBlockProposalHandler(handler);
166
+ // Block proposal handler - validates but does NOT attest (validators only attest to checkpoints)
167
+ const blockHandler = (block, proposalSender)=>this.validateBlockProposal(block, proposalSender);
168
+ this.p2pClient.registerBlockProposalHandler(blockHandler);
169
+ // Checkpoint proposal handler - validates and creates attestations
170
+ // The checkpoint is received as CheckpointProposalCore since the lastBlock is extracted
171
+ // and processed separately via the block handler above.
172
+ const checkpointHandler = (checkpoint, proposalSender)=>this.attestToCheckpointProposal(checkpoint, proposalSender);
173
+ this.p2pClient.registerCheckpointProposalHandler(checkpointHandler);
546
174
  const myAddresses = this.getValidatorAddresses();
547
175
  this.p2pClient.registerThisValidatorAddresses(myAddresses);
548
176
  await this.p2pClient.addReqRespSubProtocol(ReqRespSubProtocol.AUTH, this.handleAuthRequest.bind(this));
549
177
  }
550
178
  }
551
- async attestToProposal(proposal, proposalSender) {
179
+ /**
180
+ * Validate a block proposal from a peer.
181
+ * Note: Validators do NOT attest to individual blocks - attestations are only for checkpoint proposals.
182
+ * @returns true if the proposal is valid, false otherwise
183
+ */ async validateBlockProposal(proposal, proposalSender) {
552
184
  const slotNumber = proposal.slotNumber;
553
185
  const proposer = proposal.getSender();
554
186
  // Reject proposals with invalid signatures
555
187
  if (!proposer) {
556
- this.log.warn(`Received proposal with invalid signature for slot ${slotNumber}`);
557
- return undefined;
188
+ this.log.warn(`Received block proposal with invalid signature for slot ${slotNumber}`);
189
+ return false;
558
190
  }
559
- // Check that I have any address in current committee before attesting
191
+ // Check if we're in the committee (for metrics purposes)
560
192
  const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
561
193
  const partOfCommittee = inCommittee.length > 0;
562
194
  const proposalInfo = {
563
195
  ...proposal.toBlockInfo(),
564
196
  proposer: proposer.toString()
565
197
  };
566
- this.log.info(`Received proposal for slot ${slotNumber}`, {
198
+ this.log.info(`Received block proposal for slot ${slotNumber}`, {
567
199
  ...proposalInfo,
568
200
  txHashes: proposal.txHashes.map((t)=>t.toString()),
569
201
  fishermanMode: this.config.fishermanMode || false
570
202
  });
571
- // Reexecute txs if we are part of the committee so we can attest, or if slashing is enabled so we can slash
572
- // invalid proposals even when not in the committee, or if we are configured to always reexecute for monitoring purposes.
203
+ // Reexecute txs if we are part of the committee, or if slashing is enabled, or if we are configured to always reexecute.
573
204
  // In fisherman mode, we always reexecute to validate proposals.
574
205
  const { validatorReexecute, slashBroadcastedInvalidBlockPenalty, alwaysReexecuteBlockProposals, fishermanMode } = this.config;
575
206
  const shouldReexecute = fishermanMode || slashBroadcastedInvalidBlockPenalty > 0n && validatorReexecute || partOfCommittee && validatorReexecute || alwaysReexecuteBlockProposals || this.blobClient.canUpload();
576
207
  const validationResult = await this.blockProposalHandler.handleBlockProposal(proposal, proposalSender, !!shouldReexecute);
577
208
  if (!validationResult.isValid) {
578
- this.log.warn(`Proposal validation failed: ${validationResult.reason}`, proposalInfo);
209
+ this.log.warn(`Block proposal validation failed: ${validationResult.reason}`, proposalInfo);
579
210
  const reason = validationResult.reason || 'unknown';
580
211
  // Classify failure reason: bad proposal vs node issue
581
212
  const badProposalReasons = [
@@ -588,7 +219,7 @@ _dec = trackSpan('validator.attestToProposal', (proposal, proposalSender)=>({
588
219
  if (badProposalReasons.includes(reason)) {
589
220
  this.metrics.incFailedAttestationsBadProposal(1, reason, partOfCommittee);
590
221
  } else {
591
- // Node issues so we can't attest
222
+ // Node issues so we can't validate
592
223
  this.metrics.incFailedAttestationsNodeIssue(1, reason, partOfCommittee);
593
224
  }
594
225
  // Slash invalid block proposals (can happen even when not in committee)
@@ -596,35 +227,79 @@ _dec = trackSpan('validator.attestToProposal', (proposal, proposalSender)=>({
596
227
  this.log.warn(`Slashing proposer for invalid block proposal`, proposalInfo);
597
228
  this.slashInvalidBlock(proposal);
598
229
  }
230
+ return false;
231
+ }
232
+ this.log.info(`Validated block proposal for slot ${slotNumber}`, {
233
+ ...proposalInfo,
234
+ inCommittee: partOfCommittee,
235
+ fishermanMode: this.config.fishermanMode || false
236
+ });
237
+ // TODO(palla/mbps): Remove this once checkpoint validation is stable.
238
+ // Track that we successfully validated a block for this slot, so we can attest to checkpoint proposals for it.
239
+ this.validatedBlockSlots.add(slotNumber);
240
+ return true;
241
+ }
242
+ /**
243
+ * Validate and attest to a checkpoint proposal from a peer.
244
+ * The proposal is received as CheckpointProposalCore (without lastBlock) since
245
+ * the lastBlock is extracted and processed separately via the block handler.
246
+ * @returns Checkpoint attestations if valid, undefined otherwise
247
+ */ async attestToCheckpointProposal(proposal, _proposalSender) {
248
+ const slotNumber = proposal.slotNumber;
249
+ const proposer = proposal.getSender();
250
+ // Reject proposals with invalid signatures
251
+ if (!proposer) {
252
+ this.log.warn(`Received checkpoint proposal with invalid signature for slot ${slotNumber}`);
599
253
  return undefined;
600
254
  }
601
255
  // Check that I have any address in current committee before attesting
256
+ const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
257
+ const partOfCommittee = inCommittee.length > 0;
258
+ const proposalInfo = {
259
+ slotNumber,
260
+ archive: proposal.archive.toString(),
261
+ proposer: proposer.toString(),
262
+ txCount: proposal.txHashes.length
263
+ };
264
+ this.log.info(`Received checkpoint proposal for slot ${slotNumber}`, {
265
+ ...proposalInfo,
266
+ txHashes: proposal.txHashes.map((t)=>t.toString()),
267
+ fishermanMode: this.config.fishermanMode || false
268
+ });
269
+ // TODO(palla/mbps): Remove this once checkpoint validation is stable.
270
+ // Check that we have successfully validated a block for this slot before attesting to the checkpoint.
271
+ if (!this.validatedBlockSlots.has(slotNumber)) {
272
+ this.log.warn(`No validated block found for slot ${slotNumber}, refusing to attest to checkpoint`, proposalInfo);
273
+ return undefined;
274
+ }
275
+ // Validate the checkpoint proposal before attesting (unless skipCheckpointProposalValidation is set)
276
+ // TODO(palla/mbps): Change default to false once checkpoint validation is stable.
277
+ if (this.config.skipCheckpointProposalValidation !== false) {
278
+ this.log.verbose(`Skipping checkpoint proposal validation for slot ${slotNumber}`, proposalInfo);
279
+ } else {
280
+ const validationResult = await this.validateCheckpointProposal(proposal, proposalInfo);
281
+ if (!validationResult.isValid) {
282
+ this.log.warn(`Checkpoint proposal validation failed: ${validationResult.reason}`, proposalInfo);
283
+ return undefined;
284
+ }
285
+ }
286
+ // Upload blobs to filestore if we can (fire and forget)
287
+ if (this.blobClient.canUpload()) {
288
+ void this.uploadBlobsForCheckpoint(proposal, proposalInfo);
289
+ }
290
+ // Check that I have any address in current committee before attesting
602
291
  // In fisherman mode, we still create attestations for validation even if not in committee
603
292
  if (!partOfCommittee && !this.config.fishermanMode) {
604
293
  this.log.verbose(`No validator in the current committee, skipping attestation`, proposalInfo);
605
294
  return undefined;
606
295
  }
607
296
  // Provided all of the above checks pass, we can attest to the proposal
608
- this.log.info(`${partOfCommittee ? 'Attesting to' : 'Validated'} proposal for slot ${slotNumber}`, {
297
+ this.log.info(`${partOfCommittee ? 'Attesting to' : 'Validated'} checkpoint proposal for slot ${slotNumber}`, {
609
298
  ...proposalInfo,
610
299
  inCommittee: partOfCommittee,
611
300
  fishermanMode: this.config.fishermanMode || false
612
301
  });
613
302
  this.metrics.incSuccessfulAttestations(inCommittee.length);
614
- // Upload blobs to filestore after successful re-execution (fire-and-forget)
615
- if (validationResult.reexecutionResult?.block && this.blobClient.canUpload()) {
616
- void Promise.resolve().then(async ()=>{
617
- try {
618
- const blobFields = validationResult.reexecutionResult.block.getCheckpointBlobFields();
619
- const blobs = getBlobsPerL1Block(blobFields);
620
- await this.blobClient.sendBlobsToFilestore(blobs);
621
- this.log.debug(`Uploaded ${blobs.length} blobs to filestore from re-execution`, proposalInfo);
622
- } catch (err) {
623
- this.log.warn(`Failed to upload blobs from re-execution`, err);
624
- }
625
- });
626
- }
627
- // If the above function does not throw an error, then we can attest to the proposal
628
303
  // Determine which validators should attest
629
304
  let attestors;
630
305
  if (partOfCommittee) {
@@ -641,13 +316,214 @@ _dec = trackSpan('validator.attestToProposal', (proposal, proposalSender)=>({
641
316
  }
642
317
  if (this.config.fishermanMode) {
643
318
  // bail out early and don't save attestations to the pool in fisherman mode
644
- this.log.info(`Creating attestations for proposal for slot ${slotNumber}`, {
319
+ this.log.info(`Creating checkpoint attestations for slot ${slotNumber}`, {
645
320
  ...proposalInfo,
646
321
  attestors: attestors.map((a)=>a.toString())
647
322
  });
648
323
  return undefined;
649
324
  }
650
- return this.createBlockAttestationsFromProposal(proposal, attestors);
325
+ return this.createCheckpointAttestationsFromProposal(proposal, attestors);
326
+ }
327
+ async createCheckpointAttestationsFromProposal(proposal, attestors = []) {
328
+ const attestations = await this.validationService.attestToCheckpointProposal(proposal, attestors);
329
+ await this.p2pClient.addCheckpointAttestations(attestations);
330
+ return attestations;
331
+ }
332
+ /**
333
+ * Validates a checkpoint proposal by building the full checkpoint and comparing it with the proposal.
334
+ * @returns Validation result with isValid flag and reason if invalid.
335
+ */ async validateCheckpointProposal(proposal, proposalInfo) {
336
+ const slot = proposal.slotNumber;
337
+ const timeoutSeconds = 10;
338
+ // Wait for last block to sync by archive
339
+ let lastBlockHeader;
340
+ try {
341
+ lastBlockHeader = await retryUntil(async ()=>{
342
+ await this.blockSource.syncImmediate();
343
+ return this.blockSource.getBlockHeaderByArchive(proposal.archive);
344
+ }, `waiting for block with archive ${proposal.archive.toString()} for slot ${slot}`, timeoutSeconds, 0.5);
345
+ } catch (err) {
346
+ if (err instanceof TimeoutError) {
347
+ this.log.warn(`Timed out waiting for block with archive matching checkpoint proposal`, proposalInfo);
348
+ return {
349
+ isValid: false,
350
+ reason: 'last_block_not_found'
351
+ };
352
+ }
353
+ this.log.error(`Error fetching last block for checkpoint proposal`, err, proposalInfo);
354
+ return {
355
+ isValid: false,
356
+ reason: 'block_fetch_error'
357
+ };
358
+ }
359
+ if (!lastBlockHeader) {
360
+ this.log.warn(`Last block not found for checkpoint proposal`, proposalInfo);
361
+ return {
362
+ isValid: false,
363
+ reason: 'last_block_not_found'
364
+ };
365
+ }
366
+ // Get the last full block to determine checkpoint number
367
+ const lastBlock = await this.blockSource.getL2BlockNew(lastBlockHeader.getBlockNumber());
368
+ if (!lastBlock) {
369
+ this.log.warn(`Last block ${lastBlockHeader.getBlockNumber()} not found`, proposalInfo);
370
+ return {
371
+ isValid: false,
372
+ reason: 'last_block_not_found'
373
+ };
374
+ }
375
+ const checkpointNumber = lastBlock.checkpointNumber;
376
+ // Get all full blocks for the slot and checkpoint
377
+ const blocks = await this.getBlocksForSlot(slot, lastBlockHeader, checkpointNumber);
378
+ if (blocks.length === 0) {
379
+ this.log.warn(`No blocks found for slot ${slot}`, proposalInfo);
380
+ return {
381
+ isValid: false,
382
+ reason: 'no_blocks_for_slot'
383
+ };
384
+ }
385
+ this.log.debug(`Found ${blocks.length} blocks for slot ${slot}`, {
386
+ ...proposalInfo,
387
+ blockNumbers: blocks.map((b)=>b.number)
388
+ });
389
+ // Get checkpoint constants from first block
390
+ const firstBlock = blocks[0];
391
+ const constants = this.extractCheckpointConstants(firstBlock);
392
+ // Get L1-to-L2 messages for this checkpoint
393
+ const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
394
+ // Compute the previous checkpoint out hashes for the epoch.
395
+ // TODO: There can be a more efficient way to get the previous checkpoint out hashes without having to fetch the
396
+ // actual checkpoints and the blocks/txs in them.
397
+ const epoch = getEpochAtSlot(slot, this.epochCache.getL1Constants());
398
+ const previousCheckpoints = (await this.blockSource.getCheckpointsForEpoch(epoch)).filter((b)=>b.number < checkpointNumber).sort((a, b)=>a.number - b.number);
399
+ const previousCheckpointOutHashes = previousCheckpoints.map((c)=>c.getCheckpointOutHash());
400
+ // Fork world state at the block before the first block
401
+ const parentBlockNumber = BlockNumber(firstBlock.number - 1);
402
+ const fork = await this.worldState.fork(parentBlockNumber);
403
+ try {
404
+ // Create checkpoint builder with all existing blocks
405
+ const checkpointBuilder = await this.checkpointsBuilder.openCheckpoint(checkpointNumber, constants, l1ToL2Messages, previousCheckpointOutHashes, fork, blocks);
406
+ // Complete the checkpoint to get computed values
407
+ const computedCheckpoint = await checkpointBuilder.completeCheckpoint();
408
+ // Compare checkpoint header with proposal
409
+ if (!computedCheckpoint.header.equals(proposal.checkpointHeader)) {
410
+ this.log.warn(`Checkpoint header mismatch`, {
411
+ ...proposalInfo,
412
+ computed: computedCheckpoint.header.toInspect(),
413
+ proposal: proposal.checkpointHeader.toInspect()
414
+ });
415
+ return {
416
+ isValid: false,
417
+ reason: 'checkpoint_header_mismatch'
418
+ };
419
+ }
420
+ // Compare archive root with proposal
421
+ if (!computedCheckpoint.archive.root.equals(proposal.archive)) {
422
+ this.log.warn(`Archive root mismatch`, {
423
+ ...proposalInfo,
424
+ computed: computedCheckpoint.archive.root.toString(),
425
+ proposal: proposal.archive.toString()
426
+ });
427
+ return {
428
+ isValid: false,
429
+ reason: 'archive_mismatch'
430
+ };
431
+ }
432
+ // Check that the accumulated out hash matches the value in the proposal.
433
+ const computedOutHash = computedCheckpoint.getCheckpointOutHash();
434
+ const proposalOutHash = proposal.checkpointHeader.epochOutHash;
435
+ if (!computedOutHash.equals(proposalOutHash)) {
436
+ this.log.warn(`Epoch out hash mismatch`, {
437
+ proposalOutHash: proposalOutHash.toString(),
438
+ computedOutHash: computedOutHash.toString(),
439
+ ...proposalInfo
440
+ });
441
+ return {
442
+ isValid: false,
443
+ reason: 'out_hash_mismatch'
444
+ };
445
+ }
446
+ this.log.verbose(`Checkpoint proposal validation successful for slot ${slot}`, proposalInfo);
447
+ return {
448
+ isValid: true
449
+ };
450
+ } finally{
451
+ await fork.close();
452
+ }
453
+ }
454
+ /**
455
+ * Get all full blocks for a given slot and checkpoint by walking backwards from the last block.
456
+ * Returns blocks in ascending order (earliest to latest).
457
+ * TODO(palla/mbps): Add getL2BlocksForSlot() to L2BlockSource interface for efficiency.
458
+ */ async getBlocksForSlot(slot, lastBlockHeader, checkpointNumber) {
459
+ const blocks = [];
460
+ let currentHeader = lastBlockHeader;
461
+ const { genesisArchiveRoot } = await this.blockSource.getGenesisValues();
462
+ while(currentHeader.getSlot() === slot){
463
+ const block = await this.blockSource.getL2BlockNew(currentHeader.getBlockNumber());
464
+ if (!block) {
465
+ this.log.warn(`Block ${currentHeader.getBlockNumber()} not found while getting blocks for slot ${slot}`);
466
+ break;
467
+ }
468
+ if (block.checkpointNumber !== checkpointNumber) {
469
+ break;
470
+ }
471
+ blocks.unshift(block);
472
+ const prevArchive = currentHeader.lastArchive.root;
473
+ if (prevArchive.equals(genesisArchiveRoot)) {
474
+ break;
475
+ }
476
+ const prevHeader = await this.blockSource.getBlockHeaderByArchive(prevArchive);
477
+ if (!prevHeader || prevHeader.getSlot() !== slot) {
478
+ break;
479
+ }
480
+ currentHeader = prevHeader;
481
+ }
482
+ return blocks;
483
+ }
484
+ /**
485
+ * Extract checkpoint global variables from a block.
486
+ */ extractCheckpointConstants(block) {
487
+ const gv = block.header.globalVariables;
488
+ return {
489
+ chainId: gv.chainId,
490
+ version: gv.version,
491
+ slotNumber: gv.slotNumber,
492
+ coinbase: gv.coinbase,
493
+ feeRecipient: gv.feeRecipient,
494
+ gasFees: gv.gasFees
495
+ };
496
+ }
497
+ /**
498
+ * Uploads blobs for a checkpoint to the filestore (fire and forget).
499
+ */ async uploadBlobsForCheckpoint(proposal, proposalInfo) {
500
+ try {
501
+ const lastBlockHeader = await this.blockSource.getBlockHeaderByArchive(proposal.archive);
502
+ if (!lastBlockHeader) {
503
+ this.log.warn(`Failed to get last block header for blob upload`, proposalInfo);
504
+ return;
505
+ }
506
+ // Get the last full block to determine checkpoint number
507
+ const lastBlock = await this.blockSource.getL2BlockNew(lastBlockHeader.getBlockNumber());
508
+ if (!lastBlock) {
509
+ this.log.warn(`Failed to get last block for blob upload`, proposalInfo);
510
+ return;
511
+ }
512
+ const blocks = await this.getBlocksForSlot(proposal.slotNumber, lastBlockHeader, lastBlock.checkpointNumber);
513
+ if (blocks.length === 0) {
514
+ this.log.warn(`No blocks found for blob upload`, proposalInfo);
515
+ return;
516
+ }
517
+ const blobFields = blocks.flatMap((b)=>b.toBlobFields());
518
+ const blobs = getBlobsPerL1Block(blobFields);
519
+ await this.blobClient.sendBlobsToFilestore(blobs);
520
+ this.log.debug(`Uploaded ${blobs.length} blobs to filestore for checkpoint at slot ${proposal.slotNumber}`, {
521
+ ...proposalInfo,
522
+ numBlobs: blobs.length
523
+ });
524
+ } catch (err) {
525
+ this.log.warn(`Failed to upload blobs for checkpoint: ${err}`, proposalInfo);
526
+ }
651
527
  }
652
528
  slashInvalidBlock(proposal) {
653
529
  const proposer = proposal.getSender();
@@ -671,25 +547,23 @@ _dec = trackSpan('validator.attestToProposal', (proposal, proposalSender)=>({
671
547
  }
672
548
  ]);
673
549
  }
674
- // TODO(palla/mbps): Block proposal should not require a checkpoint proposal
675
- async createBlockProposal(blockNumber, header, archive, txs, proposerAddress, options) {
550
+ async createBlockProposal(blockHeader, indexWithinCheckpoint, inHash, archive, txs, proposerAddress, options) {
676
551
  // TODO(palla/mbps): Prevent double proposals properly
677
- // if (this.previousProposal?.slotNumber === header.slotNumber) {
552
+ // if (this.previousProposal?.slotNumber === blockHeader.globalVariables.slotNumber) {
678
553
  // this.log.verbose(`Already made a proposal for the same slot, skipping proposal`);
679
554
  // return Promise.resolve(undefined);
680
555
  // }
681
- this.log.info(`Assembling block proposal for block ${blockNumber} slot ${header.slotNumber}`);
682
- const newProposal = await this.validationService.createBlockProposal(header, archive, txs, proposerAddress, {
556
+ this.log.info(`Assembling block proposal for block ${blockHeader.globalVariables.blockNumber} slot ${blockHeader.globalVariables.slotNumber}`);
557
+ const newProposal = await this.validationService.createBlockProposal(blockHeader, indexWithinCheckpoint, inHash, archive, txs, proposerAddress, {
683
558
  ...options,
684
559
  broadcastInvalidBlockProposal: this.config.broadcastInvalidBlockProposal
685
560
  });
686
561
  this.previousProposal = newProposal;
687
562
  return newProposal;
688
563
  }
689
- // TODO(palla/mbps): Effectively create a checkpoint proposal different from a block proposal
690
- createCheckpointProposal(header, archive, txs, proposerAddress, options) {
691
- this.log.info(`Assembling checkpoint proposal for slot ${header.slotNumber}`);
692
- return this.createBlockProposal(0, header, archive, txs, proposerAddress, options);
564
+ async createCheckpointProposal(checkpointHeader, archive, lastBlockInfo, proposerAddress, options) {
565
+ this.log.info(`Assembling checkpoint proposal for slot ${checkpointHeader.slotNumber}`);
566
+ return await this.validationService.createCheckpointProposal(checkpointHeader, archive, lastBlockInfo, proposerAddress, options);
693
567
  }
694
568
  async broadcastBlockProposal(proposal) {
695
569
  await this.p2pClient.broadcastProposal(proposal);
@@ -698,23 +572,23 @@ _dec = trackSpan('validator.attestToProposal', (proposal, proposalSender)=>({
698
572
  return await this.validationService.signAttestationsAndSigners(attestationsAndSigners, proposer);
699
573
  }
700
574
  async collectOwnAttestations(proposal) {
701
- const slot = proposal.payload.header.slotNumber;
575
+ const slot = proposal.slotNumber;
702
576
  const inCommittee = await this.epochCache.filterInCommittee(slot, this.getValidatorAddresses());
703
577
  this.log.debug(`Collecting ${inCommittee.length} self-attestations for slot ${slot}`, {
704
578
  inCommittee
705
579
  });
706
- const attestations = await this.createBlockAttestationsFromProposal(proposal, inCommittee);
580
+ const attestations = await this.createCheckpointAttestationsFromProposal(proposal, inCommittee);
707
581
  // We broadcast our own attestations to our peers so, in case our block does not get mined on L1,
708
582
  // other nodes can see that our validators did attest to this block proposal, and do not slash us
709
583
  // due to inactivity for missed attestations.
710
- void this.p2pClient.broadcastAttestations(attestations).catch((err)=>{
584
+ void this.p2pClient.broadcastCheckpointAttestations(attestations).catch((err)=>{
711
585
  this.log.error(`Failed to broadcast self-attestations for slot ${slot}`, err);
712
586
  });
713
587
  return attestations;
714
588
  }
715
589
  async collectAttestations(proposal, required, deadline) {
716
- // Wait and poll the p2pClient's attestation pool for this block until we have enough attestations
717
- const slot = proposal.payload.header.slotNumber;
590
+ // Wait and poll the p2pClient's attestation pool for this checkpoint until we have enough attestations
591
+ const slot = proposal.slotNumber;
718
592
  this.log.debug(`Collecting ${required} attestations for slot ${slot} with deadline ${deadline.toISOString()}`);
719
593
  if (+deadline < this.dateProvider.now()) {
720
594
  this.log.error(`Deadline ${deadline.toISOString()} for collecting ${required} attestations for slot ${slot} is in the past`);
@@ -725,13 +599,13 @@ _dec = trackSpan('validator.attestToProposal', (proposal, proposalSender)=>({
725
599
  const myAddresses = this.getValidatorAddresses();
726
600
  let attestations = [];
727
601
  while(true){
728
- // Filter out attestations with a mismatching payload. This should NOT happen since we have verified
602
+ // Filter out attestations with a mismatching archive. This should NOT happen since we have verified
729
603
  // the proposer signature (ie our own) before accepting the attestation into the pool via the p2p client.
730
- const collectedAttestations = (await this.p2pClient.getAttestationsForSlot(slot, proposalId)).filter((attestation)=>{
731
- if (!attestation.payload.equals(proposal.payload)) {
732
- this.log.warn(`Received attestation for slot ${slot} with mismatched payload from ${attestation.getSender()?.toString()}`, {
733
- attestationPayload: attestation.payload,
734
- proposalPayload: proposal.payload
604
+ const collectedAttestations = (await this.p2pClient.getCheckpointAttestationsForSlot(slot, proposalId)).filter((attestation)=>{
605
+ if (!attestation.archive.equals(proposal.archive)) {
606
+ this.log.warn(`Received attestation for slot ${slot} with mismatched archive from ${attestation.getSender()?.toString()}`, {
607
+ attestationArchive: attestation.archive.toString(),
608
+ proposalArchive: proposal.archive.toString()
735
609
  });
736
610
  return false;
737
611
  }
@@ -763,11 +637,6 @@ _dec = trackSpan('validator.attestToProposal', (proposal, proposalSender)=>({
763
637
  await sleep(this.config.attestationPollingIntervalMs);
764
638
  }
765
639
  }
766
- async createBlockAttestationsFromProposal(proposal, attestors = []) {
767
- const attestations = await this.validationService.attestToProposal(proposal, attestors);
768
- await this.p2pClient.addAttestations(attestations);
769
- return attestations;
770
- }
771
640
  async handleAuthRequest(peer, msg) {
772
641
  const authRequest = AuthRequest.fromBuffer(msg);
773
642
  const statusMessage = await this.p2pClient.handleAuthRequestFromPeer(authRequest, peer).catch((_)=>undefined);