@aztec/archiver 0.0.1-commit.2ed92850 → 0.0.1-commit.343b43af6
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.
- package/dest/archiver.d.ts +9 -5
- package/dest/archiver.d.ts.map +1 -1
- package/dest/archiver.js +76 -111
- package/dest/errors.d.ts +7 -9
- package/dest/errors.d.ts.map +1 -1
- package/dest/errors.js +9 -14
- package/dest/factory.d.ts +5 -4
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +17 -16
- package/dest/index.d.ts +2 -1
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +1 -0
- package/dest/l1/bin/retrieve-calldata.js +36 -33
- package/dest/l1/calldata_retriever.d.ts +73 -50
- package/dest/l1/calldata_retriever.d.ts.map +1 -1
- package/dest/l1/calldata_retriever.js +190 -259
- package/dest/l1/data_retrieval.d.ts +9 -9
- package/dest/l1/data_retrieval.d.ts.map +1 -1
- package/dest/l1/data_retrieval.js +22 -20
- package/dest/l1/spire_proposer.d.ts +5 -5
- package/dest/l1/spire_proposer.d.ts.map +1 -1
- package/dest/l1/spire_proposer.js +9 -17
- package/dest/l1/validate_trace.d.ts +6 -3
- package/dest/l1/validate_trace.d.ts.map +1 -1
- package/dest/l1/validate_trace.js +13 -9
- package/dest/modules/data_source_base.d.ts +13 -8
- package/dest/modules/data_source_base.d.ts.map +1 -1
- package/dest/modules/data_source_base.js +28 -72
- package/dest/modules/data_store_updater.d.ts +22 -7
- package/dest/modules/data_store_updater.d.ts.map +1 -1
- package/dest/modules/data_store_updater.js +69 -29
- package/dest/modules/instrumentation.d.ts +15 -2
- package/dest/modules/instrumentation.d.ts.map +1 -1
- package/dest/modules/instrumentation.js +36 -12
- package/dest/modules/l1_synchronizer.d.ts +5 -8
- package/dest/modules/l1_synchronizer.d.ts.map +1 -1
- package/dest/modules/l1_synchronizer.js +43 -13
- package/dest/store/block_store.d.ts +30 -28
- package/dest/store/block_store.d.ts.map +1 -1
- package/dest/store/block_store.js +125 -76
- package/dest/store/contract_class_store.d.ts +1 -1
- package/dest/store/contract_class_store.d.ts.map +1 -1
- package/dest/store/contract_class_store.js +11 -7
- package/dest/store/kv_archiver_store.d.ts +36 -14
- package/dest/store/kv_archiver_store.d.ts.map +1 -1
- package/dest/store/kv_archiver_store.js +39 -10
- package/dest/store/l2_tips_cache.d.ts +19 -0
- package/dest/store/l2_tips_cache.d.ts.map +1 -0
- package/dest/store/l2_tips_cache.js +89 -0
- package/dest/store/log_store.d.ts +1 -1
- package/dest/store/log_store.d.ts.map +1 -1
- package/dest/store/log_store.js +57 -37
- package/dest/store/message_store.js +1 -1
- package/dest/test/fake_l1_state.d.ts +13 -1
- package/dest/test/fake_l1_state.d.ts.map +1 -1
- package/dest/test/fake_l1_state.js +84 -20
- package/dest/test/index.js +3 -1
- package/dest/test/mock_archiver.d.ts +1 -1
- package/dest/test/mock_archiver.d.ts.map +1 -1
- package/dest/test/mock_archiver.js +3 -2
- package/dest/test/mock_l2_block_source.d.ts +25 -9
- package/dest/test/mock_l2_block_source.d.ts.map +1 -1
- package/dest/test/mock_l2_block_source.js +132 -86
- package/dest/test/mock_structs.d.ts +6 -2
- package/dest/test/mock_structs.d.ts.map +1 -1
- package/dest/test/mock_structs.js +20 -6
- package/dest/test/noop_l1_archiver.d.ts +26 -0
- package/dest/test/noop_l1_archiver.d.ts.map +1 -0
- package/dest/test/noop_l1_archiver.js +72 -0
- package/package.json +14 -13
- package/src/archiver.ts +96 -132
- package/src/errors.ts +10 -24
- package/src/factory.ts +32 -17
- package/src/index.ts +1 -0
- package/src/l1/README.md +25 -68
- package/src/l1/bin/retrieve-calldata.ts +46 -39
- package/src/l1/calldata_retriever.ts +249 -379
- package/src/l1/data_retrieval.ts +24 -26
- package/src/l1/spire_proposer.ts +7 -15
- package/src/l1/validate_trace.ts +24 -6
- package/src/modules/data_source_base.ts +58 -97
- package/src/modules/data_store_updater.ts +71 -30
- package/src/modules/instrumentation.ts +44 -12
- package/src/modules/l1_synchronizer.ts +48 -17
- package/src/store/block_store.ts +151 -108
- package/src/store/contract_class_store.ts +11 -7
- package/src/store/kv_archiver_store.ts +62 -16
- package/src/store/l2_tips_cache.ts +89 -0
- package/src/store/log_store.ts +98 -36
- package/src/store/message_store.ts +1 -1
- package/src/test/fake_l1_state.ts +110 -21
- package/src/test/index.ts +3 -0
- package/src/test/mock_archiver.ts +3 -2
- package/src/test/mock_l2_block_source.ts +172 -86
- package/src/test/mock_structs.ts +42 -12
- package/src/test/noop_l1_archiver.ts +115 -0
package/src/archiver.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { BlobClientInterface } from '@aztec/blob-client/client';
|
|
2
|
-
import { GENESIS_BLOCK_HEADER_HASH, INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
|
|
3
2
|
import { EpochCache } from '@aztec/epoch-cache';
|
|
4
3
|
import { BlockTagTooOldError, RollupContract } from '@aztec/ethereum/contracts';
|
|
5
4
|
import type { L1ContractAddresses } from '@aztec/ethereum/l1-contract-addresses';
|
|
@@ -15,8 +14,6 @@ import { RunningPromise, makeLoggingErrorHandler } from '@aztec/foundation/runni
|
|
|
15
14
|
import { DateProvider } from '@aztec/foundation/timer';
|
|
16
15
|
import {
|
|
17
16
|
type ArchiverEmitter,
|
|
18
|
-
type CheckpointId,
|
|
19
|
-
GENESIS_CHECKPOINT_HEADER_HASH,
|
|
20
17
|
L2Block,
|
|
21
18
|
type L2BlockSink,
|
|
22
19
|
type L2Tips,
|
|
@@ -25,21 +22,22 @@ import {
|
|
|
25
22
|
import { PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
|
|
26
23
|
import {
|
|
27
24
|
type L1RollupConstants,
|
|
28
|
-
|
|
29
|
-
|
|
25
|
+
getEpochAtSlot,
|
|
26
|
+
getSlotAtNextL1Block,
|
|
30
27
|
getSlotRangeForEpoch,
|
|
31
28
|
getTimestampRangeForEpoch,
|
|
32
29
|
} from '@aztec/stdlib/epoch-helpers';
|
|
33
30
|
import { type TelemetryClient, type Traceable, type Tracer, trackSpan } from '@aztec/telemetry-client';
|
|
34
31
|
|
|
35
32
|
import { type ArchiverConfig, mapArchiverConfig } from './config.js';
|
|
36
|
-
import { NoBlobBodiesFoundError } from './errors.js';
|
|
33
|
+
import { BlockAlreadyCheckpointedError, NoBlobBodiesFoundError } from './errors.js';
|
|
37
34
|
import { validateAndLogTraceAvailability } from './l1/validate_trace.js';
|
|
38
35
|
import { ArchiverDataSourceBase } from './modules/data_source_base.js';
|
|
39
36
|
import { ArchiverDataStoreUpdater } from './modules/data_store_updater.js';
|
|
40
37
|
import type { ArchiverInstrumentation } from './modules/instrumentation.js';
|
|
41
38
|
import type { ArchiverL1Synchronizer } from './modules/l1_synchronizer.js';
|
|
42
39
|
import type { KVArchiverDataStore } from './store/kv_archiver_store.js';
|
|
40
|
+
import { L2TipsCache } from './store/l2_tips_cache.js';
|
|
43
41
|
|
|
44
42
|
/** Export ArchiverEmitter for use in factory and tests. */
|
|
45
43
|
export type { ArchiverEmitter };
|
|
@@ -68,7 +66,7 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
|
|
|
68
66
|
public readonly events: ArchiverEmitter;
|
|
69
67
|
|
|
70
68
|
/** A loop in which we will be continually fetching new checkpoints. */
|
|
71
|
-
|
|
69
|
+
protected runningPromise: RunningPromise;
|
|
72
70
|
|
|
73
71
|
/** L1 synchronizer that handles fetching checkpoints and messages from L1. */
|
|
74
72
|
private readonly synchronizer: ArchiverL1Synchronizer;
|
|
@@ -82,6 +80,9 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
|
|
|
82
80
|
/** Helper to handle updates to the store */
|
|
83
81
|
private readonly updater: ArchiverDataStoreUpdater;
|
|
84
82
|
|
|
83
|
+
/** In-memory cache for L2 chain tips. */
|
|
84
|
+
private readonly l2TipsCache: L2TipsCache;
|
|
85
|
+
|
|
85
86
|
public readonly tracer: Tracer;
|
|
86
87
|
|
|
87
88
|
/**
|
|
@@ -118,9 +119,13 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
|
|
|
118
119
|
},
|
|
119
120
|
private readonly blobClient: BlobClientInterface,
|
|
120
121
|
instrumentation: ArchiverInstrumentation,
|
|
121
|
-
protected override readonly l1Constants: L1RollupConstants & {
|
|
122
|
+
protected override readonly l1Constants: L1RollupConstants & {
|
|
123
|
+
l1StartBlockHash: Buffer32;
|
|
124
|
+
genesisArchiveRoot: Fr;
|
|
125
|
+
},
|
|
122
126
|
synchronizer: ArchiverL1Synchronizer,
|
|
123
127
|
events: ArchiverEmitter,
|
|
128
|
+
l2TipsCache?: L2TipsCache,
|
|
124
129
|
private readonly log: Logger = createLogger('archiver'),
|
|
125
130
|
) {
|
|
126
131
|
super(dataStore, l1Constants);
|
|
@@ -129,7 +134,10 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
|
|
|
129
134
|
this.initialSyncPromise = promiseWithResolvers();
|
|
130
135
|
this.synchronizer = synchronizer;
|
|
131
136
|
this.events = events;
|
|
132
|
-
this.
|
|
137
|
+
this.l2TipsCache = l2TipsCache ?? new L2TipsCache(this.dataStore.blockStore);
|
|
138
|
+
this.updater = new ArchiverDataStoreUpdater(this.dataStore, this.l2TipsCache, {
|
|
139
|
+
rollupManaLimit: l1Constants.rollupManaLimit,
|
|
140
|
+
});
|
|
133
141
|
|
|
134
142
|
// Running promise starts with a small interval inbetween runs, so all iterations needed for the initial sync
|
|
135
143
|
// are done as fast as possible. This then gets updated once the initial sync completes.
|
|
@@ -158,7 +166,11 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
|
|
|
158
166
|
|
|
159
167
|
await this.blobClient.testSources();
|
|
160
168
|
await this.synchronizer.testEthereumNodeSynced();
|
|
161
|
-
await validateAndLogTraceAvailability(
|
|
169
|
+
await validateAndLogTraceAvailability(
|
|
170
|
+
this.debugClient,
|
|
171
|
+
this.config.ethereumAllowNoDebugHosts ?? false,
|
|
172
|
+
this.log.getBindings(),
|
|
173
|
+
);
|
|
162
174
|
|
|
163
175
|
// Log initial state for the archiver
|
|
164
176
|
const { l1StartBlock } = this.l1Constants;
|
|
@@ -212,13 +224,33 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
|
|
|
212
224
|
const queuedItems = this.blockQueue.splice(0, this.blockQueue.length);
|
|
213
225
|
this.log.debug(`Processing ${queuedItems.length} queued block(s)`);
|
|
214
226
|
|
|
227
|
+
// Calculate slot threshold for validation
|
|
228
|
+
const l1Timestamp = this.synchronizer.getL1Timestamp();
|
|
229
|
+
const slotAtNextL1Block =
|
|
230
|
+
l1Timestamp === undefined ? undefined : getSlotAtNextL1Block(l1Timestamp, this.l1Constants);
|
|
231
|
+
|
|
215
232
|
// Process each block individually to properly resolve/reject each promise
|
|
216
233
|
for (const { block, resolve, reject } of queuedItems) {
|
|
234
|
+
const blockSlot = block.header.globalVariables.slotNumber;
|
|
235
|
+
if (slotAtNextL1Block !== undefined && blockSlot < slotAtNextL1Block) {
|
|
236
|
+
this.log.warn(
|
|
237
|
+
`Rejecting proposed block ${block.number} for past slot ${blockSlot} (current is ${slotAtNextL1Block})`,
|
|
238
|
+
{ block: block.toBlockInfo(), l1Timestamp, slotAtNextL1Block },
|
|
239
|
+
);
|
|
240
|
+
reject(new Error(`Block ${block.number} is for past slot ${blockSlot} (current is ${slotAtNextL1Block})`));
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
|
|
217
244
|
try {
|
|
218
|
-
await this.updater.
|
|
245
|
+
await this.updater.addProposedBlock(block);
|
|
219
246
|
this.log.debug(`Added block ${block.number} to store`);
|
|
220
247
|
resolve();
|
|
221
248
|
} catch (err: any) {
|
|
249
|
+
if (err instanceof BlockAlreadyCheckpointedError) {
|
|
250
|
+
this.log.debug(`Proposed block ${block.number} matches already checkpointed block, ignoring late proposal`);
|
|
251
|
+
resolve();
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
222
254
|
this.log.error(`Failed to add block ${block.number} to store: ${err.message}`);
|
|
223
255
|
reject(err);
|
|
224
256
|
}
|
|
@@ -310,16 +342,35 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
|
|
|
310
342
|
return Promise.resolve(this.synchronizer.getL1Timestamp());
|
|
311
343
|
}
|
|
312
344
|
|
|
313
|
-
public
|
|
345
|
+
public getSyncedL2SlotNumber(): Promise<SlotNumber | undefined> {
|
|
314
346
|
const l1Timestamp = this.synchronizer.getL1Timestamp();
|
|
315
|
-
|
|
347
|
+
if (l1Timestamp === undefined) {
|
|
348
|
+
return Promise.resolve(undefined);
|
|
349
|
+
}
|
|
350
|
+
// The synced slot is the last L2 slot whose all L1 blocks have been processed.
|
|
351
|
+
// If the next L1 block (at l1Timestamp + ethereumSlotDuration) falls in slot N,
|
|
352
|
+
// then we've fully synced slot N-1.
|
|
353
|
+
const nextL1BlockSlot = getSlotAtNextL1Block(l1Timestamp, this.l1Constants);
|
|
354
|
+
if (Number(nextL1BlockSlot) === 0) {
|
|
355
|
+
return Promise.resolve(undefined);
|
|
356
|
+
}
|
|
357
|
+
return Promise.resolve(SlotNumber(nextL1BlockSlot - 1));
|
|
316
358
|
}
|
|
317
359
|
|
|
318
|
-
public
|
|
319
|
-
const
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
360
|
+
public async getSyncedL2EpochNumber(): Promise<EpochNumber | undefined> {
|
|
361
|
+
const syncedSlot = await this.getSyncedL2SlotNumber();
|
|
362
|
+
if (syncedSlot === undefined) {
|
|
363
|
+
return undefined;
|
|
364
|
+
}
|
|
365
|
+
// An epoch is fully synced when all its slots are synced.
|
|
366
|
+
// We check if syncedSlot is the last slot of its epoch; if so, that epoch is fully synced.
|
|
367
|
+
// Otherwise, only the previous epoch is fully synced.
|
|
368
|
+
const epoch = getEpochAtSlot(syncedSlot, this.l1Constants);
|
|
369
|
+
const [, endSlot] = getSlotRangeForEpoch(epoch, this.l1Constants);
|
|
370
|
+
if (syncedSlot >= endSlot) {
|
|
371
|
+
return epoch;
|
|
372
|
+
}
|
|
373
|
+
return Number(epoch) > 0 ? EpochNumber(Number(epoch) - 1) : undefined;
|
|
323
374
|
}
|
|
324
375
|
|
|
325
376
|
public async isEpochComplete(epochNumber: EpochNumber): Promise<boolean> {
|
|
@@ -371,115 +422,11 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
|
|
|
371
422
|
return true;
|
|
372
423
|
}
|
|
373
424
|
|
|
374
|
-
public
|
|
375
|
-
|
|
376
|
-
this.getBlockNumber(),
|
|
377
|
-
this.getProvenBlockNumber(),
|
|
378
|
-
this.getCheckpointedL2BlockNumber(),
|
|
379
|
-
this.getFinalizedL2BlockNumber(),
|
|
380
|
-
] as const);
|
|
381
|
-
|
|
382
|
-
const beforeInitialblockNumber = BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
383
|
-
|
|
384
|
-
// Get the latest block header and checkpointed blocks for proven, finalised and checkpointed blocks
|
|
385
|
-
const [latestBlockHeader, provenCheckpointedBlock, finalizedCheckpointedBlock, checkpointedBlock] =
|
|
386
|
-
await Promise.all([
|
|
387
|
-
latestBlockNumber > beforeInitialblockNumber ? this.getBlockHeader(latestBlockNumber) : undefined,
|
|
388
|
-
provenBlockNumber > beforeInitialblockNumber ? this.getCheckpointedBlock(provenBlockNumber) : undefined,
|
|
389
|
-
finalizedBlockNumber > beforeInitialblockNumber ? this.getCheckpointedBlock(finalizedBlockNumber) : undefined,
|
|
390
|
-
checkpointedBlockNumber > beforeInitialblockNumber
|
|
391
|
-
? this.getCheckpointedBlock(checkpointedBlockNumber)
|
|
392
|
-
: undefined,
|
|
393
|
-
] as const);
|
|
394
|
-
|
|
395
|
-
if (latestBlockNumber > beforeInitialblockNumber && !latestBlockHeader) {
|
|
396
|
-
throw new Error(`Failed to retrieve latest block header for block ${latestBlockNumber}`);
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
// Checkpointed blocks must exist for proven, finalized and checkpointed tips if they are beyond the initial block number.
|
|
400
|
-
if (checkpointedBlockNumber > beforeInitialblockNumber && !checkpointedBlock?.block.header) {
|
|
401
|
-
throw new Error(
|
|
402
|
-
`Failed to retrieve checkpointed block header for block ${checkpointedBlockNumber} (latest block is ${latestBlockNumber})`,
|
|
403
|
-
);
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
if (provenBlockNumber > beforeInitialblockNumber && !provenCheckpointedBlock?.block.header) {
|
|
407
|
-
throw new Error(
|
|
408
|
-
`Failed to retrieve proven checkpointed for block ${provenBlockNumber} (latest block is ${latestBlockNumber})`,
|
|
409
|
-
);
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
if (finalizedBlockNumber > beforeInitialblockNumber && !finalizedCheckpointedBlock?.block.header) {
|
|
413
|
-
throw new Error(
|
|
414
|
-
`Failed to retrieve finalized block header for block ${finalizedBlockNumber} (latest block is ${latestBlockNumber})`,
|
|
415
|
-
);
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
const latestBlockHeaderHash = (await latestBlockHeader?.hash()) ?? GENESIS_BLOCK_HEADER_HASH;
|
|
419
|
-
const provenBlockHeaderHash = (await provenCheckpointedBlock?.block.header?.hash()) ?? GENESIS_BLOCK_HEADER_HASH;
|
|
420
|
-
const finalizedBlockHeaderHash =
|
|
421
|
-
(await finalizedCheckpointedBlock?.block.header?.hash()) ?? GENESIS_BLOCK_HEADER_HASH;
|
|
422
|
-
const checkpointedBlockHeaderHash = (await checkpointedBlock?.block.header?.hash()) ?? GENESIS_BLOCK_HEADER_HASH;
|
|
423
|
-
|
|
424
|
-
// Now attempt to retrieve checkpoints for proven, finalised and checkpointed blocks
|
|
425
|
-
const [[provenBlockCheckpoint], [finalizedBlockCheckpoint], [checkpointedBlockCheckpoint]] = await Promise.all([
|
|
426
|
-
provenCheckpointedBlock !== undefined
|
|
427
|
-
? await this.getCheckpoints(provenCheckpointedBlock?.checkpointNumber, 1)
|
|
428
|
-
: [undefined],
|
|
429
|
-
finalizedCheckpointedBlock !== undefined
|
|
430
|
-
? await this.getCheckpoints(finalizedCheckpointedBlock?.checkpointNumber, 1)
|
|
431
|
-
: [undefined],
|
|
432
|
-
checkpointedBlock !== undefined ? await this.getCheckpoints(checkpointedBlock?.checkpointNumber, 1) : [undefined],
|
|
433
|
-
]);
|
|
434
|
-
|
|
435
|
-
const initialcheckpointId: CheckpointId = {
|
|
436
|
-
number: CheckpointNumber.ZERO,
|
|
437
|
-
hash: GENESIS_CHECKPOINT_HEADER_HASH.toString(),
|
|
438
|
-
};
|
|
439
|
-
|
|
440
|
-
const makeCheckpointId = (checkpoint: PublishedCheckpoint | undefined) => {
|
|
441
|
-
if (checkpoint === undefined) {
|
|
442
|
-
return initialcheckpointId;
|
|
443
|
-
}
|
|
444
|
-
return {
|
|
445
|
-
number: checkpoint.checkpoint.number,
|
|
446
|
-
hash: checkpoint.checkpoint.hash().toString(),
|
|
447
|
-
};
|
|
448
|
-
};
|
|
449
|
-
|
|
450
|
-
const l2Tips: L2Tips = {
|
|
451
|
-
proposed: {
|
|
452
|
-
number: latestBlockNumber,
|
|
453
|
-
hash: latestBlockHeaderHash.toString(),
|
|
454
|
-
},
|
|
455
|
-
proven: {
|
|
456
|
-
block: {
|
|
457
|
-
number: provenBlockNumber,
|
|
458
|
-
hash: provenBlockHeaderHash.toString(),
|
|
459
|
-
},
|
|
460
|
-
checkpoint: makeCheckpointId(provenBlockCheckpoint),
|
|
461
|
-
},
|
|
462
|
-
finalized: {
|
|
463
|
-
block: {
|
|
464
|
-
number: finalizedBlockNumber,
|
|
465
|
-
hash: finalizedBlockHeaderHash.toString(),
|
|
466
|
-
},
|
|
467
|
-
checkpoint: makeCheckpointId(finalizedBlockCheckpoint),
|
|
468
|
-
},
|
|
469
|
-
checkpointed: {
|
|
470
|
-
block: {
|
|
471
|
-
number: checkpointedBlockNumber,
|
|
472
|
-
hash: checkpointedBlockHeaderHash.toString(),
|
|
473
|
-
},
|
|
474
|
-
checkpoint: makeCheckpointId(checkpointedBlockCheckpoint),
|
|
475
|
-
},
|
|
476
|
-
};
|
|
477
|
-
|
|
478
|
-
return l2Tips;
|
|
425
|
+
public getL2Tips(): Promise<L2Tips> {
|
|
426
|
+
return this.l2TipsCache.getL2Tips();
|
|
479
427
|
}
|
|
480
428
|
|
|
481
429
|
public async rollbackTo(targetL2BlockNumber: BlockNumber): Promise<void> {
|
|
482
|
-
// TODO(pw/mbps): This still assumes 1 block per checkpoint
|
|
483
430
|
const currentBlocks = await this.getL2Tips();
|
|
484
431
|
const currentL2Block = currentBlocks.proposed.number;
|
|
485
432
|
const currentProvenBlock = currentBlocks.proven.block.number;
|
|
@@ -491,8 +438,25 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
|
|
|
491
438
|
if (!targetL2Block) {
|
|
492
439
|
throw new Error(`Target L2 block ${targetL2BlockNumber} not found`);
|
|
493
440
|
}
|
|
494
|
-
const targetL1BlockNumber = targetL2Block.l1.blockNumber;
|
|
495
441
|
const targetCheckpointNumber = targetL2Block.checkpointNumber;
|
|
442
|
+
|
|
443
|
+
// Rollback operates at checkpoint granularity: the target block must be the last block of its checkpoint.
|
|
444
|
+
const checkpointData = await this.store.getCheckpointData(targetCheckpointNumber);
|
|
445
|
+
if (checkpointData) {
|
|
446
|
+
const lastBlockInCheckpoint = BlockNumber(checkpointData.startBlock + checkpointData.blockCount - 1);
|
|
447
|
+
if (targetL2BlockNumber !== lastBlockInCheckpoint) {
|
|
448
|
+
const previousCheckpointBoundary =
|
|
449
|
+
checkpointData.startBlock > 1 ? BlockNumber(checkpointData.startBlock - 1) : BlockNumber(0);
|
|
450
|
+
throw new Error(
|
|
451
|
+
`Target L2 block ${targetL2BlockNumber} is not at a checkpoint boundary. ` +
|
|
452
|
+
`Checkpoint ${targetCheckpointNumber} spans blocks ${checkpointData.startBlock} to ${lastBlockInCheckpoint}. ` +
|
|
453
|
+
`Use block ${lastBlockInCheckpoint} to roll back to this checkpoint, ` +
|
|
454
|
+
`or block ${previousCheckpointBoundary} to roll back to the previous one.`,
|
|
455
|
+
);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
const targetL1BlockNumber = targetL2Block.l1.blockNumber;
|
|
496
460
|
const targetL1Block = await this.publicClient.getBlock({
|
|
497
461
|
blockNumber: targetL1BlockNumber,
|
|
498
462
|
includeTransactions: false,
|
|
@@ -511,13 +475,13 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
|
|
|
511
475
|
await this.store.setCheckpointSynchedL1BlockNumber(targetL1BlockNumber);
|
|
512
476
|
await this.store.setMessageSynchedL1Block({ l1BlockNumber: targetL1BlockNumber, l1BlockHash: targetL1BlockHash });
|
|
513
477
|
if (targetL2BlockNumber < currentProvenBlock) {
|
|
514
|
-
this.log.info(`
|
|
515
|
-
await this.
|
|
478
|
+
this.log.info(`Rolling back proven L2 checkpoint to ${targetCheckpointNumber}`);
|
|
479
|
+
await this.updater.setProvenCheckpointNumber(targetCheckpointNumber);
|
|
480
|
+
}
|
|
481
|
+
const currentFinalizedBlock = currentBlocks.finalized.block.number;
|
|
482
|
+
if (targetL2BlockNumber < currentFinalizedBlock) {
|
|
483
|
+
this.log.info(`Rolling back finalized L2 checkpoint to ${targetCheckpointNumber}`);
|
|
484
|
+
await this.updater.setFinalizedCheckpointNumber(targetCheckpointNumber);
|
|
516
485
|
}
|
|
517
|
-
// TODO(palla/reorg): Set the finalized block when we add support for it.
|
|
518
|
-
// if (targetL2BlockNumber < currentFinalizedBlock) {
|
|
519
|
-
// this.log.info(`Clearing finalized L2 block number`);
|
|
520
|
-
// await this.store.setFinalizedL2BlockNumber(0);
|
|
521
|
-
// }
|
|
522
486
|
}
|
|
523
487
|
}
|
package/src/errors.ts
CHANGED
|
@@ -6,24 +6,9 @@ export class NoBlobBodiesFoundError extends Error {
|
|
|
6
6
|
}
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
-
export class InitialBlockNumberNotSequentialError extends Error {
|
|
10
|
-
constructor(
|
|
11
|
-
public readonly newBlockNumber: number,
|
|
12
|
-
public readonly previousBlockNumber: number | undefined,
|
|
13
|
-
) {
|
|
14
|
-
super(
|
|
15
|
-
`Cannot insert new block ${newBlockNumber} given previous block number in store is ${
|
|
16
|
-
previousBlockNumber ?? 'undefined'
|
|
17
|
-
}`,
|
|
18
|
-
);
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
9
|
export class BlockNumberNotSequentialError extends Error {
|
|
23
10
|
constructor(newBlockNumber: number, previous: number | undefined) {
|
|
24
|
-
super(
|
|
25
|
-
`Cannot insert new block ${newBlockNumber} given previous block number in batch is ${previous ?? 'undefined'}`,
|
|
26
|
-
);
|
|
11
|
+
super(`Cannot insert new block ${newBlockNumber} given previous block number is ${previous ?? 'undefined'}`);
|
|
27
12
|
}
|
|
28
13
|
}
|
|
29
14
|
|
|
@@ -48,14 +33,6 @@ export class CheckpointNumberNotSequentialError extends Error {
|
|
|
48
33
|
}
|
|
49
34
|
}
|
|
50
35
|
|
|
51
|
-
export class CheckpointNumberNotConsistentError extends Error {
|
|
52
|
-
constructor(newCheckpointNumber: number, previous: number | undefined) {
|
|
53
|
-
super(
|
|
54
|
-
`Cannot insert block for new checkpoint ${newCheckpointNumber} given previous block was checkpoint ${previous ?? 'undefined'}`,
|
|
55
|
-
);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
36
|
export class BlockIndexNotSequentialError extends Error {
|
|
60
37
|
constructor(newBlockIndex: number, previousBlockIndex: number | undefined) {
|
|
61
38
|
super(
|
|
@@ -89,6 +66,15 @@ export class BlockNotFoundError extends Error {
|
|
|
89
66
|
}
|
|
90
67
|
}
|
|
91
68
|
|
|
69
|
+
/** Thrown when a proposed block matches a block that was already checkpointed. This is expected for late proposals. */
|
|
70
|
+
export class BlockAlreadyCheckpointedError extends Error {
|
|
71
|
+
constructor(public readonly blockNumber: number) {
|
|
72
|
+
super(`Block ${blockNumber} has already been checkpointed with the same content`);
|
|
73
|
+
this.name = 'BlockAlreadyCheckpointedError';
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Thrown when a proposed block conflicts with an already checkpointed block (different content). */
|
|
92
78
|
export class CannotOverwriteCheckpointedBlockError extends Error {
|
|
93
79
|
constructor(
|
|
94
80
|
public readonly blockNumber: number,
|
package/src/factory.ts
CHANGED
|
@@ -6,16 +6,14 @@ import { BlockNumber } from '@aztec/foundation/branded-types';
|
|
|
6
6
|
import { Buffer32 } from '@aztec/foundation/buffer';
|
|
7
7
|
import { merge } from '@aztec/foundation/collection';
|
|
8
8
|
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
9
|
-
import { createLogger } from '@aztec/foundation/log';
|
|
10
9
|
import { DateProvider } from '@aztec/foundation/timer';
|
|
11
|
-
import type { DataStoreConfig } from '@aztec/kv-store/config';
|
|
12
10
|
import { createStore } from '@aztec/kv-store/lmdb-v2';
|
|
13
11
|
import { protocolContractNames } from '@aztec/protocol-contracts';
|
|
14
12
|
import { BundledProtocolContractsProvider } from '@aztec/protocol-contracts/providers/bundle';
|
|
15
13
|
import { FunctionType, decodeFunctionSignature } from '@aztec/stdlib/abi';
|
|
16
14
|
import type { ArchiverEmitter } from '@aztec/stdlib/block';
|
|
17
15
|
import { type ContractClassPublic, computePublicBytecodeCommitment } from '@aztec/stdlib/contract';
|
|
18
|
-
import type {
|
|
16
|
+
import type { DataStoreConfig } from '@aztec/stdlib/kv-store';
|
|
19
17
|
import { getTelemetryClient } from '@aztec/telemetry-client';
|
|
20
18
|
|
|
21
19
|
import { EventEmitter } from 'events';
|
|
@@ -26,20 +24,20 @@ import { type ArchiverConfig, mapArchiverConfig } from './config.js';
|
|
|
26
24
|
import { ArchiverInstrumentation } from './modules/instrumentation.js';
|
|
27
25
|
import { ArchiverL1Synchronizer } from './modules/l1_synchronizer.js';
|
|
28
26
|
import { ARCHIVER_DB_VERSION, KVArchiverDataStore } from './store/kv_archiver_store.js';
|
|
27
|
+
import { L2TipsCache } from './store/l2_tips_cache.js';
|
|
29
28
|
|
|
30
29
|
export const ARCHIVER_STORE_NAME = 'archiver';
|
|
31
30
|
|
|
32
31
|
/** Creates an archiver store. */
|
|
33
32
|
export async function createArchiverStore(
|
|
34
33
|
userConfig: Pick<ArchiverConfig, 'archiverStoreMapSizeKb' | 'maxLogs'> & DataStoreConfig,
|
|
35
|
-
l1Constants: Pick<L1RollupConstants, 'epochDuration'>,
|
|
36
34
|
) {
|
|
37
35
|
const config = {
|
|
38
36
|
...userConfig,
|
|
39
37
|
dataStoreMapSizeKb: userConfig.archiverStoreMapSizeKb ?? userConfig.dataStoreMapSizeKb,
|
|
40
38
|
};
|
|
41
|
-
const store = await createStore(ARCHIVER_STORE_NAME, ARCHIVER_DB_VERSION, config
|
|
42
|
-
return new KVArchiverDataStore(store, config.maxLogs
|
|
39
|
+
const store = await createStore(ARCHIVER_STORE_NAME, ARCHIVER_DB_VERSION, config);
|
|
40
|
+
return new KVArchiverDataStore(store, config.maxLogs);
|
|
43
41
|
}
|
|
44
42
|
|
|
45
43
|
/**
|
|
@@ -54,7 +52,7 @@ export async function createArchiver(
|
|
|
54
52
|
deps: ArchiverDeps,
|
|
55
53
|
opts: { blockUntilSync: boolean } = { blockUntilSync: true },
|
|
56
54
|
): Promise<Archiver> {
|
|
57
|
-
const archiverStore = await createArchiverStore(config
|
|
55
|
+
const archiverStore = await createArchiverStore(config);
|
|
58
56
|
await registerProtocolContracts(archiverStore);
|
|
59
57
|
|
|
60
58
|
// Create Ethereum clients
|
|
@@ -78,14 +76,23 @@ export async function createArchiver(
|
|
|
78
76
|
const inbox = new InboxContract(publicClient, config.l1Contracts.inboxAddress);
|
|
79
77
|
|
|
80
78
|
// Fetch L1 constants from rollup contract
|
|
81
|
-
const [
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
79
|
+
const [
|
|
80
|
+
l1StartBlock,
|
|
81
|
+
l1GenesisTime,
|
|
82
|
+
proofSubmissionEpochs,
|
|
83
|
+
genesisArchiveRoot,
|
|
84
|
+
slashingProposerAddress,
|
|
85
|
+
targetCommitteeSize,
|
|
86
|
+
rollupManaLimit,
|
|
87
|
+
] = await Promise.all([
|
|
88
|
+
rollup.getL1StartBlock(),
|
|
89
|
+
rollup.getL1GenesisTime(),
|
|
90
|
+
rollup.getProofSubmissionEpochs(),
|
|
91
|
+
rollup.getGenesisArchiveTreeRoot(),
|
|
92
|
+
rollup.getSlashingProposerAddress(),
|
|
93
|
+
rollup.getTargetCommitteeSize(),
|
|
94
|
+
rollup.getManaLimit(),
|
|
95
|
+
] as const);
|
|
89
96
|
|
|
90
97
|
const l1StartBlockHash = await publicClient
|
|
91
98
|
.getBlock({ blockNumber: l1StartBlock, includeTransactions: false })
|
|
@@ -101,7 +108,9 @@ export async function createArchiver(
|
|
|
101
108
|
slotDuration,
|
|
102
109
|
ethereumSlotDuration,
|
|
103
110
|
proofSubmissionEpochs: Number(proofSubmissionEpochs),
|
|
111
|
+
targetCommitteeSize,
|
|
104
112
|
genesisArchiveRoot: Fr.fromString(genesisArchiveRoot.toString()),
|
|
113
|
+
rollupManaLimit: Number(rollupManaLimit),
|
|
105
114
|
};
|
|
106
115
|
|
|
107
116
|
const archiverConfig = merge(
|
|
@@ -121,13 +130,15 @@ export async function createArchiver(
|
|
|
121
130
|
// Create the event emitter that will be shared by archiver and synchronizer
|
|
122
131
|
const events = new EventEmitter() as ArchiverEmitter;
|
|
123
132
|
|
|
133
|
+
// Create L2 tips cache shared by archiver and synchronizer
|
|
134
|
+
const l2TipsCache = new L2TipsCache(archiverStore.blockStore);
|
|
135
|
+
|
|
124
136
|
// Create the L1 synchronizer
|
|
125
137
|
const synchronizer = new ArchiverL1Synchronizer(
|
|
126
138
|
publicClient,
|
|
127
139
|
debugClient,
|
|
128
140
|
rollup,
|
|
129
141
|
inbox,
|
|
130
|
-
{ ...config.l1Contracts, slashingProposerAddress },
|
|
131
142
|
archiverStore,
|
|
132
143
|
archiverConfig,
|
|
133
144
|
deps.blobClient,
|
|
@@ -137,6 +148,8 @@ export async function createArchiver(
|
|
|
137
148
|
l1Constants,
|
|
138
149
|
events,
|
|
139
150
|
instrumentation.tracer,
|
|
151
|
+
l2TipsCache,
|
|
152
|
+
undefined, // log (use default)
|
|
140
153
|
);
|
|
141
154
|
|
|
142
155
|
const archiver = new Archiver(
|
|
@@ -151,13 +164,15 @@ export async function createArchiver(
|
|
|
151
164
|
l1Constants,
|
|
152
165
|
synchronizer,
|
|
153
166
|
events,
|
|
167
|
+
l2TipsCache,
|
|
154
168
|
);
|
|
155
169
|
|
|
156
170
|
await archiver.start(opts.blockUntilSync);
|
|
157
171
|
return archiver;
|
|
158
172
|
}
|
|
159
173
|
|
|
160
|
-
|
|
174
|
+
/** Registers protocol contracts in the archiver store. */
|
|
175
|
+
export async function registerProtocolContracts(store: KVArchiverDataStore) {
|
|
161
176
|
const blockNumber = 0;
|
|
162
177
|
for (const name of protocolContractNames) {
|
|
163
178
|
const provider = new BundledProtocolContractsProvider();
|
package/src/index.ts
CHANGED
|
@@ -8,5 +8,6 @@ export * from './config.js';
|
|
|
8
8
|
export { type L1PublishedData } from './structs/published.js';
|
|
9
9
|
export { KVArchiverDataStore, ARCHIVER_DB_VERSION } from './store/kv_archiver_store.js';
|
|
10
10
|
export { ContractInstanceStore } from './store/contract_instance_store.js';
|
|
11
|
+
export { L2TipsCache } from './store/l2_tips_cache.js';
|
|
11
12
|
|
|
12
13
|
export { retrieveCheckpointsFromRollup, retrieveL2ProofVerifiedEvents } from './l1/data_retrieval.js';
|
package/src/l1/README.md
CHANGED
|
@@ -5,29 +5,27 @@ Modules and classes to handle data retrieval from L1 for the archiver.
|
|
|
5
5
|
## Calldata Retriever
|
|
6
6
|
|
|
7
7
|
The sequencer publisher bundles multiple operations into a single multicall3 transaction for gas
|
|
8
|
-
efficiency.
|
|
8
|
+
efficiency. The archiver needs to extract the `propose` calldata from these bundled transactions
|
|
9
|
+
to reconstruct L2 blocks.
|
|
9
10
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
The retriever uses hash matching against `attestationsHash` and `payloadDigest` from the
|
|
12
|
+
`CheckpointProposed` L1 event to verify it has found the correct propose calldata. These hashes
|
|
13
|
+
are always required.
|
|
13
14
|
|
|
14
|
-
|
|
15
|
-
L2 blocks. This class needs to handle scenarios where the transaction was submitted via multicall3,
|
|
16
|
-
as well as alternative ways for submitting the `propose` call that other clients might use.
|
|
15
|
+
### Multicall3 Decoding with Hash Matching
|
|
17
16
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
First attempt to decode the transaction as a multicall3 `aggregate3` call with validation:
|
|
17
|
+
First attempt to decode the transaction as a multicall3 `aggregate3` call:
|
|
21
18
|
|
|
22
19
|
- Check if transaction is to multicall3 address (`0xcA11bde05977b3631167028862bE2a173976CA11`)
|
|
23
20
|
- Decode as `aggregate3(Call3[] calldata calls)`
|
|
24
|
-
-
|
|
25
|
-
-
|
|
26
|
-
|
|
27
|
-
|
|
21
|
+
- Find all calls matching the rollup contract address and the `propose` function selector
|
|
22
|
+
- Verify each candidate by computing `attestationsHash` (keccak256 of ABI-encoded attestations)
|
|
23
|
+
and `payloadDigest` (keccak256 of the consensus payload signing hash) and comparing against
|
|
24
|
+
expected values from the `CheckpointProposed` event
|
|
25
|
+
- Return the verified candidate (if multiple verify, return the first with a warning)
|
|
28
26
|
|
|
29
|
-
This
|
|
30
|
-
|
|
27
|
+
This approach works regardless of what other calls are in the multicall3 bundle, because hash
|
|
28
|
+
matching identifies the correct propose call without needing an allowlist.
|
|
31
29
|
|
|
32
30
|
### Direct Propose Call
|
|
33
31
|
|
|
@@ -35,64 +33,23 @@ Second attempt to decode the transaction as a direct `propose` call to the rollu
|
|
|
35
33
|
|
|
36
34
|
- Check if transaction is to the rollup address
|
|
37
35
|
- Decode as `propose` function call
|
|
38
|
-
- Verify
|
|
36
|
+
- Verify against expected hashes
|
|
39
37
|
- Return the transaction input as the propose calldata
|
|
40
38
|
|
|
41
|
-
This handles scenarios where clients submit transactions directly to the rollup contract without
|
|
42
|
-
using multicall3 for bundling. Any validation failure triggers fallback to the next step.
|
|
43
|
-
|
|
44
39
|
### Spire Proposer Call
|
|
45
40
|
|
|
46
|
-
Given existing attempts to route the call via the Spire proposer, we also check if the tx is
|
|
47
|
-
proposer known address
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
Similar as with the multicall3 check, we check that there are no other calls in the Spire proposer, so
|
|
51
|
-
we are absolutely sure that the only call is the successful one to the rollup. Any extraneous call would
|
|
52
|
-
imply an unexpected path to calling `propose` in the rollup contract, and since we cannot verify if the
|
|
53
|
-
calldata arguments we extracted are the correct ones (see the section below), we cannot know for sure which
|
|
54
|
-
one is the call that succeeded, so we don't know which calldata to process.
|
|
55
|
-
|
|
56
|
-
Furthermore, since the Spire proposer is upgradeable, we check if the implementation has not changed in
|
|
57
|
-
order to decode. As usual, any validation failure triggers fallback to the next step.
|
|
58
|
-
|
|
59
|
-
### Verifying Multicall3 Arguments
|
|
60
|
-
|
|
61
|
-
**This is NOT implemented for simplicity's sake**
|
|
62
|
-
|
|
63
|
-
If the checks above don't hold, such as when there are multiple calls to `propose`, then we cannot
|
|
64
|
-
reliably extract the `propose` calldata from the multicall3 arguments alone. We can try a best-effort
|
|
65
|
-
where we try all `propose` calls we see and validate them against on-chain data. Note that we can use these
|
|
66
|
-
same strategies if we were to obtain the calldata from another source.
|
|
67
|
-
|
|
68
|
-
#### TempBlockLog Verification
|
|
69
|
-
|
|
70
|
-
Read the stored `TempBlockLog` for the L2 block number from L1 and verify it matches our decoded header hash,
|
|
71
|
-
since the `TempBlockLog` stores the hash of the proposed block header, the payload commitment, and the attestations.
|
|
72
|
-
|
|
73
|
-
However, `TempBlockLog` is only stored temporarily and deleted after proven, so this method only works for recent
|
|
74
|
-
blocks, not for historical data syncing.
|
|
75
|
-
|
|
76
|
-
#### Archive Verification
|
|
77
|
-
|
|
78
|
-
Verify that the archive root in the decoded propose is correct with regard to the block header. This requires
|
|
79
|
-
hashing the block header we have retrieved, inserting it into the archive tree, and checking the resulting root
|
|
80
|
-
against the one we got from L1.
|
|
81
|
-
|
|
82
|
-
However, this requires that the archive keeps a reference to world-state, which is not the case in the current
|
|
83
|
-
system.
|
|
84
|
-
|
|
85
|
-
#### Emit Commitments in Rollup Contract
|
|
86
|
-
|
|
87
|
-
Modify rollup contract to emit commitments to the block header in the `L2BlockProposed` event, allowing us to easily
|
|
88
|
-
verify the calldata we obtained vs the emitted event.
|
|
41
|
+
Given existing attempts to route the call via the Spire proposer, we also check if the tx is
|
|
42
|
+
`to` the proposer known address. If so, we extract all wrapped calls and try each as either
|
|
43
|
+
a multicall3 or direct propose call, using hash matching to find and verify the correct one.
|
|
89
44
|
|
|
90
|
-
|
|
45
|
+
Since the Spire proposer is upgradeable, we check that the implementation has not changed in
|
|
46
|
+
order to decode. Any validation failure triggers fallback to the next step.
|
|
91
47
|
|
|
92
48
|
### Debug and Trace Transaction Fallback
|
|
93
49
|
|
|
94
|
-
Last, we use L1 node's trace/debug RPC methods to definitively identify the one successful
|
|
95
|
-
We can then extract the exact calldata that hit the `propose`
|
|
50
|
+
Last, we use L1 node's trace/debug RPC methods to definitively identify the one successful
|
|
51
|
+
`propose` call within the tx. We can then extract the exact calldata that hit the `propose`
|
|
52
|
+
function in the rollup contract.
|
|
96
53
|
|
|
97
|
-
This approach requires access to a debug-enabled L1 node, which may be more resource-intensive,
|
|
98
|
-
use it as a fallback when
|
|
54
|
+
This approach requires access to a debug-enabled L1 node, which may be more resource-intensive,
|
|
55
|
+
so we only use it as a fallback when earlier steps fail, which should be rare in practice.
|