@aztec/archiver 0.0.1-commit.343b43af6 → 0.0.1-commit.350e0a4d
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 +71 -16
- package/dest/archiver.d.ts.map +1 -1
- package/dest/archiver.js +295 -79
- package/dest/config.d.ts +7 -3
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +38 -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 +12 -7
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +49 -32
- 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 +9 -1
- package/dest/l1/calldata_retriever.d.ts.map +1 -1
- package/dest/l1/calldata_retriever.js +22 -11
- package/dest/l1/data_retrieval.d.ts +25 -11
- package/dest/l1/data_retrieval.d.ts.map +1 -1
- package/dest/l1/data_retrieval.js +25 -32
- 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 +32 -0
- package/dest/modules/data_source_base.d.ts +71 -48
- package/dest/modules/data_source_base.d.ts.map +1 -1
- package/dest/modules/data_source_base.js +250 -144
- 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 +169 -118
- package/dest/modules/instrumentation.d.ts +15 -2
- package/dest/modules/instrumentation.d.ts.map +1 -1
- package/dest/modules/instrumentation.js +48 -9
- package/dest/modules/l1_synchronizer.d.ts +13 -8
- package/dest/modules/l1_synchronizer.d.ts.map +1 -1
- package/dest/modules/l1_synchronizer.js +459 -221
- 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 +30 -7
- package/dest/modules/validation.d.ts.map +1 -1
- package/dest/modules/validation.js +35 -17
- package/dest/store/block_store.d.ts +176 -67
- package/dest/store/block_store.d.ts.map +1 -1
- package/dest/store/block_store.js +669 -233
- 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 +42 -68
- 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 +54 -2
- 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 +262 -408
- package/dest/store/log_store_codec.d.ts +78 -0
- package/dest/store/log_store_codec.d.ts.map +1 -0
- package/dest/store/log_store_codec.js +110 -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 +15 -3
- package/dest/test/fake_l1_state.d.ts.map +1 -1
- package/dest/test/fake_l1_state.js +91 -21
- package/dest/test/mock_l2_block_source.d.ts +48 -50
- package/dest/test/mock_l2_block_source.d.ts.map +1 -1
- package/dest/test/mock_l2_block_source.js +227 -178
- package/dest/test/mock_structs.d.ts +1 -1
- package/dest/test/mock_structs.d.ts.map +1 -1
- package/dest/test/mock_structs.js +2 -2
- package/dest/test/noop_l1_archiver.d.ts +14 -6
- package/dest/test/noop_l1_archiver.d.ts.map +1 -1
- package/dest/test/noop_l1_archiver.js +26 -10
- package/package.json +14 -14
- package/src/archiver.ts +383 -85
- package/src/config.ts +43 -13
- package/src/errors.ts +117 -2
- package/src/factory.ts +61 -27
- package/src/index.ts +18 -2
- package/src/l1/calldata_retriever.ts +29 -11
- package/src/l1/data_retrieval.ts +44 -46
- package/src/l1/validate_historical_logs.ts +140 -0
- package/src/modules/contract_data_source_adapter.ts +46 -0
- package/src/modules/data_source_base.ts +325 -187
- package/src/modules/data_store_updater.ts +204 -149
- package/src/modules/instrumentation.ts +43 -10
- package/src/modules/l1_synchronizer.ts +623 -265
- package/src/modules/outbox_trees_resolver.ts +199 -0
- package/src/modules/validation.ts +80 -23
- package/src/store/block_store.ts +822 -280
- package/src/store/contract_class_store.ts +49 -103
- package/src/store/contract_instance_store.ts +68 -5
- 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 +302 -559
- package/src/store/log_store_codec.ts +143 -0
- package/src/store/message_store.ts +59 -9
- package/src/structs/inbox_message.ts +1 -1
- package/src/test/fake_l1_state.ts +114 -32
- package/src/test/mock_l2_block_source.ts +284 -225
- package/src/test/mock_structs.ts +5 -2
- package/src/test/noop_l1_archiver.ts +54 -11
- 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 -476
- 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
|
-
import {
|
|
396
|
+
import { validateCheckpointAttestationsFromCalldata } 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,61 +490,58 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
477
490
|
}
|
|
478
491
|
}
|
|
479
492
|
async syncFromL1(initialSyncComplete) {
|
|
493
|
+
const blocksAdded = [];
|
|
494
|
+
// In between the various calls to L1, the block number can move meaning some of the following
|
|
495
|
+
// calls will return data for blocks that were not present during earlier calls. To combat this
|
|
496
|
+
// we ensure that all data retrieval methods only retrieve data up to the currentBlockNumber
|
|
497
|
+
// captured at the top of this function.
|
|
480
498
|
const currentL1Block = await this.publicClient.getBlock({
|
|
481
499
|
includeTransactions: false
|
|
482
500
|
});
|
|
483
501
|
const currentL1BlockNumber = currentL1Block.number;
|
|
484
502
|
const currentL1BlockHash = Buffer32.fromString(currentL1Block.hash);
|
|
485
503
|
const currentL1Timestamp = currentL1Block.timestamp;
|
|
504
|
+
const currentL1BlockData = {
|
|
505
|
+
l1BlockNumber: currentL1BlockNumber,
|
|
506
|
+
l1BlockHash: currentL1BlockHash
|
|
507
|
+
};
|
|
486
508
|
if (this.l1BlockHash && currentL1BlockHash.equals(this.l1BlockHash)) {
|
|
487
509
|
this.log.trace(`No new L1 blocks since last sync at L1 block ${this.l1BlockNumber}`);
|
|
488
|
-
return;
|
|
510
|
+
return blocksAdded;
|
|
489
511
|
}
|
|
490
|
-
//
|
|
512
|
+
// Log at error if the latest L1 block timestamp is too old
|
|
491
513
|
const maxAllowedDelay = this.config.maxAllowedEthClientDriftSeconds;
|
|
492
514
|
const now = this.dateProvider.nowInSeconds();
|
|
493
515
|
if (maxAllowedDelay > 0 && Number(currentL1Timestamp) <= now - maxAllowedDelay) {
|
|
494
|
-
this.log.
|
|
516
|
+
this.log.error(`Latest L1 block ${currentL1BlockNumber} timestamp ${currentL1Timestamp} is too old. Make sure your Ethereum node is synced.`, {
|
|
495
517
|
currentL1BlockNumber,
|
|
496
518
|
currentL1Timestamp,
|
|
497
519
|
now,
|
|
498
520
|
maxAllowedDelay
|
|
499
521
|
});
|
|
500
522
|
}
|
|
501
|
-
//
|
|
502
|
-
const
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
523
|
+
// Query finalized block on L1
|
|
524
|
+
const rawFinalizedL1Block = await getFinalizedL1Block(this.publicClient);
|
|
525
|
+
const finalizedL1Block = rawFinalizedL1Block && {
|
|
526
|
+
l1BlockNumber: rawFinalizedL1Block.number,
|
|
527
|
+
l1BlockHash: Buffer32.fromString(rawFinalizedL1Block.hash)
|
|
528
|
+
};
|
|
529
|
+
// Load sync point for blocks defaulting to start block
|
|
530
|
+
const { blocksSynchedTo = this.l1Constants.l1StartBlock } = await getArchiverSynchPoint(this.stores);
|
|
506
531
|
this.log.debug(`Starting new archiver sync iteration`, {
|
|
507
532
|
blocksSynchedTo,
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
currentL1BlockHash
|
|
533
|
+
currentL1BlockData,
|
|
534
|
+
finalizedL1Block
|
|
511
535
|
});
|
|
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 **********
|
|
536
|
+
// 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.
|
|
537
|
+
// 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
|
|
538
|
+
// block, when that wouldn't be the case, since L1 to L2 messages would need another iteration.
|
|
539
|
+
await retryTimes(()=>this.handleL1ToL2Messages(currentL1BlockData, finalizedL1Block), 'Handling L1 to L2 messages', 3, 0.1);
|
|
531
540
|
if (currentL1BlockNumber > blocksSynchedTo) {
|
|
532
541
|
// First we retrieve new checkpoints and L2 blocks and store them in the DB. This will also update the
|
|
533
542
|
// pending chain validation status, proven checkpoint number, and synched L1 block number.
|
|
534
543
|
const rollupStatus = await this.handleCheckpoints(blocksSynchedTo, currentL1BlockNumber, initialSyncComplete);
|
|
544
|
+
blocksAdded.push(...rollupStatus.blocksAdded);
|
|
535
545
|
// Then we try pruning uncheckpointed blocks if a new slot was mined without checkpoints
|
|
536
546
|
await this.pruneUncheckpointedBlocks(currentL1Timestamp);
|
|
537
547
|
// Then we prune the current epoch if it'd reorg on next submission.
|
|
@@ -540,23 +550,16 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
540
550
|
// the chain locally before we start unwinding stuff. This can be optimized by figuring out
|
|
541
551
|
// up to which point we're pruning, and then requesting checkpoints up to that point only.
|
|
542
552
|
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
553
|
// And lastly we check if we are missing any checkpoints behind us due to a possible L1 reorg.
|
|
550
554
|
// 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) {
|
|
555
|
+
// re-syncing the checkpoints we have just unwound above.
|
|
556
|
+
if (!rollupCanPrune) {
|
|
554
557
|
await this.checkForNewCheckpointsBeforeL1SyncPoint(rollupStatus, blocksSynchedTo, currentL1BlockNumber);
|
|
555
558
|
}
|
|
556
559
|
this.instrumentation.updateL1BlockHeight(currentL1BlockNumber);
|
|
557
560
|
}
|
|
558
561
|
// Update the finalized L2 checkpoint based on L1 finality.
|
|
559
|
-
await this.updateFinalizedCheckpoint();
|
|
562
|
+
await this.updateFinalizedCheckpoint(finalizedL1Block);
|
|
560
563
|
// After syncing has completed, update the current l1 block number and timestamp,
|
|
561
564
|
// otherwise we risk announcing to the world that we've synced to a given point,
|
|
562
565
|
// but the corresponding blocks have not been processed (see #12631).
|
|
@@ -569,18 +572,19 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
569
572
|
l1TimestampAtStart: currentL1Timestamp,
|
|
570
573
|
l1BlockNumberAtEnd
|
|
571
574
|
});
|
|
575
|
+
return blocksAdded;
|
|
572
576
|
}
|
|
573
|
-
/**
|
|
577
|
+
/** Updates the finalized checkpoint using the pre-fetched finalized L1 block from the current sync iteration. */ async updateFinalizedCheckpoint(finalizedL1Block) {
|
|
574
578
|
try {
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
}
|
|
579
|
-
const finalizedL1BlockNumber = finalizedL1Block.
|
|
579
|
+
if (!finalizedL1Block) {
|
|
580
|
+
this.log.trace(`Skipping finalized checkpoint update: L1 has no finalized block yet.`);
|
|
581
|
+
return;
|
|
582
|
+
}
|
|
583
|
+
const finalizedL1BlockNumber = finalizedL1Block.l1BlockNumber;
|
|
580
584
|
const finalizedCheckpointNumber = await this.rollup.getProvenCheckpointNumber({
|
|
581
585
|
blockNumber: finalizedL1BlockNumber
|
|
582
586
|
});
|
|
583
|
-
const localFinalizedCheckpointNumber = await this.
|
|
587
|
+
const localFinalizedCheckpointNumber = await this.stores.blocks.getFinalizedCheckpointNumber();
|
|
584
588
|
if (localFinalizedCheckpointNumber !== finalizedCheckpointNumber) {
|
|
585
589
|
await this.updater.setFinalizedCheckpointNumber(finalizedCheckpointNumber);
|
|
586
590
|
this.log.info(`Updated finalized chain to checkpoint ${finalizedCheckpointNumber}`, {
|
|
@@ -589,39 +593,47 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
589
593
|
});
|
|
590
594
|
}
|
|
591
595
|
} catch (err) {
|
|
592
|
-
|
|
596
|
+
// The rollup contract may not exist at the finalized L1 block right after deployment.
|
|
597
|
+
if (!err?.message?.includes('returned no data')) {
|
|
598
|
+
this.log.warn(`Failed to update finalized checkpoint: ${err}`);
|
|
599
|
+
}
|
|
593
600
|
}
|
|
594
601
|
}
|
|
595
602
|
/** Prune all proposed local blocks that should have been checkpointed by now. */ async pruneUncheckpointedBlocks(currentL1Timestamp) {
|
|
596
603
|
const [lastCheckpointedBlockNumber, lastProposedBlockNumber] = await Promise.all([
|
|
597
|
-
this.
|
|
598
|
-
this.
|
|
604
|
+
this.stores.blocks.getCheckpointedL2BlockNumber(),
|
|
605
|
+
this.stores.blocks.getLatestL2BlockNumber()
|
|
599
606
|
]);
|
|
600
607
|
// If there are no uncheckpointed blocks, we got nothing to do
|
|
601
608
|
if (lastProposedBlockNumber === lastCheckpointedBlockNumber) {
|
|
602
609
|
this.log.trace(`No uncheckpointed blocks to prune.`);
|
|
603
610
|
return;
|
|
604
611
|
}
|
|
605
|
-
// What's the slot of the first uncheckpointed block?
|
|
606
|
-
const firstUncheckpointedBlockNumber = BlockNumber(lastCheckpointedBlockNumber + 1);
|
|
607
|
-
const [firstUncheckpointedBlockHeader] = await this.store.getBlockHeaders(firstUncheckpointedBlockNumber, 1);
|
|
608
|
-
const firstUncheckpointedBlockSlot = firstUncheckpointedBlockHeader?.getSlot();
|
|
609
612
|
// What's the slot at the next L1 block? All blocks for slots strictly before this one should've been checkpointed by now.
|
|
610
613
|
const slotAtNextL1Block = getSlotAtNextL1Block(currentL1Timestamp, this.l1Constants);
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
614
|
+
const firstUncheckpointedBlockNumber = BlockNumber(lastCheckpointedBlockNumber + 1);
|
|
615
|
+
// What's the slot of the first uncheckpointed block?
|
|
616
|
+
const firstUncheckpointedBlockData = await this.stores.blocks.getBlockData({
|
|
617
|
+
number: firstUncheckpointedBlockNumber
|
|
618
|
+
});
|
|
619
|
+
const firstUncheckpointedBlockSlot = firstUncheckpointedBlockData?.header.getSlot();
|
|
620
|
+
if (firstUncheckpointedBlockSlot === undefined || firstUncheckpointedBlockSlot >= slotAtNextL1Block) {
|
|
621
|
+
return;
|
|
622
|
+
}
|
|
623
|
+
// Prune provisional blocks from slots that have ended without being checkpointed.
|
|
624
|
+
// This also clears any proposed checkpoint whose blocks are being pruned.
|
|
625
|
+
this.log.warn(`Pruning blocks after block ${lastCheckpointedBlockNumber} due to slot ${firstUncheckpointedBlockSlot} not being checkpointed`, {
|
|
626
|
+
firstUncheckpointedBlockHeader: firstUncheckpointedBlockData?.header.toInspect(),
|
|
627
|
+
slotAtNextL1Block
|
|
628
|
+
});
|
|
629
|
+
const prunedBlocks = await this.updater.removeUncheckpointedBlocksAfter(lastCheckpointedBlockNumber);
|
|
630
|
+
if (prunedBlocks.length > 0) {
|
|
631
|
+
this.instrumentation.recordPrune('uncheckpointed');
|
|
632
|
+
this.events.emit(L2BlockSourceEvents.L2PruneUncheckpointed, {
|
|
633
|
+
type: L2BlockSourceEvents.L2PruneUncheckpointed,
|
|
634
|
+
slotNumber: firstUncheckpointedBlockSlot,
|
|
635
|
+
blocks: prunedBlocks
|
|
616
636
|
});
|
|
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
|
|
623
|
-
});
|
|
624
|
-
}
|
|
625
637
|
}
|
|
626
638
|
}
|
|
627
639
|
/** Queries the rollup contract on whether a prune can be executed on the immediate next L1 block. */ async canPrune(currentL1BlockNumber, currentL1Timestamp) {
|
|
@@ -640,7 +652,7 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
640
652
|
}
|
|
641
653
|
/** Checks if there'd be a reorg for the next checkpoint submission and start pruning now. */ async handleEpochPrune(provenCheckpointNumber, currentL1BlockNumber, currentL1Timestamp) {
|
|
642
654
|
const rollupCanPrune = await this.canPrune(currentL1BlockNumber, currentL1Timestamp);
|
|
643
|
-
const localPendingCheckpointNumber = await this.
|
|
655
|
+
const localPendingCheckpointNumber = await this.stores.blocks.getLatestCheckpointNumber();
|
|
644
656
|
const canPrune = localPendingCheckpointNumber > provenCheckpointNumber && rollupCanPrune;
|
|
645
657
|
if (canPrune) {
|
|
646
658
|
const timer = new Timer();
|
|
@@ -652,12 +664,14 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
652
664
|
const pruneFromSlotNumber = header.slotNumber;
|
|
653
665
|
const pruneFromEpochNumber = getEpochAtSlot(pruneFromSlotNumber, this.l1Constants);
|
|
654
666
|
const checkpointsToUnwind = localPendingCheckpointNumber - provenCheckpointNumber;
|
|
655
|
-
|
|
667
|
+
// Fetch checkpoints and blocks in bounded batches to avoid unbounded concurrent
|
|
668
|
+
// promises when the gap between local pending and proven checkpoint numbers is large.
|
|
669
|
+
const BATCH_SIZE = 10;
|
|
670
|
+
const indices = Array.from({
|
|
656
671
|
length: checkpointsToUnwind
|
|
657
|
-
}
|
|
658
|
-
const checkpoints = await
|
|
659
|
-
const
|
|
660
|
-
const newBlocks = blockPromises.filter(isDefined).flat();
|
|
672
|
+
}, (_, i)=>CheckpointNumber(i + pruneFrom));
|
|
673
|
+
const checkpoints = (await asyncPool(BATCH_SIZE, indices, (idx)=>this.stores.blocks.getCheckpointData(idx))).filter(isDefined);
|
|
674
|
+
const newBlocks = (await asyncPool(BATCH_SIZE, checkpoints, (cp)=>this.stores.blocks.getBlocksForCheckpoint(CheckpointNumber(cp.checkpointNumber)))).filter(isDefined).flat();
|
|
661
675
|
// Emit an event for listening services to react to the chain prune
|
|
662
676
|
this.events.emit(L2BlockSourceEvents.L2PruneUnproven, {
|
|
663
677
|
type: L2BlockSourceEvents.L2PruneUnproven,
|
|
@@ -666,11 +680,11 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
666
680
|
});
|
|
667
681
|
this.log.debug(`L2 prune from ${provenCheckpointNumber + 1} to ${localPendingCheckpointNumber} will occur on next checkpoint submission.`);
|
|
668
682
|
await this.updater.removeCheckpointsAfter(provenCheckpointNumber);
|
|
669
|
-
this.log.warn(`Removed ${count(checkpointsToUnwind, 'checkpoint')} after checkpoint ${provenCheckpointNumber} ` + `due to predicted reorg at L1 block ${currentL1BlockNumber}. ` + `Updated latest checkpoint is ${await this.
|
|
683
|
+
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()}.`);
|
|
670
684
|
this.instrumentation.processPrune(timer.ms());
|
|
671
685
|
// TODO(palla/reorg): Do we need to set the block synched L1 block number here?
|
|
672
686
|
// Seems like the next iteration should handle this.
|
|
673
|
-
// await this.
|
|
687
|
+
// await this.stores.blocks.setSynchedL1BlockNumber(currentL1BlockNumber);
|
|
674
688
|
}
|
|
675
689
|
return {
|
|
676
690
|
rollupCanPrune
|
|
@@ -691,59 +705,79 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
691
705
|
nextEnd
|
|
692
706
|
];
|
|
693
707
|
}
|
|
694
|
-
async handleL1ToL2Messages(
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
708
|
+
async handleL1ToL2Messages(currentL1Block, finalizedL1Block) {
|
|
709
|
+
// Load the syncpoint, which may have been updated in a previous iteration
|
|
710
|
+
const { messagesSynchedTo = {
|
|
711
|
+
l1BlockNumber: this.l1Constants.l1StartBlock,
|
|
712
|
+
l1BlockHash: this.l1Constants.l1StartBlockHash
|
|
713
|
+
} } = await getArchiverSynchPoint(this.stores);
|
|
714
|
+
// Nothing to do if L1 block number has not moved forward
|
|
715
|
+
const currentL1BlockNumber = currentL1Block.l1BlockNumber;
|
|
716
|
+
if (currentL1BlockNumber <= messagesSynchedTo.l1BlockNumber) {
|
|
717
|
+
return true;
|
|
698
718
|
}
|
|
699
|
-
//
|
|
700
|
-
const localMessagesInserted = await this.store.getTotalL1ToL2MessageCount();
|
|
701
|
-
const localLastMessage = await this.store.getLastL1ToL2Message();
|
|
719
|
+
// Compare local message store state with the remote. If they match, we just advance the match pointer.
|
|
702
720
|
const remoteMessagesState = await this.inbox.getState({
|
|
703
721
|
blockNumber: currentL1BlockNumber
|
|
704
722
|
});
|
|
705
|
-
this.
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
remoteMessagesState
|
|
709
|
-
|
|
710
|
-
// Compare message count and rolling hash. If they match, no need to retrieve anything.
|
|
711
|
-
if (remoteMessagesState.totalMessagesInserted === localMessagesInserted && remoteMessagesState.messagesRollingHash.equals(localLastMessage?.rollingHash ?? Buffer32.ZERO)) {
|
|
712
|
-
this.log.trace(`No L1 to L2 messages to query between L1 blocks ${messagesSyncPoint.l1BlockNumber} and ${currentL1BlockNumber}.`);
|
|
713
|
-
return;
|
|
723
|
+
const localLastMessage = await this.stores.messages.getLastMessage();
|
|
724
|
+
if (await this.localStateMatches(localLastMessage, remoteMessagesState)) {
|
|
725
|
+
this.log.trace(`Local L1 to L2 messages are already in sync with remote at L1 block ${currentL1BlockNumber}`);
|
|
726
|
+
await this.stores.messages.setMessageSyncState(currentL1Block, remoteMessagesState.treeInProgress, finalizedL1Block);
|
|
727
|
+
return true;
|
|
714
728
|
}
|
|
715
|
-
//
|
|
716
|
-
//
|
|
717
|
-
//
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
this.
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
this.log.warn(`Rolling back L1 to L2 messages due to hash mismatch or msg not found.`, {
|
|
726
|
-
remoteLastMessage,
|
|
727
|
-
messagesSyncPoint,
|
|
728
|
-
localLastMessage
|
|
729
|
-
});
|
|
730
|
-
messagesSyncPoint = await this.rollbackL1ToL2Messages(localLastMessage, messagesSyncPoint);
|
|
731
|
-
this.log.debug(`Rolled back L1 to L2 messages to L1 block ${messagesSyncPoint.l1BlockNumber}.`, {
|
|
732
|
-
messagesSyncPoint
|
|
729
|
+
// If not, then we are out of sync. Most likely there are new messages on the inbox, so we try retrieving them.
|
|
730
|
+
// However, it could also be the case that there was an L1 reorg and our syncpoint is no longer valid.
|
|
731
|
+
// If that's the case, we'd get an exception out of the message store since the rolling hash of the first message
|
|
732
|
+
// we try to insert would not match the one in the db, in which case we rollback to the last common message with L1.
|
|
733
|
+
try {
|
|
734
|
+
await this.retrieveAndStoreMessages(messagesSynchedTo.l1BlockNumber, currentL1BlockNumber);
|
|
735
|
+
} catch (error) {
|
|
736
|
+
if (isErrorClass(error, MessageStoreError)) {
|
|
737
|
+
this.log.warn(`Failed to store L1 to L2 messages retrieved from L1: ${error.message}. Rolling back syncpoint to retry.`, {
|
|
738
|
+
inboxMessage: error.inboxMessage
|
|
733
739
|
});
|
|
740
|
+
await this.rollbackL1ToL2Messages(remoteMessagesState);
|
|
741
|
+
return false;
|
|
734
742
|
}
|
|
743
|
+
throw error;
|
|
744
|
+
}
|
|
745
|
+
// Note that, if there are no new messages to insert, but there was an L1 reorg that pruned out last messages,
|
|
746
|
+
// we'd notice by comparing our local state with the remote one again, and seeing they don't match even after
|
|
747
|
+
// our sync attempt. In this case, we also rollback our syncpoint, and trigger a retry.
|
|
748
|
+
const localLastMessageAfterSync = await this.stores.messages.getLastMessage();
|
|
749
|
+
if (!await this.localStateMatches(localLastMessageAfterSync, remoteMessagesState)) {
|
|
750
|
+
this.log.warn(`Local L1 to L2 messages state does not match remote after sync attempt. Rolling back syncpoint to retry.`, {
|
|
751
|
+
localLastMessageAfterSync,
|
|
752
|
+
remoteMessagesState
|
|
753
|
+
});
|
|
754
|
+
await this.rollbackL1ToL2Messages(remoteMessagesState);
|
|
755
|
+
return false;
|
|
735
756
|
}
|
|
736
|
-
//
|
|
757
|
+
// Advance the syncpoint after a successful sync
|
|
758
|
+
await this.stores.messages.setMessageSyncState(currentL1Block, remoteMessagesState.treeInProgress, finalizedL1Block);
|
|
759
|
+
return true;
|
|
760
|
+
}
|
|
761
|
+
/** Checks if the local rolling hash and message count matches the remote state */ async localStateMatches(localLastMessage, remoteState) {
|
|
762
|
+
const localMessageCount = await this.stores.messages.getTotalL1ToL2MessageCount();
|
|
763
|
+
this.log.trace(`Comparing local and remote inbox state`, {
|
|
764
|
+
localMessageCount,
|
|
765
|
+
localLastMessage,
|
|
766
|
+
remoteState
|
|
767
|
+
});
|
|
768
|
+
return remoteState.totalMessagesInserted === localMessageCount && remoteState.messagesRollingHash.equals(localLastMessage?.rollingHash ?? Buffer16.ZERO);
|
|
769
|
+
}
|
|
770
|
+
/** Retrieves L1 to L2 messages from L1 in batches and stores them. */ async retrieveAndStoreMessages(fromL1Block, toL1Block) {
|
|
737
771
|
let searchStartBlock = 0n;
|
|
738
|
-
let searchEndBlock =
|
|
772
|
+
let searchEndBlock = fromL1Block;
|
|
739
773
|
let lastMessage;
|
|
740
774
|
let messageCount = 0;
|
|
741
775
|
do {
|
|
742
|
-
[searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock,
|
|
776
|
+
[searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, toL1Block);
|
|
743
777
|
this.log.trace(`Retrieving L1 to L2 messages in L1 blocks ${searchStartBlock}-${searchEndBlock}`);
|
|
744
778
|
const messages = await retrieveL1ToL2Messages(this.inbox, searchStartBlock, searchEndBlock);
|
|
745
779
|
const timer = new Timer();
|
|
746
|
-
await this.
|
|
780
|
+
await this.stores.messages.addL1ToL2Messages(messages);
|
|
747
781
|
const perMsg = timer.ms() / messages.length;
|
|
748
782
|
this.instrumentation.processNewMessages(messages.length, perMsg);
|
|
749
783
|
for (const msg of messages){
|
|
@@ -754,74 +788,94 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
754
788
|
lastMessage = msg;
|
|
755
789
|
messageCount++;
|
|
756
790
|
}
|
|
757
|
-
}while (searchEndBlock <
|
|
758
|
-
// Log stats for messages retrieved (if any).
|
|
791
|
+
}while (searchEndBlock < toL1Block)
|
|
759
792
|
if (messageCount > 0) {
|
|
760
793
|
this.log.info(`Retrieved ${messageCount} new L1 to L2 messages up to message with index ${lastMessage?.index} for checkpoint ${lastMessage?.checkpointNumber}`, {
|
|
761
794
|
lastMessage,
|
|
762
795
|
messageCount
|
|
763
796
|
});
|
|
764
797
|
}
|
|
765
|
-
// Warn if the resulting rolling hash does not match the remote state we had retrieved.
|
|
766
|
-
if (lastMessage && !lastMessage.rollingHash.equals(remoteMessagesState.messagesRollingHash)) {
|
|
767
|
-
this.log.warn(`Last message retrieved rolling hash does not match remote state.`, {
|
|
768
|
-
lastMessage,
|
|
769
|
-
remoteMessagesState
|
|
770
|
-
});
|
|
771
|
-
}
|
|
772
798
|
}
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
}while (searchEndBlock < currentL1BlockNumber)
|
|
784
|
-
return undefined;
|
|
785
|
-
}
|
|
786
|
-
async rollbackL1ToL2Messages(localLastMessage, messagesSyncPoint) {
|
|
787
|
-
// Slowly go back through our messages until we find the last common message.
|
|
788
|
-
// We could query the logs in batch as an optimization, but the depth of the reorg should not be deep, and this
|
|
789
|
-
// is a very rare case, so it's fine to query one log at a time.
|
|
799
|
+
/**
|
|
800
|
+
* 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.
|
|
801
|
+
* If no common message is found, rolls back all messages and sets the syncpoint to the start block.
|
|
802
|
+
*/ async rollbackL1ToL2Messages(remoteMessagesState) {
|
|
803
|
+
const { treeInProgress: remoteTreeInProgress, messagesRollingHash: remoteRollingHash } = remoteMessagesState;
|
|
804
|
+
const messagesFinalizedL1Block = await this.stores.messages.getMessagesFinalizedL1Block();
|
|
805
|
+
const finalizedL1BlockNumber = messagesFinalizedL1Block?.l1BlockNumber;
|
|
806
|
+
// Slowly go back through our messages until we find the last common message. We could query the logs in
|
|
807
|
+
// batch as an optimization, but the depth of the reorg should not be deep, and this is a very rare case,
|
|
808
|
+
// so it's fine to query one log at a time.
|
|
790
809
|
let commonMsg;
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
810
|
+
let messagesToDelete = 0;
|
|
811
|
+
this.log.verbose(`Searching most recent common L1 to L2 message`);
|
|
812
|
+
for await (const localMsg of this.stores.messages.iterateL1ToL2Messages({
|
|
813
|
+
reverse: true
|
|
795
814
|
})){
|
|
796
|
-
const remoteMsg = await this.retrieveL1ToL2Message(msg.leaf);
|
|
797
815
|
const logCtx = {
|
|
798
|
-
remoteMsg,
|
|
799
|
-
localMsg
|
|
816
|
+
remoteMsg: undefined,
|
|
817
|
+
localMsg,
|
|
818
|
+
remoteMessagesState
|
|
800
819
|
};
|
|
801
|
-
if
|
|
802
|
-
|
|
820
|
+
// First check if the local message rolling hash matches the current rolling hash of the inbox contract,
|
|
821
|
+
// which means we just need to rollback some local messages and we should be back in sync. This means there
|
|
822
|
+
// was an L1 reorg that removed some of the messages we had, but no new messages were added compared.
|
|
823
|
+
if (localMsg.rollingHash.equals(remoteRollingHash)) {
|
|
824
|
+
this.log.info(`Found common L1 to L2 message at index ${localMsg.index} on L1 block ${localMsg.l1BlockNumber} matching current remote state`, logCtx);
|
|
825
|
+
commonMsg = localMsg;
|
|
826
|
+
break;
|
|
827
|
+
}
|
|
828
|
+
// Messages at or below the finalized L1 block cannot have been reorged — accept as common without querying L1.
|
|
829
|
+
if (finalizedL1BlockNumber !== undefined && localMsg.l1BlockNumber <= finalizedL1BlockNumber) {
|
|
830
|
+
this.log.info(`Found common L1 to L2 message at finalized L1 block ${localMsg.l1BlockNumber}`, logCtx);
|
|
831
|
+
commonMsg = localMsg;
|
|
832
|
+
break;
|
|
833
|
+
}
|
|
834
|
+
// If there's no match with the current remote state, check if the message exists on the inbox contract at all
|
|
835
|
+
// by looking at the inbox events. If the L1 reorg *added* new messages in addition to deleting existing ones,
|
|
836
|
+
// then the current remote state's rolling hash will not match anything we have locally, so we need to check existence
|
|
837
|
+
// of individual messages via logs. Note we use logs and not historical queries so we don't have to depend on
|
|
838
|
+
// an archival rpc node, since the message could be from a long time ago if we're catching up with syncing.
|
|
839
|
+
const remoteMsg = await retrieveL1ToL2Message(this.inbox, localMsg);
|
|
840
|
+
logCtx.remoteMsg = remoteMsg;
|
|
841
|
+
if (remoteMsg && remoteMsg.rollingHash.equals(localMsg.rollingHash)) {
|
|
842
|
+
this.log.info(`Found most recent common L1 to L2 message at index ${localMsg.index} on L1 block ${localMsg.l1BlockNumber}`, logCtx);
|
|
803
843
|
commonMsg = remoteMsg;
|
|
804
844
|
break;
|
|
805
845
|
} else if (remoteMsg) {
|
|
806
|
-
this.log.debug(`Local L1 to L2 message with index ${
|
|
846
|
+
this.log.debug(`Local L1 to L2 message with index ${localMsg.index} has different rolling hash`, logCtx);
|
|
847
|
+
messagesToDelete++;
|
|
807
848
|
} else {
|
|
808
|
-
this.log.debug(`Local L1 to L2 message with index ${
|
|
849
|
+
this.log.debug(`Local L1 to L2 message with index ${localMsg.index} not found on L1`, logCtx);
|
|
850
|
+
messagesToDelete++;
|
|
809
851
|
}
|
|
810
852
|
}
|
|
811
|
-
// Delete everything after the common message we found.
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
853
|
+
// Delete everything after the common message we found, if anything needs to be deleted.
|
|
854
|
+
// Do not exit early if there are no messages to delete, we still want to update the syncpoint.
|
|
855
|
+
if (messagesToDelete > 0) {
|
|
856
|
+
const lastGoodIndex = commonMsg?.index;
|
|
857
|
+
this.log.warn(`Rolling back all local L1 to L2 messages after index ${lastGoodIndex ?? 'initial'}`);
|
|
858
|
+
await this.stores.messages.removeL1ToL2Messages(lastGoodIndex !== undefined ? lastGoodIndex + 1n : 0n);
|
|
859
|
+
}
|
|
815
860
|
// Update the syncpoint so the loop below reprocesses the changed messages. We go to the block before
|
|
816
861
|
// the last common one, so we force reprocessing it, in case new messages were added on that same L1 block
|
|
817
|
-
// after the last common message.
|
|
818
|
-
|
|
819
|
-
const
|
|
820
|
-
|
|
862
|
+
// after the last common message. Cap at the finalized L1 block: messages at or below finalized cannot
|
|
863
|
+
// have been reorged, so there is no need to walk back any further than that.
|
|
864
|
+
const syncPointL1BlockNumber = maxBigint(...compactArray([
|
|
865
|
+
commonMsg ? commonMsg.l1BlockNumber - 1n : undefined,
|
|
866
|
+
finalizedL1BlockNumber,
|
|
867
|
+
this.l1Constants.l1StartBlock
|
|
868
|
+
]));
|
|
869
|
+
const syncPointL1BlockHash = syncPointL1BlockNumber === finalizedL1BlockNumber ? messagesFinalizedL1Block.l1BlockHash : await this.getL1BlockHash(syncPointL1BlockNumber);
|
|
870
|
+
const messagesSyncPoint = {
|
|
821
871
|
l1BlockNumber: syncPointL1BlockNumber,
|
|
822
872
|
l1BlockHash: syncPointL1BlockHash
|
|
823
873
|
};
|
|
824
|
-
await this.
|
|
874
|
+
await this.stores.messages.setMessageSyncState(messagesSyncPoint, remoteTreeInProgress);
|
|
875
|
+
this.log.verbose(`Updated messages syncpoint to L1 block ${messagesSyncPoint.l1BlockNumber}`, {
|
|
876
|
+
...messagesSyncPoint,
|
|
877
|
+
remoteTreeInProgress
|
|
878
|
+
});
|
|
825
879
|
return messagesSyncPoint;
|
|
826
880
|
}
|
|
827
881
|
async getL1BlockHash(l1BlockNumber) {
|
|
@@ -835,8 +889,9 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
835
889
|
return Buffer32.fromString(block.hash);
|
|
836
890
|
}
|
|
837
891
|
async handleCheckpoints(blocksSynchedTo, currentL1BlockNumber, initialSyncComplete) {
|
|
838
|
-
const
|
|
839
|
-
const
|
|
892
|
+
const blocksAdded = [];
|
|
893
|
+
const localPendingCheckpointNumber = await this.stores.blocks.getLatestCheckpointNumber();
|
|
894
|
+
const initialValidationResult = await this.stores.blocks.getPendingChainValidationStatus();
|
|
840
895
|
const { provenCheckpointNumber, provenArchive, pendingCheckpointNumber, pendingArchive, archiveOfMyCheckpoint: archiveForLocalPendingCheckpointNumber } = await execInSpan(this.tracer, 'Archiver.getRollupStatus', ()=>this.rollup.status(localPendingCheckpointNumber, {
|
|
841
896
|
blockNumber: currentL1BlockNumber
|
|
842
897
|
}));
|
|
@@ -845,7 +900,8 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
845
900
|
provenArchive: provenArchive.toString(),
|
|
846
901
|
pendingCheckpointNumber,
|
|
847
902
|
pendingArchive: pendingArchive.toString(),
|
|
848
|
-
validationResult: initialValidationResult
|
|
903
|
+
validationResult: initialValidationResult,
|
|
904
|
+
blocksAdded
|
|
849
905
|
};
|
|
850
906
|
this.log.trace(`Retrieved rollup status at current L1 block ${currentL1BlockNumber}.`, {
|
|
851
907
|
localPendingCheckpointNumber,
|
|
@@ -859,7 +915,7 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
859
915
|
// we need to set it to zero. This is an edge case because we dont have a checkpoint zero (initial checkpoint is one),
|
|
860
916
|
// so localCheckpointForDestinationProvenCheckpointNumber would not be found below.
|
|
861
917
|
if (provenCheckpointNumber === 0) {
|
|
862
|
-
const localProvenCheckpointNumber = await this.
|
|
918
|
+
const localProvenCheckpointNumber = await this.stores.blocks.getProvenCheckpointNumber();
|
|
863
919
|
if (localProvenCheckpointNumber !== provenCheckpointNumber) {
|
|
864
920
|
await this.updater.setProvenCheckpointNumber(provenCheckpointNumber);
|
|
865
921
|
this.log.info(`Rolled back proven chain to checkpoint ${provenCheckpointNumber}`, {
|
|
@@ -867,16 +923,16 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
867
923
|
});
|
|
868
924
|
}
|
|
869
925
|
}
|
|
870
|
-
const localCheckpointForDestinationProvenCheckpointNumber = await this.
|
|
926
|
+
const localCheckpointForDestinationProvenCheckpointNumber = await this.stores.blocks.getCheckpointData(provenCheckpointNumber);
|
|
871
927
|
// Sanity check. I've hit what seems to be a state where the proven checkpoint is set to a value greater than the latest
|
|
872
928
|
// synched checkpoint when requesting L2Tips from the archiver. This is the only place where the proven checkpoint is set.
|
|
873
|
-
const synched = await this.
|
|
929
|
+
const synched = await this.stores.blocks.getLatestCheckpointNumber();
|
|
874
930
|
if (localCheckpointForDestinationProvenCheckpointNumber && synched < localCheckpointForDestinationProvenCheckpointNumber.checkpointNumber) {
|
|
875
931
|
this.log.error(`Hit local checkpoint greater than last synched checkpoint: ${localCheckpointForDestinationProvenCheckpointNumber.checkpointNumber} > ${synched}`);
|
|
876
932
|
}
|
|
877
933
|
this.log.trace(`Local checkpoint for remote proven checkpoint ${provenCheckpointNumber} is ${localCheckpointForDestinationProvenCheckpointNumber?.archive.root.toString() ?? 'undefined'}`);
|
|
878
934
|
if (localCheckpointForDestinationProvenCheckpointNumber && provenArchive.equals(localCheckpointForDestinationProvenCheckpointNumber.archive.root)) {
|
|
879
|
-
const localProvenCheckpointNumber = await this.
|
|
935
|
+
const localProvenCheckpointNumber = await this.stores.blocks.getProvenCheckpointNumber();
|
|
880
936
|
if (localProvenCheckpointNumber !== provenCheckpointNumber) {
|
|
881
937
|
await this.updater.setProvenCheckpointNumber(provenCheckpointNumber);
|
|
882
938
|
this.log.info(`Updated proven chain to checkpoint ${provenCheckpointNumber}`, {
|
|
@@ -901,7 +957,7 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
901
957
|
// If we have 0 checkpoints locally and there are no checkpoints onchain there is nothing to do.
|
|
902
958
|
const noCheckpoints = localPendingCheckpointNumber === 0 && pendingCheckpointNumber === 0;
|
|
903
959
|
if (noCheckpoints) {
|
|
904
|
-
await this.
|
|
960
|
+
await this.stores.blocks.setSynchedL1BlockNumber(currentL1BlockNumber);
|
|
905
961
|
this.log.debug(`No checkpoints to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}, no checkpoints on chain`);
|
|
906
962
|
return rollupStatus;
|
|
907
963
|
}
|
|
@@ -909,7 +965,7 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
909
965
|
// Related to the L2 reorgs of the pending chain. We are only interested in actually addressing a reorg if there
|
|
910
966
|
// are any state that could be impacted by it. If we have no checkpoints, there is no impact.
|
|
911
967
|
if (localPendingCheckpointNumber > 0) {
|
|
912
|
-
const localPendingCheckpoint = await this.
|
|
968
|
+
const localPendingCheckpoint = await this.stores.blocks.getCheckpointData(localPendingCheckpointNumber);
|
|
913
969
|
if (localPendingCheckpoint === undefined) {
|
|
914
970
|
throw new Error(`Missing checkpoint ${localPendingCheckpointNumber}`);
|
|
915
971
|
}
|
|
@@ -923,7 +979,7 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
923
979
|
// However, in the re-org scenario, our L1 node is temporarily lying to us and we end up potentially missing checkpoints.
|
|
924
980
|
// We must only set this block number based on actually retrieved logs.
|
|
925
981
|
// TODO(#8621): Tackle this properly when we handle L1 Re-orgs.
|
|
926
|
-
// await this.
|
|
982
|
+
// await this.stores.blocks.setSynchedL1BlockNumber(currentL1BlockNumber);
|
|
927
983
|
this.log.debug(`No checkpoints to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}`);
|
|
928
984
|
return rollupStatus;
|
|
929
985
|
}
|
|
@@ -940,7 +996,7 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
940
996
|
});
|
|
941
997
|
let tipAfterUnwind = localPendingCheckpointNumber;
|
|
942
998
|
while(true){
|
|
943
|
-
const candidateCheckpoint = await this.
|
|
999
|
+
const candidateCheckpoint = await this.stores.blocks.getCheckpointData(tipAfterUnwind);
|
|
944
1000
|
if (candidateCheckpoint === undefined) {
|
|
945
1001
|
break;
|
|
946
1002
|
}
|
|
@@ -956,7 +1012,10 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
956
1012
|
}
|
|
957
1013
|
const checkpointsToRemove = localPendingCheckpointNumber - tipAfterUnwind;
|
|
958
1014
|
await this.updater.removeCheckpointsAfter(CheckpointNumber(tipAfterUnwind));
|
|
959
|
-
|
|
1015
|
+
if (checkpointsToRemove > 0) {
|
|
1016
|
+
this.instrumentation.recordPrune('l1_mismatch');
|
|
1017
|
+
}
|
|
1018
|
+
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()}.`);
|
|
960
1019
|
}
|
|
961
1020
|
}
|
|
962
1021
|
// Retrieve checkpoints in batches. Each batch is estimated to accommodate up to 'blockBatchSize' L1 blocks,
|
|
@@ -964,41 +1023,64 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
964
1023
|
let searchStartBlock = blocksSynchedTo;
|
|
965
1024
|
let searchEndBlock = blocksSynchedTo;
|
|
966
1025
|
let lastRetrievedCheckpoint;
|
|
967
|
-
let
|
|
1026
|
+
let lastSeenCheckpoint;
|
|
968
1027
|
do {
|
|
969
1028
|
[searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, currentL1BlockNumber);
|
|
970
1029
|
this.log.trace(`Retrieving checkpoints from L1 block ${searchStartBlock} to ${searchEndBlock}`);
|
|
971
|
-
//
|
|
972
|
-
const
|
|
973
|
-
if (
|
|
1030
|
+
// First fetch calldata only, no blobs yet, since we may be able to just get that data out of the proposed chain
|
|
1031
|
+
const calldataCheckpoints = await execInSpan(this.tracer, 'Archiver.retrieveCheckpointCalldataFromRollup', ()=>retrieveCheckpointCalldataFromRollup(this.rollup, this.publicClient, this.debugClient, searchStartBlock, searchEndBlock, this.instrumentation, this.log));
|
|
1032
|
+
if (calldataCheckpoints.length === 0) {
|
|
974
1033
|
// We are not calling `setBlockSynchedL1BlockNumber` because it may cause sync issues if based off infura.
|
|
975
1034
|
// See further details in earlier comments.
|
|
976
1035
|
this.log.trace(`Retrieved no new checkpoints from L1 block ${searchStartBlock} to ${searchEndBlock}`);
|
|
977
1036
|
continue;
|
|
978
1037
|
}
|
|
979
|
-
this.log.debug(`Retrieved ${
|
|
980
|
-
lastProcessedCheckpoint:
|
|
1038
|
+
this.log.debug(`Retrieved ${calldataCheckpoints.length} new checkpoint calldata between L1 blocks ${searchStartBlock} and ${searchEndBlock}`, {
|
|
1039
|
+
lastProcessedCheckpoint: calldataCheckpoints[calldataCheckpoints.length - 1].l1,
|
|
981
1040
|
searchStartBlock,
|
|
982
1041
|
searchEndBlock
|
|
983
1042
|
});
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
1043
|
+
// Check if the last checkpoint matches a local pending entry (so we can skip blob fetch).
|
|
1044
|
+
// We only check the last one; if it matches, the blob fetch is skipped for that entry.
|
|
1045
|
+
// TODO(palla/pipelining): We may have more than a single checkpoint to promote
|
|
1046
|
+
const lastCalldataCheckpoint = calldataCheckpoints[calldataCheckpoints.length - 1];
|
|
1047
|
+
const promoteResult = await this.tryBuildPublishedCheckpointFromProposed(lastCalldataCheckpoint);
|
|
1048
|
+
const checkpointToPromote = promoteResult && !('diverged' in promoteResult) ? promoteResult : undefined;
|
|
1049
|
+
const evictProposedFrom = promoteResult && 'diverged' in promoteResult ? promoteResult.fromCheckpointNumber : undefined;
|
|
1050
|
+
// Validate attestations from CALLDATA before fetching any blobs. A checkpoint with invalid
|
|
1051
|
+
// attestations (or one descending from a rejected ancestor) is rejected here without fetching its
|
|
1052
|
+
// blobs, so a malformed blob does not throw during decode before the rejection path runs and
|
|
1053
|
+
// stall sync. The signed consensus payload (header, archive root, fee asset price
|
|
1054
|
+
// modifier) is fully available from calldata.
|
|
1055
|
+
const checkpointsToIngest = [];
|
|
1056
|
+
for (const calldataCheckpoint of calldataCheckpoints){
|
|
1057
|
+
// Check the attestations uploaded by the publisher to L1 are correct.
|
|
1058
|
+
// Rollup contract does not validate attestations to save on gas, so this
|
|
1059
|
+
// falls on the nodes to verify offchain and skip those checkpoints.
|
|
987
1060
|
const validationResult = this.config.skipValidateCheckpointAttestations ? {
|
|
988
1061
|
valid: true
|
|
989
|
-
} : await
|
|
990
|
-
//
|
|
1062
|
+
} : await validateCheckpointAttestationsFromCalldata(calldataCheckpoint, this.epochCache, this.l1Constants, this.getSignatureContext(), this.log);
|
|
1063
|
+
// Also skip the checkpoint if it builds on a previously-rejected ancestor. Without
|
|
1064
|
+
// this, addCheckpoints would throw InitialCheckpointNumberNotSequentialError when the
|
|
1065
|
+
// ancestor was skipped earlier (e.g. due to invalid attestations), the catch handler
|
|
1066
|
+
// would roll back the L1 sync point, and the next iteration would re-fetch and re-throw.
|
|
1067
|
+
const rejectedAncestor = await this.stores.blocks.getRejectedCheckpointByArchiveRoot(calldataCheckpoint.header.lastArchiveRoot);
|
|
1068
|
+
// Update the validation result if it has changed, so we can keep track of the first invalid checkpoint
|
|
991
1069
|
// in case there is a sequence of more than one invalid checkpoint, as we need to invalidate the first one.
|
|
992
1070
|
// There is an exception though: if a checkpoint is invalidated and replaced with another invalid checkpoint,
|
|
993
1071
|
// we need to update the validation result, since we need to be able to invalidate the new one.
|
|
994
1072
|
// See test 'chain progresses if an invalid checkpoint is invalidated with an invalid one' for more info.
|
|
995
|
-
if
|
|
1073
|
+
// Do not update the validation result if there is a rejected ancestor, since in that case we want to keep the
|
|
1074
|
+
// original invalidation, as the new checkpoint is extending from a previous invalid one.
|
|
1075
|
+
const validStatusChanged = rollupStatus.validationResult?.valid !== validationResult.valid;
|
|
1076
|
+
const invalidStatusWithSameCheckpointNumber = !validationResult.valid && rollupStatus.validationResult && !rollupStatus.validationResult.valid && rollupStatus.validationResult.checkpoint.checkpointNumber === validationResult.checkpoint.checkpointNumber;
|
|
1077
|
+
if (!rejectedAncestor && (validStatusChanged || invalidStatusWithSameCheckpointNumber)) {
|
|
996
1078
|
rollupStatus.validationResult = validationResult;
|
|
997
1079
|
}
|
|
998
1080
|
if (!validationResult.valid) {
|
|
999
|
-
this.log.warn(`Skipping checkpoint ${
|
|
1000
|
-
|
|
1001
|
-
l1BlockNumber:
|
|
1081
|
+
this.log.warn(`Skipping checkpoint ${calldataCheckpoint.checkpointNumber} due to invalid attestations`, {
|
|
1082
|
+
checkpointNumber: calldataCheckpoint.checkpointNumber,
|
|
1083
|
+
l1BlockNumber: calldataCheckpoint.l1.blockNumber,
|
|
1002
1084
|
...pick(validationResult, 'reason')
|
|
1003
1085
|
});
|
|
1004
1086
|
// Emit event for invalid checkpoint detection
|
|
@@ -1006,12 +1088,80 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
1006
1088
|
type: L2BlockSourceEvents.InvalidAttestationsCheckpointDetected,
|
|
1007
1089
|
validationResult
|
|
1008
1090
|
});
|
|
1091
|
+
// Persist a rejected-ancestor entry so any later checkpoint that builds on this one
|
|
1092
|
+
// is detected and skipped (rather than tripping the addCheckpoints consecutive-number
|
|
1093
|
+
// check and causing the sync point to roll back in a loop).
|
|
1094
|
+
await this.stores.blocks.addRejectedCheckpoint({
|
|
1095
|
+
checkpointNumber: calldataCheckpoint.checkpointNumber,
|
|
1096
|
+
archiveRoot: calldataCheckpoint.archiveRoot,
|
|
1097
|
+
parentArchiveRoot: calldataCheckpoint.header.lastArchiveRoot,
|
|
1098
|
+
slotNumber: calldataCheckpoint.header.slotNumber,
|
|
1099
|
+
l1: calldataCheckpoint.l1,
|
|
1100
|
+
reason: 'invalid-attestations'
|
|
1101
|
+
});
|
|
1102
|
+
continue;
|
|
1103
|
+
}
|
|
1104
|
+
if (rejectedAncestor) {
|
|
1105
|
+
const descendantInfo = {
|
|
1106
|
+
archive: calldataCheckpoint.archiveRoot,
|
|
1107
|
+
lastArchive: calldataCheckpoint.header.lastArchiveRoot,
|
|
1108
|
+
slotNumber: calldataCheckpoint.header.slotNumber,
|
|
1109
|
+
checkpointNumber: calldataCheckpoint.checkpointNumber,
|
|
1110
|
+
timestamp: calldataCheckpoint.header.timestamp
|
|
1111
|
+
};
|
|
1112
|
+
this.log.warn(`Skipping checkpoint ${calldataCheckpoint.checkpointNumber} as it is a descendant of ` + `rejected checkpoint ${rejectedAncestor.checkpointNumber} (${rejectedAncestor.reason})`, {
|
|
1113
|
+
checkpointNumber: calldataCheckpoint.checkpointNumber,
|
|
1114
|
+
l1BlockNumber: calldataCheckpoint.l1.blockNumber,
|
|
1115
|
+
l1BlockHash: calldataCheckpoint.l1.blockHash,
|
|
1116
|
+
ancestorCheckpointNumber: rejectedAncestor.checkpointNumber,
|
|
1117
|
+
ancestorArchiveRoot: rejectedAncestor.archiveRoot.toString(),
|
|
1118
|
+
ancestorReason: rejectedAncestor.reason
|
|
1119
|
+
});
|
|
1120
|
+
this.events.emit(L2BlockSourceEvents.DescendentOfInvalidAttestationsCheckpointDetected, {
|
|
1121
|
+
type: L2BlockSourceEvents.DescendentOfInvalidAttestationsCheckpointDetected,
|
|
1122
|
+
checkpoint: descendantInfo,
|
|
1123
|
+
ancestorArchiveRoot: rejectedAncestor.archiveRoot,
|
|
1124
|
+
ancestorCheckpointNumber: rejectedAncestor.checkpointNumber
|
|
1125
|
+
});
|
|
1126
|
+
// Persist this chainpoint as rejected as well, so we can construct a chain of
|
|
1127
|
+
// skipped checkpoints starting from the first one with invalid attestations.
|
|
1128
|
+
await this.stores.blocks.addRejectedCheckpoint({
|
|
1129
|
+
checkpointNumber: calldataCheckpoint.checkpointNumber,
|
|
1130
|
+
archiveRoot: calldataCheckpoint.archiveRoot,
|
|
1131
|
+
parentArchiveRoot: calldataCheckpoint.header.lastArchiveRoot,
|
|
1132
|
+
slotNumber: calldataCheckpoint.header.slotNumber,
|
|
1133
|
+
l1: calldataCheckpoint.l1,
|
|
1134
|
+
reason: 'descends-from-invalid-attestations'
|
|
1135
|
+
});
|
|
1009
1136
|
continue;
|
|
1010
1137
|
}
|
|
1138
|
+
checkpointsToIngest.push(calldataCheckpoint);
|
|
1139
|
+
}
|
|
1140
|
+
// Fetch blobs in parallel only for the surviving (attestation-valid, non-descendant) checkpoints,
|
|
1141
|
+
// then build the full published checkpoints. The last calldata checkpoint may be promotable from a
|
|
1142
|
+
// local proposed block (checkpointToPromote), in which case it carries no blob to fetch. A missing or
|
|
1143
|
+
// undecodable blob throws and propagates, rolling back the L1 sync point so the fetch is retried.
|
|
1144
|
+
const toFetchBlobs = checkpointToPromote ? checkpointsToIngest.filter((c)=>c.checkpointNumber !== checkpointToPromote.checkpoint.number) : checkpointsToIngest;
|
|
1145
|
+
const blobFetched = await asyncPool(10, toFetchBlobs, async (checkpoint)=>retrievedToPublishedCheckpoint({
|
|
1146
|
+
...checkpoint,
|
|
1147
|
+
checkpointBlobData: await getCheckpointBlobDataFromBlobs(this.blobClient, checkpoint.l1.blockHash, checkpoint.blobHashes, checkpoint.checkpointNumber, this.log, !initialSyncComplete, checkpoint.parentBeaconBlockRoot, checkpoint.l1.timestamp)
|
|
1148
|
+
}));
|
|
1149
|
+
// Index the built checkpoints by number so we can ingest them in calldata order, slotting in the
|
|
1150
|
+
// promoted checkpoint (built from a local proposed block rather than blobs).
|
|
1151
|
+
const publishedByNumber = new Map(blobFetched.map((published)=>[
|
|
1152
|
+
published.checkpoint.number,
|
|
1153
|
+
published
|
|
1154
|
+
]));
|
|
1155
|
+
if (checkpointToPromote) {
|
|
1156
|
+
publishedByNumber.set(checkpointToPromote.checkpoint.number, checkpointToPromote);
|
|
1157
|
+
}
|
|
1158
|
+
const validCheckpoints = [];
|
|
1159
|
+
for (const calldataCheckpoint of checkpointsToIngest){
|
|
1160
|
+
const published = publishedByNumber.get(calldataCheckpoint.checkpointNumber);
|
|
1011
1161
|
// Check the inHash of the checkpoint against the l1->l2 messages.
|
|
1012
1162
|
// The messages should've been synced up to the currentL1BlockNumber and must be available for the published
|
|
1013
1163
|
// checkpoints we just retrieved.
|
|
1014
|
-
const l1ToL2Messages = await this.
|
|
1164
|
+
const l1ToL2Messages = await this.stores.messages.getL1ToL2Messages(published.checkpoint.number);
|
|
1015
1165
|
const computedInHash = computeInHashFromL1ToL2Messages(l1ToL2Messages);
|
|
1016
1166
|
const publishedInHash = published.checkpoint.header.inHash;
|
|
1017
1167
|
if (!computedInHash.equals(publishedInHash)) {
|
|
@@ -1041,8 +1191,21 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
1041
1191
|
}
|
|
1042
1192
|
try {
|
|
1043
1193
|
const updatedValidationResult = rollupStatus.validationResult === initialValidationResult ? undefined : rollupStatus.validationResult;
|
|
1044
|
-
|
|
1045
|
-
|
|
1194
|
+
// Split valid checkpoints: the promoted one (if any) is persisted via the proposed-promotion path,
|
|
1195
|
+
// the rest via addCheckpoints. Both paths run within the same store transaction for atomicity.
|
|
1196
|
+
const [[maybeValidCheckpointToPromote], checkpointsToAdd] = partition(validCheckpoints, (c)=>c.checkpoint.number === checkpointToPromote?.checkpoint.number);
|
|
1197
|
+
const [processDuration, result] = await elapsed(()=>execInSpan(this.tracer, 'Archiver.addCheckpoints', ()=>this.updater.addCheckpoints(checkpointsToAdd, updatedValidationResult, maybeValidCheckpointToPromote && {
|
|
1198
|
+
l1: lastCalldataCheckpoint.l1,
|
|
1199
|
+
attestations: lastCalldataCheckpoint.attestations,
|
|
1200
|
+
checkpoint: maybeValidCheckpointToPromote
|
|
1201
|
+
}, evictProposedFrom)));
|
|
1202
|
+
if (validCheckpoints.length > 0) {
|
|
1203
|
+
this.instrumentation.processNewCheckpointedBlocks(processDuration / validCheckpoints.length, validCheckpoints.flatMap((c)=>c.checkpoint.blocks));
|
|
1204
|
+
}
|
|
1205
|
+
// Record blocks newly fetched from L1 checkpoint payloads as added. The promoted checkpoint (if any) is
|
|
1206
|
+
// excluded: its blocks were already in local archiver storage (added via the proposed-block path) so the
|
|
1207
|
+
// block stream does not need them re-downloaded.
|
|
1208
|
+
blocksAdded.push(...checkpointsToAdd.flatMap((c)=>c.checkpoint.blocks));
|
|
1046
1209
|
// If blocks were pruned due to conflict with L1 checkpoints, emit event
|
|
1047
1210
|
if (result.prunedBlocks && result.prunedBlocks.length > 0) {
|
|
1048
1211
|
const prunedCheckpointNumber = result.prunedBlocks[0].checkpointNumber;
|
|
@@ -1052,6 +1215,7 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
1052
1215
|
prunedSlotNumber,
|
|
1053
1216
|
prunedCheckpointNumber
|
|
1054
1217
|
});
|
|
1218
|
+
this.instrumentation.recordPrune('l1_conflict');
|
|
1055
1219
|
// Emit event for listening services to react to the prune.
|
|
1056
1220
|
// Note: slotNumber comes from the first pruned block. If pruned blocks theoretically spanned multiple slots,
|
|
1057
1221
|
// only one slot number would be reported (though in practice all blocks in a checkpoint span a single slot).
|
|
@@ -1064,11 +1228,15 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
1064
1228
|
} catch (err) {
|
|
1065
1229
|
if (err instanceof InitialCheckpointNumberNotSequentialError) {
|
|
1066
1230
|
const { previousCheckpointNumber, newCheckpointNumber } = err;
|
|
1067
|
-
const previousCheckpoint = previousCheckpointNumber ? await this.
|
|
1068
|
-
const
|
|
1069
|
-
|
|
1231
|
+
const previousCheckpoint = previousCheckpointNumber ? await this.stores.blocks.getCheckpointData(CheckpointNumber(previousCheckpointNumber)) : undefined;
|
|
1232
|
+
const lastFinalizedCheckpoint = await this.stores.blocks.getCheckpointData(await this.stores.blocks.getFinalizedCheckpointNumber());
|
|
1233
|
+
const updatedL1SyncPoint = previousCheckpoint?.l1.blockNumber ?? lastFinalizedCheckpoint?.l1.blockNumber ?? this.l1Constants.l1StartBlock;
|
|
1234
|
+
await this.stores.blocks.setSynchedL1BlockNumber(updatedL1SyncPoint);
|
|
1070
1235
|
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.`, {
|
|
1071
1236
|
previousCheckpointNumber,
|
|
1237
|
+
previousCheckpoint: previousCheckpoint?.header.toInspect(),
|
|
1238
|
+
lastFinalizedCheckpoint: lastFinalizedCheckpoint?.header.toInspect(),
|
|
1239
|
+
l1StartBlock: this.l1Constants.l1StartBlock,
|
|
1072
1240
|
newCheckpointNumber,
|
|
1073
1241
|
updatedL1SyncPoint
|
|
1074
1242
|
});
|
|
@@ -1087,45 +1255,115 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
1087
1255
|
});
|
|
1088
1256
|
}
|
|
1089
1257
|
lastRetrievedCheckpoint = validCheckpoints.at(-1) ?? lastRetrievedCheckpoint;
|
|
1090
|
-
|
|
1258
|
+
// The last checkpoint seen on L1 this batch (valid or rejected), tracked from calldata since
|
|
1259
|
+
// rejected checkpoints are no longer built into PublishedCheckpoints.
|
|
1260
|
+
lastSeenCheckpoint = lastCalldataCheckpoint;
|
|
1091
1261
|
}while (searchEndBlock < currentL1BlockNumber)
|
|
1092
1262
|
// Important that we update AFTER inserting the blocks.
|
|
1093
1263
|
await updateProvenCheckpoint();
|
|
1094
1264
|
return {
|
|
1095
1265
|
...rollupStatus,
|
|
1096
1266
|
lastRetrievedCheckpoint,
|
|
1097
|
-
|
|
1267
|
+
lastSeenCheckpoint
|
|
1098
1268
|
};
|
|
1099
1269
|
}
|
|
1270
|
+
/**
|
|
1271
|
+
* Checks if a specific checkpoint matches a local pending entry, and if so, loads local data to build
|
|
1272
|
+
* a synthetic published checkpoint (skipping blob fetch).
|
|
1273
|
+
*
|
|
1274
|
+
* Returns { diverged: true, fromCheckpointNumber } when the L1 checkpoint does NOT match local pending
|
|
1275
|
+
* data for that number, so the caller can evict the entire pending suffix >= fromCheckpointNumber
|
|
1276
|
+
* (those entries chain off the now-invalid local state) within the same addCheckpoints transaction.
|
|
1277
|
+
*/ async tryBuildPublishedCheckpointFromProposed(calldataCheckpoint) {
|
|
1278
|
+
if (this.config.skipPromoteProposedCheckpointDuringL1Sync || !calldataCheckpoint) {
|
|
1279
|
+
return undefined;
|
|
1280
|
+
}
|
|
1281
|
+
// Look up the specific pending entry for the checkpoint being mined, not just the tip
|
|
1282
|
+
const proposed = await this.stores.blocks.getProposedCheckpointByNumber(calldataCheckpoint.checkpointNumber);
|
|
1283
|
+
if (!proposed) {
|
|
1284
|
+
return undefined;
|
|
1285
|
+
}
|
|
1286
|
+
if (!proposed.header.equals(calldataCheckpoint.header) || !proposed.archive.root.equals(calldataCheckpoint.archiveRoot)) {
|
|
1287
|
+
this.log.warn(`Local proposed checkpoint ${proposed.checkpointNumber} does not match checkpoint retrieved from L1, overriding with L1 data`, {
|
|
1288
|
+
proposedCheckpointNumber: proposed.checkpointNumber,
|
|
1289
|
+
proposedHeader: proposed.header.toInspect(),
|
|
1290
|
+
proposedArchiveRoot: proposed.archive.root.toString(),
|
|
1291
|
+
calldataCheckpointNumber: calldataCheckpoint.checkpointNumber,
|
|
1292
|
+
calldataHeader: calldataCheckpoint.header.toInspect(),
|
|
1293
|
+
calldataArchiveRoot: calldataCheckpoint.archiveRoot.toString()
|
|
1294
|
+
});
|
|
1295
|
+
// Both the locally-proposed checkpoint and the L1-confirmed one are signed by the
|
|
1296
|
+
// slot proposer; emit a divergence event so the slasher can attribute equivocation.
|
|
1297
|
+
// Only emit when the slots match — uncheckpointed entries are pruned above so this
|
|
1298
|
+
// should always hold, but guard defensively to avoid mis-attributing a slash.
|
|
1299
|
+
if (proposed.header.slotNumber === calldataCheckpoint.header.slotNumber) {
|
|
1300
|
+
this.events.emit(L2BlockSourceEvents.CheckpointEquivocationDetected, {
|
|
1301
|
+
type: L2BlockSourceEvents.CheckpointEquivocationDetected,
|
|
1302
|
+
slotNumber: calldataCheckpoint.header.slotNumber,
|
|
1303
|
+
checkpointNumber: calldataCheckpoint.checkpointNumber,
|
|
1304
|
+
l1ArchiveRoot: calldataCheckpoint.archiveRoot,
|
|
1305
|
+
proposedArchiveRoot: proposed.archive.root
|
|
1306
|
+
});
|
|
1307
|
+
}
|
|
1308
|
+
// Return a divergence signal so the caller can evict pending >= this number
|
|
1309
|
+
return {
|
|
1310
|
+
diverged: true,
|
|
1311
|
+
fromCheckpointNumber: proposed.checkpointNumber
|
|
1312
|
+
};
|
|
1313
|
+
}
|
|
1314
|
+
this.log.debug(`Building published checkpoint from proposed ${calldataCheckpoint.checkpointNumber} (skipping blob fetch)`, {
|
|
1315
|
+
proposedHeader: proposed.header.toInspect(),
|
|
1316
|
+
proposedArchiveRoot: proposed.archive.root.toString()
|
|
1317
|
+
});
|
|
1318
|
+
const blocks = await this.stores.blocks.getBlocks({
|
|
1319
|
+
from: BlockNumber(proposed.startBlock),
|
|
1320
|
+
limit: proposed.blockCount
|
|
1321
|
+
});
|
|
1322
|
+
if (blocks.length !== proposed.blockCount) {
|
|
1323
|
+
this.log.warn(`Local proposed checkpoint ${proposed.checkpointNumber} has wrong block count (expected ${proposed.blockCount} blocks starting at ${proposed.startBlock} but got ${blocks.length})`, {
|
|
1324
|
+
proposedCheckpointNumber: proposed.checkpointNumber,
|
|
1325
|
+
proposedStartBlock: proposed.startBlock,
|
|
1326
|
+
proposedBlockCount: proposed.blockCount,
|
|
1327
|
+
retrievedBlocks: blocks.map((b)=>b.number)
|
|
1328
|
+
});
|
|
1329
|
+
return undefined;
|
|
1330
|
+
}
|
|
1331
|
+
const checkpoint = Checkpoint.from({
|
|
1332
|
+
archive: proposed.archive,
|
|
1333
|
+
header: proposed.header,
|
|
1334
|
+
blocks,
|
|
1335
|
+
number: proposed.checkpointNumber,
|
|
1336
|
+
feeAssetPriceModifier: proposed.feeAssetPriceModifier
|
|
1337
|
+
});
|
|
1338
|
+
const promotedCheckpoint = PublishedCheckpoint.from({
|
|
1339
|
+
checkpoint,
|
|
1340
|
+
l1: calldataCheckpoint.l1,
|
|
1341
|
+
attestations: calldataCheckpoint.attestations
|
|
1342
|
+
});
|
|
1343
|
+
this.instrumentation.processCheckpointPromoted();
|
|
1344
|
+
return promotedCheckpoint;
|
|
1345
|
+
}
|
|
1100
1346
|
async checkForNewCheckpointsBeforeL1SyncPoint(status, blocksSynchedTo, currentL1BlockNumber) {
|
|
1101
|
-
const {
|
|
1102
|
-
// Compare the last checkpoint we have (either retrieved in this round or loaded from store)
|
|
1103
|
-
// rollup contract told us was the latest one (pinned at the currentL1BlockNumber).
|
|
1104
|
-
const latestLocalCheckpointNumber =
|
|
1347
|
+
const { lastSeenCheckpoint, pendingCheckpointNumber } = status;
|
|
1348
|
+
// Compare the last checkpoint (valid or not) we have (either retrieved in this round or loaded from store)
|
|
1349
|
+
// with what the rollup contract told us was the latest one (pinned at the currentL1BlockNumber).
|
|
1350
|
+
const latestLocalCheckpointNumber = lastSeenCheckpoint?.checkpointNumber ?? CheckpointNumber.max(await this.stores.blocks.getLatestCheckpointNumber(), await this.stores.blocks.getLatestRejectedCheckpointNumber()) ?? CheckpointNumber.ZERO;
|
|
1105
1351
|
if (latestLocalCheckpointNumber < pendingCheckpointNumber) {
|
|
1106
1352
|
// Here we have consumed all logs until the `currentL1Block` we pinned at the beginning of the archiver loop,
|
|
1107
1353
|
// but still haven't reached the pending checkpoint according to the call to the rollup contract.
|
|
1108
1354
|
// We suspect an L1 reorg that added checkpoints *behind* us. If that is the case, it must have happened between
|
|
1109
1355
|
// the last checkpoint we saw and the current one, so we reset the last synched L1 block number. In the edge case
|
|
1110
1356
|
// we don't have one, we go back 2 L1 epochs, which is the deepest possible reorg (assuming Casper is working).
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
if (lastRetrievedCheckpoint) {
|
|
1114
|
-
latestLocalCheckpointArchive = lastRetrievedCheckpoint.checkpoint.archive.root.toString();
|
|
1115
|
-
targetL1BlockNumber = lastRetrievedCheckpoint.l1.blockNumber;
|
|
1116
|
-
} else if (latestLocalCheckpointNumber > 0) {
|
|
1117
|
-
const checkpoint = await this.store.getRangeOfCheckpoints(latestLocalCheckpointNumber, 1).then(([c])=>c);
|
|
1118
|
-
latestLocalCheckpointArchive = checkpoint.archive.root.toString();
|
|
1119
|
-
targetL1BlockNumber = checkpoint.l1.blockNumber;
|
|
1120
|
-
}
|
|
1357
|
+
const latestLocalCheckpoint = lastSeenCheckpoint ?? await this.stores.blocks.getCheckpointData(latestLocalCheckpointNumber) ?? await this.stores.blocks.getRejectedCheckpointByNumber(latestLocalCheckpointNumber);
|
|
1358
|
+
const targetL1BlockNumber = latestLocalCheckpoint?.l1.blockNumber ?? maxBigint(currentL1BlockNumber - 64n, this.l1Constants.l1StartBlock, 0n);
|
|
1121
1359
|
this.log.warn(`Failed to reach checkpoint ${pendingCheckpointNumber} at ${currentL1BlockNumber} (latest is ${latestLocalCheckpointNumber}). ` + `Rolling back last synched L1 block number to ${targetL1BlockNumber}.`, {
|
|
1122
1360
|
latestLocalCheckpointNumber,
|
|
1123
|
-
|
|
1361
|
+
latestLocalCheckpointL1: latestLocalCheckpoint?.l1,
|
|
1124
1362
|
blocksSynchedTo,
|
|
1125
1363
|
currentL1BlockNumber,
|
|
1126
1364
|
...status
|
|
1127
1365
|
});
|
|
1128
|
-
await this.
|
|
1366
|
+
await this.stores.blocks.setSynchedL1BlockNumber(targetL1BlockNumber);
|
|
1129
1367
|
} else {
|
|
1130
1368
|
this.log.trace(`No new checkpoints behind L1 sync point to retrieve.`, {
|
|
1131
1369
|
latestLocalCheckpointNumber,
|
|
@@ -1134,7 +1372,7 @@ _dec = trackSpan('Archiver.syncFromL1'), _dec1 = trackSpan('Archiver.handleEpoch
|
|
|
1134
1372
|
}
|
|
1135
1373
|
}
|
|
1136
1374
|
async getCheckpointHeader(number) {
|
|
1137
|
-
const checkpoint = await this.
|
|
1375
|
+
const checkpoint = await this.stores.blocks.getCheckpointData(number);
|
|
1138
1376
|
if (!checkpoint) {
|
|
1139
1377
|
return undefined;
|
|
1140
1378
|
}
|