@aztec/archiver 0.0.1-commit.9d2bcf6d → 0.0.1-commit.9d619b6c6
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 +12 -6
- package/dest/archiver.d.ts +13 -8
- package/dest/archiver.d.ts.map +1 -1
- package/dest/archiver.js +90 -114
- package/dest/config.d.ts +5 -3
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +15 -3
- package/dest/errors.d.ts +50 -10
- package/dest/errors.d.ts.map +1 -1
- package/dest/errors.js +67 -16
- package/dest/factory.d.ts +4 -5
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +33 -27
- package/dest/index.d.ts +4 -2
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +3 -1
- 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 +191 -259
- package/dest/l1/data_retrieval.d.ts +26 -17
- package/dest/l1/data_retrieval.d.ts.map +1 -1
- package/dest/l1/data_retrieval.js +42 -47
- 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_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/data_source_base.d.ts +14 -7
- package/dest/modules/data_source_base.d.ts.map +1 -1
- package/dest/modules/data_source_base.js +39 -77
- package/dest/modules/data_store_updater.d.ts +35 -15
- package/dest/modules/data_store_updater.d.ts.map +1 -1
- package/dest/modules/data_store_updater.js +137 -96
- package/dest/modules/instrumentation.d.ts +21 -3
- package/dest/modules/instrumentation.d.ts.map +1 -1
- package/dest/modules/instrumentation.js +41 -8
- package/dest/modules/l1_synchronizer.d.ts +10 -9
- package/dest/modules/l1_synchronizer.d.ts.map +1 -1
- package/dest/modules/l1_synchronizer.js +279 -150
- package/dest/modules/validation.d.ts +1 -1
- package/dest/modules/validation.d.ts.map +1 -1
- package/dest/modules/validation.js +2 -2
- package/dest/store/block_store.d.ts +73 -28
- package/dest/store/block_store.d.ts.map +1 -1
- package/dest/store/block_store.js +395 -136
- package/dest/store/contract_class_store.d.ts +2 -3
- package/dest/store/contract_class_store.d.ts.map +1 -1
- package/dest/store/contract_class_store.js +7 -67
- package/dest/store/contract_instance_store.d.ts +1 -1
- package/dest/store/contract_instance_store.d.ts.map +1 -1
- package/dest/store/contract_instance_store.js +6 -2
- package/dest/store/kv_archiver_store.d.ts +67 -24
- package/dest/store/kv_archiver_store.d.ts.map +1 -1
- package/dest/store/kv_archiver_store.js +82 -27
- package/dest/store/l2_tips_cache.d.ts +20 -0
- package/dest/store/l2_tips_cache.d.ts.map +1 -0
- package/dest/store/l2_tips_cache.js +109 -0
- package/dest/store/log_store.d.ts +6 -3
- package/dest/store/log_store.d.ts.map +1 -1
- package/dest/store/log_store.js +150 -55
- package/dest/store/message_store.d.ts +5 -1
- package/dest/store/message_store.d.ts.map +1 -1
- package/dest/store/message_store.js +21 -9
- package/dest/test/fake_l1_state.d.ts +24 -1
- package/dest/test/fake_l1_state.d.ts.map +1 -1
- package/dest/test/fake_l1_state.js +145 -28
- 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_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 +26 -5
- package/dest/test/mock_l2_block_source.d.ts.map +1 -1
- package/dest/test/mock_l2_block_source.js +160 -89
- package/dest/test/mock_structs.d.ts +4 -1
- package/dest/test/mock_structs.d.ts.map +1 -1
- package/dest/test/mock_structs.js +13 -1
- package/dest/test/noop_l1_archiver.d.ts +4 -1
- package/dest/test/noop_l1_archiver.d.ts.map +1 -1
- package/dest/test/noop_l1_archiver.js +9 -3
- package/package.json +13 -13
- package/src/archiver.ts +120 -137
- package/src/config.ts +22 -2
- package/src/errors.ts +104 -26
- package/src/factory.ts +47 -24
- package/src/index.ts +3 -1
- package/src/l1/README.md +25 -68
- package/src/l1/bin/retrieve-calldata.ts +46 -39
- package/src/l1/calldata_retriever.ts +250 -379
- package/src/l1/data_retrieval.ts +58 -69
- package/src/l1/spire_proposer.ts +7 -15
- package/src/l1/validate_historical_logs.ts +140 -0
- package/src/modules/data_source_base.ts +78 -98
- package/src/modules/data_store_updater.ts +164 -126
- package/src/modules/instrumentation.ts +56 -9
- package/src/modules/l1_synchronizer.ts +357 -188
- package/src/modules/validation.ts +2 -2
- package/src/store/block_store.ts +503 -172
- package/src/store/contract_class_store.ts +8 -106
- package/src/store/contract_instance_store.ts +8 -5
- package/src/store/kv_archiver_store.ts +130 -41
- package/src/store/l2_tips_cache.ts +134 -0
- package/src/store/log_store.ts +221 -63
- package/src/store/message_store.ts +27 -10
- package/src/structs/inbox_message.ts +1 -1
- package/src/test/fake_l1_state.ts +193 -32
- package/src/test/mock_archiver.ts +3 -2
- package/src/test/mock_l1_to_l2_message_source.ts +1 -0
- package/src/test/mock_l2_block_source.ts +209 -82
- package/src/test/mock_structs.ts +20 -6
- package/src/test/noop_l1_archiver.ts +10 -2
|
@@ -371,20 +371,25 @@ 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 { partition, pick } from '@aztec/foundation/collection';
|
|
378
380
|
import { createLogger } from '@aztec/foundation/log';
|
|
381
|
+
import { retryTimes } from '@aztec/foundation/retry';
|
|
379
382
|
import { count } from '@aztec/foundation/string';
|
|
380
383
|
import { Timer, elapsed } from '@aztec/foundation/timer';
|
|
381
|
-
import { isDefined } from '@aztec/foundation/types';
|
|
384
|
+
import { isDefined, isErrorClass } from '@aztec/foundation/types';
|
|
382
385
|
import { L2BlockSourceEvents } from '@aztec/stdlib/block';
|
|
386
|
+
import { Checkpoint, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
|
|
383
387
|
import { getEpochAtSlot, getSlotAtNextL1Block } from '@aztec/stdlib/epoch-helpers';
|
|
384
388
|
import { computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
|
|
385
389
|
import { execInSpan, trackSpan } from '@aztec/telemetry-client';
|
|
386
390
|
import { InitialCheckpointNumberNotSequentialError } from '../errors.js';
|
|
387
|
-
import {
|
|
391
|
+
import { getCheckpointBlobDataFromBlobs, retrieveCheckpointCalldataFromRollup, retrieveL1ToL2Message, retrieveL1ToL2Messages, retrievedToPublishedCheckpoint } from '../l1/data_retrieval.js';
|
|
392
|
+
import { MessageStoreError } from '../store/message_store.js';
|
|
388
393
|
import { ArchiverDataStoreUpdater } from './data_store_updater.js';
|
|
389
394
|
import { validateCheckpointAttestations } from './validation.js';
|
|
390
395
|
_dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpochPrune'), _dec2 = trackSpan('Archiver.handleL1ToL2Messages'), _dec3 = trackSpan('Archiver.handleCheckpoints');
|
|
@@ -396,7 +401,6 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
396
401
|
debugClient;
|
|
397
402
|
rollup;
|
|
398
403
|
inbox;
|
|
399
|
-
l1Addresses;
|
|
400
404
|
store;
|
|
401
405
|
config;
|
|
402
406
|
blobClient;
|
|
@@ -435,12 +439,11 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
435
439
|
l1Timestamp;
|
|
436
440
|
updater;
|
|
437
441
|
tracer;
|
|
438
|
-
constructor(publicClient, debugClient, rollup, inbox,
|
|
442
|
+
constructor(publicClient, debugClient, rollup, inbox, store, config, blobClient, epochCache, dateProvider, instrumentation, l1Constants, events, tracer, l2TipsCache, log = createLogger('archiver:l1-sync')){
|
|
439
443
|
this.publicClient = publicClient;
|
|
440
444
|
this.debugClient = debugClient;
|
|
441
445
|
this.rollup = rollup;
|
|
442
446
|
this.inbox = inbox;
|
|
443
|
-
this.l1Addresses = l1Addresses;
|
|
444
447
|
this.store = store;
|
|
445
448
|
this.config = config;
|
|
446
449
|
this.blobClient = blobClient;
|
|
@@ -451,7 +454,9 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
451
454
|
this.events = events;
|
|
452
455
|
this.log = log;
|
|
453
456
|
_initProto(this);
|
|
454
|
-
this.updater = new ArchiverDataStoreUpdater(this.store
|
|
457
|
+
this.updater = new ArchiverDataStoreUpdater(this.store, l2TipsCache, {
|
|
458
|
+
rollupManaLimit: l1Constants.rollupManaLimit
|
|
459
|
+
});
|
|
455
460
|
this.tracer = tracer;
|
|
456
461
|
}
|
|
457
462
|
/** Sets new config */ setConfig(newConfig) {
|
|
@@ -477,12 +482,20 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
477
482
|
}
|
|
478
483
|
}
|
|
479
484
|
async syncFromL1(initialSyncComplete) {
|
|
485
|
+
// In between the various calls to L1, the block number can move meaning some of the following
|
|
486
|
+
// calls will return data for blocks that were not present during earlier calls. To combat this
|
|
487
|
+
// we ensure that all data retrieval methods only retrieve data up to the currentBlockNumber
|
|
488
|
+
// captured at the top of this function.
|
|
480
489
|
const currentL1Block = await this.publicClient.getBlock({
|
|
481
490
|
includeTransactions: false
|
|
482
491
|
});
|
|
483
492
|
const currentL1BlockNumber = currentL1Block.number;
|
|
484
493
|
const currentL1BlockHash = Buffer32.fromString(currentL1Block.hash);
|
|
485
494
|
const currentL1Timestamp = currentL1Block.timestamp;
|
|
495
|
+
const currentL1BlockData = {
|
|
496
|
+
l1BlockNumber: currentL1BlockNumber,
|
|
497
|
+
l1BlockHash: currentL1BlockHash
|
|
498
|
+
};
|
|
486
499
|
if (this.l1BlockHash && currentL1BlockHash.equals(this.l1BlockHash)) {
|
|
487
500
|
this.log.trace(`No new L1 blocks since last sync at L1 block ${this.l1BlockNumber}`);
|
|
488
501
|
return;
|
|
@@ -498,36 +511,16 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
498
511
|
maxAllowedDelay
|
|
499
512
|
});
|
|
500
513
|
}
|
|
501
|
-
// Load sync point for blocks
|
|
502
|
-
const { blocksSynchedTo = this.l1Constants.l1StartBlock
|
|
503
|
-
l1BlockNumber: this.l1Constants.l1StartBlock,
|
|
504
|
-
l1BlockHash: this.l1Constants.l1StartBlockHash
|
|
505
|
-
} } = await this.store.getSynchPoint();
|
|
514
|
+
// Load sync point for blocks defaulting to start block
|
|
515
|
+
const { blocksSynchedTo = this.l1Constants.l1StartBlock } = await this.store.getSynchPoint();
|
|
506
516
|
this.log.debug(`Starting new archiver sync iteration`, {
|
|
507
517
|
blocksSynchedTo,
|
|
508
|
-
|
|
509
|
-
currentL1BlockNumber,
|
|
510
|
-
currentL1BlockHash
|
|
518
|
+
currentL1BlockData
|
|
511
519
|
});
|
|
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 **********
|
|
520
|
+
// 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.
|
|
521
|
+
// 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
|
|
522
|
+
// block, when that wouldn't be the case, since L1 to L2 messages would need another iteration.
|
|
523
|
+
await retryTimes(()=>this.handleL1ToL2Messages(currentL1BlockData), 'Handling L1 to L2 messages', 3, 0.1);
|
|
531
524
|
if (currentL1BlockNumber > blocksSynchedTo) {
|
|
532
525
|
// First we retrieve new checkpoints and L2 blocks and store them in the DB. This will also update the
|
|
533
526
|
// pending chain validation status, proven checkpoint number, and synched L1 block number.
|
|
@@ -555,6 +548,8 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
555
548
|
}
|
|
556
549
|
this.instrumentation.updateL1BlockHeight(currentL1BlockNumber);
|
|
557
550
|
}
|
|
551
|
+
// Update the finalized L2 checkpoint based on L1 finality.
|
|
552
|
+
await this.updateFinalizedCheckpoint();
|
|
558
553
|
// After syncing has completed, update the current l1 block number and timestamp,
|
|
559
554
|
// otherwise we risk announcing to the world that we've synced to a given point,
|
|
560
555
|
// but the corresponding blocks have not been processed (see #12631).
|
|
@@ -568,6 +563,32 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
568
563
|
l1BlockNumberAtEnd
|
|
569
564
|
});
|
|
570
565
|
}
|
|
566
|
+
/** Query L1 for its finalized block and update the finalized checkpoint accordingly. */ async updateFinalizedCheckpoint() {
|
|
567
|
+
try {
|
|
568
|
+
const finalizedL1Block = await getFinalizedL1Block(this.publicClient);
|
|
569
|
+
if (!finalizedL1Block) {
|
|
570
|
+
this.log.trace(`Skipping finalized checkpoint update: L1 has no finalized block yet.`);
|
|
571
|
+
return;
|
|
572
|
+
}
|
|
573
|
+
const finalizedL1BlockNumber = finalizedL1Block.number;
|
|
574
|
+
const finalizedCheckpointNumber = await this.rollup.getProvenCheckpointNumber({
|
|
575
|
+
blockNumber: finalizedL1BlockNumber
|
|
576
|
+
});
|
|
577
|
+
const localFinalizedCheckpointNumber = await this.store.getFinalizedCheckpointNumber();
|
|
578
|
+
if (localFinalizedCheckpointNumber !== finalizedCheckpointNumber) {
|
|
579
|
+
await this.updater.setFinalizedCheckpointNumber(finalizedCheckpointNumber);
|
|
580
|
+
this.log.info(`Updated finalized chain to checkpoint ${finalizedCheckpointNumber}`, {
|
|
581
|
+
finalizedCheckpointNumber,
|
|
582
|
+
finalizedL1BlockNumber
|
|
583
|
+
});
|
|
584
|
+
}
|
|
585
|
+
} catch (err) {
|
|
586
|
+
// The rollup contract may not exist at the finalized L1 block right after deployment.
|
|
587
|
+
if (!err?.message?.includes('returned no data')) {
|
|
588
|
+
this.log.warn(`Failed to update finalized checkpoint: ${err}`);
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
}
|
|
571
592
|
/** Prune all proposed local blocks that should have been checkpointed by now. */ async pruneUncheckpointedBlocks(currentL1Timestamp) {
|
|
572
593
|
const [lastCheckpointedBlockNumber, lastProposedBlockNumber] = await Promise.all([
|
|
573
594
|
this.store.getCheckpointedL2BlockNumber(),
|
|
@@ -578,26 +599,28 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
578
599
|
this.log.trace(`No uncheckpointed blocks to prune.`);
|
|
579
600
|
return;
|
|
580
601
|
}
|
|
581
|
-
// What's the slot
|
|
602
|
+
// What's the slot at the next L1 block? All blocks for slots strictly before this one should've been checkpointed by now.
|
|
603
|
+
const slotAtNextL1Block = getSlotAtNextL1Block(currentL1Timestamp, this.l1Constants);
|
|
582
604
|
const firstUncheckpointedBlockNumber = BlockNumber(lastCheckpointedBlockNumber + 1);
|
|
605
|
+
// What's the slot of the first uncheckpointed block?
|
|
583
606
|
const [firstUncheckpointedBlockHeader] = await this.store.getBlockHeaders(firstUncheckpointedBlockNumber, 1);
|
|
584
607
|
const firstUncheckpointedBlockSlot = firstUncheckpointedBlockHeader?.getSlot();
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
608
|
+
if (firstUncheckpointedBlockSlot === undefined || firstUncheckpointedBlockSlot >= slotAtNextL1Block) {
|
|
609
|
+
return;
|
|
610
|
+
}
|
|
611
|
+
// Prune provisional blocks from slots that have ended without being checkpointed.
|
|
612
|
+
// This also clears any proposed checkpoint whose blocks are being pruned.
|
|
613
|
+
this.log.warn(`Pruning blocks after block ${lastCheckpointedBlockNumber} due to slot ${firstUncheckpointedBlockSlot} not being checkpointed`, {
|
|
614
|
+
firstUncheckpointedBlockHeader: firstUncheckpointedBlockHeader.toInspect(),
|
|
615
|
+
slotAtNextL1Block
|
|
616
|
+
});
|
|
617
|
+
const prunedBlocks = await this.updater.removeUncheckpointedBlocksAfter(lastCheckpointedBlockNumber);
|
|
618
|
+
if (prunedBlocks.length > 0) {
|
|
619
|
+
this.events.emit(L2BlockSourceEvents.L2PruneUncheckpointed, {
|
|
620
|
+
type: L2BlockSourceEvents.L2PruneUncheckpointed,
|
|
621
|
+
slotNumber: firstUncheckpointedBlockSlot,
|
|
622
|
+
blocks: prunedBlocks
|
|
592
623
|
});
|
|
593
|
-
const prunedBlocks = await this.updater.removeUncheckpointedBlocksAfter(lastCheckpointedBlockNumber);
|
|
594
|
-
if (prunedBlocks.length > 0) {
|
|
595
|
-
this.events.emit(L2BlockSourceEvents.L2PruneUncheckpointed, {
|
|
596
|
-
type: L2BlockSourceEvents.L2PruneUncheckpointed,
|
|
597
|
-
slotNumber: firstUncheckpointedBlockSlot,
|
|
598
|
-
blocks: prunedBlocks
|
|
599
|
-
});
|
|
600
|
-
}
|
|
601
624
|
}
|
|
602
625
|
}
|
|
603
626
|
/** Queries the rollup contract on whether a prune can be executed on the immediate next L1 block. */ async canPrune(currentL1BlockNumber, currentL1Timestamp) {
|
|
@@ -628,12 +651,14 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
628
651
|
const pruneFromSlotNumber = header.slotNumber;
|
|
629
652
|
const pruneFromEpochNumber = getEpochAtSlot(pruneFromSlotNumber, this.l1Constants);
|
|
630
653
|
const checkpointsToUnwind = localPendingCheckpointNumber - provenCheckpointNumber;
|
|
631
|
-
|
|
654
|
+
// Fetch checkpoints and blocks in bounded batches to avoid unbounded concurrent
|
|
655
|
+
// promises when the gap between local pending and proven checkpoint numbers is large.
|
|
656
|
+
const BATCH_SIZE = 10;
|
|
657
|
+
const indices = Array.from({
|
|
632
658
|
length: checkpointsToUnwind
|
|
633
|
-
}
|
|
634
|
-
const checkpoints = await
|
|
635
|
-
const
|
|
636
|
-
const newBlocks = blockPromises.filter(isDefined).flat();
|
|
659
|
+
}, (_, i)=>CheckpointNumber(i + pruneFrom));
|
|
660
|
+
const checkpoints = (await asyncPool(BATCH_SIZE, indices, (idx)=>this.store.getCheckpointData(idx))).filter(isDefined);
|
|
661
|
+
const newBlocks = (await asyncPool(BATCH_SIZE, checkpoints, (cp)=>this.store.getBlocksForCheckpoint(CheckpointNumber(cp.checkpointNumber)))).filter(isDefined).flat();
|
|
637
662
|
// Emit an event for listening services to react to the chain prune
|
|
638
663
|
this.events.emit(L2BlockSourceEvents.L2PruneUnproven, {
|
|
639
664
|
type: L2BlockSourceEvents.L2PruneUnproven,
|
|
@@ -667,55 +692,75 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
667
692
|
nextEnd
|
|
668
693
|
];
|
|
669
694
|
}
|
|
670
|
-
async handleL1ToL2Messages(
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
695
|
+
async handleL1ToL2Messages(currentL1Block) {
|
|
696
|
+
// Load the syncpoint, which may have been updated in a previous iteration
|
|
697
|
+
const { messagesSynchedTo = {
|
|
698
|
+
l1BlockNumber: this.l1Constants.l1StartBlock,
|
|
699
|
+
l1BlockHash: this.l1Constants.l1StartBlockHash
|
|
700
|
+
} } = await this.store.getSynchPoint();
|
|
701
|
+
// Nothing to do if L1 block number has not moved forward
|
|
702
|
+
const currentL1BlockNumber = currentL1Block.l1BlockNumber;
|
|
703
|
+
if (currentL1BlockNumber <= messagesSynchedTo.l1BlockNumber) {
|
|
704
|
+
return true;
|
|
674
705
|
}
|
|
675
|
-
//
|
|
676
|
-
const localMessagesInserted = await this.store.getTotalL1ToL2MessageCount();
|
|
677
|
-
const localLastMessage = await this.store.getLastL1ToL2Message();
|
|
706
|
+
// Compare local message store state with the remote. If they match, we just advance the match pointer.
|
|
678
707
|
const remoteMessagesState = await this.inbox.getState({
|
|
679
708
|
blockNumber: currentL1BlockNumber
|
|
680
709
|
});
|
|
681
|
-
this.
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
remoteMessagesState
|
|
685
|
-
|
|
686
|
-
// Compare message count and rolling hash. If they match, no need to retrieve anything.
|
|
687
|
-
if (remoteMessagesState.totalMessagesInserted === localMessagesInserted && remoteMessagesState.messagesRollingHash.equals(localLastMessage?.rollingHash ?? Buffer32.ZERO)) {
|
|
688
|
-
this.log.trace(`No L1 to L2 messages to query between L1 blocks ${messagesSyncPoint.l1BlockNumber} and ${currentL1BlockNumber}.`);
|
|
689
|
-
return;
|
|
710
|
+
const localLastMessage = await this.store.getLastL1ToL2Message();
|
|
711
|
+
if (await this.localStateMatches(localLastMessage, remoteMessagesState)) {
|
|
712
|
+
this.log.trace(`Local L1 to L2 messages are already in sync with remote at L1 block ${currentL1BlockNumber}`);
|
|
713
|
+
await this.store.setMessageSyncState(currentL1Block, remoteMessagesState.treeInProgress);
|
|
714
|
+
return true;
|
|
690
715
|
}
|
|
691
|
-
//
|
|
692
|
-
//
|
|
693
|
-
//
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
this.
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
this.log.warn(`Rolling back L1 to L2 messages due to hash mismatch or msg not found.`, {
|
|
702
|
-
remoteLastMessage,
|
|
703
|
-
messagesSyncPoint,
|
|
704
|
-
localLastMessage
|
|
705
|
-
});
|
|
706
|
-
messagesSyncPoint = await this.rollbackL1ToL2Messages(localLastMessage, messagesSyncPoint);
|
|
707
|
-
this.log.debug(`Rolled back L1 to L2 messages to L1 block ${messagesSyncPoint.l1BlockNumber}.`, {
|
|
708
|
-
messagesSyncPoint
|
|
716
|
+
// If not, then we are out of sync. Most likely there are new messages on the inbox, so we try retrieving them.
|
|
717
|
+
// However, it could also be the case that there was an L1 reorg and our syncpoint is no longer valid.
|
|
718
|
+
// If that's the case, we'd get an exception out of the message store since the rolling hash of the first message
|
|
719
|
+
// we try to insert would not match the one in the db, in which case we rollback to the last common message with L1.
|
|
720
|
+
try {
|
|
721
|
+
await this.retrieveAndStoreMessages(messagesSynchedTo.l1BlockNumber, currentL1BlockNumber);
|
|
722
|
+
} catch (error) {
|
|
723
|
+
if (isErrorClass(error, MessageStoreError)) {
|
|
724
|
+
this.log.warn(`Failed to store L1 to L2 messages retrieved from L1: ${error.message}. Rolling back syncpoint to retry.`, {
|
|
725
|
+
inboxMessage: error.inboxMessage
|
|
709
726
|
});
|
|
727
|
+
await this.rollbackL1ToL2Messages(remoteMessagesState);
|
|
728
|
+
return false;
|
|
710
729
|
}
|
|
730
|
+
throw error;
|
|
731
|
+
}
|
|
732
|
+
// Note that, if there are no new messages to insert, but there was an L1 reorg that pruned out last messages,
|
|
733
|
+
// we'd notice by comparing our local state with the remote one again, and seeing they don't match even after
|
|
734
|
+
// our sync attempt. In this case, we also rollback our syncpoint, and trigger a retry.
|
|
735
|
+
const localLastMessageAfterSync = await this.store.getLastL1ToL2Message();
|
|
736
|
+
if (!await this.localStateMatches(localLastMessageAfterSync, remoteMessagesState)) {
|
|
737
|
+
this.log.warn(`Local L1 to L2 messages state does not match remote after sync attempt. Rolling back syncpoint to retry.`, {
|
|
738
|
+
localLastMessageAfterSync,
|
|
739
|
+
remoteMessagesState
|
|
740
|
+
});
|
|
741
|
+
await this.rollbackL1ToL2Messages(remoteMessagesState);
|
|
742
|
+
return false;
|
|
711
743
|
}
|
|
712
|
-
//
|
|
744
|
+
// Advance the syncpoint after a successful sync
|
|
745
|
+
await this.store.setMessageSyncState(currentL1Block, remoteMessagesState.treeInProgress);
|
|
746
|
+
return true;
|
|
747
|
+
}
|
|
748
|
+
/** Checks if the local rolling hash and message count matches the remote state */ async localStateMatches(localLastMessage, remoteState) {
|
|
749
|
+
const localMessageCount = await this.store.getTotalL1ToL2MessageCount();
|
|
750
|
+
this.log.trace(`Comparing local and remote inbox state`, {
|
|
751
|
+
localMessageCount,
|
|
752
|
+
localLastMessage,
|
|
753
|
+
remoteState
|
|
754
|
+
});
|
|
755
|
+
return remoteState.totalMessagesInserted === localMessageCount && remoteState.messagesRollingHash.equals(localLastMessage?.rollingHash ?? Buffer16.ZERO);
|
|
756
|
+
}
|
|
757
|
+
/** Retrieves L1 to L2 messages from L1 in batches and stores them. */ async retrieveAndStoreMessages(fromL1Block, toL1Block) {
|
|
713
758
|
let searchStartBlock = 0n;
|
|
714
|
-
let searchEndBlock =
|
|
759
|
+
let searchEndBlock = fromL1Block;
|
|
715
760
|
let lastMessage;
|
|
716
761
|
let messageCount = 0;
|
|
717
762
|
do {
|
|
718
|
-
[searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock,
|
|
763
|
+
[searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, toL1Block);
|
|
719
764
|
this.log.trace(`Retrieving L1 to L2 messages in L1 blocks ${searchStartBlock}-${searchEndBlock}`);
|
|
720
765
|
const messages = await retrieveL1ToL2Messages(this.inbox, searchStartBlock, searchEndBlock);
|
|
721
766
|
const timer = new Timer();
|
|
@@ -730,74 +775,81 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
730
775
|
lastMessage = msg;
|
|
731
776
|
messageCount++;
|
|
732
777
|
}
|
|
733
|
-
}while (searchEndBlock <
|
|
734
|
-
// Log stats for messages retrieved (if any).
|
|
778
|
+
}while (searchEndBlock < toL1Block)
|
|
735
779
|
if (messageCount > 0) {
|
|
736
780
|
this.log.info(`Retrieved ${messageCount} new L1 to L2 messages up to message with index ${lastMessage?.index} for checkpoint ${lastMessage?.checkpointNumber}`, {
|
|
737
781
|
lastMessage,
|
|
738
782
|
messageCount
|
|
739
783
|
});
|
|
740
784
|
}
|
|
741
|
-
// Warn if the resulting rolling hash does not match the remote state we had retrieved.
|
|
742
|
-
if (lastMessage && !lastMessage.rollingHash.equals(remoteMessagesState.messagesRollingHash)) {
|
|
743
|
-
this.log.warn(`Last message retrieved rolling hash does not match remote state.`, {
|
|
744
|
-
lastMessage,
|
|
745
|
-
remoteMessagesState
|
|
746
|
-
});
|
|
747
|
-
}
|
|
748
785
|
}
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
return message;
|
|
758
|
-
}
|
|
759
|
-
}while (searchEndBlock < currentL1BlockNumber)
|
|
760
|
-
return undefined;
|
|
761
|
-
}
|
|
762
|
-
async rollbackL1ToL2Messages(localLastMessage, messagesSyncPoint) {
|
|
763
|
-
// Slowly go back through our messages until we find the last common message.
|
|
764
|
-
// We could query the logs in batch as an optimization, but the depth of the reorg should not be deep, and this
|
|
765
|
-
// is a very rare case, so it's fine to query one log at a time.
|
|
786
|
+
/**
|
|
787
|
+
* 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.
|
|
788
|
+
* If no common message is found, rolls back all messages and sets the syncpoint to the start block.
|
|
789
|
+
*/ async rollbackL1ToL2Messages(remoteMessagesState) {
|
|
790
|
+
const { treeInProgress: remoteTreeInProgress, messagesRollingHash: remoteRollingHash } = remoteMessagesState;
|
|
791
|
+
// Slowly go back through our messages until we find the last common message. We could query the logs in
|
|
792
|
+
// batch as an optimization, but the depth of the reorg should not be deep, and this is a very rare case,
|
|
793
|
+
// so it's fine to query one log at a time.
|
|
766
794
|
let commonMsg;
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
795
|
+
let messagesToDelete = 0;
|
|
796
|
+
this.log.verbose(`Searching most recent common L1 to L2 message`);
|
|
797
|
+
for await (const localMsg of this.store.iterateL1ToL2Messages({
|
|
798
|
+
reverse: true
|
|
771
799
|
})){
|
|
772
|
-
const remoteMsg = await this.retrieveL1ToL2Message(msg.leaf);
|
|
773
800
|
const logCtx = {
|
|
774
|
-
remoteMsg,
|
|
775
|
-
localMsg
|
|
801
|
+
remoteMsg: undefined,
|
|
802
|
+
localMsg,
|
|
803
|
+
remoteMessagesState
|
|
776
804
|
};
|
|
777
|
-
if
|
|
778
|
-
|
|
805
|
+
// First check if the local message rolling hash matches the current rolling hash of the inbox contract,
|
|
806
|
+
// which means we just need to rollback some local messages and we should be back in sync. This means there
|
|
807
|
+
// was an L1 reorg that removed some of the messages we had, but no new messages were added compared.
|
|
808
|
+
if (localMsg.rollingHash.equals(remoteRollingHash)) {
|
|
809
|
+
this.log.info(`Found common L1 to L2 message at index ${localMsg.index} on L1 block ${localMsg.l1BlockNumber} matching current remote state`, logCtx);
|
|
810
|
+
commonMsg = localMsg;
|
|
811
|
+
break;
|
|
812
|
+
}
|
|
813
|
+
// If there's no match with the current remote state, check if the message exists on the inbox contract at all
|
|
814
|
+
// by looking at the inbox events. If the L1 reorg *added* new messages in addition to deleting existing ones,
|
|
815
|
+
// then the current remote state's rolling hash will not match anything we have locally, so we need to check existence
|
|
816
|
+
// of individual messages via logs. Note we use logs and not historical queries so we don't have to depend on
|
|
817
|
+
// an archival rpc node, since the message could be from a long time ago if we're catching up with syncing.
|
|
818
|
+
const remoteMsg = await retrieveL1ToL2Message(this.inbox, localMsg);
|
|
819
|
+
logCtx.remoteMsg = remoteMsg;
|
|
820
|
+
if (remoteMsg && remoteMsg.rollingHash.equals(localMsg.rollingHash)) {
|
|
821
|
+
this.log.info(`Found most recent common L1 to L2 message at index ${localMsg.index} on L1 block ${localMsg.l1BlockNumber}`, logCtx);
|
|
779
822
|
commonMsg = remoteMsg;
|
|
780
823
|
break;
|
|
781
824
|
} else if (remoteMsg) {
|
|
782
|
-
this.log.debug(`Local L1 to L2 message with index ${
|
|
825
|
+
this.log.debug(`Local L1 to L2 message with index ${localMsg.index} has different rolling hash`, logCtx);
|
|
826
|
+
messagesToDelete++;
|
|
783
827
|
} else {
|
|
784
|
-
this.log.debug(`Local L1 to L2 message with index ${
|
|
828
|
+
this.log.debug(`Local L1 to L2 message with index ${localMsg.index} not found on L1`, logCtx);
|
|
829
|
+
messagesToDelete++;
|
|
785
830
|
}
|
|
786
831
|
}
|
|
787
|
-
// Delete everything after the common message we found.
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
832
|
+
// Delete everything after the common message we found, if anything needs to be deleted.
|
|
833
|
+
// Do not exit early if there are no messages to delete, we still want to update the syncpoint.
|
|
834
|
+
if (messagesToDelete > 0) {
|
|
835
|
+
const lastGoodIndex = commonMsg?.index;
|
|
836
|
+
this.log.warn(`Rolling back all local L1 to L2 messages after index ${lastGoodIndex ?? 'initial'}`);
|
|
837
|
+
await this.store.removeL1ToL2Messages(lastGoodIndex !== undefined ? lastGoodIndex + 1n : 0n);
|
|
838
|
+
}
|
|
791
839
|
// Update the syncpoint so the loop below reprocesses the changed messages. We go to the block before
|
|
792
840
|
// the last common one, so we force reprocessing it, in case new messages were added on that same L1 block
|
|
793
841
|
// after the last common message.
|
|
794
842
|
const syncPointL1BlockNumber = commonMsg ? commonMsg.l1BlockNumber - 1n : this.l1Constants.l1StartBlock;
|
|
795
843
|
const syncPointL1BlockHash = await this.getL1BlockHash(syncPointL1BlockNumber);
|
|
796
|
-
messagesSyncPoint = {
|
|
844
|
+
const messagesSyncPoint = {
|
|
797
845
|
l1BlockNumber: syncPointL1BlockNumber,
|
|
798
846
|
l1BlockHash: syncPointL1BlockHash
|
|
799
847
|
};
|
|
800
|
-
await this.store.
|
|
848
|
+
await this.store.setMessageSyncState(messagesSyncPoint, remoteTreeInProgress);
|
|
849
|
+
this.log.verbose(`Updated messages syncpoint to L1 block ${syncPointL1BlockNumber}`, {
|
|
850
|
+
...messagesSyncPoint,
|
|
851
|
+
remoteTreeInProgress
|
|
852
|
+
});
|
|
801
853
|
return messagesSyncPoint;
|
|
802
854
|
}
|
|
803
855
|
async getL1BlockHash(l1BlockNumber) {
|
|
@@ -837,7 +889,7 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
837
889
|
if (provenCheckpointNumber === 0) {
|
|
838
890
|
const localProvenCheckpointNumber = await this.store.getProvenCheckpointNumber();
|
|
839
891
|
if (localProvenCheckpointNumber !== provenCheckpointNumber) {
|
|
840
|
-
await this.
|
|
892
|
+
await this.updater.setProvenCheckpointNumber(provenCheckpointNumber);
|
|
841
893
|
this.log.info(`Rolled back proven chain to checkpoint ${provenCheckpointNumber}`, {
|
|
842
894
|
provenCheckpointNumber
|
|
843
895
|
});
|
|
@@ -854,20 +906,20 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
854
906
|
if (localCheckpointForDestinationProvenCheckpointNumber && provenArchive.equals(localCheckpointForDestinationProvenCheckpointNumber.archive.root)) {
|
|
855
907
|
const localProvenCheckpointNumber = await this.store.getProvenCheckpointNumber();
|
|
856
908
|
if (localProvenCheckpointNumber !== provenCheckpointNumber) {
|
|
857
|
-
await this.
|
|
909
|
+
await this.updater.setProvenCheckpointNumber(provenCheckpointNumber);
|
|
858
910
|
this.log.info(`Updated proven chain to checkpoint ${provenCheckpointNumber}`, {
|
|
859
911
|
provenCheckpointNumber
|
|
860
912
|
});
|
|
861
913
|
const provenSlotNumber = localCheckpointForDestinationProvenCheckpointNumber.header.slotNumber;
|
|
862
914
|
const provenEpochNumber = getEpochAtSlot(provenSlotNumber, this.l1Constants);
|
|
863
|
-
const lastBlockNumberInCheckpoint = localCheckpointForDestinationProvenCheckpointNumber.startBlock + localCheckpointForDestinationProvenCheckpointNumber.
|
|
915
|
+
const lastBlockNumberInCheckpoint = localCheckpointForDestinationProvenCheckpointNumber.startBlock + localCheckpointForDestinationProvenCheckpointNumber.blockCount - 1;
|
|
864
916
|
this.events.emit(L2BlockSourceEvents.L2BlockProven, {
|
|
865
917
|
type: L2BlockSourceEvents.L2BlockProven,
|
|
866
918
|
blockNumber: BlockNumber(lastBlockNumberInCheckpoint),
|
|
867
919
|
slotNumber: provenSlotNumber,
|
|
868
920
|
epochNumber: provenEpochNumber
|
|
869
921
|
});
|
|
870
|
-
this.instrumentation.
|
|
922
|
+
this.instrumentation.updateLastProvenCheckpoint(localCheckpointForDestinationProvenCheckpointNumber);
|
|
871
923
|
} else {
|
|
872
924
|
this.log.trace(`Proven checkpoint ${provenCheckpointNumber} already stored.`);
|
|
873
925
|
}
|
|
@@ -944,21 +996,37 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
944
996
|
do {
|
|
945
997
|
[searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, currentL1BlockNumber);
|
|
946
998
|
this.log.trace(`Retrieving checkpoints from L1 block ${searchStartBlock} to ${searchEndBlock}`);
|
|
947
|
-
//
|
|
948
|
-
const
|
|
949
|
-
if (
|
|
999
|
+
// First fetch calldata only, no blobs yet, since we may be able to just get that data out of the proposed chain
|
|
1000
|
+
const calldataCheckpoints = await execInSpan(this.tracer, 'Archiver.retrieveCheckpointCalldataFromRollup', ()=>retrieveCheckpointCalldataFromRollup(this.rollup, this.publicClient, this.debugClient, searchStartBlock, searchEndBlock, this.instrumentation, this.log));
|
|
1001
|
+
if (calldataCheckpoints.length === 0) {
|
|
950
1002
|
// We are not calling `setBlockSynchedL1BlockNumber` because it may cause sync issues if based off infura.
|
|
951
1003
|
// See further details in earlier comments.
|
|
952
1004
|
this.log.trace(`Retrieved no new checkpoints from L1 block ${searchStartBlock} to ${searchEndBlock}`);
|
|
953
1005
|
continue;
|
|
954
1006
|
}
|
|
955
|
-
this.log.debug(`Retrieved ${
|
|
956
|
-
lastProcessedCheckpoint:
|
|
1007
|
+
this.log.debug(`Retrieved ${calldataCheckpoints.length} new checkpoint calldata between L1 blocks ${searchStartBlock} and ${searchEndBlock}`, {
|
|
1008
|
+
lastProcessedCheckpoint: calldataCheckpoints[calldataCheckpoints.length - 1].l1,
|
|
957
1009
|
searchStartBlock,
|
|
958
1010
|
searchEndBlock
|
|
959
1011
|
});
|
|
960
|
-
|
|
1012
|
+
// Check if the last checkpoint matches the proposed one (so we can skip blob fetch).
|
|
1013
|
+
// We only check the last one because the proposed checkpoint is always the most recent one,
|
|
1014
|
+
// and if it's in a multi-checkpoint batch it will always be last (sorted by L1 block number).
|
|
1015
|
+
const lastCalldataCheckpoint = calldataCheckpoints[calldataCheckpoints.length - 1];
|
|
1016
|
+
const checkpointToPromote = await this.tryBuildPublishedCheckpointFromProposed(lastCalldataCheckpoint);
|
|
1017
|
+
// Then fetch blobs in parallel and build the full published checkpoints
|
|
1018
|
+
const toFetchBlobs = checkpointToPromote ? calldataCheckpoints.slice(0, -1) : calldataCheckpoints;
|
|
1019
|
+
const blobFetched = await asyncPool(10, toFetchBlobs, async (checkpoint)=>retrievedToPublishedCheckpoint({
|
|
1020
|
+
...checkpoint,
|
|
1021
|
+
checkpointBlobData: await getCheckpointBlobDataFromBlobs(this.blobClient, checkpoint.l1.blockHash, checkpoint.blobHashes, checkpoint.checkpointNumber, this.log, !initialSyncComplete, checkpoint.parentBeaconBlockRoot, checkpoint.l1.timestamp)
|
|
1022
|
+
}));
|
|
1023
|
+
// And add the promoted checkpoint to the list of all checkpoints
|
|
1024
|
+
const publishedCheckpoints = checkpointToPromote ? [
|
|
1025
|
+
...blobFetched,
|
|
1026
|
+
checkpointToPromote
|
|
1027
|
+
] : blobFetched;
|
|
961
1028
|
const validCheckpoints = [];
|
|
1029
|
+
// Now loop through all checkpoints and validate their attestations
|
|
962
1030
|
for (const published of publishedCheckpoints){
|
|
963
1031
|
const validationResult = this.config.skipValidateCheckpointAttestations ? {
|
|
964
1032
|
valid: true
|
|
@@ -1008,15 +1076,31 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
1008
1076
|
blocks: published.checkpoint.blocks.map((b)=>b.getStats())
|
|
1009
1077
|
});
|
|
1010
1078
|
}
|
|
1079
|
+
for (const published of validCheckpoints){
|
|
1080
|
+
this.instrumentation.processCheckpointL1Timing({
|
|
1081
|
+
slotNumber: published.checkpoint.header.slotNumber,
|
|
1082
|
+
l1Timestamp: published.l1.timestamp,
|
|
1083
|
+
l1Constants: this.l1Constants
|
|
1084
|
+
});
|
|
1085
|
+
}
|
|
1011
1086
|
try {
|
|
1012
1087
|
const updatedValidationResult = rollupStatus.validationResult === initialValidationResult ? undefined : rollupStatus.validationResult;
|
|
1013
|
-
|
|
1014
|
-
|
|
1088
|
+
// Split valid checkpoints: the promoted one (if any) is persisted via the proposed-promotion path,
|
|
1089
|
+
// the rest via addCheckpoints. Both paths run within the same store transaction for atomicity.
|
|
1090
|
+
const [[maybeValidCheckpointToPromote], checkpointsToAdd] = partition(validCheckpoints, (c)=>c.checkpoint.number === checkpointToPromote?.checkpoint.number);
|
|
1091
|
+
const [processDuration, result] = await elapsed(()=>execInSpan(this.tracer, 'Archiver.addCheckpoints', ()=>this.updater.addCheckpoints(checkpointsToAdd, updatedValidationResult, maybeValidCheckpointToPromote && {
|
|
1092
|
+
l1: lastCalldataCheckpoint.l1,
|
|
1093
|
+
attestations: lastCalldataCheckpoint.attestations,
|
|
1094
|
+
checkpoint: maybeValidCheckpointToPromote
|
|
1095
|
+
})));
|
|
1096
|
+
if (checkpointsToAdd.length > 0) {
|
|
1097
|
+
this.instrumentation.processNewCheckpointedBlocks(processDuration / checkpointsToAdd.length, checkpointsToAdd.flatMap((c)=>c.checkpoint.blocks));
|
|
1098
|
+
}
|
|
1015
1099
|
// If blocks were pruned due to conflict with L1 checkpoints, emit event
|
|
1016
1100
|
if (result.prunedBlocks && result.prunedBlocks.length > 0) {
|
|
1017
1101
|
const prunedCheckpointNumber = result.prunedBlocks[0].checkpointNumber;
|
|
1018
1102
|
const prunedSlotNumber = result.prunedBlocks[0].header.globalVariables.slotNumber;
|
|
1019
|
-
this.log.
|
|
1103
|
+
this.log.info(`Pruned ${result.prunedBlocks.length} mismatching blocks for checkpoint ${prunedCheckpointNumber}`, {
|
|
1020
1104
|
prunedBlocks: result.prunedBlocks.map((b)=>b.toBlockInfo()),
|
|
1021
1105
|
prunedSlotNumber,
|
|
1022
1106
|
prunedCheckpointNumber
|
|
@@ -1056,7 +1140,7 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
1056
1140
|
});
|
|
1057
1141
|
}
|
|
1058
1142
|
lastRetrievedCheckpoint = validCheckpoints.at(-1) ?? lastRetrievedCheckpoint;
|
|
1059
|
-
lastL1BlockWithCheckpoint =
|
|
1143
|
+
lastL1BlockWithCheckpoint = calldataCheckpoints.at(-1)?.l1.blockNumber ?? lastL1BlockWithCheckpoint;
|
|
1060
1144
|
}while (searchEndBlock < currentL1BlockNumber)
|
|
1061
1145
|
// Important that we update AFTER inserting the blocks.
|
|
1062
1146
|
await updateProvenCheckpoint();
|
|
@@ -1066,6 +1150,51 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
1066
1150
|
lastL1BlockWithCheckpoint
|
|
1067
1151
|
};
|
|
1068
1152
|
}
|
|
1153
|
+
/** Checks if this checkpoint matches the local proposed one, and if so, loads local data to build a synthetic published checkpoint. */ async tryBuildPublishedCheckpointFromProposed(calldataCheckpoint) {
|
|
1154
|
+
const proposed = await this.store.getProposedCheckpointOnly();
|
|
1155
|
+
if (this.config.skipPromoteProposedCheckpointDuringL1Sync || !proposed || !calldataCheckpoint || proposed.checkpointNumber !== calldataCheckpoint.checkpointNumber) {
|
|
1156
|
+
return undefined;
|
|
1157
|
+
}
|
|
1158
|
+
if (!proposed.header.equals(calldataCheckpoint.header) || !proposed.archive.root.equals(calldataCheckpoint.archiveRoot)) {
|
|
1159
|
+
this.log.warn(`Local proposed checkpoint ${proposed.checkpointNumber} does not match checkpoint retrieved from L1, overriding with L1 data`, {
|
|
1160
|
+
proposedCheckpointNumber: proposed.checkpointNumber,
|
|
1161
|
+
proposedHeader: proposed.header.toInspect(),
|
|
1162
|
+
proposedArchiveRoot: proposed.archive.root.toString(),
|
|
1163
|
+
calldataCheckpointNumber: calldataCheckpoint.checkpointNumber,
|
|
1164
|
+
calldataHeader: calldataCheckpoint.header.toInspect(),
|
|
1165
|
+
calldataArchiveRoot: calldataCheckpoint.archiveRoot.toString()
|
|
1166
|
+
});
|
|
1167
|
+
return undefined;
|
|
1168
|
+
}
|
|
1169
|
+
this.log.debug(`Building published checkpoint from proposed ${calldataCheckpoint.checkpointNumber} (skipping blob fetch)`, {
|
|
1170
|
+
proposedHeader: proposed.header.toInspect(),
|
|
1171
|
+
proposedArchiveRoot: proposed.archive.root.toString()
|
|
1172
|
+
});
|
|
1173
|
+
const blocks = await this.store.getBlocks(BlockNumber(proposed.startBlock), proposed.blockCount);
|
|
1174
|
+
if (blocks.length !== proposed.blockCount) {
|
|
1175
|
+
this.log.warn(`Local proposed checkpoint ${proposed.checkpointNumber} has wrong block count (expected ${proposed.blockCount} blocks starting at ${proposed.startBlock} but got ${blocks.length})`, {
|
|
1176
|
+
proposedCheckpointNumber: proposed.checkpointNumber,
|
|
1177
|
+
proposedStartBlock: proposed.startBlock,
|
|
1178
|
+
proposedBlockCount: proposed.blockCount,
|
|
1179
|
+
retrievedBlocks: blocks.map((b)=>b.number)
|
|
1180
|
+
});
|
|
1181
|
+
return undefined;
|
|
1182
|
+
}
|
|
1183
|
+
const checkpoint = Checkpoint.from({
|
|
1184
|
+
archive: proposed.archive,
|
|
1185
|
+
header: proposed.header,
|
|
1186
|
+
blocks,
|
|
1187
|
+
number: proposed.checkpointNumber,
|
|
1188
|
+
feeAssetPriceModifier: proposed.feeAssetPriceModifier
|
|
1189
|
+
});
|
|
1190
|
+
const promotedCheckpoint = PublishedCheckpoint.from({
|
|
1191
|
+
checkpoint,
|
|
1192
|
+
l1: calldataCheckpoint.l1,
|
|
1193
|
+
attestations: calldataCheckpoint.attestations
|
|
1194
|
+
});
|
|
1195
|
+
this.instrumentation.processCheckpointPromoted();
|
|
1196
|
+
return promotedCheckpoint;
|
|
1197
|
+
}
|
|
1069
1198
|
async checkForNewCheckpointsBeforeL1SyncPoint(status, blocksSynchedTo, currentL1BlockNumber) {
|
|
1070
1199
|
const { lastRetrievedCheckpoint, pendingCheckpointNumber } = status;
|
|
1071
1200
|
// Compare the last checkpoint we have (either retrieved in this round or loaded from store) with what the
|