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