@aztec/sequencer-client 0.0.1-commit.5476d83 → 0.0.1-commit.5914bae

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 (99) hide show
  1. package/dest/client/sequencer-client.d.ts +25 -16
  2. package/dest/client/sequencer-client.d.ts.map +1 -1
  3. package/dest/client/sequencer-client.js +69 -29
  4. package/dest/config.d.ts +34 -8
  5. package/dest/config.d.ts.map +1 -1
  6. package/dest/config.js +105 -44
  7. package/dest/global_variable_builder/global_builder.d.ts +27 -14
  8. package/dest/global_variable_builder/global_builder.d.ts.map +1 -1
  9. package/dest/global_variable_builder/global_builder.js +65 -54
  10. package/dest/global_variable_builder/index.d.ts +2 -2
  11. package/dest/global_variable_builder/index.d.ts.map +1 -1
  12. package/dest/index.d.ts +2 -3
  13. package/dest/index.d.ts.map +1 -1
  14. package/dest/index.js +1 -2
  15. package/dest/publisher/config.d.ts +37 -20
  16. package/dest/publisher/config.d.ts.map +1 -1
  17. package/dest/publisher/config.js +104 -39
  18. package/dest/publisher/sequencer-publisher-factory.d.ts +15 -6
  19. package/dest/publisher/sequencer-publisher-factory.d.ts.map +1 -1
  20. package/dest/publisher/sequencer-publisher-factory.js +14 -3
  21. package/dest/publisher/sequencer-publisher-metrics.d.ts +3 -3
  22. package/dest/publisher/sequencer-publisher-metrics.d.ts.map +1 -1
  23. package/dest/publisher/sequencer-publisher-metrics.js +23 -86
  24. package/dest/publisher/sequencer-publisher.d.ts +76 -53
  25. package/dest/publisher/sequencer-publisher.d.ts.map +1 -1
  26. package/dest/publisher/sequencer-publisher.js +691 -169
  27. package/dest/sequencer/checkpoint_proposal_job.d.ts +102 -0
  28. package/dest/sequencer/checkpoint_proposal_job.d.ts.map +1 -0
  29. package/dest/sequencer/checkpoint_proposal_job.js +1247 -0
  30. package/dest/sequencer/checkpoint_voter.d.ts +34 -0
  31. package/dest/sequencer/checkpoint_voter.d.ts.map +1 -0
  32. package/dest/sequencer/checkpoint_voter.js +106 -0
  33. package/dest/sequencer/config.d.ts +3 -2
  34. package/dest/sequencer/config.d.ts.map +1 -1
  35. package/dest/sequencer/events.d.ts +46 -0
  36. package/dest/sequencer/events.d.ts.map +1 -0
  37. package/dest/sequencer/events.js +1 -0
  38. package/dest/sequencer/index.d.ts +4 -2
  39. package/dest/sequencer/index.d.ts.map +1 -1
  40. package/dest/sequencer/index.js +3 -1
  41. package/dest/sequencer/metrics.d.ts +38 -6
  42. package/dest/sequencer/metrics.d.ts.map +1 -1
  43. package/dest/sequencer/metrics.js +216 -72
  44. package/dest/sequencer/sequencer.d.ts +125 -133
  45. package/dest/sequencer/sequencer.d.ts.map +1 -1
  46. package/dest/sequencer/sequencer.js +733 -640
  47. package/dest/sequencer/timetable.d.ts +54 -16
  48. package/dest/sequencer/timetable.d.ts.map +1 -1
  49. package/dest/sequencer/timetable.js +147 -62
  50. package/dest/sequencer/types.d.ts +3 -0
  51. package/dest/sequencer/types.d.ts.map +1 -0
  52. package/dest/sequencer/types.js +1 -0
  53. package/dest/sequencer/utils.d.ts +14 -8
  54. package/dest/sequencer/utils.d.ts.map +1 -1
  55. package/dest/sequencer/utils.js +7 -4
  56. package/dest/test/index.d.ts +6 -7
  57. package/dest/test/index.d.ts.map +1 -1
  58. package/dest/test/mock_checkpoint_builder.d.ts +95 -0
  59. package/dest/test/mock_checkpoint_builder.d.ts.map +1 -0
  60. package/dest/test/mock_checkpoint_builder.js +233 -0
  61. package/dest/test/utils.d.ts +53 -0
  62. package/dest/test/utils.d.ts.map +1 -0
  63. package/dest/test/utils.js +104 -0
  64. package/package.json +32 -30
  65. package/src/client/sequencer-client.ts +99 -53
  66. package/src/config.ts +121 -54
  67. package/src/global_variable_builder/global_builder.ts +76 -73
  68. package/src/global_variable_builder/index.ts +1 -1
  69. package/src/index.ts +1 -7
  70. package/src/publisher/config.ts +122 -50
  71. package/src/publisher/sequencer-publisher-factory.ts +28 -10
  72. package/src/publisher/sequencer-publisher-metrics.ts +19 -71
  73. package/src/publisher/sequencer-publisher.ts +406 -215
  74. package/src/sequencer/README.md +531 -0
  75. package/src/sequencer/checkpoint_proposal_job.ts +976 -0
  76. package/src/sequencer/checkpoint_voter.ts +119 -0
  77. package/src/sequencer/config.ts +2 -1
  78. package/src/sequencer/events.ts +27 -0
  79. package/src/sequencer/index.ts +3 -1
  80. package/src/sequencer/metrics.ts +268 -82
  81. package/src/sequencer/sequencer.ts +481 -845
  82. package/src/sequencer/timetable.ts +178 -83
  83. package/src/sequencer/types.ts +6 -0
  84. package/src/sequencer/utils.ts +18 -9
  85. package/src/test/index.ts +5 -6
  86. package/src/test/mock_checkpoint_builder.ts +325 -0
  87. package/src/test/utils.ts +167 -0
  88. package/dest/sequencer/block_builder.d.ts +0 -27
  89. package/dest/sequencer/block_builder.d.ts.map +0 -1
  90. package/dest/sequencer/block_builder.js +0 -134
  91. package/dest/tx_validator/nullifier_cache.d.ts +0 -14
  92. package/dest/tx_validator/nullifier_cache.d.ts.map +0 -1
  93. package/dest/tx_validator/nullifier_cache.js +0 -24
  94. package/dest/tx_validator/tx_validator_factory.d.ts +0 -17
  95. package/dest/tx_validator/tx_validator_factory.d.ts.map +0 -1
  96. package/dest/tx_validator/tx_validator_factory.js +0 -53
  97. package/src/sequencer/block_builder.ts +0 -222
  98. package/src/tx_validator/nullifier_cache.ts +0 -30
  99. package/src/tx_validator/tx_validator_factory.ts +0 -132
