@aztec/archiver 3.0.0-devnet.5 → 3.0.0-devnet.6-patch.1
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/dest/archiver/archiver.d.ts +67 -60
- package/dest/archiver/archiver.d.ts.map +1 -1
- package/dest/archiver/archiver.js +391 -262
- package/dest/archiver/archiver_store.d.ts +21 -27
- package/dest/archiver/archiver_store.d.ts.map +1 -1
- package/dest/archiver/archiver_store_test_suite.d.ts +1 -1
- package/dest/archiver/archiver_store_test_suite.d.ts.map +1 -1
- package/dest/archiver/archiver_store_test_suite.js +125 -140
- package/dest/archiver/config.d.ts +3 -2
- package/dest/archiver/config.d.ts.map +1 -1
- package/dest/archiver/config.js +13 -1
- package/dest/archiver/errors.d.ts +1 -1
- package/dest/archiver/errors.d.ts.map +1 -1
- package/dest/archiver/index.d.ts +1 -1
- package/dest/archiver/instrumentation.d.ts +5 -3
- package/dest/archiver/instrumentation.d.ts.map +1 -1
- package/dest/archiver/instrumentation.js +11 -0
- package/dest/archiver/kv_archiver_store/block_store.d.ts +10 -9
- package/dest/archiver/kv_archiver_store/block_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/block_store.js +9 -8
- package/dest/archiver/kv_archiver_store/contract_class_store.d.ts +2 -2
- package/dest/archiver/kv_archiver_store/contract_class_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/contract_class_store.js +1 -1
- package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts +2 -2
- package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +23 -29
- package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/kv_archiver_store.js +7 -15
- package/dest/archiver/kv_archiver_store/log_store.d.ts +3 -10
- package/dest/archiver/kv_archiver_store/log_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/log_store.js +4 -26
- package/dest/archiver/kv_archiver_store/message_store.d.ts +6 -5
- package/dest/archiver/kv_archiver_store/message_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/message_store.js +15 -14
- package/dest/archiver/l1/bin/retrieve-calldata.d.ts +3 -0
- package/dest/archiver/l1/bin/retrieve-calldata.d.ts.map +1 -0
- package/dest/archiver/l1/bin/retrieve-calldata.js +147 -0
- package/dest/archiver/l1/calldata_retriever.d.ts +98 -0
- package/dest/archiver/l1/calldata_retriever.d.ts.map +1 -0
- package/dest/archiver/l1/calldata_retriever.js +403 -0
- package/dest/archiver/l1/data_retrieval.d.ts +87 -0
- package/dest/archiver/l1/data_retrieval.d.ts.map +1 -0
- package/dest/archiver/l1/data_retrieval.js +318 -0
- package/dest/archiver/l1/debug_tx.d.ts +19 -0
- package/dest/archiver/l1/debug_tx.d.ts.map +1 -0
- package/dest/archiver/l1/debug_tx.js +73 -0
- package/dest/archiver/l1/spire_proposer.d.ts +70 -0
- package/dest/archiver/l1/spire_proposer.d.ts.map +1 -0
- package/dest/archiver/l1/spire_proposer.js +157 -0
- package/dest/archiver/l1/trace_tx.d.ts +97 -0
- package/dest/archiver/l1/trace_tx.d.ts.map +1 -0
- package/dest/archiver/l1/trace_tx.js +91 -0
- package/dest/archiver/l1/types.d.ts +12 -0
- package/dest/archiver/l1/types.d.ts.map +1 -0
- package/dest/archiver/l1/types.js +3 -0
- package/dest/archiver/l1/validate_trace.d.ts +29 -0
- package/dest/archiver/l1/validate_trace.d.ts.map +1 -0
- package/dest/archiver/l1/validate_trace.js +150 -0
- package/dest/archiver/structs/data_retrieval.d.ts +1 -1
- package/dest/archiver/structs/inbox_message.d.ts +4 -4
- package/dest/archiver/structs/inbox_message.d.ts.map +1 -1
- package/dest/archiver/structs/inbox_message.js +6 -5
- package/dest/archiver/structs/published.d.ts +3 -2
- package/dest/archiver/structs/published.d.ts.map +1 -1
- package/dest/archiver/validation.d.ts +10 -4
- package/dest/archiver/validation.d.ts.map +1 -1
- package/dest/archiver/validation.js +29 -21
- package/dest/factory.d.ts +3 -5
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +3 -2
- package/dest/index.d.ts +2 -2
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +1 -1
- package/dest/rpc/index.d.ts +2 -2
- package/dest/test/index.d.ts +1 -1
- package/dest/test/mock_archiver.d.ts +16 -8
- package/dest/test/mock_archiver.d.ts.map +1 -1
- package/dest/test/mock_archiver.js +19 -14
- package/dest/test/mock_l1_to_l2_message_source.d.ts +7 -6
- package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
- package/dest/test/mock_l1_to_l2_message_source.js +10 -9
- package/dest/test/mock_l2_block_source.d.ts +15 -10
- package/dest/test/mock_l2_block_source.d.ts.map +1 -1
- package/dest/test/mock_l2_block_source.js +22 -9
- package/dest/test/mock_structs.d.ts +3 -2
- package/dest/test/mock_structs.d.ts.map +1 -1
- package/dest/test/mock_structs.js +9 -8
- package/package.json +18 -17
- package/src/archiver/archiver.ts +531 -345
- package/src/archiver/archiver_store.ts +24 -27
- package/src/archiver/archiver_store_test_suite.ts +151 -128
- package/src/archiver/config.ts +13 -7
- package/src/archiver/instrumentation.ts +16 -2
- package/src/archiver/kv_archiver_store/block_store.ts +18 -17
- package/src/archiver/kv_archiver_store/contract_class_store.ts +1 -1
- package/src/archiver/kv_archiver_store/contract_instance_store.ts +1 -1
- package/src/archiver/kv_archiver_store/kv_archiver_store.ts +23 -32
- package/src/archiver/kv_archiver_store/log_store.ts +4 -30
- package/src/archiver/kv_archiver_store/message_store.ts +21 -18
- package/src/archiver/l1/README.md +98 -0
- package/src/archiver/l1/bin/retrieve-calldata.ts +182 -0
- package/src/archiver/l1/calldata_retriever.ts +531 -0
- package/src/archiver/{data_retrieval.ts → l1/data_retrieval.ts} +196 -250
- package/src/archiver/l1/debug_tx.ts +99 -0
- package/src/archiver/l1/spire_proposer.ts +160 -0
- package/src/archiver/l1/trace_tx.ts +128 -0
- package/src/archiver/l1/types.ts +13 -0
- package/src/archiver/l1/validate_trace.ts +211 -0
- package/src/archiver/structs/inbox_message.ts +8 -8
- package/src/archiver/structs/published.ts +2 -1
- package/src/archiver/validation.ts +52 -27
- package/src/factory.ts +4 -5
- package/src/index.ts +1 -1
- 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/mock_archiver.ts +22 -16
- package/src/test/mock_l1_to_l2_message_source.ts +10 -9
- package/src/test/mock_l2_block_source.ts +32 -15
- package/src/test/mock_structs.ts +10 -9
- package/dest/archiver/data_retrieval.d.ts +0 -79
- package/dest/archiver/data_retrieval.d.ts.map +0 -1
- package/dest/archiver/data_retrieval.js +0 -362
|
@@ -4,76 +4,89 @@ function _ts_decorate(decorators, target, key, desc) {
|
|
|
4
4
|
else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
5
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
6
|
}
|
|
7
|
+
import { GENESIS_BLOCK_HEADER_HASH } from '@aztec/constants';
|
|
7
8
|
import { EpochCache } from '@aztec/epoch-cache';
|
|
8
|
-
import {
|
|
9
|
+
import { createEthereumChain } from '@aztec/ethereum/chain';
|
|
10
|
+
import { BlockTagTooOldError, InboxContract, RollupContract } from '@aztec/ethereum/contracts';
|
|
9
11
|
import { maxBigint } from '@aztec/foundation/bigint';
|
|
12
|
+
import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
|
|
10
13
|
import { Buffer16, Buffer32 } from '@aztec/foundation/buffer';
|
|
11
14
|
import { merge, pick } from '@aztec/foundation/collection';
|
|
12
|
-
import { Fr } from '@aztec/foundation/
|
|
15
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
13
16
|
import { createLogger } from '@aztec/foundation/log';
|
|
17
|
+
import { promiseWithResolvers } from '@aztec/foundation/promise';
|
|
14
18
|
import { RunningPromise, makeLoggingErrorHandler } from '@aztec/foundation/running-promise';
|
|
15
|
-
import { sleep } from '@aztec/foundation/sleep';
|
|
16
19
|
import { count } from '@aztec/foundation/string';
|
|
17
|
-
import { Timer, elapsed } from '@aztec/foundation/timer';
|
|
20
|
+
import { DateProvider, Timer, elapsed } from '@aztec/foundation/timer';
|
|
18
21
|
import { ContractClassPublishedEvent, PrivateFunctionBroadcastedEvent, UtilityFunctionBroadcastedEvent } from '@aztec/protocol-contracts/class-registry';
|
|
19
22
|
import { ContractInstancePublishedEvent, ContractInstanceUpdatedEvent } from '@aztec/protocol-contracts/instance-registry';
|
|
20
|
-
import { L2BlockSourceEvents } from '@aztec/stdlib/block';
|
|
23
|
+
import { L2Block, L2BlockSourceEvents, PublishedL2Block } from '@aztec/stdlib/block';
|
|
21
24
|
import { computePublicBytecodeCommitment, isValidPrivateFunctionMembershipProof, isValidUtilityFunctionMembershipProof } from '@aztec/stdlib/contract';
|
|
22
25
|
import { getEpochAtSlot, getEpochNumberAtTimestamp, getSlotAtTimestamp, getSlotRangeForEpoch, getTimestampRangeForEpoch } from '@aztec/stdlib/epoch-helpers';
|
|
23
|
-
import {
|
|
26
|
+
import { computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
|
|
27
|
+
import { getTelemetryClient, trackSpan } from '@aztec/telemetry-client';
|
|
24
28
|
import { EventEmitter } from 'events';
|
|
25
29
|
import groupBy from 'lodash.groupby';
|
|
26
30
|
import { createPublicClient, fallback, http } from 'viem';
|
|
27
|
-
import { retrieveBlocksFromRollup, retrieveL1ToL2Message, retrieveL1ToL2Messages, retrievedBlockToPublishedL2Block } from './data_retrieval.js';
|
|
28
31
|
import { InitialBlockNumberNotSequentialError, NoBlobBodiesFoundError } from './errors.js';
|
|
29
32
|
import { ArchiverInstrumentation } from './instrumentation.js';
|
|
30
|
-
import {
|
|
33
|
+
import { retrieveCheckpointsFromRollup, retrieveL1ToL2Message, retrieveL1ToL2Messages, retrievedToPublishedCheckpoint } from './l1/data_retrieval.js';
|
|
34
|
+
import { validateAndLogTraceAvailability } from './l1/validate_trace.js';
|
|
35
|
+
import { validateCheckpointAttestations } from './validation.js';
|
|
31
36
|
function mapArchiverConfig(config) {
|
|
32
37
|
return {
|
|
33
38
|
pollingIntervalMs: config.archiverPollingIntervalMS,
|
|
34
39
|
batchSize: config.archiverBatchSize,
|
|
35
|
-
skipValidateBlockAttestations: config.skipValidateBlockAttestations
|
|
40
|
+
skipValidateBlockAttestations: config.skipValidateBlockAttestations,
|
|
41
|
+
maxAllowedEthClientDriftSeconds: config.maxAllowedEthClientDriftSeconds,
|
|
42
|
+
ethereumAllowNoDebugHosts: config.ethereumAllowNoDebugHosts
|
|
36
43
|
};
|
|
37
44
|
}
|
|
38
45
|
/**
|
|
39
|
-
* Pulls
|
|
46
|
+
* Pulls checkpoints in a non-blocking manner and provides interface for their retrieval.
|
|
40
47
|
* Responsible for handling robust L1 polling so that other components do not need to
|
|
41
48
|
* concern themselves with it.
|
|
42
49
|
*/ export class Archiver extends EventEmitter {
|
|
43
50
|
publicClient;
|
|
51
|
+
debugClient;
|
|
44
52
|
l1Addresses;
|
|
45
53
|
dataStore;
|
|
46
54
|
config;
|
|
47
55
|
blobSinkClient;
|
|
48
56
|
epochCache;
|
|
57
|
+
dateProvider;
|
|
49
58
|
instrumentation;
|
|
50
59
|
l1constants;
|
|
51
60
|
log;
|
|
52
|
-
/**
|
|
53
|
-
* A promise in which we will be continually fetching new L2 blocks.
|
|
54
|
-
*/ runningPromise;
|
|
61
|
+
/** A loop in which we will be continually fetching new checkpoints. */ runningPromise;
|
|
55
62
|
rollup;
|
|
56
63
|
inbox;
|
|
57
64
|
store;
|
|
58
65
|
l1BlockNumber;
|
|
59
66
|
l1Timestamp;
|
|
60
67
|
initialSyncComplete;
|
|
68
|
+
initialSyncPromise;
|
|
61
69
|
tracer;
|
|
62
70
|
/**
|
|
63
71
|
* Creates a new instance of the Archiver.
|
|
64
72
|
* @param publicClient - A client for interacting with the Ethereum node.
|
|
73
|
+
* @param debugClient - A client for interacting with the Ethereum node for debug/trace methods.
|
|
65
74
|
* @param rollupAddress - Ethereum address of the rollup contract.
|
|
66
75
|
* @param inboxAddress - Ethereum address of the inbox contract.
|
|
67
76
|
* @param registryAddress - Ethereum address of the registry contract.
|
|
68
77
|
* @param pollingIntervalMs - The interval for polling for L1 logs (in milliseconds).
|
|
69
78
|
* @param store - An archiver data store for storage & retrieval of blocks, encrypted logs & contract data.
|
|
70
79
|
* @param log - A logger.
|
|
71
|
-
*/ constructor(publicClient, l1Addresses, dataStore, config, blobSinkClient, epochCache, instrumentation, l1constants, log = createLogger('archiver')){
|
|
72
|
-
super(), this.publicClient = publicClient, this.l1Addresses = l1Addresses, this.dataStore = dataStore, this.config = config, this.blobSinkClient = blobSinkClient, this.epochCache = epochCache, this.instrumentation = instrumentation, this.l1constants = l1constants, this.log = log, this.initialSyncComplete = false;
|
|
80
|
+
*/ constructor(publicClient, debugClient, l1Addresses, dataStore, config, blobSinkClient, epochCache, dateProvider, instrumentation, l1constants, log = createLogger('archiver')){
|
|
81
|
+
super(), this.publicClient = publicClient, this.debugClient = debugClient, this.l1Addresses = l1Addresses, this.dataStore = dataStore, this.config = config, this.blobSinkClient = blobSinkClient, this.epochCache = epochCache, this.dateProvider = dateProvider, this.instrumentation = instrumentation, this.l1constants = l1constants, this.log = log, this.initialSyncComplete = false;
|
|
73
82
|
this.tracer = instrumentation.tracer;
|
|
74
83
|
this.store = new ArchiverStoreHelper(dataStore);
|
|
75
84
|
this.rollup = new RollupContract(publicClient, l1Addresses.rollupAddress);
|
|
76
85
|
this.inbox = new InboxContract(publicClient, l1Addresses.inboxAddress);
|
|
86
|
+
this.initialSyncPromise = promiseWithResolvers();
|
|
87
|
+
// Running promise starts with a small interval inbetween runs, so all iterations needed for the initial sync
|
|
88
|
+
// are done as fast as possible. This then gets updated once the initial sync completes.
|
|
89
|
+
this.runningPromise = new RunningPromise(()=>this.sync(), this.log, this.config.pollingIntervalMs / 10, makeLoggingErrorHandler(this.log, NoBlobBodiesFoundError, BlockTagTooOldError));
|
|
77
90
|
}
|
|
78
91
|
/**
|
|
79
92
|
* Creates a new instance of the Archiver and blocks until it syncs from chain.
|
|
@@ -88,12 +101,20 @@ function mapArchiverConfig(config) {
|
|
|
88
101
|
transport: fallback(config.l1RpcUrls.map((url)=>http(url))),
|
|
89
102
|
pollingInterval: config.viemPollingIntervalMS
|
|
90
103
|
});
|
|
104
|
+
// Create debug client using debug RPC URLs if available, otherwise fall back to regular RPC URLs
|
|
105
|
+
const debugRpcUrls = config.l1DebugRpcUrls.length > 0 ? config.l1DebugRpcUrls : config.l1RpcUrls;
|
|
106
|
+
const debugClient = createPublicClient({
|
|
107
|
+
chain: chain.chainInfo,
|
|
108
|
+
transport: fallback(debugRpcUrls.map((url)=>http(url))),
|
|
109
|
+
pollingInterval: config.viemPollingIntervalMS
|
|
110
|
+
});
|
|
91
111
|
const rollup = new RollupContract(publicClient, config.l1Contracts.rollupAddress);
|
|
92
|
-
const [l1StartBlock, l1GenesisTime, proofSubmissionEpochs, genesisArchiveRoot] = await Promise.all([
|
|
112
|
+
const [l1StartBlock, l1GenesisTime, proofSubmissionEpochs, genesisArchiveRoot, slashingProposerAddress] = await Promise.all([
|
|
93
113
|
rollup.getL1StartBlock(),
|
|
94
114
|
rollup.getL1GenesisTime(),
|
|
95
115
|
rollup.getProofSubmissionEpochs(),
|
|
96
|
-
rollup.getGenesisArchiveTreeRoot()
|
|
116
|
+
rollup.getGenesisArchiveTreeRoot(),
|
|
117
|
+
rollup.getSlashingProposerAddress()
|
|
97
118
|
]);
|
|
98
119
|
const l1StartBlockHash = await publicClient.getBlock({
|
|
99
120
|
blockNumber: l1StartBlock,
|
|
@@ -112,11 +133,16 @@ function mapArchiverConfig(config) {
|
|
|
112
133
|
};
|
|
113
134
|
const opts = merge({
|
|
114
135
|
pollingIntervalMs: 10_000,
|
|
115
|
-
batchSize: 100
|
|
136
|
+
batchSize: 100,
|
|
137
|
+
maxAllowedEthClientDriftSeconds: 300,
|
|
138
|
+
ethereumAllowNoDebugHosts: false
|
|
116
139
|
}, mapArchiverConfig(config));
|
|
117
140
|
const epochCache = deps.epochCache ?? await EpochCache.create(config.l1Contracts.rollupAddress, config, deps);
|
|
118
141
|
const telemetry = deps.telemetry ?? getTelemetryClient();
|
|
119
|
-
const archiver = new Archiver(publicClient,
|
|
142
|
+
const archiver = new Archiver(publicClient, debugClient, {
|
|
143
|
+
...config.l1Contracts,
|
|
144
|
+
slashingProposerAddress
|
|
145
|
+
}, archiverStore, opts, deps.blobSinkClient, epochCache, deps.dateProvider ?? new DateProvider(), await ArchiverInstrumentation.new(telemetry, ()=>archiverStore.estimateSize()), l1Constants);
|
|
120
146
|
await archiver.start(blockUntilSynced);
|
|
121
147
|
return archiver;
|
|
122
148
|
}
|
|
@@ -127,45 +153,49 @@ function mapArchiverConfig(config) {
|
|
|
127
153
|
* Starts sync process.
|
|
128
154
|
* @param blockUntilSynced - If true, blocks until the archiver has fully synced.
|
|
129
155
|
*/ async start(blockUntilSynced) {
|
|
130
|
-
if (this.runningPromise) {
|
|
156
|
+
if (this.runningPromise.isRunning()) {
|
|
131
157
|
throw new Error('Archiver is already running');
|
|
132
158
|
}
|
|
133
159
|
await this.blobSinkClient.testSources();
|
|
160
|
+
await this.testEthereumNodeSynced();
|
|
161
|
+
await validateAndLogTraceAvailability(this.debugClient, this.config.ethereumAllowNoDebugHosts ?? false);
|
|
162
|
+
// Log initial state for the archiver
|
|
163
|
+
const { l1StartBlock } = this.l1constants;
|
|
164
|
+
const { blocksSynchedTo = l1StartBlock, messagesSynchedTo = l1StartBlock } = await this.store.getSynchPoint();
|
|
165
|
+
const currentL2Block = await this.getBlockNumber();
|
|
166
|
+
this.log.info(`Starting archiver sync to rollup contract ${this.l1Addresses.rollupAddress.toString()} from L1 block ${blocksSynchedTo} and L2 block ${currentL2Block}`, {
|
|
167
|
+
blocksSynchedTo,
|
|
168
|
+
messagesSynchedTo,
|
|
169
|
+
currentL2Block
|
|
170
|
+
});
|
|
171
|
+
// Start sync loop, and return the wait for initial sync if we are asked to block until synced
|
|
172
|
+
this.runningPromise.start();
|
|
134
173
|
if (blockUntilSynced) {
|
|
135
|
-
|
|
136
|
-
this.log.info(`Retrying initial archiver sync in ${this.config.pollingIntervalMs}ms`);
|
|
137
|
-
await sleep(this.config.pollingIntervalMs);
|
|
138
|
-
}
|
|
174
|
+
return this.waitForInitialSync();
|
|
139
175
|
}
|
|
140
|
-
this.runningPromise = new RunningPromise(()=>this.sync(false), this.log, this.config.pollingIntervalMs, makeLoggingErrorHandler(this.log, // Ignored errors will not log to the console
|
|
141
|
-
// We ignore NoBlobBodiesFound as the message may not have been passed to the blob sink yet
|
|
142
|
-
NoBlobBodiesFoundError));
|
|
143
|
-
this.runningPromise.start();
|
|
144
176
|
}
|
|
145
177
|
syncImmediate() {
|
|
146
|
-
if (!this.runningPromise) {
|
|
147
|
-
throw new Error('Archiver is not running');
|
|
148
|
-
}
|
|
149
178
|
return this.runningPromise.trigger();
|
|
150
179
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
180
|
+
waitForInitialSync() {
|
|
181
|
+
return this.initialSyncPromise.promise;
|
|
182
|
+
}
|
|
183
|
+
/** Checks that the ethereum node we are connected to has a latest timestamp no more than the allowed drift. Throw if not. */ async testEthereumNodeSynced() {
|
|
184
|
+
const maxAllowedDelay = this.config.maxAllowedEthClientDriftSeconds;
|
|
185
|
+
if (maxAllowedDelay === 0) {
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
const { number, timestamp: l1Timestamp } = await this.publicClient.getBlock({
|
|
189
|
+
includeTransactions: false
|
|
190
|
+
});
|
|
191
|
+
const currentTime = BigInt(this.dateProvider.nowInSeconds());
|
|
192
|
+
if (currentTime - l1Timestamp > BigInt(maxAllowedDelay)) {
|
|
193
|
+
throw new Error(`Ethereum node is out of sync (last block synced ${number} at ${l1Timestamp} vs current time ${currentTime})`);
|
|
164
194
|
}
|
|
165
195
|
}
|
|
166
196
|
/**
|
|
167
197
|
* Fetches logs from L1 contracts and processes them.
|
|
168
|
-
*/ async sync(
|
|
198
|
+
*/ async sync() {
|
|
169
199
|
/**
|
|
170
200
|
* We keep track of three "pointers" to L1 blocks:
|
|
171
201
|
* 1. the last L1 block that published an L2 block
|
|
@@ -175,8 +205,6 @@ function mapArchiverConfig(config) {
|
|
|
175
205
|
* We do this to deal with L1 data providers that are eventually consistent (e.g. Infura).
|
|
176
206
|
* We guard against seeing block X with no data at one point, and later, the provider processes the block and it has data.
|
|
177
207
|
* The archiver will stay back, until there's data on L1 that will move the pointers forward.
|
|
178
|
-
*
|
|
179
|
-
* This code does not handle reorgs.
|
|
180
208
|
*/ const { l1StartBlock, l1StartBlockHash } = this.l1constants;
|
|
181
209
|
const { blocksSynchedTo = l1StartBlock, messagesSynchedTo = {
|
|
182
210
|
l1BlockNumber: l1StartBlock,
|
|
@@ -187,12 +215,12 @@ function mapArchiverConfig(config) {
|
|
|
187
215
|
});
|
|
188
216
|
const currentL1BlockNumber = currentL1Block.number;
|
|
189
217
|
const currentL1BlockHash = Buffer32.fromString(currentL1Block.hash);
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
}
|
|
218
|
+
this.log.trace(`Starting new archiver sync iteration`, {
|
|
219
|
+
blocksSynchedTo,
|
|
220
|
+
messagesSynchedTo,
|
|
221
|
+
currentL1BlockNumber,
|
|
222
|
+
currentL1BlockHash
|
|
223
|
+
});
|
|
196
224
|
// ********** Ensuring Consistency of data pulled from L1 **********
|
|
197
225
|
/**
|
|
198
226
|
* There are a number of calls in this sync operation to L1 for retrieving
|
|
@@ -215,23 +243,40 @@ function mapArchiverConfig(config) {
|
|
|
215
243
|
const currentL1Timestamp = !this.l1Timestamp || !this.l1BlockNumber || this.l1BlockNumber !== currentL1BlockNumber ? (await this.publicClient.getBlock({
|
|
216
244
|
blockNumber: currentL1BlockNumber
|
|
217
245
|
})).timestamp : this.l1Timestamp;
|
|
218
|
-
//
|
|
246
|
+
// Warn if the latest L1 block timestamp is too old
|
|
247
|
+
const maxAllowedDelay = this.config.maxAllowedEthClientDriftSeconds;
|
|
248
|
+
const now = this.dateProvider.nowInSeconds();
|
|
249
|
+
if (maxAllowedDelay > 0 && Number(currentL1Timestamp) <= now - maxAllowedDelay) {
|
|
250
|
+
this.log.warn(`Latest L1 block ${currentL1BlockNumber} timestamp ${currentL1Timestamp} is too old. Make sure your Ethereum node is synced.`, {
|
|
251
|
+
currentL1BlockNumber,
|
|
252
|
+
currentL1Timestamp,
|
|
253
|
+
now,
|
|
254
|
+
maxAllowedDelay
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
// ********** Events that are processed per checkpoint **********
|
|
219
258
|
if (currentL1BlockNumber > blocksSynchedTo) {
|
|
220
|
-
// First we retrieve new L2 blocks and store them in the DB. This will also update the
|
|
221
|
-
// pending chain validation status, proven
|
|
222
|
-
const rollupStatus = await this.
|
|
259
|
+
// First we retrieve new checkpoints and L2 blocks and store them in the DB. This will also update the
|
|
260
|
+
// pending chain validation status, proven checkpoint number, and synched L1 block number.
|
|
261
|
+
const rollupStatus = await this.handleCheckpoints(blocksSynchedTo, currentL1BlockNumber);
|
|
223
262
|
// Then we prune the current epoch if it'd reorg on next submission.
|
|
224
|
-
// Note that we don't do this before retrieving
|
|
225
|
-
//
|
|
263
|
+
// Note that we don't do this before retrieving checkpoints because we may need to retrieve
|
|
264
|
+
// checkpoints from more than 2 epochs ago, so we want to make sure we have the latest view of
|
|
226
265
|
// the chain locally before we start unwinding stuff. This can be optimized by figuring out
|
|
227
|
-
// up to which point we're pruning, and then requesting
|
|
228
|
-
const { rollupCanPrune } = await this.handleEpochPrune(rollupStatus.
|
|
229
|
-
//
|
|
266
|
+
// up to which point we're pruning, and then requesting checkpoints up to that point only.
|
|
267
|
+
const { rollupCanPrune } = await this.handleEpochPrune(rollupStatus.provenCheckpointNumber, currentL1BlockNumber, currentL1Timestamp);
|
|
268
|
+
// If the last checkpoint we processed had an invalid attestation, we manually advance the L1 syncpoint
|
|
269
|
+
// past it, since otherwise we'll keep downloading it and reprocessing it on every iteration until
|
|
270
|
+
// we get a valid checkpoint to advance the syncpoint.
|
|
271
|
+
if (!rollupStatus.validationResult?.valid && rollupStatus.lastL1BlockWithCheckpoint !== undefined) {
|
|
272
|
+
await this.store.setBlockSynchedL1BlockNumber(rollupStatus.lastL1BlockWithCheckpoint);
|
|
273
|
+
}
|
|
274
|
+
// And lastly we check if we are missing any checkpoints behind us due to a possible L1 reorg.
|
|
230
275
|
// We only do this if rollup cant prune on the next submission. Otherwise we will end up
|
|
231
|
-
// re-syncing the
|
|
276
|
+
// re-syncing the checkpoints we have just unwound above. We also dont do this if the last checkpoint is invalid,
|
|
232
277
|
// since the archiver will rightfully refuse to sync up to it.
|
|
233
278
|
if (!rollupCanPrune && rollupStatus.validationResult?.valid) {
|
|
234
|
-
await this.
|
|
279
|
+
await this.checkForNewCheckpointsBeforeL1SyncPoint(rollupStatus, blocksSynchedTo, currentL1BlockNumber);
|
|
235
280
|
}
|
|
236
281
|
this.instrumentation.updateL1BlockHeight(currentL1BlockNumber);
|
|
237
282
|
}
|
|
@@ -240,13 +285,17 @@ function mapArchiverConfig(config) {
|
|
|
240
285
|
// but the corresponding blocks have not been processed (see #12631).
|
|
241
286
|
this.l1Timestamp = currentL1Timestamp;
|
|
242
287
|
this.l1BlockNumber = currentL1BlockNumber;
|
|
243
|
-
|
|
244
|
-
if
|
|
245
|
-
|
|
288
|
+
// We resolve the initial sync only once we've caught up with the latest L1 block number (with 1 block grace)
|
|
289
|
+
// so if the initial sync took too long, we still go for another iteration.
|
|
290
|
+
if (!this.initialSyncComplete && currentL1BlockNumber + 1n >= await this.publicClient.getBlockNumber()) {
|
|
291
|
+
this.log.info(`Initial archiver sync to L1 block ${currentL1BlockNumber} complete`, {
|
|
246
292
|
l1BlockNumber: currentL1BlockNumber,
|
|
247
293
|
syncPoint: await this.store.getSynchPoint(),
|
|
248
294
|
...await this.getL2Tips()
|
|
249
295
|
});
|
|
296
|
+
this.runningPromise.setPollingIntervalMS(this.config.pollingIntervalMs);
|
|
297
|
+
this.initialSyncComplete = true;
|
|
298
|
+
this.initialSyncPromise.resolve();
|
|
250
299
|
}
|
|
251
300
|
}
|
|
252
301
|
/** Queries the rollup contract on whether a prune can be executed on the immediate next L1 block. */ async canPrune(currentL1BlockNumber, currentL1Timestamp) {
|
|
@@ -263,30 +312,30 @@ function mapArchiverConfig(config) {
|
|
|
263
312
|
}
|
|
264
313
|
return result;
|
|
265
314
|
}
|
|
266
|
-
/** Checks if there'd be a reorg for the next
|
|
315
|
+
/** Checks if there'd be a reorg for the next checkpoint submission and start pruning now. */ async handleEpochPrune(provenCheckpointNumber, currentL1BlockNumber, currentL1Timestamp) {
|
|
267
316
|
const rollupCanPrune = await this.canPrune(currentL1BlockNumber, currentL1Timestamp);
|
|
268
|
-
const
|
|
269
|
-
const canPrune =
|
|
317
|
+
const localPendingCheckpointNumber = await this.getSynchedCheckpointNumber();
|
|
318
|
+
const canPrune = localPendingCheckpointNumber > provenCheckpointNumber && rollupCanPrune;
|
|
270
319
|
if (canPrune) {
|
|
271
320
|
const timer = new Timer();
|
|
272
|
-
const pruneFrom =
|
|
273
|
-
const header = await this.
|
|
321
|
+
const pruneFrom = CheckpointNumber(provenCheckpointNumber + 1);
|
|
322
|
+
const header = await this.getCheckpointHeader(pruneFrom);
|
|
274
323
|
if (header === undefined) {
|
|
275
|
-
throw new Error(`Missing
|
|
324
|
+
throw new Error(`Missing checkpoint header ${pruneFrom}`);
|
|
276
325
|
}
|
|
277
|
-
const pruneFromSlotNumber = header.
|
|
326
|
+
const pruneFromSlotNumber = header.slotNumber;
|
|
278
327
|
const pruneFromEpochNumber = getEpochAtSlot(pruneFromSlotNumber, this.l1constants);
|
|
279
|
-
const
|
|
280
|
-
const
|
|
328
|
+
const checkpointsToUnwind = localPendingCheckpointNumber - provenCheckpointNumber;
|
|
329
|
+
const checkpoints = await this.getCheckpoints(pruneFrom, checkpointsToUnwind);
|
|
281
330
|
// Emit an event for listening services to react to the chain prune
|
|
282
331
|
this.emit(L2BlockSourceEvents.L2PruneDetected, {
|
|
283
332
|
type: L2BlockSourceEvents.L2PruneDetected,
|
|
284
333
|
epochNumber: pruneFromEpochNumber,
|
|
285
|
-
blocks
|
|
334
|
+
blocks: checkpoints.flatMap((c)=>L2Block.fromCheckpoint(c))
|
|
286
335
|
});
|
|
287
|
-
this.log.debug(`L2 prune from ${
|
|
288
|
-
await this.
|
|
289
|
-
this.log.warn(`Unwound ${count(
|
|
336
|
+
this.log.debug(`L2 prune from ${provenCheckpointNumber + 1} to ${localPendingCheckpointNumber} will occur on next checkpoint submission.`);
|
|
337
|
+
await this.unwindCheckpoints(localPendingCheckpointNumber, checkpointsToUnwind);
|
|
338
|
+
this.log.warn(`Unwound ${count(checkpointsToUnwind, 'checkpoint')} from checkpoint ${localPendingCheckpointNumber} ` + `to ${provenCheckpointNumber} due to predicted reorg at L1 block ${currentL1BlockNumber}. ` + `Updated latest checkpoint is ${await this.getSynchedCheckpointNumber()}.`);
|
|
290
339
|
this.instrumentation.processPrune(timer.ms());
|
|
291
340
|
// TODO(palla/reorg): Do we need to set the block synched L1 block number here?
|
|
292
341
|
// Seems like the next iteration should handle this.
|
|
@@ -329,7 +378,7 @@ function mapArchiverConfig(config) {
|
|
|
329
378
|
});
|
|
330
379
|
// Compare message count and rolling hash. If they match, no need to retrieve anything.
|
|
331
380
|
if (remoteMessagesState.totalMessagesInserted === localMessagesInserted && remoteMessagesState.messagesRollingHash.equals(localLastMessage?.rollingHash ?? Buffer16.ZERO)) {
|
|
332
|
-
this.log.
|
|
381
|
+
this.log.trace(`No L1 to L2 messages to query between L1 blocks ${messagesSyncPoint.l1BlockNumber} and ${currentL1BlockNumber}.`);
|
|
333
382
|
return;
|
|
334
383
|
}
|
|
335
384
|
// Check if our syncpoint is still valid. If not, there was an L1 reorg and we need to re-retrieve messages.
|
|
@@ -378,7 +427,7 @@ function mapArchiverConfig(config) {
|
|
|
378
427
|
}while (searchEndBlock < currentL1BlockNumber)
|
|
379
428
|
// Log stats for messages retrieved (if any).
|
|
380
429
|
if (messageCount > 0) {
|
|
381
|
-
this.log.info(`Retrieved ${messageCount} new L1 to L2 messages up to message with index ${lastMessage?.index} for
|
|
430
|
+
this.log.info(`Retrieved ${messageCount} new L1 to L2 messages up to message with index ${lastMessage?.index} for checkpoint ${lastMessage?.checkpointNumber}`, {
|
|
382
431
|
lastMessage,
|
|
383
432
|
messageCount
|
|
384
433
|
});
|
|
@@ -455,162 +504,173 @@ function mapArchiverConfig(config) {
|
|
|
455
504
|
}
|
|
456
505
|
return Buffer32.fromString(block.hash);
|
|
457
506
|
}
|
|
458
|
-
async
|
|
459
|
-
const
|
|
507
|
+
async handleCheckpoints(blocksSynchedTo, currentL1BlockNumber) {
|
|
508
|
+
const localPendingCheckpointNumber = await this.getSynchedCheckpointNumber();
|
|
460
509
|
const initialValidationResult = await this.store.getPendingChainValidationStatus();
|
|
461
|
-
const [
|
|
510
|
+
const [rollupProvenCheckpointNumber, provenArchive, rollupPendingCheckpointNumber, pendingArchive, archiveForLocalPendingCheckpointNumber] = await this.rollup.status(localPendingCheckpointNumber, {
|
|
462
511
|
blockNumber: currentL1BlockNumber
|
|
463
512
|
});
|
|
513
|
+
const provenCheckpointNumber = CheckpointNumber.fromBigInt(rollupProvenCheckpointNumber);
|
|
514
|
+
const pendingCheckpointNumber = CheckpointNumber.fromBigInt(rollupPendingCheckpointNumber);
|
|
464
515
|
const rollupStatus = {
|
|
465
|
-
|
|
516
|
+
provenCheckpointNumber,
|
|
466
517
|
provenArchive,
|
|
467
|
-
|
|
518
|
+
pendingCheckpointNumber,
|
|
468
519
|
pendingArchive,
|
|
469
520
|
validationResult: initialValidationResult
|
|
470
521
|
};
|
|
471
522
|
this.log.trace(`Retrieved rollup status at current L1 block ${currentL1BlockNumber}.`, {
|
|
472
|
-
|
|
523
|
+
localPendingCheckpointNumber,
|
|
473
524
|
blocksSynchedTo,
|
|
474
525
|
currentL1BlockNumber,
|
|
475
|
-
|
|
526
|
+
archiveForLocalPendingCheckpointNumber,
|
|
476
527
|
...rollupStatus
|
|
477
528
|
});
|
|
478
|
-
const
|
|
479
|
-
// Annoying edge case: if proven
|
|
480
|
-
// we need to set it to zero. This is an edge case because we dont have a
|
|
481
|
-
// so
|
|
482
|
-
if (
|
|
483
|
-
const
|
|
484
|
-
if (
|
|
485
|
-
await this.
|
|
486
|
-
this.log.info(`Rolled back proven chain to
|
|
487
|
-
|
|
529
|
+
const updateProvenCheckpoint = async ()=>{
|
|
530
|
+
// Annoying edge case: if proven checkpoint is moved back to 0 due to a reorg at the beginning of the chain,
|
|
531
|
+
// we need to set it to zero. This is an edge case because we dont have a checkpoint zero (initial checkpoint is one),
|
|
532
|
+
// so localCheckpointForDestinationProvenCheckpointNumber would not be found below.
|
|
533
|
+
if (provenCheckpointNumber === 0) {
|
|
534
|
+
const localProvenCheckpointNumber = await this.getProvenCheckpointNumber();
|
|
535
|
+
if (localProvenCheckpointNumber !== provenCheckpointNumber) {
|
|
536
|
+
await this.setProvenCheckpointNumber(provenCheckpointNumber);
|
|
537
|
+
this.log.info(`Rolled back proven chain to checkpoint ${provenCheckpointNumber}`, {
|
|
538
|
+
provenCheckpointNumber
|
|
488
539
|
});
|
|
489
540
|
}
|
|
490
541
|
}
|
|
491
|
-
const
|
|
492
|
-
// Sanity check. I've hit what seems to be a state where the proven
|
|
493
|
-
// synched
|
|
494
|
-
const synched = await this.
|
|
495
|
-
if (
|
|
496
|
-
this.log.error(`Hit local
|
|
542
|
+
const localCheckpointForDestinationProvenCheckpointNumber = await this.getCheckpoint(provenCheckpointNumber);
|
|
543
|
+
// Sanity check. I've hit what seems to be a state where the proven checkpoint is set to a value greater than the latest
|
|
544
|
+
// synched checkpoint when requesting L2Tips from the archiver. This is the only place where the proven checkpoint is set.
|
|
545
|
+
const synched = await this.getSynchedCheckpointNumber();
|
|
546
|
+
if (localCheckpointForDestinationProvenCheckpointNumber && synched < localCheckpointForDestinationProvenCheckpointNumber.number) {
|
|
547
|
+
this.log.error(`Hit local checkpoint greater than last synched checkpoint: ${localCheckpointForDestinationProvenCheckpointNumber.number} > ${synched}`);
|
|
497
548
|
}
|
|
498
|
-
this.log.trace(`Local
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
this.
|
|
504
|
-
|
|
549
|
+
this.log.trace(`Local checkpoint for remote proven checkpoint ${provenCheckpointNumber} is ${localCheckpointForDestinationProvenCheckpointNumber?.archive.root.toString() ?? 'undefined'}`);
|
|
550
|
+
const lastProvenBlockNumber = await this.getLastBlockNumberInCheckpoint(provenCheckpointNumber);
|
|
551
|
+
if (localCheckpointForDestinationProvenCheckpointNumber && provenArchive === localCheckpointForDestinationProvenCheckpointNumber.archive.root.toString()) {
|
|
552
|
+
const localProvenCheckpointNumber = await this.getProvenCheckpointNumber();
|
|
553
|
+
if (localProvenCheckpointNumber !== provenCheckpointNumber) {
|
|
554
|
+
await this.setProvenCheckpointNumber(provenCheckpointNumber);
|
|
555
|
+
this.log.info(`Updated proven chain to checkpoint ${provenCheckpointNumber}`, {
|
|
556
|
+
provenCheckpointNumber
|
|
505
557
|
});
|
|
506
|
-
const provenSlotNumber =
|
|
558
|
+
const provenSlotNumber = localCheckpointForDestinationProvenCheckpointNumber.header.slotNumber;
|
|
507
559
|
const provenEpochNumber = getEpochAtSlot(provenSlotNumber, this.l1constants);
|
|
508
560
|
this.emit(L2BlockSourceEvents.L2BlockProven, {
|
|
509
561
|
type: L2BlockSourceEvents.L2BlockProven,
|
|
510
|
-
blockNumber:
|
|
562
|
+
blockNumber: lastProvenBlockNumber,
|
|
511
563
|
slotNumber: provenSlotNumber,
|
|
512
564
|
epochNumber: provenEpochNumber
|
|
513
565
|
});
|
|
514
566
|
} else {
|
|
515
|
-
this.log.trace(`Proven
|
|
567
|
+
this.log.trace(`Proven checkpoint ${provenCheckpointNumber} already stored.`);
|
|
516
568
|
}
|
|
517
569
|
}
|
|
518
|
-
this.instrumentation.updateLastProvenBlock(
|
|
570
|
+
this.instrumentation.updateLastProvenBlock(lastProvenBlockNumber);
|
|
519
571
|
};
|
|
520
|
-
// This is an edge case that we only hit if there are no proposed
|
|
521
|
-
// If we have 0
|
|
522
|
-
const
|
|
523
|
-
if (
|
|
572
|
+
// This is an edge case that we only hit if there are no proposed checkpoints.
|
|
573
|
+
// If we have 0 checkpoints locally and there are no checkpoints onchain there is nothing to do.
|
|
574
|
+
const noCheckpoints = localPendingCheckpointNumber === 0 && pendingCheckpointNumber === 0;
|
|
575
|
+
if (noCheckpoints) {
|
|
524
576
|
await this.store.setBlockSynchedL1BlockNumber(currentL1BlockNumber);
|
|
525
|
-
this.log.debug(`No
|
|
577
|
+
this.log.debug(`No checkpoints to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}, no checkpoints on chain`);
|
|
526
578
|
return rollupStatus;
|
|
527
579
|
}
|
|
528
|
-
await
|
|
580
|
+
await updateProvenCheckpoint();
|
|
529
581
|
// Related to the L2 reorgs of the pending chain. We are only interested in actually addressing a reorg if there
|
|
530
|
-
// are any state that could be impacted by it. If we have no
|
|
531
|
-
if (
|
|
532
|
-
const
|
|
533
|
-
if (
|
|
534
|
-
throw new Error(`Missing
|
|
582
|
+
// are any state that could be impacted by it. If we have no checkpoints, there is no impact.
|
|
583
|
+
if (localPendingCheckpointNumber > 0) {
|
|
584
|
+
const localPendingCheckpoint = await this.getCheckpoint(localPendingCheckpointNumber);
|
|
585
|
+
if (localPendingCheckpoint === undefined) {
|
|
586
|
+
throw new Error(`Missing checkpoint ${localPendingCheckpointNumber}`);
|
|
535
587
|
}
|
|
536
|
-
const localPendingArchiveRoot =
|
|
537
|
-
const
|
|
538
|
-
if (
|
|
588
|
+
const localPendingArchiveRoot = localPendingCheckpoint.archive.root.toString();
|
|
589
|
+
const noCheckpointSinceLast = localPendingCheckpoint && pendingArchive === localPendingArchiveRoot;
|
|
590
|
+
if (noCheckpointSinceLast) {
|
|
539
591
|
// We believe the following line causes a problem when we encounter L1 re-orgs.
|
|
540
592
|
// Basically, by setting the synched L1 block number here, we are saying that we have
|
|
541
|
-
// processed all
|
|
593
|
+
// processed all checkpoints up to the current L1 block number and we will not attempt to retrieve logs from
|
|
542
594
|
// this block again (or any blocks before).
|
|
543
|
-
// However, in the re-org scenario, our L1 node is temporarily lying to us and we end up potentially missing
|
|
595
|
+
// However, in the re-org scenario, our L1 node is temporarily lying to us and we end up potentially missing checkpoints.
|
|
544
596
|
// We must only set this block number based on actually retrieved logs.
|
|
545
597
|
// TODO(#8621): Tackle this properly when we handle L1 Re-orgs.
|
|
546
598
|
// await this.store.setBlockSynchedL1BlockNumber(currentL1BlockNumber);
|
|
547
|
-
this.log.debug(`No
|
|
599
|
+
this.log.debug(`No checkpoints to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}`);
|
|
548
600
|
return rollupStatus;
|
|
549
601
|
}
|
|
550
|
-
const
|
|
551
|
-
if (!
|
|
552
|
-
// If our local pending
|
|
602
|
+
const localPendingCheckpointInChain = archiveForLocalPendingCheckpointNumber === localPendingArchiveRoot;
|
|
603
|
+
if (!localPendingCheckpointInChain) {
|
|
604
|
+
// If our local pending checkpoint tip is not in the chain on L1 a "prune" must have happened
|
|
553
605
|
// or the L1 have reorged.
|
|
554
606
|
// In any case, we have to figure out how far into the past the action will take us.
|
|
555
|
-
// For simplicity here, we will simply rewind until we end in a
|
|
556
|
-
this.log.debug(`L2 prune has been detected due to local pending
|
|
557
|
-
|
|
607
|
+
// For simplicity here, we will simply rewind until we end in a checkpoint that is also on the chain on L1.
|
|
608
|
+
this.log.debug(`L2 prune has been detected due to local pending checkpoint ${localPendingCheckpointNumber} not in chain`, {
|
|
609
|
+
localPendingCheckpointNumber,
|
|
558
610
|
localPendingArchiveRoot,
|
|
559
|
-
|
|
611
|
+
archiveForLocalPendingCheckpointNumber
|
|
560
612
|
});
|
|
561
|
-
let tipAfterUnwind =
|
|
613
|
+
let tipAfterUnwind = localPendingCheckpointNumber;
|
|
562
614
|
while(true){
|
|
563
|
-
const
|
|
564
|
-
if (
|
|
615
|
+
const candidateCheckpoint = await this.getCheckpoint(tipAfterUnwind);
|
|
616
|
+
if (candidateCheckpoint === undefined) {
|
|
565
617
|
break;
|
|
566
618
|
}
|
|
567
|
-
const archiveAtContract = await this.rollup.archiveAt(
|
|
568
|
-
|
|
619
|
+
const archiveAtContract = await this.rollup.archiveAt(candidateCheckpoint.number);
|
|
620
|
+
this.log.trace(`Checking local checkpoint ${candidateCheckpoint.number} with archive ${candidateCheckpoint.archive.root}`, {
|
|
621
|
+
archiveAtContract,
|
|
622
|
+
archiveLocal: candidateCheckpoint.archive.root.toString()
|
|
623
|
+
});
|
|
624
|
+
if (archiveAtContract === candidateCheckpoint.archive.root.toString()) {
|
|
569
625
|
break;
|
|
570
626
|
}
|
|
571
627
|
tipAfterUnwind--;
|
|
572
628
|
}
|
|
573
|
-
const
|
|
574
|
-
await this.
|
|
575
|
-
this.log.warn(`Unwound ${count(
|
|
629
|
+
const checkpointsToUnwind = localPendingCheckpointNumber - tipAfterUnwind;
|
|
630
|
+
await this.unwindCheckpoints(localPendingCheckpointNumber, checkpointsToUnwind);
|
|
631
|
+
this.log.warn(`Unwound ${count(checkpointsToUnwind, 'checkpoint')} from checkpoint ${localPendingCheckpointNumber} ` + `due to mismatched checkpoint hashes at L1 block ${currentL1BlockNumber}. ` + `Updated L2 latest checkpoint is ${await this.getSynchedCheckpointNumber()}.`);
|
|
576
632
|
}
|
|
577
633
|
}
|
|
578
|
-
// Retrieve
|
|
634
|
+
// Retrieve checkpoints in batches. Each batch is estimated to accommodate up to 'blockBatchSize' L1 blocks,
|
|
579
635
|
// computed using the L2 block time vs the L1 block time.
|
|
580
636
|
let searchStartBlock = blocksSynchedTo;
|
|
581
637
|
let searchEndBlock = blocksSynchedTo;
|
|
582
|
-
let
|
|
638
|
+
let lastRetrievedCheckpoint;
|
|
639
|
+
let lastL1BlockWithCheckpoint = undefined;
|
|
583
640
|
do {
|
|
584
641
|
[searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, currentL1BlockNumber);
|
|
585
|
-
this.log.trace(`Retrieving
|
|
642
|
+
this.log.trace(`Retrieving checkpoints from L1 block ${searchStartBlock} to ${searchEndBlock}`);
|
|
586
643
|
// TODO(md): Retrieve from blob sink then from consensus client, then from peers
|
|
587
|
-
const
|
|
588
|
-
if (
|
|
644
|
+
const retrievedCheckpoints = await retrieveCheckpointsFromRollup(this.rollup.getContract(), this.publicClient, this.debugClient, this.blobSinkClient, searchStartBlock, searchEndBlock, this.l1Addresses, this.instrumentation, this.log);
|
|
645
|
+
if (retrievedCheckpoints.length === 0) {
|
|
589
646
|
// We are not calling `setBlockSynchedL1BlockNumber` because it may cause sync issues if based off infura.
|
|
590
647
|
// See further details in earlier comments.
|
|
591
|
-
this.log.trace(`Retrieved no new
|
|
648
|
+
this.log.trace(`Retrieved no new checkpoints from L1 block ${searchStartBlock} to ${searchEndBlock}`);
|
|
592
649
|
continue;
|
|
593
650
|
}
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
651
|
+
this.log.debug(`Retrieved ${retrievedCheckpoints.length} new checkpoints between L1 blocks ${searchStartBlock} and ${searchEndBlock}`, {
|
|
652
|
+
lastProcessedCheckpoint: retrievedCheckpoints[retrievedCheckpoints.length - 1].l1,
|
|
653
|
+
searchStartBlock,
|
|
654
|
+
searchEndBlock
|
|
655
|
+
});
|
|
656
|
+
const publishedCheckpoints = await Promise.all(retrievedCheckpoints.map((b)=>retrievedToPublishedCheckpoint(b)));
|
|
657
|
+
const validCheckpoints = [];
|
|
658
|
+
for (const published of publishedCheckpoints){
|
|
599
659
|
const validationResult = this.config.skipValidateBlockAttestations ? {
|
|
600
660
|
valid: true
|
|
601
|
-
} : await
|
|
602
|
-
// Only update the validation result if it has changed, so we can keep track of the first invalid
|
|
603
|
-
// in case there is a sequence of more than one invalid
|
|
604
|
-
// There is an exception though: if
|
|
661
|
+
} : await validateCheckpointAttestations(published, this.epochCache, this.l1constants, this.log);
|
|
662
|
+
// Only update the validation result if it has changed, so we can keep track of the first invalid checkpoint
|
|
663
|
+
// in case there is a sequence of more than one invalid checkpoint, as we need to invalidate the first one.
|
|
664
|
+
// There is an exception though: if a checkpoint is invalidated and replaced with another invalid checkpoint,
|
|
605
665
|
// we need to update the validation result, since we need to be able to invalidate the new one.
|
|
606
|
-
// See test 'chain progresses if an invalid
|
|
666
|
+
// See test 'chain progresses if an invalid checkpoint is invalidated with an invalid one' for more info.
|
|
607
667
|
if (rollupStatus.validationResult?.valid !== validationResult.valid || !rollupStatus.validationResult.valid && !validationResult.valid && rollupStatus.validationResult.block.blockNumber === validationResult.block.blockNumber) {
|
|
608
668
|
rollupStatus.validationResult = validationResult;
|
|
609
669
|
}
|
|
610
670
|
if (!validationResult.valid) {
|
|
611
|
-
this.log.warn(`Skipping
|
|
612
|
-
|
|
613
|
-
l1BlockNumber:
|
|
671
|
+
this.log.warn(`Skipping checkpoint ${published.checkpoint.number} due to invalid attestations`, {
|
|
672
|
+
checkpointHash: published.checkpoint.hash(),
|
|
673
|
+
l1BlockNumber: published.l1.blockNumber,
|
|
614
674
|
...pick(validationResult, 'reason')
|
|
615
675
|
});
|
|
616
676
|
// Emit event for invalid block detection
|
|
@@ -620,22 +680,38 @@ function mapArchiverConfig(config) {
|
|
|
620
680
|
});
|
|
621
681
|
continue;
|
|
622
682
|
}
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
683
|
+
// Check the inHash of the checkpoint against the l1->l2 messages.
|
|
684
|
+
// The messages should've been synced up to the currentL1BlockNumber and must be available for the published
|
|
685
|
+
// checkpoints we just retrieved.
|
|
686
|
+
const l1ToL2Messages = await this.getL1ToL2Messages(published.checkpoint.number);
|
|
687
|
+
const computedInHash = computeInHashFromL1ToL2Messages(l1ToL2Messages);
|
|
688
|
+
const publishedInHash = published.checkpoint.header.contentCommitment.inHash;
|
|
689
|
+
if (!computedInHash.equals(publishedInHash)) {
|
|
690
|
+
this.log.fatal(`Mismatch inHash for checkpoint ${published.checkpoint.number}`, {
|
|
691
|
+
checkpointHash: published.checkpoint.hash(),
|
|
692
|
+
l1BlockNumber: published.l1.blockNumber,
|
|
693
|
+
computedInHash,
|
|
694
|
+
publishedInHash
|
|
695
|
+
});
|
|
696
|
+
// Throwing an error since this is most likely caused by a bug.
|
|
697
|
+
throw new Error(`Mismatch inHash for checkpoint ${published.checkpoint.number}. Expected ${computedInHash} but got ${publishedInHash}`);
|
|
698
|
+
}
|
|
699
|
+
validCheckpoints.push(published);
|
|
700
|
+
this.log.debug(`Ingesting new checkpoint ${published.checkpoint.number} with ${published.checkpoint.blocks.length} blocks`, {
|
|
701
|
+
checkpointHash: published.checkpoint.hash(),
|
|
702
|
+
l1BlockNumber: published.l1.blockNumber,
|
|
703
|
+
...published.checkpoint.header.toInspect(),
|
|
704
|
+
blocks: published.checkpoint.blocks.map((b)=>b.getStats())
|
|
629
705
|
});
|
|
630
706
|
}
|
|
631
707
|
try {
|
|
632
708
|
const updatedValidationResult = rollupStatus.validationResult === initialValidationResult ? undefined : rollupStatus.validationResult;
|
|
633
|
-
const [processDuration] = await elapsed(()=>this.
|
|
634
|
-
this.instrumentation.processNewBlocks(processDuration /
|
|
709
|
+
const [processDuration] = await elapsed(()=>this.addCheckpoints(validCheckpoints, updatedValidationResult));
|
|
710
|
+
this.instrumentation.processNewBlocks(processDuration / validCheckpoints.length, validCheckpoints.flatMap((c)=>c.checkpoint.blocks));
|
|
635
711
|
} catch (err) {
|
|
636
712
|
if (err instanceof InitialBlockNumberNotSequentialError) {
|
|
637
713
|
const { previousBlockNumber, newBlockNumber } = err;
|
|
638
|
-
const previousBlock = previousBlockNumber ? await this.store.getPublishedBlock(previousBlockNumber) : undefined;
|
|
714
|
+
const previousBlock = previousBlockNumber ? await this.store.getPublishedBlock(BlockNumber(previousBlockNumber)) : undefined;
|
|
639
715
|
const updatedL1SyncPoint = previousBlock?.l1.blockNumber ?? this.l1constants.l1StartBlock;
|
|
640
716
|
await this.store.setBlockSynchedL1BlockNumber(updatedL1SyncPoint);
|
|
641
717
|
this.log.warn(`Attempting to insert block ${newBlockNumber} with previous block ${previousBlockNumber}. Rolling back L1 sync point to ${updatedL1SyncPoint} to try and fetch the missing blocks.`, {
|
|
@@ -647,58 +723,58 @@ function mapArchiverConfig(config) {
|
|
|
647
723
|
}
|
|
648
724
|
throw err;
|
|
649
725
|
}
|
|
650
|
-
for (const
|
|
651
|
-
this.log.info(`Downloaded
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
726
|
+
for (const checkpoint of validCheckpoints){
|
|
727
|
+
this.log.info(`Downloaded checkpoint ${checkpoint.checkpoint.number}`, {
|
|
728
|
+
checkpointHash: checkpoint.checkpoint.hash(),
|
|
729
|
+
checkpointNumber: checkpoint.checkpoint.number,
|
|
730
|
+
blockCount: checkpoint.checkpoint.blocks.length,
|
|
731
|
+
txCount: checkpoint.checkpoint.blocks.reduce((acc, b)=>acc + b.body.txEffects.length, 0),
|
|
732
|
+
header: checkpoint.checkpoint.header.toInspect(),
|
|
733
|
+
archiveRoot: checkpoint.checkpoint.archive.root.toString(),
|
|
734
|
+
archiveNextLeafIndex: checkpoint.checkpoint.archive.nextAvailableLeafIndex
|
|
658
735
|
});
|
|
659
736
|
}
|
|
660
|
-
|
|
737
|
+
lastRetrievedCheckpoint = validCheckpoints.at(-1) ?? lastRetrievedCheckpoint;
|
|
738
|
+
lastL1BlockWithCheckpoint = retrievedCheckpoints.at(-1)?.l1.blockNumber ?? lastL1BlockWithCheckpoint;
|
|
661
739
|
}while (searchEndBlock < currentL1BlockNumber)
|
|
662
740
|
// Important that we update AFTER inserting the blocks.
|
|
663
|
-
await
|
|
741
|
+
await updateProvenCheckpoint();
|
|
664
742
|
return {
|
|
665
743
|
...rollupStatus,
|
|
666
|
-
|
|
744
|
+
lastRetrievedCheckpoint,
|
|
745
|
+
lastL1BlockWithCheckpoint
|
|
667
746
|
};
|
|
668
747
|
}
|
|
669
|
-
async
|
|
670
|
-
const {
|
|
671
|
-
// Compare the last
|
|
748
|
+
async checkForNewCheckpointsBeforeL1SyncPoint(status, blocksSynchedTo, currentL1BlockNumber) {
|
|
749
|
+
const { lastRetrievedCheckpoint, pendingCheckpointNumber } = status;
|
|
750
|
+
// Compare the last checkpoint we have (either retrieved in this round or loaded from store) with what the
|
|
672
751
|
// rollup contract told us was the latest one (pinned at the currentL1BlockNumber).
|
|
673
|
-
const
|
|
674
|
-
if (
|
|
752
|
+
const latestLocalCheckpointNumber = lastRetrievedCheckpoint?.checkpoint.number ?? await this.getSynchedCheckpointNumber();
|
|
753
|
+
if (latestLocalCheckpointNumber < pendingCheckpointNumber) {
|
|
675
754
|
// Here we have consumed all logs until the `currentL1Block` we pinned at the beginning of the archiver loop,
|
|
676
|
-
// but still
|
|
677
|
-
// We suspect an L1 reorg that added
|
|
678
|
-
// last
|
|
679
|
-
// don't have one, we go back 2 L1 epochs, which is the deepest possible reorg (assuming Casper is working).
|
|
680
|
-
const
|
|
681
|
-
const targetL1BlockNumber =
|
|
682
|
-
const
|
|
683
|
-
this.log.warn(`Failed to reach
|
|
684
|
-
|
|
685
|
-
|
|
755
|
+
// but still haven't reached the pending checkpoint according to the call to the rollup contract.
|
|
756
|
+
// We suspect an L1 reorg that added checkpoints *behind* us. If that is the case, it must have happened between
|
|
757
|
+
// the last checkpoint we saw and the current one, so we reset the last synched L1 block number. In the edge case
|
|
758
|
+
// we don't have one, we go back 2 L1 epochs, which is the deepest possible reorg (assuming Casper is working).
|
|
759
|
+
const latestLocalCheckpoint = lastRetrievedCheckpoint ?? (latestLocalCheckpointNumber > 0 ? await this.getPublishedCheckpoints(latestLocalCheckpointNumber, 1).then(([c])=>c) : undefined);
|
|
760
|
+
const targetL1BlockNumber = latestLocalCheckpoint?.l1.blockNumber ?? maxBigint(currentL1BlockNumber - 64n, 0n);
|
|
761
|
+
const latestLocalCheckpointArchive = latestLocalCheckpoint?.checkpoint.archive.root.toString();
|
|
762
|
+
this.log.warn(`Failed to reach checkpoint ${pendingCheckpointNumber} at ${currentL1BlockNumber} (latest is ${latestLocalCheckpointNumber}). ` + `Rolling back last synched L1 block number to ${targetL1BlockNumber}.`, {
|
|
763
|
+
latestLocalCheckpointNumber,
|
|
764
|
+
latestLocalCheckpointArchive,
|
|
686
765
|
blocksSynchedTo,
|
|
687
766
|
currentL1BlockNumber,
|
|
688
767
|
...status
|
|
689
768
|
});
|
|
690
769
|
await this.store.setBlockSynchedL1BlockNumber(targetL1BlockNumber);
|
|
691
770
|
} else {
|
|
692
|
-
this.log.trace(`No new
|
|
693
|
-
|
|
694
|
-
|
|
771
|
+
this.log.trace(`No new checkpoints behind L1 sync point to retrieve.`, {
|
|
772
|
+
latestLocalCheckpointNumber,
|
|
773
|
+
pendingCheckpointNumber
|
|
695
774
|
});
|
|
696
775
|
}
|
|
697
776
|
}
|
|
698
777
|
/** Resumes the archiver after a stop. */ resume() {
|
|
699
|
-
if (!this.runningPromise) {
|
|
700
|
-
throw new Error(`Archiver was never started`);
|
|
701
|
-
}
|
|
702
778
|
if (this.runningPromise.isRunning()) {
|
|
703
779
|
this.log.warn(`Archiver already running`);
|
|
704
780
|
}
|
|
@@ -710,7 +786,7 @@ function mapArchiverConfig(config) {
|
|
|
710
786
|
* @returns A promise signalling completion of the stop process.
|
|
711
787
|
*/ async stop() {
|
|
712
788
|
this.log.debug('Stopping...');
|
|
713
|
-
await this.runningPromise
|
|
789
|
+
await this.runningPromise.stop();
|
|
714
790
|
this.log.info('Stopped.');
|
|
715
791
|
return Promise.resolve();
|
|
716
792
|
}
|
|
@@ -732,24 +808,16 @@ function mapArchiverConfig(config) {
|
|
|
732
808
|
return Promise.resolve(this.l1Addresses.registryAddress);
|
|
733
809
|
}
|
|
734
810
|
getL1BlockNumber() {
|
|
735
|
-
|
|
736
|
-
if (!l1BlockNumber) {
|
|
737
|
-
throw new Error('L1 block number not yet available. Complete an initial sync first.');
|
|
738
|
-
}
|
|
739
|
-
return l1BlockNumber;
|
|
811
|
+
return this.l1BlockNumber;
|
|
740
812
|
}
|
|
741
813
|
getL1Timestamp() {
|
|
742
|
-
|
|
743
|
-
if (!l1Timestamp) {
|
|
744
|
-
throw new Error('L1 timestamp not yet available. Complete an initial sync first.');
|
|
745
|
-
}
|
|
746
|
-
return Promise.resolve(l1Timestamp);
|
|
814
|
+
return Promise.resolve(this.l1Timestamp);
|
|
747
815
|
}
|
|
748
|
-
|
|
749
|
-
return getSlotAtTimestamp(
|
|
816
|
+
getL2SlotNumber() {
|
|
817
|
+
return Promise.resolve(this.l1Timestamp === undefined ? undefined : getSlotAtTimestamp(this.l1Timestamp, this.l1constants));
|
|
750
818
|
}
|
|
751
|
-
|
|
752
|
-
return getEpochNumberAtTimestamp(
|
|
819
|
+
getL2EpochNumber() {
|
|
820
|
+
return Promise.resolve(this.l1Timestamp === undefined ? undefined : getEpochNumberAtTimestamp(this.l1Timestamp, this.l1constants));
|
|
753
821
|
}
|
|
754
822
|
async getBlocksForEpoch(epochNumber) {
|
|
755
823
|
const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1constants);
|
|
@@ -757,12 +825,12 @@ function mapArchiverConfig(config) {
|
|
|
757
825
|
// Walk the list of blocks backwards and filter by slots matching the requested epoch.
|
|
758
826
|
// We'll typically ask for blocks for a very recent epoch, so we shouldn't need an index here.
|
|
759
827
|
let block = await this.getBlock(await this.store.getSynchedL2BlockNumber());
|
|
760
|
-
const slot = (b)=>b.header.globalVariables.slotNumber
|
|
828
|
+
const slot = (b)=>b.header.globalVariables.slotNumber;
|
|
761
829
|
while(block && slot(block) >= start){
|
|
762
830
|
if (slot(block) <= end) {
|
|
763
831
|
blocks.push(block);
|
|
764
832
|
}
|
|
765
|
-
block = await this.getBlock(block.number - 1);
|
|
833
|
+
block = await this.getBlock(BlockNumber(block.number - 1));
|
|
766
834
|
}
|
|
767
835
|
return blocks.reverse();
|
|
768
836
|
}
|
|
@@ -773,19 +841,20 @@ function mapArchiverConfig(config) {
|
|
|
773
841
|
// We'll typically ask for blocks for a very recent epoch, so we shouldn't need an index here.
|
|
774
842
|
let number = await this.store.getSynchedL2BlockNumber();
|
|
775
843
|
let header = await this.getBlockHeader(number);
|
|
776
|
-
const slot = (b)=>b.globalVariables.slotNumber
|
|
844
|
+
const slot = (b)=>b.globalVariables.slotNumber;
|
|
777
845
|
while(header && slot(header) >= start){
|
|
778
846
|
if (slot(header) <= end) {
|
|
779
847
|
blocks.push(header);
|
|
780
848
|
}
|
|
781
|
-
|
|
849
|
+
number = BlockNumber(number - 1);
|
|
850
|
+
header = await this.getBlockHeader(number);
|
|
782
851
|
}
|
|
783
852
|
return blocks.reverse();
|
|
784
853
|
}
|
|
785
854
|
async isEpochComplete(epochNumber) {
|
|
786
855
|
// The epoch is complete if the current L2 block is the last one in the epoch (or later)
|
|
787
856
|
const header = await this.getBlockHeader('latest');
|
|
788
|
-
const slot = header
|
|
857
|
+
const slot = header ? header.globalVariables.slotNumber : undefined;
|
|
789
858
|
const [_startSlot, endSlot] = getSlotRangeForEpoch(epochNumber, this.l1constants);
|
|
790
859
|
if (slot && slot >= endSlot) {
|
|
791
860
|
return true;
|
|
@@ -809,6 +878,78 @@ function mapArchiverConfig(config) {
|
|
|
809
878
|
/** Returns whether the archiver has completed an initial sync run successfully. */ isInitialSyncComplete() {
|
|
810
879
|
return this.initialSyncComplete;
|
|
811
880
|
}
|
|
881
|
+
async getPublishedCheckpoints(from, limit, proven) {
|
|
882
|
+
// TODO: Implement this properly. This only works when we have one block per checkpoint.
|
|
883
|
+
const blocks = await this.getPublishedBlocks(BlockNumber(from), limit, proven);
|
|
884
|
+
return blocks.map((b)=>b.toPublishedCheckpoint());
|
|
885
|
+
}
|
|
886
|
+
async getCheckpointByArchive(archive) {
|
|
887
|
+
// TODO: Implement this properly. This only works when we have one block per checkpoint.
|
|
888
|
+
return (await this.getPublishedBlockByArchive(archive))?.block.toCheckpoint();
|
|
889
|
+
}
|
|
890
|
+
async getCheckpoints(from, limit, proven) {
|
|
891
|
+
const published = await this.getPublishedCheckpoints(from, limit, proven);
|
|
892
|
+
return published.map((p)=>p.checkpoint);
|
|
893
|
+
}
|
|
894
|
+
async getCheckpoint(number) {
|
|
895
|
+
if (number < 0) {
|
|
896
|
+
number = await this.getSynchedCheckpointNumber();
|
|
897
|
+
}
|
|
898
|
+
if (number === 0) {
|
|
899
|
+
return undefined;
|
|
900
|
+
}
|
|
901
|
+
const published = await this.getPublishedCheckpoints(number, 1);
|
|
902
|
+
return published[0]?.checkpoint;
|
|
903
|
+
}
|
|
904
|
+
async getCheckpointHeader(number) {
|
|
905
|
+
if (number === 'latest') {
|
|
906
|
+
number = await this.getSynchedCheckpointNumber();
|
|
907
|
+
}
|
|
908
|
+
if (number === 0) {
|
|
909
|
+
return undefined;
|
|
910
|
+
}
|
|
911
|
+
const checkpoint = await this.getCheckpoint(number);
|
|
912
|
+
return checkpoint?.header;
|
|
913
|
+
}
|
|
914
|
+
getCheckpointNumber() {
|
|
915
|
+
return this.getSynchedCheckpointNumber();
|
|
916
|
+
}
|
|
917
|
+
async getSynchedCheckpointNumber() {
|
|
918
|
+
// TODO: Create store and apis for checkpoints.
|
|
919
|
+
// Checkpoint number will no longer be the same as the block number once we support multiple blocks per checkpoint.
|
|
920
|
+
return CheckpointNumber(await this.store.getSynchedL2BlockNumber());
|
|
921
|
+
}
|
|
922
|
+
async getProvenCheckpointNumber() {
|
|
923
|
+
// TODO: Create store and apis for checkpoints.
|
|
924
|
+
// Proven checkpoint number will no longer be the same as the proven block number once we support multiple blocks per checkpoint.
|
|
925
|
+
return CheckpointNumber(await this.store.getProvenL2BlockNumber());
|
|
926
|
+
}
|
|
927
|
+
setProvenCheckpointNumber(checkpointNumber) {
|
|
928
|
+
// TODO: Create store and apis for checkpoints.
|
|
929
|
+
// Proven checkpoint number will no longer be the same as the proven block number once we support multiple blocks per checkpoint.
|
|
930
|
+
return this.store.setProvenL2BlockNumber(BlockNumber.fromCheckpointNumber(checkpointNumber));
|
|
931
|
+
}
|
|
932
|
+
unwindCheckpoints(from, checkpointsToUnwind) {
|
|
933
|
+
// TODO: Create store and apis for checkpoints.
|
|
934
|
+
// This only works when we have one block per checkpoint.
|
|
935
|
+
return this.store.unwindBlocks(BlockNumber.fromCheckpointNumber(from), checkpointsToUnwind);
|
|
936
|
+
}
|
|
937
|
+
getLastBlockNumberInCheckpoint(checkpointNumber) {
|
|
938
|
+
// TODO: Create store and apis for checkpoints.
|
|
939
|
+
// Checkpoint number will no longer be the same as the block number once we support multiple blocks per checkpoint.
|
|
940
|
+
return Promise.resolve(BlockNumber.fromCheckpointNumber(checkpointNumber));
|
|
941
|
+
}
|
|
942
|
+
addCheckpoints(checkpoints, pendingChainValidationStatus) {
|
|
943
|
+
// TODO: Create store and apis for checkpoints.
|
|
944
|
+
// This only works when we have one block per checkpoint.
|
|
945
|
+
return this.store.addBlocks(checkpoints.map((p)=>PublishedL2Block.fromPublishedCheckpoint(p)), pendingChainValidationStatus);
|
|
946
|
+
}
|
|
947
|
+
async getCheckpointsForEpoch(epochNumber) {
|
|
948
|
+
// TODO: Create store and apis for checkpoints.
|
|
949
|
+
// This only works when we have one block per checkpoint.
|
|
950
|
+
const blocks = await this.getBlocksForEpoch(epochNumber);
|
|
951
|
+
return blocks.map((b)=>b.toCheckpoint());
|
|
952
|
+
}
|
|
812
953
|
/**
|
|
813
954
|
* Gets up to `limit` amount of L2 blocks starting from `from`.
|
|
814
955
|
* @param from - Number of the first block to return (inclusive).
|
|
@@ -866,14 +1007,6 @@ function mapArchiverConfig(config) {
|
|
|
866
1007
|
return this.store.getSettledTxReceipt(txHash);
|
|
867
1008
|
}
|
|
868
1009
|
/**
|
|
869
|
-
* Retrieves all private logs from up to `limit` blocks, starting from the block number `from`.
|
|
870
|
-
* @param from - The block number from which to begin retrieving logs.
|
|
871
|
-
* @param limit - The maximum number of blocks to retrieve logs from.
|
|
872
|
-
* @returns An array of private logs from the specified range of blocks.
|
|
873
|
-
*/ getPrivateLogs(from, limit) {
|
|
874
|
-
return this.store.getPrivateLogs(from, limit);
|
|
875
|
-
}
|
|
876
|
-
/**
|
|
877
1010
|
* Gets all logs that match any of the received tags (i.e. logs with their first field equal to a tag).
|
|
878
1011
|
* @param tags - The tags to filter the logs by.
|
|
879
1012
|
* @returns For each received tag, an array of matching logs is returned. An empty array implies no logs match
|
|
@@ -925,11 +1058,11 @@ function mapArchiverConfig(config) {
|
|
|
925
1058
|
return this.store.getContractInstance(address, timestamp);
|
|
926
1059
|
}
|
|
927
1060
|
/**
|
|
928
|
-
* Gets L1 to L2 message (to be) included in a given
|
|
929
|
-
* @param
|
|
1061
|
+
* Gets L1 to L2 message (to be) included in a given checkpoint.
|
|
1062
|
+
* @param checkpointNumber - Checkpoint number to get messages for.
|
|
930
1063
|
* @returns The L1 to L2 messages/leaves of the messages subtree (throws if not found).
|
|
931
|
-
*/ getL1ToL2Messages(
|
|
932
|
-
return this.store.getL1ToL2Messages(
|
|
1064
|
+
*/ getL1ToL2Messages(checkpointNumber) {
|
|
1065
|
+
return this.store.getL1ToL2Messages(checkpointNumber);
|
|
933
1066
|
}
|
|
934
1067
|
/**
|
|
935
1068
|
* Gets the L1 to L2 message index in the L1 to L2 message tree.
|
|
@@ -963,7 +1096,7 @@ function mapArchiverConfig(config) {
|
|
|
963
1096
|
// TODO(#13569): Compute proper finalized block number based on L1 finalized block.
|
|
964
1097
|
// We just force it 2 epochs worth of proven data for now.
|
|
965
1098
|
// NOTE: update end-to-end/src/e2e_epochs/epochs_empty_blocks.test.ts as that uses finalized blocks in computations
|
|
966
|
-
const finalizedBlockNumber = Math.max(provenBlockNumber - this.l1constants.epochDuration * 2, 0);
|
|
1099
|
+
const finalizedBlockNumber = BlockNumber(Math.max(provenBlockNumber - this.l1constants.epochDuration * 2, 0));
|
|
967
1100
|
const [latestBlockHeader, provenBlockHeader, finalizedBlockHeader] = await Promise.all([
|
|
968
1101
|
latestBlockNumber > 0 ? this.getBlockHeader(latestBlockNumber) : undefined,
|
|
969
1102
|
provenBlockNumber > 0 ? this.getBlockHeader(provenBlockNumber) : undefined,
|
|
@@ -978,21 +1111,21 @@ function mapArchiverConfig(config) {
|
|
|
978
1111
|
if (finalizedBlockNumber > 0 && !finalizedBlockHeader) {
|
|
979
1112
|
throw new Error(`Failed to retrieve finalized block header for block ${finalizedBlockNumber} (latest block is ${latestBlockNumber})`);
|
|
980
1113
|
}
|
|
981
|
-
const latestBlockHeaderHash = await latestBlockHeader?.hash();
|
|
982
|
-
const provenBlockHeaderHash = await provenBlockHeader?.hash();
|
|
983
|
-
const finalizedBlockHeaderHash = await finalizedBlockHeader?.hash();
|
|
1114
|
+
const latestBlockHeaderHash = await latestBlockHeader?.hash() ?? GENESIS_BLOCK_HEADER_HASH;
|
|
1115
|
+
const provenBlockHeaderHash = await provenBlockHeader?.hash() ?? GENESIS_BLOCK_HEADER_HASH;
|
|
1116
|
+
const finalizedBlockHeaderHash = await finalizedBlockHeader?.hash() ?? GENESIS_BLOCK_HEADER_HASH;
|
|
984
1117
|
return {
|
|
985
1118
|
latest: {
|
|
986
1119
|
number: latestBlockNumber,
|
|
987
|
-
hash: latestBlockHeaderHash
|
|
1120
|
+
hash: latestBlockHeaderHash.toString()
|
|
988
1121
|
},
|
|
989
1122
|
proven: {
|
|
990
1123
|
number: provenBlockNumber,
|
|
991
|
-
hash: provenBlockHeaderHash
|
|
1124
|
+
hash: provenBlockHeaderHash.toString()
|
|
992
1125
|
},
|
|
993
1126
|
finalized: {
|
|
994
1127
|
number: finalizedBlockNumber,
|
|
995
|
-
hash: finalizedBlockHeaderHash
|
|
1128
|
+
hash: finalizedBlockHeaderHash.toString()
|
|
996
1129
|
}
|
|
997
1130
|
};
|
|
998
1131
|
}
|
|
@@ -1010,11 +1143,12 @@ function mapArchiverConfig(config) {
|
|
|
1010
1143
|
throw new Error(`Target L2 block ${targetL2BlockNumber} not found`);
|
|
1011
1144
|
}
|
|
1012
1145
|
const targetL1BlockNumber = targetL2Block.l1.blockNumber;
|
|
1146
|
+
const targetCheckpointNumber = CheckpointNumber.fromBlockNumber(targetL2BlockNumber);
|
|
1013
1147
|
const targetL1BlockHash = await this.getL1BlockHash(targetL1BlockNumber);
|
|
1014
1148
|
this.log.info(`Unwinding ${blocksToUnwind} blocks from L2 block ${currentL2Block}`);
|
|
1015
|
-
await this.store.unwindBlocks(currentL2Block, blocksToUnwind);
|
|
1016
|
-
this.log.info(`Unwinding L1 to L2 messages to ${
|
|
1017
|
-
await this.store.
|
|
1149
|
+
await this.store.unwindBlocks(BlockNumber(currentL2Block), blocksToUnwind);
|
|
1150
|
+
this.log.info(`Unwinding L1 to L2 messages to checkpoint ${targetCheckpointNumber}`);
|
|
1151
|
+
await this.store.rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber);
|
|
1018
1152
|
this.log.info(`Setting L1 syncpoints to ${targetL1BlockNumber}`);
|
|
1019
1153
|
await this.store.setBlockSynchedL1BlockNumber(targetL1BlockNumber);
|
|
1020
1154
|
await this.store.setMessageSynchedL1Block({
|
|
@@ -1023,7 +1157,7 @@ function mapArchiverConfig(config) {
|
|
|
1023
1157
|
});
|
|
1024
1158
|
if (targetL2BlockNumber < currentProvenBlock) {
|
|
1025
1159
|
this.log.info(`Clearing proven L2 block number`);
|
|
1026
|
-
await this.store.setProvenL2BlockNumber(
|
|
1160
|
+
await this.store.setProvenL2BlockNumber(BlockNumber.ZERO);
|
|
1027
1161
|
}
|
|
1028
1162
|
// TODO(palla/reorg): Set the finalized block when we add support for it.
|
|
1029
1163
|
// if (targetL2BlockNumber < currentFinalizedBlock) {
|
|
@@ -1033,9 +1167,7 @@ function mapArchiverConfig(config) {
|
|
|
1033
1167
|
}
|
|
1034
1168
|
}
|
|
1035
1169
|
_ts_decorate([
|
|
1036
|
-
trackSpan('Archiver.sync'
|
|
1037
|
-
[Attributes.INITIAL_SYNC]: initialRun
|
|
1038
|
-
}))
|
|
1170
|
+
trackSpan('Archiver.sync')
|
|
1039
1171
|
], Archiver.prototype, "sync", null);
|
|
1040
1172
|
var Operation = /*#__PURE__*/ function(Operation) {
|
|
1041
1173
|
Operation[Operation["Store"] = 0] = "Store";
|
|
@@ -1190,7 +1322,7 @@ var Operation = /*#__PURE__*/ function(Operation) {
|
|
|
1190
1322
|
throw new Error(`Cannot unwind ${blocksToUnwind} blocks`);
|
|
1191
1323
|
}
|
|
1192
1324
|
// from - blocksToUnwind = the new head, so + 1 for what we need to remove
|
|
1193
|
-
const blocks = await this.getPublishedBlocks(from - blocksToUnwind + 1, blocksToUnwind);
|
|
1325
|
+
const blocks = await this.getPublishedBlocks(BlockNumber(from - blocksToUnwind + 1), blocksToUnwind);
|
|
1194
1326
|
const opResults = await Promise.all([
|
|
1195
1327
|
// Prune rolls back to the last proven block, which is by definition valid
|
|
1196
1328
|
this.store.setPendingChainValidationStatus({
|
|
@@ -1243,15 +1375,12 @@ var Operation = /*#__PURE__*/ function(Operation) {
|
|
|
1243
1375
|
addL1ToL2Messages(messages) {
|
|
1244
1376
|
return this.store.addL1ToL2Messages(messages);
|
|
1245
1377
|
}
|
|
1246
|
-
getL1ToL2Messages(
|
|
1247
|
-
return this.store.getL1ToL2Messages(
|
|
1378
|
+
getL1ToL2Messages(checkpointNumber) {
|
|
1379
|
+
return this.store.getL1ToL2Messages(checkpointNumber);
|
|
1248
1380
|
}
|
|
1249
1381
|
getL1ToL2MessageIndex(l1ToL2Message) {
|
|
1250
1382
|
return this.store.getL1ToL2MessageIndex(l1ToL2Message);
|
|
1251
1383
|
}
|
|
1252
|
-
getPrivateLogs(from, limit) {
|
|
1253
|
-
return this.store.getPrivateLogs(from, limit);
|
|
1254
|
-
}
|
|
1255
1384
|
getLogsByTags(tags, logsPerTag) {
|
|
1256
1385
|
return this.store.getLogsByTags(tags, logsPerTag);
|
|
1257
1386
|
}
|
|
@@ -1303,8 +1432,8 @@ var Operation = /*#__PURE__*/ function(Operation) {
|
|
|
1303
1432
|
estimateSize() {
|
|
1304
1433
|
return this.store.estimateSize();
|
|
1305
1434
|
}
|
|
1306
|
-
|
|
1307
|
-
return this.store.
|
|
1435
|
+
rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber) {
|
|
1436
|
+
return this.store.rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber);
|
|
1308
1437
|
}
|
|
1309
1438
|
iterateL1ToL2Messages(range = {}) {
|
|
1310
1439
|
return this.store.iterateL1ToL2Messages(range);
|