@aztec/archiver 0.0.1-commit.2448fdb → 0.0.1-commit.2606882
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/README.md +19 -11
- package/dest/archiver.d.ts +53 -16
- package/dest/archiver.d.ts.map +1 -1
- package/dest/archiver.js +229 -68
- package/dest/config.d.ts +4 -1
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +21 -14
- package/dest/errors.d.ts +50 -2
- package/dest/errors.d.ts.map +1 -1
- package/dest/errors.js +74 -2
- package/dest/factory.d.ts +13 -7
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +33 -24
- package/dest/index.d.ts +11 -3
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +10 -2
- package/dest/l1/calldata_retriever.d.ts +2 -1
- package/dest/l1/calldata_retriever.d.ts.map +1 -1
- package/dest/l1/calldata_retriever.js +15 -5
- package/dest/l1/data_retrieval.d.ts +24 -12
- package/dest/l1/data_retrieval.d.ts.map +1 -1
- package/dest/l1/data_retrieval.js +36 -37
- package/dest/l1/trace_tx.d.ts +12 -66
- package/dest/l1/trace_tx.d.ts.map +1 -1
- package/dest/l1/validate_historical_logs.d.ts +23 -0
- package/dest/l1/validate_historical_logs.d.ts.map +1 -0
- package/dest/l1/validate_historical_logs.js +108 -0
- package/dest/modules/contract_data_source_adapter.d.ts +25 -0
- package/dest/modules/contract_data_source_adapter.d.ts.map +1 -0
- package/dest/modules/contract_data_source_adapter.js +40 -0
- package/dest/modules/data_source_base.d.ts +71 -47
- package/dest/modules/data_source_base.d.ts.map +1 -1
- package/dest/modules/data_source_base.js +269 -137
- package/dest/modules/data_store_updater.d.ts +30 -13
- package/dest/modules/data_store_updater.d.ts.map +1 -1
- package/dest/modules/data_store_updater.js +126 -109
- package/dest/modules/instrumentation.d.ts +18 -2
- package/dest/modules/instrumentation.d.ts.map +1 -1
- package/dest/modules/instrumentation.js +35 -7
- package/dest/modules/l1_synchronizer.d.ts +11 -6
- package/dest/modules/l1_synchronizer.d.ts.map +1 -1
- package/dest/modules/l1_synchronizer.js +418 -216
- package/dest/modules/outbox_trees_resolver.d.ts +62 -0
- package/dest/modules/outbox_trees_resolver.d.ts.map +1 -0
- package/dest/modules/outbox_trees_resolver.js +162 -0
- package/dest/modules/validation.d.ts +4 -3
- package/dest/modules/validation.d.ts.map +1 -1
- package/dest/modules/validation.js +6 -6
- package/dest/store/block_store.d.ts +164 -66
- package/dest/store/block_store.d.ts.map +1 -1
- package/dest/store/block_store.js +605 -235
- package/dest/store/contract_class_store.d.ts +17 -4
- package/dest/store/contract_class_store.d.ts.map +1 -1
- package/dest/store/contract_class_store.js +18 -66
- package/dest/store/contract_instance_store.d.ts +28 -1
- package/dest/store/contract_instance_store.d.ts.map +1 -1
- package/dest/store/contract_instance_store.js +31 -0
- package/dest/store/data_stores.d.ts +68 -0
- package/dest/store/data_stores.d.ts.map +1 -0
- package/dest/store/data_stores.js +54 -0
- package/dest/store/function_names_cache.d.ts +17 -0
- package/dest/store/function_names_cache.d.ts.map +1 -0
- package/dest/store/function_names_cache.js +30 -0
- package/dest/store/l2_tips_cache.d.ts +13 -7
- package/dest/store/l2_tips_cache.d.ts.map +1 -1
- package/dest/store/l2_tips_cache.js +13 -76
- package/dest/store/log_store.d.ts +42 -37
- package/dest/store/log_store.d.ts.map +1 -1
- package/dest/store/log_store.js +259 -443
- package/dest/store/log_store_codec.d.ts +70 -0
- package/dest/store/log_store_codec.d.ts.map +1 -0
- package/dest/store/log_store_codec.js +101 -0
- package/dest/store/message_store.d.ts +11 -1
- package/dest/store/message_store.d.ts.map +1 -1
- package/dest/store/message_store.js +50 -8
- package/dest/test/fake_l1_state.d.ts +8 -3
- package/dest/test/fake_l1_state.d.ts.map +1 -1
- package/dest/test/fake_l1_state.js +75 -18
- package/dest/test/mock_l1_to_l2_message_source.d.ts +1 -1
- package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
- package/dest/test/mock_l1_to_l2_message_source.js +2 -1
- package/dest/test/mock_l2_block_source.d.ts +51 -50
- package/dest/test/mock_l2_block_source.d.ts.map +1 -1
- package/dest/test/mock_l2_block_source.js +244 -181
- package/dest/test/noop_l1_archiver.d.ts +10 -6
- package/dest/test/noop_l1_archiver.d.ts.map +1 -1
- package/dest/test/noop_l1_archiver.js +22 -9
- package/package.json +14 -14
- package/src/archiver.ts +293 -75
- package/src/config.ts +24 -11
- package/src/errors.ts +117 -2
- package/src/factory.ts +40 -23
- package/src/index.ts +18 -2
- package/src/l1/calldata_retriever.ts +16 -5
- package/src/l1/data_retrieval.ts +52 -53
- package/src/l1/validate_historical_logs.ts +140 -0
- package/src/modules/contract_data_source_adapter.ts +55 -0
- package/src/modules/data_source_base.ts +341 -174
- package/src/modules/data_store_updater.ts +152 -137
- package/src/modules/instrumentation.ts +48 -8
- package/src/modules/l1_synchronizer.ts +556 -262
- package/src/modules/outbox_trees_resolver.ts +199 -0
- package/src/modules/validation.ts +10 -9
- package/src/store/block_store.ts +750 -285
- package/src/store/contract_class_store.ts +24 -100
- package/src/store/contract_instance_store.ts +43 -0
- package/src/store/data_stores.ts +104 -0
- package/src/store/function_names_cache.ts +37 -0
- package/src/store/l2_tips_cache.ts +16 -70
- package/src/store/log_store.ts +297 -609
- package/src/store/log_store_codec.ts +132 -0
- package/src/store/message_store.ts +59 -9
- package/src/structs/inbox_message.ts +1 -1
- package/src/test/fake_l1_state.ts +96 -29
- package/src/test/mock_l1_to_l2_message_source.ts +1 -0
- package/src/test/mock_l2_block_source.ts +299 -232
- package/src/test/noop_l1_archiver.ts +42 -9
- package/dest/store/kv_archiver_store.d.ts +0 -362
- package/dest/store/kv_archiver_store.d.ts.map +0 -1
- package/dest/store/kv_archiver_store.js +0 -475
- package/src/store/kv_archiver_store.ts +0 -685
|
@@ -371,32 +371,39 @@ function _apply_decs_2203_r(targetClass, memberDecs, classDecs, parentClass) {
|
|
|
371
371
|
return (_apply_decs_2203_r = applyDecs2203RFactory())(targetClass, memberDecs, classDecs, parentClass);
|
|
372
372
|
}
|
|
373
373
|
var _dec, _dec1, _dec2, _dec3, _initProto;
|
|
374
|
+
import { getFinalizedL1Block } from '@aztec/ethereum/queries';
|
|
375
|
+
import { asyncPool } from '@aztec/foundation/async-pool';
|
|
374
376
|
import { maxBigint } from '@aztec/foundation/bigint';
|
|
375
377
|
import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
|
|
376
|
-
import { Buffer32 } from '@aztec/foundation/buffer';
|
|
377
|
-
import { pick } from '@aztec/foundation/collection';
|
|
378
|
+
import { Buffer16, Buffer32 } from '@aztec/foundation/buffer';
|
|
379
|
+
import { compactArray, partition, pick } from '@aztec/foundation/collection';
|
|
380
|
+
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
378
381
|
import { createLogger } from '@aztec/foundation/log';
|
|
382
|
+
import { retryTimes } from '@aztec/foundation/retry';
|
|
379
383
|
import { count } from '@aztec/foundation/string';
|
|
380
384
|
import { Timer, elapsed } from '@aztec/foundation/timer';
|
|
381
|
-
import { isDefined } from '@aztec/foundation/types';
|
|
385
|
+
import { isDefined, isErrorClass } from '@aztec/foundation/types';
|
|
382
386
|
import { L2BlockSourceEvents } from '@aztec/stdlib/block';
|
|
387
|
+
import { Checkpoint, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
|
|
383
388
|
import { getEpochAtSlot, getSlotAtNextL1Block } from '@aztec/stdlib/epoch-helpers';
|
|
384
389
|
import { computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
|
|
385
390
|
import { execInSpan, trackSpan } from '@aztec/telemetry-client';
|
|
386
391
|
import { InitialCheckpointNumberNotSequentialError } from '../errors.js';
|
|
387
|
-
import {
|
|
392
|
+
import { getCheckpointBlobDataFromBlobs, retrieveCheckpointCalldataFromRollup, retrieveL1ToL2Message, retrieveL1ToL2Messages, retrievedToPublishedCheckpoint } from '../l1/data_retrieval.js';
|
|
393
|
+
import { getArchiverSynchPoint } from '../store/data_stores.js';
|
|
394
|
+
import { MessageStoreError } from '../store/message_store.js';
|
|
388
395
|
import { ArchiverDataStoreUpdater } from './data_store_updater.js';
|
|
389
396
|
import { validateCheckpointAttestations } from './validation.js';
|
|
390
397
|
_dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpochPrune'), _dec2 = trackSpan('Archiver.handleL1ToL2Messages'), _dec3 = trackSpan('Archiver.handleCheckpoints');
|
|
391
398
|
/**
|
|
392
399
|
* Handles L1 synchronization for the archiver.
|
|
393
|
-
* Responsible for fetching checkpoints, L1
|
|
400
|
+
* Responsible for fetching checkpoints, L1 to L2 messages, and handling L1 reorgs.
|
|
394
401
|
*/ export class ArchiverL1Synchronizer {
|
|
395
402
|
publicClient;
|
|
396
403
|
debugClient;
|
|
397
404
|
rollup;
|
|
398
405
|
inbox;
|
|
399
|
-
|
|
406
|
+
stores;
|
|
400
407
|
config;
|
|
401
408
|
blobClient;
|
|
402
409
|
epochCache;
|
|
@@ -434,12 +441,12 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
434
441
|
l1Timestamp;
|
|
435
442
|
updater;
|
|
436
443
|
tracer;
|
|
437
|
-
constructor(publicClient, debugClient, rollup, inbox,
|
|
444
|
+
constructor(publicClient, debugClient, rollup, inbox, stores, config, blobClient, epochCache, dateProvider, instrumentation, l1Constants, events, tracer, l2TipsCache, log = createLogger('archiver:l1-sync')){
|
|
438
445
|
this.publicClient = publicClient;
|
|
439
446
|
this.debugClient = debugClient;
|
|
440
447
|
this.rollup = rollup;
|
|
441
448
|
this.inbox = inbox;
|
|
442
|
-
this.
|
|
449
|
+
this.stores = stores;
|
|
443
450
|
this.config = config;
|
|
444
451
|
this.blobClient = blobClient;
|
|
445
452
|
this.epochCache = epochCache;
|
|
@@ -449,7 +456,7 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
449
456
|
this.events = events;
|
|
450
457
|
this.log = log;
|
|
451
458
|
_initProto(this);
|
|
452
|
-
this.updater = new ArchiverDataStoreUpdater(this.
|
|
459
|
+
this.updater = new ArchiverDataStoreUpdater(this.stores, l2TipsCache, {
|
|
453
460
|
rollupManaLimit: l1Constants.rollupManaLimit
|
|
454
461
|
});
|
|
455
462
|
this.tracer = tracer;
|
|
@@ -463,6 +470,12 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
463
470
|
/** Returns the last L1 timestamp that was synced. */ getL1Timestamp() {
|
|
464
471
|
return this.l1Timestamp;
|
|
465
472
|
}
|
|
473
|
+
getSignatureContext() {
|
|
474
|
+
return {
|
|
475
|
+
chainId: this.publicClient.chain.id,
|
|
476
|
+
rollupAddress: EthAddress.fromString(this.rollup.address)
|
|
477
|
+
};
|
|
478
|
+
}
|
|
466
479
|
/** Checks that the ethereum node we are connected to has a latest timestamp no more than the allowed drift. Throw if not. */ async testEthereumNodeSynced() {
|
|
467
480
|
const maxAllowedDelay = this.config.maxAllowedEthClientDriftSeconds;
|
|
468
481
|
if (maxAllowedDelay === 0) {
|
|
@@ -477,57 +490,52 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
477
490
|
}
|
|
478
491
|
}
|
|
479
492
|
async syncFromL1(initialSyncComplete) {
|
|
493
|
+
// In between the various calls to L1, the block number can move meaning some of the following
|
|
494
|
+
// calls will return data for blocks that were not present during earlier calls. To combat this
|
|
495
|
+
// we ensure that all data retrieval methods only retrieve data up to the currentBlockNumber
|
|
496
|
+
// captured at the top of this function.
|
|
480
497
|
const currentL1Block = await this.publicClient.getBlock({
|
|
481
498
|
includeTransactions: false
|
|
482
499
|
});
|
|
483
500
|
const currentL1BlockNumber = currentL1Block.number;
|
|
484
501
|
const currentL1BlockHash = Buffer32.fromString(currentL1Block.hash);
|
|
485
502
|
const currentL1Timestamp = currentL1Block.timestamp;
|
|
503
|
+
const currentL1BlockData = {
|
|
504
|
+
l1BlockNumber: currentL1BlockNumber,
|
|
505
|
+
l1BlockHash: currentL1BlockHash
|
|
506
|
+
};
|
|
486
507
|
if (this.l1BlockHash && currentL1BlockHash.equals(this.l1BlockHash)) {
|
|
487
508
|
this.log.trace(`No new L1 blocks since last sync at L1 block ${this.l1BlockNumber}`);
|
|
488
509
|
return;
|
|
489
510
|
}
|
|
490
|
-
//
|
|
511
|
+
// Log at error if the latest L1 block timestamp is too old
|
|
491
512
|
const maxAllowedDelay = this.config.maxAllowedEthClientDriftSeconds;
|
|
492
513
|
const now = this.dateProvider.nowInSeconds();
|
|
493
514
|
if (maxAllowedDelay > 0 && Number(currentL1Timestamp) <= now - maxAllowedDelay) {
|
|
494
|
-
this.log.
|
|
515
|
+
this.log.error(`Latest L1 block ${currentL1BlockNumber} timestamp ${currentL1Timestamp} is too old. Make sure your Ethereum node is synced.`, {
|
|
495
516
|
currentL1BlockNumber,
|
|
496
517
|
currentL1Timestamp,
|
|
497
518
|
now,
|
|
498
519
|
maxAllowedDelay
|
|
499
520
|
});
|
|
500
521
|
}
|
|
501
|
-
//
|
|
502
|
-
const
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
522
|
+
// Query finalized block on L1
|
|
523
|
+
const rawFinalizedL1Block = await getFinalizedL1Block(this.publicClient);
|
|
524
|
+
const finalizedL1Block = rawFinalizedL1Block && {
|
|
525
|
+
l1BlockNumber: rawFinalizedL1Block.number,
|
|
526
|
+
l1BlockHash: Buffer32.fromString(rawFinalizedL1Block.hash)
|
|
527
|
+
};
|
|
528
|
+
// Load sync point for blocks defaulting to start block
|
|
529
|
+
const { blocksSynchedTo = this.l1Constants.l1StartBlock } = await getArchiverSynchPoint(this.stores);
|
|
506
530
|
this.log.debug(`Starting new archiver sync iteration`, {
|
|
507
531
|
blocksSynchedTo,
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
currentL1BlockHash
|
|
532
|
+
currentL1BlockData,
|
|
533
|
+
finalizedL1Block
|
|
511
534
|
});
|
|
512
|
-
//
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
* to ensure that data is read exactly once.
|
|
517
|
-
*
|
|
518
|
-
* The first is the problem of eventually consistent ETH service providers like Infura.
|
|
519
|
-
* Each L1 read operation will query data from the last L1 block that it saw emit its kind of data.
|
|
520
|
-
* (so pending L1 to L2 messages will read from the last L1 block that emitted a message and so on)
|
|
521
|
-
* This will mean the archiver will lag behind L1 and will only advance when there's L2-relevant activity on the chain.
|
|
522
|
-
*
|
|
523
|
-
* The second is that in between the various calls to L1, the block number can move meaning some
|
|
524
|
-
* of the following calls will return data for blocks that were not present during earlier calls.
|
|
525
|
-
* To combat this for the time being we simply ensure that all data retrieval methods only retrieve
|
|
526
|
-
* data up to the currentBlockNumber captured at the top of this function. We might want to improve on this
|
|
527
|
-
* in future but for the time being it should give us the guarantees that we need
|
|
528
|
-
*/ // ********** Events that are processed per L1 block **********
|
|
529
|
-
await this.handleL1ToL2Messages(messagesSynchedTo, currentL1BlockNumber);
|
|
530
|
-
// ********** Events that are processed per checkpoint **********
|
|
535
|
+
// Sync L1 to L2 messages. We retry this a few times since there are error conditions that reset the sync point, requiring a new iteration.
|
|
536
|
+
// Note that we cannot just wait for the l1 synchronizer to loop again, since the synchronizer would report as synced up to the current L1
|
|
537
|
+
// block, when that wouldn't be the case, since L1 to L2 messages would need another iteration.
|
|
538
|
+
await retryTimes(()=>this.handleL1ToL2Messages(currentL1BlockData, finalizedL1Block), 'Handling L1 to L2 messages', 3, 0.1);
|
|
531
539
|
if (currentL1BlockNumber > blocksSynchedTo) {
|
|
532
540
|
// First we retrieve new checkpoints and L2 blocks and store them in the DB. This will also update the
|
|
533
541
|
// pending chain validation status, proven checkpoint number, and synched L1 block number.
|
|
@@ -540,23 +548,16 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
540
548
|
// the chain locally before we start unwinding stuff. This can be optimized by figuring out
|
|
541
549
|
// up to which point we're pruning, and then requesting checkpoints up to that point only.
|
|
542
550
|
const { rollupCanPrune } = await this.handleEpochPrune(rollupStatus.provenCheckpointNumber, currentL1BlockNumber, currentL1Timestamp);
|
|
543
|
-
// If the last checkpoint we processed had an invalid attestation, we manually advance the L1 syncpoint
|
|
544
|
-
// past it, since otherwise we'll keep downloading it and reprocessing it on every iteration until
|
|
545
|
-
// we get a valid checkpoint to advance the syncpoint.
|
|
546
|
-
if (!rollupStatus.validationResult?.valid && rollupStatus.lastL1BlockWithCheckpoint !== undefined) {
|
|
547
|
-
await this.store.setCheckpointSynchedL1BlockNumber(rollupStatus.lastL1BlockWithCheckpoint);
|
|
548
|
-
}
|
|
549
551
|
// And lastly we check if we are missing any checkpoints behind us due to a possible L1 reorg.
|
|
550
552
|
// We only do this if rollup cant prune on the next submission. Otherwise we will end up
|
|
551
|
-
// re-syncing the checkpoints we have just unwound above.
|
|
552
|
-
|
|
553
|
-
if (!rollupCanPrune && rollupStatus.validationResult?.valid) {
|
|
553
|
+
// re-syncing the checkpoints we have just unwound above.
|
|
554
|
+
if (!rollupCanPrune) {
|
|
554
555
|
await this.checkForNewCheckpointsBeforeL1SyncPoint(rollupStatus, blocksSynchedTo, currentL1BlockNumber);
|
|
555
556
|
}
|
|
556
557
|
this.instrumentation.updateL1BlockHeight(currentL1BlockNumber);
|
|
557
558
|
}
|
|
558
559
|
// Update the finalized L2 checkpoint based on L1 finality.
|
|
559
|
-
await this.updateFinalizedCheckpoint();
|
|
560
|
+
await this.updateFinalizedCheckpoint(finalizedL1Block);
|
|
560
561
|
// After syncing has completed, update the current l1 block number and timestamp,
|
|
561
562
|
// otherwise we risk announcing to the world that we've synced to a given point,
|
|
562
563
|
// but the corresponding blocks have not been processed (see #12631).
|
|
@@ -570,24 +571,21 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
570
571
|
l1BlockNumberAtEnd
|
|
571
572
|
});
|
|
572
573
|
}
|
|
573
|
-
/**
|
|
574
|
+
/** Updates the finalized checkpoint using the pre-fetched finalized L1 block from the current sync iteration. */ async updateFinalizedCheckpoint(finalizedL1Block) {
|
|
574
575
|
try {
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
}
|
|
579
|
-
const finalizedL1BlockNumber = finalizedL1Block.
|
|
576
|
+
if (!finalizedL1Block) {
|
|
577
|
+
this.log.trace(`Skipping finalized checkpoint update: L1 has no finalized block yet.`);
|
|
578
|
+
return;
|
|
579
|
+
}
|
|
580
|
+
const finalizedL1BlockNumber = finalizedL1Block.l1BlockNumber;
|
|
580
581
|
const finalizedCheckpointNumber = await this.rollup.getProvenCheckpointNumber({
|
|
581
582
|
blockNumber: finalizedL1BlockNumber
|
|
582
583
|
});
|
|
583
|
-
const localFinalizedCheckpointNumber = await this.
|
|
584
|
+
const localFinalizedCheckpointNumber = await this.stores.blocks.getFinalizedCheckpointNumber();
|
|
584
585
|
if (localFinalizedCheckpointNumber !== finalizedCheckpointNumber) {
|
|
585
586
|
await this.updater.setFinalizedCheckpointNumber(finalizedCheckpointNumber);
|
|
586
|
-
|
|
587
|
-
this.log.info(`Updated finalized chain to checkpoint ${finalizedCheckpointNumber} (L2 block ${finalizedL2BlockNumber})`, {
|
|
587
|
+
this.log.info(`Updated finalized chain to checkpoint ${finalizedCheckpointNumber}`, {
|
|
588
588
|
finalizedCheckpointNumber,
|
|
589
|
-
previousFinalizedCheckpointNumber: localFinalizedCheckpointNumber,
|
|
590
|
-
finalizedL2BlockNumber,
|
|
591
589
|
finalizedL1BlockNumber
|
|
592
590
|
});
|
|
593
591
|
}
|
|
@@ -600,34 +598,38 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
600
598
|
}
|
|
601
599
|
/** Prune all proposed local blocks that should have been checkpointed by now. */ async pruneUncheckpointedBlocks(currentL1Timestamp) {
|
|
602
600
|
const [lastCheckpointedBlockNumber, lastProposedBlockNumber] = await Promise.all([
|
|
603
|
-
this.
|
|
604
|
-
this.
|
|
601
|
+
this.stores.blocks.getCheckpointedL2BlockNumber(),
|
|
602
|
+
this.stores.blocks.getLatestL2BlockNumber()
|
|
605
603
|
]);
|
|
606
604
|
// If there are no uncheckpointed blocks, we got nothing to do
|
|
607
605
|
if (lastProposedBlockNumber === lastCheckpointedBlockNumber) {
|
|
608
606
|
this.log.trace(`No uncheckpointed blocks to prune.`);
|
|
609
607
|
return;
|
|
610
608
|
}
|
|
611
|
-
// What's the slot of the first uncheckpointed block?
|
|
612
|
-
const firstUncheckpointedBlockNumber = BlockNumber(lastCheckpointedBlockNumber + 1);
|
|
613
|
-
const [firstUncheckpointedBlockHeader] = await this.store.getBlockHeaders(firstUncheckpointedBlockNumber, 1);
|
|
614
|
-
const firstUncheckpointedBlockSlot = firstUncheckpointedBlockHeader?.getSlot();
|
|
615
609
|
// What's the slot at the next L1 block? All blocks for slots strictly before this one should've been checkpointed by now.
|
|
616
610
|
const slotAtNextL1Block = getSlotAtNextL1Block(currentL1Timestamp, this.l1Constants);
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
611
|
+
const firstUncheckpointedBlockNumber = BlockNumber(lastCheckpointedBlockNumber + 1);
|
|
612
|
+
// What's the slot of the first uncheckpointed block?
|
|
613
|
+
const firstUncheckpointedBlockData = await this.stores.blocks.getBlockData({
|
|
614
|
+
number: firstUncheckpointedBlockNumber
|
|
615
|
+
});
|
|
616
|
+
const firstUncheckpointedBlockSlot = firstUncheckpointedBlockData?.header.getSlot();
|
|
617
|
+
if (firstUncheckpointedBlockSlot === undefined || firstUncheckpointedBlockSlot >= slotAtNextL1Block) {
|
|
618
|
+
return;
|
|
619
|
+
}
|
|
620
|
+
// Prune provisional blocks from slots that have ended without being checkpointed.
|
|
621
|
+
// This also clears any proposed checkpoint whose blocks are being pruned.
|
|
622
|
+
this.log.warn(`Pruning blocks after block ${lastCheckpointedBlockNumber} due to slot ${firstUncheckpointedBlockSlot} not being checkpointed`, {
|
|
623
|
+
firstUncheckpointedBlockHeader: firstUncheckpointedBlockData?.header.toInspect(),
|
|
624
|
+
slotAtNextL1Block
|
|
625
|
+
});
|
|
626
|
+
const prunedBlocks = await this.updater.removeUncheckpointedBlocksAfter(lastCheckpointedBlockNumber);
|
|
627
|
+
if (prunedBlocks.length > 0) {
|
|
628
|
+
this.events.emit(L2BlockSourceEvents.L2PruneUncheckpointed, {
|
|
629
|
+
type: L2BlockSourceEvents.L2PruneUncheckpointed,
|
|
630
|
+
slotNumber: firstUncheckpointedBlockSlot,
|
|
631
|
+
blocks: prunedBlocks
|
|
622
632
|
});
|
|
623
|
-
const prunedBlocks = await this.updater.removeUncheckpointedBlocksAfter(lastCheckpointedBlockNumber);
|
|
624
|
-
if (prunedBlocks.length > 0) {
|
|
625
|
-
this.events.emit(L2BlockSourceEvents.L2PruneUncheckpointed, {
|
|
626
|
-
type: L2BlockSourceEvents.L2PruneUncheckpointed,
|
|
627
|
-
slotNumber: firstUncheckpointedBlockSlot,
|
|
628
|
-
blocks: prunedBlocks
|
|
629
|
-
});
|
|
630
|
-
}
|
|
631
633
|
}
|
|
632
634
|
}
|
|
633
635
|
/** Queries the rollup contract on whether a prune can be executed on the immediate next L1 block. */ async canPrune(currentL1BlockNumber, currentL1Timestamp) {
|
|
@@ -646,7 +648,7 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
646
648
|
}
|
|
647
649
|
/** Checks if there'd be a reorg for the next checkpoint submission and start pruning now. */ async handleEpochPrune(provenCheckpointNumber, currentL1BlockNumber, currentL1Timestamp) {
|
|
648
650
|
const rollupCanPrune = await this.canPrune(currentL1BlockNumber, currentL1Timestamp);
|
|
649
|
-
const localPendingCheckpointNumber = await this.
|
|
651
|
+
const localPendingCheckpointNumber = await this.stores.blocks.getLatestCheckpointNumber();
|
|
650
652
|
const canPrune = localPendingCheckpointNumber > provenCheckpointNumber && rollupCanPrune;
|
|
651
653
|
if (canPrune) {
|
|
652
654
|
const timer = new Timer();
|
|
@@ -658,12 +660,14 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
658
660
|
const pruneFromSlotNumber = header.slotNumber;
|
|
659
661
|
const pruneFromEpochNumber = getEpochAtSlot(pruneFromSlotNumber, this.l1Constants);
|
|
660
662
|
const checkpointsToUnwind = localPendingCheckpointNumber - provenCheckpointNumber;
|
|
661
|
-
|
|
663
|
+
// Fetch checkpoints and blocks in bounded batches to avoid unbounded concurrent
|
|
664
|
+
// promises when the gap between local pending and proven checkpoint numbers is large.
|
|
665
|
+
const BATCH_SIZE = 10;
|
|
666
|
+
const indices = Array.from({
|
|
662
667
|
length: checkpointsToUnwind
|
|
663
|
-
}
|
|
664
|
-
const checkpoints = await
|
|
665
|
-
const
|
|
666
|
-
const newBlocks = blockPromises.filter(isDefined).flat();
|
|
668
|
+
}, (_, i)=>CheckpointNumber(i + pruneFrom));
|
|
669
|
+
const checkpoints = (await asyncPool(BATCH_SIZE, indices, (idx)=>this.stores.blocks.getCheckpointData(idx))).filter(isDefined);
|
|
670
|
+
const newBlocks = (await asyncPool(BATCH_SIZE, checkpoints, (cp)=>this.stores.blocks.getBlocksForCheckpoint(CheckpointNumber(cp.checkpointNumber)))).filter(isDefined).flat();
|
|
667
671
|
// Emit an event for listening services to react to the chain prune
|
|
668
672
|
this.events.emit(L2BlockSourceEvents.L2PruneUnproven, {
|
|
669
673
|
type: L2BlockSourceEvents.L2PruneUnproven,
|
|
@@ -672,11 +676,11 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
672
676
|
});
|
|
673
677
|
this.log.debug(`L2 prune from ${provenCheckpointNumber + 1} to ${localPendingCheckpointNumber} will occur on next checkpoint submission.`);
|
|
674
678
|
await this.updater.removeCheckpointsAfter(provenCheckpointNumber);
|
|
675
|
-
this.log.warn(`Removed ${count(checkpointsToUnwind, 'checkpoint')} after checkpoint ${provenCheckpointNumber} ` + `due to predicted reorg at L1 block ${currentL1BlockNumber}. ` + `Updated latest checkpoint is ${await this.
|
|
679
|
+
this.log.warn(`Removed ${count(checkpointsToUnwind, 'checkpoint')} after checkpoint ${provenCheckpointNumber} ` + `due to predicted reorg at L1 block ${currentL1BlockNumber}. ` + `Updated latest checkpoint is ${await this.stores.blocks.getLatestCheckpointNumber()}.`);
|
|
676
680
|
this.instrumentation.processPrune(timer.ms());
|
|
677
681
|
// TODO(palla/reorg): Do we need to set the block synched L1 block number here?
|
|
678
682
|
// Seems like the next iteration should handle this.
|
|
679
|
-
// await this.
|
|
683
|
+
// await this.stores.blocks.setSynchedL1BlockNumber(currentL1BlockNumber);
|
|
680
684
|
}
|
|
681
685
|
return {
|
|
682
686
|
rollupCanPrune
|
|
@@ -697,59 +701,79 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
697
701
|
nextEnd
|
|
698
702
|
];
|
|
699
703
|
}
|
|
700
|
-
async handleL1ToL2Messages(
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
+
async handleL1ToL2Messages(currentL1Block, finalizedL1Block) {
|
|
705
|
+
// Load the syncpoint, which may have been updated in a previous iteration
|
|
706
|
+
const { messagesSynchedTo = {
|
|
707
|
+
l1BlockNumber: this.l1Constants.l1StartBlock,
|
|
708
|
+
l1BlockHash: this.l1Constants.l1StartBlockHash
|
|
709
|
+
} } = await getArchiverSynchPoint(this.stores);
|
|
710
|
+
// Nothing to do if L1 block number has not moved forward
|
|
711
|
+
const currentL1BlockNumber = currentL1Block.l1BlockNumber;
|
|
712
|
+
if (currentL1BlockNumber <= messagesSynchedTo.l1BlockNumber) {
|
|
713
|
+
return true;
|
|
704
714
|
}
|
|
705
|
-
//
|
|
706
|
-
const localMessagesInserted = await this.store.getTotalL1ToL2MessageCount();
|
|
707
|
-
const localLastMessage = await this.store.getLastL1ToL2Message();
|
|
715
|
+
// Compare local message store state with the remote. If they match, we just advance the match pointer.
|
|
708
716
|
const remoteMessagesState = await this.inbox.getState({
|
|
709
717
|
blockNumber: currentL1BlockNumber
|
|
710
718
|
});
|
|
711
|
-
this.
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
remoteMessagesState
|
|
715
|
-
|
|
716
|
-
// Compare message count and rolling hash. If they match, no need to retrieve anything.
|
|
717
|
-
if (remoteMessagesState.totalMessagesInserted === localMessagesInserted && remoteMessagesState.messagesRollingHash.equals(localLastMessage?.rollingHash ?? Buffer32.ZERO)) {
|
|
718
|
-
this.log.trace(`No L1 to L2 messages to query between L1 blocks ${messagesSyncPoint.l1BlockNumber} and ${currentL1BlockNumber}.`);
|
|
719
|
-
return;
|
|
719
|
+
const localLastMessage = await this.stores.messages.getLastMessage();
|
|
720
|
+
if (await this.localStateMatches(localLastMessage, remoteMessagesState)) {
|
|
721
|
+
this.log.trace(`Local L1 to L2 messages are already in sync with remote at L1 block ${currentL1BlockNumber}`);
|
|
722
|
+
await this.stores.messages.setMessageSyncState(currentL1Block, remoteMessagesState.treeInProgress, finalizedL1Block);
|
|
723
|
+
return true;
|
|
720
724
|
}
|
|
721
|
-
//
|
|
722
|
-
//
|
|
723
|
-
//
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
this.
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
this.log.warn(`Rolling back L1 to L2 messages due to hash mismatch or msg not found.`, {
|
|
732
|
-
remoteLastMessage,
|
|
733
|
-
messagesSyncPoint,
|
|
734
|
-
localLastMessage
|
|
735
|
-
});
|
|
736
|
-
messagesSyncPoint = await this.rollbackL1ToL2Messages(localLastMessage, messagesSyncPoint);
|
|
737
|
-
this.log.debug(`Rolled back L1 to L2 messages to L1 block ${messagesSyncPoint.l1BlockNumber}.`, {
|
|
738
|
-
messagesSyncPoint
|
|
725
|
+
// If not, then we are out of sync. Most likely there are new messages on the inbox, so we try retrieving them.
|
|
726
|
+
// However, it could also be the case that there was an L1 reorg and our syncpoint is no longer valid.
|
|
727
|
+
// If that's the case, we'd get an exception out of the message store since the rolling hash of the first message
|
|
728
|
+
// we try to insert would not match the one in the db, in which case we rollback to the last common message with L1.
|
|
729
|
+
try {
|
|
730
|
+
await this.retrieveAndStoreMessages(messagesSynchedTo.l1BlockNumber, currentL1BlockNumber);
|
|
731
|
+
} catch (error) {
|
|
732
|
+
if (isErrorClass(error, MessageStoreError)) {
|
|
733
|
+
this.log.warn(`Failed to store L1 to L2 messages retrieved from L1: ${error.message}. Rolling back syncpoint to retry.`, {
|
|
734
|
+
inboxMessage: error.inboxMessage
|
|
739
735
|
});
|
|
736
|
+
await this.rollbackL1ToL2Messages(remoteMessagesState);
|
|
737
|
+
return false;
|
|
740
738
|
}
|
|
739
|
+
throw error;
|
|
740
|
+
}
|
|
741
|
+
// Note that, if there are no new messages to insert, but there was an L1 reorg that pruned out last messages,
|
|
742
|
+
// we'd notice by comparing our local state with the remote one again, and seeing they don't match even after
|
|
743
|
+
// our sync attempt. In this case, we also rollback our syncpoint, and trigger a retry.
|
|
744
|
+
const localLastMessageAfterSync = await this.stores.messages.getLastMessage();
|
|
745
|
+
if (!await this.localStateMatches(localLastMessageAfterSync, remoteMessagesState)) {
|
|
746
|
+
this.log.warn(`Local L1 to L2 messages state does not match remote after sync attempt. Rolling back syncpoint to retry.`, {
|
|
747
|
+
localLastMessageAfterSync,
|
|
748
|
+
remoteMessagesState
|
|
749
|
+
});
|
|
750
|
+
await this.rollbackL1ToL2Messages(remoteMessagesState);
|
|
751
|
+
return false;
|
|
741
752
|
}
|
|
742
|
-
//
|
|
753
|
+
// Advance the syncpoint after a successful sync
|
|
754
|
+
await this.stores.messages.setMessageSyncState(currentL1Block, remoteMessagesState.treeInProgress, finalizedL1Block);
|
|
755
|
+
return true;
|
|
756
|
+
}
|
|
757
|
+
/** Checks if the local rolling hash and message count matches the remote state */ async localStateMatches(localLastMessage, remoteState) {
|
|
758
|
+
const localMessageCount = await this.stores.messages.getTotalL1ToL2MessageCount();
|
|
759
|
+
this.log.trace(`Comparing local and remote inbox state`, {
|
|
760
|
+
localMessageCount,
|
|
761
|
+
localLastMessage,
|
|
762
|
+
remoteState
|
|
763
|
+
});
|
|
764
|
+
return remoteState.totalMessagesInserted === localMessageCount && remoteState.messagesRollingHash.equals(localLastMessage?.rollingHash ?? Buffer16.ZERO);
|
|
765
|
+
}
|
|
766
|
+
/** Retrieves L1 to L2 messages from L1 in batches and stores them. */ async retrieveAndStoreMessages(fromL1Block, toL1Block) {
|
|
743
767
|
let searchStartBlock = 0n;
|
|
744
|
-
let searchEndBlock =
|
|
768
|
+
let searchEndBlock = fromL1Block;
|
|
745
769
|
let lastMessage;
|
|
746
770
|
let messageCount = 0;
|
|
747
771
|
do {
|
|
748
|
-
[searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock,
|
|
772
|
+
[searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, toL1Block);
|
|
749
773
|
this.log.trace(`Retrieving L1 to L2 messages in L1 blocks ${searchStartBlock}-${searchEndBlock}`);
|
|
750
774
|
const messages = await retrieveL1ToL2Messages(this.inbox, searchStartBlock, searchEndBlock);
|
|
751
775
|
const timer = new Timer();
|
|
752
|
-
await this.
|
|
776
|
+
await this.stores.messages.addL1ToL2Messages(messages);
|
|
753
777
|
const perMsg = timer.ms() / messages.length;
|
|
754
778
|
this.instrumentation.processNewMessages(messages.length, perMsg);
|
|
755
779
|
for (const msg of messages){
|
|
@@ -760,74 +784,94 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
760
784
|
lastMessage = msg;
|
|
761
785
|
messageCount++;
|
|
762
786
|
}
|
|
763
|
-
}while (searchEndBlock <
|
|
764
|
-
// Log stats for messages retrieved (if any).
|
|
787
|
+
}while (searchEndBlock < toL1Block)
|
|
765
788
|
if (messageCount > 0) {
|
|
766
789
|
this.log.info(`Retrieved ${messageCount} new L1 to L2 messages up to message with index ${lastMessage?.index} for checkpoint ${lastMessage?.checkpointNumber}`, {
|
|
767
790
|
lastMessage,
|
|
768
791
|
messageCount
|
|
769
792
|
});
|
|
770
793
|
}
|
|
771
|
-
// Warn if the resulting rolling hash does not match the remote state we had retrieved.
|
|
772
|
-
if (lastMessage && !lastMessage.rollingHash.equals(remoteMessagesState.messagesRollingHash)) {
|
|
773
|
-
this.log.warn(`Last message retrieved rolling hash does not match remote state.`, {
|
|
774
|
-
lastMessage,
|
|
775
|
-
remoteMessagesState
|
|
776
|
-
});
|
|
777
|
-
}
|
|
778
794
|
}
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
}while (searchEndBlock < currentL1BlockNumber)
|
|
790
|
-
return undefined;
|
|
791
|
-
}
|
|
792
|
-
async rollbackL1ToL2Messages(localLastMessage, messagesSyncPoint) {
|
|
793
|
-
// Slowly go back through our messages until we find the last common message.
|
|
794
|
-
// We could query the logs in batch as an optimization, but the depth of the reorg should not be deep, and this
|
|
795
|
-
// is a very rare case, so it's fine to query one log at a time.
|
|
795
|
+
/**
|
|
796
|
+
* Rolls back local L1 to L2 messages to the last common message with L1, and updates the syncpoint to the L1 block of that message.
|
|
797
|
+
* If no common message is found, rolls back all messages and sets the syncpoint to the start block.
|
|
798
|
+
*/ async rollbackL1ToL2Messages(remoteMessagesState) {
|
|
799
|
+
const { treeInProgress: remoteTreeInProgress, messagesRollingHash: remoteRollingHash } = remoteMessagesState;
|
|
800
|
+
const messagesFinalizedL1Block = await this.stores.messages.getMessagesFinalizedL1Block();
|
|
801
|
+
const finalizedL1BlockNumber = messagesFinalizedL1Block?.l1BlockNumber;
|
|
802
|
+
// Slowly go back through our messages until we find the last common message. We could query the logs in
|
|
803
|
+
// batch as an optimization, but the depth of the reorg should not be deep, and this is a very rare case,
|
|
804
|
+
// so it's fine to query one log at a time.
|
|
796
805
|
let commonMsg;
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
806
|
+
let messagesToDelete = 0;
|
|
807
|
+
this.log.verbose(`Searching most recent common L1 to L2 message`);
|
|
808
|
+
for await (const localMsg of this.stores.messages.iterateL1ToL2Messages({
|
|
809
|
+
reverse: true
|
|
801
810
|
})){
|
|
802
|
-
const remoteMsg = await this.retrieveL1ToL2Message(msg.leaf);
|
|
803
811
|
const logCtx = {
|
|
804
|
-
remoteMsg,
|
|
805
|
-
localMsg
|
|
812
|
+
remoteMsg: undefined,
|
|
813
|
+
localMsg,
|
|
814
|
+
remoteMessagesState
|
|
806
815
|
};
|
|
807
|
-
if
|
|
808
|
-
|
|
816
|
+
// First check if the local message rolling hash matches the current rolling hash of the inbox contract,
|
|
817
|
+
// which means we just need to rollback some local messages and we should be back in sync. This means there
|
|
818
|
+
// was an L1 reorg that removed some of the messages we had, but no new messages were added compared.
|
|
819
|
+
if (localMsg.rollingHash.equals(remoteRollingHash)) {
|
|
820
|
+
this.log.info(`Found common L1 to L2 message at index ${localMsg.index} on L1 block ${localMsg.l1BlockNumber} matching current remote state`, logCtx);
|
|
821
|
+
commonMsg = localMsg;
|
|
822
|
+
break;
|
|
823
|
+
}
|
|
824
|
+
// Messages at or below the finalized L1 block cannot have been reorged — accept as common without querying L1.
|
|
825
|
+
if (finalizedL1BlockNumber !== undefined && localMsg.l1BlockNumber <= finalizedL1BlockNumber) {
|
|
826
|
+
this.log.info(`Found common L1 to L2 message at finalized L1 block ${localMsg.l1BlockNumber}`, logCtx);
|
|
827
|
+
commonMsg = localMsg;
|
|
828
|
+
break;
|
|
829
|
+
}
|
|
830
|
+
// If there's no match with the current remote state, check if the message exists on the inbox contract at all
|
|
831
|
+
// by looking at the inbox events. If the L1 reorg *added* new messages in addition to deleting existing ones,
|
|
832
|
+
// then the current remote state's rolling hash will not match anything we have locally, so we need to check existence
|
|
833
|
+
// of individual messages via logs. Note we use logs and not historical queries so we don't have to depend on
|
|
834
|
+
// an archival rpc node, since the message could be from a long time ago if we're catching up with syncing.
|
|
835
|
+
const remoteMsg = await retrieveL1ToL2Message(this.inbox, localMsg);
|
|
836
|
+
logCtx.remoteMsg = remoteMsg;
|
|
837
|
+
if (remoteMsg && remoteMsg.rollingHash.equals(localMsg.rollingHash)) {
|
|
838
|
+
this.log.info(`Found most recent common L1 to L2 message at index ${localMsg.index} on L1 block ${localMsg.l1BlockNumber}`, logCtx);
|
|
809
839
|
commonMsg = remoteMsg;
|
|
810
840
|
break;
|
|
811
841
|
} else if (remoteMsg) {
|
|
812
|
-
this.log.debug(`Local L1 to L2 message with index ${
|
|
842
|
+
this.log.debug(`Local L1 to L2 message with index ${localMsg.index} has different rolling hash`, logCtx);
|
|
843
|
+
messagesToDelete++;
|
|
813
844
|
} else {
|
|
814
|
-
this.log.debug(`Local L1 to L2 message with index ${
|
|
845
|
+
this.log.debug(`Local L1 to L2 message with index ${localMsg.index} not found on L1`, logCtx);
|
|
846
|
+
messagesToDelete++;
|
|
815
847
|
}
|
|
816
848
|
}
|
|
817
|
-
// Delete everything after the common message we found.
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
849
|
+
// Delete everything after the common message we found, if anything needs to be deleted.
|
|
850
|
+
// Do not exit early if there are no messages to delete, we still want to update the syncpoint.
|
|
851
|
+
if (messagesToDelete > 0) {
|
|
852
|
+
const lastGoodIndex = commonMsg?.index;
|
|
853
|
+
this.log.warn(`Rolling back all local L1 to L2 messages after index ${lastGoodIndex ?? 'initial'}`);
|
|
854
|
+
await this.stores.messages.removeL1ToL2Messages(lastGoodIndex !== undefined ? lastGoodIndex + 1n : 0n);
|
|
855
|
+
}
|
|
821
856
|
// Update the syncpoint so the loop below reprocesses the changed messages. We go to the block before
|
|
822
857
|
// the last common one, so we force reprocessing it, in case new messages were added on that same L1 block
|
|
823
|
-
// after the last common message.
|
|
824
|
-
|
|
825
|
-
const
|
|
826
|
-
|
|
858
|
+
// after the last common message. Cap at the finalized L1 block: messages at or below finalized cannot
|
|
859
|
+
// have been reorged, so there is no need to walk back any further than that.
|
|
860
|
+
const syncPointL1BlockNumber = maxBigint(...compactArray([
|
|
861
|
+
commonMsg ? commonMsg.l1BlockNumber - 1n : undefined,
|
|
862
|
+
finalizedL1BlockNumber,
|
|
863
|
+
this.l1Constants.l1StartBlock
|
|
864
|
+
]));
|
|
865
|
+
const syncPointL1BlockHash = syncPointL1BlockNumber === finalizedL1BlockNumber ? messagesFinalizedL1Block.l1BlockHash : await this.getL1BlockHash(syncPointL1BlockNumber);
|
|
866
|
+
const messagesSyncPoint = {
|
|
827
867
|
l1BlockNumber: syncPointL1BlockNumber,
|
|
828
868
|
l1BlockHash: syncPointL1BlockHash
|
|
829
869
|
};
|
|
830
|
-
await this.
|
|
870
|
+
await this.stores.messages.setMessageSyncState(messagesSyncPoint, remoteTreeInProgress);
|
|
871
|
+
this.log.verbose(`Updated messages syncpoint to L1 block ${messagesSyncPoint.l1BlockNumber}`, {
|
|
872
|
+
...messagesSyncPoint,
|
|
873
|
+
remoteTreeInProgress
|
|
874
|
+
});
|
|
831
875
|
return messagesSyncPoint;
|
|
832
876
|
}
|
|
833
877
|
async getL1BlockHash(l1BlockNumber) {
|
|
@@ -841,8 +885,8 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
841
885
|
return Buffer32.fromString(block.hash);
|
|
842
886
|
}
|
|
843
887
|
async handleCheckpoints(blocksSynchedTo, currentL1BlockNumber, initialSyncComplete) {
|
|
844
|
-
const localPendingCheckpointNumber = await this.
|
|
845
|
-
const initialValidationResult = await this.
|
|
888
|
+
const localPendingCheckpointNumber = await this.stores.blocks.getLatestCheckpointNumber();
|
|
889
|
+
const initialValidationResult = await this.stores.blocks.getPendingChainValidationStatus();
|
|
846
890
|
const { provenCheckpointNumber, provenArchive, pendingCheckpointNumber, pendingArchive, archiveOfMyCheckpoint: archiveForLocalPendingCheckpointNumber } = await execInSpan(this.tracer, 'Archiver.getRollupStatus', ()=>this.rollup.status(localPendingCheckpointNumber, {
|
|
847
891
|
blockNumber: currentL1BlockNumber
|
|
848
892
|
}));
|
|
@@ -865,7 +909,7 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
865
909
|
// we need to set it to zero. This is an edge case because we dont have a checkpoint zero (initial checkpoint is one),
|
|
866
910
|
// so localCheckpointForDestinationProvenCheckpointNumber would not be found below.
|
|
867
911
|
if (provenCheckpointNumber === 0) {
|
|
868
|
-
const localProvenCheckpointNumber = await this.
|
|
912
|
+
const localProvenCheckpointNumber = await this.stores.blocks.getProvenCheckpointNumber();
|
|
869
913
|
if (localProvenCheckpointNumber !== provenCheckpointNumber) {
|
|
870
914
|
await this.updater.setProvenCheckpointNumber(provenCheckpointNumber);
|
|
871
915
|
this.log.info(`Rolled back proven chain to checkpoint ${provenCheckpointNumber}`, {
|
|
@@ -873,16 +917,16 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
873
917
|
});
|
|
874
918
|
}
|
|
875
919
|
}
|
|
876
|
-
const localCheckpointForDestinationProvenCheckpointNumber = await this.
|
|
920
|
+
const localCheckpointForDestinationProvenCheckpointNumber = await this.stores.blocks.getCheckpointData(provenCheckpointNumber);
|
|
877
921
|
// Sanity check. I've hit what seems to be a state where the proven checkpoint is set to a value greater than the latest
|
|
878
922
|
// synched checkpoint when requesting L2Tips from the archiver. This is the only place where the proven checkpoint is set.
|
|
879
|
-
const synched = await this.
|
|
923
|
+
const synched = await this.stores.blocks.getLatestCheckpointNumber();
|
|
880
924
|
if (localCheckpointForDestinationProvenCheckpointNumber && synched < localCheckpointForDestinationProvenCheckpointNumber.checkpointNumber) {
|
|
881
925
|
this.log.error(`Hit local checkpoint greater than last synched checkpoint: ${localCheckpointForDestinationProvenCheckpointNumber.checkpointNumber} > ${synched}`);
|
|
882
926
|
}
|
|
883
927
|
this.log.trace(`Local checkpoint for remote proven checkpoint ${provenCheckpointNumber} is ${localCheckpointForDestinationProvenCheckpointNumber?.archive.root.toString() ?? 'undefined'}`);
|
|
884
928
|
if (localCheckpointForDestinationProvenCheckpointNumber && provenArchive.equals(localCheckpointForDestinationProvenCheckpointNumber.archive.root)) {
|
|
885
|
-
const localProvenCheckpointNumber = await this.
|
|
929
|
+
const localProvenCheckpointNumber = await this.stores.blocks.getProvenCheckpointNumber();
|
|
886
930
|
if (localProvenCheckpointNumber !== provenCheckpointNumber) {
|
|
887
931
|
await this.updater.setProvenCheckpointNumber(provenCheckpointNumber);
|
|
888
932
|
this.log.info(`Updated proven chain to checkpoint ${provenCheckpointNumber}`, {
|
|
@@ -907,7 +951,7 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
907
951
|
// If we have 0 checkpoints locally and there are no checkpoints onchain there is nothing to do.
|
|
908
952
|
const noCheckpoints = localPendingCheckpointNumber === 0 && pendingCheckpointNumber === 0;
|
|
909
953
|
if (noCheckpoints) {
|
|
910
|
-
await this.
|
|
954
|
+
await this.stores.blocks.setSynchedL1BlockNumber(currentL1BlockNumber);
|
|
911
955
|
this.log.debug(`No checkpoints to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}, no checkpoints on chain`);
|
|
912
956
|
return rollupStatus;
|
|
913
957
|
}
|
|
@@ -915,7 +959,7 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
915
959
|
// Related to the L2 reorgs of the pending chain. We are only interested in actually addressing a reorg if there
|
|
916
960
|
// are any state that could be impacted by it. If we have no checkpoints, there is no impact.
|
|
917
961
|
if (localPendingCheckpointNumber > 0) {
|
|
918
|
-
const localPendingCheckpoint = await this.
|
|
962
|
+
const localPendingCheckpoint = await this.stores.blocks.getCheckpointData(localPendingCheckpointNumber);
|
|
919
963
|
if (localPendingCheckpoint === undefined) {
|
|
920
964
|
throw new Error(`Missing checkpoint ${localPendingCheckpointNumber}`);
|
|
921
965
|
}
|
|
@@ -929,7 +973,7 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
929
973
|
// However, in the re-org scenario, our L1 node is temporarily lying to us and we end up potentially missing checkpoints.
|
|
930
974
|
// We must only set this block number based on actually retrieved logs.
|
|
931
975
|
// TODO(#8621): Tackle this properly when we handle L1 Re-orgs.
|
|
932
|
-
// await this.
|
|
976
|
+
// await this.stores.blocks.setSynchedL1BlockNumber(currentL1BlockNumber);
|
|
933
977
|
this.log.debug(`No checkpoints to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}`);
|
|
934
978
|
return rollupStatus;
|
|
935
979
|
}
|
|
@@ -946,7 +990,7 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
946
990
|
});
|
|
947
991
|
let tipAfterUnwind = localPendingCheckpointNumber;
|
|
948
992
|
while(true){
|
|
949
|
-
const candidateCheckpoint = await this.
|
|
993
|
+
const candidateCheckpoint = await this.stores.blocks.getCheckpointData(tipAfterUnwind);
|
|
950
994
|
if (candidateCheckpoint === undefined) {
|
|
951
995
|
break;
|
|
952
996
|
}
|
|
@@ -962,7 +1006,7 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
962
1006
|
}
|
|
963
1007
|
const checkpointsToRemove = localPendingCheckpointNumber - tipAfterUnwind;
|
|
964
1008
|
await this.updater.removeCheckpointsAfter(CheckpointNumber(tipAfterUnwind));
|
|
965
|
-
this.log.warn(`Removed ${count(checkpointsToRemove, 'checkpoint')} after checkpoint ${tipAfterUnwind} ` + `due to mismatched checkpoint hashes at L1 block ${currentL1BlockNumber}. ` + `Updated L2 latest checkpoint is ${await this.
|
|
1009
|
+
this.log.warn(`Removed ${count(checkpointsToRemove, 'checkpoint')} after checkpoint ${tipAfterUnwind} ` + `due to mismatched checkpoint hashes at L1 block ${currentL1BlockNumber}. ` + `Updated L2 latest checkpoint is ${await this.stores.blocks.getLatestCheckpointNumber()}.`);
|
|
966
1010
|
}
|
|
967
1011
|
}
|
|
968
1012
|
// Retrieve checkpoints in batches. Each batch is estimated to accommodate up to 'blockBatchSize' L1 blocks,
|
|
@@ -970,35 +1014,65 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
970
1014
|
let searchStartBlock = blocksSynchedTo;
|
|
971
1015
|
let searchEndBlock = blocksSynchedTo;
|
|
972
1016
|
let lastRetrievedCheckpoint;
|
|
973
|
-
let
|
|
1017
|
+
let lastSeenCheckpoint;
|
|
974
1018
|
do {
|
|
975
1019
|
[searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, currentL1BlockNumber);
|
|
976
1020
|
this.log.trace(`Retrieving checkpoints from L1 block ${searchStartBlock} to ${searchEndBlock}`);
|
|
977
|
-
//
|
|
978
|
-
const
|
|
979
|
-
if (
|
|
1021
|
+
// First fetch calldata only, no blobs yet, since we may be able to just get that data out of the proposed chain
|
|
1022
|
+
const calldataCheckpoints = await execInSpan(this.tracer, 'Archiver.retrieveCheckpointCalldataFromRollup', ()=>retrieveCheckpointCalldataFromRollup(this.rollup, this.publicClient, this.debugClient, searchStartBlock, searchEndBlock, this.instrumentation, this.log));
|
|
1023
|
+
if (calldataCheckpoints.length === 0) {
|
|
980
1024
|
// We are not calling `setBlockSynchedL1BlockNumber` because it may cause sync issues if based off infura.
|
|
981
1025
|
// See further details in earlier comments.
|
|
982
1026
|
this.log.trace(`Retrieved no new checkpoints from L1 block ${searchStartBlock} to ${searchEndBlock}`);
|
|
983
1027
|
continue;
|
|
984
1028
|
}
|
|
985
|
-
this.log.debug(`Retrieved ${
|
|
986
|
-
lastProcessedCheckpoint:
|
|
1029
|
+
this.log.debug(`Retrieved ${calldataCheckpoints.length} new checkpoint calldata between L1 blocks ${searchStartBlock} and ${searchEndBlock}`, {
|
|
1030
|
+
lastProcessedCheckpoint: calldataCheckpoints[calldataCheckpoints.length - 1].l1,
|
|
987
1031
|
searchStartBlock,
|
|
988
1032
|
searchEndBlock
|
|
989
1033
|
});
|
|
990
|
-
|
|
1034
|
+
// Check if the last checkpoint matches a local pending entry (so we can skip blob fetch).
|
|
1035
|
+
// We only check the last one; if it matches, the blob fetch is skipped for that entry.
|
|
1036
|
+
// TODO(palla/pipelining): We may have more than a single checkpoint to promote
|
|
1037
|
+
const lastCalldataCheckpoint = calldataCheckpoints[calldataCheckpoints.length - 1];
|
|
1038
|
+
const promoteResult = await this.tryBuildPublishedCheckpointFromProposed(lastCalldataCheckpoint);
|
|
1039
|
+
const checkpointToPromote = promoteResult && !('diverged' in promoteResult) ? promoteResult : undefined;
|
|
1040
|
+
const evictProposedFrom = promoteResult && 'diverged' in promoteResult ? promoteResult.fromCheckpointNumber : undefined;
|
|
1041
|
+
// Then fetch blobs in parallel and build the full published checkpoints
|
|
1042
|
+
const toFetchBlobs = checkpointToPromote ? calldataCheckpoints.slice(0, -1) : calldataCheckpoints;
|
|
1043
|
+
const blobFetched = await asyncPool(10, toFetchBlobs, async (checkpoint)=>retrievedToPublishedCheckpoint({
|
|
1044
|
+
...checkpoint,
|
|
1045
|
+
checkpointBlobData: await getCheckpointBlobDataFromBlobs(this.blobClient, checkpoint.l1.blockHash, checkpoint.blobHashes, checkpoint.checkpointNumber, this.log, !initialSyncComplete, checkpoint.parentBeaconBlockRoot, checkpoint.l1.timestamp)
|
|
1046
|
+
}));
|
|
1047
|
+
// And add the promoted checkpoint to the list of all checkpoints
|
|
1048
|
+
const publishedCheckpoints = checkpointToPromote ? [
|
|
1049
|
+
...blobFetched,
|
|
1050
|
+
checkpointToPromote
|
|
1051
|
+
] : blobFetched;
|
|
991
1052
|
const validCheckpoints = [];
|
|
1053
|
+
// Now loop through all checkpoints and validate their attestations
|
|
992
1054
|
for (const published of publishedCheckpoints){
|
|
1055
|
+
// Check the attestations uploaded by the publisher to L1 are correct
|
|
1056
|
+
// Rollup contract does not validate attestations to save on gas, so this
|
|
1057
|
+
// falls on the nodes to verify offchain and skip those checkpoints.
|
|
993
1058
|
const validationResult = this.config.skipValidateCheckpointAttestations ? {
|
|
994
1059
|
valid: true
|
|
995
|
-
} : await validateCheckpointAttestations(published, this.epochCache, this.l1Constants, this.log);
|
|
996
|
-
//
|
|
1060
|
+
} : await validateCheckpointAttestations(published, this.epochCache, this.l1Constants, this.getSignatureContext(), this.log);
|
|
1061
|
+
// Also skip the checkpoint if it builds on a previously-rejected ancestor. Without
|
|
1062
|
+
// this, addCheckpoints would throw InitialCheckpointNumberNotSequentialError when the
|
|
1063
|
+
// ancestor was skipped earlier (e.g. due to invalid attestations), the catch handler
|
|
1064
|
+
// would roll back the L1 sync point, and the next iteration would re-fetch and re-throw.
|
|
1065
|
+
const rejectedAncestor = await this.stores.blocks.getRejectedCheckpointByArchiveRoot(published.checkpoint.header.lastArchiveRoot);
|
|
1066
|
+
// Update the validation result if it has changed, so we can keep track of the first invalid checkpoint
|
|
997
1067
|
// in case there is a sequence of more than one invalid checkpoint, as we need to invalidate the first one.
|
|
998
1068
|
// There is an exception though: if a checkpoint is invalidated and replaced with another invalid checkpoint,
|
|
999
1069
|
// we need to update the validation result, since we need to be able to invalidate the new one.
|
|
1000
1070
|
// See test 'chain progresses if an invalid checkpoint is invalidated with an invalid one' for more info.
|
|
1001
|
-
if
|
|
1071
|
+
// Do not update the validation result if there is a rejected ancestor, since in that case we want to keep the
|
|
1072
|
+
// original invalidation, as the new checkpoint is extending from a previous invalid one.
|
|
1073
|
+
const validStatusChanged = rollupStatus.validationResult?.valid !== validationResult.valid;
|
|
1074
|
+
const invalidStatusWithSameCheckpointNumber = !validationResult.valid && rollupStatus.validationResult && !rollupStatus.validationResult.valid && rollupStatus.validationResult.checkpoint.checkpointNumber === validationResult.checkpoint.checkpointNumber;
|
|
1075
|
+
if (!rejectedAncestor && (validStatusChanged || invalidStatusWithSameCheckpointNumber)) {
|
|
1002
1076
|
rollupStatus.validationResult = validationResult;
|
|
1003
1077
|
}
|
|
1004
1078
|
if (!validationResult.valid) {
|
|
@@ -1012,12 +1086,52 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
1012
1086
|
type: L2BlockSourceEvents.InvalidAttestationsCheckpointDetected,
|
|
1013
1087
|
validationResult
|
|
1014
1088
|
});
|
|
1089
|
+
// Persist a rejected-ancestor entry so any later checkpoint that builds on this one
|
|
1090
|
+
// is detected and skipped (rather than tripping the addCheckpoints consecutive-number
|
|
1091
|
+
// check and causing the sync point to roll back in a loop).
|
|
1092
|
+
await this.stores.blocks.addRejectedCheckpoint({
|
|
1093
|
+
checkpointNumber: published.checkpoint.number,
|
|
1094
|
+
archiveRoot: published.checkpoint.archive.root,
|
|
1095
|
+
parentArchiveRoot: published.checkpoint.header.lastArchiveRoot,
|
|
1096
|
+
slotNumber: published.checkpoint.header.slotNumber,
|
|
1097
|
+
l1: published.l1,
|
|
1098
|
+
reason: 'invalid-attestations'
|
|
1099
|
+
});
|
|
1100
|
+
continue;
|
|
1101
|
+
}
|
|
1102
|
+
if (rejectedAncestor) {
|
|
1103
|
+
const descendantInfo = published.checkpoint.toCheckpointInfo();
|
|
1104
|
+
this.log.warn(`Skipping checkpoint ${published.checkpoint.number} as it is a descendant of ` + `rejected checkpoint ${rejectedAncestor.checkpointNumber} (${rejectedAncestor.reason})`, {
|
|
1105
|
+
checkpointNumber: published.checkpoint.number,
|
|
1106
|
+
checkpointHash: published.checkpoint.hash(),
|
|
1107
|
+
l1BlockNumber: published.l1.blockNumber,
|
|
1108
|
+
l1BlockHash: published.l1.blockHash,
|
|
1109
|
+
ancestorCheckpointNumber: rejectedAncestor.checkpointNumber,
|
|
1110
|
+
ancestorArchiveRoot: rejectedAncestor.archiveRoot.toString(),
|
|
1111
|
+
ancestorReason: rejectedAncestor.reason
|
|
1112
|
+
});
|
|
1113
|
+
this.events.emit(L2BlockSourceEvents.DescendentOfInvalidAttestationsCheckpointDetected, {
|
|
1114
|
+
type: L2BlockSourceEvents.DescendentOfInvalidAttestationsCheckpointDetected,
|
|
1115
|
+
checkpoint: descendantInfo,
|
|
1116
|
+
ancestorArchiveRoot: rejectedAncestor.archiveRoot,
|
|
1117
|
+
ancestorCheckpointNumber: rejectedAncestor.checkpointNumber
|
|
1118
|
+
});
|
|
1119
|
+
// Persist this chainpoint as rejected as well, so we can construct a chain of
|
|
1120
|
+
// skipped checkpoints starting from the first one with invalid attestations.
|
|
1121
|
+
await this.stores.blocks.addRejectedCheckpoint({
|
|
1122
|
+
checkpointNumber: published.checkpoint.number,
|
|
1123
|
+
archiveRoot: published.checkpoint.archive.root,
|
|
1124
|
+
parentArchiveRoot: published.checkpoint.header.lastArchiveRoot,
|
|
1125
|
+
slotNumber: published.checkpoint.header.slotNumber,
|
|
1126
|
+
l1: published.l1,
|
|
1127
|
+
reason: 'descends-from-invalid-attestations'
|
|
1128
|
+
});
|
|
1015
1129
|
continue;
|
|
1016
1130
|
}
|
|
1017
1131
|
// Check the inHash of the checkpoint against the l1->l2 messages.
|
|
1018
1132
|
// The messages should've been synced up to the currentL1BlockNumber and must be available for the published
|
|
1019
1133
|
// checkpoints we just retrieved.
|
|
1020
|
-
const l1ToL2Messages = await this.
|
|
1134
|
+
const l1ToL2Messages = await this.stores.messages.getL1ToL2Messages(published.checkpoint.number);
|
|
1021
1135
|
const computedInHash = computeInHashFromL1ToL2Messages(l1ToL2Messages);
|
|
1022
1136
|
const publishedInHash = published.checkpoint.header.inHash;
|
|
1023
1137
|
if (!computedInHash.equals(publishedInHash)) {
|
|
@@ -1038,10 +1152,26 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
1038
1152
|
blocks: published.checkpoint.blocks.map((b)=>b.getStats())
|
|
1039
1153
|
});
|
|
1040
1154
|
}
|
|
1155
|
+
for (const published of validCheckpoints){
|
|
1156
|
+
this.instrumentation.processCheckpointL1Timing({
|
|
1157
|
+
slotNumber: published.checkpoint.header.slotNumber,
|
|
1158
|
+
l1Timestamp: published.l1.timestamp,
|
|
1159
|
+
l1Constants: this.l1Constants
|
|
1160
|
+
});
|
|
1161
|
+
}
|
|
1041
1162
|
try {
|
|
1042
1163
|
const updatedValidationResult = rollupStatus.validationResult === initialValidationResult ? undefined : rollupStatus.validationResult;
|
|
1043
|
-
|
|
1044
|
-
|
|
1164
|
+
// Split valid checkpoints: the promoted one (if any) is persisted via the proposed-promotion path,
|
|
1165
|
+
// the rest via addCheckpoints. Both paths run within the same store transaction for atomicity.
|
|
1166
|
+
const [[maybeValidCheckpointToPromote], checkpointsToAdd] = partition(validCheckpoints, (c)=>c.checkpoint.number === checkpointToPromote?.checkpoint.number);
|
|
1167
|
+
const [processDuration, result] = await elapsed(()=>execInSpan(this.tracer, 'Archiver.addCheckpoints', ()=>this.updater.addCheckpoints(checkpointsToAdd, updatedValidationResult, maybeValidCheckpointToPromote && {
|
|
1168
|
+
l1: lastCalldataCheckpoint.l1,
|
|
1169
|
+
attestations: lastCalldataCheckpoint.attestations,
|
|
1170
|
+
checkpoint: maybeValidCheckpointToPromote
|
|
1171
|
+
}, evictProposedFrom)));
|
|
1172
|
+
if (validCheckpoints.length > 0) {
|
|
1173
|
+
this.instrumentation.processNewCheckpointedBlocks(processDuration / validCheckpoints.length, validCheckpoints.flatMap((c)=>c.checkpoint.blocks));
|
|
1174
|
+
}
|
|
1045
1175
|
// If blocks were pruned due to conflict with L1 checkpoints, emit event
|
|
1046
1176
|
if (result.prunedBlocks && result.prunedBlocks.length > 0) {
|
|
1047
1177
|
const prunedCheckpointNumber = result.prunedBlocks[0].checkpointNumber;
|
|
@@ -1063,11 +1193,15 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
1063
1193
|
} catch (err) {
|
|
1064
1194
|
if (err instanceof InitialCheckpointNumberNotSequentialError) {
|
|
1065
1195
|
const { previousCheckpointNumber, newCheckpointNumber } = err;
|
|
1066
|
-
const previousCheckpoint = previousCheckpointNumber ? await this.
|
|
1067
|
-
const
|
|
1068
|
-
|
|
1196
|
+
const previousCheckpoint = previousCheckpointNumber ? await this.stores.blocks.getCheckpointData(CheckpointNumber(previousCheckpointNumber)) : undefined;
|
|
1197
|
+
const lastFinalizedCheckpoint = await this.stores.blocks.getCheckpointData(await this.stores.blocks.getFinalizedCheckpointNumber());
|
|
1198
|
+
const updatedL1SyncPoint = previousCheckpoint?.l1.blockNumber ?? lastFinalizedCheckpoint?.l1.blockNumber ?? this.l1Constants.l1StartBlock;
|
|
1199
|
+
await this.stores.blocks.setSynchedL1BlockNumber(updatedL1SyncPoint);
|
|
1069
1200
|
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.`, {
|
|
1070
1201
|
previousCheckpointNumber,
|
|
1202
|
+
previousCheckpoint: previousCheckpoint?.header.toInspect(),
|
|
1203
|
+
lastFinalizedCheckpoint: lastFinalizedCheckpoint?.header.toInspect(),
|
|
1204
|
+
l1StartBlock: this.l1Constants.l1StartBlock,
|
|
1071
1205
|
newCheckpointNumber,
|
|
1072
1206
|
updatedL1SyncPoint
|
|
1073
1207
|
});
|
|
@@ -1086,45 +1220,113 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
1086
1220
|
});
|
|
1087
1221
|
}
|
|
1088
1222
|
lastRetrievedCheckpoint = validCheckpoints.at(-1) ?? lastRetrievedCheckpoint;
|
|
1089
|
-
|
|
1223
|
+
lastSeenCheckpoint = publishedCheckpoints.at(-1) ?? lastSeenCheckpoint;
|
|
1090
1224
|
}while (searchEndBlock < currentL1BlockNumber)
|
|
1091
1225
|
// Important that we update AFTER inserting the blocks.
|
|
1092
1226
|
await updateProvenCheckpoint();
|
|
1093
1227
|
return {
|
|
1094
1228
|
...rollupStatus,
|
|
1095
1229
|
lastRetrievedCheckpoint,
|
|
1096
|
-
|
|
1230
|
+
lastSeenCheckpoint
|
|
1097
1231
|
};
|
|
1098
1232
|
}
|
|
1233
|
+
/**
|
|
1234
|
+
* Checks if a specific checkpoint matches a local pending entry, and if so, loads local data to build
|
|
1235
|
+
* a synthetic published checkpoint (skipping blob fetch).
|
|
1236
|
+
*
|
|
1237
|
+
* Returns { diverged: true, fromCheckpointNumber } when the L1 checkpoint does NOT match local pending
|
|
1238
|
+
* data for that number, so the caller can evict the entire pending suffix >= fromCheckpointNumber
|
|
1239
|
+
* (those entries chain off the now-invalid local state) within the same addCheckpoints transaction.
|
|
1240
|
+
*/ async tryBuildPublishedCheckpointFromProposed(calldataCheckpoint) {
|
|
1241
|
+
if (this.config.skipPromoteProposedCheckpointDuringL1Sync || !calldataCheckpoint) {
|
|
1242
|
+
return undefined;
|
|
1243
|
+
}
|
|
1244
|
+
// Look up the specific pending entry for the checkpoint being mined, not just the tip
|
|
1245
|
+
const proposed = await this.stores.blocks.getProposedCheckpointByNumber(calldataCheckpoint.checkpointNumber);
|
|
1246
|
+
if (!proposed) {
|
|
1247
|
+
return undefined;
|
|
1248
|
+
}
|
|
1249
|
+
if (!proposed.header.equals(calldataCheckpoint.header) || !proposed.archive.root.equals(calldataCheckpoint.archiveRoot)) {
|
|
1250
|
+
this.log.warn(`Local proposed checkpoint ${proposed.checkpointNumber} does not match checkpoint retrieved from L1, overriding with L1 data`, {
|
|
1251
|
+
proposedCheckpointNumber: proposed.checkpointNumber,
|
|
1252
|
+
proposedHeader: proposed.header.toInspect(),
|
|
1253
|
+
proposedArchiveRoot: proposed.archive.root.toString(),
|
|
1254
|
+
calldataCheckpointNumber: calldataCheckpoint.checkpointNumber,
|
|
1255
|
+
calldataHeader: calldataCheckpoint.header.toInspect(),
|
|
1256
|
+
calldataArchiveRoot: calldataCheckpoint.archiveRoot.toString()
|
|
1257
|
+
});
|
|
1258
|
+
// Both the locally-proposed checkpoint and the L1-confirmed one are signed by the
|
|
1259
|
+
// slot proposer; emit a divergence event so the slasher can attribute equivocation.
|
|
1260
|
+
// Only emit when the slots match — uncheckpointed entries are pruned above so this
|
|
1261
|
+
// should always hold, but guard defensively to avoid mis-attributing a slash.
|
|
1262
|
+
if (proposed.header.slotNumber === calldataCheckpoint.header.slotNumber) {
|
|
1263
|
+
this.events.emit(L2BlockSourceEvents.CheckpointEquivocationDetected, {
|
|
1264
|
+
type: L2BlockSourceEvents.CheckpointEquivocationDetected,
|
|
1265
|
+
slotNumber: calldataCheckpoint.header.slotNumber,
|
|
1266
|
+
checkpointNumber: calldataCheckpoint.checkpointNumber,
|
|
1267
|
+
l1ArchiveRoot: calldataCheckpoint.archiveRoot,
|
|
1268
|
+
proposedArchiveRoot: proposed.archive.root
|
|
1269
|
+
});
|
|
1270
|
+
}
|
|
1271
|
+
// Return a divergence signal so the caller can evict pending >= this number
|
|
1272
|
+
return {
|
|
1273
|
+
diverged: true,
|
|
1274
|
+
fromCheckpointNumber: proposed.checkpointNumber
|
|
1275
|
+
};
|
|
1276
|
+
}
|
|
1277
|
+
this.log.debug(`Building published checkpoint from proposed ${calldataCheckpoint.checkpointNumber} (skipping blob fetch)`, {
|
|
1278
|
+
proposedHeader: proposed.header.toInspect(),
|
|
1279
|
+
proposedArchiveRoot: proposed.archive.root.toString()
|
|
1280
|
+
});
|
|
1281
|
+
const blocks = await this.stores.blocks.getBlocks({
|
|
1282
|
+
from: BlockNumber(proposed.startBlock),
|
|
1283
|
+
limit: proposed.blockCount
|
|
1284
|
+
});
|
|
1285
|
+
if (blocks.length !== proposed.blockCount) {
|
|
1286
|
+
this.log.warn(`Local proposed checkpoint ${proposed.checkpointNumber} has wrong block count (expected ${proposed.blockCount} blocks starting at ${proposed.startBlock} but got ${blocks.length})`, {
|
|
1287
|
+
proposedCheckpointNumber: proposed.checkpointNumber,
|
|
1288
|
+
proposedStartBlock: proposed.startBlock,
|
|
1289
|
+
proposedBlockCount: proposed.blockCount,
|
|
1290
|
+
retrievedBlocks: blocks.map((b)=>b.number)
|
|
1291
|
+
});
|
|
1292
|
+
return undefined;
|
|
1293
|
+
}
|
|
1294
|
+
const checkpoint = Checkpoint.from({
|
|
1295
|
+
archive: proposed.archive,
|
|
1296
|
+
header: proposed.header,
|
|
1297
|
+
blocks,
|
|
1298
|
+
number: proposed.checkpointNumber,
|
|
1299
|
+
feeAssetPriceModifier: proposed.feeAssetPriceModifier
|
|
1300
|
+
});
|
|
1301
|
+
const promotedCheckpoint = PublishedCheckpoint.from({
|
|
1302
|
+
checkpoint,
|
|
1303
|
+
l1: calldataCheckpoint.l1,
|
|
1304
|
+
attestations: calldataCheckpoint.attestations
|
|
1305
|
+
});
|
|
1306
|
+
this.instrumentation.processCheckpointPromoted();
|
|
1307
|
+
return promotedCheckpoint;
|
|
1308
|
+
}
|
|
1099
1309
|
async checkForNewCheckpointsBeforeL1SyncPoint(status, blocksSynchedTo, currentL1BlockNumber) {
|
|
1100
|
-
const {
|
|
1101
|
-
// Compare the last checkpoint we have (either retrieved in this round or loaded from store)
|
|
1102
|
-
// rollup contract told us was the latest one (pinned at the currentL1BlockNumber).
|
|
1103
|
-
const latestLocalCheckpointNumber =
|
|
1310
|
+
const { lastSeenCheckpoint, pendingCheckpointNumber } = status;
|
|
1311
|
+
// Compare the last checkpoint (valid or not) we have (either retrieved in this round or loaded from store)
|
|
1312
|
+
// with what the rollup contract told us was the latest one (pinned at the currentL1BlockNumber).
|
|
1313
|
+
const latestLocalCheckpointNumber = lastSeenCheckpoint?.checkpoint.number ?? CheckpointNumber.max(await this.stores.blocks.getLatestCheckpointNumber(), await this.stores.blocks.getLatestRejectedCheckpointNumber()) ?? CheckpointNumber.ZERO;
|
|
1104
1314
|
if (latestLocalCheckpointNumber < pendingCheckpointNumber) {
|
|
1105
1315
|
// Here we have consumed all logs until the `currentL1Block` we pinned at the beginning of the archiver loop,
|
|
1106
1316
|
// but still haven't reached the pending checkpoint according to the call to the rollup contract.
|
|
1107
1317
|
// We suspect an L1 reorg that added checkpoints *behind* us. If that is the case, it must have happened between
|
|
1108
1318
|
// the last checkpoint we saw and the current one, so we reset the last synched L1 block number. In the edge case
|
|
1109
1319
|
// we don't have one, we go back 2 L1 epochs, which is the deepest possible reorg (assuming Casper is working).
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
if (lastRetrievedCheckpoint) {
|
|
1113
|
-
latestLocalCheckpointArchive = lastRetrievedCheckpoint.checkpoint.archive.root.toString();
|
|
1114
|
-
targetL1BlockNumber = lastRetrievedCheckpoint.l1.blockNumber;
|
|
1115
|
-
} else if (latestLocalCheckpointNumber > 0) {
|
|
1116
|
-
const checkpoint = await this.store.getRangeOfCheckpoints(latestLocalCheckpointNumber, 1).then(([c])=>c);
|
|
1117
|
-
latestLocalCheckpointArchive = checkpoint.archive.root.toString();
|
|
1118
|
-
targetL1BlockNumber = checkpoint.l1.blockNumber;
|
|
1119
|
-
}
|
|
1320
|
+
const latestLocalCheckpoint = lastSeenCheckpoint ?? await this.stores.blocks.getCheckpointData(latestLocalCheckpointNumber) ?? await this.stores.blocks.getRejectedCheckpointByNumber(latestLocalCheckpointNumber);
|
|
1321
|
+
const targetL1BlockNumber = latestLocalCheckpoint?.l1.blockNumber ?? maxBigint(currentL1BlockNumber - 64n, this.l1Constants.l1StartBlock, 0n);
|
|
1120
1322
|
this.log.warn(`Failed to reach checkpoint ${pendingCheckpointNumber} at ${currentL1BlockNumber} (latest is ${latestLocalCheckpointNumber}). ` + `Rolling back last synched L1 block number to ${targetL1BlockNumber}.`, {
|
|
1121
1323
|
latestLocalCheckpointNumber,
|
|
1122
|
-
|
|
1324
|
+
latestLocalCheckpointL1: latestLocalCheckpoint?.l1,
|
|
1123
1325
|
blocksSynchedTo,
|
|
1124
1326
|
currentL1BlockNumber,
|
|
1125
1327
|
...status
|
|
1126
1328
|
});
|
|
1127
|
-
await this.
|
|
1329
|
+
await this.stores.blocks.setSynchedL1BlockNumber(targetL1BlockNumber);
|
|
1128
1330
|
} else {
|
|
1129
1331
|
this.log.trace(`No new checkpoints behind L1 sync point to retrieve.`, {
|
|
1130
1332
|
latestLocalCheckpointNumber,
|
|
@@ -1133,7 +1335,7 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
1133
1335
|
}
|
|
1134
1336
|
}
|
|
1135
1337
|
async getCheckpointHeader(number) {
|
|
1136
|
-
const checkpoint = await this.
|
|
1338
|
+
const checkpoint = await this.stores.blocks.getCheckpointData(number);
|
|
1137
1339
|
if (!checkpoint) {
|
|
1138
1340
|
return undefined;
|
|
1139
1341
|
}
|