@aztec/sequencer-client 0.0.1-commit.fce3e4f → 0.0.1-commit.ff7989d6c

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 (96) hide show
  1. package/dest/client/sequencer-client.d.ts +21 -16
  2. package/dest/client/sequencer-client.d.ts.map +1 -1
  3. package/dest/client/sequencer-client.js +45 -27
  4. package/dest/config.d.ts +14 -8
  5. package/dest/config.d.ts.map +1 -1
  6. package/dest/config.js +83 -35
  7. package/dest/global_variable_builder/global_builder.d.ts +20 -13
  8. package/dest/global_variable_builder/global_builder.d.ts.map +1 -1
  9. package/dest/global_variable_builder/global_builder.js +51 -41
  10. package/dest/index.d.ts +2 -3
  11. package/dest/index.d.ts.map +1 -1
  12. package/dest/index.js +1 -2
  13. package/dest/publisher/config.d.ts +37 -20
  14. package/dest/publisher/config.d.ts.map +1 -1
  15. package/dest/publisher/config.js +104 -39
  16. package/dest/publisher/sequencer-publisher-factory.d.ts +15 -6
  17. package/dest/publisher/sequencer-publisher-factory.d.ts.map +1 -1
  18. package/dest/publisher/sequencer-publisher-factory.js +14 -3
  19. package/dest/publisher/sequencer-publisher-metrics.d.ts +3 -3
  20. package/dest/publisher/sequencer-publisher-metrics.d.ts.map +1 -1
  21. package/dest/publisher/sequencer-publisher-metrics.js +23 -86
  22. package/dest/publisher/sequencer-publisher.d.ts +63 -47
  23. package/dest/publisher/sequencer-publisher.d.ts.map +1 -1
  24. package/dest/publisher/sequencer-publisher.js +630 -137
  25. package/dest/sequencer/checkpoint_proposal_job.d.ts +102 -0
  26. package/dest/sequencer/checkpoint_proposal_job.d.ts.map +1 -0
  27. package/dest/sequencer/checkpoint_proposal_job.js +1213 -0
  28. package/dest/sequencer/checkpoint_voter.d.ts +35 -0
  29. package/dest/sequencer/checkpoint_voter.d.ts.map +1 -0
  30. package/dest/sequencer/checkpoint_voter.js +109 -0
  31. package/dest/sequencer/config.d.ts +3 -2
  32. package/dest/sequencer/config.d.ts.map +1 -1
  33. package/dest/sequencer/events.d.ts +46 -0
  34. package/dest/sequencer/events.d.ts.map +1 -0
  35. package/dest/sequencer/events.js +1 -0
  36. package/dest/sequencer/index.d.ts +4 -2
  37. package/dest/sequencer/index.d.ts.map +1 -1
  38. package/dest/sequencer/index.js +3 -1
  39. package/dest/sequencer/metrics.d.ts +38 -6
  40. package/dest/sequencer/metrics.d.ts.map +1 -1
  41. package/dest/sequencer/metrics.js +216 -72
  42. package/dest/sequencer/sequencer.d.ts +119 -133
  43. package/dest/sequencer/sequencer.d.ts.map +1 -1
  44. package/dest/sequencer/sequencer.js +717 -625
  45. package/dest/sequencer/timetable.d.ts +51 -14
  46. package/dest/sequencer/timetable.d.ts.map +1 -1
  47. package/dest/sequencer/timetable.js +145 -59
  48. package/dest/sequencer/types.d.ts +3 -0
  49. package/dest/sequencer/types.d.ts.map +1 -0
  50. package/dest/sequencer/types.js +1 -0
  51. package/dest/sequencer/utils.d.ts +14 -8
  52. package/dest/sequencer/utils.d.ts.map +1 -1
  53. package/dest/sequencer/utils.js +7 -4
  54. package/dest/test/index.d.ts +6 -7
  55. package/dest/test/index.d.ts.map +1 -1
  56. package/dest/test/mock_checkpoint_builder.d.ts +97 -0
  57. package/dest/test/mock_checkpoint_builder.d.ts.map +1 -0
  58. package/dest/test/mock_checkpoint_builder.js +222 -0
  59. package/dest/test/utils.d.ts +53 -0
  60. package/dest/test/utils.d.ts.map +1 -0
  61. package/dest/test/utils.js +104 -0
  62. package/package.json +32 -30
  63. package/src/client/sequencer-client.ts +54 -47
  64. package/src/config.ts +95 -44
  65. package/src/global_variable_builder/global_builder.ts +65 -61
  66. package/src/index.ts +1 -7
  67. package/src/publisher/config.ts +122 -50
  68. package/src/publisher/sequencer-publisher-factory.ts +28 -10
  69. package/src/publisher/sequencer-publisher-metrics.ts +19 -71
  70. package/src/publisher/sequencer-publisher.ts +350 -176
  71. package/src/sequencer/README.md +531 -0
  72. package/src/sequencer/checkpoint_proposal_job.ts +914 -0
  73. package/src/sequencer/checkpoint_voter.ts +130 -0
  74. package/src/sequencer/config.ts +2 -1
  75. package/src/sequencer/events.ts +27 -0
  76. package/src/sequencer/index.ts +3 -1
  77. package/src/sequencer/metrics.ts +268 -82
  78. package/src/sequencer/sequencer.ts +464 -831
  79. package/src/sequencer/timetable.ts +175 -80
  80. package/src/sequencer/types.ts +6 -0
  81. package/src/sequencer/utils.ts +18 -9
  82. package/src/test/index.ts +5 -6
  83. package/src/test/mock_checkpoint_builder.ts +320 -0
  84. package/src/test/utils.ts +167 -0
  85. package/dest/sequencer/block_builder.d.ts +0 -27
  86. package/dest/sequencer/block_builder.d.ts.map +0 -1
  87. package/dest/sequencer/block_builder.js +0 -134
  88. package/dest/tx_validator/nullifier_cache.d.ts +0 -14
  89. package/dest/tx_validator/nullifier_cache.d.ts.map +0 -1
  90. package/dest/tx_validator/nullifier_cache.js +0 -24
  91. package/dest/tx_validator/tx_validator_factory.d.ts +0 -17
  92. package/dest/tx_validator/tx_validator_factory.d.ts.map +0 -1
  93. package/dest/tx_validator/tx_validator_factory.js +0 -53
  94. package/src/sequencer/block_builder.ts +0 -222
  95. package/src/tx_validator/nullifier_cache.ts +0 -30
  96. package/src/tx_validator/tx_validator_factory.ts +0 -132