@@ -0,0 +1,1247 @@
1
+ function _ts_add_disposable_resource(env, value, async) {
2
+ if (value !== null && value !== void 0) {
3
+ if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
4
+ var dispose, inner;
5
+ if (async) {
6
+ if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
7
+ dispose = value[Symbol.asyncDispose];
8
+ }
9
+ if (dispose === void 0) {
10
+ if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
11
+ dispose = value[Symbol.dispose];
12
+ if (async) inner = dispose;
13
+ }
14
+ if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
15
+ if (inner) dispose = function() {
16
+ try {
17
+ inner.call(this);
18
+ } catch (e) {
19
+ return Promise.reject(e);
20
+ }
21
+ };
22
+ env.stack.push({
23
+ value: value,
24
+ dispose: dispose,
25
+ async: async
26
+ });
27
+ } else if (async) {
28
+ env.stack.push({
29
+ async: true
30
+ });
31
+ }
32
+ return value;
33
+ }
34
+ function _ts_dispose_resources(env) {
35
+ var _SuppressedError = typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed, message) {
36
+ var e = new Error(message);
37
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
38
+ };
39
+ return (_ts_dispose_resources = function _ts_dispose_resources(env) {
40
+ function fail(e) {
41
+ env.error = env.hasError ? new _SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
42
+ env.hasError = true;
43
+ }
44
+ var r, s = 0;
45
+ function next() {
46
+ while(r = env.stack.pop()){
47
+ try {
48
+ if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
49
+ if (r.dispose) {
50
+ var result = r.dispose.call(r.value);
51
+ if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) {
52
+ fail(e);
53
+ return next();
54
+ });
55
+ } else s |= 1;
56
+ } catch (e) {
57
+ fail(e);
58
+ }
59
+ }
60
+ if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
61
+ if (env.hasError) throw env.error;
62
+ }
63
+ return next();
64
+ })(env);
65
+ }
66
+ function applyDecs2203RFactory() {
67
+ function createAddInitializerMethod(initializers, decoratorFinishedRef) {
68
+ return function addInitializer(initializer) {
69
+ assertNotFinished(decoratorFinishedRef, "addInitializer");
70
+ assertCallable(initializer, "An initializer");
71
+ initializers.push(initializer);
72
+ };
73
+ }
74
+ function memberDec(dec, name, desc, initializers, kind, isStatic, isPrivate, metadata, value) {
75
+ var kindStr;
76
+ switch(kind){
77
+ case 1:
78
+ kindStr = "accessor";
79
+ break;
80
+ case 2:
81
+ kindStr = "method";
82
+ break;
83
+ case 3:
84
+ kindStr = "getter";
85
+ break;
86
+ case 4:
87
+ kindStr = "setter";
88
+ break;
89
+ default:
90
+ kindStr = "field";
91
+ }
92
+ var ctx = {
93
+ kind: kindStr,
94
+ name: isPrivate ? "#" + name : name,
95
+ static: isStatic,
96
+ private: isPrivate,
97
+ metadata: metadata
98
+ };
99
+ var decoratorFinishedRef = {
100
+ v: false
101
+ };
102
+ ctx.addInitializer = createAddInitializerMethod(initializers, decoratorFinishedRef);
103
+ var get, set;
104
+ if (kind === 0) {
105
+ if (isPrivate) {
106
+ get = desc.get;
107
+ set = desc.set;
108
+ } else {
109
+ get = function() {
110
+ return this[name];
111
+ };
112
+ set = function(v) {
113
+ this[name] = v;
114
+ };
115
+ }
116
+ } else if (kind === 2) {
117
+ get = function() {
118
+ return desc.value;
119
+ };
120
+ } else {
121
+ if (kind === 1 || kind === 3) {
122
+ get = function() {
123
+ return desc.get.call(this);
124
+ };
125
+ }
126
+ if (kind === 1 || kind === 4) {
127
+ set = function(v) {
128
+ desc.set.call(this, v);
129
+ };
130
+ }
131
+ }
132
+ ctx.access = get && set ? {
133
+ get: get,
134
+ set: set
135
+ } : get ? {
136
+ get: get
137
+ } : {
138
+ set: set
139
+ };
140
+ try {
141
+ return dec(value, ctx);
142
+ } finally{
143
+ decoratorFinishedRef.v = true;
144
+ }
145
+ }
146
+ function assertNotFinished(decoratorFinishedRef, fnName) {
147
+ if (decoratorFinishedRef.v) {
148
+ throw new Error("attempted to call " + fnName + " after decoration was finished");
149
+ }
150
+ }
151
+ function assertCallable(fn, hint) {
152
+ if (typeof fn !== "function") {
153
+ throw new TypeError(hint + " must be a function");
154
+ }
155
+ }
156
+ function assertValidReturnValue(kind, value) {
157
+ var type = typeof value;
158
+ if (kind === 1) {
159
+ if (type !== "object" || value === null) {
160
+ throw new TypeError("accessor decorators must return an object with get, set, or init properties or void 0");
161
+ }
162
+ if (value.get !== undefined) {
163
+ assertCallable(value.get, "accessor.get");
164
+ }
165
+ if (value.set !== undefined) {
166
+ assertCallable(value.set, "accessor.set");
167
+ }
168
+ if (value.init !== undefined) {
169
+ assertCallable(value.init, "accessor.init");
170
+ }
171
+ } else if (type !== "function") {
172
+ var hint;
173
+ if (kind === 0) {
174
+ hint = "field";
175
+ } else if (kind === 10) {
176
+ hint = "class";
177
+ } else {
178
+ hint = "method";
179
+ }
180
+ throw new TypeError(hint + " decorators must return a function or void 0");
181
+ }
182
+ }
183
+ function applyMemberDec(ret, base, decInfo, name, kind, isStatic, isPrivate, initializers, metadata) {
184
+ var decs = decInfo[0];
185
+ var desc, init, value;
186
+ if (isPrivate) {
187
+ if (kind === 0 || kind === 1) {
188
+ desc = {
189
+ get: decInfo[3],
190
+ set: decInfo[4]
191
+ };
192
+ } else if (kind === 3) {
193
+ desc = {
194
+ get: decInfo[3]
195
+ };
196
+ } else if (kind === 4) {
197
+ desc = {
198
+ set: decInfo[3]
199
+ };
200
+ } else {
201
+ desc = {
202
+ value: decInfo[3]
203
+ };
204
+ }
205
+ } else if (kind !== 0) {
206
+ desc = Object.getOwnPropertyDescriptor(base, name);
207
+ }
208
+ if (kind === 1) {
209
+ value = {
210
+ get: desc.get,
211
+ set: desc.set
212
+ };
213
+ } else if (kind === 2) {
214
+ value = desc.value;
215
+ } else if (kind === 3) {
216
+ value = desc.get;
217
+ } else if (kind === 4) {
218
+ value = desc.set;
219
+ }
220
+ var newValue, get, set;
221
+ if (typeof decs === "function") {
222
+ newValue = memberDec(decs, name, desc, initializers, kind, isStatic, isPrivate, metadata, value);
223
+ if (newValue !== void 0) {
224
+ assertValidReturnValue(kind, newValue);
225
+ if (kind === 0) {
226
+ init = newValue;
227
+ } else if (kind === 1) {
228
+ init = newValue.init;
229
+ get = newValue.get || value.get;
230
+ set = newValue.set || value.set;
231
+ value = {
232
+ get: get,
233
+ set: set
234
+ };
235
+ } else {
236
+ value = newValue;
237
+ }
238
+ }
239
+ } else {
240
+ for(var i = decs.length - 1; i >= 0; i--){
241
+ var dec = decs[i];
242
+ newValue = memberDec(dec, name, desc, initializers, kind, isStatic, isPrivate, metadata, value);
243
+ if (newValue !== void 0) {
244
+ assertValidReturnValue(kind, newValue);
245
+ var newInit;
246
+ if (kind === 0) {
247
+ newInit = newValue;
248
+ } else if (kind === 1) {
249
+ newInit = newValue.init;
250
+ get = newValue.get || value.get;
251
+ set = newValue.set || value.set;
252
+ value = {
253
+ get: get,
254
+ set: set
255
+ };
256
+ } else {
257
+ value = newValue;
258
+ }
259
+ if (newInit !== void 0) {
260
+ if (init === void 0) {
261
+ init = newInit;
262
+ } else if (typeof init === "function") {
263
+ init = [
264
+ init,
265
+ newInit
266
+ ];
267
+ } else {
268
+ init.push(newInit);
269
+ }
270
+ }
271
+ }
272
+ }
273
+ }
274
+ if (kind === 0 || kind === 1) {
275
+ if (init === void 0) {
276
+ init = function(instance, init) {
277
+ return init;
278
+ };
279
+ } else if (typeof init !== "function") {
280
+ var ownInitializers = init;
281
+ init = function(instance, init) {
282
+ var value = init;
283
+ for(var i = 0; i < ownInitializers.length; i++){
284
+ value = ownInitializers[i].call(instance, value);
285
+ }
286
+ return value;
287
+ };
288
+ } else {
289
+ var originalInitializer = init;
290
+ init = function(instance, init) {
291
+ return originalInitializer.call(instance, init);
292
+ };
293
+ }
294
+ ret.push(init);
295
+ }
296
+ if (kind !== 0) {
297
+ if (kind === 1) {
298
+ desc.get = value.get;
299
+ desc.set = value.set;
300
+ } else if (kind === 2) {
301
+ desc.value = value;
302
+ } else if (kind === 3) {
303
+ desc.get = value;
304
+ } else if (kind === 4) {
305
+ desc.set = value;
306
+ }
307
+ if (isPrivate) {
308
+ if (kind === 1) {
309
+ ret.push(function(instance, args) {
310
+ return value.get.call(instance, args);
311
+ });
312
+ ret.push(function(instance, args) {
313
+ return value.set.call(instance, args);
314
+ });
315
+ } else if (kind === 2) {
316
+ ret.push(value);
317
+ } else {
318
+ ret.push(function(instance, args) {
319
+ return value.call(instance, args);
320
+ });
321
+ }
322
+ } else {
323
+ Object.defineProperty(base, name, desc);
324
+ }
325
+ }
326
+ }
327
+ function applyMemberDecs(Class, decInfos, metadata) {
328
+ var ret = [];
329
+ var protoInitializers;
330
+ var staticInitializers;
331
+ var existingProtoNonFields = new Map();
332
+ var existingStaticNonFields = new Map();
333
+ for(var i = 0; i < decInfos.length; i++){
334
+ var decInfo = decInfos[i];
335
+ if (!Array.isArray(decInfo)) continue;
336
+ var kind = decInfo[1];
337
+ var name = decInfo[2];
338
+ var isPrivate = decInfo.length > 3;
339
+ var isStatic = kind >= 5;
340
+ var base;
341
+ var initializers;
342
+ if (isStatic) {
343
+ base = Class;
344
+ kind = kind - 5;
345
+ staticInitializers = staticInitializers || [];
346
+ initializers = staticInitializers;
347
+ } else {
348
+ base = Class.prototype;
349
+ protoInitializers = protoInitializers || [];
350
+ initializers = protoInitializers;
351
+ }
352
+ if (kind !== 0 && !isPrivate) {
353
+ var existingNonFields = isStatic ? existingStaticNonFields : existingProtoNonFields;
354
+ var existingKind = existingNonFields.get(name) || 0;
355
+ if (existingKind === true || existingKind === 3 && kind !== 4 || existingKind === 4 && kind !== 3) {
356
+ 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);
357
+ } else if (!existingKind && kind > 2) {
358
+ existingNonFields.set(name, kind);
359
+ } else {
360
+ existingNonFields.set(name, true);
361
+ }
362
+ }
363
+ applyMemberDec(ret, base, decInfo, name, kind, isStatic, isPrivate, initializers, metadata);
364
+ }
365
+ pushInitializers(ret, protoInitializers);
366
+ pushInitializers(ret, staticInitializers);
367
+ return ret;
368
+ }
369
+ function pushInitializers(ret, initializers) {
370
+ if (initializers) {
371
+ ret.push(function(instance) {
372
+ for(var i = 0; i < initializers.length; i++){
373
+ initializers[i].call(instance);
374
+ }
375
+ return instance;
376
+ });
377
+ }
378
+ }
379
+ function applyClassDecs(targetClass, classDecs, metadata) {
380
+ if (classDecs.length > 0) {
381
+ var initializers = [];
382
+ var newClass = targetClass;
383
+ var name = targetClass.name;
384
+ for(var i = classDecs.length - 1; i >= 0; i--){
385
+ var decoratorFinishedRef = {
386
+ v: false
387
+ };
388
+ try {
389
+ var nextNewClass = classDecs[i](newClass, {
390
+ kind: "class",
391
+ name: name,
392
+ addInitializer: createAddInitializerMethod(initializers, decoratorFinishedRef),
393
+ metadata
394
+ });
395
+ } finally{
396
+ decoratorFinishedRef.v = true;
397
+ }
398
+ if (nextNewClass !== undefined) {
399
+ assertValidReturnValue(10, nextNewClass);
400
+ newClass = nextNewClass;
401
+ }
402
+ }
403
+ return [
404
+ defineMetadata(newClass, metadata),
405
+ function() {
406
+ for(var i = 0; i < initializers.length; i++){
407
+ initializers[i].call(newClass);
408
+ }
409
+ }
410
+ ];
411
+ }
412
+ }
413
+ function defineMetadata(Class, metadata) {
414
+ return Object.defineProperty(Class, Symbol.metadata || Symbol.for("Symbol.metadata"), {
415
+ configurable: true,
416
+ enumerable: true,
417
+ value: metadata
418
+ });
419
+ }
420
+ return function applyDecs2203R(targetClass, memberDecs, classDecs, parentClass) {
421
+ if (parentClass !== void 0) {
422
+ var parentMetadata = parentClass[Symbol.metadata || Symbol.for("Symbol.metadata")];
423
+ }
424
+ var metadata = Object.create(parentMetadata === void 0 ? null : parentMetadata);
425
+ var e = applyMemberDecs(targetClass, memberDecs, metadata);
426
+ if (!classDecs.length) defineMetadata(targetClass, metadata);
427
+ return {
428
+ e: e,
429
+ get c () {
430
+ return applyClassDecs(targetClass, classDecs, metadata);
431
+ }
432
+ };
433
+ };
434
+ }
435
+ function _apply_decs_2203_r(targetClass, memberDecs, classDecs, parentClass) {
436
+ return (_apply_decs_2203_r = applyDecs2203RFactory())(targetClass, memberDecs, classDecs, parentClass);
437
+ }
438
+ var _dec, _dec1, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _initProto;
439
+ import { BlockNumber, IndexWithinCheckpoint } from '@aztec/foundation/branded-types';
440
+ import { randomInt } from '@aztec/foundation/crypto/random';
441
+ import { flipSignature, generateRecoverableSignature, generateUnrecoverableSignature } from '@aztec/foundation/crypto/secp256k1-signer';
442
+ import { filter } from '@aztec/foundation/iterator';
443
+ import { createLogger } from '@aztec/foundation/log';
444
+ import { sleep, sleepUntil } from '@aztec/foundation/sleep';
445
+ import { Timer } from '@aztec/foundation/timer';
446
+ import { isErrorClass, unfreeze } from '@aztec/foundation/types';
447
+ import { CommitteeAttestationsAndSigners, MaliciousCommitteeAttestationsAndSigners } from '@aztec/stdlib/block';
448
+ import { validateCheckpoint } from '@aztec/stdlib/checkpoint';
449
+ import { getSlotStartBuildTimestamp } from '@aztec/stdlib/epoch-helpers';
450
+ import { Gas } from '@aztec/stdlib/gas';
451
+ import { InsufficientValidTxsError } from '@aztec/stdlib/interfaces/server';
452
+ import { computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
453
+ import { orderAttestations, trimAttestations } from '@aztec/stdlib/p2p';
454
+ import { AttestationTimeoutError } from '@aztec/stdlib/validators';
455
+ import { Attributes, trackSpan } from '@aztec/telemetry-client';
456
+ import { DutyAlreadySignedError, SlashingProtectionError } from '@aztec/validator-ha-signer/errors';
457
+ import { CheckpointVoter } from './checkpoint_voter.js';
458
+ import { SequencerInterruptedError } from './errors.js';
459
+ import { SequencerState } from './utils.js';
460
+ /** How much time to sleep while waiting for min transactions to accumulate for a block */ const TXS_POLLING_MS = 500;
461
+ _dec = trackSpan('CheckpointProposalJob.execute'), _dec1 = trackSpan('CheckpointProposalJob.proposeCheckpoint', function() {
462
+ return {
463
+ // nullish operator needed for tests
464
+ [Attributes.COINBASE]: this.validatorClient.getCoinbaseForAttestor(this.attestorAddress)?.toString(),
465
+ [Attributes.SLOT_NUMBER]: this.slot
466
+ };
467
+ }), _dec2 = trackSpan('CheckpointProposalJob.buildBlocksForCheckpoint'), _dec3 = trackSpan('CheckpointProposalJob.waitUntilNextSubslot'), _dec4 = trackSpan('CheckpointProposalJob.buildSingleBlock'), _dec5 = trackSpan('CheckpointProposalJob.waitForMinTxs'), _dec6 = trackSpan('CheckpointProposalJob.waitForAttestations'), _dec7 = trackSpan('CheckpointProposalJob.waitUntilTimeInSlot');
468
+ /**
469
+ * Handles the execution of a checkpoint proposal after the initial preparation phase.
470
+ * This includes building blocks, collecting attestations, and publishing the checkpoint to L1,
471
+ * as well as enqueueing votes for slashing and governance proposals. This class is created from
472
+ * the Sequencer once the check for being the proposer for the slot has succeeded.
473
+ */ export class CheckpointProposalJob {
474
+ epoch;
475
+ slot;
476
+ checkpointNumber;
477
+ syncedToBlockNumber;
478
+ proposer;
479
+ publisher;
480
+ attestorAddress;
481
+ invalidateCheckpoint;
482
+ validatorClient;
483
+ globalsBuilder;
484
+ p2pClient;
485
+ worldState;
486
+ l1ToL2MessageSource;
487
+ l2BlockSource;
488
+ checkpointsBuilder;
489
+ blockSink;
490
+ l1Constants;
491
+ config;
492
+ timetable;
493
+ slasherClient;
494
+ epochCache;
495
+ dateProvider;
496
+ metrics;
497
+ eventEmitter;
498
+ setStateFn;
499
+ tracer;
500
+ static{
501
+ ({ e: [_initProto] } = _apply_decs_2203_r(this, [
502
+ [
503
+ _dec,
504
+ 2,
505
+ "execute"
506
+ ],
507
+ [
508
+ _dec1,
509
+ 2,
510
+ "proposeCheckpoint"
511
+ ],
512
+ [
513
+ _dec2,
514
+ 2,
515
+ "buildBlocksForCheckpoint"
516
+ ],
517
+ [
518
+ _dec3,
519
+ 2,
520
+ "waitUntilNextSubslot"
521
+ ],
522
+ [
523
+ _dec4,
524
+ 2,
525
+ "buildSingleBlock"
526
+ ],
527
+ [
528
+ _dec5,
529
+ 2,
530
+ "waitForMinTxs"
531
+ ],
532
+ [
533
+ _dec6,
534
+ 2,
535
+ "waitForAttestations"
536
+ ],
537
+ [
538
+ _dec7,
539
+ 2,
540
+ "waitUntilTimeInSlot"
541
+ ]
542
+ ], []));
543
+ }
544
+ log;
545
+ constructor(epoch, slot, checkpointNumber, syncedToBlockNumber, // TODO(palla/mbps): Can we remove the proposer in favor of attestorAddress? Need to check fisherman-node flows.
546
+ proposer, publisher, attestorAddress, invalidateCheckpoint, validatorClient, globalsBuilder, p2pClient, worldState, l1ToL2MessageSource, l2BlockSource, checkpointsBuilder, blockSink, l1Constants, config, timetable, slasherClient, epochCache, dateProvider, metrics, eventEmitter, setStateFn, tracer, bindings){
547
+ this.epoch = epoch;
548
+ this.slot = slot;
549
+ this.checkpointNumber = checkpointNumber;
550
+ this.syncedToBlockNumber = syncedToBlockNumber;
551
+ this.proposer = proposer;
552
+ this.publisher = publisher;
553
+ this.attestorAddress = attestorAddress;
554
+ this.invalidateCheckpoint = invalidateCheckpoint;
555
+ this.validatorClient = validatorClient;
556
+ this.globalsBuilder = globalsBuilder;
557
+ this.p2pClient = p2pClient;
558
+ this.worldState = worldState;
559
+ this.l1ToL2MessageSource = l1ToL2MessageSource;
560
+ this.l2BlockSource = l2BlockSource;
561
+ this.checkpointsBuilder = checkpointsBuilder;
562
+ this.blockSink = blockSink;
563
+ this.l1Constants = l1Constants;
564
+ this.config = config;
565
+ this.timetable = timetable;
566
+ this.slasherClient = slasherClient;
567
+ this.epochCache = epochCache;
568
+ this.dateProvider = dateProvider;
569
+ this.metrics = metrics;
570
+ this.eventEmitter = eventEmitter;
571
+ this.setStateFn = setStateFn;
572
+ this.tracer = tracer;
573
+ _initProto(this);
574
+ this.log = createLogger('sequencer:checkpoint-proposal', {
575
+ ...bindings,
576
+ instanceId: `slot-${slot}`
577
+ });
578
+ }
579
+ /**
580
+ * Executes the checkpoint proposal job.
581
+ * Returns the published checkpoint if successful, undefined otherwise.
582
+ */ async execute() {
583
+ // Enqueue governance and slashing votes (returns promises that will be awaited later)
584
+ // In fisherman mode, we simulate slashing but don't actually publish to L1
585
+ // These are constant for the whole slot, so we only enqueue them once
586
+ const votesPromises = new CheckpointVoter(this.slot, this.publisher, this.attestorAddress, this.validatorClient, this.slasherClient, this.l1Constants, this.config, this.metrics, this.log).enqueueVotes();
587
+ // Build and propose the checkpoint. This will enqueue the request on the publisher if a checkpoint is built.
588
+ const checkpoint = await this.proposeCheckpoint();
589
+ // Wait until the voting promises have resolved, so all requests are enqueued (not sent)
590
+ await Promise.all(votesPromises);
591
+ if (checkpoint) {
592
+ this.metrics.recordCheckpointProposalSuccess();
593
+ }
594
+ // Do not post anything to L1 if we are fishermen, but do perform L1 fee analysis
595
+ if (this.config.fishermanMode) {
596
+ await this.handleCheckpointEndAsFisherman(checkpoint);
597
+ return;
598
+ }
599
+ // Then send everything to L1
600
+ const l1Response = await this.publisher.sendRequests();
601
+ const proposedAction = l1Response?.successfulActions.find((a)=>a === 'propose');
602
+ if (proposedAction) {
603
+ this.eventEmitter.emit('checkpoint-published', {
604
+ checkpoint: this.checkpointNumber,
605
+ slot: this.slot
606
+ });
607
+ const coinbase = checkpoint?.header.coinbase;
608
+ await this.metrics.incFilledSlot(this.publisher.getSenderAddress().toString(), coinbase);
609
+ return checkpoint;
610
+ } else if (checkpoint) {
611
+ this.eventEmitter.emit('checkpoint-publish-failed', {
612
+ ...l1Response,
613
+ slot: this.slot
614
+ });
615
+ return undefined;
616
+ }
617
+ }
618
+ async proposeCheckpoint() {
619
+ try {
620
+ const env = {
621
+ stack: [],
622
+ error: void 0,
623
+ hasError: false
624
+ };
625
+ try {
626
+ // Get operator configured coinbase and fee recipient for this attestor
627
+ const coinbase = this.validatorClient.getCoinbaseForAttestor(this.attestorAddress);
628
+ const feeRecipient = this.validatorClient.getFeeRecipientForAttestor(this.attestorAddress);
629
+ // Start the checkpoint
630
+ this.setStateFn(SequencerState.INITIALIZING_CHECKPOINT, this.slot);
631
+ this.metrics.incOpenSlot(this.slot, this.proposer?.toString() ?? 'unknown');
632
+ // Enqueues checkpoint invalidation (constant for the whole slot)
633
+ if (this.invalidateCheckpoint && !this.config.skipInvalidateBlockAsProposer) {
634
+ this.publisher.enqueueInvalidateCheckpoint(this.invalidateCheckpoint);
635
+ }
636
+ // Create checkpoint builder for the slot
637
+ const checkpointGlobalVariables = await this.globalsBuilder.buildCheckpointGlobalVariables(coinbase, feeRecipient, this.slot);
638
+ // Collect L1 to L2 messages for the checkpoint and compute their hash
639
+ const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(this.checkpointNumber);
640
+ const inHash = computeInHashFromL1ToL2Messages(l1ToL2Messages);
641
+ // Collect the out hashes of all the checkpoints before this one in the same epoch
642
+ const previousCheckpointOutHashes = (await this.l2BlockSource.getCheckpointsDataForEpoch(this.epoch)).filter((c)=>c.checkpointNumber < this.checkpointNumber).map((c)=>c.checkpointOutHash);
643
+ // Get the fee asset price modifier from the oracle
644
+ const feeAssetPriceModifier = await this.publisher.getFeeAssetPriceModifier();
645
+ const fork = _ts_add_disposable_resource(env, await this.worldState.fork(this.syncedToBlockNumber, {
646
+ closeDelayMs: 12_000
647
+ }), true);
648
+ // Create checkpoint builder for the entire slot
649
+ const checkpointBuilder = await this.checkpointsBuilder.startCheckpoint(this.checkpointNumber, checkpointGlobalVariables, feeAssetPriceModifier, l1ToL2Messages, previousCheckpointOutHashes, fork, this.log.getBindings());
650
+ // Options for the validator client when creating block and checkpoint proposals
651
+ const blockProposalOptions = {
652
+ publishFullTxs: !!this.config.publishTxsWithProposals,
653
+ broadcastInvalidBlockProposal: this.config.broadcastInvalidBlockProposal
654
+ };
655
+ const checkpointProposalOptions = {
656
+ publishFullTxs: !!this.config.publishTxsWithProposals,
657
+ broadcastInvalidCheckpointProposal: this.config.broadcastInvalidBlockProposal
658
+ };
659
+ let blocksInCheckpoint = [];
660
+ let blockPendingBroadcast = undefined;
661
+ const checkpointBuildTimer = new Timer();
662
+ try {
663
+ // Main loop: build blocks for the checkpoint
664
+ const result = await this.buildBlocksForCheckpoint(checkpointBuilder, checkpointGlobalVariables.timestamp, inHash, blockProposalOptions);
665
+ blocksInCheckpoint = result.blocksInCheckpoint;
666
+ blockPendingBroadcast = result.blockPendingBroadcast;
667
+ } catch (err) {
668
+ // These errors are expected in HA mode, so we yield and let another HA node handle the slot
669
+ // The only distinction between the 2 errors is SlashingProtectionError throws when the payload is different,
670
+ // which is normal for block building (may have picked different txs)
671
+ if (this.handleHASigningError(err, 'Block proposal')) {
672
+ return undefined;
673
+ }
674
+ throw err;
675
+ }
676
+ if (blocksInCheckpoint.length === 0) {
677
+ this.log.warn(`No blocks were built for slot ${this.slot}`, {
678
+ slot: this.slot
679
+ });
680
+ this.eventEmitter.emit('checkpoint-empty', {
681
+ slot: this.slot
682
+ });
683
+ return undefined;
684
+ }
685
+ const minBlocksForCheckpoint = this.config.minBlocksForCheckpoint;
686
+ if (minBlocksForCheckpoint !== undefined && blocksInCheckpoint.length < minBlocksForCheckpoint) {
687
+ this.log.warn(`Checkpoint has fewer blocks than minimum (${blocksInCheckpoint.length} < ${minBlocksForCheckpoint}), skipping proposal`, {
688
+ slot: this.slot,
689
+ blocksBuilt: blocksInCheckpoint.length,
690
+ minBlocksForCheckpoint
691
+ });
692
+ return undefined;
693
+ }
694
+ // Assemble and broadcast the checkpoint proposal, including the last block that was not
695
+ // broadcasted yet, and wait to collect the committee attestations.
696
+ this.setStateFn(SequencerState.ASSEMBLING_CHECKPOINT, this.slot);
697
+ const checkpoint = await checkpointBuilder.completeCheckpoint();
698
+ // Final validation: per-block limits are only checked if the operator set them explicitly.
699
+ // Otherwise, checkpoint-level budgets were already enforced by the redistribution logic.
700
+ try {
701
+ validateCheckpoint(checkpoint, {
702
+ rollupManaLimit: this.l1Constants.rollupManaLimit,
703
+ maxL2BlockGas: this.config.maxL2BlockGas,
704
+ maxDABlockGas: this.config.maxDABlockGas,
705
+ maxTxsPerBlock: this.config.maxTxsPerBlock,
706
+ maxTxsPerCheckpoint: this.config.maxTxsPerCheckpoint
707
+ });
708
+ } catch (err) {
709
+ this.log.error(`Built an invalid checkpoint at slot ${this.slot} (skipping proposal)`, err, {
710
+ checkpoint: checkpoint.header.toInspect()
711
+ });
712
+ return undefined;
713
+ }
714
+ // Record checkpoint-level build metrics
715
+ this.metrics.recordCheckpointBuild(checkpointBuildTimer.ms(), blocksInCheckpoint.length, checkpoint.getStats().txCount, Number(checkpoint.header.totalManaUsed.toBigInt()));
716
+ // Do not collect attestations nor publish to L1 in fisherman mode
717
+ if (this.config.fishermanMode) {
718
+ this.log.info(`Built checkpoint for slot ${this.slot} with ${blocksInCheckpoint.length} blocks. ` + `Skipping proposal in fisherman mode.`, {
719
+ slot: this.slot,
720
+ checkpoint: checkpoint.header.toInspect(),
721
+ blocksBuilt: blocksInCheckpoint.length
722
+ });
723
+ this.metrics.recordCheckpointSuccess();
724
+ return checkpoint;
725
+ }
726
+ // Include the block pending broadcast in the checkpoint proposal if any
727
+ const lastBlock = blockPendingBroadcast && {
728
+ blockHeader: blockPendingBroadcast.block.header,
729
+ indexWithinCheckpoint: blockPendingBroadcast.block.indexWithinCheckpoint,
730
+ txs: blockPendingBroadcast.txs
731
+ };
732
+ // Create the checkpoint proposal and broadcast it
733
+ const proposal = await this.validatorClient.createCheckpointProposal(checkpoint.header, checkpoint.archive.root, feeAssetPriceModifier, lastBlock, this.proposer, checkpointProposalOptions);
734
+ const blockProposedAt = this.dateProvider.now();
735
+ await this.p2pClient.broadcastCheckpointProposal(proposal);
736
+ this.setStateFn(SequencerState.COLLECTING_ATTESTATIONS, this.slot);
737
+ const attestations = await this.waitForAttestations(proposal);
738
+ const blockAttestedAt = this.dateProvider.now();
739
+ this.metrics.recordCheckpointAttestationDelay(blockAttestedAt - blockProposedAt);
740
+ // Proposer must sign over the attestations before pushing them to L1
741
+ const signer = this.proposer ?? this.publisher.getSenderAddress();
742
+ let attestationsSignature;
743
+ try {
744
+ attestationsSignature = await this.validatorClient.signAttestationsAndSigners(attestations, signer, this.slot, this.checkpointNumber);
745
+ } catch (err) {
746
+ // We shouldn't really get here since we yield to another HA node
747
+ // as soon as we see these errors when creating block or checkpoint proposals.
748
+ if (this.handleHASigningError(err, 'Attestations signature')) {
749
+ return undefined;
750
+ }
751
+ throw err;
752
+ }
753
+ // Enqueue publishing the checkpoint to L1
754
+ this.setStateFn(SequencerState.PUBLISHING_CHECKPOINT, this.slot);
755
+ const aztecSlotDuration = this.l1Constants.slotDuration;
756
+ const slotStartBuildTimestamp = this.getSlotStartBuildTimestamp();
757
+ const txTimeoutAt = new Date((slotStartBuildTimestamp + aztecSlotDuration) * 1000);
758
+ // If we have been configured to potentially skip publishing checkpoint then roll the dice here
759
+ if (this.config.skipPublishingCheckpointsPercent !== undefined && this.config.skipPublishingCheckpointsPercent > 0) {
760
+ const result = Math.max(0, randomInt(100));
761
+ if (result < this.config.skipPublishingCheckpointsPercent) {
762
+ this.log.warn(`Skipping publishing proposal for checkpoint ${checkpoint.number}. Configured percentage: ${this.config.skipPublishingCheckpointsPercent}, generated value: ${result}`);
763
+ return checkpoint;
764
+ }
765
+ }
766
+ await this.publisher.enqueueProposeCheckpoint(checkpoint, attestations, attestationsSignature, {
767
+ txTimeoutAt,
768
+ forcePendingCheckpointNumber: this.invalidateCheckpoint?.forcePendingCheckpointNumber
769
+ });
770
+ return checkpoint;
771
+ } catch (e) {
772
+ env.error = e;
773
+ env.hasError = true;
774
+ } finally{
775
+ const result = _ts_dispose_resources(env);
776
+ if (result) await result;
777
+ }
778
+ } catch (err) {
779
+ if (err && (err instanceof DutyAlreadySignedError || err instanceof SlashingProtectionError)) {
780
+ // swallow this error. It's already been logged by a function deeper in the stack
781
+ return undefined;
782
+ }
783
+ this.log.error(`Error building checkpoint at slot ${this.slot}`, err);
784
+ return undefined;
785
+ }
786
+ }
787
+ /**
788
+ * Builds blocks for a checkpoint within the current slot.
789
+ */ async buildBlocksForCheckpoint(checkpointBuilder, timestamp, inHash, blockProposalOptions) {
790
+ const blocksInCheckpoint = [];
791
+ const txHashesAlreadyIncluded = new Set();
792
+ const initialBlockNumber = BlockNumber(this.syncedToBlockNumber + 1);
793
+ const slot = this.slot;
794
+ // Last block in the checkpoint will usually be flagged as pending broadcast, so we send it along with the checkpoint proposal
795
+ let blockPendingBroadcast = undefined;
796
+ while(true){
797
+ const blocksBuilt = blocksInCheckpoint.length;
798
+ const indexWithinCheckpoint = IndexWithinCheckpoint(blocksBuilt);
799
+ const blockNumber = BlockNumber(initialBlockNumber + blocksBuilt);
800
+ const secondsIntoSlot = this.getSecondsIntoSlot();
801
+ const timingInfo = this.timetable.canStartNextBlock(secondsIntoSlot);
802
+ if (!timingInfo.canStart) {
803
+ this.log.debug(`Not enough time left in slot to start another block`, {
804
+ slot,
805
+ blocksBuilt,
806
+ secondsIntoSlot
807
+ });
808
+ break;
809
+ }
810
+ const buildResult = await this.buildSingleBlock(checkpointBuilder, {
811
+ // Create all blocks with the same timestamp
812
+ blockTimestamp: timestamp,
813
+ // Create an empty block if we haven't already and this is the last one
814
+ forceCreate: timingInfo.isLastBlock && blocksBuilt === 0 && this.config.buildCheckpointIfEmpty,
815
+ // Build deadline is only set if we are enforcing the timetable
816
+ buildDeadline: timingInfo.deadline ? new Date((this.getSlotStartBuildTimestamp() + timingInfo.deadline) * 1000) : undefined,
817
+ blockNumber,
818
+ indexWithinCheckpoint,
819
+ txHashesAlreadyIncluded
820
+ });
821
+ // TODO(palla/mbps): Review these conditions. We may want to keep trying in some scenarios.
822
+ if (!buildResult && timingInfo.isLastBlock) {
823
+ break;
824
+ } else if (!buildResult && timingInfo.deadline !== undefined) {
825
+ // But if there is still time for more blocks, wait until the next subslot and try again
826
+ await this.waitUntilNextSubslot(timingInfo.deadline);
827
+ continue;
828
+ } else if (!buildResult) {
829
+ break;
830
+ } else if ('error' in buildResult) {
831
+ // If there was an error building the block, just exit the loop and give up the rest of the slot
832
+ if (!(buildResult.error instanceof SequencerInterruptedError)) {
833
+ this.log.warn(`Halting block building for slot ${slot}`, {
834
+ slot,
835
+ blocksBuilt,
836
+ error: buildResult.error
837
+ });
838
+ }
839
+ break;
840
+ }
841
+ const { block, usedTxs } = buildResult;
842
+ blocksInCheckpoint.push(block);
843
+ usedTxs.forEach((tx)=>txHashesAlreadyIncluded.add(tx.txHash.toString()));
844
+ // If this is the last block, send the proposed block to the archiver,
845
+ // and exit the loop now so we can build the checkpoint and start collecting attestations.
846
+ if (timingInfo.isLastBlock) {
847
+ await this.syncProposedBlockToArchiver(block);
848
+ this.log.verbose(`Completed final block ${blockNumber} for slot ${slot}`, {
849
+ slot,
850
+ blockNumber,
851
+ blocksBuilt
852
+ });
853
+ blockPendingBroadcast = {
854
+ block,
855
+ txs: usedTxs
856
+ };
857
+ break;
858
+ }
859
+ // Broadcast the block proposal (unless we're in fisherman mode) unless the block is the last one,
860
+ // in which case we'll broadcast it along with the checkpoint at the end of the loop.
861
+ // Note that we only send the block to the archiver if we manage to create the proposal, so if there's
862
+ // a HA error we don't pollute our archiver with a block that won't make it to the chain.
863
+ const proposal = await this.createBlockProposal(block, inHash, usedTxs, blockProposalOptions);
864
+ // Sync the proposed block to the archiver to make it available, only after we've managed to sign the proposal.
865
+ // We wait for the sync to succeed, as this helps catch consistency errors, even if it means we lose some time for block-building.
866
+ // If this throws, we abort the entire checkpoint.
867
+ await this.syncProposedBlockToArchiver(block);
868
+ // Once we have a signed proposal and the archiver agreed with our proposed block, then we broadcast it.
869
+ proposal && await this.p2pClient.broadcastProposal(proposal);
870
+ // Wait until the next block's start time
871
+ await this.waitUntilNextSubslot(timingInfo.deadline);
872
+ }
873
+ this.log.verbose(`Block building loop completed for slot ${this.slot}`, {
874
+ slot: this.slot,
875
+ blocksBuilt: blocksInCheckpoint.length
876
+ });
877
+ return {
878
+ blocksInCheckpoint,
879
+ blockPendingBroadcast
880
+ };
881
+ }
882
+ /** Creates a block proposal for a given block via the validator client (unless in fisherman mode) */ createBlockProposal(block, inHash, usedTxs, blockProposalOptions) {
883
+ if (this.config.fishermanMode) {
884
+ this.log.info(`Skipping block proposal for block ${block.number} in fisherman mode`);
885
+ return Promise.resolve(undefined);
886
+ }
887
+ return this.validatorClient.createBlockProposal(block.header, block.indexWithinCheckpoint, inHash, block.archive.root, usedTxs, this.proposer, blockProposalOptions);
888
+ }
889
+ /** Sleeps until it is time to produce the next block in the slot */ async waitUntilNextSubslot(nextSubslotStart) {
890
+ this.setStateFn(SequencerState.WAITING_UNTIL_NEXT_BLOCK, this.slot);
891
+ this.log.verbose(`Waiting until time for the next block at ${nextSubslotStart}s into slot`, {
892
+ slot: this.slot
893
+ });
894
+ await this.waitUntilTimeInSlot(nextSubslotStart);
895
+ }
896
+ /** Builds a single block. Called from the main block building loop. */ async buildSingleBlock(checkpointBuilder, opts) {
897
+ const { blockTimestamp, forceCreate, blockNumber, indexWithinCheckpoint, buildDeadline, txHashesAlreadyIncluded } = opts;
898
+ this.log.verbose(`Preparing block ${blockNumber} index ${indexWithinCheckpoint} at checkpoint ${this.checkpointNumber} for slot ${this.slot}`, {
899
+ ...checkpointBuilder.getConstantData(),
900
+ ...opts
901
+ });
902
+ try {
903
+ // Wait until we have enough txs to build the block
904
+ const { availableTxs, canStartBuilding, minTxs } = await this.waitForMinTxs(opts);
905
+ if (!canStartBuilding) {
906
+ this.log.warn(`Not enough txs to build block ${blockNumber} at index ${indexWithinCheckpoint} in slot ${this.slot} (got ${availableTxs} txs but needs ${minTxs})`, {
907
+ blockNumber,
908
+ slot: this.slot,
909
+ indexWithinCheckpoint
910
+ });
911
+ this.eventEmitter.emit('block-tx-count-check-failed', {
912
+ minTxs,
913
+ availableTxs,
914
+ slot: this.slot
915
+ });
916
+ this.metrics.recordBlockProposalFailed('insufficient_txs');
917
+ return undefined;
918
+ }
919
+ // Create iterator to pending txs. We filter out txs already included in previous blocks in the checkpoint
920
+ // just in case p2p failed to sync the provisional block and didn't get to remove those txs from the mempool yet.
921
+ const pendingTxs = filter(this.p2pClient.iterateEligiblePendingTxs(), (tx)=>!txHashesAlreadyIncluded.has(tx.txHash.toString()));
922
+ this.log.debug(`Building block ${blockNumber} at index ${indexWithinCheckpoint} for slot ${this.slot} with ${availableTxs} available txs`, {
923
+ slot: this.slot,
924
+ blockNumber,
925
+ indexWithinCheckpoint
926
+ });
927
+ this.setStateFn(SequencerState.CREATING_BLOCK, this.slot);
928
+ // Per-block limits are operator overrides (from SEQ_MAX_L2_BLOCK_GAS etc.) further capped
929
+ // by remaining checkpoint-level budgets inside CheckpointBuilder before each block is built.
930
+ // minValidTxs is passed into the builder so it can reject the block *before* updating state.
931
+ const minValidTxs = forceCreate ? 0 : this.config.minValidTxsPerBlock ?? minTxs;
932
+ const blockBuilderOptions = {
933
+ maxTransactions: this.config.maxTxsPerBlock,
934
+ maxBlockGas: this.config.maxL2BlockGas !== undefined || this.config.maxDABlockGas !== undefined ? new Gas(this.config.maxDABlockGas ?? Infinity, this.config.maxL2BlockGas ?? Infinity) : undefined,
935
+ deadline: buildDeadline,
936
+ isBuildingProposal: true,
937
+ minValidTxs,
938
+ maxBlocksPerCheckpoint: this.timetable.maxNumberOfBlocks,
939
+ perBlockAllocationMultiplier: this.config.perBlockAllocationMultiplier
940
+ };
941
+ // Actually build the block by executing txs. The builder throws InsufficientValidTxsError
942
+ // if the number of successfully processed txs is below minValidTxs, ensuring state is not
943
+ // updated for blocks that will be discarded.
944
+ const buildResult = await this.buildSingleBlockWithCheckpointBuilder(checkpointBuilder, pendingTxs, blockNumber, blockTimestamp, blockBuilderOptions);
945
+ // If any txs failed during execution, drop them from the mempool so we don't pick them up again
946
+ await this.dropFailedTxsFromP2P(buildResult.failedTxs);
947
+ if (buildResult.status === 'insufficient-valid-txs') {
948
+ this.log.warn(`Block ${blockNumber} at index ${indexWithinCheckpoint} on slot ${this.slot} has too few valid txs to be proposed`, {
949
+ slot: this.slot,
950
+ blockNumber,
951
+ numTxs: buildResult.processedCount,
952
+ indexWithinCheckpoint,
953
+ minValidTxs
954
+ });
955
+ this.eventEmitter.emit('block-build-failed', {
956
+ reason: `Insufficient valid txs`,
957
+ slot: this.slot
958
+ });
959
+ this.metrics.recordBlockProposalFailed('insufficient_valid_txs');
960
+ return undefined;
961
+ }
962
+ // Block creation succeeded, emit stats and metrics
963
+ const { block, publicProcessorDuration, usedTxs, blockBuildDuration, numTxs } = buildResult;
964
+ const blockStats = {
965
+ eventName: 'l2-block-built',
966
+ duration: blockBuildDuration,
967
+ publicProcessDuration: publicProcessorDuration,
968
+ ...block.getStats()
969
+ };
970
+ const blockHash = await block.hash();
971
+ const txHashes = block.body.txEffects.map((tx)=>tx.txHash);
972
+ const manaPerSec = block.header.totalManaUsed.toNumberUnsafe() / (blockBuildDuration / 1000);
973
+ this.log.info(`Built block ${block.number} at checkpoint ${this.checkpointNumber} for slot ${this.slot} with ${numTxs} txs`, {
974
+ blockHash,
975
+ txHashes,
976
+ manaPerSec,
977
+ ...blockStats
978
+ });
979
+ this.eventEmitter.emit('block-proposed', {
980
+ blockNumber: block.number,
981
+ slot: this.slot
982
+ });
983
+ this.metrics.recordBuiltBlock(blockBuildDuration, block.header.totalManaUsed.toNumberUnsafe());
984
+ return {
985
+ block,
986
+ usedTxs
987
+ };
988
+ } catch (err) {
989
+ this.eventEmitter.emit('block-build-failed', {
990
+ reason: err.message,
991
+ slot: this.slot
992
+ });
993
+ this.log.error(`Error building block`, err, {
994
+ blockNumber,
995
+ slot: this.slot
996
+ });
997
+ this.metrics.recordBlockProposalFailed(err.name || 'unknown_error');
998
+ this.metrics.recordFailedBlock();
999
+ return {
1000
+ error: err
1001
+ };
1002
+ }
1003
+ }
1004
+ /** Uses the checkpoint builder to build a block, catching InsufficientValidTxsError. */ async buildSingleBlockWithCheckpointBuilder(checkpointBuilder, pendingTxs, blockNumber, blockTimestamp, blockBuilderOptions) {
1005
+ try {
1006
+ const workTimer = new Timer();
1007
+ const result = await checkpointBuilder.buildBlock(pendingTxs, blockNumber, blockTimestamp, blockBuilderOptions);
1008
+ const blockBuildDuration = workTimer.ms();
1009
+ return {
1010
+ ...result,
1011
+ blockBuildDuration,
1012
+ status: 'success'
1013
+ };
1014
+ } catch (err) {
1015
+ if (isErrorClass(err, InsufficientValidTxsError)) {
1016
+ return {
1017
+ failedTxs: err.failedTxs,
1018
+ processedCount: err.processedCount,
1019
+ status: 'insufficient-valid-txs'
1020
+ };
1021
+ }
1022
+ throw err;
1023
+ }
1024
+ }
1025
+ /** Waits until minTxs are available on the pool for building a block. */ async waitForMinTxs(opts) {
1026
+ const { indexWithinCheckpoint, blockNumber, buildDeadline, forceCreate } = opts;
1027
+ // We only allow a block with 0 txs in the first block of the checkpoint
1028
+ const minTxs = indexWithinCheckpoint > 0 && this.config.minTxsPerBlock === 0 ? 1 : this.config.minTxsPerBlock;
1029
+ // Deadline is undefined if we are not enforcing the timetable, meaning we'll exit immediately when out of time
1030
+ const startBuildingDeadline = buildDeadline ? new Date(buildDeadline.getTime() - this.timetable.minExecutionTime * 1000) : undefined;
1031
+ let availableTxs = await this.p2pClient.getPendingTxCount();
1032
+ while(!forceCreate && availableTxs < minTxs){
1033
+ // If we're past deadline, or we have no deadline, give up
1034
+ const now = this.dateProvider.nowAsDate();
1035
+ if (startBuildingDeadline === undefined || now >= startBuildingDeadline) {
1036
+ return {
1037
+ canStartBuilding: false,
1038
+ availableTxs,
1039
+ minTxs
1040
+ };
1041
+ }
1042
+ // Wait a bit before checking again
1043
+ this.setStateFn(SequencerState.WAITING_FOR_TXS, this.slot);
1044
+ this.log.verbose(`Waiting for enough txs to build block ${blockNumber} at index ${indexWithinCheckpoint} in slot ${this.slot} (have ${availableTxs} but need ${minTxs})`, {
1045
+ blockNumber,
1046
+ slot: this.slot,
1047
+ indexWithinCheckpoint
1048
+ });
1049
+ await this.waitForTxsPollingInterval();
1050
+ availableTxs = await this.p2pClient.getPendingTxCount();
1051
+ }
1052
+ return {
1053
+ canStartBuilding: true,
1054
+ availableTxs,
1055
+ minTxs
1056
+ };
1057
+ }
1058
+ /**
1059
+ * Waits for enough attestations to be collected via p2p.
1060
+ * This is run after all blocks for the checkpoint have been built.
1061
+ */ async waitForAttestations(proposal) {
1062
+ if (this.config.fishermanMode) {
1063
+ this.log.debug('Skipping attestation collection in fisherman mode');
1064
+ return CommitteeAttestationsAndSigners.empty();
1065
+ }
1066
+ const slotNumber = proposal.slotNumber;
1067
+ const { committee, seed, epoch } = await this.epochCache.getCommittee(slotNumber);
1068
+ if (!committee) {
1069
+ throw new Error('No committee when collecting attestations');
1070
+ } else if (committee.length === 0) {
1071
+ this.log.verbose(`Attesting committee is empty`);
1072
+ return CommitteeAttestationsAndSigners.empty();
1073
+ } else {
1074
+ this.log.debug(`Attesting committee length is ${committee.length}`, {
1075
+ committee
1076
+ });
1077
+ }
1078
+ const numberOfRequiredAttestations = Math.floor(committee.length * 2 / 3) + 1;
1079
+ if (this.config.skipCollectingAttestations) {
1080
+ this.log.warn('Skipping attestation collection as per config (attesting with own keys only)');
1081
+ const attestations = await this.validatorClient?.collectOwnAttestations(proposal);
1082
+ return new CommitteeAttestationsAndSigners(orderAttestations(attestations ?? [], committee));
1083
+ }
1084
+ const attestationTimeAllowed = this.config.enforceTimeTable ? this.timetable.getMaxAllowedTime(SequencerState.PUBLISHING_CHECKPOINT) : this.l1Constants.slotDuration;
1085
+ const attestationDeadline = new Date((this.getSlotStartBuildTimestamp() + attestationTimeAllowed) * 1000);
1086
+ this.metrics.recordRequiredAttestations(numberOfRequiredAttestations, attestationTimeAllowed);
1087
+ const collectAttestationsTimer = new Timer();
1088
+ let collectedAttestationsCount = 0;
1089
+ try {
1090
+ const attestations = await this.validatorClient.collectAttestations(proposal, numberOfRequiredAttestations, attestationDeadline);
1091
+ collectedAttestationsCount = attestations.length;
1092
+ // Trim attestations to minimum required to save L1 calldata gas
1093
+ const localAddresses = this.validatorClient.getValidatorAddresses();
1094
+ const trimmed = trimAttestations(attestations, numberOfRequiredAttestations, this.attestorAddress, localAddresses);
1095
+ if (trimmed.length < attestations.length) {
1096
+ this.log.debug(`Trimmed attestations from ${attestations.length} to ${trimmed.length} for L1 submission`);
1097
+ }
1098
+ // Rollup contract requires that the signatures are provided in the order of the committee
1099
+ const sorted = orderAttestations(trimmed, committee);
1100
+ // Manipulate the attestations if we've been configured to do so
1101
+ if (this.config.injectFakeAttestation || this.config.injectHighSValueAttestation || this.config.injectUnrecoverableSignatureAttestation || this.config.shuffleAttestationOrdering) {
1102
+ return this.manipulateAttestations(proposal.slotNumber, epoch, seed, committee, sorted);
1103
+ }
1104
+ return new CommitteeAttestationsAndSigners(sorted);
1105
+ } catch (err) {
1106
+ if (err && err instanceof AttestationTimeoutError) {
1107
+ collectedAttestationsCount = err.collectedCount;
1108
+ }
1109
+ throw err;
1110
+ } finally{
1111
+ this.metrics.recordCollectedAttestations(collectedAttestationsCount, collectAttestationsTimer.ms());
1112
+ }
1113
+ }
1114
+ /** Breaks the attestations before publishing based on attack configs */ manipulateAttestations(slotNumber, epoch, seed, committee, attestations) {
1115
+ // Compute the proposer index in the committee, since we dont want to tweak it.
1116
+ // Otherwise, the L1 rollup contract will reject the block outright.
1117
+ const proposerIndex = Number(this.epochCache.computeProposerIndex(slotNumber, epoch, seed, BigInt(committee.length)));
1118
+ if (this.config.injectFakeAttestation || this.config.injectHighSValueAttestation || this.config.injectUnrecoverableSignatureAttestation) {
1119
+ // Find non-empty attestations that are not from the proposer
1120
+ const nonProposerIndices = [];
1121
+ for(let i = 0; i < attestations.length; i++){
1122
+ if (!attestations[i].signature.isEmpty() && i !== proposerIndex) {
1123
+ nonProposerIndices.push(i);
1124
+ }
1125
+ }
1126
+ if (nonProposerIndices.length > 0) {
1127
+ const targetIndex = nonProposerIndices[randomInt(nonProposerIndices.length)];
1128
+ if (this.config.injectHighSValueAttestation) {
1129
+ this.log.warn(`Injecting high-s value attestation in checkpoint for slot ${slotNumber} at index ${targetIndex}`);
1130
+ unfreeze(attestations[targetIndex]).signature = flipSignature(attestations[targetIndex].signature);
1131
+ } else if (this.config.injectUnrecoverableSignatureAttestation) {
1132
+ this.log.warn(`Injecting unrecoverable signature attestation in checkpoint for slot ${slotNumber} at index ${targetIndex}`);
1133
+ unfreeze(attestations[targetIndex]).signature = generateUnrecoverableSignature();
1134
+ } else {
1135
+ this.log.warn(`Injecting fake attestation in checkpoint for slot ${slotNumber} at index ${targetIndex}`);
1136
+ unfreeze(attestations[targetIndex]).signature = generateRecoverableSignature();
1137
+ }
1138
+ }
1139
+ return new CommitteeAttestationsAndSigners(attestations);
1140
+ }
1141
+ if (this.config.shuffleAttestationOrdering) {
1142
+ this.log.warn(`Shuffling attestation ordering in checkpoint for slot ${slotNumber} (proposer #${proposerIndex})`);
1143
+ const shuffled = [
1144
+ ...attestations
1145
+ ];
1146
+ const [i, j] = [
1147
+ (proposerIndex + 1) % shuffled.length,
1148
+ (proposerIndex + 2) % shuffled.length
1149
+ ];
1150
+ const valueI = shuffled[i];
1151
+ const valueJ = shuffled[j];
1152
+ shuffled[i] = valueJ;
1153
+ shuffled[j] = valueI;
1154
+ const signers = new CommitteeAttestationsAndSigners(attestations).getSigners();
1155
+ return new MaliciousCommitteeAttestationsAndSigners(shuffled, signers);
1156
+ }
1157
+ return new CommitteeAttestationsAndSigners(attestations);
1158
+ }
1159
+ async dropFailedTxsFromP2P(failedTxs) {
1160
+ if (failedTxs.length === 0) {
1161
+ return;
1162
+ }
1163
+ const failedTxData = failedTxs.map((fail)=>fail.tx);
1164
+ const failedTxHashes = failedTxData.map((tx)=>tx.getTxHash());
1165
+ this.log.verbose(`Dropping failed txs ${failedTxHashes.join(', ')}`);
1166
+ await this.p2pClient.handleFailedExecution(failedTxHashes);
1167
+ }
1168
+ /**
1169
+ * Adds the proposed block to the archiver so it's available via P2P.
1170
+ * Gossip doesn't echo messages back to the sender, so the proposer's archiver/world-state
1171
+ * would never receive its own block without this explicit sync.
1172
+ *
1173
+ * In fisherman mode we skip this push: the fisherman builds blocks locally for validation
1174
+ * and fee analysis only, and pushing them to the archiver causes spurious reorg cascades
1175
+ * whenever the real proposer's block arrives from L1.
1176
+ */ async syncProposedBlockToArchiver(block) {
1177
+ if (this.config.skipPushProposedBlocksToArchiver !== false || this.config.fishermanMode) {
1178
+ this.log.warn(`Skipping push of proposed block ${block.number} to archiver`, {
1179
+ blockNumber: block.number,
1180
+ slot: block.header.globalVariables.slotNumber
1181
+ });
1182
+ return;
1183
+ }
1184
+ this.log.debug(`Syncing proposed block ${block.number} to archiver`, {
1185
+ blockNumber: block.number,
1186
+ slot: block.header.globalVariables.slotNumber
1187
+ });
1188
+ await this.blockSink.addBlock(block);
1189
+ }
1190
+ /** Runs fee analysis and logs checkpoint outcome as fisherman */ async handleCheckpointEndAsFisherman(checkpoint) {
1191
+ // Perform L1 fee analysis before clearing requests
1192
+ // The callback is invoked asynchronously after the next block is mined
1193
+ const feeAnalysis = await this.publisher.analyzeL1Fees(this.slot, (analysis)=>this.metrics.recordFishermanFeeAnalysis(analysis));
1194
+ if (checkpoint) {
1195
+ this.log.info(`Validation checkpoint building SUCCEEDED for slot ${this.slot}`, {
1196
+ ...checkpoint.toCheckpointInfo(),
1197
+ ...checkpoint.getStats(),
1198
+ feeAnalysisId: feeAnalysis?.id
1199
+ });
1200
+ } else {
1201
+ this.log.warn(`Validation block building FAILED for slot ${this.slot}`, {
1202
+ slot: this.slot,
1203
+ feeAnalysisId: feeAnalysis?.id
1204
+ });
1205
+ this.metrics.recordCheckpointProposalFailed('block_build_failed');
1206
+ }
1207
+ this.publisher.clearPendingRequests();
1208
+ }
1209
+ /**
1210
+ * Helper to handle HA double-signing errors. Returns true if the error was handled (caller should yield).
1211
+ */ handleHASigningError(err, errorContext) {
1212
+ if (err instanceof DutyAlreadySignedError) {
1213
+ this.log.info(`${errorContext} for slot ${this.slot} already signed by another HA node, yielding`, {
1214
+ slot: this.slot,
1215
+ signedByNode: err.signedByNode
1216
+ });
1217
+ return true;
1218
+ }
1219
+ if (err instanceof SlashingProtectionError) {
1220
+ this.log.info(`${errorContext} for slot ${this.slot} blocked by slashing protection, yielding`, {
1221
+ slot: this.slot,
1222
+ existingMessageHash: err.existingMessageHash,
1223
+ attemptedMessageHash: err.attemptedMessageHash
1224
+ });
1225
+ return true;
1226
+ }
1227
+ return false;
1228
+ }
1229
+ /** Waits until a specific time within the current slot */ async waitUntilTimeInSlot(targetSecondsIntoSlot) {
1230
+ const slotStartTimestamp = this.getSlotStartBuildTimestamp();
1231
+ const targetTimestamp = slotStartTimestamp + targetSecondsIntoSlot;
1232
+ await sleepUntil(new Date(targetTimestamp * 1000), this.dateProvider.nowAsDate());
1233
+ }
1234
+ /** Waits the polling interval for transactions. Extracted for test overriding. */ async waitForTxsPollingInterval() {
1235
+ await sleep(TXS_POLLING_MS);
1236
+ }
1237
+ getSlotStartBuildTimestamp() {
1238
+ return getSlotStartBuildTimestamp(this.slot, this.l1Constants);
1239
+ }
1240
+ getSecondsIntoSlot() {
1241
+ const slotStartTimestamp = this.getSlotStartBuildTimestamp();
1242
+ return Number((this.dateProvider.now() / 1000 - slotStartTimestamp).toFixed(3));
1243
+ }
1244
+ getPublisher() {
1245
+ return this.publisher;
1246
+ }
1247
+ }