@aztec/archiver 0.0.1-commit.d3ec352c → 0.0.1-commit.d58ff9d0
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 +164 -22
- package/dest/archiver.d.ts +194 -0
- package/dest/archiver.d.ts.map +1 -0
- package/dest/archiver.js +949 -0
- package/dest/config.d.ts +34 -0
- package/dest/config.d.ts.map +1 -0
- package/dest/config.js +95 -0
- package/dest/errors.d.ts +87 -0
- package/dest/errors.d.ts.map +1 -0
- package/dest/errors.js +129 -0
- package/dest/factory.d.ts +16 -10
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +117 -20
- package/dest/index.d.ts +19 -4
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +17 -3
- package/dest/interfaces.d.ts +9 -0
- package/dest/interfaces.d.ts.map +1 -0
- package/dest/interfaces.js +3 -0
- package/dest/l1/bin/retrieve-calldata.d.ts +3 -0
- package/dest/l1/bin/retrieve-calldata.d.ts.map +1 -0
- package/dest/l1/bin/retrieve-calldata.js +152 -0
- package/dest/l1/calldata_retriever.d.ts +143 -0
- package/dest/l1/calldata_retriever.d.ts.map +1 -0
- package/dest/l1/calldata_retriever.js +413 -0
- package/dest/l1/data_retrieval.d.ts +102 -0
- package/dest/l1/data_retrieval.d.ts.map +1 -0
- package/dest/{archiver → l1}/data_retrieval.js +85 -165
- package/dest/l1/debug_tx.d.ts +19 -0
- package/dest/l1/debug_tx.d.ts.map +1 -0
- package/dest/l1/debug_tx.js +73 -0
- package/dest/l1/spire_proposer.d.ts +70 -0
- package/dest/l1/spire_proposer.d.ts.map +1 -0
- package/dest/l1/spire_proposer.js +149 -0
- package/dest/l1/trace_tx.d.ts +43 -0
- package/dest/l1/trace_tx.d.ts.map +1 -0
- package/dest/l1/trace_tx.js +91 -0
- package/dest/l1/types.d.ts +12 -0
- package/dest/l1/types.d.ts.map +1 -0
- package/dest/l1/types.js +3 -0
- 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/l1/validate_trace.d.ts +32 -0
- package/dest/l1/validate_trace.d.ts.map +1 -0
- package/dest/l1/validate_trace.js +154 -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 +112 -0
- package/dest/modules/data_source_base.d.ts.map +1 -0
- package/dest/modules/data_source_base.js +322 -0
- package/dest/modules/data_store_updater.d.ts +105 -0
- package/dest/modules/data_store_updater.d.ts.map +1 -0
- package/dest/modules/data_store_updater.js +393 -0
- package/dest/modules/instrumentation.d.ts +63 -0
- package/dest/modules/instrumentation.d.ts.map +1 -0
- package/dest/modules/instrumentation.js +166 -0
- package/dest/modules/l1_synchronizer.d.ts +77 -0
- package/dest/modules/l1_synchronizer.d.ts.map +1 -0
- package/dest/modules/l1_synchronizer.js +1381 -0
- 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 +40 -0
- package/dest/modules/validation.d.ts.map +1 -0
- package/dest/{archiver → modules}/validation.js +41 -17
- package/dest/store/block_store.d.ts +303 -0
- package/dest/store/block_store.d.ts.map +1 -0
- package/dest/store/block_store.js +1206 -0
- package/dest/store/contract_class_store.d.ts +31 -0
- package/dest/store/contract_class_store.d.ts.map +1 -0
- package/dest/store/contract_class_store.js +98 -0
- package/dest/store/contract_instance_store.d.ts +51 -0
- package/dest/store/contract_instance_store.d.ts.map +1 -0
- package/dest/{archiver/kv_archiver_store → store}/contract_instance_store.js +55 -3
- 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 +25 -0
- package/dest/store/l2_tips_cache.d.ts.map +1 -0
- package/dest/store/l2_tips_cache.js +26 -0
- package/dest/store/log_store.d.ts +59 -0
- package/dest/store/log_store.d.ts.map +1 -0
- package/dest/store/log_store.js +310 -0
- 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 +50 -0
- package/dest/store/message_store.d.ts.map +1 -0
- package/dest/{archiver/kv_archiver_store → store}/message_store.js +66 -23
- package/dest/{archiver/structs → structs}/data_retrieval.d.ts +1 -1
- package/dest/structs/data_retrieval.d.ts.map +1 -0
- package/dest/structs/inbox_message.d.ts +15 -0
- package/dest/structs/inbox_message.d.ts.map +1 -0
- package/dest/{archiver/structs → structs}/inbox_message.js +6 -6
- package/dest/structs/published.d.ts +2 -0
- package/dest/structs/published.d.ts.map +1 -0
- package/dest/test/fake_l1_state.d.ts +214 -0
- package/dest/test/fake_l1_state.d.ts.map +1 -0
- package/dest/test/fake_l1_state.js +517 -0
- package/dest/test/index.d.ts +2 -1
- package/dest/test/index.d.ts.map +1 -1
- package/dest/test/index.js +4 -1
- package/dest/test/mock_archiver.d.ts +5 -6
- package/dest/test/mock_archiver.d.ts.map +1 -1
- package/dest/test/mock_archiver.js +8 -12
- package/dest/test/mock_l1_to_l2_message_source.d.ts +6 -7
- package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
- package/dest/test/mock_l1_to_l2_message_source.js +19 -14
- package/dest/test/mock_l2_block_source.d.ts +68 -40
- package/dest/test/mock_l2_block_source.d.ts.map +1 -1
- package/dest/test/mock_l2_block_source.js +347 -145
- package/dest/test/mock_structs.d.ts +83 -4
- package/dest/test/mock_structs.d.ts.map +1 -1
- package/dest/test/mock_structs.js +157 -12
- package/dest/test/noop_l1_archiver.d.ts +34 -0
- package/dest/test/noop_l1_archiver.d.ts.map +1 -0
- package/dest/test/noop_l1_archiver.js +88 -0
- package/package.json +18 -18
- package/src/archiver.ts +785 -0
- package/src/config.ts +125 -0
- package/src/errors.ts +203 -0
- package/src/factory.ts +185 -22
- package/src/index.ts +27 -3
- package/src/interfaces.ts +9 -0
- package/src/l1/README.md +55 -0
- package/src/l1/bin/retrieve-calldata.ts +194 -0
- package/src/l1/calldata_retriever.ts +529 -0
- package/src/{archiver → l1}/data_retrieval.ts +157 -243
- package/src/l1/debug_tx.ts +99 -0
- package/src/l1/spire_proposer.ts +152 -0
- package/src/l1/trace_tx.ts +128 -0
- package/src/l1/types.ts +13 -0
- package/src/l1/validate_historical_logs.ts +140 -0
- package/src/l1/validate_trace.ts +229 -0
- package/src/modules/contract_data_source_adapter.ts +46 -0
- package/src/modules/data_source_base.ts +466 -0
- package/src/modules/data_store_updater.ts +519 -0
- package/src/modules/instrumentation.ts +217 -0
- package/src/modules/l1_synchronizer.ts +1320 -0
- package/src/modules/outbox_trees_resolver.ts +199 -0
- package/src/modules/validation.ts +186 -0
- package/src/store/block_store.ts +1551 -0
- package/src/store/contract_class_store.ts +126 -0
- package/src/{archiver/kv_archiver_store → store}/contract_instance_store.ts +70 -7
- package/src/store/data_stores.ts +104 -0
- package/src/store/function_names_cache.ts +37 -0
- package/src/store/l2_tips_cache.ts +35 -0
- package/src/store/log_store.ts +380 -0
- package/src/store/log_store_codec.ts +143 -0
- package/src/{archiver/kv_archiver_store → store}/message_store.ts +81 -28
- package/src/{archiver/structs → structs}/inbox_message.ts +8 -9
- package/src/{archiver/structs → structs}/published.ts +0 -1
- package/src/test/fake_l1_state.ts +770 -0
- package/src/test/fixtures/debug_traceTransaction-multicall3.json +88 -0
- package/src/test/fixtures/debug_traceTransaction-multiplePropose.json +153 -0
- package/src/test/fixtures/debug_traceTransaction-proxied.json +122 -0
- package/src/test/fixtures/trace_transaction-multicall3.json +65 -0
- package/src/test/fixtures/trace_transaction-multiplePropose.json +319 -0
- package/src/test/fixtures/trace_transaction-proxied.json +128 -0
- package/src/test/fixtures/trace_transaction-randomRevert.json +216 -0
- package/src/test/index.ts +4 -0
- package/src/test/mock_archiver.ts +10 -14
- package/src/test/mock_l1_to_l2_message_source.ts +16 -15
- package/src/test/mock_l2_block_source.ts +416 -161
- package/src/test/mock_structs.ts +292 -14
- package/src/test/noop_l1_archiver.ts +158 -0
- package/dest/archiver/archiver.d.ts +0 -290
- package/dest/archiver/archiver.d.ts.map +0 -1
- package/dest/archiver/archiver.js +0 -1434
- package/dest/archiver/archiver_store.d.ts +0 -256
- package/dest/archiver/archiver_store.d.ts.map +0 -1
- package/dest/archiver/archiver_store.js +0 -4
- package/dest/archiver/archiver_store_test_suite.d.ts +0 -8
- package/dest/archiver/archiver_store_test_suite.d.ts.map +0 -1
- package/dest/archiver/archiver_store_test_suite.js +0 -1289
- package/dest/archiver/config.d.ts +0 -21
- package/dest/archiver/config.d.ts.map +0 -1
- package/dest/archiver/config.js +0 -55
- package/dest/archiver/data_retrieval.d.ts +0 -80
- package/dest/archiver/data_retrieval.d.ts.map +0 -1
- package/dest/archiver/errors.d.ts +0 -12
- package/dest/archiver/errors.d.ts.map +0 -1
- package/dest/archiver/errors.js +0 -17
- package/dest/archiver/index.d.ts +0 -7
- package/dest/archiver/index.d.ts.map +0 -1
- package/dest/archiver/index.js +0 -4
- package/dest/archiver/instrumentation.d.ts +0 -35
- package/dest/archiver/instrumentation.d.ts.map +0 -1
- package/dest/archiver/instrumentation.js +0 -140
- package/dest/archiver/kv_archiver_store/block_store.d.ts +0 -125
- package/dest/archiver/kv_archiver_store/block_store.d.ts.map +0 -1
- package/dest/archiver/kv_archiver_store/block_store.js +0 -371
- package/dest/archiver/kv_archiver_store/contract_class_store.d.ts +0 -18
- package/dest/archiver/kv_archiver_store/contract_class_store.d.ts.map +0 -1
- package/dest/archiver/kv_archiver_store/contract_class_store.js +0 -120
- package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts +0 -24
- package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +0 -1
- package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +0 -169
- package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +0 -1
- package/dest/archiver/kv_archiver_store/kv_archiver_store.js +0 -296
- package/dest/archiver/kv_archiver_store/log_store.d.ts +0 -49
- package/dest/archiver/kv_archiver_store/log_store.d.ts.map +0 -1
- package/dest/archiver/kv_archiver_store/log_store.js +0 -337
- package/dest/archiver/kv_archiver_store/message_store.d.ts +0 -39
- package/dest/archiver/kv_archiver_store/message_store.d.ts.map +0 -1
- package/dest/archiver/structs/data_retrieval.d.ts.map +0 -1
- package/dest/archiver/structs/inbox_message.d.ts +0 -15
- package/dest/archiver/structs/inbox_message.d.ts.map +0 -1
- package/dest/archiver/structs/published.d.ts +0 -3
- package/dest/archiver/structs/published.d.ts.map +0 -1
- package/dest/archiver/validation.d.ts +0 -17
- package/dest/archiver/validation.d.ts.map +0 -1
- package/dest/rpc/index.d.ts +0 -9
- package/dest/rpc/index.d.ts.map +0 -1
- package/dest/rpc/index.js +0 -15
- package/src/archiver/archiver.ts +0 -1880
- package/src/archiver/archiver_store.ts +0 -310
- package/src/archiver/archiver_store_test_suite.ts +0 -1295
- package/src/archiver/config.ts +0 -79
- package/src/archiver/errors.ts +0 -26
- package/src/archiver/index.ts +0 -6
- package/src/archiver/instrumentation.ts +0 -187
- package/src/archiver/kv_archiver_store/block_store.ts +0 -482
- package/src/archiver/kv_archiver_store/contract_class_store.ts +0 -176
- package/src/archiver/kv_archiver_store/kv_archiver_store.ts +0 -423
- package/src/archiver/kv_archiver_store/log_store.ts +0 -407
- package/src/archiver/validation.ts +0 -124
- package/src/rpc/index.ts +0 -16
- /package/dest/{archiver/structs → structs}/data_retrieval.js +0 -0
- /package/dest/{archiver/structs → structs}/published.js +0 -0
- /package/src/{archiver/structs → structs}/data_retrieval.ts +0 -0
package/src/archiver.ts
ADDED
|
@@ -0,0 +1,785 @@
|
|
|
1
|
+
import type { BlobClientInterface } from '@aztec/blob-client/client';
|
|
2
|
+
import { EpochCache } from '@aztec/epoch-cache';
|
|
3
|
+
import { BlockTagTooOldError, OutboxContract, RollupContract } from '@aztec/ethereum/contracts';
|
|
4
|
+
import type { L1ContractAddresses } from '@aztec/ethereum/l1-contract-addresses';
|
|
5
|
+
import type { ViemPublicClient, ViemPublicDebugClient } from '@aztec/ethereum/types';
|
|
6
|
+
import { BlockNumber, CheckpointNumber, EpochNumber, SlotNumber } from '@aztec/foundation/branded-types';
|
|
7
|
+
import { Buffer32 } from '@aztec/foundation/buffer';
|
|
8
|
+
import { merge } from '@aztec/foundation/collection';
|
|
9
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
10
|
+
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
11
|
+
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
12
|
+
import { type PromiseWithResolvers, promiseWithResolvers } from '@aztec/foundation/promise';
|
|
13
|
+
import { RunningPromise, makeLoggingErrorHandler } from '@aztec/foundation/running-promise';
|
|
14
|
+
import { DateProvider, elapsed } from '@aztec/foundation/timer';
|
|
15
|
+
import {
|
|
16
|
+
type ArchiverEmitter,
|
|
17
|
+
type BlockHash,
|
|
18
|
+
L2Block,
|
|
19
|
+
type L2BlockSink,
|
|
20
|
+
L2BlockSourceEvents,
|
|
21
|
+
type L2Tips,
|
|
22
|
+
type ValidateCheckpointResult,
|
|
23
|
+
l2TipsEqual,
|
|
24
|
+
} from '@aztec/stdlib/block';
|
|
25
|
+
import { type ProposedCheckpointInput, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
|
|
26
|
+
import {
|
|
27
|
+
type L1RollupConstants,
|
|
28
|
+
getEpochAtSlot,
|
|
29
|
+
getSlotAtNextL1Block,
|
|
30
|
+
getSlotRangeForEpoch,
|
|
31
|
+
getTimestampForSlot,
|
|
32
|
+
getTimestampRangeForEpoch,
|
|
33
|
+
} from '@aztec/stdlib/epoch-helpers';
|
|
34
|
+
import type { L2ToL1MembershipWitness } from '@aztec/stdlib/messaging';
|
|
35
|
+
import { ConsensusTimetable } from '@aztec/stdlib/timetable';
|
|
36
|
+
import type { BlockHeader, TxHash } from '@aztec/stdlib/tx';
|
|
37
|
+
import { type TelemetryClient, type Traceable, type Tracer, trackSpan } from '@aztec/telemetry-client';
|
|
38
|
+
|
|
39
|
+
import { type ArchiverConfig, mapArchiverConfig } from './config.js';
|
|
40
|
+
import { BlockAlreadyCheckpointedError, BlockOrCheckpointSlotExpiredError, NoBlobBodiesFoundError } from './errors.js';
|
|
41
|
+
import { validateAndLogHistoricalLogsAvailability } from './l1/validate_historical_logs.js';
|
|
42
|
+
import { validateAndLogTraceAvailability } from './l1/validate_trace.js';
|
|
43
|
+
import { ArchiverDataSourceBase } from './modules/data_source_base.js';
|
|
44
|
+
import { ArchiverDataStoreUpdater } from './modules/data_store_updater.js';
|
|
45
|
+
import type { ArchiverInstrumentation } from './modules/instrumentation.js';
|
|
46
|
+
import type { ArchiverL1Synchronizer } from './modules/l1_synchronizer.js';
|
|
47
|
+
import { OutboxTreesResolver } from './modules/outbox_trees_resolver.js';
|
|
48
|
+
import { type ArchiverDataStores, backupArchiverDataStores, getArchiverSynchPoint } from './store/data_stores.js';
|
|
49
|
+
import { L2TipsCache } from './store/l2_tips_cache.js';
|
|
50
|
+
|
|
51
|
+
/** Export ArchiverEmitter for use in factory and tests. */
|
|
52
|
+
export type { ArchiverEmitter };
|
|
53
|
+
|
|
54
|
+
/** Request to add a block to the archiver, queued for processing by the sync loop. */
|
|
55
|
+
type AddBlockRequest = {
|
|
56
|
+
type: 'block';
|
|
57
|
+
block: L2Block;
|
|
58
|
+
resolve: () => void;
|
|
59
|
+
reject: (err: Error) => void;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
/** Request to add a proposed checkpoint to the archiver, queued for processing by the sync loop. */
|
|
63
|
+
type AddProposedCheckpointRequest = {
|
|
64
|
+
type: 'checkpoint';
|
|
65
|
+
checkpoint: ProposedCheckpointInput;
|
|
66
|
+
resolve: () => void;
|
|
67
|
+
reject: (err: Error) => void;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
export type ArchiverDeps = {
|
|
71
|
+
telemetry?: TelemetryClient;
|
|
72
|
+
blobClient: BlobClientInterface;
|
|
73
|
+
epochCache?: EpochCache;
|
|
74
|
+
dateProvider?: DateProvider;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/** Minimal dependency for observing whether a checkpoint proposal was received for a slot. */
|
|
78
|
+
export type CheckpointProposalPresence = {
|
|
79
|
+
/** Returns true when a checkpoint proposal for `slot` has been retained locally. */
|
|
80
|
+
hasCheckpointProposalForSlot(slot: SlotNumber): Promise<boolean>;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const noCheckpointProposalPresence: CheckpointProposalPresence = {
|
|
84
|
+
hasCheckpointProposalForSlot: () => Promise.resolve(false),
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Pulls checkpoints in a non-blocking manner and provides interface for their retrieval.
|
|
89
|
+
* Responsible for handling robust L1 polling so that other components do not need to
|
|
90
|
+
* concern themselves with it.
|
|
91
|
+
*/
|
|
92
|
+
export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Traceable {
|
|
93
|
+
/** Event emitter for archiver events (L2BlockProven, L2PruneUnproven, L2PruneUncheckpointed, etc). */
|
|
94
|
+
public readonly events: ArchiverEmitter;
|
|
95
|
+
|
|
96
|
+
/** A loop in which we will be continually fetching new checkpoints. */
|
|
97
|
+
protected runningPromise: RunningPromise;
|
|
98
|
+
|
|
99
|
+
/** L1 synchronizer that handles fetching checkpoints and messages from L1. */
|
|
100
|
+
private readonly synchronizer: ArchiverL1Synchronizer;
|
|
101
|
+
|
|
102
|
+
/** Resolver for L2-to-L1 message membership witnesses, built over this archiver's read view. */
|
|
103
|
+
private readonly outboxTreesResolver: OutboxTreesResolver;
|
|
104
|
+
|
|
105
|
+
private initialSyncComplete: boolean = false;
|
|
106
|
+
private initialSyncPromise: PromiseWithResolvers<void>;
|
|
107
|
+
|
|
108
|
+
/** Queue of blocks and checkpoints to be added to the store, processed by the sync loop. */
|
|
109
|
+
private inboundQueue: (AddBlockRequest | AddProposedCheckpointRequest)[] = [];
|
|
110
|
+
|
|
111
|
+
/** Helper to handle updates to the store */
|
|
112
|
+
private readonly updater: ArchiverDataStoreUpdater;
|
|
113
|
+
|
|
114
|
+
/** In-memory cache for L2 chain tips. */
|
|
115
|
+
private readonly l2TipsCache: L2TipsCache;
|
|
116
|
+
|
|
117
|
+
/** Consensus timing model used for proposed-checkpoint arrival expectations. */
|
|
118
|
+
private readonly timetable: ConsensusTimetable;
|
|
119
|
+
|
|
120
|
+
public readonly tracer: Tracer;
|
|
121
|
+
|
|
122
|
+
private readonly instrumentation: ArchiverInstrumentation;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Creates a new instance of the Archiver.
|
|
126
|
+
* @param publicClient - A client for interacting with the Ethereum node.
|
|
127
|
+
* @param debugClient - A client for interacting with the Ethereum node for debug/trace methods.
|
|
128
|
+
* @param rollup - Rollup contract instance.
|
|
129
|
+
* @param outbox - Outbox contract instance, used to read per-epoch L2-to-L1 message roots.
|
|
130
|
+
* @param inbox - Inbox contract instance.
|
|
131
|
+
* @param l1Addresses - L1 contract addresses (registry, governance proposer, slashing proposer).
|
|
132
|
+
* @param dataStores - Archiver substores for storage & retrieval of blocks, encrypted logs & contract data.
|
|
133
|
+
* @param config - Archiver configuration options.
|
|
134
|
+
* @param blobClient - Client for retrieving blob data.
|
|
135
|
+
* @param instrumentation - Instrumentation for metrics and tracing.
|
|
136
|
+
* @param l1Constants - L1 rollup constants.
|
|
137
|
+
* @param synchronizer - L1 synchronizer that handles fetching checkpoints and messages from L1.
|
|
138
|
+
* @param events - Event emitter shared with the synchronizer.
|
|
139
|
+
* @param initialHeader - Genesis block header.
|
|
140
|
+
* @param initialBlockHash - Precomputed hash of the genesis block header.
|
|
141
|
+
* @param l2TipsCache - In-memory cache for L2 chain tips.
|
|
142
|
+
* @param dateProvider - Provider for current date/time, used for wall-clock orphan-block pruning.
|
|
143
|
+
* @param log - A logger.
|
|
144
|
+
*/
|
|
145
|
+
constructor(
|
|
146
|
+
private readonly publicClient: ViemPublicClient,
|
|
147
|
+
private readonly debugClient: ViemPublicDebugClient,
|
|
148
|
+
private readonly rollup: RollupContract,
|
|
149
|
+
private readonly outbox: OutboxContract,
|
|
150
|
+
private readonly l1Addresses: Pick<
|
|
151
|
+
L1ContractAddresses,
|
|
152
|
+
'rollupAddress' | 'registryAddress' | 'inboxAddress' | 'governanceProposerAddress'
|
|
153
|
+
> & {
|
|
154
|
+
slashingProposerAddress: EthAddress;
|
|
155
|
+
},
|
|
156
|
+
readonly dataStores: ArchiverDataStores,
|
|
157
|
+
private config: {
|
|
158
|
+
pollingIntervalMs: number;
|
|
159
|
+
batchSize: number;
|
|
160
|
+
skipValidateCheckpointAttestations?: boolean;
|
|
161
|
+
maxAllowedEthClientDriftSeconds: number;
|
|
162
|
+
ethereumAllowNoDebugHosts?: boolean;
|
|
163
|
+
skipHistoricalLogsCheck?: boolean;
|
|
164
|
+
checkpointProposalSyncGrace: number;
|
|
165
|
+
orphanPruneNoProposalTolerance: number;
|
|
166
|
+
skipOrphanProposedBlockPruning: boolean;
|
|
167
|
+
blockDuration: number;
|
|
168
|
+
},
|
|
169
|
+
private readonly blobClient: BlobClientInterface,
|
|
170
|
+
instrumentation: ArchiverInstrumentation,
|
|
171
|
+
protected override readonly l1Constants: L1RollupConstants & {
|
|
172
|
+
l1StartBlockHash: Buffer32;
|
|
173
|
+
genesisArchiveRoot: Fr;
|
|
174
|
+
},
|
|
175
|
+
synchronizer: ArchiverL1Synchronizer,
|
|
176
|
+
events: ArchiverEmitter,
|
|
177
|
+
initialHeader: BlockHeader,
|
|
178
|
+
initialBlockHash: BlockHash,
|
|
179
|
+
l2TipsCache: L2TipsCache,
|
|
180
|
+
private readonly dateProvider: DateProvider,
|
|
181
|
+
private checkpointProposalPresence: CheckpointProposalPresence = noCheckpointProposalPresence,
|
|
182
|
+
private readonly log: Logger = createLogger('archiver'),
|
|
183
|
+
) {
|
|
184
|
+
super(dataStores, l1Constants, initialHeader, initialBlockHash, l1Constants.genesisArchiveRoot);
|
|
185
|
+
|
|
186
|
+
this.tracer = instrumentation.tracer;
|
|
187
|
+
this.instrumentation = instrumentation;
|
|
188
|
+
this.initialSyncPromise = promiseWithResolvers();
|
|
189
|
+
this.synchronizer = synchronizer;
|
|
190
|
+
this.events = events;
|
|
191
|
+
this.l2TipsCache = l2TipsCache;
|
|
192
|
+
this.timetable = new ConsensusTimetable({
|
|
193
|
+
l1Constants,
|
|
194
|
+
blockDuration: this.config.blockDuration,
|
|
195
|
+
checkpointProposalSyncGrace: this.config.checkpointProposalSyncGrace,
|
|
196
|
+
});
|
|
197
|
+
this.updater = new ArchiverDataStoreUpdater(this.dataStores, this.l2TipsCache, {
|
|
198
|
+
rollupManaLimit: l1Constants.rollupManaLimit,
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
// The resolver reads its witness data from this archiver and pins its lazy Outbox-root fetches
|
|
202
|
+
// to the archiver's synced L1 block. No method on `this` is invoked during construction.
|
|
203
|
+
this.outboxTreesResolver = new OutboxTreesResolver(
|
|
204
|
+
this.outbox,
|
|
205
|
+
this,
|
|
206
|
+
() => Promise.resolve(this.getL1BlockNumber()),
|
|
207
|
+
l1Constants.epochDuration,
|
|
208
|
+
);
|
|
209
|
+
|
|
210
|
+
// Running promise starts with a small interval inbetween runs, so all iterations needed for the initial sync
|
|
211
|
+
// are done as fast as possible. This then gets updated once the initial sync completes.
|
|
212
|
+
this.runningPromise = new RunningPromise(
|
|
213
|
+
() => this.sync(),
|
|
214
|
+
this.log,
|
|
215
|
+
this.config.pollingIntervalMs / 10,
|
|
216
|
+
makeLoggingErrorHandler(this.log, NoBlobBodiesFoundError, BlockTagTooOldError),
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Returns the L2-to-L1 membership witness for `message` emitted by tx `txHash`. The node selects
|
|
222
|
+
* the smallest partial-proof root on the Outbox that covers the tx's checkpoint and builds the
|
|
223
|
+
* witness against it.
|
|
224
|
+
*
|
|
225
|
+
* The node reads the Outbox roots lazily, pinned to its synced L1 block, so the witness reflects
|
|
226
|
+
* the node's synced view. Returns `undefined` if the tx isn't yet in a block/epoch or no covering
|
|
227
|
+
* root has landed on L1 as of the synced block.
|
|
228
|
+
*
|
|
229
|
+
* Caveat: roots are cached only for the current synced L1 block and re-fetched once the node
|
|
230
|
+
* advances, so a reorg is picked up on the next synced-block advance rather than re-validated per
|
|
231
|
+
* request.
|
|
232
|
+
*/
|
|
233
|
+
public getL2ToL1MembershipWitness(
|
|
234
|
+
txHash: TxHash,
|
|
235
|
+
message: Fr,
|
|
236
|
+
messageIndexInTx?: number,
|
|
237
|
+
): Promise<L2ToL1MembershipWitness | undefined> {
|
|
238
|
+
return this.outboxTreesResolver.getL2ToL1MembershipWitness(txHash, message, messageIndexInTx);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** Updates archiver config */
|
|
242
|
+
public updateConfig(newConfig: Partial<ArchiverConfig>) {
|
|
243
|
+
this.config = merge(this.config, mapArchiverConfig(newConfig));
|
|
244
|
+
this.synchronizer.setConfig(this.config);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Starts sync process.
|
|
249
|
+
* @param blockUntilSynced - If true, blocks until the archiver has fully synced.
|
|
250
|
+
*/
|
|
251
|
+
public async start(blockUntilSynced: boolean): Promise<void> {
|
|
252
|
+
if (this.runningPromise.isRunning()) {
|
|
253
|
+
throw new Error('Archiver is already running');
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
await this.blobClient.testSources();
|
|
257
|
+
await this.synchronizer.testEthereumNodeSynced();
|
|
258
|
+
await validateAndLogTraceAvailability(
|
|
259
|
+
this.debugClient,
|
|
260
|
+
this.config.ethereumAllowNoDebugHosts ?? false,
|
|
261
|
+
this.log.getBindings(),
|
|
262
|
+
);
|
|
263
|
+
await validateAndLogHistoricalLogsAvailability(
|
|
264
|
+
this.publicClient,
|
|
265
|
+
{
|
|
266
|
+
rollupAddress: this.l1Addresses.rollupAddress,
|
|
267
|
+
inboxAddress: this.l1Addresses.inboxAddress,
|
|
268
|
+
registryAddress: this.l1Addresses.registryAddress,
|
|
269
|
+
governanceProposerAddress: this.l1Addresses.governanceProposerAddress,
|
|
270
|
+
},
|
|
271
|
+
this.config.skipHistoricalLogsCheck ?? false,
|
|
272
|
+
this.log.getBindings(),
|
|
273
|
+
);
|
|
274
|
+
|
|
275
|
+
// Log initial state for the archiver
|
|
276
|
+
const { l1StartBlock } = this.l1Constants;
|
|
277
|
+
const { blocksSynchedTo = l1StartBlock, messagesSynchedTo = l1StartBlock } = await getArchiverSynchPoint(
|
|
278
|
+
this.stores,
|
|
279
|
+
);
|
|
280
|
+
const currentL2Checkpoint = await this.getCheckpointNumber();
|
|
281
|
+
this.log.info(
|
|
282
|
+
`Starting archiver sync to rollup contract ${this.rollup.address} from L1 block ${blocksSynchedTo} and L2 checkpoint ${currentL2Checkpoint}`,
|
|
283
|
+
{ blocksSynchedTo, messagesSynchedTo, currentL2Checkpoint },
|
|
284
|
+
);
|
|
285
|
+
|
|
286
|
+
// Start sync loop, and return the wait for initial sync if we are asked to block until synced
|
|
287
|
+
this.runningPromise.start();
|
|
288
|
+
if (blockUntilSynced) {
|
|
289
|
+
return this.waitForInitialSync();
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
public syncImmediate() {
|
|
294
|
+
return this.runningPromise.trigger();
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/** Sets the proposal-presence provider used by orphan proposed-block pruning. */
|
|
298
|
+
public setCheckpointProposalPresence(checkpointProposalPresence: CheckpointProposalPresence): void {
|
|
299
|
+
this.checkpointProposalPresence = checkpointProposalPresence;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
public trySyncImmediate() {
|
|
303
|
+
try {
|
|
304
|
+
return this.syncImmediate();
|
|
305
|
+
} catch (err) {
|
|
306
|
+
this.log.error(`Failed to trigger immediate archiver sync: ${err}`, err);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Queues a block to be added to the archiver store and triggers processing.
|
|
312
|
+
* The block will be processed by the sync loop.
|
|
313
|
+
* Implements the L2BlockSink interface.
|
|
314
|
+
* @param block - The L2 block to add.
|
|
315
|
+
* @returns A promise that resolves when the block has been added to the store, or rejects on error.
|
|
316
|
+
*/
|
|
317
|
+
public addBlock(block: L2Block): Promise<void> {
|
|
318
|
+
const promise = promiseWithResolvers<void>();
|
|
319
|
+
this.inboundQueue.push({ block, ...promise, type: 'block' });
|
|
320
|
+
this.log.debug(`Queued block ${block.number} for processing`);
|
|
321
|
+
void this.trySyncImmediate();
|
|
322
|
+
return promise.promise;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Queues a new proposed checkpoint into the archiver store.
|
|
327
|
+
* Checks that the checkpoint is not for an L2 slot already synced from L1.
|
|
328
|
+
* Resolves once the checkpoint has been processed.
|
|
329
|
+
*/
|
|
330
|
+
public addProposedCheckpoint(pending: ProposedCheckpointInput): Promise<void> {
|
|
331
|
+
const promise = promiseWithResolvers<void>();
|
|
332
|
+
this.inboundQueue.push({ checkpoint: pending, ...promise, type: 'checkpoint' });
|
|
333
|
+
this.log.debug(`Queued checkpoint ${pending.checkpointNumber} for processing`);
|
|
334
|
+
void this.trySyncImmediate();
|
|
335
|
+
return promise.promise;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Processes all queued blocks and checkpoints, adding them to the store.
|
|
340
|
+
* Called at the beginning of each sync iteration.
|
|
341
|
+
* Items are processed in the order they were queued.
|
|
342
|
+
*/
|
|
343
|
+
private async processInboundQueue(): Promise<L2Block[]> {
|
|
344
|
+
const blocksAdded: L2Block[] = [];
|
|
345
|
+
if (this.inboundQueue.length === 0) {
|
|
346
|
+
return blocksAdded;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// Take all items from the queue
|
|
350
|
+
const queuedItems = this.inboundQueue.splice(0, this.inboundQueue.length);
|
|
351
|
+
this.log.debug(`Processing ${queuedItems.length} queued inbound items`);
|
|
352
|
+
|
|
353
|
+
// Calculate slot threshold for validation
|
|
354
|
+
const l1Timestamp = this.synchronizer.getL1Timestamp();
|
|
355
|
+
const slotAtNextL1Block =
|
|
356
|
+
l1Timestamp === undefined ? undefined : getSlotAtNextL1Block(l1Timestamp, this.l1Constants);
|
|
357
|
+
|
|
358
|
+
// Helpers for manipulating blocks and checkpoints in the queue
|
|
359
|
+
const getSlot: (item: AddBlockRequest | AddProposedCheckpointRequest) => SlotNumber = item =>
|
|
360
|
+
item.type === 'block' ? item.block.header.globalVariables.slotNumber : item.checkpoint.header.slotNumber;
|
|
361
|
+
const getNumber: (item: AddBlockRequest | AddProposedCheckpointRequest) => number = item =>
|
|
362
|
+
item.type === 'block' ? item.block.number : item.checkpoint.checkpointNumber;
|
|
363
|
+
|
|
364
|
+
// Process each item individually to properly resolve/reject each promise
|
|
365
|
+
for (const item of queuedItems) {
|
|
366
|
+
const { resolve, reject, type } = item;
|
|
367
|
+
const itemSlot = getSlot(item);
|
|
368
|
+
const itemNumber = getNumber(item);
|
|
369
|
+
if (slotAtNextL1Block !== undefined && itemSlot < slotAtNextL1Block) {
|
|
370
|
+
const nextSlotTimestamp = getTimestampForSlot(slotAtNextL1Block, this.l1Constants);
|
|
371
|
+
this.log.warn(
|
|
372
|
+
`Rejecting proposed ${type} ${itemNumber} for past slot ${itemSlot} (current ${slotAtNextL1Block})`,
|
|
373
|
+
{ number: itemNumber, type, l1Timestamp, slotAtNextL1Block, nextSlotTimestamp },
|
|
374
|
+
);
|
|
375
|
+
reject(new BlockOrCheckpointSlotExpiredError(itemSlot, nextSlotTimestamp, l1Timestamp));
|
|
376
|
+
continue;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
try {
|
|
380
|
+
if (type === 'block') {
|
|
381
|
+
const [durationMs] = await elapsed(() => this.updater.addProposedBlock(item.block));
|
|
382
|
+
this.instrumentation.processNewProposedBlock(durationMs, item.block);
|
|
383
|
+
blocksAdded.push(item.block);
|
|
384
|
+
} else {
|
|
385
|
+
await this.updater.addProposedCheckpoint(item.checkpoint);
|
|
386
|
+
}
|
|
387
|
+
this.log.debug(`Added ${type} ${itemNumber} to store`);
|
|
388
|
+
resolve();
|
|
389
|
+
} catch (err: any) {
|
|
390
|
+
if (err instanceof BlockAlreadyCheckpointedError) {
|
|
391
|
+
this.log.debug(`Proposed block ${itemNumber} matches already checkpointed block, ignoring late proposal`);
|
|
392
|
+
// A late proposal that matches an already-checkpointed block adds nothing new, so it is not appended.
|
|
393
|
+
resolve();
|
|
394
|
+
continue;
|
|
395
|
+
}
|
|
396
|
+
this.log.error(`Failed to add ${type} ${itemNumber} to store: ${err.message}`, err, {
|
|
397
|
+
number: itemNumber,
|
|
398
|
+
type,
|
|
399
|
+
});
|
|
400
|
+
reject(err);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
return blocksAdded;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
public waitForInitialSync() {
|
|
407
|
+
return this.initialSyncPromise.promise;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* Fetches logs from L1 contracts and processes them.
|
|
412
|
+
*/
|
|
413
|
+
@trackSpan('Archiver.sync')
|
|
414
|
+
private async sync() {
|
|
415
|
+
// Capture the tips before any mutation so the aggregate event can report what moved this pass.
|
|
416
|
+
const fromTips = await this.getL2Tips();
|
|
417
|
+
|
|
418
|
+
// Accumulate the blocks added across the pass's sub-steps (queued proposals + L1 checkpoint payloads).
|
|
419
|
+
const blocksAdded: L2Block[] = [];
|
|
420
|
+
// Process any queued blocks first, before doing L1 sync
|
|
421
|
+
blocksAdded.push(...(await this.processInboundQueue()));
|
|
422
|
+
// Now perform L1 sync
|
|
423
|
+
blocksAdded.push(...(await this.syncFromL1()));
|
|
424
|
+
// Prune proposed blocks with no corresponding proposed checkpoint after the appropriate materialization deadline.
|
|
425
|
+
await this.pruneOrphanProposedBlocks();
|
|
426
|
+
|
|
427
|
+
// Emit a single aggregate update event after all pass work has committed and the tips cache has refreshed,
|
|
428
|
+
// so `toTips` and the source state observed by listeners are consistent with the event payload. The pass
|
|
429
|
+
// mutated state if the tips moved (added blocks, a prune, or a thin tier movement) or blocks were added; a
|
|
430
|
+
// fully-synced no-op pass emits nothing.
|
|
431
|
+
const toTips = await this.getL2Tips();
|
|
432
|
+
if (!l2TipsEqual(fromTips, toTips) || blocksAdded.length > 0) {
|
|
433
|
+
this.events.emit(L2BlockSourceEvents.L2BlockSourceUpdated, {
|
|
434
|
+
type: L2BlockSourceEvents.L2BlockSourceUpdated,
|
|
435
|
+
fromTips,
|
|
436
|
+
toTips,
|
|
437
|
+
blocksAdded,
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* Prunes a block-only local tip that was built atop a checkpoint that was never itself proposed.
|
|
444
|
+
*
|
|
445
|
+
* Under pipelining, a proposer publishes the blocks for a checkpoint (block-only proposals) before
|
|
446
|
+
* assembling and publishing the enclosing proposed checkpoint at the end of the build slot. A node
|
|
447
|
+
* that received those blocks but never the proposed checkpoint is left with an orphan tip it must not build on.
|
|
448
|
+
* If no checkpoint proposal was received for the orphan slot, we prune after the receive deadline plus local
|
|
449
|
+
* tolerance. If a checkpoint proposal was received but has not materialized into proposed archiver state,
|
|
450
|
+
* we prune after the consensus materialization deadline.
|
|
451
|
+
*
|
|
452
|
+
* The uncheckpointed suffix is scanned in order. Blocks covered by proposed checkpoints are left in
|
|
453
|
+
* place; the first block not covered by a proposed checkpoint starts the orphan suffix to prune.
|
|
454
|
+
*/
|
|
455
|
+
private async pruneOrphanProposedBlocks(): Promise<void> {
|
|
456
|
+
if (this.config.skipOrphanProposedBlockPruning) {
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
459
|
+
const tips = await this.getL2Tips();
|
|
460
|
+
const now = BigInt(this.dateProvider.nowInSeconds());
|
|
461
|
+
|
|
462
|
+
// Frontier block covered by a proposed (or, falling back, confirmed) checkpoint. Blocks beyond it
|
|
463
|
+
// have no enclosing checkpoint proposal and are the orphan-pruning candidates.
|
|
464
|
+
const proposedCheckpointBlockNumber = await this.stores.blocks.getProposedCheckpointL2BlockNumber();
|
|
465
|
+
|
|
466
|
+
// The proposed tip is a proposed-checkpointed block, so there are no orphan proposed blocks to prune
|
|
467
|
+
if (proposedCheckpointBlockNumber === tips.proposed.number) {
|
|
468
|
+
this.log.trace(`No orphan proposed blocks to prune: proposed tip ${tips.proposed.number} is checkpointed`, {
|
|
469
|
+
proposed: tips.proposed,
|
|
470
|
+
proposedCheckpointBlockNumber,
|
|
471
|
+
});
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
// Load the blocks that are candidates for pruning (ie blocks without a proposed checkpoint covering them)
|
|
476
|
+
const blocksWithoutProposedCheckpoint = await this.stores.blocks.getBlocksData({
|
|
477
|
+
from: BlockNumber(proposedCheckpointBlockNumber + 1),
|
|
478
|
+
limit: tips.proposed.number - proposedCheckpointBlockNumber,
|
|
479
|
+
});
|
|
480
|
+
|
|
481
|
+
// Iterate through them in order, the first one with a slot that should have received a proposed checkpoint
|
|
482
|
+
// is the first orphan block, and all blocks after it are also orphaned and should be pruned.
|
|
483
|
+
let lastSlotChecked = undefined;
|
|
484
|
+
for (const blockData of blocksWithoutProposedCheckpoint) {
|
|
485
|
+
// No need to recheck if this block had the same slot as the previous one.
|
|
486
|
+
const blockSlot = blockData.header.getSlot();
|
|
487
|
+
const blockNumber = blockData.header.getBlockNumber();
|
|
488
|
+
if (lastSlotChecked !== undefined && blockSlot === lastSlotChecked) {
|
|
489
|
+
continue;
|
|
490
|
+
}
|
|
491
|
+
lastSlotChecked = blockSlot;
|
|
492
|
+
|
|
493
|
+
const hasCheckpointProposal = await this.checkpointProposalPresence.hasCheckpointProposalForSlot(blockSlot);
|
|
494
|
+
const deadlineType = hasCheckpointProposal ? 'checkpoint-proposal-synced' : 'checkpoint-proposal-received';
|
|
495
|
+
const selectedDeadline = BigInt(
|
|
496
|
+
hasCheckpointProposal
|
|
497
|
+
? this.timetable.getCheckpointProposalSyncedDeadline(blockSlot)
|
|
498
|
+
: Math.ceil(
|
|
499
|
+
this.timetable.getCheckpointProposalReceiveDeadline(blockSlot) +
|
|
500
|
+
this.config.orphanPruneNoProposalTolerance,
|
|
501
|
+
),
|
|
502
|
+
);
|
|
503
|
+
|
|
504
|
+
// If it's still not checkpointed once strictly past the selected deadline, prune it along with all blocks after
|
|
505
|
+
// it. A proposal may still legitimately arrive or materialize at exactly its deadline, so the tip is only
|
|
506
|
+
// orphaned once that instant has fully elapsed.
|
|
507
|
+
if (now > selectedDeadline) {
|
|
508
|
+
const pruneAfterBlockNumber = BlockNumber(blockNumber - 1);
|
|
509
|
+
this.log.warn(
|
|
510
|
+
`Pruning orphan blocks after block ${pruneAfterBlockNumber}: block at slot ${blockSlot} belongs to ` +
|
|
511
|
+
`checkpoint ${blockData.checkpointNumber} which has no matching proposed checkpoint`,
|
|
512
|
+
{
|
|
513
|
+
firstUncheckpointedBlockHeader: blockData.header.toInspect(),
|
|
514
|
+
blockCheckpointNumber: blockData.checkpointNumber,
|
|
515
|
+
blockNumber,
|
|
516
|
+
blockSlot,
|
|
517
|
+
hasCheckpointProposal,
|
|
518
|
+
deadlineType,
|
|
519
|
+
selectedDeadline,
|
|
520
|
+
now,
|
|
521
|
+
},
|
|
522
|
+
);
|
|
523
|
+
|
|
524
|
+
const prunedBlocks = await this.updater.removeBlocksWithoutProposedCheckpointAfter(pruneAfterBlockNumber);
|
|
525
|
+
if (prunedBlocks.length > 0) {
|
|
526
|
+
this.instrumentation.recordPrune('orphan');
|
|
527
|
+
this.events.emit(L2BlockSourceEvents.L2PruneUncheckpointed, {
|
|
528
|
+
type: L2BlockSourceEvents.L2PruneUncheckpointed,
|
|
529
|
+
slotNumber: blockSlot,
|
|
530
|
+
blocks: prunedBlocks,
|
|
531
|
+
});
|
|
532
|
+
}
|
|
533
|
+
return;
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
this.log.trace('No orphan proposed blocks to prune: all uncheckpointed blocks are still within deadline', {
|
|
538
|
+
blocksWithoutProposedCheckpoint: blocksWithoutProposedCheckpoint.map(b => b.header.toInspect()),
|
|
539
|
+
});
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
private async syncFromL1(): Promise<L2Block[]> {
|
|
543
|
+
// Delegate to the L1 synchronizer
|
|
544
|
+
const blocksAdded = await this.synchronizer.syncFromL1(this.initialSyncComplete);
|
|
545
|
+
|
|
546
|
+
// Check if we've completed initial sync
|
|
547
|
+
const currentL1BlockNumber = this.synchronizer.getL1BlockNumber();
|
|
548
|
+
if (currentL1BlockNumber !== undefined && !this.initialSyncComplete) {
|
|
549
|
+
const l1BlockNumberAtEnd = await this.publicClient.getBlockNumber();
|
|
550
|
+
if (currentL1BlockNumber + 1n >= l1BlockNumberAtEnd) {
|
|
551
|
+
this.log.info(`Initial archiver sync to L1 block ${currentL1BlockNumber} complete`, {
|
|
552
|
+
l1BlockNumber: currentL1BlockNumber,
|
|
553
|
+
syncPoint: await getArchiverSynchPoint(this.stores),
|
|
554
|
+
...(await this.getL2Tips()),
|
|
555
|
+
});
|
|
556
|
+
this.runningPromise.setPollingIntervalMS(this.config.pollingIntervalMs);
|
|
557
|
+
this.initialSyncComplete = true;
|
|
558
|
+
this.initialSyncPromise.resolve();
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
return blocksAdded;
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
/** Resumes the archiver after a stop. */
|
|
565
|
+
public resume() {
|
|
566
|
+
if (this.runningPromise.isRunning()) {
|
|
567
|
+
this.log.warn(`Archiver already running`);
|
|
568
|
+
}
|
|
569
|
+
this.log.info(`Restarting archiver`);
|
|
570
|
+
this.runningPromise.start();
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
/**
|
|
574
|
+
* Stops the archiver.
|
|
575
|
+
* @returns A promise signalling completion of the stop process.
|
|
576
|
+
*/
|
|
577
|
+
public async stop(): Promise<void> {
|
|
578
|
+
this.log.debug('Stopping...');
|
|
579
|
+
await this.runningPromise.stop();
|
|
580
|
+
|
|
581
|
+
this.log.info('Stopped.');
|
|
582
|
+
return Promise.resolve();
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
public backupTo(destPath: string): Promise<string> {
|
|
586
|
+
return backupArchiverDataStores(this.dataStores, destPath);
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
public getL1Constants(): Promise<L1RollupConstants> {
|
|
590
|
+
return Promise.resolve(this.l1Constants);
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
public getGenesisValues(): Promise<{ genesisArchiveRoot: Fr }> {
|
|
594
|
+
return Promise.resolve({ genesisArchiveRoot: this.l1Constants.genesisArchiveRoot });
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
public getRollupAddress(): Promise<EthAddress> {
|
|
598
|
+
return Promise.resolve(EthAddress.fromString(this.rollup.address));
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
public getRegistryAddress(): Promise<EthAddress> {
|
|
602
|
+
return Promise.resolve(this.l1Addresses.registryAddress);
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
public getL1BlockNumber(): bigint | undefined {
|
|
606
|
+
return this.synchronizer.getL1BlockNumber();
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
public getL1Timestamp(): Promise<bigint | undefined> {
|
|
610
|
+
return Promise.resolve(this.synchronizer.getL1Timestamp());
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
public async getSyncedL2SlotNumber(): Promise<SlotNumber | undefined> {
|
|
614
|
+
// The synced L2 slot is the latest slot for which we have all L1 data,
|
|
615
|
+
// either because we have seen all L1 blocks for that slot, or because
|
|
616
|
+
// we have seen the corresponding checkpoint.
|
|
617
|
+
|
|
618
|
+
let slotFromL1Sync: SlotNumber | undefined;
|
|
619
|
+
const l1Timestamp = this.synchronizer.getL1Timestamp();
|
|
620
|
+
if (l1Timestamp !== undefined) {
|
|
621
|
+
const nextL1BlockSlot = getSlotAtNextL1Block(l1Timestamp, this.l1Constants);
|
|
622
|
+
if (Number(nextL1BlockSlot) > 0) {
|
|
623
|
+
slotFromL1Sync = SlotNumber.add(nextL1BlockSlot, -1);
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
let slotFromCheckpoint: SlotNumber | undefined;
|
|
628
|
+
const latestCheckpointNumber = await this.stores.blocks.getLatestCheckpointNumber();
|
|
629
|
+
if (latestCheckpointNumber > 0) {
|
|
630
|
+
const checkpointData = await this.stores.blocks.getCheckpointData(latestCheckpointNumber);
|
|
631
|
+
if (checkpointData) {
|
|
632
|
+
slotFromCheckpoint = checkpointData.header.slotNumber;
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
if (slotFromL1Sync === undefined && slotFromCheckpoint === undefined) {
|
|
637
|
+
return undefined;
|
|
638
|
+
}
|
|
639
|
+
return SlotNumber(Math.max(slotFromL1Sync ?? 0, slotFromCheckpoint ?? 0));
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
public async getSyncedL2EpochNumber(): Promise<EpochNumber | undefined> {
|
|
643
|
+
const syncedSlot = await this.getSyncedL2SlotNumber();
|
|
644
|
+
if (syncedSlot === undefined) {
|
|
645
|
+
return undefined;
|
|
646
|
+
}
|
|
647
|
+
// An epoch is fully synced when all its slots are synced.
|
|
648
|
+
// We check if syncedSlot is the last slot of its epoch; if so, that epoch is fully synced.
|
|
649
|
+
// Otherwise, only the previous epoch is fully synced.
|
|
650
|
+
const epoch = getEpochAtSlot(syncedSlot, this.l1Constants);
|
|
651
|
+
const [, endSlot] = getSlotRangeForEpoch(epoch, this.l1Constants);
|
|
652
|
+
if (syncedSlot >= endSlot) {
|
|
653
|
+
return epoch;
|
|
654
|
+
}
|
|
655
|
+
return Number(epoch) > 0 ? EpochNumber(Number(epoch) - 1) : undefined;
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
public async isEpochComplete(epochNumber: EpochNumber): Promise<boolean> {
|
|
659
|
+
// The epoch is complete if the current checkpointed L2 block is the last one in the epoch (or later).
|
|
660
|
+
// We use the checkpointed block number (synced from L1) instead of 'latest' to avoid returning true
|
|
661
|
+
// prematurely when proposed blocks have been pushed to the archiver but not yet checkpointed on L1.
|
|
662
|
+
const header = (await this.getBlockData({ tag: 'checkpointed' }))?.header;
|
|
663
|
+
const slot = header ? header.globalVariables.slotNumber : undefined;
|
|
664
|
+
const [_startSlot, endSlot] = getSlotRangeForEpoch(epochNumber, this.l1Constants);
|
|
665
|
+
if (slot && slot >= endSlot) {
|
|
666
|
+
return true;
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
// If we haven't run an initial sync, just return false.
|
|
670
|
+
const l1Timestamp = this.synchronizer.getL1Timestamp();
|
|
671
|
+
if (l1Timestamp === undefined) {
|
|
672
|
+
return false;
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
// If not, the epoch may also be complete if the L2 slot has passed without a block
|
|
676
|
+
// We compute this based on the end timestamp for the given epoch and the timestamp of the last L1 block
|
|
677
|
+
const [_startTimestamp, endTimestamp] = getTimestampRangeForEpoch(epochNumber, this.l1Constants);
|
|
678
|
+
|
|
679
|
+
// For this computation, we throw in a few extra seconds just for good measure,
|
|
680
|
+
// since we know the next L1 block won't be mined within this range. Remember that
|
|
681
|
+
// l1timestamp is the timestamp of the last l1 block we've seen, so this relies on
|
|
682
|
+
// the fact that L1 won't mine two blocks within this time of each other.
|
|
683
|
+
// TODO(palla/reorg): Is the above a safe assumption?
|
|
684
|
+
const leeway = 1n;
|
|
685
|
+
return l1Timestamp + leeway >= endTimestamp;
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
/** Returns whether the archiver has completed an initial sync run successfully. */
|
|
689
|
+
public isInitialSyncComplete(): boolean {
|
|
690
|
+
return this.initialSyncComplete;
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
public removeCheckpointsAfter(checkpointNumber: CheckpointNumber): Promise<boolean> {
|
|
694
|
+
return this.updater.removeCheckpointsAfter(checkpointNumber);
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
/**
|
|
698
|
+
* Removes all uncheckpointed blocks strictly after `blockNumber`, along with the proposed checkpoints
|
|
699
|
+
* that referenced them. Used by the AutomineSequencer to undo a local insert whose propose tx failed
|
|
700
|
+
* to land on L1 (no reorg needed — nothing reached L1). Refuses to touch checkpointed blocks.
|
|
701
|
+
*/
|
|
702
|
+
public removeUncheckpointedBlocksAfter(blockNumber: BlockNumber): Promise<L2Block[]> {
|
|
703
|
+
return this.updater.removeUncheckpointedBlocksAfter(blockNumber);
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
/** Used by TXE to add checkpoints directly without syncing from L1. */
|
|
707
|
+
public async addCheckpoints(
|
|
708
|
+
checkpoints: PublishedCheckpoint[],
|
|
709
|
+
pendingChainValidationStatus?: ValidateCheckpointResult,
|
|
710
|
+
): Promise<boolean> {
|
|
711
|
+
await this.updater.addCheckpoints(checkpoints, pendingChainValidationStatus);
|
|
712
|
+
return true;
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
public getL2Tips(): Promise<L2Tips> {
|
|
716
|
+
return this.l2TipsCache.getL2Tips();
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
public async rollbackTo(targetL2BlockNumber: BlockNumber): Promise<void> {
|
|
720
|
+
const currentBlocks = await this.getL2Tips();
|
|
721
|
+
const currentL2Block = currentBlocks.proposed.number;
|
|
722
|
+
const currentProvenBlock = currentBlocks.proven.block.number;
|
|
723
|
+
|
|
724
|
+
if (targetL2BlockNumber >= currentL2Block) {
|
|
725
|
+
throw new Error(`Target L2 block ${targetL2BlockNumber} must be less than current L2 block ${currentL2Block}`);
|
|
726
|
+
}
|
|
727
|
+
const checkpointedTip = await this.stores.blocks.getCheckpointedL2BlockNumber();
|
|
728
|
+
if (targetL2BlockNumber > checkpointedTip) {
|
|
729
|
+
throw new Error(`Target L2 block ${targetL2BlockNumber} is not checkpointed yet`);
|
|
730
|
+
}
|
|
731
|
+
const targetBlockData = await this.stores.blocks.getBlockData({ number: targetL2BlockNumber });
|
|
732
|
+
if (!targetBlockData) {
|
|
733
|
+
throw new Error(`Target L2 block ${targetL2BlockNumber} not found`);
|
|
734
|
+
}
|
|
735
|
+
const targetCheckpointNumber = targetBlockData.checkpointNumber;
|
|
736
|
+
|
|
737
|
+
// Rollback operates at checkpoint granularity: the target block must be the last block of its checkpoint.
|
|
738
|
+
const checkpointData = await this.stores.blocks.getCheckpointData(targetCheckpointNumber);
|
|
739
|
+
if (!checkpointData) {
|
|
740
|
+
throw new Error(`Checkpoint ${targetCheckpointNumber} not found for block ${targetL2BlockNumber}`);
|
|
741
|
+
}
|
|
742
|
+
const lastBlockInCheckpoint = BlockNumber(checkpointData.startBlock + checkpointData.blockCount - 1);
|
|
743
|
+
if (targetL2BlockNumber !== lastBlockInCheckpoint) {
|
|
744
|
+
const previousCheckpointBoundary =
|
|
745
|
+
checkpointData.startBlock > 1 ? BlockNumber(checkpointData.startBlock - 1) : BlockNumber(0);
|
|
746
|
+
throw new Error(
|
|
747
|
+
`Target L2 block ${targetL2BlockNumber} is not at a checkpoint boundary. ` +
|
|
748
|
+
`Checkpoint ${targetCheckpointNumber} spans blocks ${checkpointData.startBlock} to ${lastBlockInCheckpoint}. ` +
|
|
749
|
+
`Use block ${lastBlockInCheckpoint} to roll back to this checkpoint, ` +
|
|
750
|
+
`or block ${previousCheckpointBoundary} to roll back to the previous one.`,
|
|
751
|
+
);
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
const targetL1BlockNumber = checkpointData.l1.blockNumber;
|
|
755
|
+
const targetL1Block = await this.publicClient.getBlock({
|
|
756
|
+
blockNumber: targetL1BlockNumber,
|
|
757
|
+
includeTransactions: false,
|
|
758
|
+
});
|
|
759
|
+
if (!targetL1Block) {
|
|
760
|
+
throw new Error(`Missing L1 block ${targetL1BlockNumber}`);
|
|
761
|
+
}
|
|
762
|
+
const targetL1BlockHash = Buffer32.fromString(targetL1Block.hash);
|
|
763
|
+
this.log.info(
|
|
764
|
+
`Removing checkpoints after checkpoint ${targetCheckpointNumber} (target block ${targetL2BlockNumber})`,
|
|
765
|
+
);
|
|
766
|
+
await this.updater.removeCheckpointsAfter(targetCheckpointNumber);
|
|
767
|
+
this.log.info(`Rolling back L1 to L2 messages to checkpoint ${targetCheckpointNumber}`);
|
|
768
|
+
await this.stores.messages.rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber);
|
|
769
|
+
this.log.info(`Setting L1 syncpoints to ${targetL1BlockNumber}`);
|
|
770
|
+
await this.stores.blocks.setSynchedL1BlockNumber(targetL1BlockNumber);
|
|
771
|
+
await this.stores.messages.setMessageSyncState(
|
|
772
|
+
{ l1BlockNumber: targetL1BlockNumber, l1BlockHash: targetL1BlockHash },
|
|
773
|
+
undefined,
|
|
774
|
+
);
|
|
775
|
+
if (targetL2BlockNumber < currentProvenBlock) {
|
|
776
|
+
this.log.info(`Rolling back proven L2 checkpoint to ${targetCheckpointNumber}`);
|
|
777
|
+
await this.updater.setProvenCheckpointNumber(targetCheckpointNumber);
|
|
778
|
+
}
|
|
779
|
+
const currentFinalizedBlock = currentBlocks.finalized.block.number;
|
|
780
|
+
if (targetL2BlockNumber < currentFinalizedBlock) {
|
|
781
|
+
this.log.info(`Rolling back finalized L2 checkpoint to ${targetCheckpointNumber}`);
|
|
782
|
+
await this.updater.setFinalizedCheckpointNumber(targetCheckpointNumber);
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
}
|