@aztec/archiver 0.0.1-commit.9593d84 → 0.0.1-commit.96bb3f7

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 (128) hide show
  1. package/README.md +14 -14
  2. package/dest/archiver/archiver.d.ts +105 -85
  3. package/dest/archiver/archiver.d.ts.map +1 -1
  4. package/dest/archiver/archiver.js +949 -255
  5. package/dest/archiver/archiver_store.d.ts +109 -49
  6. package/dest/archiver/archiver_store.d.ts.map +1 -1
  7. package/dest/archiver/archiver_store_test_suite.d.ts +1 -1
  8. package/dest/archiver/archiver_store_test_suite.d.ts.map +1 -1
  9. package/dest/archiver/archiver_store_test_suite.js +1871 -390
  10. package/dest/archiver/config.d.ts +5 -4
  11. package/dest/archiver/config.d.ts.map +1 -1
  12. package/dest/archiver/config.js +12 -5
  13. package/dest/archiver/errors.d.ts +25 -1
  14. package/dest/archiver/errors.d.ts.map +1 -1
  15. package/dest/archiver/errors.js +37 -0
  16. package/dest/archiver/index.d.ts +2 -2
  17. package/dest/archiver/index.d.ts.map +1 -1
  18. package/dest/archiver/instrumentation.d.ts +3 -1
  19. package/dest/archiver/instrumentation.d.ts.map +1 -1
  20. package/dest/archiver/instrumentation.js +22 -59
  21. package/dest/archiver/kv_archiver_store/block_store.d.ts +60 -20
  22. package/dest/archiver/kv_archiver_store/block_store.d.ts.map +1 -1
  23. package/dest/archiver/kv_archiver_store/block_store.js +345 -89
  24. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts +2 -2
  25. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts.map +1 -1
  26. package/dest/archiver/kv_archiver_store/contract_class_store.js +1 -1
  27. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts +2 -2
  28. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +1 -1
  29. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +49 -58
  30. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +1 -1
  31. package/dest/archiver/kv_archiver_store/kv_archiver_store.js +68 -48
  32. package/dest/archiver/kv_archiver_store/log_store.d.ts +12 -16
  33. package/dest/archiver/kv_archiver_store/log_store.d.ts.map +1 -1
  34. package/dest/archiver/kv_archiver_store/log_store.js +149 -84
  35. package/dest/archiver/kv_archiver_store/message_store.d.ts +6 -5
  36. package/dest/archiver/kv_archiver_store/message_store.d.ts.map +1 -1
  37. package/dest/archiver/kv_archiver_store/message_store.js +15 -14
  38. package/dest/archiver/l1/bin/retrieve-calldata.d.ts +3 -0
  39. package/dest/archiver/l1/bin/retrieve-calldata.d.ts.map +1 -0
  40. package/dest/archiver/l1/bin/retrieve-calldata.js +149 -0
  41. package/dest/archiver/l1/calldata_retriever.d.ts +112 -0
  42. package/dest/archiver/l1/calldata_retriever.d.ts.map +1 -0
  43. package/dest/archiver/l1/calldata_retriever.js +471 -0
  44. package/dest/archiver/l1/data_retrieval.d.ts +90 -0
  45. package/dest/archiver/l1/data_retrieval.d.ts.map +1 -0
  46. package/dest/archiver/{data_retrieval.js → l1/data_retrieval.js} +52 -107
  47. package/dest/archiver/l1/debug_tx.d.ts +19 -0
  48. package/dest/archiver/l1/debug_tx.d.ts.map +1 -0
  49. package/dest/archiver/l1/debug_tx.js +73 -0
  50. package/dest/archiver/l1/spire_proposer.d.ts +70 -0
  51. package/dest/archiver/l1/spire_proposer.d.ts.map +1 -0
  52. package/dest/archiver/l1/spire_proposer.js +157 -0
  53. package/dest/archiver/l1/trace_tx.d.ts +97 -0
  54. package/dest/archiver/l1/trace_tx.d.ts.map +1 -0
  55. package/dest/archiver/l1/trace_tx.js +91 -0
  56. package/dest/archiver/l1/types.d.ts +12 -0
  57. package/dest/archiver/l1/types.d.ts.map +1 -0
  58. package/dest/archiver/l1/types.js +3 -0
  59. package/dest/archiver/l1/validate_trace.d.ts +29 -0
  60. package/dest/archiver/l1/validate_trace.d.ts.map +1 -0
  61. package/dest/archiver/l1/validate_trace.js +150 -0
  62. package/dest/archiver/structs/inbox_message.d.ts +4 -4
  63. package/dest/archiver/structs/inbox_message.d.ts.map +1 -1
  64. package/dest/archiver/structs/inbox_message.js +6 -5
  65. package/dest/archiver/structs/published.d.ts +1 -2
  66. package/dest/archiver/structs/published.d.ts.map +1 -1
  67. package/dest/archiver/validation.d.ts +4 -4
  68. package/dest/archiver/validation.d.ts.map +1 -1
  69. package/dest/archiver/validation.js +1 -1
  70. package/dest/factory.d.ts +2 -2
  71. package/dest/factory.d.ts.map +1 -1
  72. package/dest/factory.js +4 -3
  73. package/dest/index.d.ts +2 -2
  74. package/dest/index.d.ts.map +1 -1
  75. package/dest/index.js +1 -1
  76. package/dest/test/mock_archiver.d.ts +16 -8
  77. package/dest/test/mock_archiver.d.ts.map +1 -1
  78. package/dest/test/mock_archiver.js +19 -14
  79. package/dest/test/mock_l1_to_l2_message_source.d.ts +7 -6
  80. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
  81. package/dest/test/mock_l1_to_l2_message_source.js +21 -11
  82. package/dest/test/mock_l2_block_source.d.ts +23 -8
  83. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  84. package/dest/test/mock_l2_block_source.js +95 -35
  85. package/dest/test/mock_structs.d.ts +3 -2
  86. package/dest/test/mock_structs.d.ts.map +1 -1
  87. package/dest/test/mock_structs.js +9 -8
  88. package/package.json +17 -16
  89. package/src/archiver/archiver.ts +751 -344
  90. package/src/archiver/archiver_store.ts +123 -48
  91. package/src/archiver/archiver_store_test_suite.ts +1943 -365
  92. package/src/archiver/config.ts +17 -12
  93. package/src/archiver/errors.ts +64 -0
  94. package/src/archiver/index.ts +1 -1
  95. package/src/archiver/instrumentation.ts +24 -59
  96. package/src/archiver/kv_archiver_store/block_store.ts +475 -106
  97. package/src/archiver/kv_archiver_store/contract_class_store.ts +1 -1
  98. package/src/archiver/kv_archiver_store/contract_instance_store.ts +1 -1
  99. package/src/archiver/kv_archiver_store/kv_archiver_store.ts +92 -72
  100. package/src/archiver/kv_archiver_store/log_store.ts +209 -99
  101. package/src/archiver/kv_archiver_store/message_store.ts +21 -18
  102. package/src/archiver/l1/README.md +98 -0
  103. package/src/archiver/l1/bin/retrieve-calldata.ts +182 -0
  104. package/src/archiver/l1/calldata_retriever.ts +641 -0
  105. package/src/archiver/{data_retrieval.ts → l1/data_retrieval.ts} +101 -165
  106. package/src/archiver/l1/debug_tx.ts +99 -0
  107. package/src/archiver/l1/spire_proposer.ts +160 -0
  108. package/src/archiver/l1/trace_tx.ts +128 -0
  109. package/src/archiver/l1/types.ts +13 -0
  110. package/src/archiver/l1/validate_trace.ts +211 -0
  111. package/src/archiver/structs/inbox_message.ts +8 -8
  112. package/src/archiver/structs/published.ts +0 -1
  113. package/src/archiver/validation.ts +6 -6
  114. package/src/factory.ts +4 -3
  115. package/src/index.ts +1 -1
  116. package/src/test/fixtures/debug_traceTransaction-multicall3.json +88 -0
  117. package/src/test/fixtures/debug_traceTransaction-multiplePropose.json +153 -0
  118. package/src/test/fixtures/debug_traceTransaction-proxied.json +122 -0
  119. package/src/test/fixtures/trace_transaction-multicall3.json +65 -0
  120. package/src/test/fixtures/trace_transaction-multiplePropose.json +319 -0
  121. package/src/test/fixtures/trace_transaction-proxied.json +128 -0
  122. package/src/test/fixtures/trace_transaction-randomRevert.json +216 -0
  123. package/src/test/mock_archiver.ts +22 -16
  124. package/src/test/mock_l1_to_l2_message_source.ts +18 -11
  125. package/src/test/mock_l2_block_source.ts +115 -37
  126. package/src/test/mock_structs.ts +10 -9
  127. package/dest/archiver/data_retrieval.d.ts +0 -79
  128. package/dest/archiver/data_retrieval.d.ts.map +0 -1