@@ -0,0 +1,1213 @@
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 { NUM_CHECKPOINT_END_MARKER_FIELDS, getNumBlockEndBlobFields } from '@aztec/blob-lib/encoding';
440
+ import { BLOBS_PER_CHECKPOINT, FIELDS_PER_BLOB } from '@aztec/constants';
441
+ import { BlockNumber, IndexWithinCheckpoint } from '@aztec/foundation/branded-types';
442
+ import { randomInt } from '@aztec/foundation/crypto/random';
443
+ import { Signature } from '@aztec/foundation/eth-signature';
444
+ import { filter } from '@aztec/foundation/iterator';
445
+ import { createLogger } from '@aztec/foundation/log';
446
+ import { sleep, sleepUntil } from '@aztec/foundation/sleep';
447
+ import { Timer } from '@aztec/foundation/timer';
448
+ import { isErrorClass, unfreeze } from '@aztec/foundation/types';
449
+ import { CommitteeAttestationsAndSigners, MaliciousCommitteeAttestationsAndSigners } from '@aztec/stdlib/block';
450
+ import { getSlotStartBuildTimestamp } from '@aztec/stdlib/epoch-helpers';
451
+ import { Gas } from '@aztec/stdlib/gas';
452
+ import { NoValidTxsError } from '@aztec/stdlib/interfaces/server';
453
+ import { computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
454
+ import { orderAttestations } from '@aztec/stdlib/p2p';
455
+ import { AttestationTimeoutError } from '@aztec/stdlib/validators';
456
+ import { Attributes, trackSpan } from '@aztec/telemetry-client';
457
+ import { DutyAlreadySignedError, SlashingProtectionError } from '@aztec/validator-ha-signer/errors';
458
+ import { CheckpointVoter } from './checkpoint_voter.js';
459
+ import { SequencerInterruptedError } from './errors.js';
460
+ import { SequencerState } from './utils.js';
461
+ /** How much time to sleep while waiting for min transactions to accumulate for a block */ const TXS_POLLING_MS = 500;
462
+ _dec = trackSpan('CheckpointProposalJob.execute'), _dec1 = trackSpan('CheckpointProposalJob.proposeCheckpoint', function() {
463
+ return {
464
+ // nullish operator needed for tests
465
+ [Attributes.COINBASE]: this.validatorClient.getCoinbaseForAttestor(this.attestorAddress)?.toString(),
466
+ [Attributes.SLOT_NUMBER]: this.slot
467
+ };
468
+ }), _dec2 = trackSpan('CheckpointProposalJob.buildBlocksForCheckpoint'), _dec3 = trackSpan('CheckpointProposalJob.waitUntilNextSubslot'), _dec4 = trackSpan('CheckpointProposalJob.buildSingleBlock'), _dec5 = trackSpan('CheckpointProposalJob.waitForMinTxs'), _dec6 = trackSpan('CheckpointProposalJob.waitForAttestations'), _dec7 = trackSpan('CheckpointProposalJob.waitUntilTimeInSlot');
469
+ /**
470
+ * Handles the execution of a checkpoint proposal after the initial preparation phase.
471
+ * This includes building blocks, collecting attestations, and publishing the checkpoint to L1,
472
+ * as well as enqueueing votes for slashing and governance proposals. This class is created from
473
+ * the Sequencer once the check for being the proposer for the slot has succeeded.
474
+ */ export class CheckpointProposalJob {
475
+ epoch;
476
+ slot;
477
+ checkpointNumber;
478
+ syncedToBlockNumber;
479
+ proposer;
480
+ publisher;
481
+ attestorAddress;
482
+ invalidateCheckpoint;
483
+ validatorClient;
484
+ globalsBuilder;
485
+ p2pClient;
486
+ worldState;
487
+ l1ToL2MessageSource;
488
+ l2BlockSource;
489
+ checkpointsBuilder;
490
+ blockSink;
491
+ l1Constants;
492
+ config;
493
+ timetable;
494
+ slasherClient;
495
+ epochCache;
496
+ dateProvider;
497
+ metrics;
498
+ eventEmitter;
499
+ setStateFn;
500
+ tracer;
501
+ static{
502
+ ({ e: [_initProto] } = _apply_decs_2203_r(this, [
503
+ [
504
+ _dec,
505
+ 2,
506
+ "execute"
507
+ ],
508
+ [
509
+ _dec1,
510
+ 2,
511
+ "proposeCheckpoint"
512
+ ],
513
+ [
514
+ _dec2,
515
+ 2,
516
+ "buildBlocksForCheckpoint"
517
+ ],
518
+ [
519
+ _dec3,
520
+ 2,
521
+ "waitUntilNextSubslot"
522
+ ],
523
+ [
524
+ _dec4,
525
+ 2,
526
+ "buildSingleBlock"
527
+ ],
528
+ [
529
+ _dec5,
530
+ 2,
531
+ "waitForMinTxs"
532
+ ],
533
+ [
534
+ _dec6,
535
+ 2,
536
+ "waitForAttestations"
537
+ ],
538
+ [
539
+ _dec7,
540
+ 2,
541
+ "waitUntilTimeInSlot"
542
+ ]
543
+ ], []));
544
+ }
545
+ log;
546
+ constructor(epoch, slot, checkpointNumber, syncedToBlockNumber, // TODO(palla/mbps): Can we remove the proposer in favor of attestorAddress? Need to check fisherman-node flows.
547
+ proposer, publisher, attestorAddress, invalidateCheckpoint, validatorClient, globalsBuilder, p2pClient, worldState, l1ToL2MessageSource, l2BlockSource, checkpointsBuilder, blockSink, l1Constants, config, timetable, slasherClient, epochCache, dateProvider, metrics, eventEmitter, setStateFn, tracer, bindings){
548
+ this.epoch = epoch;
549
+ this.slot = slot;
550
+ this.checkpointNumber = checkpointNumber;
551
+ this.syncedToBlockNumber = syncedToBlockNumber;
552
+ this.proposer = proposer;
553
+ this.publisher = publisher;
554
+ this.attestorAddress = attestorAddress;
555
+ this.invalidateCheckpoint = invalidateCheckpoint;
556
+ this.validatorClient = validatorClient;
557
+ this.globalsBuilder = globalsBuilder;
558
+ this.p2pClient = p2pClient;
559
+ this.worldState = worldState;
560
+ this.l1ToL2MessageSource = l1ToL2MessageSource;
561
+ this.l2BlockSource = l2BlockSource;
562
+ this.checkpointsBuilder = checkpointsBuilder;
563
+ this.blockSink = blockSink;
564
+ this.l1Constants = l1Constants;
565
+ this.config = config;
566
+ this.timetable = timetable;
567
+ this.slasherClient = slasherClient;
568
+ this.epochCache = epochCache;
569
+ this.dateProvider = dateProvider;
570
+ this.metrics = metrics;
571
+ this.eventEmitter = eventEmitter;
572
+ this.setStateFn = setStateFn;
573
+ this.tracer = tracer;
574
+ _initProto(this);
575
+ this.log = createLogger('sequencer:checkpoint-proposal', {
576
+ ...bindings,
577
+ instanceId: `slot-${slot}`
578
+ });
579
+ }
580
+ /**
581
+ * Executes the checkpoint proposal job.
582
+ * Returns the published checkpoint if successful, undefined otherwise.
583
+ */ async execute() {
584
+ // Enqueue governance and slashing votes (returns promises that will be awaited later)
585
+ // In fisherman mode, we simulate slashing but don't actually publish to L1
586
+ // These are constant for the whole slot, so we only enqueue them once
587
+ const votesPromises = new CheckpointVoter(this.slot, this.publisher, this.attestorAddress, this.validatorClient, this.slasherClient, this.l1Constants, this.config, this.metrics, this.log).enqueueVotes();
588
+ // Build and propose the checkpoint. This will enqueue the request on the publisher if a checkpoint is built.
589
+ const checkpoint = await this.proposeCheckpoint();
590
+ // Wait until the voting promises have resolved, so all requests are enqueued (not sent)
591
+ await Promise.all(votesPromises);
592
+ if (checkpoint) {
593
+ this.metrics.recordCheckpointProposalSuccess();
594
+ }
595
+ // Do not post anything to L1 if we are fishermen, but do perform L1 fee analysis
596
+ if (this.config.fishermanMode) {
597
+ await this.handleCheckpointEndAsFisherman(checkpoint);
598
+ return;
599
+ }
600
+ // Then send everything to L1
601
+ const l1Response = await this.publisher.sendRequests();
602
+ const proposedAction = l1Response?.successfulActions.find((a)=>a === 'propose');
603
+ if (proposedAction) {
604
+ this.eventEmitter.emit('checkpoint-published', {
605
+ checkpoint: this.checkpointNumber,
606
+ slot: this.slot
607
+ });
608
+ const coinbase = checkpoint?.header.coinbase;
609
+ await this.metrics.incFilledSlot(this.publisher.getSenderAddress().toString(), coinbase);
610
+ return checkpoint;
611
+ } else if (checkpoint) {
612
+ this.eventEmitter.emit('checkpoint-publish-failed', {
613
+ ...l1Response,
614
+ slot: this.slot
615
+ });
616
+ return undefined;
617
+ }
618
+ }
619
+ async proposeCheckpoint() {
620
+ try {
621
+ const env = {
622
+ stack: [],
623
+ error: void 0,
624
+ hasError: false
625
+ };
626
+ try {
627
+ // Get operator configured coinbase and fee recipient for this attestor
628
+ const coinbase = this.validatorClient.getCoinbaseForAttestor(this.attestorAddress);
629
+ const feeRecipient = this.validatorClient.getFeeRecipientForAttestor(this.attestorAddress);
630
+ // Start the checkpoint
631
+ this.setStateFn(SequencerState.INITIALIZING_CHECKPOINT, this.slot);
632
+ this.metrics.incOpenSlot(this.slot, this.proposer?.toString() ?? 'unknown');
633
+ // Enqueues checkpoint invalidation (constant for the whole slot)
634
+ if (this.invalidateCheckpoint && !this.config.skipInvalidateBlockAsProposer) {
635
+ this.publisher.enqueueInvalidateCheckpoint(this.invalidateCheckpoint);
636
+ }
637
+ // Create checkpoint builder for the slot
638
+ const checkpointGlobalVariables = await this.globalsBuilder.buildCheckpointGlobalVariables(coinbase, feeRecipient, this.slot);
639
+ // Collect L1 to L2 messages for the checkpoint and compute their hash
640
+ const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(this.checkpointNumber);
641
+ const inHash = computeInHashFromL1ToL2Messages(l1ToL2Messages);
642
+ // Collect the out hashes of all the checkpoints before this one in the same epoch
643
+ const previousCheckpointOutHashes = (await this.l2BlockSource.getCheckpointsDataForEpoch(this.epoch)).filter((c)=>c.checkpointNumber < this.checkpointNumber).map((c)=>c.checkpointOutHash);
644
+ // Get the fee asset price modifier from the oracle
645
+ const feeAssetPriceModifier = await this.publisher.getFeeAssetPriceModifier();
646
+ const fork = _ts_add_disposable_resource(env, await this.worldState.fork(this.syncedToBlockNumber, {
647
+ closeDelayMs: 12_000
648
+ }), true);
649
+ // Create checkpoint builder for the entire slot
650
+ const checkpointBuilder = await this.checkpointsBuilder.startCheckpoint(this.checkpointNumber, checkpointGlobalVariables, feeAssetPriceModifier, l1ToL2Messages, previousCheckpointOutHashes, fork, this.log.getBindings());
651
+ // Options for the validator client when creating block and checkpoint proposals
652
+ const blockProposalOptions = {
653
+ publishFullTxs: !!this.config.publishTxsWithProposals,
654
+ broadcastInvalidBlockProposal: this.config.broadcastInvalidBlockProposal
655
+ };
656
+ const checkpointProposalOptions = {
657
+ publishFullTxs: !!this.config.publishTxsWithProposals,
658
+ broadcastInvalidCheckpointProposal: this.config.broadcastInvalidBlockProposal
659
+ };
660
+ let blocksInCheckpoint = [];
661
+ let blockPendingBroadcast = undefined;
662
+ const checkpointBuildTimer = new Timer();
663
+ try {
664
+ // Main loop: build blocks for the checkpoint
665
+ const result = await this.buildBlocksForCheckpoint(checkpointBuilder, checkpointGlobalVariables.timestamp, inHash, blockProposalOptions);
666
+ blocksInCheckpoint = result.blocksInCheckpoint;
667
+ blockPendingBroadcast = result.blockPendingBroadcast;
668
+ } catch (err) {
669
+ // These errors are expected in HA mode, so we yield and let another HA node handle the slot
670
+ // The only distinction between the 2 errors is SlashingProtectionError throws when the payload is different,
671
+ // which is normal for block building (may have picked different txs)
672
+ if (this.handleHASigningError(err, 'Block proposal')) {
673
+ return undefined;
674
+ }
675
+ throw err;
676
+ }
677
+ if (blocksInCheckpoint.length === 0) {
678
+ this.log.warn(`No blocks were built for slot ${this.slot}`, {
679
+ slot: this.slot
680
+ });
681
+ this.eventEmitter.emit('checkpoint-empty', {
682
+ slot: this.slot
683
+ });
684
+ return undefined;
685
+ }
686
+ const minBlocksForCheckpoint = this.config.minBlocksForCheckpoint;
687
+ if (minBlocksForCheckpoint !== undefined && blocksInCheckpoint.length < minBlocksForCheckpoint) {
688
+ this.log.warn(`Checkpoint has fewer blocks than minimum (${blocksInCheckpoint.length} < ${minBlocksForCheckpoint}), skipping proposal`, {
689
+ slot: this.slot,
690
+ blocksBuilt: blocksInCheckpoint.length,
691
+ minBlocksForCheckpoint
692
+ });
693
+ return undefined;
694
+ }
695
+ // Assemble and broadcast the checkpoint proposal, including the last block that was not
696
+ // broadcasted yet, and wait to collect the committee attestations.
697
+ this.setStateFn(SequencerState.ASSEMBLING_CHECKPOINT, this.slot);
698
+ const checkpoint = await checkpointBuilder.completeCheckpoint();
699
+ // Record checkpoint-level build metrics
700
+ this.metrics.recordCheckpointBuild(checkpointBuildTimer.ms(), blocksInCheckpoint.length, checkpoint.getStats().txCount, Number(checkpoint.header.totalManaUsed.toBigInt()));
701
+ // Do not collect attestations nor publish to L1 in fisherman mode
702
+ if (this.config.fishermanMode) {
703
+ this.log.info(`Built checkpoint for slot ${this.slot} with ${blocksInCheckpoint.length} blocks. ` + `Skipping proposal in fisherman mode.`, {
704
+ slot: this.slot,
705
+ checkpoint: checkpoint.header.toInspect(),
706
+ blocksBuilt: blocksInCheckpoint.length
707
+ });
708
+ this.metrics.recordCheckpointSuccess();
709
+ return checkpoint;
710
+ }
711
+ // Include the block pending broadcast in the checkpoint proposal if any
712
+ const lastBlock = blockPendingBroadcast && {
713
+ blockHeader: blockPendingBroadcast.block.header,
714
+ indexWithinCheckpoint: blockPendingBroadcast.block.indexWithinCheckpoint,
715
+ txs: blockPendingBroadcast.txs
716
+ };
717
+ // Create the checkpoint proposal and broadcast it
718
+ const proposal = await this.validatorClient.createCheckpointProposal(checkpoint.header, checkpoint.archive.root, feeAssetPriceModifier, lastBlock, this.proposer, checkpointProposalOptions);
719
+ const blockProposedAt = this.dateProvider.now();
720
+ await this.p2pClient.broadcastCheckpointProposal(proposal);
721
+ this.setStateFn(SequencerState.COLLECTING_ATTESTATIONS, this.slot);
722
+ const attestations = await this.waitForAttestations(proposal);
723
+ const blockAttestedAt = this.dateProvider.now();
724
+ this.metrics.recordCheckpointAttestationDelay(blockAttestedAt - blockProposedAt);
725
+ // Proposer must sign over the attestations before pushing them to L1
726
+ const signer = this.proposer ?? this.publisher.getSenderAddress();
727
+ let attestationsSignature;
728
+ try {
729
+ attestationsSignature = await this.validatorClient.signAttestationsAndSigners(attestations, signer, this.slot, this.checkpointNumber);
730
+ } catch (err) {
731
+ // We shouldn't really get here since we yield to another HA node
732
+ // as soon as we see these errors when creating block or checkpoint proposals.
733
+ if (this.handleHASigningError(err, 'Attestations signature')) {
734
+ return undefined;
735
+ }
736
+ throw err;
737
+ }
738
+ // Enqueue publishing the checkpoint to L1
739
+ this.setStateFn(SequencerState.PUBLISHING_CHECKPOINT, this.slot);
740
+ const aztecSlotDuration = this.l1Constants.slotDuration;
741
+ const slotStartBuildTimestamp = this.getSlotStartBuildTimestamp();
742
+ const txTimeoutAt = new Date((slotStartBuildTimestamp + aztecSlotDuration) * 1000);
743
+ // If we have been configured to potentially skip publishing checkpoint then roll the dice here
744
+ if (this.config.skipPublishingCheckpointsPercent !== undefined && this.config.skipPublishingCheckpointsPercent > 0) {
745
+ const result = Math.max(0, randomInt(100));
746
+ if (result < this.config.skipPublishingCheckpointsPercent) {
747
+ this.log.warn(`Skipping publishing proposal for checkpoint ${checkpoint.number}. Configured percentage: ${this.config.skipPublishingCheckpointsPercent}, generated value: ${result}`);
748
+ return checkpoint;
749
+ }
750
+ }
751
+ await this.publisher.enqueueProposeCheckpoint(checkpoint, attestations, attestationsSignature, {
752
+ txTimeoutAt,
753
+ forcePendingCheckpointNumber: this.invalidateCheckpoint?.forcePendingCheckpointNumber
754
+ });
755
+ return checkpoint;
756
+ } catch (e) {
757
+ env.error = e;
758
+ env.hasError = true;
759
+ } finally{
760
+ const result = _ts_dispose_resources(env);
761
+ if (result) await result;
762
+ }
763
+ } catch (err) {
764
+ if (err && (err instanceof DutyAlreadySignedError || err instanceof SlashingProtectionError)) {
765
+ // swallow this error. It's already been logged by a function deeper in the stack
766
+ return undefined;
767
+ }
768
+ this.log.error(`Error building checkpoint at slot ${this.slot}`, err);
769
+ return undefined;
770
+ }
771
+ }
772
+ /**
773
+ * Builds blocks for a checkpoint within the current slot.
774
+ */ async buildBlocksForCheckpoint(checkpointBuilder, timestamp, inHash, blockProposalOptions) {
775
+ const blocksInCheckpoint = [];
776
+ const txHashesAlreadyIncluded = new Set();
777
+ const initialBlockNumber = BlockNumber(this.syncedToBlockNumber + 1);
778
+ // Remaining blob fields available for blocks (checkpoint end marker already subtracted)
779
+ let remainingBlobFields = BLOBS_PER_CHECKPOINT * FIELDS_PER_BLOB - NUM_CHECKPOINT_END_MARKER_FIELDS;
780
+ // Last block in the checkpoint will usually be flagged as pending broadcast, so we send it along with the checkpoint proposal
781
+ let blockPendingBroadcast = undefined;
782
+ while(true){
783
+ const blocksBuilt = blocksInCheckpoint.length;
784
+ const indexWithinCheckpoint = IndexWithinCheckpoint(blocksBuilt);
785
+ const blockNumber = BlockNumber(initialBlockNumber + blocksBuilt);
786
+ const secondsIntoSlot = this.getSecondsIntoSlot();
787
+ const timingInfo = this.timetable.canStartNextBlock(secondsIntoSlot);
788
+ if (!timingInfo.canStart) {
789
+ this.log.debug(`Not enough time left in slot to start another block`, {
790
+ slot: this.slot,
791
+ blocksBuilt,
792
+ secondsIntoSlot
793
+ });
794
+ break;
795
+ }
796
+ const buildResult = await this.buildSingleBlock(checkpointBuilder, {
797
+ // Create all blocks with the same timestamp
798
+ blockTimestamp: timestamp,
799
+ // Create an empty block if we haven't already and this is the last one
800
+ forceCreate: timingInfo.isLastBlock && blocksBuilt === 0 && this.config.buildCheckpointIfEmpty,
801
+ // Build deadline is only set if we are enforcing the timetable
802
+ buildDeadline: timingInfo.deadline ? new Date((this.getSlotStartBuildTimestamp() + timingInfo.deadline) * 1000) : undefined,
803
+ blockNumber,
804
+ indexWithinCheckpoint,
805
+ txHashesAlreadyIncluded,
806
+ remainingBlobFields
807
+ });
808
+ // TODO(palla/mbps): Review these conditions. We may want to keep trying in some scenarios.
809
+ if (!buildResult && timingInfo.isLastBlock) {
810
+ break;
811
+ } else if (!buildResult && timingInfo.deadline !== undefined) {
812
+ // But if there is still time for more blocks, wait until the next subslot and try again
813
+ await this.waitUntilNextSubslot(timingInfo.deadline);
814
+ continue;
815
+ } else if (!buildResult) {
816
+ break;
817
+ } else if ('error' in buildResult) {
818
+ // If there was an error building the block, just exit the loop and give up the rest of the slot
819
+ if (!(buildResult.error instanceof SequencerInterruptedError)) {
820
+ this.log.warn(`Halting block building for slot ${this.slot}`, {
821
+ slot: this.slot,
822
+ blocksBuilt,
823
+ error: buildResult.error
824
+ });
825
+ }
826
+ break;
827
+ }
828
+ const { block, usedTxs, remainingBlobFields: newRemainingBlobFields } = buildResult;
829
+ blocksInCheckpoint.push(block);
830
+ // Update remaining blob fields for the next block
831
+ remainingBlobFields = newRemainingBlobFields;
832
+ // Sync the proposed block to the archiver to make it available
833
+ // Note that the checkpoint builder uses its own fork so it should not need to wait for this syncing
834
+ // Eventually we should refactor the checkpoint builder to not need a separate long-lived fork
835
+ // Fire and forget - don't block the critical path, but log errors
836
+ this.syncProposedBlockToArchiver(block).catch((err)=>{
837
+ this.log.error(`Failed to sync proposed block ${block.number} to archiver`, {
838
+ blockNumber: block.number,
839
+ err
840
+ });
841
+ });
842
+ usedTxs.forEach((tx)=>txHashesAlreadyIncluded.add(tx.txHash.toString()));
843
+ // If this is the last block, exit the loop now so we start collecting attestations
844
+ if (timingInfo.isLastBlock) {
845
+ this.log.verbose(`Completed final block ${blockNumber} for slot ${this.slot}`, {
846
+ slot: this.slot,
847
+ blockNumber,
848
+ blocksBuilt
849
+ });
850
+ blockPendingBroadcast = {
851
+ block,
852
+ txs: usedTxs
853
+ };
854
+ break;
855
+ }
856
+ // For non-last blocks, broadcast the block proposal (unless we're in fisherman mode)
857
+ // If the block is the last one, we'll broadcast it along with the checkpoint at the end of the loop
858
+ if (!this.config.fishermanMode) {
859
+ const proposal = await this.validatorClient.createBlockProposal(block.header, block.indexWithinCheckpoint, inHash, block.archive.root, usedTxs, this.proposer, blockProposalOptions);
860
+ await this.p2pClient.broadcastProposal(proposal);
861
+ }
862
+ // Wait until the next block's start time
863
+ await this.waitUntilNextSubslot(timingInfo.deadline);
864
+ }
865
+ this.log.verbose(`Block building loop completed for slot ${this.slot}`, {
866
+ slot: this.slot,
867
+ blocksBuilt: blocksInCheckpoint.length
868
+ });
869
+ return {
870
+ blocksInCheckpoint,
871
+ blockPendingBroadcast
872
+ };
873
+ }
874
+ /** Sleeps until it is time to produce the next block in the slot */ async waitUntilNextSubslot(nextSubslotStart) {
875
+ this.setStateFn(SequencerState.WAITING_UNTIL_NEXT_BLOCK, this.slot);
876
+ this.log.verbose(`Waiting until time for the next block at ${nextSubslotStart}s into slot`, {
877
+ slot: this.slot
878
+ });
879
+ await this.waitUntilTimeInSlot(nextSubslotStart);
880
+ }
881
+ /** Builds a single block. Called from the main block building loop. */ async buildSingleBlock(checkpointBuilder, opts) {
882
+ const { blockTimestamp, forceCreate, blockNumber, indexWithinCheckpoint, buildDeadline, txHashesAlreadyIncluded, remainingBlobFields } = opts;
883
+ this.log.verbose(`Preparing block ${blockNumber} index ${indexWithinCheckpoint} at checkpoint ${this.checkpointNumber} for slot ${this.slot}`, {
884
+ ...checkpointBuilder.getConstantData(),
885
+ ...opts
886
+ });
887
+ try {
888
+ // Wait until we have enough txs to build the block
889
+ const minTxs = this.config.minTxsPerBlock;
890
+ const { availableTxs, canStartBuilding } = await this.waitForMinTxs(opts);
891
+ if (!canStartBuilding) {
892
+ this.log.warn(`Not enough txs to build block ${blockNumber} at index ${indexWithinCheckpoint} in slot ${this.slot} (got ${availableTxs} txs but needs ${minTxs})`, {
893
+ blockNumber,
894
+ slot: this.slot,
895
+ indexWithinCheckpoint
896
+ });
897
+ this.eventEmitter.emit('block-tx-count-check-failed', {
898
+ minTxs,
899
+ availableTxs,
900
+ slot: this.slot
901
+ });
902
+ this.metrics.recordBlockProposalFailed('insufficient_txs');
903
+ return undefined;
904
+ }
905
+ // Create iterator to pending txs. We filter out txs already included in previous blocks in the checkpoint
906
+ // just in case p2p failed to sync the provisional block and didn't get to remove those txs from the mempool yet.
907
+ const pendingTxs = filter(this.p2pClient.iterateEligiblePendingTxs(), (tx)=>!txHashesAlreadyIncluded.has(tx.txHash.toString()));
908
+ this.log.debug(`Building block ${blockNumber} at index ${indexWithinCheckpoint} for slot ${this.slot} with ${availableTxs} available txs`, {
909
+ slot: this.slot,
910
+ blockNumber,
911
+ indexWithinCheckpoint
912
+ });
913
+ this.setStateFn(SequencerState.CREATING_BLOCK, this.slot);
914
+ // Calculate blob fields limit for txs (remaining capacity - this block's end overhead)
915
+ const blockEndOverhead = getNumBlockEndBlobFields(indexWithinCheckpoint === 0);
916
+ const maxBlobFieldsForTxs = remainingBlobFields - blockEndOverhead;
917
+ const blockBuilderOptions = {
918
+ maxTransactions: this.config.maxTxsPerBlock,
919
+ maxBlockSize: this.config.maxBlockSizeInBytes,
920
+ maxBlockGas: new Gas(this.config.maxDABlockGas, this.config.maxL2BlockGas),
921
+ maxBlobFields: maxBlobFieldsForTxs,
922
+ deadline: buildDeadline
923
+ };
924
+ // Actually build the block by executing txs
925
+ const buildResult = await this.buildSingleBlockWithCheckpointBuilder(checkpointBuilder, pendingTxs, blockNumber, blockTimestamp, blockBuilderOptions);
926
+ // If any txs failed during execution, drop them from the mempool so we don't pick them up again
927
+ await this.dropFailedTxsFromP2P(buildResult.failedTxs);
928
+ // Check if we have created a block with enough txs. If there were invalid txs in the pool, or if execution took
929
+ // too long, then we may not get to minTxsPerBlock after executing public functions.
930
+ const minValidTxs = this.config.minValidTxsPerBlock ?? minTxs;
931
+ const numTxs = buildResult.status === 'no-valid-txs' ? 0 : buildResult.numTxs;
932
+ if (buildResult.status === 'no-valid-txs' || !forceCreate && numTxs < minValidTxs) {
933
+ this.log.warn(`Block ${blockNumber} at index ${indexWithinCheckpoint} on slot ${this.slot} has too few valid txs to be proposed`, {
934
+ slot: this.slot,
935
+ blockNumber,
936
+ numTxs,
937
+ indexWithinCheckpoint,
938
+ minValidTxs,
939
+ buildResult: buildResult.status
940
+ });
941
+ this.eventEmitter.emit('block-build-failed', {
942
+ reason: `Insufficient valid txs`,
943
+ slot: this.slot
944
+ });
945
+ this.metrics.recordBlockProposalFailed('insufficient_valid_txs');
946
+ return undefined;
947
+ }
948
+ // Block creation succeeded, emit stats and metrics
949
+ const { publicGas, block, publicProcessorDuration, usedTxs, usedTxBlobFields, blockBuildDuration } = buildResult;
950
+ const blockStats = {
951
+ eventName: 'l2-block-built',
952
+ duration: blockBuildDuration,
953
+ publicProcessDuration: publicProcessorDuration,
954
+ ...block.getStats()
955
+ };
956
+ const blockHash = await block.hash();
957
+ const txHashes = block.body.txEffects.map((tx)=>tx.txHash);
958
+ const manaPerSec = publicGas.l2Gas / (blockBuildDuration / 1000);
959
+ this.log.info(`Built block ${block.number} at checkpoint ${this.checkpointNumber} for slot ${this.slot} with ${numTxs} txs`, {
960
+ blockHash,
961
+ txHashes,
962
+ manaPerSec,
963
+ ...blockStats
964
+ });
965
+ this.eventEmitter.emit('block-proposed', {
966
+ blockNumber: block.number,
967
+ slot: this.slot
968
+ });
969
+ this.metrics.recordBuiltBlock(blockBuildDuration, publicGas.l2Gas);
970
+ return {
971
+ block,
972
+ usedTxs,
973
+ remainingBlobFields: maxBlobFieldsForTxs - usedTxBlobFields
974
+ };
975
+ } catch (err) {
976
+ this.eventEmitter.emit('block-build-failed', {
977
+ reason: err.message,
978
+ slot: this.slot
979
+ });
980
+ this.log.error(`Error building block`, err, {
981
+ blockNumber,
982
+ slot: this.slot
983
+ });
984
+ this.metrics.recordBlockProposalFailed(err.name || 'unknown_error');
985
+ this.metrics.recordFailedBlock();
986
+ return {
987
+ error: err
988
+ };
989
+ }
990
+ }
991
+ /** Uses the checkpoint builder to build a block, catching specific txs */ async buildSingleBlockWithCheckpointBuilder(checkpointBuilder, pendingTxs, blockNumber, blockTimestamp, blockBuilderOptions) {
992
+ try {
993
+ const workTimer = new Timer();
994
+ const result = await checkpointBuilder.buildBlock(pendingTxs, blockNumber, blockTimestamp, blockBuilderOptions);
995
+ const blockBuildDuration = workTimer.ms();
996
+ return {
997
+ ...result,
998
+ blockBuildDuration,
999
+ status: 'success'
1000
+ };
1001
+ } catch (err) {
1002
+ if (isErrorClass(err, NoValidTxsError)) {
1003
+ return {
1004
+ failedTxs: err.failedTxs,
1005
+ status: 'no-valid-txs'
1006
+ };
1007
+ }
1008
+ throw err;
1009
+ }
1010
+ }
1011
+ /** Waits until minTxs are available on the pool for building a block. */ async waitForMinTxs(opts) {
1012
+ const { indexWithinCheckpoint, blockNumber, buildDeadline, forceCreate } = opts;
1013
+ // We only allow a block with 0 txs in the first block of the checkpoint
1014
+ const minTxs = indexWithinCheckpoint > 0 && this.config.minTxsPerBlock === 0 ? 1 : this.config.minTxsPerBlock;
1015
+ // Deadline is undefined if we are not enforcing the timetable, meaning we'll exit immediately when out of time
1016
+ const startBuildingDeadline = buildDeadline ? new Date(buildDeadline.getTime() - this.timetable.minExecutionTime * 1000) : undefined;
1017
+ let availableTxs = await this.p2pClient.getPendingTxCount();
1018
+ while(!forceCreate && availableTxs < minTxs){
1019
+ // If we're past deadline, or we have no deadline, give up
1020
+ const now = this.dateProvider.nowAsDate();
1021
+ if (startBuildingDeadline === undefined || now >= startBuildingDeadline) {
1022
+ return {
1023
+ canStartBuilding: false,
1024
+ availableTxs: availableTxs
1025
+ };
1026
+ }
1027
+ // Wait a bit before checking again
1028
+ this.setStateFn(SequencerState.WAITING_FOR_TXS, this.slot);
1029
+ this.log.verbose(`Waiting for enough txs to build block ${blockNumber} at index ${indexWithinCheckpoint} in slot ${this.slot} (have ${availableTxs} but need ${minTxs})`, {
1030
+ blockNumber,
1031
+ slot: this.slot,
1032
+ indexWithinCheckpoint
1033
+ });
1034
+ await this.waitForTxsPollingInterval();
1035
+ availableTxs = await this.p2pClient.getPendingTxCount();
1036
+ }
1037
+ return {
1038
+ canStartBuilding: true,
1039
+ availableTxs
1040
+ };
1041
+ }
1042
+ /**
1043
+ * Waits for enough attestations to be collected via p2p.
1044
+ * This is run after all blocks for the checkpoint have been built.
1045
+ */ async waitForAttestations(proposal) {
1046
+ if (this.config.fishermanMode) {
1047
+ this.log.debug('Skipping attestation collection in fisherman mode');
1048
+ return CommitteeAttestationsAndSigners.empty();
1049
+ }
1050
+ const slotNumber = proposal.slotNumber;
1051
+ const { committee, seed, epoch } = await this.epochCache.getCommittee(slotNumber);
1052
+ if (!committee) {
1053
+ throw new Error('No committee when collecting attestations');
1054
+ } else if (committee.length === 0) {
1055
+ this.log.verbose(`Attesting committee is empty`);
1056
+ return CommitteeAttestationsAndSigners.empty();
1057
+ } else {
1058
+ this.log.debug(`Attesting committee length is ${committee.length}`, {
1059
+ committee
1060
+ });
1061
+ }
1062
+ const numberOfRequiredAttestations = Math.floor(committee.length * 2 / 3) + 1;
1063
+ if (this.config.skipCollectingAttestations) {
1064
+ this.log.warn('Skipping attestation collection as per config (attesting with own keys only)');
1065
+ const attestations = await this.validatorClient?.collectOwnAttestations(proposal);
1066
+ return new CommitteeAttestationsAndSigners(orderAttestations(attestations ?? [], committee));
1067
+ }
1068
+ const attestationTimeAllowed = this.config.enforceTimeTable ? this.timetable.getMaxAllowedTime(SequencerState.PUBLISHING_CHECKPOINT) : this.l1Constants.slotDuration;
1069
+ const attestationDeadline = new Date((this.getSlotStartBuildTimestamp() + attestationTimeAllowed) * 1000);
1070
+ this.metrics.recordRequiredAttestations(numberOfRequiredAttestations, attestationTimeAllowed);
1071
+ const collectAttestationsTimer = new Timer();
1072
+ let collectedAttestationsCount = 0;
1073
+ try {
1074
+ const attestations = await this.validatorClient.collectAttestations(proposal, numberOfRequiredAttestations, attestationDeadline);
1075
+ collectedAttestationsCount = attestations.length;
1076
+ // Rollup contract requires that the signatures are provided in the order of the committee
1077
+ const sorted = orderAttestations(attestations, committee);
1078
+ // Manipulate the attestations if we've been configured to do so
1079
+ if (this.config.injectFakeAttestation || this.config.shuffleAttestationOrdering) {
1080
+ return this.manipulateAttestations(proposal.slotNumber, epoch, seed, committee, sorted);
1081
+ }
1082
+ return new CommitteeAttestationsAndSigners(sorted);
1083
+ } catch (err) {
1084
+ if (err && err instanceof AttestationTimeoutError) {
1085
+ collectedAttestationsCount = err.collectedCount;
1086
+ }
1087
+ throw err;
1088
+ } finally{
1089
+ this.metrics.recordCollectedAttestations(collectedAttestationsCount, collectAttestationsTimer.ms());
1090
+ }
1091
+ }
1092
+ /** Breaks the attestations before publishing based on attack configs */ manipulateAttestations(slotNumber, epoch, seed, committee, attestations) {
1093
+ // Compute the proposer index in the committee, since we dont want to tweak it.
1094
+ // Otherwise, the L1 rollup contract will reject the block outright.
1095
+ const proposerIndex = Number(this.epochCache.computeProposerIndex(slotNumber, epoch, seed, BigInt(committee.length)));
1096
+ if (this.config.injectFakeAttestation) {
1097
+ // Find non-empty attestations that are not from the proposer
1098
+ const nonProposerIndices = [];
1099
+ for(let i = 0; i < attestations.length; i++){
1100
+ if (!attestations[i].signature.isEmpty() && i !== proposerIndex) {
1101
+ nonProposerIndices.push(i);
1102
+ }
1103
+ }
1104
+ if (nonProposerIndices.length > 0) {
1105
+ const targetIndex = nonProposerIndices[randomInt(nonProposerIndices.length)];
1106
+ this.log.warn(`Injecting fake attestation in checkpoint for slot ${slotNumber} at index ${targetIndex}`);
1107
+ unfreeze(attestations[targetIndex]).signature = Signature.random();
1108
+ }
1109
+ return new CommitteeAttestationsAndSigners(attestations);
1110
+ }
1111
+ if (this.config.shuffleAttestationOrdering) {
1112
+ this.log.warn(`Shuffling attestation ordering in checkpoint for slot ${slotNumber} (proposer #${proposerIndex})`);
1113
+ const shuffled = [
1114
+ ...attestations
1115
+ ];
1116
+ const [i, j] = [
1117
+ (proposerIndex + 1) % shuffled.length,
1118
+ (proposerIndex + 2) % shuffled.length
1119
+ ];
1120
+ const valueI = shuffled[i];
1121
+ const valueJ = shuffled[j];
1122
+ shuffled[i] = valueJ;
1123
+ shuffled[j] = valueI;
1124
+ const signers = new CommitteeAttestationsAndSigners(attestations).getSigners();
1125
+ return new MaliciousCommitteeAttestationsAndSigners(shuffled, signers);
1126
+ }
1127
+ return new CommitteeAttestationsAndSigners(attestations);
1128
+ }
1129
+ async dropFailedTxsFromP2P(failedTxs) {
1130
+ if (failedTxs.length === 0) {
1131
+ return;
1132
+ }
1133
+ const failedTxData = failedTxs.map((fail)=>fail.tx);
1134
+ const failedTxHashes = failedTxData.map((tx)=>tx.getTxHash());
1135
+ this.log.verbose(`Dropping failed txs ${failedTxHashes.join(', ')}`);
1136
+ await this.p2pClient.handleFailedExecution(failedTxHashes);
1137
+ }
1138
+ /**
1139
+ * Adds the proposed block to the archiver so it's available via P2P.
1140
+ * Gossip doesn't echo messages back to the sender, so the proposer's archiver/world-state
1141
+ * would never receive its own block without this explicit sync.
1142
+ */ async syncProposedBlockToArchiver(block) {
1143
+ if (this.config.skipPushProposedBlocksToArchiver !== false) {
1144
+ this.log.warn(`Skipping push of proposed block ${block.number} to archiver`, {
1145
+ blockNumber: block.number,
1146
+ slot: block.header.globalVariables.slotNumber
1147
+ });
1148
+ return;
1149
+ }
1150
+ this.log.debug(`Syncing proposed block ${block.number} to archiver`, {
1151
+ blockNumber: block.number,
1152
+ slot: block.header.globalVariables.slotNumber
1153
+ });
1154
+ await this.blockSink.addBlock(block);
1155
+ }
1156
+ /** Runs fee analysis and logs checkpoint outcome as fisherman */ async handleCheckpointEndAsFisherman(checkpoint) {
1157
+ // Perform L1 fee analysis before clearing requests
1158
+ // The callback is invoked asynchronously after the next block is mined
1159
+ const feeAnalysis = await this.publisher.analyzeL1Fees(this.slot, (analysis)=>this.metrics.recordFishermanFeeAnalysis(analysis));
1160
+ if (checkpoint) {
1161
+ this.log.info(`Validation checkpoint building SUCCEEDED for slot ${this.slot}`, {
1162
+ ...checkpoint.toCheckpointInfo(),
1163
+ ...checkpoint.getStats(),
1164
+ feeAnalysisId: feeAnalysis?.id
1165
+ });
1166
+ } else {
1167
+ this.log.warn(`Validation block building FAILED for slot ${this.slot}`, {
1168
+ slot: this.slot,
1169
+ feeAnalysisId: feeAnalysis?.id
1170
+ });
1171
+ this.metrics.recordCheckpointProposalFailed('block_build_failed');
1172
+ }
1173
+ this.publisher.clearPendingRequests();
1174
+ }
1175
+ /**
1176
+ * Helper to handle HA double-signing errors. Returns true if the error was handled (caller should yield).
1177
+ */ handleHASigningError(err, errorContext) {
1178
+ if (err instanceof DutyAlreadySignedError) {
1179
+ this.log.info(`${errorContext} for slot ${this.slot} already signed by another HA node, yielding`, {
1180
+ slot: this.slot,
1181
+ signedByNode: err.signedByNode
1182
+ });
1183
+ return true;
1184
+ }
1185
+ if (err instanceof SlashingProtectionError) {
1186
+ this.log.info(`${errorContext} for slot ${this.slot} blocked by slashing protection, yielding`, {
1187
+ slot: this.slot,
1188
+ existingMessageHash: err.existingMessageHash,
1189
+ attemptedMessageHash: err.attemptedMessageHash
1190
+ });
1191
+ return true;
1192
+ }
1193
+ return false;
1194
+ }
1195
+ /** Waits until a specific time within the current slot */ async waitUntilTimeInSlot(targetSecondsIntoSlot) {
1196
+ const slotStartTimestamp = this.getSlotStartBuildTimestamp();
1197
+ const targetTimestamp = slotStartTimestamp + targetSecondsIntoSlot;
1198
+ await sleepUntil(new Date(targetTimestamp * 1000), this.dateProvider.nowAsDate());
1199
+ }
1200
+ /** Waits the polling interval for transactions. Extracted for test overriding. */ async waitForTxsPollingInterval() {
1201
+ await sleep(TXS_POLLING_MS);
1202
+ }
1203
+ getSlotStartBuildTimestamp() {
1204
+ return getSlotStartBuildTimestamp(this.slot, this.l1Constants);
1205
+ }
1206
+ getSecondsIntoSlot() {
1207
+ const slotStartTimestamp = this.getSlotStartBuildTimestamp();
1208
+ return Number((this.dateProvider.now() / 1000 - slotStartTimestamp).toFixed(3));
1209
+ }
1210
+ getPublisher() {
1211
+ return this.publisher;
1212
+ }
1213
+ }