@aztec/archiver 4.0.0-nightly.20260113 → 4.0.0-nightly.20260115

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 (174) hide show
  1. package/README.md +139 -22
  2. package/dest/archiver.d.ts +134 -0
  3. package/dest/archiver.d.ts.map +1 -0
  4. package/dest/archiver.js +767 -0
  5. package/dest/{archiver/config.d.ts → config.d.ts} +9 -1
  6. package/dest/config.d.ts.map +1 -0
  7. package/dest/{archiver/config.js → config.js} +9 -0
  8. package/dest/{archiver/errors.d.ts → errors.d.ts} +1 -1
  9. package/dest/errors.d.ts.map +1 -0
  10. package/dest/factory.d.ts +5 -6
  11. package/dest/factory.d.ts.map +1 -1
  12. package/dest/factory.js +82 -5
  13. package/dest/index.d.ts +10 -4
  14. package/dest/index.d.ts.map +1 -1
  15. package/dest/index.js +8 -3
  16. package/dest/interfaces.d.ts +9 -0
  17. package/dest/interfaces.d.ts.map +1 -0
  18. package/dest/interfaces.js +3 -0
  19. package/dest/{archiver/l1 → l1}/bin/retrieve-calldata.d.ts +1 -1
  20. package/dest/l1/bin/retrieve-calldata.d.ts.map +1 -0
  21. package/dest/{archiver/l1 → l1}/calldata_retriever.d.ts +2 -2
  22. package/dest/l1/calldata_retriever.d.ts.map +1 -0
  23. package/dest/l1/data_retrieval.d.ts +88 -0
  24. package/dest/l1/data_retrieval.d.ts.map +1 -0
  25. package/dest/{archiver/l1 → l1}/data_retrieval.js +32 -51
  26. package/dest/{archiver/l1 → l1}/debug_tx.d.ts +1 -1
  27. package/dest/l1/debug_tx.d.ts.map +1 -0
  28. package/dest/{archiver/l1 → l1}/spire_proposer.d.ts +1 -1
  29. package/dest/l1/spire_proposer.d.ts.map +1 -0
  30. package/dest/{archiver/l1 → l1}/trace_tx.d.ts +1 -1
  31. package/dest/l1/trace_tx.d.ts.map +1 -0
  32. package/dest/l1/types.d.ts +12 -0
  33. package/dest/l1/types.d.ts.map +1 -0
  34. package/dest/{archiver/l1 → l1}/validate_trace.d.ts +1 -1
  35. package/dest/l1/validate_trace.d.ts.map +1 -0
  36. package/dest/modules/data_source_base.d.ts +83 -0
  37. package/dest/modules/data_source_base.d.ts.map +1 -0
  38. package/dest/modules/data_source_base.js +301 -0
  39. package/dest/modules/data_store_updater.d.ts +46 -0
  40. package/dest/modules/data_store_updater.d.ts.map +1 -0
  41. package/dest/modules/data_store_updater.js +216 -0
  42. package/dest/modules/instrumentation.d.ts +37 -0
  43. package/dest/modules/instrumentation.d.ts.map +1 -0
  44. package/dest/modules/l1_synchronizer.d.ts +67 -0
  45. package/dest/modules/l1_synchronizer.d.ts.map +1 -0
  46. package/dest/modules/l1_synchronizer.js +1064 -0
  47. package/dest/{archiver → modules}/validation.d.ts +1 -1
  48. package/dest/modules/validation.d.ts.map +1 -0
  49. package/dest/{archiver/kv_archiver_store → store}/block_store.d.ts +2 -2
  50. package/dest/store/block_store.d.ts.map +1 -0
  51. package/dest/{archiver/kv_archiver_store → store}/block_store.js +1 -1
  52. package/dest/store/contract_class_store.d.ts +18 -0
  53. package/dest/store/contract_class_store.d.ts.map +1 -0
  54. package/dest/{archiver/kv_archiver_store → store}/contract_class_store.js +1 -1
  55. package/dest/store/contract_instance_store.d.ts +24 -0
  56. package/dest/store/contract_instance_store.d.ts.map +1 -0
  57. package/dest/{archiver/kv_archiver_store → store}/contract_instance_store.js +1 -1
  58. package/dest/{archiver/archiver_store.d.ts → store/kv_archiver_store.d.ts} +143 -139
  59. package/dest/store/kv_archiver_store.d.ts.map +1 -0
  60. package/dest/{archiver/kv_archiver_store → store}/kv_archiver_store.js +157 -49
  61. package/dest/{archiver/kv_archiver_store → store}/log_store.d.ts +1 -1
  62. package/dest/store/log_store.d.ts.map +1 -0
  63. package/dest/{archiver/kv_archiver_store → store}/message_store.d.ts +1 -1
  64. package/dest/store/message_store.d.ts.map +1 -0
  65. package/dest/{archiver/structs → structs}/data_retrieval.d.ts +1 -1
  66. package/dest/structs/data_retrieval.d.ts.map +1 -0
  67. package/dest/structs/inbox_message.d.ts +15 -0
  68. package/dest/structs/inbox_message.d.ts.map +1 -0
  69. package/dest/{archiver/structs → structs}/published.d.ts +1 -1
  70. package/dest/structs/published.d.ts.map +1 -0
  71. package/dest/test/fake_l1_state.d.ts +173 -0
  72. package/dest/test/fake_l1_state.d.ts.map +1 -0
  73. package/dest/test/fake_l1_state.js +364 -0
  74. package/dest/test/index.d.ts +2 -1
  75. package/dest/test/index.d.ts.map +1 -1
  76. package/dest/test/index.js +1 -0
  77. package/dest/test/mock_structs.d.ts +76 -2
  78. package/dest/test/mock_structs.d.ts.map +1 -1
  79. package/dest/test/mock_structs.js +133 -2
  80. package/package.json +15 -17
  81. package/src/archiver.ts +522 -0
  82. package/src/{archiver/config.ts → config.ts} +11 -0
  83. package/src/factory.ts +118 -6
  84. package/src/index.ts +10 -3
  85. package/src/interfaces.ts +9 -0
  86. package/src/{archiver/l1 → l1}/calldata_retriever.ts +1 -1
  87. package/src/{archiver/l1 → l1}/data_retrieval.ts +52 -69
  88. package/src/modules/data_source_base.ts +439 -0
  89. package/src/modules/data_store_updater.ts +318 -0
  90. package/src/modules/l1_synchronizer.ts +870 -0
  91. package/src/{archiver/kv_archiver_store → store}/block_store.ts +1 -1
  92. package/src/{archiver/kv_archiver_store → store}/contract_class_store.ts +1 -1
  93. package/src/{archiver/kv_archiver_store → store}/contract_instance_store.ts +1 -1
  94. package/src/{archiver/kv_archiver_store → store}/kv_archiver_store.ts +170 -8
  95. package/src/test/fake_l1_state.ts +561 -0
  96. package/src/test/index.ts +1 -0
  97. package/src/test/mock_structs.ts +247 -2
  98. package/dest/archiver/archiver.d.ts +0 -307
  99. package/dest/archiver/archiver.d.ts.map +0 -1
  100. package/dest/archiver/archiver.js +0 -2102
  101. package/dest/archiver/archiver_store.d.ts.map +0 -1
  102. package/dest/archiver/archiver_store.js +0 -4
  103. package/dest/archiver/archiver_store_test_suite.d.ts +0 -8
  104. package/dest/archiver/archiver_store_test_suite.d.ts.map +0 -1
  105. package/dest/archiver/archiver_store_test_suite.js +0 -2770
  106. package/dest/archiver/config.d.ts.map +0 -1
  107. package/dest/archiver/errors.d.ts.map +0 -1
  108. package/dest/archiver/index.d.ts +0 -7
  109. package/dest/archiver/index.d.ts.map +0 -1
  110. package/dest/archiver/index.js +0 -4
  111. package/dest/archiver/instrumentation.d.ts +0 -37
  112. package/dest/archiver/instrumentation.d.ts.map +0 -1
  113. package/dest/archiver/kv_archiver_store/block_store.d.ts.map +0 -1
  114. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts +0 -18
  115. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts.map +0 -1
  116. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts +0 -24
  117. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +0 -1
  118. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +0 -159
  119. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +0 -1
  120. package/dest/archiver/kv_archiver_store/log_store.d.ts.map +0 -1
  121. package/dest/archiver/kv_archiver_store/message_store.d.ts.map +0 -1
  122. package/dest/archiver/l1/bin/retrieve-calldata.d.ts.map +0 -1
  123. package/dest/archiver/l1/calldata_retriever.d.ts.map +0 -1
  124. package/dest/archiver/l1/data_retrieval.d.ts +0 -90
  125. package/dest/archiver/l1/data_retrieval.d.ts.map +0 -1
  126. package/dest/archiver/l1/debug_tx.d.ts.map +0 -1
  127. package/dest/archiver/l1/spire_proposer.d.ts.map +0 -1
  128. package/dest/archiver/l1/trace_tx.d.ts.map +0 -1
  129. package/dest/archiver/l1/types.d.ts +0 -12
  130. package/dest/archiver/l1/types.d.ts.map +0 -1
  131. package/dest/archiver/l1/validate_trace.d.ts.map +0 -1
  132. package/dest/archiver/structs/data_retrieval.d.ts.map +0 -1
  133. package/dest/archiver/structs/inbox_message.d.ts +0 -15
  134. package/dest/archiver/structs/inbox_message.d.ts.map +0 -1
  135. package/dest/archiver/structs/published.d.ts.map +0 -1
  136. package/dest/archiver/validation.d.ts.map +0 -1
  137. package/dest/rpc/index.d.ts +0 -9
  138. package/dest/rpc/index.d.ts.map +0 -1
  139. package/dest/rpc/index.js +0 -15
  140. package/src/archiver/archiver.ts +0 -2265
  141. package/src/archiver/archiver_store.ts +0 -380
  142. package/src/archiver/archiver_store_test_suite.ts +0 -2842
  143. package/src/archiver/index.ts +0 -6
  144. package/src/rpc/index.ts +0 -16
  145. /package/dest/{archiver/errors.js → errors.js} +0 -0
  146. /package/dest/{archiver/l1 → l1}/bin/retrieve-calldata.js +0 -0
  147. /package/dest/{archiver/l1 → l1}/calldata_retriever.js +0 -0
  148. /package/dest/{archiver/l1 → l1}/debug_tx.js +0 -0
  149. /package/dest/{archiver/l1 → l1}/spire_proposer.js +0 -0
  150. /package/dest/{archiver/l1 → l1}/trace_tx.js +0 -0
  151. /package/dest/{archiver/l1 → l1}/types.js +0 -0
  152. /package/dest/{archiver/l1 → l1}/validate_trace.js +0 -0
  153. /package/dest/{archiver → modules}/instrumentation.js +0 -0
  154. /package/dest/{archiver → modules}/validation.js +0 -0
  155. /package/dest/{archiver/kv_archiver_store → store}/log_store.js +0 -0
  156. /package/dest/{archiver/kv_archiver_store → store}/message_store.js +0 -0
  157. /package/dest/{archiver/structs → structs}/data_retrieval.js +0 -0
  158. /package/dest/{archiver/structs → structs}/inbox_message.js +0 -0
  159. /package/dest/{archiver/structs → structs}/published.js +0 -0
  160. /package/src/{archiver/errors.ts → errors.ts} +0 -0
  161. /package/src/{archiver/l1 → l1}/README.md +0 -0
  162. /package/src/{archiver/l1 → l1}/bin/retrieve-calldata.ts +0 -0
  163. /package/src/{archiver/l1 → l1}/debug_tx.ts +0 -0
  164. /package/src/{archiver/l1 → l1}/spire_proposer.ts +0 -0
  165. /package/src/{archiver/l1 → l1}/trace_tx.ts +0 -0
  166. /package/src/{archiver/l1 → l1}/types.ts +0 -0
  167. /package/src/{archiver/l1 → l1}/validate_trace.ts +0 -0
  168. /package/src/{archiver → modules}/instrumentation.ts +0 -0
  169. /package/src/{archiver → modules}/validation.ts +0 -0
  170. /package/src/{archiver/kv_archiver_store → store}/log_store.ts +0 -0
  171. /package/src/{archiver/kv_archiver_store → store}/message_store.ts +0 -0
  172. /package/src/{archiver/structs → structs}/data_retrieval.ts +0 -0
  173. /package/src/{archiver/structs → structs}/inbox_message.ts +0 -0
  174. /package/src/{archiver/structs → structs}/published.ts +0 -0