@@ -1,56 +1,462 @@
1
- function _ts_decorate(decorators, target, key, desc) {
2
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
- else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
- return c > 3 && r && Object.defineProperty(target, key, r), r;
1
+ function applyDecs2203RFactory() {
2
+ function createAddInitializerMethod(initializers, decoratorFinishedRef) {
3
+ return function addInitializer(initializer) {
4
+ assertNotFinished(decoratorFinishedRef, "addInitializer");
5
+ assertCallable(initializer, "An initializer");
6
+ initializers.push(initializer);
7
+ };
8
+ }
9
+ function memberDec(dec, name, desc, initializers, kind, isStatic, isPrivate, metadata, value) {
10
+ var kindStr;
11
+ switch(kind){
12
+ case 1:
13
+ kindStr = "accessor";
14
+ break;
15
+ case 2:
16
+ kindStr = "method";
17
+ break;
18
+ case 3:
19
+ kindStr = "getter";
20
+ break;
21
+ case 4:
22
+ kindStr = "setter";
23
+ break;
24
+ default:
25
+ kindStr = "field";
26
+ }
27
+ var ctx = {
28
+ kind: kindStr,
29
+ name: isPrivate ? "#" + name : name,
30
+ static: isStatic,
31
+ private: isPrivate,
32
+ metadata: metadata
33
+ };
34
+ var decoratorFinishedRef = {
35
+ v: false
36
+ };
37
+ ctx.addInitializer = createAddInitializerMethod(initializers, decoratorFinishedRef);
38
+ var get, set;
39
+ if (kind === 0) {
40
+ if (isPrivate) {
41
+ get = desc.get;
42
+ set = desc.set;
43
+ } else {
44
+ get = function() {
45
+ return this[name];
46
+ };
47
+ set = function(v) {
48
+ this[name] = v;
49
+ };
50
+ }
51
+ } else if (kind === 2) {
52
+ get = function() {
53
+ return desc.value;
54
+ };
55
+ } else {
56
+ if (kind === 1 || kind === 3) {
57
+ get = function() {
58
+ return desc.get.call(this);
59
+ };
60
+ }
61
+ if (kind === 1 || kind === 4) {
62
+ set = function(v) {
63
+ desc.set.call(this, v);
64
+ };
65
+ }
66
+ }
67
+ ctx.access = get && set ? {
68
+ get: get,
69
+ set: set
70
+ } : get ? {
71
+ get: get
72
+ } : {
73
+ set: set
74
+ };
75
+ try {
76
+ return dec(value, ctx);
77
+ } finally{
78
+ decoratorFinishedRef.v = true;
79
+ }
80
+ }
81
+ function assertNotFinished(decoratorFinishedRef, fnName) {
82
+ if (decoratorFinishedRef.v) {
83
+ throw new Error("attempted to call " + fnName + " after decoration was finished");
84
+ }
85
+ }
86
+ function assertCallable(fn, hint) {
87
+ if (typeof fn !== "function") {
88
+ throw new TypeError(hint + " must be a function");
89
+ }
90
+ }
91
+ function assertValidReturnValue(kind, value) {
92
+ var type = typeof value;
93
+ if (kind === 1) {
94
+ if (type !== "object" || value === null) {
95
+ throw new TypeError("accessor decorators must return an object with get, set, or init properties or void 0");
96
+ }
97
+ if (value.get !== undefined) {
98
+ assertCallable(value.get, "accessor.get");
99
+ }
100
+ if (value.set !== undefined) {
101
+ assertCallable(value.set, "accessor.set");
102
+ }
103
+ if (value.init !== undefined) {
104
+ assertCallable(value.init, "accessor.init");
105
+ }
106
+ } else if (type !== "function") {
107
+ var hint;
108
+ if (kind === 0) {
109
+ hint = "field";
110
+ } else if (kind === 10) {
111
+ hint = "class";
112
+ } else {
113
+ hint = "method";
114
+ }
115
+ throw new TypeError(hint + " decorators must return a function or void 0");
116
+ }
117
+ }
118
+ function applyMemberDec(ret, base, decInfo, name, kind, isStatic, isPrivate, initializers, metadata) {
119
+ var decs = decInfo[0];
120
+ var desc, init, value;
121
+ if (isPrivate) {
122
+ if (kind === 0 || kind === 1) {
123
+ desc = {
124
+ get: decInfo[3],
125
+ set: decInfo[4]
126
+ };
127
+ } else if (kind === 3) {
128
+ desc = {
129
+ get: decInfo[3]
130
+ };
131
+ } else if (kind === 4) {
132
+ desc = {
133
+ set: decInfo[3]
134
+ };
135
+ } else {
136
+ desc = {
137
+ value: decInfo[3]
138
+ };
139
+ }
140
+ } else if (kind !== 0) {
141
+ desc = Object.getOwnPropertyDescriptor(base, name);
142
+ }
143
+ if (kind === 1) {
144
+ value = {
145
+ get: desc.get,
146
+ set: desc.set
147
+ };
148
+ } else if (kind === 2) {
149
+ value = desc.value;
150
+ } else if (kind === 3) {
151
+ value = desc.get;
152
+ } else if (kind === 4) {
153
+ value = desc.set;
154
+ }
155
+ var newValue, get, set;
156
+ if (typeof decs === "function") {
157
+ newValue = memberDec(decs, name, desc, initializers, kind, isStatic, isPrivate, metadata, value);
158
+ if (newValue !== void 0) {
159
+ assertValidReturnValue(kind, newValue);
160
+ if (kind === 0) {
161
+ init = newValue;
162
+ } else if (kind === 1) {
163
+ init = newValue.init;
164
+ get = newValue.get || value.get;
165
+ set = newValue.set || value.set;
166
+ value = {
167
+ get: get,
168
+ set: set
169
+ };
170
+ } else {
171
+ value = newValue;
172
+ }
173
+ }
174
+ } else {
175
+ for(var i = decs.length - 1; i >= 0; i--){
176
+ var dec = decs[i];
177
+ newValue = memberDec(dec, name, desc, initializers, kind, isStatic, isPrivate, metadata, value);
178
+ if (newValue !== void 0) {
179
+ assertValidReturnValue(kind, newValue);
180
+ var newInit;
181
+ if (kind === 0) {
182
+ newInit = newValue;
183
+ } else if (kind === 1) {
184
+ newInit = newValue.init;
185
+ get = newValue.get || value.get;
186
+ set = newValue.set || value.set;
187
+ value = {
188
+ get: get,
189
+ set: set
190
+ };
191
+ } else {
192
+ value = newValue;
193
+ }
194
+ if (newInit !== void 0) {
195
+ if (init === void 0) {
196
+ init = newInit;
197
+ } else if (typeof init === "function") {
198
+ init = [
199
+ init,
200
+ newInit
201
+ ];
202
+ } else {
203
+ init.push(newInit);
204
+ }
205
+ }
206
+ }
207
+ }
208
+ }
209
+ if (kind === 0 || kind === 1) {
210
+ if (init === void 0) {
211
+ init = function(instance, init) {
212
+ return init;
213
+ };
214
+ } else if (typeof init !== "function") {
215
+ var ownInitializers = init;
216
+ init = function(instance, init) {
217
+ var value = init;
218
+ for(var i = 0; i < ownInitializers.length; i++){
219
+ value = ownInitializers[i].call(instance, value);
220
+ }
221
+ return value;
222
+ };
223
+ } else {
224
+ var originalInitializer = init;
225
+ init = function(instance, init) {
226
+ return originalInitializer.call(instance, init);
227
+ };
228
+ }
229
+ ret.push(init);
230
+ }
231
+ if (kind !== 0) {
232
+ if (kind === 1) {
233
+ desc.get = value.get;
234
+ desc.set = value.set;
235
+ } else if (kind === 2) {
236
+ desc.value = value;
237
+ } else if (kind === 3) {
238
+ desc.get = value;
239
+ } else if (kind === 4) {
240
+ desc.set = value;
241
+ }
242
+ if (isPrivate) {
243
+ if (kind === 1) {
244
+ ret.push(function(instance, args) {
245
+ return value.get.call(instance, args);
246
+ });
247
+ ret.push(function(instance, args) {
248
+ return value.set.call(instance, args);
249
+ });
250
+ } else if (kind === 2) {
251
+ ret.push(value);
252
+ } else {
253
+ ret.push(function(instance, args) {
254
+ return value.call(instance, args);
255
+ });
256
+ }
257
+ } else {
258
+ Object.defineProperty(base, name, desc);
259
+ }
260
+ }
261
+ }
262
+ function applyMemberDecs(Class, decInfos, metadata) {
263
+ var ret = [];
264
+ var protoInitializers;
265
+ var staticInitializers;
266
+ var existingProtoNonFields = new Map();
267
+ var existingStaticNonFields = new Map();
268
+ for(var i = 0; i < decInfos.length; i++){
269
+ var decInfo = decInfos[i];
270
+ if (!Array.isArray(decInfo)) continue;
271
+ var kind = decInfo[1];
272
+ var name = decInfo[2];
273
+ var isPrivate = decInfo.length > 3;
274
+ var isStatic = kind >= 5;
275
+ var base;
276
+ var initializers;
277
+ if (isStatic) {
278
+ base = Class;
279
+ kind = kind - 5;
280
+ staticInitializers = staticInitializers || [];
281
+ initializers = staticInitializers;
282
+ } else {
283
+ base = Class.prototype;
284
+ protoInitializers = protoInitializers || [];
285
+ initializers = protoInitializers;
286
+ }
287
+ if (kind !== 0 && !isPrivate) {
288
+ var existingNonFields = isStatic ? existingStaticNonFields : existingProtoNonFields;
289
+ var existingKind = existingNonFields.get(name) || 0;
290
+ if (existingKind === true || existingKind === 3 && kind !== 4 || existingKind === 4 && kind !== 3) {
291
+ throw new Error("Attempted to decorate a public method/accessor that has the same name as a previously decorated public method/accessor. This is not currently supported by the decorators plugin. Property name was: " + name);
292
+ } else if (!existingKind && kind > 2) {
293
+ existingNonFields.set(name, kind);
294
+ } else {
295
+ existingNonFields.set(name, true);
296
+ }
297
+ }
298
+ applyMemberDec(ret, base, decInfo, name, kind, isStatic, isPrivate, initializers, metadata);
299
+ }
300
+ pushInitializers(ret, protoInitializers);
301
+ pushInitializers(ret, staticInitializers);
302
+ return ret;
303
+ }
304
+ function pushInitializers(ret, initializers) {
305
+ if (initializers) {
306
+ ret.push(function(instance) {
307
+ for(var i = 0; i < initializers.length; i++){
308
+ initializers[i].call(instance);
309
+ }
310
+ return instance;
311
+ });
312
+ }
313
+ }
314
+ function applyClassDecs(targetClass, classDecs, metadata) {
315
+ if (classDecs.length > 0) {
316
+ var initializers = [];
317
+ var newClass = targetClass;
318
+ var name = targetClass.name;
319
+ for(var i = classDecs.length - 1; i >= 0; i--){
320
+ var decoratorFinishedRef = {
321
+ v: false
322
+ };
323
+ try {
324
+ var nextNewClass = classDecs[i](newClass, {
325
+ kind: "class",
326
+ name: name,
327
+ addInitializer: createAddInitializerMethod(initializers, decoratorFinishedRef),
328
+ metadata
329
+ });
330
+ } finally{
331
+ decoratorFinishedRef.v = true;
332
+ }
333
+ if (nextNewClass !== undefined) {
334
+ assertValidReturnValue(10, nextNewClass);
335
+ newClass = nextNewClass;
336
+ }
337
+ }
338
+ return [
339
+ defineMetadata(newClass, metadata),
340
+ function() {
341
+ for(var i = 0; i < initializers.length; i++){
342
+ initializers[i].call(newClass);
343
+ }
344
+ }
345
+ ];
346
+ }
347
+ }
348
+ function defineMetadata(Class, metadata) {
349
+ return Object.defineProperty(Class, Symbol.metadata || Symbol.for("Symbol.metadata"), {
350
+ configurable: true,
351
+ enumerable: true,
352
+ value: metadata
353
+ });
354
+ }
355
+ return function applyDecs2203R(targetClass, memberDecs, classDecs, parentClass) {
356
+ if (parentClass !== void 0) {
357
+ var parentMetadata = parentClass[Symbol.metadata || Symbol.for("Symbol.metadata")];
358
+ }
359
+ var metadata = Object.create(parentMetadata === void 0 ? null : parentMetadata);
360
+ var e = applyMemberDecs(targetClass, memberDecs, metadata);
361
+ if (!classDecs.length) defineMetadata(targetClass, metadata);
362
+ return {
363
+ e: e,
364
+ get c () {
365
+ return applyClassDecs(targetClass, classDecs, metadata);
366
+ }
367
+ };
368
+ };
369
+ }
370
+ function _apply_decs_2203_r(targetClass, memberDecs, classDecs, parentClass) {
371
+ return (_apply_decs_2203_r = applyDecs2203RFactory())(targetClass, memberDecs, classDecs, parentClass);
6
372
  }
373
+ var _dec, _dec1, _dec2, _dec3, _dec4, _initProto;
374
+ import { GENESIS_BLOCK_HEADER_HASH, INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
7
375
  import { EpochCache } from '@aztec/epoch-cache';
8
- import { BlockTagTooOldError, InboxContract, RollupContract, createEthereumChain } from '@aztec/ethereum';
376
+ import { createEthereumChain } from '@aztec/ethereum/chain';
377
+ import { BlockTagTooOldError, InboxContract, RollupContract } from '@aztec/ethereum/contracts';
9
378
  import { maxBigint } from '@aztec/foundation/bigint';
379
+ import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
10
380
  import { Buffer16, Buffer32 } from '@aztec/foundation/buffer';
11
381
  import { merge, pick } from '@aztec/foundation/collection';
12
- import { Fr } from '@aztec/foundation/fields';
382
+ import { Fr } from '@aztec/foundation/curves/bn254';
13
383
  import { createLogger } from '@aztec/foundation/log';
14
384
  import { promiseWithResolvers } from '@aztec/foundation/promise';
15
385
  import { RunningPromise, makeLoggingErrorHandler } from '@aztec/foundation/running-promise';
16
386
  import { count } from '@aztec/foundation/string';
17
387
  import { DateProvider, Timer, elapsed } from '@aztec/foundation/timer';
388
+ import { isDefined } from '@aztec/foundation/types';
18
389
  import { ContractClassPublishedEvent, PrivateFunctionBroadcastedEvent, UtilityFunctionBroadcastedEvent } from '@aztec/protocol-contracts/class-registry';
19
390
  import { ContractInstancePublishedEvent, ContractInstanceUpdatedEvent } from '@aztec/protocol-contracts/instance-registry';
20
- import { L2Block, L2BlockSourceEvents, PublishedL2Block } from '@aztec/stdlib/block';
391
+ import { CommitteeAttestation, GENESIS_CHECKPOINT_HEADER_HASH, L2Block, L2BlockSourceEvents, PublishedL2Block } from '@aztec/stdlib/block';
392
+ import { Checkpoint, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
21
393
  import { computePublicBytecodeCommitment, isValidPrivateFunctionMembershipProof, isValidUtilityFunctionMembershipProof } from '@aztec/stdlib/contract';
22
394
  import { getEpochAtSlot, getEpochNumberAtTimestamp, getSlotAtTimestamp, getSlotRangeForEpoch, getTimestampRangeForEpoch } from '@aztec/stdlib/epoch-helpers';
23
- import { getTelemetryClient, trackSpan } from '@aztec/telemetry-client';
395
+ import { computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
396
+ import { execInSpan, getTelemetryClient, trackSpan } from '@aztec/telemetry-client';
24
397
  import { EventEmitter } from 'events';
25
398
  import groupBy from 'lodash.groupby';
26
399
  import { createPublicClient, fallback, http } from 'viem';
27
- import { retrieveCheckpointsFromRollup, retrieveL1ToL2Message, retrieveL1ToL2Messages, retrievedToPublishedCheckpoint } from './data_retrieval.js';
28
- import { InitialBlockNumberNotSequentialError, NoBlobBodiesFoundError } from './errors.js';
400
+ import { InitialCheckpointNumberNotSequentialError, NoBlobBodiesFoundError } from './errors.js';
29
401
  import { ArchiverInstrumentation } from './instrumentation.js';
402
+ import { retrieveCheckpointsFromRollup, retrieveL1ToL2Message, retrieveL1ToL2Messages, retrievedToPublishedCheckpoint } from './l1/data_retrieval.js';
403
+ import { validateAndLogTraceAvailability } from './l1/validate_trace.js';
30
404
  import { validateCheckpointAttestations } from './validation.js';
31
405
  function mapArchiverConfig(config) {
32
406
  return {
33
407
  pollingIntervalMs: config.archiverPollingIntervalMS,
34
408
  batchSize: config.archiverBatchSize,
35
- skipValidateBlockAttestations: config.skipValidateBlockAttestations,
36
- maxAllowedEthClientDriftSeconds: config.maxAllowedEthClientDriftSeconds
409
+ skipValidateCheckpointAttestations: config.skipValidateCheckpointAttestations,
410
+ maxAllowedEthClientDriftSeconds: config.maxAllowedEthClientDriftSeconds,
411
+ ethereumAllowNoDebugHosts: config.ethereumAllowNoDebugHosts
37
412
  };
38
413
  }
414
+ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.sync'), _dec2 = trackSpan('Archiver.handleEpochPrune'), _dec3 = trackSpan('Archiver.handleL1ToL2Messages'), _dec4 = trackSpan('Archiver.handleCheckpoints');
39
415
  /**
40
416
  * Pulls checkpoints in a non-blocking manner and provides interface for their retrieval.
41
417
  * Responsible for handling robust L1 polling so that other components do not need to
42
418
  * concern themselves with it.
43
419
  */ export class Archiver extends EventEmitter {
44
420
  publicClient;
421
+ debugClient;
45
422
  l1Addresses;
46
423
  dataStore;
47
424
  config;
48
- blobSinkClient;
425
+ blobClient;
49
426
  epochCache;
50
427
  dateProvider;
51
428
  instrumentation;
52
429
  l1constants;
53
430
  log;
431
+ static{
432
+ ({ e: [_initProto] } = _apply_decs_2203_r(this, [
433
+ [
434
+ _dec,
435
+ 2,
436
+ "syncFromL1"
437
+ ],
438
+ [
439
+ _dec1,
440
+ 2,
441
+ "sync"
442
+ ],
443
+ [
444
+ _dec2,
445
+ 2,
446
+ "handleEpochPrune"
447
+ ],
448
+ [
449
+ _dec3,
450
+ 2,
451
+ "handleL1ToL2Messages"
452
+ ],
453
+ [
454
+ _dec4,
455
+ 2,
456
+ "handleCheckpoints"
457
+ ]
458
+ ], []));
459
+ }
54
460
  /** A loop in which we will be continually fetching new checkpoints. */ runningPromise;
55
461
  rollup;
56
462
  inbox;
@@ -59,18 +465,20 @@ function mapArchiverConfig(config) {
59
465
  l1Timestamp;
60
466
  initialSyncComplete;
61
467
  initialSyncPromise;
468
+ /** Queue of blocks to be added to the store, processed by the sync loop. */ blockQueue;
62
469
  tracer;
63
470
  /**
64
471
  * Creates a new instance of the Archiver.
65
472
  * @param publicClient - A client for interacting with the Ethereum node.
473
+ * @param debugClient - A client for interacting with the Ethereum node for debug/trace methods.
66
474
  * @param rollupAddress - Ethereum address of the rollup contract.
67
475
  * @param inboxAddress - Ethereum address of the inbox contract.
68
476
  * @param registryAddress - Ethereum address of the registry contract.
69
477
  * @param pollingIntervalMs - The interval for polling for L1 logs (in milliseconds).
70
478
  * @param store - An archiver data store for storage & retrieval of blocks, encrypted logs & contract data.
71
479
  * @param log - A logger.
72
- */ constructor(publicClient, l1Addresses, dataStore, config, blobSinkClient, epochCache, dateProvider, instrumentation, l1constants, log = createLogger('archiver')){
73
- super(), this.publicClient = publicClient, this.l1Addresses = l1Addresses, this.dataStore = dataStore, this.config = config, this.blobSinkClient = blobSinkClient, this.epochCache = epochCache, this.dateProvider = dateProvider, this.instrumentation = instrumentation, this.l1constants = l1constants, this.log = log, this.initialSyncComplete = false;
480
+ */ constructor(publicClient, debugClient, l1Addresses, dataStore, config, blobClient, epochCache, dateProvider, instrumentation, l1constants, log = createLogger('archiver')){
481
+ super(), this.publicClient = publicClient, this.debugClient = debugClient, this.l1Addresses = l1Addresses, this.dataStore = dataStore, this.config = config, this.blobClient = blobClient, this.epochCache = epochCache, this.dateProvider = dateProvider, this.instrumentation = instrumentation, this.l1constants = l1constants, this.log = log, this.initialSyncComplete = (_initProto(this), false), this.blockQueue = [];
74
482
  this.tracer = instrumentation.tracer;
75
483
  this.store = new ArchiverStoreHelper(dataStore);
76
484
  this.rollup = new RollupContract(publicClient, l1Addresses.rollupAddress);
@@ -90,15 +498,27 @@ function mapArchiverConfig(config) {
90
498
  const chain = createEthereumChain(config.l1RpcUrls, config.l1ChainId);
91
499
  const publicClient = createPublicClient({
92
500
  chain: chain.chainInfo,
93
- transport: fallback(config.l1RpcUrls.map((url)=>http(url))),
501
+ transport: fallback(config.l1RpcUrls.map((url)=>http(url, {
502
+ batch: false
503
+ }))),
504
+ pollingInterval: config.viemPollingIntervalMS
505
+ });
506
+ // Create debug client using debug RPC URLs if available, otherwise fall back to regular RPC URLs
507
+ const debugRpcUrls = config.l1DebugRpcUrls.length > 0 ? config.l1DebugRpcUrls : config.l1RpcUrls;
508
+ const debugClient = createPublicClient({
509
+ chain: chain.chainInfo,
510
+ transport: fallback(debugRpcUrls.map((url)=>http(url, {
511
+ batch: false
512
+ }))),
94
513
  pollingInterval: config.viemPollingIntervalMS
95
514
  });
96
515
  const rollup = new RollupContract(publicClient, config.l1Contracts.rollupAddress);
97
- const [l1StartBlock, l1GenesisTime, proofSubmissionEpochs, genesisArchiveRoot] = await Promise.all([
516
+ const [l1StartBlock, l1GenesisTime, proofSubmissionEpochs, genesisArchiveRoot, slashingProposerAddress] = await Promise.all([
98
517
  rollup.getL1StartBlock(),
99
518
  rollup.getL1GenesisTime(),
100
519
  rollup.getProofSubmissionEpochs(),
101
- rollup.getGenesisArchiveTreeRoot()
520
+ rollup.getGenesisArchiveTreeRoot(),
521
+ rollup.getSlashingProposerAddress()
102
522
  ]);
103
523
  const l1StartBlockHash = await publicClient.getBlock({
104
524
  blockNumber: l1StartBlock,
@@ -113,16 +533,20 @@ function mapArchiverConfig(config) {
113
533
  slotDuration,
114
534
  ethereumSlotDuration,
115
535
  proofSubmissionEpochs: Number(proofSubmissionEpochs),
116
- genesisArchiveRoot: Fr.fromHexString(genesisArchiveRoot)
536
+ genesisArchiveRoot: Fr.fromString(genesisArchiveRoot.toString())
117
537
  };
118
538
  const opts = merge({
119
539
  pollingIntervalMs: 10_000,
120
540
  batchSize: 100,
121
- maxAllowedEthClientDriftSeconds: 300
541
+ maxAllowedEthClientDriftSeconds: 300,
542
+ ethereumAllowNoDebugHosts: false
122
543
  }, mapArchiverConfig(config));
123
544
  const epochCache = deps.epochCache ?? await EpochCache.create(config.l1Contracts.rollupAddress, config, deps);
124
545
  const telemetry = deps.telemetry ?? getTelemetryClient();
125
- const archiver = new Archiver(publicClient, config.l1Contracts, archiverStore, opts, deps.blobSinkClient, epochCache, deps.dateProvider ?? new DateProvider(), await ArchiverInstrumentation.new(telemetry, ()=>archiverStore.estimateSize()), l1Constants);
546
+ const archiver = new Archiver(publicClient, debugClient, {
547
+ ...config.l1Contracts,
548
+ slashingProposerAddress
549
+ }, archiverStore, opts, deps.blobClient, epochCache, deps.dateProvider ?? new DateProvider(), await ArchiverInstrumentation.new(telemetry, ()=>archiverStore.estimateSize()), l1Constants);
126
550
  await archiver.start(blockUntilSynced);
127
551
  return archiver;
128
552
  }
@@ -136,16 +560,17 @@ function mapArchiverConfig(config) {
136
560
  if (this.runningPromise.isRunning()) {
137
561
  throw new Error('Archiver is already running');
138
562
  }
139
- await this.blobSinkClient.testSources();
563
+ await this.blobClient.testSources();
140
564
  await this.testEthereumNodeSynced();
565
+ await validateAndLogTraceAvailability(this.debugClient, this.config.ethereumAllowNoDebugHosts ?? false);
141
566
  // Log initial state for the archiver
142
567
  const { l1StartBlock } = this.l1constants;
143
568
  const { blocksSynchedTo = l1StartBlock, messagesSynchedTo = l1StartBlock } = await this.store.getSynchPoint();
144
- const currentL2Block = await this.getBlockNumber();
145
- this.log.info(`Starting archiver sync to rollup contract ${this.l1Addresses.rollupAddress.toString()} from L1 block ${blocksSynchedTo} and L2 block ${currentL2Block}`, {
569
+ const currentL2Checkpoint = await this.getSynchedCheckpointNumber();
570
+ this.log.info(`Starting archiver sync to rollup contract ${this.l1Addresses.rollupAddress.toString()} from L1 block ${blocksSynchedTo} and L2 checkpoint ${currentL2Checkpoint}`, {
146
571
  blocksSynchedTo,
147
572
  messagesSynchedTo,
148
- currentL2Block
573
+ currentL2Checkpoint
149
574
  });
150
575
  // Start sync loop, and return the wait for initial sync if we are asked to block until synced
151
576
  this.runningPromise.start();
@@ -156,6 +581,51 @@ function mapArchiverConfig(config) {
156
581
  syncImmediate() {
157
582
  return this.runningPromise.trigger();
158
583
  }
584
+ /**
585
+ * Queues a block to be added to the archiver store and triggers processing.
586
+ * The block will be processed by the sync loop.
587
+ * Implements the L2BlockSink interface.
588
+ * @param block - The L2 block to add.
589
+ * @returns A promise that resolves when the block has been added to the store, or rejects on error.
590
+ */ addBlock(block) {
591
+ return new Promise((resolve, reject)=>{
592
+ this.blockQueue.push({
593
+ block,
594
+ resolve,
595
+ reject
596
+ });
597
+ this.log.debug(`Queued block ${block.number} for processing`);
598
+ // Trigger an immediate sync, but don't wait for it - the promise resolves when the block is processed
599
+ this.syncImmediate().catch((err)=>{
600
+ this.log.error(`Sync immediate call failed: ${err}`);
601
+ });
602
+ });
603
+ }
604
+ /**
605
+ * Processes all queued blocks, adding them to the store.
606
+ * Called at the beginning of each sync iteration.
607
+ * Blocks are processed in the order they were queued.
608
+ */ async processQueuedBlocks() {
609
+ if (this.blockQueue.length === 0) {
610
+ return;
611
+ }
612
+ // Take all blocks from the queue
613
+ const queuedItems = this.blockQueue.splice(0, this.blockQueue.length);
614
+ this.log.debug(`Processing ${queuedItems.length} queued block(s)`);
615
+ // Process each block individually to properly resolve/reject each promise
616
+ for (const { block, resolve, reject } of queuedItems){
617
+ try {
618
+ await this.store.addBlocks([
619
+ block
620
+ ]);
621
+ this.log.debug(`Added block ${block.number} to store`);
622
+ resolve();
623
+ } catch (err) {
624
+ this.log.error(`Failed to add block ${block.number} to store: ${err.message}`);
625
+ reject(err);
626
+ }
627
+ }
628
+ }
159
629
  waitForInitialSync() {
160
630
  return this.initialSyncPromise.promise;
161
631
  }
@@ -172,9 +642,7 @@ function mapArchiverConfig(config) {
172
642
  throw new Error(`Ethereum node is out of sync (last block synced ${number} at ${l1Timestamp} vs current time ${currentTime})`);
173
643
  }
174
644
  }
175
- /**
176
- * Fetches logs from L1 contracts and processes them.
177
- */ async sync() {
645
+ async syncFromL1() {
178
646
  /**
179
647
  * We keep track of three "pointers" to L1 blocks:
180
648
  * 1. the last L1 block that published an L2 block
@@ -248,7 +716,7 @@ function mapArchiverConfig(config) {
248
716
  // past it, since otherwise we'll keep downloading it and reprocessing it on every iteration until
249
717
  // we get a valid checkpoint to advance the syncpoint.
250
718
  if (!rollupStatus.validationResult?.valid && rollupStatus.lastL1BlockWithCheckpoint !== undefined) {
251
- await this.store.setBlockSynchedL1BlockNumber(rollupStatus.lastL1BlockWithCheckpoint);
719
+ await this.store.setCheckpointSynchedL1BlockNumber(rollupStatus.lastL1BlockWithCheckpoint);
252
720
  }
253
721
  // And lastly we check if we are missing any checkpoints behind us due to a possible L1 reorg.
254
722
  // We only do this if rollup cant prune on the next submission. Otherwise we will end up
@@ -277,6 +745,14 @@ function mapArchiverConfig(config) {
277
745
  this.initialSyncPromise.resolve();
278
746
  }
279
747
  }
748
+ /**
749
+ * Fetches logs from L1 contracts and processes them.
750
+ */ async sync() {
751
+ // Process any queued blocks first, before doing L1 sync
752
+ await this.processQueuedBlocks();
753
+ // Now perform L1 sync
754
+ await this.syncFromL1();
755
+ }
280
756
  /** Queries the rollup contract on whether a prune can be executed on the immediate next L1 block. */ async canPrune(currentL1BlockNumber, currentL1Timestamp) {
281
757
  const time = (currentL1Timestamp ?? 0n) + BigInt(this.l1constants.ethereumSlotDuration);
282
758
  const result = await this.rollup.canPruneAtTime(time, {
@@ -297,20 +773,25 @@ function mapArchiverConfig(config) {
297
773
  const canPrune = localPendingCheckpointNumber > provenCheckpointNumber && rollupCanPrune;
298
774
  if (canPrune) {
299
775
  const timer = new Timer();
300
- const pruneFrom = provenCheckpointNumber + 1;
301
- const header = await this.getCheckpointHeader(Number(pruneFrom));
776
+ const pruneFrom = CheckpointNumber(provenCheckpointNumber + 1);
777
+ const header = await this.getCheckpointHeader(pruneFrom);
302
778
  if (header === undefined) {
303
779
  throw new Error(`Missing checkpoint header ${pruneFrom}`);
304
780
  }
305
781
  const pruneFromSlotNumber = header.slotNumber;
306
782
  const pruneFromEpochNumber = getEpochAtSlot(pruneFromSlotNumber, this.l1constants);
307
783
  const checkpointsToUnwind = localPendingCheckpointNumber - provenCheckpointNumber;
308
- const checkpoints = await this.getCheckpoints(Number(provenCheckpointNumber) + 1, Number(checkpointsToUnwind));
784
+ const checkpointPromises = Array.from({
785
+ length: checkpointsToUnwind
786
+ }).fill(0).map((_, i)=>this.store.getCheckpointData(CheckpointNumber(i + pruneFrom)));
787
+ const checkpoints = await Promise.all(checkpointPromises);
788
+ const blockPromises = await Promise.all(checkpoints.filter(isDefined).map((cp)=>this.store.getBlocksForCheckpoint(CheckpointNumber(cp.checkpointNumber))));
789
+ const newBlocks = blockPromises.filter(isDefined).flat();
309
790
  // Emit an event for listening services to react to the chain prune
310
791
  this.emit(L2BlockSourceEvents.L2PruneDetected, {
311
792
  type: L2BlockSourceEvents.L2PruneDetected,
312
793
  epochNumber: pruneFromEpochNumber,
313
- blocks: checkpoints.flatMap((c)=>L2Block.fromCheckpoint(c))
794
+ blocks: newBlocks
314
795
  });
315
796
  this.log.debug(`L2 prune from ${provenCheckpointNumber + 1} to ${localPendingCheckpointNumber} will occur on next checkpoint submission.`);
316
797
  await this.unwindCheckpoints(localPendingCheckpointNumber, checkpointsToUnwind);
@@ -406,7 +887,7 @@ function mapArchiverConfig(config) {
406
887
  }while (searchEndBlock < currentL1BlockNumber)
407
888
  // Log stats for messages retrieved (if any).
408
889
  if (messageCount > 0) {
409
- this.log.info(`Retrieved ${messageCount} new L1 to L2 messages up to message with index ${lastMessage?.index} for L2 block ${lastMessage?.l2BlockNumber}`, {
890
+ this.log.info(`Retrieved ${messageCount} new L1 to L2 messages up to message with index ${lastMessage?.index} for checkpoint ${lastMessage?.checkpointNumber}`, {
410
891
  lastMessage,
411
892
  messageCount
412
893
  });
@@ -486,16 +967,14 @@ function mapArchiverConfig(config) {
486
967
  async handleCheckpoints(blocksSynchedTo, currentL1BlockNumber) {
487
968
  const localPendingCheckpointNumber = await this.getSynchedCheckpointNumber();
488
969
  const initialValidationResult = await this.store.getPendingChainValidationStatus();
489
- const [rollupProvenCheckpointNumber, provenArchive, rollupPendingCheckpointNumber, pendingArchive, archiveForLocalPendingCheckpointNumber] = await this.rollup.status(BigInt(localPendingCheckpointNumber), {
490
- blockNumber: currentL1BlockNumber
491
- });
492
- const provenCheckpointNumber = Number(rollupProvenCheckpointNumber);
493
- const pendingCheckpointNumber = Number(rollupPendingCheckpointNumber);
970
+ const { provenCheckpointNumber, provenArchive, pendingCheckpointNumber, pendingArchive, archiveOfMyCheckpoint: archiveForLocalPendingCheckpointNumber } = await execInSpan(this.tracer, 'Archiver.getRollupStatus', ()=>this.rollup.status(localPendingCheckpointNumber, {
971
+ blockNumber: currentL1BlockNumber
972
+ }));
494
973
  const rollupStatus = {
495
974
  provenCheckpointNumber,
496
- provenArchive,
975
+ provenArchive: provenArchive.toString(),
497
976
  pendingCheckpointNumber,
498
- pendingArchive,
977
+ pendingArchive: pendingArchive.toString(),
499
978
  validationResult: initialValidationResult
500
979
  };
501
980
  this.log.trace(`Retrieved rollup status at current L1 block ${currentL1BlockNumber}.`, {
@@ -518,16 +997,15 @@ function mapArchiverConfig(config) {
518
997
  });
519
998
  }
520
999
  }
521
- const localCheckpointForDestinationProvenCheckpointNumber = await this.getCheckpoint(provenCheckpointNumber);
1000
+ const localCheckpointForDestinationProvenCheckpointNumber = await this.store.getCheckpointData(provenCheckpointNumber);
522
1001
  // Sanity check. I've hit what seems to be a state where the proven checkpoint is set to a value greater than the latest
523
1002
  // synched checkpoint when requesting L2Tips from the archiver. This is the only place where the proven checkpoint is set.
524
1003
  const synched = await this.getSynchedCheckpointNumber();
525
- if (localCheckpointForDestinationProvenCheckpointNumber && synched < localCheckpointForDestinationProvenCheckpointNumber.number) {
526
- this.log.error(`Hit local checkpoint greater than last synched checkpoint: ${localCheckpointForDestinationProvenCheckpointNumber.number} > ${synched}`);
1004
+ if (localCheckpointForDestinationProvenCheckpointNumber && synched < localCheckpointForDestinationProvenCheckpointNumber.checkpointNumber) {
1005
+ this.log.error(`Hit local checkpoint greater than last synched checkpoint: ${localCheckpointForDestinationProvenCheckpointNumber.checkpointNumber} > ${synched}`);
527
1006
  }
528
1007
  this.log.trace(`Local checkpoint for remote proven checkpoint ${provenCheckpointNumber} is ${localCheckpointForDestinationProvenCheckpointNumber?.archive.root.toString() ?? 'undefined'}`);
529
- const lastProvenBlockNumber = await this.getLastBlockNumberInCheckpoint(provenCheckpointNumber);
530
- if (localCheckpointForDestinationProvenCheckpointNumber && provenArchive === localCheckpointForDestinationProvenCheckpointNumber.archive.root.toString()) {
1008
+ if (localCheckpointForDestinationProvenCheckpointNumber && provenArchive.equals(localCheckpointForDestinationProvenCheckpointNumber.archive.root)) {
531
1009
  const localProvenCheckpointNumber = await this.getProvenCheckpointNumber();
532
1010
  if (localProvenCheckpointNumber !== provenCheckpointNumber) {
533
1011
  await this.setProvenCheckpointNumber(provenCheckpointNumber);
@@ -536,23 +1014,24 @@ function mapArchiverConfig(config) {
536
1014
  });
537
1015
  const provenSlotNumber = localCheckpointForDestinationProvenCheckpointNumber.header.slotNumber;
538
1016
  const provenEpochNumber = getEpochAtSlot(provenSlotNumber, this.l1constants);
1017
+ const lastBlockNumberInCheckpoint = localCheckpointForDestinationProvenCheckpointNumber.startBlock + localCheckpointForDestinationProvenCheckpointNumber.numBlocks - 1;
539
1018
  this.emit(L2BlockSourceEvents.L2BlockProven, {
540
1019
  type: L2BlockSourceEvents.L2BlockProven,
541
- blockNumber: BigInt(lastProvenBlockNumber),
1020
+ blockNumber: BlockNumber(lastBlockNumberInCheckpoint),
542
1021
  slotNumber: provenSlotNumber,
543
1022
  epochNumber: provenEpochNumber
544
1023
  });
1024
+ this.instrumentation.updateLastProvenBlock(lastBlockNumberInCheckpoint);
545
1025
  } else {
546
1026
  this.log.trace(`Proven checkpoint ${provenCheckpointNumber} already stored.`);
547
1027
  }
548
1028
  }
549
- this.instrumentation.updateLastProvenBlock(lastProvenBlockNumber);
550
1029
  };
551
1030
  // This is an edge case that we only hit if there are no proposed checkpoints.
552
1031
  // If we have 0 checkpoints locally and there are no checkpoints onchain there is nothing to do.
553
1032
  const noCheckpoints = localPendingCheckpointNumber === 0 && pendingCheckpointNumber === 0;
554
1033
  if (noCheckpoints) {
555
- await this.store.setBlockSynchedL1BlockNumber(currentL1BlockNumber);
1034
+ await this.store.setCheckpointSynchedL1BlockNumber(currentL1BlockNumber);
556
1035
  this.log.debug(`No checkpoints to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}, no checkpoints on chain`);
557
1036
  return rollupStatus;
558
1037
  }
@@ -560,12 +1039,12 @@ function mapArchiverConfig(config) {
560
1039
  // Related to the L2 reorgs of the pending chain. We are only interested in actually addressing a reorg if there
561
1040
  // are any state that could be impacted by it. If we have no checkpoints, there is no impact.
562
1041
  if (localPendingCheckpointNumber > 0) {
563
- const localPendingCheckpoint = await this.getCheckpoint(localPendingCheckpointNumber);
1042
+ const localPendingCheckpoint = await this.store.getCheckpointData(localPendingCheckpointNumber);
564
1043
  if (localPendingCheckpoint === undefined) {
565
1044
  throw new Error(`Missing checkpoint ${localPendingCheckpointNumber}`);
566
1045
  }
567
1046
  const localPendingArchiveRoot = localPendingCheckpoint.archive.root.toString();
568
- const noCheckpointSinceLast = localPendingCheckpoint && pendingArchive === localPendingArchiveRoot;
1047
+ const noCheckpointSinceLast = localPendingCheckpoint && pendingArchive.toString() === localPendingArchiveRoot;
569
1048
  if (noCheckpointSinceLast) {
570
1049
  // We believe the following line causes a problem when we encounter L1 re-orgs.
571
1050
  // Basically, by setting the synched L1 block number here, we are saying that we have
@@ -578,7 +1057,7 @@ function mapArchiverConfig(config) {
578
1057
  this.log.debug(`No checkpoints to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}`);
579
1058
  return rollupStatus;
580
1059
  }
581
- const localPendingCheckpointInChain = archiveForLocalPendingCheckpointNumber === localPendingArchiveRoot;
1060
+ const localPendingCheckpointInChain = archiveForLocalPendingCheckpointNumber.equals(localPendingCheckpoint.archive.root);
582
1061
  if (!localPendingCheckpointInChain) {
583
1062
  // If our local pending checkpoint tip is not in the chain on L1 a "prune" must have happened
584
1063
  // or the L1 have reorged.
@@ -591,16 +1070,16 @@ function mapArchiverConfig(config) {
591
1070
  });
592
1071
  let tipAfterUnwind = localPendingCheckpointNumber;
593
1072
  while(true){
594
- const candidateCheckpoint = await this.getCheckpoint(tipAfterUnwind);
1073
+ const candidateCheckpoint = await this.store.getCheckpointData(tipAfterUnwind);
595
1074
  if (candidateCheckpoint === undefined) {
596
1075
  break;
597
1076
  }
598
- const archiveAtContract = await this.rollup.archiveAt(BigInt(candidateCheckpoint.number));
599
- this.log.trace(`Checking local checkpoint ${candidateCheckpoint.number} with archive ${candidateCheckpoint.archive.root}`, {
1077
+ const archiveAtContract = await this.rollup.archiveAt(candidateCheckpoint.checkpointNumber);
1078
+ this.log.trace(`Checking local checkpoint ${candidateCheckpoint.checkpointNumber} with archive ${candidateCheckpoint.archive.root}`, {
600
1079
  archiveAtContract,
601
1080
  archiveLocal: candidateCheckpoint.archive.root.toString()
602
1081
  });
603
- if (archiveAtContract === candidateCheckpoint.archive.root.toString()) {
1082
+ if (archiveAtContract.equals(candidateCheckpoint.archive.root)) {
604
1083
  break;
605
1084
  }
606
1085
  tipAfterUnwind--;
@@ -619,8 +1098,8 @@ function mapArchiverConfig(config) {
619
1098
  do {
620
1099
  [searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, currentL1BlockNumber);
621
1100
  this.log.trace(`Retrieving checkpoints from L1 block ${searchStartBlock} to ${searchEndBlock}`);
622
- // TODO(md): Retrieve from blob sink then from consensus client, then from peers
623
- const retrievedCheckpoints = await retrieveCheckpointsFromRollup(this.rollup.getContract(), this.publicClient, this.blobSinkClient, searchStartBlock, searchEndBlock, this.log);
1101
+ // TODO(md): Retrieve from blob client then from consensus client, then from peers
1102
+ const retrievedCheckpoints = await execInSpan(this.tracer, 'Archiver.retrieveCheckpointsFromRollup', ()=>retrieveCheckpointsFromRollup(this.rollup.getContract(), this.publicClient, this.debugClient, this.blobClient, searchStartBlock, searchEndBlock, this.l1Addresses, this.instrumentation, this.log, !this.initialSyncComplete));
624
1103
  if (retrievedCheckpoints.length === 0) {
625
1104
  // We are not calling `setBlockSynchedL1BlockNumber` because it may cause sync issues if based off infura.
626
1105
  // See further details in earlier comments.
@@ -635,7 +1114,7 @@ function mapArchiverConfig(config) {
635
1114
  const publishedCheckpoints = await Promise.all(retrievedCheckpoints.map((b)=>retrievedToPublishedCheckpoint(b)));
636
1115
  const validCheckpoints = [];
637
1116
  for (const published of publishedCheckpoints){
638
- const validationResult = this.config.skipValidateBlockAttestations ? {
1117
+ const validationResult = this.config.skipValidateCheckpointAttestations ? {
639
1118
  valid: true
640
1119
  } : await validateCheckpointAttestations(published, this.epochCache, this.l1constants, this.log);
641
1120
  // Only update the validation result if it has changed, so we can keep track of the first invalid checkpoint
@@ -643,7 +1122,7 @@ function mapArchiverConfig(config) {
643
1122
  // There is an exception though: if a checkpoint is invalidated and replaced with another invalid checkpoint,
644
1123
  // we need to update the validation result, since we need to be able to invalidate the new one.
645
1124
  // See test 'chain progresses if an invalid checkpoint is invalidated with an invalid one' for more info.
646
- if (rollupStatus.validationResult?.valid !== validationResult.valid || !rollupStatus.validationResult.valid && !validationResult.valid && rollupStatus.validationResult.block.blockNumber === validationResult.block.blockNumber) {
1125
+ if (rollupStatus.validationResult?.valid !== validationResult.valid || !rollupStatus.validationResult.valid && !validationResult.valid && rollupStatus.validationResult.checkpoint.checkpointNumber === validationResult.checkpoint.checkpointNumber) {
647
1126
  rollupStatus.validationResult = validationResult;
648
1127
  }
649
1128
  if (!validationResult.valid) {
@@ -652,13 +1131,29 @@ function mapArchiverConfig(config) {
652
1131
  l1BlockNumber: published.l1.blockNumber,
653
1132
  ...pick(validationResult, 'reason')
654
1133
  });
655
- // Emit event for invalid block detection
656
- this.emit(L2BlockSourceEvents.InvalidAttestationsBlockDetected, {
657
- type: L2BlockSourceEvents.InvalidAttestationsBlockDetected,
1134
+ // Emit event for invalid checkpoint detection
1135
+ this.emit(L2BlockSourceEvents.InvalidAttestationsCheckpointDetected, {
1136
+ type: L2BlockSourceEvents.InvalidAttestationsCheckpointDetected,
658
1137
  validationResult
659
1138
  });
660
1139
  continue;
661
1140
  }
1141
+ // Check the inHash of the checkpoint against the l1->l2 messages.
1142
+ // The messages should've been synced up to the currentL1BlockNumber and must be available for the published
1143
+ // checkpoints we just retrieved.
1144
+ const l1ToL2Messages = await this.getL1ToL2Messages(published.checkpoint.number);
1145
+ const computedInHash = computeInHashFromL1ToL2Messages(l1ToL2Messages);
1146
+ const publishedInHash = published.checkpoint.header.inHash;
1147
+ if (!computedInHash.equals(publishedInHash)) {
1148
+ this.log.fatal(`Mismatch inHash for checkpoint ${published.checkpoint.number}`, {
1149
+ checkpointHash: published.checkpoint.hash(),
1150
+ l1BlockNumber: published.l1.blockNumber,
1151
+ computedInHash,
1152
+ publishedInHash
1153
+ });
1154
+ // Throwing an error since this is most likely caused by a bug.
1155
+ throw new Error(`Mismatch inHash for checkpoint ${published.checkpoint.number}. Expected ${computedInHash} but got ${publishedInHash}`);
1156
+ }
662
1157
  validCheckpoints.push(published);
663
1158
  this.log.debug(`Ingesting new checkpoint ${published.checkpoint.number} with ${published.checkpoint.blocks.length} blocks`, {
664
1159
  checkpointHash: published.checkpoint.hash(),
@@ -669,18 +1164,17 @@ function mapArchiverConfig(config) {
669
1164
  }
670
1165
  try {
671
1166
  const updatedValidationResult = rollupStatus.validationResult === initialValidationResult ? undefined : rollupStatus.validationResult;
672
- const [processDuration] = await elapsed(()=>this.addCheckpoints(validCheckpoints, updatedValidationResult));
1167
+ const [processDuration] = await elapsed(()=>execInSpan(this.tracer, 'Archiver.addCheckpoints', ()=>this.addCheckpoints(validCheckpoints, updatedValidationResult)));
673
1168
  this.instrumentation.processNewBlocks(processDuration / validCheckpoints.length, validCheckpoints.flatMap((c)=>c.checkpoint.blocks));
674
1169
  } catch (err) {
675
- if (err instanceof InitialBlockNumberNotSequentialError) {
676
- const { previousBlockNumber, newBlockNumber } = err;
677
- const previousBlock = previousBlockNumber ? await this.store.getPublishedBlock(previousBlockNumber) : undefined;
678
- const updatedL1SyncPoint = previousBlock?.l1.blockNumber ?? this.l1constants.l1StartBlock;
1170
+ if (err instanceof InitialCheckpointNumberNotSequentialError) {
1171
+ const { previousCheckpointNumber, newCheckpointNumber } = err;
1172
+ const previousCheckpoint = previousCheckpointNumber ? await this.store.getCheckpointData(CheckpointNumber(previousCheckpointNumber)) : undefined;
1173
+ const updatedL1SyncPoint = previousCheckpoint?.l1.blockNumber ?? this.l1constants.l1StartBlock;
679
1174
  await this.store.setBlockSynchedL1BlockNumber(updatedL1SyncPoint);
680
- this.log.warn(`Attempting to insert block ${newBlockNumber} with previous block ${previousBlockNumber}. Rolling back L1 sync point to ${updatedL1SyncPoint} to try and fetch the missing blocks.`, {
681
- previousBlockNumber,
682
- previousBlockHash: await previousBlock?.block.hash(),
683
- newBlockNumber,
1175
+ this.log.warn(`Attempting to insert checkpoint ${newCheckpointNumber} with previous block ${previousCheckpointNumber}. Rolling back L1 sync point to ${updatedL1SyncPoint} to try and fetch the missing blocks.`, {
1176
+ previousCheckpointNumber,
1177
+ newCheckpointNumber,
684
1178
  updatedL1SyncPoint
685
1179
  });
686
1180
  }
@@ -698,7 +1192,7 @@ function mapArchiverConfig(config) {
698
1192
  });
699
1193
  }
700
1194
  lastRetrievedCheckpoint = validCheckpoints.at(-1) ?? lastRetrievedCheckpoint;
701
- lastL1BlockWithCheckpoint = publishedCheckpoints.at(-1)?.l1.blockNumber ?? lastL1BlockWithCheckpoint;
1195
+ lastL1BlockWithCheckpoint = retrievedCheckpoints.at(-1)?.l1.blockNumber ?? lastL1BlockWithCheckpoint;
702
1196
  }while (searchEndBlock < currentL1BlockNumber)
703
1197
  // Important that we update AFTER inserting the blocks.
704
1198
  await updateProvenCheckpoint();
@@ -719,9 +1213,16 @@ function mapArchiverConfig(config) {
719
1213
  // We suspect an L1 reorg that added checkpoints *behind* us. If that is the case, it must have happened between
720
1214
  // the last checkpoint we saw and the current one, so we reset the last synched L1 block number. In the edge case
721
1215
  // we don't have one, we go back 2 L1 epochs, which is the deepest possible reorg (assuming Casper is working).
722
- const latestLocalCheckpoint = lastRetrievedCheckpoint ?? (latestLocalCheckpointNumber > 0 ? await this.getPublishedCheckpoints(latestLocalCheckpointNumber, 1).then(([c])=>c) : undefined);
723
- const targetL1BlockNumber = latestLocalCheckpoint?.l1.blockNumber ?? maxBigint(currentL1BlockNumber - 64n, 0n);
724
- const latestLocalCheckpointArchive = latestLocalCheckpoint?.checkpoint.archive.root.toString();
1216
+ let latestLocalCheckpointArchive = undefined;
1217
+ let targetL1BlockNumber = maxBigint(currentL1BlockNumber - 64n, 0n);
1218
+ if (lastRetrievedCheckpoint) {
1219
+ latestLocalCheckpointArchive = lastRetrievedCheckpoint.checkpoint.archive.root.toString();
1220
+ targetL1BlockNumber = lastRetrievedCheckpoint.l1.blockNumber;
1221
+ } else if (latestLocalCheckpointNumber > 0) {
1222
+ const checkpoint = await this.store.getRangeOfCheckpoints(latestLocalCheckpointNumber, 1).then(([c])=>c);
1223
+ latestLocalCheckpointArchive = checkpoint.archive.root.toString();
1224
+ targetL1BlockNumber = checkpoint.l1.blockNumber;
1225
+ }
725
1226
  this.log.warn(`Failed to reach checkpoint ${pendingCheckpointNumber} at ${currentL1BlockNumber} (latest is ${latestLocalCheckpointNumber}). ` + `Rolling back last synched L1 block number to ${targetL1BlockNumber}.`, {
726
1227
  latestLocalCheckpointNumber,
727
1228
  latestLocalCheckpointArchive,
@@ -785,31 +1286,44 @@ function mapArchiverConfig(config) {
785
1286
  async getBlocksForEpoch(epochNumber) {
786
1287
  const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1constants);
787
1288
  const blocks = [];
788
- // Walk the list of blocks backwards and filter by slots matching the requested epoch.
789
- // We'll typically ask for blocks for a very recent epoch, so we shouldn't need an index here.
790
- let block = await this.getBlock(await this.store.getSynchedL2BlockNumber());
791
- const slot = (b)=>b.header.globalVariables.slotNumber;
792
- while(block && slot(block) >= start){
793
- if (slot(block) <= end) {
794
- blocks.push(block);
1289
+ // Walk the list of checkpoints backwards and filter by slots matching the requested epoch.
1290
+ // We'll typically ask for checkpoints for a very recent epoch, so we shouldn't need an index here.
1291
+ let checkpoint = await this.store.getCheckpointData(await this.store.getSynchedCheckpointNumber());
1292
+ const slot = (b)=>b.header.slotNumber;
1293
+ while(checkpoint && slot(checkpoint) >= start){
1294
+ if (slot(checkpoint) <= end) {
1295
+ // push the blocks on backwards
1296
+ const endBlock = checkpoint.startBlock + checkpoint.numBlocks - 1;
1297
+ for(let i = endBlock; i >= checkpoint.startBlock; i--){
1298
+ const block = await this.getBlock(BlockNumber(i));
1299
+ if (block) {
1300
+ blocks.push(block);
1301
+ }
1302
+ }
795
1303
  }
796
- block = await this.getBlock(block.number - 1);
1304
+ checkpoint = await this.store.getCheckpointData(CheckpointNumber(checkpoint.checkpointNumber - 1));
797
1305
  }
798
1306
  return blocks.reverse();
799
1307
  }
800
1308
  async getBlockHeadersForEpoch(epochNumber) {
801
1309
  const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1constants);
802
1310
  const blocks = [];
803
- // Walk the list of blocks backwards and filter by slots matching the requested epoch.
804
- // We'll typically ask for blocks for a very recent epoch, so we shouldn't need an index here.
805
- let number = await this.store.getSynchedL2BlockNumber();
806
- let header = await this.getBlockHeader(number);
807
- const slot = (b)=>b.globalVariables.slotNumber;
808
- while(header && slot(header) >= start){
809
- if (slot(header) <= end) {
810
- blocks.push(header);
1311
+ // Walk the list of checkpoints backwards and filter by slots matching the requested epoch.
1312
+ // We'll typically ask for checkpoints for a very recent epoch, so we shouldn't need an index here.
1313
+ let checkpoint = await this.store.getCheckpointData(await this.store.getSynchedCheckpointNumber());
1314
+ const slot = (b)=>b.header.slotNumber;
1315
+ while(checkpoint && slot(checkpoint) >= start){
1316
+ if (slot(checkpoint) <= end) {
1317
+ // push the blocks on backwards
1318
+ const endBlock = checkpoint.startBlock + checkpoint.numBlocks - 1;
1319
+ for(let i = endBlock; i >= checkpoint.startBlock; i--){
1320
+ const block = await this.getBlockHeader(BlockNumber(i));
1321
+ if (block) {
1322
+ blocks.push(block);
1323
+ }
1324
+ }
811
1325
  }
812
- header = await this.getBlockHeader(--number);
1326
+ checkpoint = await this.store.getCheckpointData(CheckpointNumber(checkpoint.checkpointNumber - 1));
813
1327
  }
814
1328
  return blocks.reverse();
815
1329
  }
@@ -840,24 +1354,6 @@ function mapArchiverConfig(config) {
840
1354
  /** Returns whether the archiver has completed an initial sync run successfully. */ isInitialSyncComplete() {
841
1355
  return this.initialSyncComplete;
842
1356
  }
843
- async getPublishedCheckpoints(from, limit, proven) {
844
- const blocks = await this.getPublishedBlocks(from, limit, proven);
845
- return blocks.map((b)=>b.toPublishedCheckpoint());
846
- }
847
- async getCheckpoints(from, limit, proven) {
848
- const published = await this.getPublishedCheckpoints(from, limit, proven);
849
- return published.map((p)=>p.checkpoint);
850
- }
851
- async getCheckpoint(number) {
852
- if (number < 0) {
853
- number = await this.getSynchedCheckpointNumber();
854
- }
855
- if (number === 0) {
856
- return undefined;
857
- }
858
- const published = await this.getPublishedCheckpoints(number, 1);
859
- return published[0]?.checkpoint;
860
- }
861
1357
  async getCheckpointHeader(number) {
862
1358
  if (number === 'latest') {
863
1359
  number = await this.getSynchedCheckpointNumber();
@@ -865,53 +1361,36 @@ function mapArchiverConfig(config) {
865
1361
  if (number === 0) {
866
1362
  return undefined;
867
1363
  }
868
- const checkpoint = await this.getCheckpoint(number);
869
- return checkpoint?.header;
1364
+ const checkpoint = await this.store.getCheckpointData(number);
1365
+ if (!checkpoint) {
1366
+ return undefined;
1367
+ }
1368
+ return checkpoint.header;
870
1369
  }
871
1370
  getCheckpointNumber() {
872
1371
  return this.getSynchedCheckpointNumber();
873
1372
  }
874
1373
  getSynchedCheckpointNumber() {
875
- // TODO: Checkpoint number will no longer be the same as the block number once we support multiple blocks per checkpoint.
876
- return this.store.getSynchedL2BlockNumber();
1374
+ return this.store.getSynchedCheckpointNumber();
877
1375
  }
878
1376
  getProvenCheckpointNumber() {
879
- // TODO: Proven checkpoint number will no longer be the same as the proven block number once we support multiple blocks per checkpoint.
880
- return this.store.getProvenL2BlockNumber();
1377
+ return this.store.getProvenCheckpointNumber();
881
1378
  }
882
1379
  setProvenCheckpointNumber(checkpointNumber) {
883
- // TODO: Proven checkpoint number will no longer be the same as the proven block number once we support multiple blocks per checkpoint.
884
- return this.store.setProvenL2BlockNumber(checkpointNumber);
1380
+ return this.store.setProvenCheckpointNumber(checkpointNumber);
885
1381
  }
886
1382
  unwindCheckpoints(from, checkpointsToUnwind) {
887
- // TODO: This only works if we have one block per checkpoint.
888
- return this.store.unwindBlocks(from, checkpointsToUnwind);
1383
+ return this.store.unwindCheckpoints(from, checkpointsToUnwind);
889
1384
  }
890
- getLastBlockNumberInCheckpoint(checkpointNumber) {
891
- // TODO: Checkpoint number will no longer be the same as the block number once we support multiple blocks per checkpoint.
892
- return Promise.resolve(checkpointNumber);
1385
+ async getLastBlockNumberInCheckpoint(checkpointNumber) {
1386
+ const checkpointData = await this.store.getCheckpointData(checkpointNumber);
1387
+ if (!checkpointData) {
1388
+ return undefined;
1389
+ }
1390
+ return BlockNumber(checkpointData.startBlock + checkpointData.numBlocks - 1);
893
1391
  }
894
1392
  addCheckpoints(checkpoints, pendingChainValidationStatus) {
895
- return this.store.addBlocks(checkpoints.map((p)=>PublishedL2Block.fromPublishedCheckpoint(p)), pendingChainValidationStatus);
896
- }
897
- /**
898
- * Gets up to `limit` amount of L2 blocks starting from `from`.
899
- * @param from - Number of the first block to return (inclusive).
900
- * @param limit - The number of blocks to return.
901
- * @param proven - If true, only return blocks that have been proven.
902
- * @returns The requested L2 blocks.
903
- */ getBlocks(from, limit, proven) {
904
- return this.getPublishedBlocks(from, limit, proven).then((blocks)=>blocks.map((b)=>b.block));
905
- }
906
- /** Equivalent to getBlocks but includes publish data. */ async getPublishedBlocks(from, limit, proven) {
907
- const limitWithProven = proven ? Math.min(limit, Math.max(await this.store.getProvenL2BlockNumber() - from + 1, 0)) : limit;
908
- return limitWithProven === 0 ? [] : await this.store.getPublishedBlocks(from, limitWithProven);
909
- }
910
- getPublishedBlockByHash(blockHash) {
911
- return this.store.getPublishedBlockByHash(blockHash);
912
- }
913
- getPublishedBlockByArchive(archive) {
914
- return this.store.getPublishedBlockByArchive(archive);
1393
+ return this.store.addCheckpoints(checkpoints, pendingChainValidationStatus);
915
1394
  }
916
1395
  getBlockHeaderByHash(blockHash) {
917
1396
  return this.store.getBlockHeaderByHash(blockHash);
@@ -923,7 +1402,7 @@ function mapArchiverConfig(config) {
923
1402
  * Gets an l2 block.
924
1403
  * @param number - The block number to return.
925
1404
  * @returns The requested L2 block.
926
- */ async getBlock(number) {
1405
+ */ async getL2BlockNew(number) {
927
1406
  // If the number provided is -ve, then return the latest block.
928
1407
  if (number < 0) {
929
1408
  number = await this.store.getSynchedL2BlockNumber();
@@ -931,8 +1410,16 @@ function mapArchiverConfig(config) {
931
1410
  if (number === 0) {
932
1411
  return undefined;
933
1412
  }
934
- const publishedBlock = await this.store.getPublishedBlock(number);
935
- return publishedBlock?.block;
1413
+ const publishedBlock = await this.store.store.getBlock(number);
1414
+ return publishedBlock;
1415
+ }
1416
+ async getL2BlocksNew(from, limit, proven) {
1417
+ const blocks = await this.store.store.getBlocks(from, limit);
1418
+ if (proven === true) {
1419
+ const provenBlockNumber = await this.store.getProvenBlockNumber();
1420
+ return blocks.filter((b)=>b.number <= provenBlockNumber);
1421
+ }
1422
+ return blocks;
936
1423
  }
937
1424
  async getBlockHeader(number) {
938
1425
  if (number === 'latest') {
@@ -944,27 +1431,40 @@ function mapArchiverConfig(config) {
944
1431
  const headers = await this.store.getBlockHeaders(number, 1);
945
1432
  return headers.length === 0 ? undefined : headers[0];
946
1433
  }
1434
+ getCheckpointedBlock(number) {
1435
+ return this.store.getCheckpointedBlock(number);
1436
+ }
1437
+ async getCheckpointedBlocks(from, limit, proven) {
1438
+ const blocks = await this.store.store.getCheckpointedBlocks(from, limit);
1439
+ if (proven === true) {
1440
+ const provenBlockNumber = await this.store.getProvenBlockNumber();
1441
+ return blocks.filter((b)=>b.block.number <= provenBlockNumber);
1442
+ }
1443
+ return blocks;
1444
+ }
1445
+ getCheckpointedBlockByHash(blockHash) {
1446
+ return this.store.getCheckpointedBlockByHash(blockHash);
1447
+ }
1448
+ getProvenBlockNumber() {
1449
+ return this.store.getProvenBlockNumber();
1450
+ }
1451
+ getCheckpointedBlockNumber() {
1452
+ return this.store.getCheckpointedL2BlockNumber();
1453
+ }
1454
+ getCheckpointedBlockByArchive(archive) {
1455
+ return this.store.getCheckpointedBlockByArchive(archive);
1456
+ }
947
1457
  getTxEffect(txHash) {
948
1458
  return this.store.getTxEffect(txHash);
949
1459
  }
950
1460
  getSettledTxReceipt(txHash) {
951
1461
  return this.store.getSettledTxReceipt(txHash);
952
1462
  }
953
- /**
954
- * Retrieves all private logs from up to `limit` blocks, starting from the block number `from`.
955
- * @param from - The block number from which to begin retrieving logs.
956
- * @param limit - The maximum number of blocks to retrieve logs from.
957
- * @returns An array of private logs from the specified range of blocks.
958
- */ getPrivateLogs(from, limit) {
959
- return this.store.getPrivateLogs(from, limit);
1463
+ getPrivateLogsByTags(tags) {
1464
+ return this.store.getPrivateLogsByTags(tags);
960
1465
  }
961
- /**
962
- * Gets all logs that match any of the received tags (i.e. logs with their first field equal to a tag).
963
- * @param tags - The tags to filter the logs by.
964
- * @returns For each received tag, an array of matching logs is returned. An empty array implies no logs match
965
- * that tag.
966
- */ getLogsByTags(tags) {
967
- return this.store.getLogsByTags(tags);
1466
+ getPublicLogsByTagsFromContract(contractAddress, tags) {
1467
+ return this.store.getPublicLogsByTagsFromContract(contractAddress, tags);
968
1468
  }
969
1469
  /**
970
1470
  * Gets public logs based on the provided filter.
@@ -982,15 +1482,10 @@ function mapArchiverConfig(config) {
982
1482
  }
983
1483
  /**
984
1484
  * Gets the number of the latest L2 block processed by the block source implementation.
1485
+ * This includes both checkpointed and uncheckpointed blocks.
985
1486
  * @returns The number of the latest L2 block processed by the block source implementation.
986
1487
  */ getBlockNumber() {
987
- return this.store.getSynchedL2BlockNumber();
988
- }
989
- getProvenBlockNumber() {
990
- return this.store.getProvenL2BlockNumber();
991
- }
992
- /** Forcefully updates the last proven block number. Use for testing. */ setProvenBlockNumber(blockNumber) {
993
- return this.store.setProvenL2BlockNumber(blockNumber);
1488
+ return this.store.getLatestBlockNumber();
994
1489
  }
995
1490
  getContractClass(id) {
996
1491
  return this.store.getContractClass(id);
@@ -1010,11 +1505,11 @@ function mapArchiverConfig(config) {
1010
1505
  return this.store.getContractInstance(address, timestamp);
1011
1506
  }
1012
1507
  /**
1013
- * Gets L1 to L2 message (to be) included in a given block.
1014
- * @param blockNumber - L2 block number to get messages for.
1508
+ * Gets L1 to L2 message (to be) included in a given checkpoint.
1509
+ * @param checkpointNumber - Checkpoint number to get messages for.
1015
1510
  * @returns The L1 to L2 messages/leaves of the messages subtree (throws if not found).
1016
- */ getL1ToL2Messages(blockNumber) {
1017
- return this.store.getL1ToL2Messages(blockNumber);
1511
+ */ getL1ToL2Messages(checkpointNumber) {
1512
+ return this.store.getL1ToL2Messages(checkpointNumber);
1018
1513
  }
1019
1514
  /**
1020
1515
  * Gets the L1 to L2 message index in the L1 to L2 message tree.
@@ -1041,65 +1536,114 @@ function mapArchiverConfig(config) {
1041
1536
  return this.getPendingChainValidationStatus().then((status)=>!status.valid);
1042
1537
  }
1043
1538
  async getL2Tips() {
1044
- const [latestBlockNumber, provenBlockNumber] = await Promise.all([
1539
+ const [latestBlockNumber, provenBlockNumber, checkpointedBlockNumber] = await Promise.all([
1045
1540
  this.getBlockNumber(),
1046
- this.getProvenBlockNumber()
1541
+ this.getProvenBlockNumber(),
1542
+ this.getCheckpointedBlockNumber()
1047
1543
  ]);
1048
1544
  // TODO(#13569): Compute proper finalized block number based on L1 finalized block.
1049
1545
  // We just force it 2 epochs worth of proven data for now.
1050
1546
  // NOTE: update end-to-end/src/e2e_epochs/epochs_empty_blocks.test.ts as that uses finalized blocks in computations
1051
- const finalizedBlockNumber = Math.max(provenBlockNumber - this.l1constants.epochDuration * 2, 0);
1052
- const [latestBlockHeader, provenBlockHeader, finalizedBlockHeader] = await Promise.all([
1053
- latestBlockNumber > 0 ? this.getBlockHeader(latestBlockNumber) : undefined,
1054
- provenBlockNumber > 0 ? this.getBlockHeader(provenBlockNumber) : undefined,
1055
- finalizedBlockNumber > 0 ? this.getBlockHeader(finalizedBlockNumber) : undefined
1547
+ const finalizedBlockNumber = BlockNumber(Math.max(provenBlockNumber - this.l1constants.epochDuration * 2, 0));
1548
+ const beforeInitialblockNumber = BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
1549
+ // Get the latest block header and checkpointed blocks for proven, finalised and checkpointed blocks
1550
+ const [latestBlockHeader, provenCheckpointedBlock, finalizedCheckpointedBlock, checkpointedBlock] = await Promise.all([
1551
+ latestBlockNumber > beforeInitialblockNumber ? this.getBlockHeader(latestBlockNumber) : undefined,
1552
+ provenBlockNumber > beforeInitialblockNumber ? this.getCheckpointedBlock(provenBlockNumber) : undefined,
1553
+ finalizedBlockNumber > beforeInitialblockNumber ? this.getCheckpointedBlock(finalizedBlockNumber) : undefined,
1554
+ checkpointedBlockNumber > beforeInitialblockNumber ? this.getCheckpointedBlock(checkpointedBlockNumber) : undefined
1056
1555
  ]);
1057
- if (latestBlockNumber > 0 && !latestBlockHeader) {
1556
+ if (latestBlockNumber > beforeInitialblockNumber && !latestBlockHeader) {
1058
1557
  throw new Error(`Failed to retrieve latest block header for block ${latestBlockNumber}`);
1059
1558
  }
1060
- if (provenBlockNumber > 0 && !provenBlockHeader) {
1061
- throw new Error(`Failed to retrieve proven block header for block ${provenBlockNumber} (latest block is ${latestBlockNumber})`);
1559
+ // Checkpointed blocks must exist for proven, finalized and checkpointed tips if they are beyond the initial block number.
1560
+ if (checkpointedBlockNumber > beforeInitialblockNumber && !checkpointedBlock?.block.header) {
1561
+ throw new Error(`Failed to retrieve checkpointed block header for block ${checkpointedBlockNumber} (latest block is ${latestBlockNumber})`);
1062
1562
  }
1063
- if (finalizedBlockNumber > 0 && !finalizedBlockHeader) {
1563
+ if (provenBlockNumber > beforeInitialblockNumber && !provenCheckpointedBlock?.block.header) {
1564
+ throw new Error(`Failed to retrieve proven checkpointed for block ${provenBlockNumber} (latest block is ${latestBlockNumber})`);
1565
+ }
1566
+ if (finalizedBlockNumber > beforeInitialblockNumber && !finalizedCheckpointedBlock?.block.header) {
1064
1567
  throw new Error(`Failed to retrieve finalized block header for block ${finalizedBlockNumber} (latest block is ${latestBlockNumber})`);
1065
1568
  }
1066
- const latestBlockHeaderHash = await latestBlockHeader?.hash();
1067
- const provenBlockHeaderHash = await provenBlockHeader?.hash();
1068
- const finalizedBlockHeaderHash = await finalizedBlockHeader?.hash();
1069
- return {
1070
- latest: {
1569
+ const latestBlockHeaderHash = await latestBlockHeader?.hash() ?? GENESIS_BLOCK_HEADER_HASH;
1570
+ const provenBlockHeaderHash = await provenCheckpointedBlock?.block.header?.hash() ?? GENESIS_BLOCK_HEADER_HASH;
1571
+ const finalizedBlockHeaderHash = await finalizedCheckpointedBlock?.block.header?.hash() ?? GENESIS_BLOCK_HEADER_HASH;
1572
+ const checkpointedBlockHeaderHash = await checkpointedBlock?.block.header?.hash() ?? GENESIS_BLOCK_HEADER_HASH;
1573
+ // Now attempt to retrieve checkpoints for proven, finalised and checkpointed blocks
1574
+ const [[provenBlockCheckpoint], [finalizedBlockCheckpoint], [checkpointedBlockCheckpoint]] = await Promise.all([
1575
+ provenCheckpointedBlock !== undefined ? await this.getPublishedCheckpoints(provenCheckpointedBlock?.checkpointNumber, 1) : [
1576
+ undefined
1577
+ ],
1578
+ finalizedCheckpointedBlock !== undefined ? await this.getPublishedCheckpoints(finalizedCheckpointedBlock?.checkpointNumber, 1) : [
1579
+ undefined
1580
+ ],
1581
+ checkpointedBlock !== undefined ? await this.getPublishedCheckpoints(checkpointedBlock?.checkpointNumber, 1) : [
1582
+ undefined
1583
+ ]
1584
+ ]);
1585
+ const initialcheckpointId = {
1586
+ number: CheckpointNumber.ZERO,
1587
+ hash: GENESIS_CHECKPOINT_HEADER_HASH.toString()
1588
+ };
1589
+ const makeCheckpointId = (checkpoint)=>{
1590
+ if (checkpoint === undefined) {
1591
+ return initialcheckpointId;
1592
+ }
1593
+ return {
1594
+ number: checkpoint.checkpoint.number,
1595
+ hash: checkpoint.checkpoint.hash().toString()
1596
+ };
1597
+ };
1598
+ const l2Tips = {
1599
+ proposed: {
1071
1600
  number: latestBlockNumber,
1072
- hash: latestBlockHeaderHash?.toString()
1601
+ hash: latestBlockHeaderHash.toString()
1073
1602
  },
1074
1603
  proven: {
1075
- number: provenBlockNumber,
1076
- hash: provenBlockHeaderHash?.toString()
1604
+ block: {
1605
+ number: provenBlockNumber,
1606
+ hash: provenBlockHeaderHash.toString()
1607
+ },
1608
+ checkpoint: makeCheckpointId(provenBlockCheckpoint)
1077
1609
  },
1078
1610
  finalized: {
1079
- number: finalizedBlockNumber,
1080
- hash: finalizedBlockHeaderHash?.toString()
1611
+ block: {
1612
+ number: finalizedBlockNumber,
1613
+ hash: finalizedBlockHeaderHash.toString()
1614
+ },
1615
+ checkpoint: makeCheckpointId(finalizedBlockCheckpoint)
1616
+ },
1617
+ checkpointed: {
1618
+ block: {
1619
+ number: checkpointedBlockNumber,
1620
+ hash: checkpointedBlockHeaderHash.toString()
1621
+ },
1622
+ checkpoint: makeCheckpointId(checkpointedBlockCheckpoint)
1081
1623
  }
1082
1624
  };
1625
+ return l2Tips;
1083
1626
  }
1084
1627
  async rollbackTo(targetL2BlockNumber) {
1628
+ // TODO(pw/mbps): This still assumes 1 block per checkpoint
1085
1629
  const currentBlocks = await this.getL2Tips();
1086
- const currentL2Block = currentBlocks.latest.number;
1087
- const currentProvenBlock = currentBlocks.proven.number;
1088
- // const currentFinalizedBlock = currentBlocks.finalized.number;
1630
+ const currentL2Block = currentBlocks.proposed.number;
1631
+ const currentProvenBlock = currentBlocks.proven.block.number;
1089
1632
  if (targetL2BlockNumber >= currentL2Block) {
1090
1633
  throw new Error(`Target L2 block ${targetL2BlockNumber} must be less than current L2 block ${currentL2Block}`);
1091
1634
  }
1092
1635
  const blocksToUnwind = currentL2Block - targetL2BlockNumber;
1093
- const targetL2Block = await this.store.getPublishedBlock(targetL2BlockNumber);
1636
+ const targetL2Block = await this.store.getCheckpointedBlock(targetL2BlockNumber);
1094
1637
  if (!targetL2Block) {
1095
1638
  throw new Error(`Target L2 block ${targetL2BlockNumber} not found`);
1096
1639
  }
1097
1640
  const targetL1BlockNumber = targetL2Block.l1.blockNumber;
1641
+ const targetCheckpointNumber = CheckpointNumber.fromBlockNumber(targetL2BlockNumber);
1098
1642
  const targetL1BlockHash = await this.getL1BlockHash(targetL1BlockNumber);
1099
- this.log.info(`Unwinding ${blocksToUnwind} blocks from L2 block ${currentL2Block}`);
1100
- await this.store.unwindBlocks(currentL2Block, blocksToUnwind);
1101
- this.log.info(`Unwinding L1 to L2 messages to ${targetL2BlockNumber}`);
1102
- await this.store.rollbackL1ToL2MessagesToL2Block(targetL2BlockNumber);
1643
+ this.log.info(`Unwinding ${blocksToUnwind} checkpoints from L2 block ${currentL2Block}`);
1644
+ await this.store.unwindCheckpoints(CheckpointNumber(currentL2Block), blocksToUnwind);
1645
+ this.log.info(`Unwinding L1 to L2 messages to checkpoint ${targetCheckpointNumber}`);
1646
+ await this.store.rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber);
1103
1647
  this.log.info(`Setting L1 syncpoints to ${targetL1BlockNumber}`);
1104
1648
  await this.store.setBlockSynchedL1BlockNumber(targetL1BlockNumber);
1105
1649
  await this.store.setMessageSynchedL1Block({
@@ -1108,7 +1652,7 @@ function mapArchiverConfig(config) {
1108
1652
  });
1109
1653
  if (targetL2BlockNumber < currentProvenBlock) {
1110
1654
  this.log.info(`Clearing proven L2 block number`);
1111
- await this.store.setProvenL2BlockNumber(0);
1655
+ await this.store.setProvenCheckpointNumber(CheckpointNumber.ZERO);
1112
1656
  }
1113
1657
  // TODO(palla/reorg): Set the finalized block when we add support for it.
1114
1658
  // if (targetL2BlockNumber < currentFinalizedBlock) {
@@ -1116,10 +1660,104 @@ function mapArchiverConfig(config) {
1116
1660
  // await this.store.setFinalizedL2BlockNumber(0);
1117
1661
  // }
1118
1662
  }
1663
+ async getPublishedCheckpoints(checkpointNumber, limit) {
1664
+ const checkpoints = await this.store.getRangeOfCheckpoints(checkpointNumber, limit);
1665
+ const blocks = (await Promise.all(checkpoints.map((ch)=>this.store.getBlocksForCheckpoint(ch.checkpointNumber)))).filter(isDefined);
1666
+ const fullCheckpoints = [];
1667
+ for(let i = 0; i < checkpoints.length; i++){
1668
+ const blocksForCheckpoint = blocks[i];
1669
+ const checkpoint = checkpoints[i];
1670
+ const fullCheckpoint = new Checkpoint(checkpoint.archive, checkpoint.header, blocksForCheckpoint, checkpoint.checkpointNumber);
1671
+ const publishedCheckpoint = new PublishedCheckpoint(fullCheckpoint, checkpoint.l1, checkpoint.attestations.map((x)=>CommitteeAttestation.fromBuffer(x)));
1672
+ fullCheckpoints.push(publishedCheckpoint);
1673
+ }
1674
+ return fullCheckpoints;
1675
+ }
1676
+ async getCheckpointsForEpoch(epochNumber) {
1677
+ const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1constants);
1678
+ const checkpoints = [];
1679
+ // Walk the list of checkpoints backwards and filter by slots matching the requested epoch.
1680
+ // We'll typically ask for checkpoints for a very recent epoch, so we shouldn't need an index here.
1681
+ let checkpointData = await this.store.getCheckpointData(await this.store.getSynchedCheckpointNumber());
1682
+ const slot = (b)=>b.header.slotNumber;
1683
+ while(checkpointData && slot(checkpointData) >= start){
1684
+ if (slot(checkpointData) <= end) {
1685
+ // push the checkpoints on backwards
1686
+ const [checkpoint] = await this.getPublishedCheckpoints(checkpointData.checkpointNumber, 1);
1687
+ checkpoints.push(checkpoint.checkpoint);
1688
+ }
1689
+ checkpointData = await this.store.getCheckpointData(CheckpointNumber(checkpointData.checkpointNumber - 1));
1690
+ }
1691
+ return checkpoints.reverse();
1692
+ }
1693
+ /* Legacy APIs */ async getPublishedBlockByHash(blockHash) {
1694
+ const checkpointedBlock = await this.store.getCheckpointedBlockByHash(blockHash);
1695
+ return this.buildOldBlockFromCheckpointedBlock(checkpointedBlock);
1696
+ }
1697
+ async getPublishedBlockByArchive(archive) {
1698
+ const checkpointedBlock = await this.store.getCheckpointedBlockByArchive(archive);
1699
+ return this.buildOldBlockFromCheckpointedBlock(checkpointedBlock);
1700
+ }
1701
+ /**
1702
+ * Gets up to `limit` amount of L2 blocks starting from `from`.
1703
+ * @param from - Number of the first block to return (inclusive).
1704
+ * @param limit - The number of blocks to return.
1705
+ * @param proven - If true, only return blocks that have been proven.
1706
+ * @returns The requested L2 blocks.
1707
+ */ async getBlocks(from, limit, proven) {
1708
+ const publishedBlocks = await this.getPublishedBlocks(from, limit, proven);
1709
+ return publishedBlocks.map((x)=>x.block);
1710
+ }
1711
+ async getPublishedBlocks(from, limit, proven) {
1712
+ const checkpoints = await this.store.getRangeOfCheckpoints(CheckpointNumber(from), limit);
1713
+ const provenCheckpointNumber = await this.getProvenCheckpointNumber();
1714
+ const blocks = (await Promise.all(checkpoints.map((ch)=>this.store.getBlocksForCheckpoint(ch.checkpointNumber)))).filter(isDefined);
1715
+ const olbBlocks = [];
1716
+ for(let i = 0; i < checkpoints.length; i++){
1717
+ const blockForCheckpoint = blocks[i][0];
1718
+ const checkpoint = checkpoints[i];
1719
+ if (checkpoint.checkpointNumber > provenCheckpointNumber && proven === true) {
1720
+ continue;
1721
+ }
1722
+ const oldCheckpoint = new Checkpoint(blockForCheckpoint.archive, checkpoint.header, [
1723
+ blockForCheckpoint
1724
+ ], checkpoint.checkpointNumber);
1725
+ const oldBlock = L2Block.fromCheckpoint(oldCheckpoint);
1726
+ const publishedBlock = new PublishedL2Block(oldBlock, checkpoint.l1, checkpoint.attestations.map((x)=>CommitteeAttestation.fromBuffer(x)));
1727
+ olbBlocks.push(publishedBlock);
1728
+ }
1729
+ return olbBlocks;
1730
+ }
1731
+ async buildOldBlockFromCheckpointedBlock(checkpointedBlock) {
1732
+ if (!checkpointedBlock) {
1733
+ return undefined;
1734
+ }
1735
+ const checkpoint = await this.store.getCheckpointData(checkpointedBlock.checkpointNumber);
1736
+ if (!checkpoint) {
1737
+ return checkpoint;
1738
+ }
1739
+ const fullCheckpoint = new Checkpoint(checkpointedBlock?.block.archive, checkpoint?.header, [
1740
+ checkpointedBlock.block
1741
+ ], checkpoint.checkpointNumber);
1742
+ const oldBlock = L2Block.fromCheckpoint(fullCheckpoint);
1743
+ const published = new PublishedL2Block(oldBlock, checkpoint.l1, checkpoint.attestations.map((x)=>CommitteeAttestation.fromBuffer(x)));
1744
+ return published;
1745
+ }
1746
+ async getBlock(number) {
1747
+ // If the number provided is -ve, then return the latest block.
1748
+ if (number < 0) {
1749
+ number = await this.store.getSynchedL2BlockNumber();
1750
+ }
1751
+ if (number === 0) {
1752
+ return undefined;
1753
+ }
1754
+ const publishedBlocks = await this.getPublishedBlocks(number, 1);
1755
+ if (publishedBlocks.length === 0) {
1756
+ return undefined;
1757
+ }
1758
+ return publishedBlocks[0].block;
1759
+ }
1119
1760
  }
1120
- _ts_decorate([
1121
- trackSpan('Archiver.sync')
1122
- ], Archiver.prototype, "sync", null);
1123
1761
  var Operation = /*#__PURE__*/ function(Operation) {
1124
1762
  Operation[Operation["Store"] = 0] = "Store";
1125
1763
  Operation[Operation["Delete"] = 1] = "Delete";
@@ -1237,6 +1875,18 @@ var Operation = /*#__PURE__*/ function(Operation) {
1237
1875
  }
1238
1876
  return true;
1239
1877
  }
1878
+ async addBlockDataToDB(block) {
1879
+ const contractClassLogs = block.body.txEffects.flatMap((txEffect)=>txEffect.contractClassLogs);
1880
+ // ContractInstancePublished event logs are broadcast in privateLogs.
1881
+ const privateLogs = block.body.txEffects.flatMap((txEffect)=>txEffect.privateLogs);
1882
+ const publicLogs = block.body.txEffects.flatMap((txEffect)=>txEffect.publicLogs);
1883
+ return (await Promise.all([
1884
+ this.#updatePublishedContractClasses(contractClassLogs, block.number, 0),
1885
+ this.#updateDeployedContractInstances(privateLogs, block.number, 0),
1886
+ this.#updateUpdatedContractInstances(publicLogs, block.header.globalVariables.timestamp, 0),
1887
+ this.#storeBroadcastedIndividualFunctions(contractClassLogs, block.number)
1888
+ ])).every(Boolean);
1889
+ }
1240
1890
  addBlocks(blocks, pendingChainValidationStatus) {
1241
1891
  // Add the blocks to the store. Store will throw if the blocks are not in order, there are gaps,
1242
1892
  // or if the previous block is not in the store.
@@ -1246,34 +1896,51 @@ var Operation = /*#__PURE__*/ function(Operation) {
1246
1896
  // Update the pending chain validation status if provided
1247
1897
  pendingChainValidationStatus && this.store.setPendingChainValidationStatus(pendingChainValidationStatus),
1248
1898
  // Add any logs emitted during the retrieved blocks
1249
- this.store.addLogs(blocks.map((block)=>block.block)),
1899
+ this.store.addLogs(blocks),
1250
1900
  // Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
1251
- ...blocks.map(async (block)=>{
1252
- const contractClassLogs = block.block.body.txEffects.flatMap((txEffect)=>txEffect.contractClassLogs);
1253
- // ContractInstancePublished event logs are broadcast in privateLogs.
1254
- const privateLogs = block.block.body.txEffects.flatMap((txEffect)=>txEffect.privateLogs);
1255
- const publicLogs = block.block.body.txEffects.flatMap((txEffect)=>txEffect.publicLogs);
1256
- return (await Promise.all([
1257
- this.#updatePublishedContractClasses(contractClassLogs, block.block.number, 0),
1258
- this.#updateDeployedContractInstances(privateLogs, block.block.number, 0),
1259
- this.#updateUpdatedContractInstances(publicLogs, block.block.header.globalVariables.timestamp, 0),
1260
- this.#storeBroadcastedIndividualFunctions(contractClassLogs, block.block.number)
1261
- ])).every(Boolean);
1901
+ ...blocks.map((block)=>{
1902
+ return this.addBlockDataToDB(block);
1262
1903
  })
1263
1904
  ]);
1264
1905
  return opResults.every(Boolean);
1265
1906
  });
1266
1907
  }
1267
- async unwindBlocks(from, blocksToUnwind) {
1268
- const last = await this.getSynchedL2BlockNumber();
1908
+ addCheckpoints(checkpoints, pendingChainValidationStatus) {
1909
+ // Add the blocks to the store. Store will throw if the blocks are not in order, there are gaps,
1910
+ // or if the previous block is not in the store.
1911
+ return this.store.transactionAsync(async ()=>{
1912
+ await this.store.addCheckpoints(checkpoints);
1913
+ const allBlocks = checkpoints.flatMap((ch)=>ch.checkpoint.blocks);
1914
+ const opResults = await Promise.all([
1915
+ // Update the pending chain validation status if provided
1916
+ pendingChainValidationStatus && this.store.setPendingChainValidationStatus(pendingChainValidationStatus),
1917
+ // Add any logs emitted during the retrieved blocks
1918
+ this.store.addLogs(allBlocks),
1919
+ // Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
1920
+ ...allBlocks.map((block)=>{
1921
+ return this.addBlockDataToDB(block);
1922
+ })
1923
+ ]);
1924
+ return opResults.every(Boolean);
1925
+ });
1926
+ }
1927
+ async unwindCheckpoints(from, checkpointsToUnwind) {
1928
+ if (checkpointsToUnwind <= 0) {
1929
+ throw new Error(`Cannot unwind ${checkpointsToUnwind} blocks`);
1930
+ }
1931
+ const last = await this.getSynchedCheckpointNumber();
1269
1932
  if (from != last) {
1270
- throw new Error(`Cannot unwind blocks from block ${from} when the last block is ${last}`);
1933
+ throw new Error(`Cannot unwind checkpoints from checkpoint ${from} when the last checkpoint is ${last}`);
1271
1934
  }
1272
- if (blocksToUnwind <= 0) {
1273
- throw new Error(`Cannot unwind ${blocksToUnwind} blocks`);
1935
+ const blocks = [];
1936
+ const lastCheckpointNumber = from + checkpointsToUnwind - 1;
1937
+ for(let checkpointNumber = from; checkpointNumber <= lastCheckpointNumber; checkpointNumber++){
1938
+ const blocksForCheckpoint = await this.store.getBlocksForCheckpoint(checkpointNumber);
1939
+ if (!blocksForCheckpoint) {
1940
+ continue;
1941
+ }
1942
+ blocks.push(...blocksForCheckpoint);
1274
1943
  }
1275
- // from - blocksToUnwind = the new head, so + 1 for what we need to remove
1276
- const blocks = await this.getPublishedBlocks(from - blocksToUnwind + 1, blocksToUnwind);
1277
1944
  const opResults = await Promise.all([
1278
1945
  // Prune rolls back to the last proven block, which is by definition valid
1279
1946
  this.store.setPendingChainValidationStatus({
@@ -1281,32 +1948,44 @@ var Operation = /*#__PURE__*/ function(Operation) {
1281
1948
  }),
1282
1949
  // Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
1283
1950
  ...blocks.map(async (block)=>{
1284
- const contractClassLogs = block.block.body.txEffects.flatMap((txEffect)=>txEffect.contractClassLogs);
1951
+ const contractClassLogs = block.body.txEffects.flatMap((txEffect)=>txEffect.contractClassLogs);
1285
1952
  // ContractInstancePublished event logs are broadcast in privateLogs.
1286
- const privateLogs = block.block.body.txEffects.flatMap((txEffect)=>txEffect.privateLogs);
1287
- const publicLogs = block.block.body.txEffects.flatMap((txEffect)=>txEffect.publicLogs);
1953
+ const privateLogs = block.body.txEffects.flatMap((txEffect)=>txEffect.privateLogs);
1954
+ const publicLogs = block.body.txEffects.flatMap((txEffect)=>txEffect.publicLogs);
1288
1955
  return (await Promise.all([
1289
- this.#updatePublishedContractClasses(contractClassLogs, block.block.number, 1),
1290
- this.#updateDeployedContractInstances(privateLogs, block.block.number, 1),
1291
- this.#updateUpdatedContractInstances(publicLogs, block.block.header.globalVariables.timestamp, 1)
1956
+ this.#updatePublishedContractClasses(contractClassLogs, block.number, 1),
1957
+ this.#updateDeployedContractInstances(privateLogs, block.number, 1),
1958
+ this.#updateUpdatedContractInstances(publicLogs, block.header.globalVariables.timestamp, 1)
1292
1959
  ])).every(Boolean);
1293
1960
  }),
1294
- this.store.deleteLogs(blocks.map((b)=>b.block)),
1295
- this.store.unwindBlocks(from, blocksToUnwind)
1961
+ this.store.deleteLogs(blocks),
1962
+ this.store.unwindCheckpoints(from, checkpointsToUnwind)
1296
1963
  ]);
1297
1964
  return opResults.every(Boolean);
1298
1965
  }
1299
- getPublishedBlocks(from, limit) {
1300
- return this.store.getPublishedBlocks(from, limit);
1966
+ getCheckpointData(checkpointNumber) {
1967
+ return this.store.getCheckpointData(checkpointNumber);
1968
+ }
1969
+ getRangeOfCheckpoints(from, limit) {
1970
+ return this.store.getRangeOfCheckpoints(from, limit);
1301
1971
  }
1302
- getPublishedBlock(number) {
1303
- return this.store.getPublishedBlock(number);
1972
+ getCheckpointedL2BlockNumber() {
1973
+ return this.store.getCheckpointedL2BlockNumber();
1974
+ }
1975
+ getSynchedCheckpointNumber() {
1976
+ return this.store.getSynchedCheckpointNumber();
1304
1977
  }
1305
- getPublishedBlockByHash(blockHash) {
1306
- return this.store.getPublishedBlockByHash(blockHash);
1978
+ setCheckpointSynchedL1BlockNumber(l1BlockNumber) {
1979
+ return this.store.setCheckpointSynchedL1BlockNumber(l1BlockNumber);
1307
1980
  }
1308
- getPublishedBlockByArchive(archive) {
1309
- return this.store.getPublishedBlockByArchive(archive);
1981
+ getCheckpointedBlock(number) {
1982
+ return this.store.getCheckpointedBlock(number);
1983
+ }
1984
+ getCheckpointedBlockByHash(blockHash) {
1985
+ return this.store.getCheckpointedBlockByHash(blockHash);
1986
+ }
1987
+ getCheckpointedBlockByArchive(archive) {
1988
+ return this.store.getCheckpointedBlockByArchive(archive);
1310
1989
  }
1311
1990
  getBlockHeaders(from, limit) {
1312
1991
  return this.store.getBlockHeaders(from, limit);
@@ -1317,6 +1996,18 @@ var Operation = /*#__PURE__*/ function(Operation) {
1317
1996
  getBlockHeaderByArchive(archive) {
1318
1997
  return this.store.getBlockHeaderByArchive(archive);
1319
1998
  }
1999
+ getBlockByHash(blockHash) {
2000
+ return this.store.getBlockByHash(blockHash);
2001
+ }
2002
+ getBlockByArchive(archive) {
2003
+ return this.store.getBlockByArchive(archive);
2004
+ }
2005
+ getLatestBlockNumber() {
2006
+ return this.store.getLatestBlockNumber();
2007
+ }
2008
+ getBlocksForCheckpoint(checkpointNumber) {
2009
+ return this.store.getBlocksForCheckpoint(checkpointNumber);
2010
+ }
1320
2011
  getTxEffect(txHash) {
1321
2012
  return this.store.getTxEffect(txHash);
1322
2013
  }
@@ -1326,17 +2017,17 @@ var Operation = /*#__PURE__*/ function(Operation) {
1326
2017
  addL1ToL2Messages(messages) {
1327
2018
  return this.store.addL1ToL2Messages(messages);
1328
2019
  }
1329
- getL1ToL2Messages(blockNumber) {
1330
- return this.store.getL1ToL2Messages(blockNumber);
2020
+ getL1ToL2Messages(checkpointNumber) {
2021
+ return this.store.getL1ToL2Messages(checkpointNumber);
1331
2022
  }
1332
2023
  getL1ToL2MessageIndex(l1ToL2Message) {
1333
2024
  return this.store.getL1ToL2MessageIndex(l1ToL2Message);
1334
2025
  }
1335
- getPrivateLogs(from, limit) {
1336
- return this.store.getPrivateLogs(from, limit);
2026
+ getPrivateLogsByTags(tags) {
2027
+ return this.store.getPrivateLogsByTags(tags);
1337
2028
  }
1338
- getLogsByTags(tags, logsPerTag) {
1339
- return this.store.getLogsByTags(tags, logsPerTag);
2029
+ getPublicLogsByTagsFromContract(contractAddress, tags) {
2030
+ return this.store.getPublicLogsByTagsFromContract(contractAddress, tags);
1340
2031
  }
1341
2032
  getPublicLogs(filter) {
1342
2033
  return this.store.getPublicLogs(filter);
@@ -1345,16 +2036,19 @@ var Operation = /*#__PURE__*/ function(Operation) {
1345
2036
  return this.store.getContractClassLogs(filter);
1346
2037
  }
1347
2038
  getSynchedL2BlockNumber() {
1348
- return this.store.getSynchedL2BlockNumber();
2039
+ return this.store.getLatestBlockNumber();
1349
2040
  }
1350
- getProvenL2BlockNumber() {
1351
- return this.store.getProvenL2BlockNumber();
2041
+ getProvenCheckpointNumber() {
2042
+ return this.store.getProvenCheckpointNumber();
1352
2043
  }
1353
- setProvenL2BlockNumber(l2BlockNumber) {
1354
- return this.store.setProvenL2BlockNumber(l2BlockNumber);
2044
+ getProvenBlockNumber() {
2045
+ return this.store.getProvenBlockNumber();
2046
+ }
2047
+ setProvenCheckpointNumber(checkpointNumber) {
2048
+ return this.store.setProvenCheckpointNumber(checkpointNumber);
1355
2049
  }
1356
2050
  setBlockSynchedL1BlockNumber(l1BlockNumber) {
1357
- return this.store.setBlockSynchedL1BlockNumber(l1BlockNumber);
2051
+ return this.store.setCheckpointSynchedL1BlockNumber(l1BlockNumber);
1358
2052
  }
1359
2053
  setMessageSynchedL1Block(l1Block) {
1360
2054
  return this.store.setMessageSynchedL1Block(l1Block);
@@ -1386,8 +2080,8 @@ var Operation = /*#__PURE__*/ function(Operation) {
1386
2080
  estimateSize() {
1387
2081
  return this.store.estimateSize();
1388
2082
  }
1389
- rollbackL1ToL2MessagesToL2Block(targetBlockNumber) {
1390
- return this.store.rollbackL1ToL2MessagesToL2Block(targetBlockNumber);
2083
+ rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber) {
2084
+ return this.store.rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber);
1391
2085
  }
1392
2086
  iterateL1ToL2Messages(range = {}) {
1393
2087
  return this.store.iterateL1ToL2Messages(range);