@aztec/sequencer-client 0.0.1-commit.b655e406 → 0.0.1-commit.b6e433891

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