@@ -0,0 +1,1064 @@
1
+ function applyDecs2203RFactory() {
2
+ function createAddInitializerMethod(initializers, decoratorFinishedRef) {
3
+ return function addInitializer(initializer) {
4
+ assertNotFinished(decoratorFinishedRef, "addInitializer");
5
+ assertCallable(initializer, "An initializer");
6
+ initializers.push(initializer);
7
+ };
8
+ }
9
+ function memberDec(dec, name, desc, initializers, kind, isStatic, isPrivate, metadata, value) {
10
+ var kindStr;
11
+ switch(kind){
12
+ case 1:
13
+ kindStr = "accessor";
14
+ break;
15
+ case 2:
16
+ kindStr = "method";
17
+ break;
18
+ case 3:
19
+ kindStr = "getter";
20
+ break;
21
+ case 4:
22
+ kindStr = "setter";
23
+ break;
24
+ default:
25
+ kindStr = "field";
26
+ }
27
+ var ctx = {
28
+ kind: kindStr,
29
+ name: isPrivate ? "#" + name : name,
30
+ static: isStatic,
31
+ private: isPrivate,
32
+ metadata: metadata
33
+ };
34
+ var decoratorFinishedRef = {
35
+ v: false
36
+ };
37
+ ctx.addInitializer = createAddInitializerMethod(initializers, decoratorFinishedRef);
38
+ var get, set;
39
+ if (kind === 0) {
40
+ if (isPrivate) {
41
+ get = desc.get;
42
+ set = desc.set;
43
+ } else {
44
+ get = function() {
45
+ return this[name];
46
+ };
47
+ set = function(v) {
48
+ this[name] = v;
49
+ };
50
+ }
51
+ } else if (kind === 2) {
52
+ get = function() {
53
+ return desc.value;
54
+ };
55
+ } else {
56
+ if (kind === 1 || kind === 3) {
57
+ get = function() {
58
+ return desc.get.call(this);
59
+ };
60
+ }
61
+ if (kind === 1 || kind === 4) {
62
+ set = function(v) {
63
+ desc.set.call(this, v);
64
+ };
65
+ }
66
+ }
67
+ ctx.access = get && set ? {
68
+ get: get,
69
+ set: set
70
+ } : get ? {
71
+ get: get
72
+ } : {
73
+ set: set
74
+ };
75
+ try {
76
+ return dec(value, ctx);
77
+ } finally{
78
+ decoratorFinishedRef.v = true;
79
+ }
80
+ }
81
+ function assertNotFinished(decoratorFinishedRef, fnName) {
82
+ if (decoratorFinishedRef.v) {
83
+ throw new Error("attempted to call " + fnName + " after decoration was finished");
84
+ }
85
+ }
86
+ function assertCallable(fn, hint) {
87
+ if (typeof fn !== "function") {
88
+ throw new TypeError(hint + " must be a function");
89
+ }
90
+ }
91
+ function assertValidReturnValue(kind, value) {
92
+ var type = typeof value;
93
+ if (kind === 1) {
94
+ if (type !== "object" || value === null) {
95
+ throw new TypeError("accessor decorators must return an object with get, set, or init properties or void 0");
96
+ }
97
+ if (value.get !== undefined) {
98
+ assertCallable(value.get, "accessor.get");
99
+ }
100
+ if (value.set !== undefined) {
101
+ assertCallable(value.set, "accessor.set");
102
+ }
103
+ if (value.init !== undefined) {
104
+ assertCallable(value.init, "accessor.init");
105
+ }
106
+ } else if (type !== "function") {
107
+ var hint;
108
+ if (kind === 0) {
109
+ hint = "field";
110
+ } else if (kind === 10) {
111
+ hint = "class";
112
+ } else {
113
+ hint = "method";
114
+ }
115
+ throw new TypeError(hint + " decorators must return a function or void 0");
116
+ }
117
+ }
118
+ function applyMemberDec(ret, base, decInfo, name, kind, isStatic, isPrivate, initializers, metadata) {
119
+ var decs = decInfo[0];
120
+ var desc, init, value;
121
+ if (isPrivate) {
122
+ if (kind === 0 || kind === 1) {
123
+ desc = {
124
+ get: decInfo[3],
125
+ set: decInfo[4]
126
+ };
127
+ } else if (kind === 3) {
128
+ desc = {
129
+ get: decInfo[3]
130
+ };
131
+ } else if (kind === 4) {
132
+ desc = {
133
+ set: decInfo[3]
134
+ };
135
+ } else {
136
+ desc = {
137
+ value: decInfo[3]
138
+ };
139
+ }
140
+ } else if (kind !== 0) {
141
+ desc = Object.getOwnPropertyDescriptor(base, name);
142
+ }
143
+ if (kind === 1) {
144
+ value = {
145
+ get: desc.get,
146
+ set: desc.set
147
+ };
148
+ } else if (kind === 2) {
149
+ value = desc.value;
150
+ } else if (kind === 3) {
151
+ value = desc.get;
152
+ } else if (kind === 4) {
153
+ value = desc.set;
154
+ }
155
+ var newValue, get, set;
156
+ if (typeof decs === "function") {
157
+ newValue = memberDec(decs, name, desc, initializers, kind, isStatic, isPrivate, metadata, value);
158
+ if (newValue !== void 0) {
159
+ assertValidReturnValue(kind, newValue);
160
+ if (kind === 0) {
161
+ init = newValue;
162
+ } else if (kind === 1) {
163
+ init = newValue.init;
164
+ get = newValue.get || value.get;
165
+ set = newValue.set || value.set;
166
+ value = {
167
+ get: get,
168
+ set: set
169
+ };
170
+ } else {
171
+ value = newValue;
172
+ }
173
+ }
174
+ } else {
175
+ for(var i = decs.length - 1; i >= 0; i--){
176
+ var dec = decs[i];
177
+ newValue = memberDec(dec, name, desc, initializers, kind, isStatic, isPrivate, metadata, value);
178
+ if (newValue !== void 0) {
179
+ assertValidReturnValue(kind, newValue);
180
+ var newInit;
181
+ if (kind === 0) {
182
+ newInit = newValue;
183
+ } else if (kind === 1) {
184
+ newInit = newValue.init;
185
+ get = newValue.get || value.get;
186
+ set = newValue.set || value.set;
187
+ value = {
188
+ get: get,
189
+ set: set
190
+ };
191
+ } else {
192
+ value = newValue;
193
+ }
194
+ if (newInit !== void 0) {
195
+ if (init === void 0) {
196
+ init = newInit;
197
+ } else if (typeof init === "function") {
198
+ init = [
199
+ init,
200
+ newInit
201
+ ];
202
+ } else {
203
+ init.push(newInit);
204
+ }
205
+ }
206
+ }
207
+ }
208
+ }
209
+ if (kind === 0 || kind === 1) {
210
+ if (init === void 0) {
211
+ init = function(instance, init) {
212
+ return init;
213
+ };
214
+ } else if (typeof init !== "function") {
215
+ var ownInitializers = init;
216
+ init = function(instance, init) {
217
+ var value = init;
218
+ for(var i = 0; i < ownInitializers.length; i++){
219
+ value = ownInitializers[i].call(instance, value);
220
+ }
221
+ return value;
222
+ };
223
+ } else {
224
+ var originalInitializer = init;
225
+ init = function(instance, init) {
226
+ return originalInitializer.call(instance, init);
227
+ };
228
+ }
229
+ ret.push(init);
230
+ }
231
+ if (kind !== 0) {
232
+ if (kind === 1) {
233
+ desc.get = value.get;
234
+ desc.set = value.set;
235
+ } else if (kind === 2) {
236
+ desc.value = value;
237
+ } else if (kind === 3) {
238
+ desc.get = value;
239
+ } else if (kind === 4) {
240
+ desc.set = value;
241
+ }
242
+ if (isPrivate) {
243
+ if (kind === 1) {
244
+ ret.push(function(instance, args) {
245
+ return value.get.call(instance, args);
246
+ });
247
+ ret.push(function(instance, args) {
248
+ return value.set.call(instance, args);
249
+ });
250
+ } else if (kind === 2) {
251
+ ret.push(value);
252
+ } else {
253
+ ret.push(function(instance, args) {
254
+ return value.call(instance, args);
255
+ });
256
+ }
257
+ } else {
258
+ Object.defineProperty(base, name, desc);
259
+ }
260
+ }
261
+ }
262
+ function applyMemberDecs(Class, decInfos, metadata) {
263
+ var ret = [];
264
+ var protoInitializers;
265
+ var staticInitializers;
266
+ var existingProtoNonFields = new Map();
267
+ var existingStaticNonFields = new Map();
268
+ for(var i = 0; i < decInfos.length; i++){
269
+ var decInfo = decInfos[i];
270
+ if (!Array.isArray(decInfo)) continue;
271
+ var kind = decInfo[1];
272
+ var name = decInfo[2];
273
+ var isPrivate = decInfo.length > 3;
274
+ var isStatic = kind >= 5;
275
+ var base;
276
+ var initializers;
277
+ if (isStatic) {
278
+ base = Class;
279
+ kind = kind - 5;
280
+ staticInitializers = staticInitializers || [];
281
+ initializers = staticInitializers;
282
+ } else {
283
+ base = Class.prototype;
284
+ protoInitializers = protoInitializers || [];
285
+ initializers = protoInitializers;
286
+ }
287
+ if (kind !== 0 && !isPrivate) {
288
+ var existingNonFields = isStatic ? existingStaticNonFields : existingProtoNonFields;
289
+ var existingKind = existingNonFields.get(name) || 0;
290
+ if (existingKind === true || existingKind === 3 && kind !== 4 || existingKind === 4 && kind !== 3) {
291
+ throw new Error("Attempted to decorate a public method/accessor that has the same name as a previously decorated public method/accessor. This is not currently supported by the decorators plugin. Property name was: " + name);
292
+ } else if (!existingKind && kind > 2) {
293
+ existingNonFields.set(name, kind);
294
+ } else {
295
+ existingNonFields.set(name, true);
296
+ }
297
+ }
298
+ applyMemberDec(ret, base, decInfo, name, kind, isStatic, isPrivate, initializers, metadata);
299
+ }
300
+ pushInitializers(ret, protoInitializers);
301
+ pushInitializers(ret, staticInitializers);
302
+ return ret;
303
+ }
304
+ function pushInitializers(ret, initializers) {
305
+ if (initializers) {
306
+ ret.push(function(instance) {
307
+ for(var i = 0; i < initializers.length; i++){
308
+ initializers[i].call(instance);
309
+ }
310
+ return instance;
311
+ });
312
+ }
313
+ }
314
+ function applyClassDecs(targetClass, classDecs, metadata) {
315
+ if (classDecs.length > 0) {
316
+ var initializers = [];
317
+ var newClass = targetClass;
318
+ var name = targetClass.name;
319
+ for(var i = classDecs.length - 1; i >= 0; i--){
320
+ var decoratorFinishedRef = {
321
+ v: false
322
+ };
323
+ try {
324
+ var nextNewClass = classDecs[i](newClass, {
325
+ kind: "class",
326
+ name: name,
327
+ addInitializer: createAddInitializerMethod(initializers, decoratorFinishedRef),
328
+ metadata
329
+ });
330
+ } finally{
331
+ decoratorFinishedRef.v = true;
332
+ }
333
+ if (nextNewClass !== undefined) {
334
+ assertValidReturnValue(10, nextNewClass);
335
+ newClass = nextNewClass;
336
+ }
337
+ }
338
+ return [
339
+ defineMetadata(newClass, metadata),
340
+ function() {
341
+ for(var i = 0; i < initializers.length; i++){
342
+ initializers[i].call(newClass);
343
+ }
344
+ }
345
+ ];
346
+ }
347
+ }
348
+ function defineMetadata(Class, metadata) {
349
+ return Object.defineProperty(Class, Symbol.metadata || Symbol.for("Symbol.metadata"), {
350
+ configurable: true,
351
+ enumerable: true,
352
+ value: metadata
353
+ });
354
+ }
355
+ return function applyDecs2203R(targetClass, memberDecs, classDecs, parentClass) {
356
+ if (parentClass !== void 0) {
357
+ var parentMetadata = parentClass[Symbol.metadata || Symbol.for("Symbol.metadata")];
358
+ }
359
+ var metadata = Object.create(parentMetadata === void 0 ? null : parentMetadata);
360
+ var e = applyMemberDecs(targetClass, memberDecs, metadata);
361
+ if (!classDecs.length) defineMetadata(targetClass, metadata);
362
+ return {
363
+ e: e,
364
+ get c () {
365
+ return applyClassDecs(targetClass, classDecs, metadata);
366
+ }
367
+ };
368
+ };
369
+ }
370
+ function _apply_decs_2203_r(targetClass, memberDecs, classDecs, parentClass) {
371
+ return (_apply_decs_2203_r = applyDecs2203RFactory())(targetClass, memberDecs, classDecs, parentClass);
372
+ }
373
+ var _dec, _dec1, _dec2, _dec3, _initProto;
374
+ import { maxBigint } from '@aztec/foundation/bigint';
375
+ import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
376
+ import { Buffer32 } from '@aztec/foundation/buffer';
377
+ import { pick } from '@aztec/foundation/collection';
378
+ import { createLogger } from '@aztec/foundation/log';
379
+ import { count } from '@aztec/foundation/string';
380
+ import { Timer, elapsed } from '@aztec/foundation/timer';
381
+ import { isDefined } from '@aztec/foundation/types';
382
+ import { L2BlockSourceEvents } from '@aztec/stdlib/block';
383
+ import { getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
384
+ import { computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
385
+ import { execInSpan, trackSpan } from '@aztec/telemetry-client';
386
+ import { InitialCheckpointNumberNotSequentialError } from '../errors.js';
387
+ import { retrieveCheckpointsFromRollup, retrieveL1ToL2Message, retrieveL1ToL2Messages, retrievedToPublishedCheckpoint } from '../l1/data_retrieval.js';
388
+ import { ArchiverDataStoreUpdater } from './data_store_updater.js';
389
+ import { validateCheckpointAttestations } from './validation.js';
390
+ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpochPrune'), _dec2 = trackSpan('Archiver.handleL1ToL2Messages'), _dec3 = trackSpan('Archiver.handleCheckpoints');
391
+ /**
392
+ * Handles L1 synchronization for the archiver.
393
+ * Responsible for fetching checkpoints, L1→L2 messages, and handling L1 reorgs.
394
+ */ export class ArchiverL1Synchronizer {
395
+ publicClient;
396
+ debugClient;
397
+ rollup;
398
+ inbox;
399
+ l1Addresses;
400
+ store;
401
+ config;
402
+ blobClient;
403
+ epochCache;
404
+ dateProvider;
405
+ instrumentation;
406
+ l1constants;
407
+ events;
408
+ log;
409
+ static{
410
+ ({ e: [_initProto] } = _apply_decs_2203_r(this, [
411
+ [
412
+ _dec,
413
+ 2,
414
+ "syncFromL1"
415
+ ],
416
+ [
417
+ _dec1,
418
+ 2,
419
+ "handleEpochPrune"
420
+ ],
421
+ [
422
+ _dec2,
423
+ 2,
424
+ "handleL1ToL2Messages"
425
+ ],
426
+ [
427
+ _dec3,
428
+ 2,
429
+ "handleCheckpoints"
430
+ ]
431
+ ], []));
432
+ }
433
+ l1BlockNumber;
434
+ l1Timestamp;
435
+ updater;
436
+ tracer;
437
+ constructor(publicClient, debugClient, rollup, inbox, l1Addresses, store, config, blobClient, epochCache, dateProvider, instrumentation, l1constants, events, tracer, log = createLogger('archiver:l1-sync')){
438
+ this.publicClient = publicClient;
439
+ this.debugClient = debugClient;
440
+ this.rollup = rollup;
441
+ this.inbox = inbox;
442
+ this.l1Addresses = l1Addresses;
443
+ this.store = store;
444
+ this.config = config;
445
+ this.blobClient = blobClient;
446
+ this.epochCache = epochCache;
447
+ this.dateProvider = dateProvider;
448
+ this.instrumentation = instrumentation;
449
+ this.l1constants = l1constants;
450
+ this.events = events;
451
+ this.log = log;
452
+ _initProto(this);
453
+ this.updater = new ArchiverDataStoreUpdater(this.store);
454
+ this.tracer = tracer;
455
+ }
456
+ /** Returns the last L1 block number that was synced. */ getL1BlockNumber() {
457
+ return this.l1BlockNumber;
458
+ }
459
+ /** Returns the last L1 timestamp that was synced. */ getL1Timestamp() {
460
+ return this.l1Timestamp;
461
+ }
462
+ /** Checks that the ethereum node we are connected to has a latest timestamp no more than the allowed drift. Throw if not. */ async testEthereumNodeSynced() {
463
+ const maxAllowedDelay = this.config.maxAllowedEthClientDriftSeconds;
464
+ if (maxAllowedDelay === 0) {
465
+ return;
466
+ }
467
+ const { number, timestamp: l1Timestamp } = await this.publicClient.getBlock({
468
+ includeTransactions: false
469
+ });
470
+ const currentTime = BigInt(this.dateProvider.nowInSeconds());
471
+ if (currentTime - l1Timestamp > BigInt(maxAllowedDelay)) {
472
+ throw new Error(`Ethereum node is out of sync (last block synced ${number} at ${l1Timestamp} vs current time ${currentTime})`);
473
+ }
474
+ }
475
+ async syncFromL1(initialSyncComplete) {
476
+ /**
477
+ * We keep track of three "pointers" to L1 blocks:
478
+ * 1. the last L1 block that published an L2 block
479
+ * 2. the last L1 block that added L1 to L2 messages
480
+ * 3. the last L1 block that cancelled L1 to L2 messages
481
+ *
482
+ * We do this to deal with L1 data providers that are eventually consistent (e.g. Infura).
483
+ * We guard against seeing block X with no data at one point, and later, the provider processes the block and it has data.
484
+ * The archiver will stay back, until there's data on L1 that will move the pointers forward.
485
+ */ const { l1StartBlock, l1StartBlockHash } = this.l1constants;
486
+ const { blocksSynchedTo = l1StartBlock, messagesSynchedTo = {
487
+ l1BlockNumber: l1StartBlock,
488
+ l1BlockHash: l1StartBlockHash
489
+ } } = await this.store.getSynchPoint();
490
+ const currentL1Block = await this.publicClient.getBlock({
491
+ includeTransactions: false
492
+ });
493
+ const currentL1BlockNumber = currentL1Block.number;
494
+ const currentL1BlockHash = Buffer32.fromString(currentL1Block.hash);
495
+ this.log.trace(`Starting new archiver sync iteration`, {
496
+ blocksSynchedTo,
497
+ messagesSynchedTo,
498
+ currentL1BlockNumber,
499
+ currentL1BlockHash
500
+ });
501
+ // ********** Ensuring Consistency of data pulled from L1 **********
502
+ /**
503
+ * There are a number of calls in this sync operation to L1 for retrieving
504
+ * events and transaction data. There are a couple of things we need to bear in mind
505
+ * to ensure that data is read exactly once.
506
+ *
507
+ * The first is the problem of eventually consistent ETH service providers like Infura.
508
+ * Each L1 read operation will query data from the last L1 block that it saw emit its kind of data.
509
+ * (so pending L1 to L2 messages will read from the last L1 block that emitted a message and so on)
510
+ * This will mean the archiver will lag behind L1 and will only advance when there's L2-relevant activity on the chain.
511
+ *
512
+ * The second is that in between the various calls to L1, the block number can move meaning some
513
+ * of the following calls will return data for blocks that were not present during earlier calls.
514
+ * To combat this for the time being we simply ensure that all data retrieval methods only retrieve
515
+ * data up to the currentBlockNumber captured at the top of this function. We might want to improve on this
516
+ * in future but for the time being it should give us the guarantees that we need
517
+ */ // ********** Events that are processed per L1 block **********
518
+ await this.handleL1ToL2Messages(messagesSynchedTo, currentL1BlockNumber, currentL1BlockHash);
519
+ // Get L1 timestamp for the current block
520
+ const currentL1Timestamp = !this.l1Timestamp || !this.l1BlockNumber || this.l1BlockNumber !== currentL1BlockNumber ? (await this.publicClient.getBlock({
521
+ blockNumber: currentL1BlockNumber
522
+ })).timestamp : this.l1Timestamp;
523
+ // Warn if the latest L1 block timestamp is too old
524
+ const maxAllowedDelay = this.config.maxAllowedEthClientDriftSeconds;
525
+ const now = this.dateProvider.nowInSeconds();
526
+ if (maxAllowedDelay > 0 && Number(currentL1Timestamp) <= now - maxAllowedDelay) {
527
+ this.log.warn(`Latest L1 block ${currentL1BlockNumber} timestamp ${currentL1Timestamp} is too old. Make sure your Ethereum node is synced.`, {
528
+ currentL1BlockNumber,
529
+ currentL1Timestamp,
530
+ now,
531
+ maxAllowedDelay
532
+ });
533
+ }
534
+ // ********** Events that are processed per checkpoint **********
535
+ if (currentL1BlockNumber > blocksSynchedTo) {
536
+ // First we retrieve new checkpoints and L2 blocks and store them in the DB. This will also update the
537
+ // pending chain validation status, proven checkpoint number, and synched L1 block number.
538
+ const rollupStatus = await this.handleCheckpoints(blocksSynchedTo, currentL1BlockNumber, initialSyncComplete);
539
+ // Then we prune the current epoch if it'd reorg on next submission.
540
+ // Note that we don't do this before retrieving checkpoints because we may need to retrieve
541
+ // checkpoints from more than 2 epochs ago, so we want to make sure we have the latest view of
542
+ // the chain locally before we start unwinding stuff. This can be optimized by figuring out
543
+ // up to which point we're pruning, and then requesting checkpoints up to that point only.
544
+ const { rollupCanPrune } = await this.handleEpochPrune(rollupStatus.provenCheckpointNumber, currentL1BlockNumber, currentL1Timestamp);
545
+ // If the last checkpoint we processed had an invalid attestation, we manually advance the L1 syncpoint
546
+ // past it, since otherwise we'll keep downloading it and reprocessing it on every iteration until
547
+ // we get a valid checkpoint to advance the syncpoint.
548
+ if (!rollupStatus.validationResult?.valid && rollupStatus.lastL1BlockWithCheckpoint !== undefined) {
549
+ await this.store.setCheckpointSynchedL1BlockNumber(rollupStatus.lastL1BlockWithCheckpoint);
550
+ }
551
+ // And lastly we check if we are missing any checkpoints behind us due to a possible L1 reorg.
552
+ // We only do this if rollup cant prune on the next submission. Otherwise we will end up
553
+ // re-syncing the checkpoints we have just unwound above. We also dont do this if the last checkpoint is invalid,
554
+ // since the archiver will rightfully refuse to sync up to it.
555
+ if (!rollupCanPrune && rollupStatus.validationResult?.valid) {
556
+ await this.checkForNewCheckpointsBeforeL1SyncPoint(rollupStatus, blocksSynchedTo, currentL1BlockNumber);
557
+ }
558
+ this.instrumentation.updateL1BlockHeight(currentL1BlockNumber);
559
+ }
560
+ // After syncing has completed, update the current l1 block number and timestamp,
561
+ // otherwise we risk announcing to the world that we've synced to a given point,
562
+ // but the corresponding blocks have not been processed (see #12631).
563
+ this.l1Timestamp = currentL1Timestamp;
564
+ this.l1BlockNumber = currentL1BlockNumber;
565
+ const l1BlockNumberAtEnd = await this.publicClient.getBlockNumber();
566
+ this.log.trace(`Archiver sync iteration complete`, {
567
+ l1BlockNumberAtStart: currentL1BlockNumber,
568
+ l1TimestampAtStart: currentL1Timestamp,
569
+ l1BlockNumberAtEnd
570
+ });
571
+ }
572
+ /** Queries the rollup contract on whether a prune can be executed on the immediate next L1 block. */ async canPrune(currentL1BlockNumber, currentL1Timestamp) {
573
+ const time = (currentL1Timestamp ?? 0n) + BigInt(this.l1constants.ethereumSlotDuration);
574
+ const result = await this.rollup.canPruneAtTime(time, {
575
+ blockNumber: currentL1BlockNumber
576
+ });
577
+ if (result) {
578
+ this.log.debug(`Rollup contract allows pruning at L1 block ${currentL1BlockNumber} time ${time}`, {
579
+ currentL1Timestamp,
580
+ pruneTime: time,
581
+ currentL1BlockNumber
582
+ });
583
+ }
584
+ return result;
585
+ }
586
+ /** Checks if there'd be a reorg for the next checkpoint submission and start pruning now. */ async handleEpochPrune(provenCheckpointNumber, currentL1BlockNumber, currentL1Timestamp) {
587
+ const rollupCanPrune = await this.canPrune(currentL1BlockNumber, currentL1Timestamp);
588
+ const localPendingCheckpointNumber = await this.store.getSynchedCheckpointNumber();
589
+ const canPrune = localPendingCheckpointNumber > provenCheckpointNumber && rollupCanPrune;
590
+ if (canPrune) {
591
+ const timer = new Timer();
592
+ const pruneFrom = CheckpointNumber(provenCheckpointNumber + 1);
593
+ const header = await this.getCheckpointHeader(pruneFrom);
594
+ if (header === undefined) {
595
+ throw new Error(`Missing checkpoint header ${pruneFrom}`);
596
+ }
597
+ const pruneFromSlotNumber = header.slotNumber;
598
+ const pruneFromEpochNumber = getEpochAtSlot(pruneFromSlotNumber, this.l1constants);
599
+ const checkpointsToUnwind = localPendingCheckpointNumber - provenCheckpointNumber;
600
+ const checkpointPromises = Array.from({
601
+ length: checkpointsToUnwind
602
+ }).fill(0).map((_, i)=>this.store.getCheckpointData(CheckpointNumber(i + pruneFrom)));
603
+ const checkpoints = await Promise.all(checkpointPromises);
604
+ const blockPromises = await Promise.all(checkpoints.filter(isDefined).map((cp)=>this.store.getBlocksForCheckpoint(CheckpointNumber(cp.checkpointNumber))));
605
+ const newBlocks = blockPromises.filter(isDefined).flat();
606
+ // Emit an event for listening services to react to the chain prune
607
+ this.events.emit(L2BlockSourceEvents.L2PruneDetected, {
608
+ type: L2BlockSourceEvents.L2PruneDetected,
609
+ epochNumber: pruneFromEpochNumber,
610
+ blocks: newBlocks
611
+ });
612
+ this.log.debug(`L2 prune from ${provenCheckpointNumber + 1} to ${localPendingCheckpointNumber} will occur on next checkpoint submission.`);
613
+ await this.updater.unwindCheckpointsWithContractData(localPendingCheckpointNumber, checkpointsToUnwind);
614
+ this.log.warn(`Unwound ${count(checkpointsToUnwind, 'checkpoint')} from checkpoint ${localPendingCheckpointNumber} ` + `to ${provenCheckpointNumber} due to predicted reorg at L1 block ${currentL1BlockNumber}. ` + `Updated latest checkpoint is ${await this.store.getSynchedCheckpointNumber()}.`);
615
+ this.instrumentation.processPrune(timer.ms());
616
+ // TODO(palla/reorg): Do we need to set the block synched L1 block number here?
617
+ // Seems like the next iteration should handle this.
618
+ // await this.store.setCheckpointSynchedL1BlockNumber(currentL1BlockNumber);
619
+ }
620
+ return {
621
+ rollupCanPrune
622
+ };
623
+ }
624
+ nextRange(end, limit) {
625
+ const batchSize = this.config.batchSize * this.l1constants.slotDuration / this.l1constants.ethereumSlotDuration;
626
+ const nextStart = end + 1n;
627
+ const nextEnd = nextStart + BigInt(batchSize);
628
+ if (nextEnd > limit) {
629
+ return [
630
+ nextStart,
631
+ limit
632
+ ];
633
+ }
634
+ return [
635
+ nextStart,
636
+ nextEnd
637
+ ];
638
+ }
639
+ async handleL1ToL2Messages(messagesSyncPoint, currentL1BlockNumber, _currentL1BlockHash) {
640
+ this.log.trace(`Handling L1 to L2 messages from ${messagesSyncPoint.l1BlockNumber} to ${currentL1BlockNumber}.`);
641
+ if (currentL1BlockNumber <= messagesSyncPoint.l1BlockNumber) {
642
+ return;
643
+ }
644
+ // Load remote and local inbox states.
645
+ const localMessagesInserted = await this.store.getTotalL1ToL2MessageCount();
646
+ const localLastMessage = await this.store.getLastL1ToL2Message();
647
+ const remoteMessagesState = await this.inbox.getState({
648
+ blockNumber: currentL1BlockNumber
649
+ });
650
+ this.log.trace(`Retrieved remote inbox state at L1 block ${currentL1BlockNumber}.`, {
651
+ localMessagesInserted,
652
+ localLastMessage,
653
+ remoteMessagesState
654
+ });
655
+ // Compare message count and rolling hash. If they match, no need to retrieve anything.
656
+ if (remoteMessagesState.totalMessagesInserted === localMessagesInserted && remoteMessagesState.messagesRollingHash.equals(localLastMessage?.rollingHash ?? Buffer32.ZERO)) {
657
+ this.log.trace(`No L1 to L2 messages to query between L1 blocks ${messagesSyncPoint.l1BlockNumber} and ${currentL1BlockNumber}.`);
658
+ return;
659
+ }
660
+ // Check if our syncpoint is still valid. If not, there was an L1 reorg and we need to re-retrieve messages.
661
+ // Note that we need to fetch it from logs and not from inbox state at the syncpoint l1 block number, since it
662
+ // could be older than 128 blocks and non-archive nodes cannot resolve it.
663
+ if (localLastMessage) {
664
+ const remoteLastMessage = await this.retrieveL1ToL2Message(localLastMessage.leaf);
665
+ this.log.trace(`Retrieved remote message for local last`, {
666
+ remoteLastMessage,
667
+ localLastMessage
668
+ });
669
+ if (!remoteLastMessage || !remoteLastMessage.rollingHash.equals(localLastMessage.rollingHash)) {
670
+ this.log.warn(`Rolling back L1 to L2 messages due to hash mismatch or msg not found.`, {
671
+ remoteLastMessage,
672
+ messagesSyncPoint,
673
+ localLastMessage
674
+ });
675
+ messagesSyncPoint = await this.rollbackL1ToL2Messages(localLastMessage, messagesSyncPoint);
676
+ this.log.debug(`Rolled back L1 to L2 messages to L1 block ${messagesSyncPoint.l1BlockNumber}.`, {
677
+ messagesSyncPoint
678
+ });
679
+ }
680
+ }
681
+ // Retrieve and save messages in batches. Each batch is estimated to acommodate up to L2 'blockBatchSize' blocks,
682
+ let searchStartBlock = 0n;
683
+ let searchEndBlock = messagesSyncPoint.l1BlockNumber;
684
+ let lastMessage;
685
+ let messageCount = 0;
686
+ do {
687
+ [searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, currentL1BlockNumber);
688
+ this.log.trace(`Retrieving L1 to L2 messages between L1 blocks ${searchStartBlock} and ${searchEndBlock}.`);
689
+ const messages = await retrieveL1ToL2Messages(this.inbox, searchStartBlock, searchEndBlock);
690
+ this.log.verbose(`Retrieved ${messages.length} new L1 to L2 messages between L1 blocks ${searchStartBlock} and ${searchEndBlock}.`);
691
+ const timer = new Timer();
692
+ await this.store.addL1ToL2Messages(messages);
693
+ const perMsg = timer.ms() / messages.length;
694
+ this.instrumentation.processNewMessages(messages.length, perMsg);
695
+ for (const msg of messages){
696
+ this.log.debug(`Downloaded L1 to L2 message`, {
697
+ ...msg,
698
+ leaf: msg.leaf.toString()
699
+ });
700
+ lastMessage = msg;
701
+ messageCount++;
702
+ }
703
+ }while (searchEndBlock < currentL1BlockNumber)
704
+ // Log stats for messages retrieved (if any).
705
+ if (messageCount > 0) {
706
+ this.log.info(`Retrieved ${messageCount} new L1 to L2 messages up to message with index ${lastMessage?.index} for checkpoint ${lastMessage?.checkpointNumber}`, {
707
+ lastMessage,
708
+ messageCount
709
+ });
710
+ }
711
+ // Warn if the resulting rolling hash does not match the remote state we had retrieved.
712
+ if (lastMessage && !lastMessage.rollingHash.equals(remoteMessagesState.messagesRollingHash)) {
713
+ this.log.warn(`Last message retrieved rolling hash does not match remote state.`, {
714
+ lastMessage,
715
+ remoteMessagesState
716
+ });
717
+ }
718
+ }
719
+ async retrieveL1ToL2Message(leaf) {
720
+ const currentL1BlockNumber = await this.publicClient.getBlockNumber();
721
+ let searchStartBlock = 0n;
722
+ let searchEndBlock = this.l1constants.l1StartBlock - 1n;
723
+ do {
724
+ [searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, currentL1BlockNumber);
725
+ const message = await retrieveL1ToL2Message(this.inbox, leaf, searchStartBlock, searchEndBlock);
726
+ if (message) {
727
+ return message;
728
+ }
729
+ }while (searchEndBlock < currentL1BlockNumber)
730
+ return undefined;
731
+ }
732
+ async rollbackL1ToL2Messages(localLastMessage, messagesSyncPoint) {
733
+ // Slowly go back through our messages until we find the last common message.
734
+ // We could query the logs in batch as an optimization, but the depth of the reorg should not be deep, and this
735
+ // is a very rare case, so it's fine to query one log at a time.
736
+ let commonMsg;
737
+ this.log.verbose(`Searching most recent common L1 to L2 message at or before index ${localLastMessage.index}`);
738
+ for await (const msg of this.store.iterateL1ToL2Messages({
739
+ reverse: true,
740
+ end: localLastMessage.index
741
+ })){
742
+ const remoteMsg = await this.retrieveL1ToL2Message(msg.leaf);
743
+ const logCtx = {
744
+ remoteMsg,
745
+ localMsg: msg
746
+ };
747
+ if (remoteMsg && remoteMsg.rollingHash.equals(msg.rollingHash)) {
748
+ this.log.verbose(`Found most recent common L1 to L2 message at index ${msg.index} on L1 block ${msg.l1BlockNumber}`, logCtx);
749
+ commonMsg = remoteMsg;
750
+ break;
751
+ } else if (remoteMsg) {
752
+ this.log.debug(`Local L1 to L2 message with index ${msg.index} has different rolling hash`, logCtx);
753
+ } else {
754
+ this.log.debug(`Local L1 to L2 message with index ${msg.index} not found on L1`, logCtx);
755
+ }
756
+ }
757
+ // Delete everything after the common message we found.
758
+ const lastGoodIndex = commonMsg?.index;
759
+ this.log.warn(`Deleting all local L1 to L2 messages after index ${lastGoodIndex ?? 'undefined'}`);
760
+ await this.store.removeL1ToL2Messages(lastGoodIndex !== undefined ? lastGoodIndex + 1n : 0n);
761
+ // Update the syncpoint so the loop below reprocesses the changed messages. We go to the block before
762
+ // the last common one, so we force reprocessing it, in case new messages were added on that same L1 block
763
+ // after the last common message.
764
+ const syncPointL1BlockNumber = commonMsg ? commonMsg.l1BlockNumber - 1n : this.l1constants.l1StartBlock;
765
+ const syncPointL1BlockHash = await this.getL1BlockHash(syncPointL1BlockNumber);
766
+ messagesSyncPoint = {
767
+ l1BlockNumber: syncPointL1BlockNumber,
768
+ l1BlockHash: syncPointL1BlockHash
769
+ };
770
+ await this.store.setMessageSynchedL1Block(messagesSyncPoint);
771
+ return messagesSyncPoint;
772
+ }
773
+ async getL1BlockHash(l1BlockNumber) {
774
+ const block = await this.publicClient.getBlock({
775
+ blockNumber: l1BlockNumber,
776
+ includeTransactions: false
777
+ });
778
+ if (!block) {
779
+ throw new Error(`Missing L1 block ${l1BlockNumber}`);
780
+ }
781
+ return Buffer32.fromString(block.hash);
782
+ }
783
+ async handleCheckpoints(blocksSynchedTo, currentL1BlockNumber, initialSyncComplete) {
784
+ const localPendingCheckpointNumber = await this.store.getSynchedCheckpointNumber();
785
+ const initialValidationResult = await this.store.getPendingChainValidationStatus();
786
+ const { provenCheckpointNumber, provenArchive, pendingCheckpointNumber, pendingArchive, archiveOfMyCheckpoint: archiveForLocalPendingCheckpointNumber } = await execInSpan(this.tracer, 'Archiver.getRollupStatus', ()=>this.rollup.status(localPendingCheckpointNumber, {
787
+ blockNumber: currentL1BlockNumber
788
+ }));
789
+ const rollupStatus = {
790
+ provenCheckpointNumber,
791
+ provenArchive: provenArchive.toString(),
792
+ pendingCheckpointNumber,
793
+ pendingArchive: pendingArchive.toString(),
794
+ validationResult: initialValidationResult
795
+ };
796
+ this.log.trace(`Retrieved rollup status at current L1 block ${currentL1BlockNumber}.`, {
797
+ localPendingCheckpointNumber,
798
+ blocksSynchedTo,
799
+ currentL1BlockNumber,
800
+ archiveForLocalPendingCheckpointNumber,
801
+ ...rollupStatus
802
+ });
803
+ const updateProvenCheckpoint = async ()=>{
804
+ // Annoying edge case: if proven checkpoint is moved back to 0 due to a reorg at the beginning of the chain,
805
+ // we need to set it to zero. This is an edge case because we dont have a checkpoint zero (initial checkpoint is one),
806
+ // so localCheckpointForDestinationProvenCheckpointNumber would not be found below.
807
+ if (provenCheckpointNumber === 0) {
808
+ const localProvenCheckpointNumber = await this.store.getProvenCheckpointNumber();
809
+ if (localProvenCheckpointNumber !== provenCheckpointNumber) {
810
+ await this.store.setProvenCheckpointNumber(provenCheckpointNumber);
811
+ this.log.info(`Rolled back proven chain to checkpoint ${provenCheckpointNumber}`, {
812
+ provenCheckpointNumber
813
+ });
814
+ }
815
+ }
816
+ const localCheckpointForDestinationProvenCheckpointNumber = await this.store.getCheckpointData(provenCheckpointNumber);
817
+ // Sanity check. I've hit what seems to be a state where the proven checkpoint is set to a value greater than the latest
818
+ // synched checkpoint when requesting L2Tips from the archiver. This is the only place where the proven checkpoint is set.
819
+ const synched = await this.store.getSynchedCheckpointNumber();
820
+ if (localCheckpointForDestinationProvenCheckpointNumber && synched < localCheckpointForDestinationProvenCheckpointNumber.checkpointNumber) {
821
+ this.log.error(`Hit local checkpoint greater than last synched checkpoint: ${localCheckpointForDestinationProvenCheckpointNumber.checkpointNumber} > ${synched}`);
822
+ }
823
+ this.log.trace(`Local checkpoint for remote proven checkpoint ${provenCheckpointNumber} is ${localCheckpointForDestinationProvenCheckpointNumber?.archive.root.toString() ?? 'undefined'}`);
824
+ if (localCheckpointForDestinationProvenCheckpointNumber && provenArchive.equals(localCheckpointForDestinationProvenCheckpointNumber.archive.root)) {
825
+ const localProvenCheckpointNumber = await this.store.getProvenCheckpointNumber();
826
+ if (localProvenCheckpointNumber !== provenCheckpointNumber) {
827
+ await this.store.setProvenCheckpointNumber(provenCheckpointNumber);
828
+ this.log.info(`Updated proven chain to checkpoint ${provenCheckpointNumber}`, {
829
+ provenCheckpointNumber
830
+ });
831
+ const provenSlotNumber = localCheckpointForDestinationProvenCheckpointNumber.header.slotNumber;
832
+ const provenEpochNumber = getEpochAtSlot(provenSlotNumber, this.l1constants);
833
+ const lastBlockNumberInCheckpoint = localCheckpointForDestinationProvenCheckpointNumber.startBlock + localCheckpointForDestinationProvenCheckpointNumber.numBlocks - 1;
834
+ this.events.emit(L2BlockSourceEvents.L2BlockProven, {
835
+ type: L2BlockSourceEvents.L2BlockProven,
836
+ blockNumber: BlockNumber(lastBlockNumberInCheckpoint),
837
+ slotNumber: provenSlotNumber,
838
+ epochNumber: provenEpochNumber
839
+ });
840
+ this.instrumentation.updateLastProvenBlock(lastBlockNumberInCheckpoint);
841
+ } else {
842
+ this.log.trace(`Proven checkpoint ${provenCheckpointNumber} already stored.`);
843
+ }
844
+ }
845
+ };
846
+ // This is an edge case that we only hit if there are no proposed checkpoints.
847
+ // If we have 0 checkpoints locally and there are no checkpoints onchain there is nothing to do.
848
+ const noCheckpoints = localPendingCheckpointNumber === 0 && pendingCheckpointNumber === 0;
849
+ if (noCheckpoints) {
850
+ await this.store.setCheckpointSynchedL1BlockNumber(currentL1BlockNumber);
851
+ this.log.debug(`No checkpoints to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}, no checkpoints on chain`);
852
+ return rollupStatus;
853
+ }
854
+ await updateProvenCheckpoint();
855
+ // Related to the L2 reorgs of the pending chain. We are only interested in actually addressing a reorg if there
856
+ // are any state that could be impacted by it. If we have no checkpoints, there is no impact.
857
+ if (localPendingCheckpointNumber > 0) {
858
+ const localPendingCheckpoint = await this.store.getCheckpointData(localPendingCheckpointNumber);
859
+ if (localPendingCheckpoint === undefined) {
860
+ throw new Error(`Missing checkpoint ${localPendingCheckpointNumber}`);
861
+ }
862
+ const localPendingArchiveRoot = localPendingCheckpoint.archive.root.toString();
863
+ const noCheckpointSinceLast = localPendingCheckpoint && pendingArchive.toString() === localPendingArchiveRoot;
864
+ if (noCheckpointSinceLast) {
865
+ // We believe the following line causes a problem when we encounter L1 re-orgs.
866
+ // Basically, by setting the synched L1 block number here, we are saying that we have
867
+ // processed all checkpoints up to the current L1 block number and we will not attempt to retrieve logs from
868
+ // this block again (or any blocks before).
869
+ // However, in the re-org scenario, our L1 node is temporarily lying to us and we end up potentially missing checkpoints.
870
+ // We must only set this block number based on actually retrieved logs.
871
+ // TODO(#8621): Tackle this properly when we handle L1 Re-orgs.
872
+ // await this.store.setCheckpointSynchedL1BlockNumber(currentL1BlockNumber);
873
+ this.log.debug(`No checkpoints to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}`);
874
+ return rollupStatus;
875
+ }
876
+ const localPendingCheckpointInChain = archiveForLocalPendingCheckpointNumber.equals(localPendingCheckpoint.archive.root);
877
+ if (!localPendingCheckpointInChain) {
878
+ // If our local pending checkpoint tip is not in the chain on L1 a "prune" must have happened
879
+ // or the L1 have reorged.
880
+ // In any case, we have to figure out how far into the past the action will take us.
881
+ // For simplicity here, we will simply rewind until we end in a checkpoint that is also on the chain on L1.
882
+ this.log.debug(`L2 prune has been detected due to local pending checkpoint ${localPendingCheckpointNumber} not in chain`, {
883
+ localPendingCheckpointNumber,
884
+ localPendingArchiveRoot,
885
+ archiveForLocalPendingCheckpointNumber
886
+ });
887
+ let tipAfterUnwind = localPendingCheckpointNumber;
888
+ while(true){
889
+ const candidateCheckpoint = await this.store.getCheckpointData(tipAfterUnwind);
890
+ if (candidateCheckpoint === undefined) {
891
+ break;
892
+ }
893
+ const archiveAtContract = await this.rollup.archiveAt(candidateCheckpoint.checkpointNumber);
894
+ this.log.trace(`Checking local checkpoint ${candidateCheckpoint.checkpointNumber} with archive ${candidateCheckpoint.archive.root}`, {
895
+ archiveAtContract,
896
+ archiveLocal: candidateCheckpoint.archive.root.toString()
897
+ });
898
+ if (archiveAtContract.equals(candidateCheckpoint.archive.root)) {
899
+ break;
900
+ }
901
+ tipAfterUnwind--;
902
+ }
903
+ const checkpointsToUnwind = localPendingCheckpointNumber - tipAfterUnwind;
904
+ await this.updater.unwindCheckpointsWithContractData(localPendingCheckpointNumber, checkpointsToUnwind);
905
+ this.log.warn(`Unwound ${count(checkpointsToUnwind, 'checkpoint')} from checkpoint ${localPendingCheckpointNumber} ` + `due to mismatched checkpoint hashes at L1 block ${currentL1BlockNumber}. ` + `Updated L2 latest checkpoint is ${await this.store.getSynchedCheckpointNumber()}.`);
906
+ }
907
+ }
908
+ // Retrieve checkpoints in batches. Each batch is estimated to accommodate up to 'blockBatchSize' L1 blocks,
909
+ // computed using the L2 block time vs the L1 block time.
910
+ let searchStartBlock = blocksSynchedTo;
911
+ let searchEndBlock = blocksSynchedTo;
912
+ let lastRetrievedCheckpoint;
913
+ let lastL1BlockWithCheckpoint = undefined;
914
+ do {
915
+ [searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, currentL1BlockNumber);
916
+ this.log.trace(`Retrieving checkpoints from L1 block ${searchStartBlock} to ${searchEndBlock}`);
917
+ // TODO(md): Retrieve from blob client then from consensus client, then from peers
918
+ const retrievedCheckpoints = await execInSpan(this.tracer, 'Archiver.retrieveCheckpointsFromRollup', ()=>retrieveCheckpointsFromRollup(this.rollup, this.publicClient, this.debugClient, this.blobClient, searchStartBlock, searchEndBlock, this.l1Addresses, this.instrumentation, this.log, !initialSyncComplete));
919
+ if (retrievedCheckpoints.length === 0) {
920
+ // We are not calling `setBlockSynchedL1BlockNumber` because it may cause sync issues if based off infura.
921
+ // See further details in earlier comments.
922
+ this.log.trace(`Retrieved no new checkpoints from L1 block ${searchStartBlock} to ${searchEndBlock}`);
923
+ continue;
924
+ }
925
+ this.log.debug(`Retrieved ${retrievedCheckpoints.length} new checkpoints between L1 blocks ${searchStartBlock} and ${searchEndBlock}`, {
926
+ lastProcessedCheckpoint: retrievedCheckpoints[retrievedCheckpoints.length - 1].l1,
927
+ searchStartBlock,
928
+ searchEndBlock
929
+ });
930
+ const publishedCheckpoints = await Promise.all(retrievedCheckpoints.map((b)=>retrievedToPublishedCheckpoint(b)));
931
+ const validCheckpoints = [];
932
+ for (const published of publishedCheckpoints){
933
+ const validationResult = this.config.skipValidateCheckpointAttestations ? {
934
+ valid: true
935
+ } : await validateCheckpointAttestations(published, this.epochCache, this.l1constants, this.log);
936
+ // Only update the validation result if it has changed, so we can keep track of the first invalid checkpoint
937
+ // in case there is a sequence of more than one invalid checkpoint, as we need to invalidate the first one.
938
+ // There is an exception though: if a checkpoint is invalidated and replaced with another invalid checkpoint,
939
+ // we need to update the validation result, since we need to be able to invalidate the new one.
940
+ // See test 'chain progresses if an invalid checkpoint is invalidated with an invalid one' for more info.
941
+ if (rollupStatus.validationResult?.valid !== validationResult.valid || !rollupStatus.validationResult.valid && !validationResult.valid && rollupStatus.validationResult.checkpoint.checkpointNumber === validationResult.checkpoint.checkpointNumber) {
942
+ rollupStatus.validationResult = validationResult;
943
+ }
944
+ if (!validationResult.valid) {
945
+ this.log.warn(`Skipping checkpoint ${published.checkpoint.number} due to invalid attestations`, {
946
+ checkpointHash: published.checkpoint.hash(),
947
+ l1BlockNumber: published.l1.blockNumber,
948
+ ...pick(validationResult, 'reason')
949
+ });
950
+ // Emit event for invalid checkpoint detection
951
+ this.events.emit(L2BlockSourceEvents.InvalidAttestationsCheckpointDetected, {
952
+ type: L2BlockSourceEvents.InvalidAttestationsCheckpointDetected,
953
+ validationResult
954
+ });
955
+ continue;
956
+ }
957
+ // Check the inHash of the checkpoint against the l1->l2 messages.
958
+ // The messages should've been synced up to the currentL1BlockNumber and must be available for the published
959
+ // checkpoints we just retrieved.
960
+ const l1ToL2Messages = await this.store.getL1ToL2Messages(published.checkpoint.number);
961
+ const computedInHash = computeInHashFromL1ToL2Messages(l1ToL2Messages);
962
+ const publishedInHash = published.checkpoint.header.inHash;
963
+ if (!computedInHash.equals(publishedInHash)) {
964
+ this.log.fatal(`Mismatch inHash for checkpoint ${published.checkpoint.number}`, {
965
+ checkpointHash: published.checkpoint.hash(),
966
+ l1BlockNumber: published.l1.blockNumber,
967
+ computedInHash,
968
+ publishedInHash
969
+ });
970
+ // Throwing an error since this is most likely caused by a bug.
971
+ throw new Error(`Mismatch inHash for checkpoint ${published.checkpoint.number}. Expected ${computedInHash} but got ${publishedInHash}`);
972
+ }
973
+ validCheckpoints.push(published);
974
+ this.log.debug(`Ingesting new checkpoint ${published.checkpoint.number} with ${published.checkpoint.blocks.length} blocks`, {
975
+ checkpointHash: published.checkpoint.hash(),
976
+ l1BlockNumber: published.l1.blockNumber,
977
+ ...published.checkpoint.header.toInspect(),
978
+ blocks: published.checkpoint.blocks.map((b)=>b.getStats())
979
+ });
980
+ }
981
+ try {
982
+ const updatedValidationResult = rollupStatus.validationResult === initialValidationResult ? undefined : rollupStatus.validationResult;
983
+ const [processDuration] = await elapsed(()=>execInSpan(this.tracer, 'Archiver.addCheckpoints', ()=>this.updater.addCheckpointsWithContractData(validCheckpoints, updatedValidationResult)));
984
+ this.instrumentation.processNewBlocks(processDuration / validCheckpoints.length, validCheckpoints.flatMap((c)=>c.checkpoint.blocks));
985
+ } catch (err) {
986
+ if (err instanceof InitialCheckpointNumberNotSequentialError) {
987
+ const { previousCheckpointNumber, newCheckpointNumber } = err;
988
+ const previousCheckpoint = previousCheckpointNumber ? await this.store.getCheckpointData(CheckpointNumber(previousCheckpointNumber)) : undefined;
989
+ const updatedL1SyncPoint = previousCheckpoint?.l1.blockNumber ?? this.l1constants.l1StartBlock;
990
+ await this.store.setCheckpointSynchedL1BlockNumber(updatedL1SyncPoint);
991
+ 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.`, {
992
+ previousCheckpointNumber,
993
+ newCheckpointNumber,
994
+ updatedL1SyncPoint
995
+ });
996
+ }
997
+ throw err;
998
+ }
999
+ for (const checkpoint of validCheckpoints){
1000
+ this.log.info(`Downloaded checkpoint ${checkpoint.checkpoint.number}`, {
1001
+ checkpointHash: checkpoint.checkpoint.hash(),
1002
+ checkpointNumber: checkpoint.checkpoint.number,
1003
+ blockCount: checkpoint.checkpoint.blocks.length,
1004
+ txCount: checkpoint.checkpoint.blocks.reduce((acc, b)=>acc + b.body.txEffects.length, 0),
1005
+ header: checkpoint.checkpoint.header.toInspect(),
1006
+ archiveRoot: checkpoint.checkpoint.archive.root.toString(),
1007
+ archiveNextLeafIndex: checkpoint.checkpoint.archive.nextAvailableLeafIndex
1008
+ });
1009
+ }
1010
+ lastRetrievedCheckpoint = validCheckpoints.at(-1) ?? lastRetrievedCheckpoint;
1011
+ lastL1BlockWithCheckpoint = retrievedCheckpoints.at(-1)?.l1.blockNumber ?? lastL1BlockWithCheckpoint;
1012
+ }while (searchEndBlock < currentL1BlockNumber)
1013
+ // Important that we update AFTER inserting the blocks.
1014
+ await updateProvenCheckpoint();
1015
+ return {
1016
+ ...rollupStatus,
1017
+ lastRetrievedCheckpoint,
1018
+ lastL1BlockWithCheckpoint
1019
+ };
1020
+ }
1021
+ async checkForNewCheckpointsBeforeL1SyncPoint(status, blocksSynchedTo, currentL1BlockNumber) {
1022
+ const { lastRetrievedCheckpoint, pendingCheckpointNumber } = status;
1023
+ // Compare the last checkpoint we have (either retrieved in this round or loaded from store) with what the
1024
+ // rollup contract told us was the latest one (pinned at the currentL1BlockNumber).
1025
+ const latestLocalCheckpointNumber = lastRetrievedCheckpoint?.checkpoint.number ?? await this.store.getSynchedCheckpointNumber();
1026
+ if (latestLocalCheckpointNumber < pendingCheckpointNumber) {
1027
+ // Here we have consumed all logs until the `currentL1Block` we pinned at the beginning of the archiver loop,
1028
+ // but still haven't reached the pending checkpoint according to the call to the rollup contract.
1029
+ // We suspect an L1 reorg that added checkpoints *behind* us. If that is the case, it must have happened between
1030
+ // the last checkpoint we saw and the current one, so we reset the last synched L1 block number. In the edge case
1031
+ // we don't have one, we go back 2 L1 epochs, which is the deepest possible reorg (assuming Casper is working).
1032
+ let latestLocalCheckpointArchive = undefined;
1033
+ let targetL1BlockNumber = maxBigint(currentL1BlockNumber - 64n, 0n);
1034
+ if (lastRetrievedCheckpoint) {
1035
+ latestLocalCheckpointArchive = lastRetrievedCheckpoint.checkpoint.archive.root.toString();
1036
+ targetL1BlockNumber = lastRetrievedCheckpoint.l1.blockNumber;
1037
+ } else if (latestLocalCheckpointNumber > 0) {
1038
+ const checkpoint = await this.store.getRangeOfCheckpoints(latestLocalCheckpointNumber, 1).then(([c])=>c);
1039
+ latestLocalCheckpointArchive = checkpoint.archive.root.toString();
1040
+ targetL1BlockNumber = checkpoint.l1.blockNumber;
1041
+ }
1042
+ this.log.warn(`Failed to reach checkpoint ${pendingCheckpointNumber} at ${currentL1BlockNumber} (latest is ${latestLocalCheckpointNumber}). ` + `Rolling back last synched L1 block number to ${targetL1BlockNumber}.`, {
1043
+ latestLocalCheckpointNumber,
1044
+ latestLocalCheckpointArchive,
1045
+ blocksSynchedTo,
1046
+ currentL1BlockNumber,
1047
+ ...status
1048
+ });
1049
+ await this.store.setCheckpointSynchedL1BlockNumber(targetL1BlockNumber);
1050
+ } else {
1051
+ this.log.trace(`No new checkpoints behind L1 sync point to retrieve.`, {
1052
+ latestLocalCheckpointNumber,
1053
+ pendingCheckpointNumber
1054
+ });
1055
+ }
1056
+ }
1057
+ async getCheckpointHeader(number) {
1058
+ const checkpoint = await this.store.getCheckpointData(number);
1059
+ if (!checkpoint) {
1060
+ return undefined;
1061
+ }
1062
+ return checkpoint.header;
1063
+ }
1064
+ }