@aztec/archiver 3.0.0-canary.a9708bd → 3.0.0-devnet.2-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/README.md +27 -6
- package/dest/archiver/archiver.d.ts +87 -64
- package/dest/archiver/archiver.d.ts.map +1 -1
- package/dest/archiver/archiver.js +463 -278
- package/dest/archiver/archiver_store.d.ts +46 -28
- 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 +316 -143
- package/dest/archiver/config.d.ts +6 -23
- package/dest/archiver/config.d.ts.map +1 -1
- package/dest/archiver/config.js +19 -12
- 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 +14 -0
- package/dest/archiver/kv_archiver_store/block_store.d.ts +45 -9
- package/dest/archiver/kv_archiver_store/block_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/block_store.js +99 -12
- 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 +30 -30
- package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/kv_archiver_store.js +26 -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/{data_retrieval.js → l1/data_retrieval.js} +118 -154
- 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 +66 -44
- package/dest/factory.d.ts +3 -11
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +5 -17
- 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 +24 -20
- package/dest/test/mock_l2_block_source.d.ts.map +1 -1
- package/dest/test/mock_l2_block_source.js +79 -13
- 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 +610 -363
- package/src/archiver/archiver_store.ts +55 -28
- package/src/archiver/archiver_store_test_suite.ts +369 -143
- package/src/archiver/config.ts +26 -51
- package/src/archiver/instrumentation.ts +19 -2
- package/src/archiver/kv_archiver_store/block_store.ts +139 -21
- 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 +48 -33
- 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} +198 -242
- 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 +86 -32
- package/src/factory.ts +6 -26
- 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 +110 -27
- package/src/test/mock_structs.ts +10 -9
- package/dest/archiver/data_retrieval.d.ts +0 -78
- package/dest/archiver/data_retrieval.d.ts.map +0 -1
package/src/archiver/archiver.ts
CHANGED
|
@@ -1,21 +1,20 @@
|
|
|
1
1
|
import type { BlobSinkClientInterface } from '@aztec/blob-sink/client';
|
|
2
|
+
import { GENESIS_BLOCK_HEADER_HASH } from '@aztec/constants';
|
|
2
3
|
import { EpochCache } from '@aztec/epoch-cache';
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
type ViemPublicClient,
|
|
9
|
-
createEthereumChain,
|
|
10
|
-
} from '@aztec/ethereum';
|
|
4
|
+
import { createEthereumChain } from '@aztec/ethereum/chain';
|
|
5
|
+
import { BlockTagTooOldError, InboxContract, RollupContract } from '@aztec/ethereum/contracts';
|
|
6
|
+
import type { L1ContractAddresses } from '@aztec/ethereum/l1-contract-addresses';
|
|
7
|
+
import type { L1BlockId } from '@aztec/ethereum/l1-types';
|
|
8
|
+
import type { ViemPublicClient, ViemPublicDebugClient } from '@aztec/ethereum/types';
|
|
11
9
|
import { maxBigint } from '@aztec/foundation/bigint';
|
|
10
|
+
import { BlockNumber, CheckpointNumber, EpochNumber, SlotNumber } from '@aztec/foundation/branded-types';
|
|
12
11
|
import { Buffer16, Buffer32 } from '@aztec/foundation/buffer';
|
|
13
|
-
import { pick } from '@aztec/foundation/collection';
|
|
12
|
+
import { merge, pick } from '@aztec/foundation/collection';
|
|
13
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
14
14
|
import type { EthAddress } from '@aztec/foundation/eth-address';
|
|
15
|
-
import { Fr } from '@aztec/foundation/fields';
|
|
16
15
|
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
16
|
+
import { type PromiseWithResolvers, promiseWithResolvers } from '@aztec/foundation/promise';
|
|
17
17
|
import { RunningPromise, makeLoggingErrorHandler } from '@aztec/foundation/running-promise';
|
|
18
|
-
import { sleep } from '@aztec/foundation/sleep';
|
|
19
18
|
import { count } from '@aztec/foundation/string';
|
|
20
19
|
import { DateProvider, Timer, elapsed } from '@aztec/foundation/timer';
|
|
21
20
|
import type { CustomRange } from '@aztec/kv-store';
|
|
@@ -33,12 +32,13 @@ import type { FunctionSelector } from '@aztec/stdlib/abi';
|
|
|
33
32
|
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
34
33
|
import {
|
|
35
34
|
type ArchiverEmitter,
|
|
36
|
-
|
|
37
|
-
type L2BlockId,
|
|
35
|
+
L2Block,
|
|
38
36
|
type L2BlockSource,
|
|
39
37
|
L2BlockSourceEvents,
|
|
40
38
|
type L2Tips,
|
|
39
|
+
PublishedL2Block,
|
|
41
40
|
} from '@aztec/stdlib/block';
|
|
41
|
+
import type { Checkpoint, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
|
|
42
42
|
import {
|
|
43
43
|
type ContractClassPublic,
|
|
44
44
|
type ContractDataSource,
|
|
@@ -60,11 +60,11 @@ import {
|
|
|
60
60
|
import type { GetContractClassLogsResponse, GetPublicLogsResponse } from '@aztec/stdlib/interfaces/client';
|
|
61
61
|
import type { L2LogsSource } from '@aztec/stdlib/interfaces/server';
|
|
62
62
|
import { ContractClassLog, type LogFilter, type PrivateLog, type PublicLog, TxScopedL2Log } from '@aztec/stdlib/logs';
|
|
63
|
-
import type
|
|
63
|
+
import { type L1ToL2MessageSource, computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
|
|
64
|
+
import type { CheckpointHeader } from '@aztec/stdlib/rollup';
|
|
64
65
|
import { type BlockHeader, type IndexedTxEffect, TxHash, TxReceipt } from '@aztec/stdlib/tx';
|
|
65
66
|
import type { UInt64 } from '@aztec/stdlib/types';
|
|
66
67
|
import {
|
|
67
|
-
Attributes,
|
|
68
68
|
type TelemetryClient,
|
|
69
69
|
type Traceable,
|
|
70
70
|
type Tracer,
|
|
@@ -74,21 +74,21 @@ import {
|
|
|
74
74
|
|
|
75
75
|
import { EventEmitter } from 'events';
|
|
76
76
|
import groupBy from 'lodash.groupby';
|
|
77
|
-
import { type GetContractReturnType, createPublicClient, fallback, http } from 'viem';
|
|
77
|
+
import { type GetContractReturnType, type Hex, createPublicClient, fallback, http } from 'viem';
|
|
78
78
|
|
|
79
79
|
import type { ArchiverDataStore, ArchiverL1SynchPoint } from './archiver_store.js';
|
|
80
80
|
import type { ArchiverConfig } from './config.js';
|
|
81
|
+
import { InitialBlockNumberNotSequentialError, NoBlobBodiesFoundError } from './errors.js';
|
|
82
|
+
import { ArchiverInstrumentation } from './instrumentation.js';
|
|
81
83
|
import {
|
|
82
|
-
|
|
84
|
+
retrieveCheckpointsFromRollup,
|
|
83
85
|
retrieveL1ToL2Message,
|
|
84
86
|
retrieveL1ToL2Messages,
|
|
85
|
-
|
|
86
|
-
} from './data_retrieval.js';
|
|
87
|
-
import {
|
|
88
|
-
import { ArchiverInstrumentation } from './instrumentation.js';
|
|
87
|
+
retrievedToPublishedCheckpoint,
|
|
88
|
+
} from './l1/data_retrieval.js';
|
|
89
|
+
import { validateAndLogTraceAvailability } from './l1/validate_trace.js';
|
|
89
90
|
import type { InboxMessage } from './structs/inbox_message.js';
|
|
90
|
-
import type
|
|
91
|
-
import { type ValidateBlockResult, validateBlockAttestations } from './validation.js';
|
|
91
|
+
import { type ValidateBlockResult, validateCheckpointAttestations } from './validation.js';
|
|
92
92
|
|
|
93
93
|
/**
|
|
94
94
|
* Helper interface to combine all sources this archiver implementation provides.
|
|
@@ -102,16 +102,34 @@ export type ArchiverDeps = {
|
|
|
102
102
|
dateProvider?: DateProvider;
|
|
103
103
|
};
|
|
104
104
|
|
|
105
|
+
function mapArchiverConfig(config: Partial<ArchiverConfig>) {
|
|
106
|
+
return {
|
|
107
|
+
pollingIntervalMs: config.archiverPollingIntervalMS,
|
|
108
|
+
batchSize: config.archiverBatchSize,
|
|
109
|
+
skipValidateBlockAttestations: config.skipValidateBlockAttestations,
|
|
110
|
+
maxAllowedEthClientDriftSeconds: config.maxAllowedEthClientDriftSeconds,
|
|
111
|
+
ethereumAllowNoDebugHosts: config.ethereumAllowNoDebugHosts,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
type RollupStatus = {
|
|
116
|
+
provenCheckpointNumber: CheckpointNumber;
|
|
117
|
+
provenArchive: Hex;
|
|
118
|
+
pendingCheckpointNumber: CheckpointNumber;
|
|
119
|
+
pendingArchive: Hex;
|
|
120
|
+
validationResult: ValidateBlockResult | undefined;
|
|
121
|
+
lastRetrievedCheckpoint?: PublishedCheckpoint;
|
|
122
|
+
lastL1BlockWithCheckpoint?: bigint;
|
|
123
|
+
};
|
|
124
|
+
|
|
105
125
|
/**
|
|
106
|
-
* Pulls
|
|
126
|
+
* Pulls checkpoints in a non-blocking manner and provides interface for their retrieval.
|
|
107
127
|
* Responsible for handling robust L1 polling so that other components do not need to
|
|
108
128
|
* concern themselves with it.
|
|
109
129
|
*/
|
|
110
130
|
export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implements ArchiveSource, Traceable {
|
|
111
|
-
/**
|
|
112
|
-
|
|
113
|
-
*/
|
|
114
|
-
private runningPromise?: RunningPromise;
|
|
131
|
+
/** A loop in which we will be continually fetching new checkpoints. */
|
|
132
|
+
private runningPromise: RunningPromise;
|
|
115
133
|
|
|
116
134
|
private rollup: RollupContract;
|
|
117
135
|
private inbox: InboxContract;
|
|
@@ -120,14 +138,15 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
|
|
|
120
138
|
|
|
121
139
|
private l1BlockNumber: bigint | undefined;
|
|
122
140
|
private l1Timestamp: bigint | undefined;
|
|
123
|
-
private pendingChainValidationStatus: ValidateBlockResult = { valid: true };
|
|
124
141
|
private initialSyncComplete: boolean = false;
|
|
142
|
+
private initialSyncPromise: PromiseWithResolvers<void>;
|
|
125
143
|
|
|
126
144
|
public readonly tracer: Tracer;
|
|
127
145
|
|
|
128
146
|
/**
|
|
129
147
|
* Creates a new instance of the Archiver.
|
|
130
148
|
* @param publicClient - A client for interacting with the Ethereum node.
|
|
149
|
+
* @param debugClient - A client for interacting with the Ethereum node for debug/trace methods.
|
|
131
150
|
* @param rollupAddress - Ethereum address of the rollup contract.
|
|
132
151
|
* @param inboxAddress - Ethereum address of the inbox contract.
|
|
133
152
|
* @param registryAddress - Ethereum address of the registry contract.
|
|
@@ -137,13 +156,24 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
|
|
|
137
156
|
*/
|
|
138
157
|
constructor(
|
|
139
158
|
private readonly publicClient: ViemPublicClient,
|
|
140
|
-
private readonly
|
|
159
|
+
private readonly debugClient: ViemPublicDebugClient,
|
|
160
|
+
private readonly l1Addresses: Pick<
|
|
161
|
+
L1ContractAddresses,
|
|
162
|
+
'rollupAddress' | 'inboxAddress' | 'registryAddress' | 'governanceProposerAddress' | 'slashFactoryAddress'
|
|
163
|
+
> & { slashingProposerAddress: EthAddress },
|
|
141
164
|
readonly dataStore: ArchiverDataStore,
|
|
142
|
-
private
|
|
165
|
+
private config: {
|
|
166
|
+
pollingIntervalMs: number;
|
|
167
|
+
batchSize: number;
|
|
168
|
+
skipValidateBlockAttestations?: boolean;
|
|
169
|
+
maxAllowedEthClientDriftSeconds: number;
|
|
170
|
+
ethereumAllowNoDebugHosts?: boolean;
|
|
171
|
+
},
|
|
143
172
|
private readonly blobSinkClient: BlobSinkClientInterface,
|
|
144
173
|
private readonly epochCache: EpochCache,
|
|
174
|
+
private readonly dateProvider: DateProvider,
|
|
145
175
|
private readonly instrumentation: ArchiverInstrumentation,
|
|
146
|
-
private readonly l1constants: L1RollupConstants & { l1StartBlockHash: Buffer32 },
|
|
176
|
+
private readonly l1constants: L1RollupConstants & { l1StartBlockHash: Buffer32; genesisArchiveRoot: Fr },
|
|
147
177
|
private readonly log: Logger = createLogger('archiver'),
|
|
148
178
|
) {
|
|
149
179
|
super();
|
|
@@ -153,6 +183,16 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
|
|
|
153
183
|
|
|
154
184
|
this.rollup = new RollupContract(publicClient, l1Addresses.rollupAddress);
|
|
155
185
|
this.inbox = new InboxContract(publicClient, l1Addresses.inboxAddress);
|
|
186
|
+
this.initialSyncPromise = promiseWithResolvers();
|
|
187
|
+
|
|
188
|
+
// Running promise starts with a small interval inbetween runs, so all iterations needed for the initial sync
|
|
189
|
+
// are done as fast as possible. This then gets updated once the initial sync completes.
|
|
190
|
+
this.runningPromise = new RunningPromise(
|
|
191
|
+
() => this.sync(),
|
|
192
|
+
this.log,
|
|
193
|
+
this.config.pollingIntervalMs / 10,
|
|
194
|
+
makeLoggingErrorHandler(this.log, NoBlobBodiesFoundError, BlockTagTooOldError),
|
|
195
|
+
);
|
|
156
196
|
}
|
|
157
197
|
|
|
158
198
|
/**
|
|
@@ -175,13 +215,24 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
|
|
|
175
215
|
pollingInterval: config.viemPollingIntervalMS,
|
|
176
216
|
});
|
|
177
217
|
|
|
218
|
+
// Create debug client using debug RPC URLs if available, otherwise fall back to regular RPC URLs
|
|
219
|
+
const debugRpcUrls = config.l1DebugRpcUrls.length > 0 ? config.l1DebugRpcUrls : config.l1RpcUrls;
|
|
220
|
+
const debugClient = createPublicClient({
|
|
221
|
+
chain: chain.chainInfo,
|
|
222
|
+
transport: fallback(debugRpcUrls.map(url => http(url))),
|
|
223
|
+
pollingInterval: config.viemPollingIntervalMS,
|
|
224
|
+
}) as ViemPublicDebugClient;
|
|
225
|
+
|
|
178
226
|
const rollup = new RollupContract(publicClient, config.l1Contracts.rollupAddress);
|
|
179
227
|
|
|
180
|
-
const [l1StartBlock, l1GenesisTime, proofSubmissionEpochs] =
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
228
|
+
const [l1StartBlock, l1GenesisTime, proofSubmissionEpochs, genesisArchiveRoot, slashingProposerAddress] =
|
|
229
|
+
await Promise.all([
|
|
230
|
+
rollup.getL1StartBlock(),
|
|
231
|
+
rollup.getL1GenesisTime(),
|
|
232
|
+
rollup.getProofSubmissionEpochs(),
|
|
233
|
+
rollup.getGenesisArchiveTreeRoot(),
|
|
234
|
+
rollup.getSlashingProposerAddress(),
|
|
235
|
+
] as const);
|
|
185
236
|
|
|
186
237
|
const l1StartBlockHash = await publicClient
|
|
187
238
|
.getBlock({ blockNumber: l1StartBlock, includeTransactions: false })
|
|
@@ -197,23 +248,31 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
|
|
|
197
248
|
slotDuration,
|
|
198
249
|
ethereumSlotDuration,
|
|
199
250
|
proofSubmissionEpochs: Number(proofSubmissionEpochs),
|
|
251
|
+
genesisArchiveRoot: Fr.fromHexString(genesisArchiveRoot),
|
|
200
252
|
};
|
|
201
253
|
|
|
202
|
-
const opts =
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
254
|
+
const opts = merge(
|
|
255
|
+
{
|
|
256
|
+
pollingIntervalMs: 10_000,
|
|
257
|
+
batchSize: 100,
|
|
258
|
+
maxAllowedEthClientDriftSeconds: 300,
|
|
259
|
+
ethereumAllowNoDebugHosts: false,
|
|
260
|
+
},
|
|
261
|
+
mapArchiverConfig(config),
|
|
262
|
+
);
|
|
206
263
|
|
|
207
264
|
const epochCache = deps.epochCache ?? (await EpochCache.create(config.l1Contracts.rollupAddress, config, deps));
|
|
208
265
|
const telemetry = deps.telemetry ?? getTelemetryClient();
|
|
209
266
|
|
|
210
267
|
const archiver = new Archiver(
|
|
211
268
|
publicClient,
|
|
212
|
-
|
|
269
|
+
debugClient,
|
|
270
|
+
{ ...config.l1Contracts, slashingProposerAddress },
|
|
213
271
|
archiverStore,
|
|
214
272
|
opts,
|
|
215
273
|
deps.blobSinkClient,
|
|
216
274
|
epochCache,
|
|
275
|
+
deps.dateProvider ?? new DateProvider(),
|
|
217
276
|
await ArchiverInstrumentation.new(telemetry, () => archiverStore.estimateSize()),
|
|
218
277
|
l1Constants,
|
|
219
278
|
);
|
|
@@ -221,67 +280,68 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
|
|
|
221
280
|
return archiver;
|
|
222
281
|
}
|
|
223
282
|
|
|
283
|
+
/** Updates archiver config */
|
|
284
|
+
public updateConfig(newConfig: Partial<ArchiverConfig>) {
|
|
285
|
+
this.config = merge(this.config, mapArchiverConfig(newConfig));
|
|
286
|
+
}
|
|
287
|
+
|
|
224
288
|
/**
|
|
225
289
|
* Starts sync process.
|
|
226
290
|
* @param blockUntilSynced - If true, blocks until the archiver has fully synced.
|
|
227
291
|
*/
|
|
228
292
|
public async start(blockUntilSynced: boolean): Promise<void> {
|
|
229
|
-
if (this.runningPromise) {
|
|
293
|
+
if (this.runningPromise.isRunning()) {
|
|
230
294
|
throw new Error('Archiver is already running');
|
|
231
295
|
}
|
|
232
296
|
|
|
233
297
|
await this.blobSinkClient.testSources();
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
this.log,
|
|
245
|
-
this.config.pollingIntervalMs,
|
|
246
|
-
makeLoggingErrorHandler(
|
|
247
|
-
this.log,
|
|
248
|
-
// Ignored errors will not log to the console
|
|
249
|
-
// We ignore NoBlobBodiesFound as the message may not have been passed to the blob sink yet
|
|
250
|
-
NoBlobBodiesFoundError,
|
|
251
|
-
),
|
|
298
|
+
await this.testEthereumNodeSynced();
|
|
299
|
+
await validateAndLogTraceAvailability(this.debugClient, this.config.ethereumAllowNoDebugHosts ?? false);
|
|
300
|
+
|
|
301
|
+
// Log initial state for the archiver
|
|
302
|
+
const { l1StartBlock } = this.l1constants;
|
|
303
|
+
const { blocksSynchedTo = l1StartBlock, messagesSynchedTo = l1StartBlock } = await this.store.getSynchPoint();
|
|
304
|
+
const currentL2Block = await this.getBlockNumber();
|
|
305
|
+
this.log.info(
|
|
306
|
+
`Starting archiver sync to rollup contract ${this.l1Addresses.rollupAddress.toString()} from L1 block ${blocksSynchedTo} and L2 block ${currentL2Block}`,
|
|
307
|
+
{ blocksSynchedTo, messagesSynchedTo, currentL2Block },
|
|
252
308
|
);
|
|
253
309
|
|
|
310
|
+
// Start sync loop, and return the wait for initial sync if we are asked to block until synced
|
|
254
311
|
this.runningPromise.start();
|
|
312
|
+
if (blockUntilSynced) {
|
|
313
|
+
return this.waitForInitialSync();
|
|
314
|
+
}
|
|
255
315
|
}
|
|
256
316
|
|
|
257
317
|
public syncImmediate() {
|
|
258
|
-
if (!this.runningPromise) {
|
|
259
|
-
throw new Error('Archiver is not running');
|
|
260
|
-
}
|
|
261
318
|
return this.runningPromise.trigger();
|
|
262
319
|
}
|
|
263
320
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
321
|
+
public waitForInitialSync() {
|
|
322
|
+
return this.initialSyncPromise.promise;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/** Checks that the ethereum node we are connected to has a latest timestamp no more than the allowed drift. Throw if not. */
|
|
326
|
+
private async testEthereumNodeSynced() {
|
|
327
|
+
const maxAllowedDelay = this.config.maxAllowedEthClientDriftSeconds;
|
|
328
|
+
if (maxAllowedDelay === 0) {
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
const { number, timestamp: l1Timestamp } = await this.publicClient.getBlock({ includeTransactions: false });
|
|
332
|
+
const currentTime = BigInt(this.dateProvider.nowInSeconds());
|
|
333
|
+
if (currentTime - l1Timestamp > BigInt(maxAllowedDelay)) {
|
|
334
|
+
throw new Error(
|
|
335
|
+
`Ethereum node is out of sync (last block synced ${number} at ${l1Timestamp} vs current time ${currentTime})`,
|
|
336
|
+
);
|
|
277
337
|
}
|
|
278
338
|
}
|
|
279
339
|
|
|
280
340
|
/**
|
|
281
341
|
* Fetches logs from L1 contracts and processes them.
|
|
282
342
|
*/
|
|
283
|
-
@trackSpan('Archiver.sync'
|
|
284
|
-
private async sync(
|
|
343
|
+
@trackSpan('Archiver.sync')
|
|
344
|
+
private async sync() {
|
|
285
345
|
/**
|
|
286
346
|
* We keep track of three "pointers" to L1 blocks:
|
|
287
347
|
* 1. the last L1 block that published an L2 block
|
|
@@ -291,8 +351,6 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
|
|
|
291
351
|
* We do this to deal with L1 data providers that are eventually consistent (e.g. Infura).
|
|
292
352
|
* We guard against seeing block X with no data at one point, and later, the provider processes the block and it has data.
|
|
293
353
|
* The archiver will stay back, until there's data on L1 that will move the pointers forward.
|
|
294
|
-
*
|
|
295
|
-
* This code does not handle reorgs.
|
|
296
354
|
*/
|
|
297
355
|
const { l1StartBlock, l1StartBlockHash } = this.l1constants;
|
|
298
356
|
const {
|
|
@@ -304,13 +362,12 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
|
|
|
304
362
|
const currentL1BlockNumber = currentL1Block.number;
|
|
305
363
|
const currentL1BlockHash = Buffer32.fromString(currentL1Block.hash);
|
|
306
364
|
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
}
|
|
365
|
+
this.log.trace(`Starting new archiver sync iteration`, {
|
|
366
|
+
blocksSynchedTo,
|
|
367
|
+
messagesSynchedTo,
|
|
368
|
+
currentL1BlockNumber,
|
|
369
|
+
currentL1BlockHash,
|
|
370
|
+
});
|
|
314
371
|
|
|
315
372
|
// ********** Ensuring Consistency of data pulled from L1 **********
|
|
316
373
|
|
|
@@ -340,37 +397,45 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
|
|
|
340
397
|
? (await this.publicClient.getBlock({ blockNumber: currentL1BlockNumber })).timestamp
|
|
341
398
|
: this.l1Timestamp;
|
|
342
399
|
|
|
343
|
-
//
|
|
400
|
+
// Warn if the latest L1 block timestamp is too old
|
|
401
|
+
const maxAllowedDelay = this.config.maxAllowedEthClientDriftSeconds;
|
|
402
|
+
const now = this.dateProvider.nowInSeconds();
|
|
403
|
+
if (maxAllowedDelay > 0 && Number(currentL1Timestamp) <= now - maxAllowedDelay) {
|
|
404
|
+
this.log.warn(
|
|
405
|
+
`Latest L1 block ${currentL1BlockNumber} timestamp ${currentL1Timestamp} is too old. Make sure your Ethereum node is synced.`,
|
|
406
|
+
{ currentL1BlockNumber, currentL1Timestamp, now, maxAllowedDelay },
|
|
407
|
+
);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
// ********** Events that are processed per checkpoint **********
|
|
344
411
|
if (currentL1BlockNumber > blocksSynchedTo) {
|
|
345
|
-
// First we retrieve new L2 blocks
|
|
346
|
-
|
|
412
|
+
// First we retrieve new checkpoints and L2 blocks and store them in the DB. This will also update the
|
|
413
|
+
// pending chain validation status, proven checkpoint number, and synched L1 block number.
|
|
414
|
+
const rollupStatus = await this.handleCheckpoints(blocksSynchedTo, currentL1BlockNumber);
|
|
347
415
|
// Then we prune the current epoch if it'd reorg on next submission.
|
|
348
|
-
// Note that we don't do this before retrieving
|
|
349
|
-
//
|
|
416
|
+
// Note that we don't do this before retrieving checkpoints because we may need to retrieve
|
|
417
|
+
// checkpoints from more than 2 epochs ago, so we want to make sure we have the latest view of
|
|
350
418
|
// the chain locally before we start unwinding stuff. This can be optimized by figuring out
|
|
351
|
-
// up to which point we're pruning, and then requesting
|
|
419
|
+
// up to which point we're pruning, and then requesting checkpoints up to that point only.
|
|
352
420
|
const { rollupCanPrune } = await this.handleEpochPrune(
|
|
353
|
-
rollupStatus.
|
|
421
|
+
rollupStatus.provenCheckpointNumber,
|
|
354
422
|
currentL1BlockNumber,
|
|
355
423
|
currentL1Timestamp,
|
|
356
424
|
);
|
|
357
425
|
|
|
358
|
-
//
|
|
359
|
-
//
|
|
360
|
-
// we
|
|
361
|
-
if (
|
|
362
|
-
rollupStatus.
|
|
363
|
-
rollupStatus.validationResult?.valid !== this.pendingChainValidationStatus.valid
|
|
364
|
-
) {
|
|
365
|
-
this.pendingChainValidationStatus = rollupStatus.validationResult;
|
|
426
|
+
// If the last checkpoint we processed had an invalid attestation, we manually advance the L1 syncpoint
|
|
427
|
+
// past it, since otherwise we'll keep downloading it and reprocessing it on every iteration until
|
|
428
|
+
// we get a valid checkpoint to advance the syncpoint.
|
|
429
|
+
if (!rollupStatus.validationResult?.valid && rollupStatus.lastL1BlockWithCheckpoint !== undefined) {
|
|
430
|
+
await this.store.setBlockSynchedL1BlockNumber(rollupStatus.lastL1BlockWithCheckpoint);
|
|
366
431
|
}
|
|
367
432
|
|
|
368
|
-
// And lastly we check if we are missing any
|
|
433
|
+
// And lastly we check if we are missing any checkpoints behind us due to a possible L1 reorg.
|
|
369
434
|
// We only do this if rollup cant prune on the next submission. Otherwise we will end up
|
|
370
|
-
// re-syncing the
|
|
435
|
+
// re-syncing the checkpoints we have just unwound above. We also dont do this if the last checkpoint is invalid,
|
|
371
436
|
// since the archiver will rightfully refuse to sync up to it.
|
|
372
|
-
if (!rollupCanPrune &&
|
|
373
|
-
await this.
|
|
437
|
+
if (!rollupCanPrune && rollupStatus.validationResult?.valid) {
|
|
438
|
+
await this.checkForNewCheckpointsBeforeL1SyncPoint(rollupStatus, blocksSynchedTo, currentL1BlockNumber);
|
|
374
439
|
}
|
|
375
440
|
|
|
376
441
|
this.instrumentation.updateL1BlockHeight(currentL1BlockNumber);
|
|
@@ -381,14 +446,18 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
|
|
|
381
446
|
// but the corresponding blocks have not been processed (see #12631).
|
|
382
447
|
this.l1Timestamp = currentL1Timestamp;
|
|
383
448
|
this.l1BlockNumber = currentL1BlockNumber;
|
|
384
|
-
this.initialSyncComplete = true;
|
|
385
449
|
|
|
386
|
-
|
|
387
|
-
|
|
450
|
+
// We resolve the initial sync only once we've caught up with the latest L1 block number (with 1 block grace)
|
|
451
|
+
// so if the initial sync took too long, we still go for another iteration.
|
|
452
|
+
if (!this.initialSyncComplete && currentL1BlockNumber + 1n >= (await this.publicClient.getBlockNumber())) {
|
|
453
|
+
this.log.info(`Initial archiver sync to L1 block ${currentL1BlockNumber} complete`, {
|
|
388
454
|
l1BlockNumber: currentL1BlockNumber,
|
|
389
455
|
syncPoint: await this.store.getSynchPoint(),
|
|
390
456
|
...(await this.getL2Tips()),
|
|
391
457
|
});
|
|
458
|
+
this.runningPromise.setPollingIntervalMS(this.config.pollingIntervalMs);
|
|
459
|
+
this.initialSyncComplete = true;
|
|
460
|
+
this.initialSyncPromise.resolve();
|
|
392
461
|
}
|
|
393
462
|
}
|
|
394
463
|
|
|
@@ -406,43 +475,47 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
|
|
|
406
475
|
return result;
|
|
407
476
|
}
|
|
408
477
|
|
|
409
|
-
/** Checks if there'd be a reorg for the next
|
|
410
|
-
private async handleEpochPrune(
|
|
478
|
+
/** Checks if there'd be a reorg for the next checkpoint submission and start pruning now. */
|
|
479
|
+
private async handleEpochPrune(
|
|
480
|
+
provenCheckpointNumber: CheckpointNumber,
|
|
481
|
+
currentL1BlockNumber: bigint,
|
|
482
|
+
currentL1Timestamp: bigint,
|
|
483
|
+
) {
|
|
411
484
|
const rollupCanPrune = await this.canPrune(currentL1BlockNumber, currentL1Timestamp);
|
|
412
|
-
const
|
|
413
|
-
const canPrune =
|
|
485
|
+
const localPendingCheckpointNumber = await this.getSynchedCheckpointNumber();
|
|
486
|
+
const canPrune = localPendingCheckpointNumber > provenCheckpointNumber && rollupCanPrune;
|
|
414
487
|
|
|
415
488
|
if (canPrune) {
|
|
416
489
|
const timer = new Timer();
|
|
417
|
-
const pruneFrom =
|
|
490
|
+
const pruneFrom = CheckpointNumber(provenCheckpointNumber + 1);
|
|
418
491
|
|
|
419
|
-
const header = await this.
|
|
492
|
+
const header = await this.getCheckpointHeader(pruneFrom);
|
|
420
493
|
if (header === undefined) {
|
|
421
|
-
throw new Error(`Missing
|
|
494
|
+
throw new Error(`Missing checkpoint header ${pruneFrom}`);
|
|
422
495
|
}
|
|
423
496
|
|
|
424
|
-
const pruneFromSlotNumber = header.
|
|
425
|
-
const pruneFromEpochNumber = getEpochAtSlot(pruneFromSlotNumber, this.l1constants);
|
|
497
|
+
const pruneFromSlotNumber = header.slotNumber;
|
|
498
|
+
const pruneFromEpochNumber: EpochNumber = getEpochAtSlot(pruneFromSlotNumber, this.l1constants);
|
|
426
499
|
|
|
427
|
-
const
|
|
500
|
+
const checkpointsToUnwind = localPendingCheckpointNumber - provenCheckpointNumber;
|
|
428
501
|
|
|
429
|
-
const
|
|
502
|
+
const checkpoints = await this.getCheckpoints(pruneFrom, checkpointsToUnwind);
|
|
430
503
|
|
|
431
504
|
// Emit an event for listening services to react to the chain prune
|
|
432
505
|
this.emit(L2BlockSourceEvents.L2PruneDetected, {
|
|
433
506
|
type: L2BlockSourceEvents.L2PruneDetected,
|
|
434
507
|
epochNumber: pruneFromEpochNumber,
|
|
435
|
-
blocks,
|
|
508
|
+
blocks: checkpoints.flatMap(c => L2Block.fromCheckpoint(c)),
|
|
436
509
|
});
|
|
437
510
|
|
|
438
511
|
this.log.debug(
|
|
439
|
-
`L2 prune from ${
|
|
512
|
+
`L2 prune from ${provenCheckpointNumber + 1} to ${localPendingCheckpointNumber} will occur on next checkpoint submission.`,
|
|
440
513
|
);
|
|
441
|
-
await this.
|
|
514
|
+
await this.unwindCheckpoints(localPendingCheckpointNumber, checkpointsToUnwind);
|
|
442
515
|
this.log.warn(
|
|
443
|
-
`Unwound ${count(
|
|
444
|
-
`to ${
|
|
445
|
-
`Updated
|
|
516
|
+
`Unwound ${count(checkpointsToUnwind, 'checkpoint')} from checkpoint ${localPendingCheckpointNumber} ` +
|
|
517
|
+
`to ${provenCheckpointNumber} due to predicted reorg at L1 block ${currentL1BlockNumber}. ` +
|
|
518
|
+
`Updated latest checkpoint is ${await this.getSynchedCheckpointNumber()}.`,
|
|
446
519
|
);
|
|
447
520
|
this.instrumentation.processPrune(timer.ms());
|
|
448
521
|
// TODO(palla/reorg): Do we need to set the block synched L1 block number here?
|
|
@@ -489,7 +562,7 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
|
|
|
489
562
|
remoteMessagesState.totalMessagesInserted === localMessagesInserted &&
|
|
490
563
|
remoteMessagesState.messagesRollingHash.equals(localLastMessage?.rollingHash ?? Buffer16.ZERO)
|
|
491
564
|
) {
|
|
492
|
-
this.log.
|
|
565
|
+
this.log.trace(
|
|
493
566
|
`No L1 to L2 messages to query between L1 blocks ${messagesSyncPoint.l1BlockNumber} and ${currentL1BlockNumber}.`,
|
|
494
567
|
);
|
|
495
568
|
return;
|
|
@@ -543,7 +616,7 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
|
|
|
543
616
|
// Log stats for messages retrieved (if any).
|
|
544
617
|
if (messageCount > 0) {
|
|
545
618
|
this.log.info(
|
|
546
|
-
`Retrieved ${messageCount} new L1 to L2 messages up to message with index ${lastMessage?.index} for
|
|
619
|
+
`Retrieved ${messageCount} new L1 to L2 messages up to message with index ${lastMessage?.index} for checkpoint ${lastMessage?.checkpointNumber}`,
|
|
547
620
|
{ lastMessage, messageCount },
|
|
548
621
|
);
|
|
549
622
|
}
|
|
@@ -621,202 +694,238 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
|
|
|
621
694
|
return Buffer32.fromString(block.hash);
|
|
622
695
|
}
|
|
623
696
|
|
|
624
|
-
private async
|
|
625
|
-
const
|
|
626
|
-
const
|
|
627
|
-
|
|
697
|
+
private async handleCheckpoints(blocksSynchedTo: bigint, currentL1BlockNumber: bigint): Promise<RollupStatus> {
|
|
698
|
+
const localPendingCheckpointNumber = await this.getSynchedCheckpointNumber();
|
|
699
|
+
const initialValidationResult: ValidateBlockResult | undefined = await this.store.getPendingChainValidationStatus();
|
|
700
|
+
const [
|
|
701
|
+
rollupProvenCheckpointNumber,
|
|
702
|
+
provenArchive,
|
|
703
|
+
rollupPendingCheckpointNumber,
|
|
704
|
+
pendingArchive,
|
|
705
|
+
archiveForLocalPendingCheckpointNumber,
|
|
706
|
+
] = await this.rollup.status(localPendingCheckpointNumber, { blockNumber: currentL1BlockNumber });
|
|
707
|
+
const provenCheckpointNumber = CheckpointNumber.fromBigInt(rollupProvenCheckpointNumber);
|
|
708
|
+
const pendingCheckpointNumber = CheckpointNumber.fromBigInt(rollupPendingCheckpointNumber);
|
|
628
709
|
const rollupStatus = {
|
|
629
|
-
|
|
710
|
+
provenCheckpointNumber,
|
|
630
711
|
provenArchive,
|
|
631
|
-
|
|
712
|
+
pendingCheckpointNumber,
|
|
632
713
|
pendingArchive,
|
|
633
|
-
validationResult:
|
|
714
|
+
validationResult: initialValidationResult,
|
|
634
715
|
};
|
|
635
716
|
this.log.trace(`Retrieved rollup status at current L1 block ${currentL1BlockNumber}.`, {
|
|
636
|
-
|
|
717
|
+
localPendingCheckpointNumber,
|
|
637
718
|
blocksSynchedTo,
|
|
638
719
|
currentL1BlockNumber,
|
|
639
|
-
|
|
720
|
+
archiveForLocalPendingCheckpointNumber,
|
|
640
721
|
...rollupStatus,
|
|
641
722
|
});
|
|
642
723
|
|
|
643
|
-
const
|
|
644
|
-
// Annoying edge case: if proven
|
|
645
|
-
// we need to set it to zero. This is an edge case because we dont have a
|
|
646
|
-
// so
|
|
647
|
-
if (
|
|
648
|
-
const
|
|
649
|
-
if (
|
|
650
|
-
await this.
|
|
651
|
-
this.log.info(`Rolled back proven chain to
|
|
724
|
+
const updateProvenCheckpoint = async () => {
|
|
725
|
+
// Annoying edge case: if proven checkpoint is moved back to 0 due to a reorg at the beginning of the chain,
|
|
726
|
+
// we need to set it to zero. This is an edge case because we dont have a checkpoint zero (initial checkpoint is one),
|
|
727
|
+
// so localCheckpointForDestinationProvenCheckpointNumber would not be found below.
|
|
728
|
+
if (provenCheckpointNumber === 0) {
|
|
729
|
+
const localProvenCheckpointNumber = await this.getProvenCheckpointNumber();
|
|
730
|
+
if (localProvenCheckpointNumber !== provenCheckpointNumber) {
|
|
731
|
+
await this.setProvenCheckpointNumber(provenCheckpointNumber);
|
|
732
|
+
this.log.info(`Rolled back proven chain to checkpoint ${provenCheckpointNumber}`, { provenCheckpointNumber });
|
|
652
733
|
}
|
|
653
734
|
}
|
|
654
735
|
|
|
655
|
-
const
|
|
736
|
+
const localCheckpointForDestinationProvenCheckpointNumber = await this.getCheckpoint(provenCheckpointNumber);
|
|
656
737
|
|
|
657
|
-
// Sanity check. I've hit what seems to be a state where the proven
|
|
658
|
-
// synched
|
|
659
|
-
const synched = await this.
|
|
660
|
-
if (
|
|
738
|
+
// Sanity check. I've hit what seems to be a state where the proven checkpoint is set to a value greater than the latest
|
|
739
|
+
// synched checkpoint when requesting L2Tips from the archiver. This is the only place where the proven checkpoint is set.
|
|
740
|
+
const synched = await this.getSynchedCheckpointNumber();
|
|
741
|
+
if (
|
|
742
|
+
localCheckpointForDestinationProvenCheckpointNumber &&
|
|
743
|
+
synched < localCheckpointForDestinationProvenCheckpointNumber.number
|
|
744
|
+
) {
|
|
661
745
|
this.log.error(
|
|
662
|
-
`Hit local
|
|
746
|
+
`Hit local checkpoint greater than last synched checkpoint: ${localCheckpointForDestinationProvenCheckpointNumber.number} > ${synched}`,
|
|
663
747
|
);
|
|
664
748
|
}
|
|
665
749
|
|
|
666
750
|
this.log.trace(
|
|
667
|
-
`Local
|
|
668
|
-
|
|
751
|
+
`Local checkpoint for remote proven checkpoint ${provenCheckpointNumber} is ${
|
|
752
|
+
localCheckpointForDestinationProvenCheckpointNumber?.archive.root.toString() ?? 'undefined'
|
|
669
753
|
}`,
|
|
670
754
|
);
|
|
671
755
|
|
|
756
|
+
const lastProvenBlockNumber = await this.getLastBlockNumberInCheckpoint(provenCheckpointNumber);
|
|
672
757
|
if (
|
|
673
|
-
|
|
674
|
-
provenArchive ===
|
|
758
|
+
localCheckpointForDestinationProvenCheckpointNumber &&
|
|
759
|
+
provenArchive === localCheckpointForDestinationProvenCheckpointNumber.archive.root.toString()
|
|
675
760
|
) {
|
|
676
|
-
const
|
|
677
|
-
if (
|
|
678
|
-
await this.
|
|
679
|
-
this.log.info(`Updated proven chain to
|
|
680
|
-
|
|
761
|
+
const localProvenCheckpointNumber = await this.getProvenCheckpointNumber();
|
|
762
|
+
if (localProvenCheckpointNumber !== provenCheckpointNumber) {
|
|
763
|
+
await this.setProvenCheckpointNumber(provenCheckpointNumber);
|
|
764
|
+
this.log.info(`Updated proven chain to checkpoint ${provenCheckpointNumber}`, {
|
|
765
|
+
provenCheckpointNumber,
|
|
681
766
|
});
|
|
682
|
-
const provenSlotNumber =
|
|
683
|
-
|
|
684
|
-
|
|
767
|
+
const provenSlotNumber = localCheckpointForDestinationProvenCheckpointNumber.header.slotNumber;
|
|
768
|
+
const provenEpochNumber: EpochNumber = getEpochAtSlot(provenSlotNumber, this.l1constants);
|
|
769
|
+
|
|
685
770
|
this.emit(L2BlockSourceEvents.L2BlockProven, {
|
|
686
771
|
type: L2BlockSourceEvents.L2BlockProven,
|
|
687
|
-
blockNumber:
|
|
772
|
+
blockNumber: lastProvenBlockNumber,
|
|
688
773
|
slotNumber: provenSlotNumber,
|
|
689
774
|
epochNumber: provenEpochNumber,
|
|
690
775
|
});
|
|
691
776
|
} else {
|
|
692
|
-
this.log.trace(`Proven
|
|
777
|
+
this.log.trace(`Proven checkpoint ${provenCheckpointNumber} already stored.`);
|
|
693
778
|
}
|
|
694
779
|
}
|
|
695
|
-
this.instrumentation.updateLastProvenBlock(
|
|
780
|
+
this.instrumentation.updateLastProvenBlock(lastProvenBlockNumber);
|
|
696
781
|
};
|
|
697
782
|
|
|
698
|
-
// This is an edge case that we only hit if there are no proposed
|
|
699
|
-
// If we have 0
|
|
700
|
-
const
|
|
701
|
-
if (
|
|
783
|
+
// This is an edge case that we only hit if there are no proposed checkpoints.
|
|
784
|
+
// If we have 0 checkpoints locally and there are no checkpoints onchain there is nothing to do.
|
|
785
|
+
const noCheckpoints = localPendingCheckpointNumber === 0 && pendingCheckpointNumber === 0;
|
|
786
|
+
if (noCheckpoints) {
|
|
702
787
|
await this.store.setBlockSynchedL1BlockNumber(currentL1BlockNumber);
|
|
703
788
|
this.log.debug(
|
|
704
|
-
`No
|
|
789
|
+
`No checkpoints to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}, no checkpoints on chain`,
|
|
705
790
|
);
|
|
706
791
|
return rollupStatus;
|
|
707
792
|
}
|
|
708
793
|
|
|
709
|
-
await
|
|
794
|
+
await updateProvenCheckpoint();
|
|
710
795
|
|
|
711
796
|
// Related to the L2 reorgs of the pending chain. We are only interested in actually addressing a reorg if there
|
|
712
|
-
// are any state that could be impacted by it. If we have no
|
|
713
|
-
if (
|
|
714
|
-
const
|
|
715
|
-
if (
|
|
716
|
-
throw new Error(`Missing
|
|
797
|
+
// are any state that could be impacted by it. If we have no checkpoints, there is no impact.
|
|
798
|
+
if (localPendingCheckpointNumber > 0) {
|
|
799
|
+
const localPendingCheckpoint = await this.getCheckpoint(localPendingCheckpointNumber);
|
|
800
|
+
if (localPendingCheckpoint === undefined) {
|
|
801
|
+
throw new Error(`Missing checkpoint ${localPendingCheckpointNumber}`);
|
|
717
802
|
}
|
|
718
803
|
|
|
719
|
-
const localPendingArchiveRoot =
|
|
720
|
-
const
|
|
721
|
-
if (
|
|
804
|
+
const localPendingArchiveRoot = localPendingCheckpoint.archive.root.toString();
|
|
805
|
+
const noCheckpointSinceLast = localPendingCheckpoint && pendingArchive === localPendingArchiveRoot;
|
|
806
|
+
if (noCheckpointSinceLast) {
|
|
722
807
|
// We believe the following line causes a problem when we encounter L1 re-orgs.
|
|
723
808
|
// Basically, by setting the synched L1 block number here, we are saying that we have
|
|
724
|
-
// processed all
|
|
809
|
+
// processed all checkpoints up to the current L1 block number and we will not attempt to retrieve logs from
|
|
725
810
|
// this block again (or any blocks before).
|
|
726
|
-
// However, in the re-org scenario, our L1 node is temporarily lying to us and we end up potentially missing
|
|
811
|
+
// However, in the re-org scenario, our L1 node is temporarily lying to us and we end up potentially missing checkpoints.
|
|
727
812
|
// We must only set this block number based on actually retrieved logs.
|
|
728
813
|
// TODO(#8621): Tackle this properly when we handle L1 Re-orgs.
|
|
729
814
|
// await this.store.setBlockSynchedL1BlockNumber(currentL1BlockNumber);
|
|
730
|
-
this.log.debug(`No
|
|
815
|
+
this.log.debug(`No checkpoints to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}`);
|
|
731
816
|
return rollupStatus;
|
|
732
817
|
}
|
|
733
818
|
|
|
734
|
-
const
|
|
735
|
-
if (!
|
|
736
|
-
// If our local pending
|
|
819
|
+
const localPendingCheckpointInChain = archiveForLocalPendingCheckpointNumber === localPendingArchiveRoot;
|
|
820
|
+
if (!localPendingCheckpointInChain) {
|
|
821
|
+
// If our local pending checkpoint tip is not in the chain on L1 a "prune" must have happened
|
|
737
822
|
// or the L1 have reorged.
|
|
738
823
|
// In any case, we have to figure out how far into the past the action will take us.
|
|
739
|
-
// For simplicity here, we will simply rewind until we end in a
|
|
824
|
+
// For simplicity here, we will simply rewind until we end in a checkpoint that is also on the chain on L1.
|
|
740
825
|
this.log.debug(
|
|
741
|
-
`L2 prune has been detected due to local pending
|
|
742
|
-
{
|
|
826
|
+
`L2 prune has been detected due to local pending checkpoint ${localPendingCheckpointNumber} not in chain`,
|
|
827
|
+
{ localPendingCheckpointNumber, localPendingArchiveRoot, archiveForLocalPendingCheckpointNumber },
|
|
743
828
|
);
|
|
744
829
|
|
|
745
|
-
let tipAfterUnwind =
|
|
830
|
+
let tipAfterUnwind = localPendingCheckpointNumber;
|
|
746
831
|
while (true) {
|
|
747
|
-
const
|
|
748
|
-
if (
|
|
832
|
+
const candidateCheckpoint = await this.getCheckpoint(tipAfterUnwind);
|
|
833
|
+
if (candidateCheckpoint === undefined) {
|
|
749
834
|
break;
|
|
750
835
|
}
|
|
751
836
|
|
|
752
|
-
const archiveAtContract = await this.rollup.archiveAt(
|
|
753
|
-
|
|
754
|
-
|
|
837
|
+
const archiveAtContract = await this.rollup.archiveAt(candidateCheckpoint.number);
|
|
838
|
+
this.log.trace(
|
|
839
|
+
`Checking local checkpoint ${candidateCheckpoint.number} with archive ${candidateCheckpoint.archive.root}`,
|
|
840
|
+
{
|
|
841
|
+
archiveAtContract,
|
|
842
|
+
archiveLocal: candidateCheckpoint.archive.root.toString(),
|
|
843
|
+
},
|
|
844
|
+
);
|
|
845
|
+
if (archiveAtContract === candidateCheckpoint.archive.root.toString()) {
|
|
755
846
|
break;
|
|
756
847
|
}
|
|
757
848
|
tipAfterUnwind--;
|
|
758
849
|
}
|
|
759
850
|
|
|
760
|
-
const
|
|
761
|
-
await this.
|
|
851
|
+
const checkpointsToUnwind = localPendingCheckpointNumber - tipAfterUnwind;
|
|
852
|
+
await this.unwindCheckpoints(localPendingCheckpointNumber, checkpointsToUnwind);
|
|
762
853
|
|
|
763
854
|
this.log.warn(
|
|
764
|
-
`Unwound ${count(
|
|
765
|
-
`due to mismatched
|
|
766
|
-
`Updated L2 latest
|
|
855
|
+
`Unwound ${count(checkpointsToUnwind, 'checkpoint')} from checkpoint ${localPendingCheckpointNumber} ` +
|
|
856
|
+
`due to mismatched checkpoint hashes at L1 block ${currentL1BlockNumber}. ` +
|
|
857
|
+
`Updated L2 latest checkpoint is ${await this.getSynchedCheckpointNumber()}.`,
|
|
767
858
|
);
|
|
768
859
|
}
|
|
769
860
|
}
|
|
770
861
|
|
|
771
|
-
// Retrieve
|
|
862
|
+
// Retrieve checkpoints in batches. Each batch is estimated to accommodate up to 'blockBatchSize' L1 blocks,
|
|
772
863
|
// computed using the L2 block time vs the L1 block time.
|
|
773
864
|
let searchStartBlock: bigint = blocksSynchedTo;
|
|
774
865
|
let searchEndBlock: bigint = blocksSynchedTo;
|
|
775
|
-
let
|
|
866
|
+
let lastRetrievedCheckpoint: PublishedCheckpoint | undefined;
|
|
867
|
+
let lastL1BlockWithCheckpoint: bigint | undefined = undefined;
|
|
776
868
|
|
|
777
869
|
do {
|
|
778
870
|
[searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, currentL1BlockNumber);
|
|
779
871
|
|
|
780
|
-
this.log.trace(`Retrieving
|
|
872
|
+
this.log.trace(`Retrieving checkpoints from L1 block ${searchStartBlock} to ${searchEndBlock}`);
|
|
781
873
|
|
|
782
874
|
// TODO(md): Retrieve from blob sink then from consensus client, then from peers
|
|
783
|
-
const
|
|
875
|
+
const retrievedCheckpoints = await retrieveCheckpointsFromRollup(
|
|
784
876
|
this.rollup.getContract() as GetContractReturnType<typeof RollupAbi, ViemPublicClient>,
|
|
785
877
|
this.publicClient,
|
|
878
|
+
this.debugClient,
|
|
786
879
|
this.blobSinkClient,
|
|
787
880
|
searchStartBlock, // TODO(palla/reorg): If the L2 reorg was due to an L1 reorg, we need to start search earlier
|
|
788
881
|
searchEndBlock,
|
|
882
|
+
this.l1Addresses,
|
|
883
|
+
this.instrumentation,
|
|
789
884
|
this.log,
|
|
790
885
|
);
|
|
791
886
|
|
|
792
|
-
if (
|
|
887
|
+
if (retrievedCheckpoints.length === 0) {
|
|
793
888
|
// We are not calling `setBlockSynchedL1BlockNumber` because it may cause sync issues if based off infura.
|
|
794
889
|
// See further details in earlier comments.
|
|
795
|
-
this.log.trace(`Retrieved no new
|
|
890
|
+
this.log.trace(`Retrieved no new checkpoints from L1 block ${searchStartBlock} to ${searchEndBlock}`);
|
|
796
891
|
continue;
|
|
797
892
|
}
|
|
798
893
|
|
|
799
|
-
const lastProcessedL1BlockNumber = retrievedBlocks[retrievedBlocks.length - 1].l1.blockNumber;
|
|
800
894
|
this.log.debug(
|
|
801
|
-
`Retrieved ${
|
|
895
|
+
`Retrieved ${retrievedCheckpoints.length} new checkpoints between L1 blocks ${searchStartBlock} and ${searchEndBlock}`,
|
|
896
|
+
{
|
|
897
|
+
lastProcessedCheckpoint: retrievedCheckpoints[retrievedCheckpoints.length - 1].l1,
|
|
898
|
+
searchStartBlock,
|
|
899
|
+
searchEndBlock,
|
|
900
|
+
},
|
|
802
901
|
);
|
|
803
902
|
|
|
804
|
-
const
|
|
805
|
-
const
|
|
806
|
-
|
|
807
|
-
for (const
|
|
808
|
-
const validationResult =
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
if
|
|
903
|
+
const publishedCheckpoints = await Promise.all(retrievedCheckpoints.map(b => retrievedToPublishedCheckpoint(b)));
|
|
904
|
+
const validCheckpoints: PublishedCheckpoint[] = [];
|
|
905
|
+
|
|
906
|
+
for (const published of publishedCheckpoints) {
|
|
907
|
+
const validationResult = this.config.skipValidateBlockAttestations
|
|
908
|
+
? { valid: true as const }
|
|
909
|
+
: await validateCheckpointAttestations(published, this.epochCache, this.l1constants, this.log);
|
|
910
|
+
|
|
911
|
+
// Only update the validation result if it has changed, so we can keep track of the first invalid checkpoint
|
|
912
|
+
// in case there is a sequence of more than one invalid checkpoint, as we need to invalidate the first one.
|
|
913
|
+
// There is an exception though: if a checkpoint is invalidated and replaced with another invalid checkpoint,
|
|
914
|
+
// we need to update the validation result, since we need to be able to invalidate the new one.
|
|
915
|
+
// See test 'chain progresses if an invalid checkpoint is invalidated with an invalid one' for more info.
|
|
916
|
+
if (
|
|
917
|
+
rollupStatus.validationResult?.valid !== validationResult.valid ||
|
|
918
|
+
(!rollupStatus.validationResult.valid &&
|
|
919
|
+
!validationResult.valid &&
|
|
920
|
+
rollupStatus.validationResult.block.blockNumber === validationResult.block.blockNumber)
|
|
921
|
+
) {
|
|
813
922
|
rollupStatus.validationResult = validationResult;
|
|
814
923
|
}
|
|
815
924
|
|
|
816
925
|
if (!validationResult.valid) {
|
|
817
|
-
this.log.warn(`Skipping
|
|
818
|
-
|
|
819
|
-
l1BlockNumber:
|
|
926
|
+
this.log.warn(`Skipping checkpoint ${published.checkpoint.number} due to invalid attestations`, {
|
|
927
|
+
checkpointHash: published.checkpoint.hash(),
|
|
928
|
+
l1BlockNumber: published.l1.blockNumber,
|
|
820
929
|
...pick(validationResult, 'reason'),
|
|
821
930
|
});
|
|
822
931
|
|
|
@@ -826,31 +935,56 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
|
|
|
826
935
|
validationResult,
|
|
827
936
|
});
|
|
828
937
|
|
|
829
|
-
// We keep consuming
|
|
830
|
-
// We just pretend the invalid ones are not there and keep consuming the next
|
|
938
|
+
// We keep consuming checkpoints if we find an invalid one, since we do not listen for CheckpointInvalidated events
|
|
939
|
+
// We just pretend the invalid ones are not there and keep consuming the next checkpoints
|
|
940
|
+
// Note that this breaks if the committee ever attests to a descendant of an invalid checkpoint
|
|
831
941
|
continue;
|
|
832
942
|
}
|
|
833
943
|
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
944
|
+
// Check the inHash of the checkpoint against the l1->l2 messages.
|
|
945
|
+
// The messages should've been synced up to the currentL1BlockNumber and must be available for the published
|
|
946
|
+
// checkpoints we just retrieved.
|
|
947
|
+
const l1ToL2Messages = await this.getL1ToL2Messages(published.checkpoint.number);
|
|
948
|
+
const computedInHash = computeInHashFromL1ToL2Messages(l1ToL2Messages);
|
|
949
|
+
const publishedInHash = published.checkpoint.header.contentCommitment.inHash;
|
|
950
|
+
if (!computedInHash.equals(publishedInHash)) {
|
|
951
|
+
this.log.fatal(`Mismatch inHash for checkpoint ${published.checkpoint.number}`, {
|
|
952
|
+
checkpointHash: published.checkpoint.hash(),
|
|
953
|
+
l1BlockNumber: published.l1.blockNumber,
|
|
954
|
+
computedInHash,
|
|
955
|
+
publishedInHash,
|
|
956
|
+
});
|
|
957
|
+
// Throwing an error since this is most likely caused by a bug.
|
|
958
|
+
throw new Error(
|
|
959
|
+
`Mismatch inHash for checkpoint ${published.checkpoint.number}. Expected ${computedInHash} but got ${publishedInHash}`,
|
|
960
|
+
);
|
|
961
|
+
}
|
|
962
|
+
|
|
963
|
+
validCheckpoints.push(published);
|
|
964
|
+
this.log.debug(
|
|
965
|
+
`Ingesting new checkpoint ${published.checkpoint.number} with ${published.checkpoint.blocks.length} blocks`,
|
|
966
|
+
{
|
|
967
|
+
checkpointHash: published.checkpoint.hash(),
|
|
968
|
+
l1BlockNumber: published.l1.blockNumber,
|
|
969
|
+
...published.checkpoint.header.toInspect(),
|
|
970
|
+
blocks: published.checkpoint.blocks.map(b => b.getStats()),
|
|
971
|
+
},
|
|
972
|
+
);
|
|
841
973
|
}
|
|
842
974
|
|
|
843
975
|
try {
|
|
844
|
-
const
|
|
976
|
+
const updatedValidationResult =
|
|
977
|
+
rollupStatus.validationResult === initialValidationResult ? undefined : rollupStatus.validationResult;
|
|
978
|
+
const [processDuration] = await elapsed(() => this.addCheckpoints(validCheckpoints, updatedValidationResult));
|
|
845
979
|
this.instrumentation.processNewBlocks(
|
|
846
|
-
processDuration /
|
|
847
|
-
|
|
980
|
+
processDuration / validCheckpoints.length,
|
|
981
|
+
validCheckpoints.flatMap(c => c.checkpoint.blocks),
|
|
848
982
|
);
|
|
849
983
|
} catch (err) {
|
|
850
984
|
if (err instanceof InitialBlockNumberNotSequentialError) {
|
|
851
985
|
const { previousBlockNumber, newBlockNumber } = err;
|
|
852
986
|
const previousBlock = previousBlockNumber
|
|
853
|
-
? await this.store.getPublishedBlock(previousBlockNumber)
|
|
987
|
+
? await this.store.getPublishedBlock(BlockNumber(previousBlockNumber))
|
|
854
988
|
: undefined;
|
|
855
989
|
const updatedL1SyncPoint = previousBlock?.l1.blockNumber ?? this.l1constants.l1StartBlock;
|
|
856
990
|
await this.store.setBlockSynchedL1BlockNumber(updatedL1SyncPoint);
|
|
@@ -867,56 +1001,56 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
|
|
|
867
1001
|
throw err;
|
|
868
1002
|
}
|
|
869
1003
|
|
|
870
|
-
for (const
|
|
871
|
-
this.log.info(`Downloaded
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
1004
|
+
for (const checkpoint of validCheckpoints) {
|
|
1005
|
+
this.log.info(`Downloaded checkpoint ${checkpoint.checkpoint.number}`, {
|
|
1006
|
+
checkpointHash: checkpoint.checkpoint.hash(),
|
|
1007
|
+
checkpointNumber: checkpoint.checkpoint.number,
|
|
1008
|
+
blockCount: checkpoint.checkpoint.blocks.length,
|
|
1009
|
+
txCount: checkpoint.checkpoint.blocks.reduce((acc, b) => acc + b.body.txEffects.length, 0),
|
|
1010
|
+
header: checkpoint.checkpoint.header.toInspect(),
|
|
1011
|
+
archiveRoot: checkpoint.checkpoint.archive.root.toString(),
|
|
1012
|
+
archiveNextLeafIndex: checkpoint.checkpoint.archive.nextAvailableLeafIndex,
|
|
878
1013
|
});
|
|
879
1014
|
}
|
|
880
|
-
|
|
1015
|
+
lastRetrievedCheckpoint = validCheckpoints.at(-1) ?? lastRetrievedCheckpoint;
|
|
1016
|
+
lastL1BlockWithCheckpoint = retrievedCheckpoints.at(-1)?.l1.blockNumber ?? lastL1BlockWithCheckpoint;
|
|
881
1017
|
} while (searchEndBlock < currentL1BlockNumber);
|
|
882
1018
|
|
|
883
1019
|
// Important that we update AFTER inserting the blocks.
|
|
884
|
-
await
|
|
1020
|
+
await updateProvenCheckpoint();
|
|
885
1021
|
|
|
886
|
-
return { ...rollupStatus,
|
|
1022
|
+
return { ...rollupStatus, lastRetrievedCheckpoint, lastL1BlockWithCheckpoint };
|
|
887
1023
|
}
|
|
888
1024
|
|
|
889
|
-
private async
|
|
890
|
-
status:
|
|
891
|
-
lastRetrievedBlock?: PublishedL2Block;
|
|
892
|
-
pendingBlockNumber: number;
|
|
893
|
-
},
|
|
1025
|
+
private async checkForNewCheckpointsBeforeL1SyncPoint(
|
|
1026
|
+
status: RollupStatus,
|
|
894
1027
|
blocksSynchedTo: bigint,
|
|
895
1028
|
currentL1BlockNumber: bigint,
|
|
896
1029
|
) {
|
|
897
|
-
const {
|
|
898
|
-
// Compare the last
|
|
1030
|
+
const { lastRetrievedCheckpoint, pendingCheckpointNumber } = status;
|
|
1031
|
+
// Compare the last checkpoint we have (either retrieved in this round or loaded from store) with what the
|
|
899
1032
|
// rollup contract told us was the latest one (pinned at the currentL1BlockNumber).
|
|
900
|
-
const
|
|
901
|
-
|
|
1033
|
+
const latestLocalCheckpointNumber =
|
|
1034
|
+
lastRetrievedCheckpoint?.checkpoint.number ?? (await this.getSynchedCheckpointNumber());
|
|
1035
|
+
if (latestLocalCheckpointNumber < pendingCheckpointNumber) {
|
|
902
1036
|
// Here we have consumed all logs until the `currentL1Block` we pinned at the beginning of the archiver loop,
|
|
903
|
-
// but still
|
|
904
|
-
// We suspect an L1 reorg that added
|
|
905
|
-
// last
|
|
906
|
-
// don't have one, we go back 2 L1 epochs, which is the deepest possible reorg (assuming Casper is working).
|
|
907
|
-
const
|
|
908
|
-
|
|
909
|
-
(
|
|
910
|
-
? await this.
|
|
1037
|
+
// but still haven't reached the pending checkpoint according to the call to the rollup contract.
|
|
1038
|
+
// We suspect an L1 reorg that added checkpoints *behind* us. If that is the case, it must have happened between
|
|
1039
|
+
// the last checkpoint we saw and the current one, so we reset the last synched L1 block number. In the edge case
|
|
1040
|
+
// we don't have one, we go back 2 L1 epochs, which is the deepest possible reorg (assuming Casper is working).
|
|
1041
|
+
const latestLocalCheckpoint =
|
|
1042
|
+
lastRetrievedCheckpoint ??
|
|
1043
|
+
(latestLocalCheckpointNumber > 0
|
|
1044
|
+
? await this.getPublishedCheckpoints(latestLocalCheckpointNumber, 1).then(([c]) => c)
|
|
911
1045
|
: undefined);
|
|
912
|
-
const targetL1BlockNumber =
|
|
913
|
-
const
|
|
1046
|
+
const targetL1BlockNumber = latestLocalCheckpoint?.l1.blockNumber ?? maxBigint(currentL1BlockNumber - 64n, 0n);
|
|
1047
|
+
const latestLocalCheckpointArchive = latestLocalCheckpoint?.checkpoint.archive.root.toString();
|
|
914
1048
|
this.log.warn(
|
|
915
|
-
`Failed to reach
|
|
1049
|
+
`Failed to reach checkpoint ${pendingCheckpointNumber} at ${currentL1BlockNumber} (latest is ${latestLocalCheckpointNumber}). ` +
|
|
916
1050
|
`Rolling back last synched L1 block number to ${targetL1BlockNumber}.`,
|
|
917
1051
|
{
|
|
918
|
-
|
|
919
|
-
|
|
1052
|
+
latestLocalCheckpointNumber,
|
|
1053
|
+
latestLocalCheckpointArchive,
|
|
920
1054
|
blocksSynchedTo,
|
|
921
1055
|
currentL1BlockNumber,
|
|
922
1056
|
...status,
|
|
@@ -924,18 +1058,15 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
|
|
|
924
1058
|
);
|
|
925
1059
|
await this.store.setBlockSynchedL1BlockNumber(targetL1BlockNumber);
|
|
926
1060
|
} else {
|
|
927
|
-
this.log.trace(`No new
|
|
928
|
-
|
|
929
|
-
|
|
1061
|
+
this.log.trace(`No new checkpoints behind L1 sync point to retrieve.`, {
|
|
1062
|
+
latestLocalCheckpointNumber,
|
|
1063
|
+
pendingCheckpointNumber,
|
|
930
1064
|
});
|
|
931
1065
|
}
|
|
932
1066
|
}
|
|
933
1067
|
|
|
934
1068
|
/** Resumes the archiver after a stop. */
|
|
935
1069
|
public resume() {
|
|
936
|
-
if (!this.runningPromise) {
|
|
937
|
-
throw new Error(`Archiver was never started`);
|
|
938
|
-
}
|
|
939
1070
|
if (this.runningPromise.isRunning()) {
|
|
940
1071
|
this.log.warn(`Archiver already running`);
|
|
941
1072
|
}
|
|
@@ -949,7 +1080,7 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
|
|
|
949
1080
|
*/
|
|
950
1081
|
public async stop(): Promise<void> {
|
|
951
1082
|
this.log.debug('Stopping...');
|
|
952
|
-
await this.runningPromise
|
|
1083
|
+
await this.runningPromise.stop();
|
|
953
1084
|
|
|
954
1085
|
this.log.info('Stopped.');
|
|
955
1086
|
return Promise.resolve();
|
|
@@ -963,6 +1094,10 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
|
|
|
963
1094
|
return Promise.resolve(this.l1constants);
|
|
964
1095
|
}
|
|
965
1096
|
|
|
1097
|
+
public getGenesisValues(): Promise<{ genesisArchiveRoot: Fr }> {
|
|
1098
|
+
return Promise.resolve({ genesisArchiveRoot: this.l1constants.genesisArchiveRoot });
|
|
1099
|
+
}
|
|
1100
|
+
|
|
966
1101
|
public getRollupAddress(): Promise<EthAddress> {
|
|
967
1102
|
return Promise.resolve(this.l1Addresses.rollupAddress);
|
|
968
1103
|
}
|
|
@@ -971,49 +1106,45 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
|
|
|
971
1106
|
return Promise.resolve(this.l1Addresses.registryAddress);
|
|
972
1107
|
}
|
|
973
1108
|
|
|
974
|
-
public getL1BlockNumber(): bigint {
|
|
975
|
-
|
|
976
|
-
if (!l1BlockNumber) {
|
|
977
|
-
throw new Error('L1 block number not yet available. Complete an initial sync first.');
|
|
978
|
-
}
|
|
979
|
-
return l1BlockNumber;
|
|
1109
|
+
public getL1BlockNumber(): bigint | undefined {
|
|
1110
|
+
return this.l1BlockNumber;
|
|
980
1111
|
}
|
|
981
1112
|
|
|
982
|
-
public getL1Timestamp(): Promise<bigint> {
|
|
983
|
-
|
|
984
|
-
if (!l1Timestamp) {
|
|
985
|
-
throw new Error('L1 timestamp not yet available. Complete an initial sync first.');
|
|
986
|
-
}
|
|
987
|
-
return Promise.resolve(l1Timestamp);
|
|
1113
|
+
public getL1Timestamp(): Promise<bigint | undefined> {
|
|
1114
|
+
return Promise.resolve(this.l1Timestamp);
|
|
988
1115
|
}
|
|
989
1116
|
|
|
990
|
-
public
|
|
991
|
-
return
|
|
1117
|
+
public getL2SlotNumber(): Promise<SlotNumber | undefined> {
|
|
1118
|
+
return Promise.resolve(
|
|
1119
|
+
this.l1Timestamp === undefined ? undefined : getSlotAtTimestamp(this.l1Timestamp, this.l1constants),
|
|
1120
|
+
);
|
|
992
1121
|
}
|
|
993
1122
|
|
|
994
|
-
public
|
|
995
|
-
return
|
|
1123
|
+
public getL2EpochNumber(): Promise<EpochNumber | undefined> {
|
|
1124
|
+
return Promise.resolve(
|
|
1125
|
+
this.l1Timestamp === undefined ? undefined : getEpochNumberAtTimestamp(this.l1Timestamp, this.l1constants),
|
|
1126
|
+
);
|
|
996
1127
|
}
|
|
997
1128
|
|
|
998
|
-
public async getBlocksForEpoch(epochNumber:
|
|
1129
|
+
public async getBlocksForEpoch(epochNumber: EpochNumber): Promise<L2Block[]> {
|
|
999
1130
|
const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1constants);
|
|
1000
1131
|
const blocks: L2Block[] = [];
|
|
1001
1132
|
|
|
1002
1133
|
// Walk the list of blocks backwards and filter by slots matching the requested epoch.
|
|
1003
1134
|
// We'll typically ask for blocks for a very recent epoch, so we shouldn't need an index here.
|
|
1004
1135
|
let block = await this.getBlock(await this.store.getSynchedL2BlockNumber());
|
|
1005
|
-
const slot = (b: L2Block) => b.header.globalVariables.slotNumber
|
|
1136
|
+
const slot = (b: L2Block) => b.header.globalVariables.slotNumber;
|
|
1006
1137
|
while (block && slot(block) >= start) {
|
|
1007
1138
|
if (slot(block) <= end) {
|
|
1008
1139
|
blocks.push(block);
|
|
1009
1140
|
}
|
|
1010
|
-
block = await this.getBlock(block.number - 1);
|
|
1141
|
+
block = await this.getBlock(BlockNumber(block.number - 1));
|
|
1011
1142
|
}
|
|
1012
1143
|
|
|
1013
1144
|
return blocks.reverse();
|
|
1014
1145
|
}
|
|
1015
1146
|
|
|
1016
|
-
public async getBlockHeadersForEpoch(epochNumber:
|
|
1147
|
+
public async getBlockHeadersForEpoch(epochNumber: EpochNumber): Promise<BlockHeader[]> {
|
|
1017
1148
|
const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1constants);
|
|
1018
1149
|
const blocks: BlockHeader[] = [];
|
|
1019
1150
|
|
|
@@ -1021,20 +1152,21 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
|
|
|
1021
1152
|
// We'll typically ask for blocks for a very recent epoch, so we shouldn't need an index here.
|
|
1022
1153
|
let number = await this.store.getSynchedL2BlockNumber();
|
|
1023
1154
|
let header = await this.getBlockHeader(number);
|
|
1024
|
-
const slot = (b: BlockHeader) => b.globalVariables.slotNumber
|
|
1155
|
+
const slot = (b: BlockHeader) => b.globalVariables.slotNumber;
|
|
1025
1156
|
while (header && slot(header) >= start) {
|
|
1026
1157
|
if (slot(header) <= end) {
|
|
1027
1158
|
blocks.push(header);
|
|
1028
1159
|
}
|
|
1029
|
-
|
|
1160
|
+
number = BlockNumber(number - 1);
|
|
1161
|
+
header = await this.getBlockHeader(number);
|
|
1030
1162
|
}
|
|
1031
1163
|
return blocks.reverse();
|
|
1032
1164
|
}
|
|
1033
1165
|
|
|
1034
|
-
public async isEpochComplete(epochNumber:
|
|
1166
|
+
public async isEpochComplete(epochNumber: EpochNumber): Promise<boolean> {
|
|
1035
1167
|
// The epoch is complete if the current L2 block is the last one in the epoch (or later)
|
|
1036
1168
|
const header = await this.getBlockHeader('latest');
|
|
1037
|
-
const slot = header
|
|
1169
|
+
const slot = header ? header.globalVariables.slotNumber : undefined;
|
|
1038
1170
|
const [_startSlot, endSlot] = getSlotRangeForEpoch(epochNumber, this.l1constants);
|
|
1039
1171
|
if (slot && slot >= endSlot) {
|
|
1040
1172
|
return true;
|
|
@@ -1064,6 +1196,101 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
|
|
|
1064
1196
|
return this.initialSyncComplete;
|
|
1065
1197
|
}
|
|
1066
1198
|
|
|
1199
|
+
public async getPublishedCheckpoints(
|
|
1200
|
+
from: CheckpointNumber,
|
|
1201
|
+
limit: number,
|
|
1202
|
+
proven?: boolean,
|
|
1203
|
+
): Promise<PublishedCheckpoint[]> {
|
|
1204
|
+
// TODO: Implement this properly. This only works when we have one block per checkpoint.
|
|
1205
|
+
const blocks = await this.getPublishedBlocks(BlockNumber(from), limit, proven);
|
|
1206
|
+
return blocks.map(b => b.toPublishedCheckpoint());
|
|
1207
|
+
}
|
|
1208
|
+
|
|
1209
|
+
public async getCheckpointByArchive(archive: Fr): Promise<Checkpoint | undefined> {
|
|
1210
|
+
// TODO: Implement this properly. This only works when we have one block per checkpoint.
|
|
1211
|
+
return (await this.getPublishedBlockByArchive(archive))?.block.toCheckpoint();
|
|
1212
|
+
}
|
|
1213
|
+
|
|
1214
|
+
public async getCheckpoints(from: CheckpointNumber, limit: number, proven?: boolean): Promise<Checkpoint[]> {
|
|
1215
|
+
const published = await this.getPublishedCheckpoints(from, limit, proven);
|
|
1216
|
+
return published.map(p => p.checkpoint);
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1219
|
+
public async getCheckpoint(number: CheckpointNumber): Promise<Checkpoint | undefined> {
|
|
1220
|
+
if (number < 0) {
|
|
1221
|
+
number = await this.getSynchedCheckpointNumber();
|
|
1222
|
+
}
|
|
1223
|
+
if (number === 0) {
|
|
1224
|
+
return undefined;
|
|
1225
|
+
}
|
|
1226
|
+
const published = await this.getPublishedCheckpoints(number, 1);
|
|
1227
|
+
return published[0]?.checkpoint;
|
|
1228
|
+
}
|
|
1229
|
+
|
|
1230
|
+
public async getCheckpointHeader(number: CheckpointNumber | 'latest'): Promise<CheckpointHeader | undefined> {
|
|
1231
|
+
if (number === 'latest') {
|
|
1232
|
+
number = await this.getSynchedCheckpointNumber();
|
|
1233
|
+
}
|
|
1234
|
+
if (number === 0) {
|
|
1235
|
+
return undefined;
|
|
1236
|
+
}
|
|
1237
|
+
const checkpoint = await this.getCheckpoint(number);
|
|
1238
|
+
return checkpoint?.header;
|
|
1239
|
+
}
|
|
1240
|
+
|
|
1241
|
+
public getCheckpointNumber(): Promise<CheckpointNumber> {
|
|
1242
|
+
return this.getSynchedCheckpointNumber();
|
|
1243
|
+
}
|
|
1244
|
+
|
|
1245
|
+
public async getSynchedCheckpointNumber(): Promise<CheckpointNumber> {
|
|
1246
|
+
// TODO: Create store and apis for checkpoints.
|
|
1247
|
+
// Checkpoint number will no longer be the same as the block number once we support multiple blocks per checkpoint.
|
|
1248
|
+
return CheckpointNumber(await this.store.getSynchedL2BlockNumber());
|
|
1249
|
+
}
|
|
1250
|
+
|
|
1251
|
+
public async getProvenCheckpointNumber(): Promise<CheckpointNumber> {
|
|
1252
|
+
// TODO: Create store and apis for checkpoints.
|
|
1253
|
+
// Proven checkpoint number will no longer be the same as the proven block number once we support multiple blocks per checkpoint.
|
|
1254
|
+
return CheckpointNumber(await this.store.getProvenL2BlockNumber());
|
|
1255
|
+
}
|
|
1256
|
+
|
|
1257
|
+
public setProvenCheckpointNumber(checkpointNumber: CheckpointNumber): Promise<void> {
|
|
1258
|
+
// TODO: Create store and apis for checkpoints.
|
|
1259
|
+
// Proven checkpoint number will no longer be the same as the proven block number once we support multiple blocks per checkpoint.
|
|
1260
|
+
return this.store.setProvenL2BlockNumber(BlockNumber.fromCheckpointNumber(checkpointNumber));
|
|
1261
|
+
}
|
|
1262
|
+
|
|
1263
|
+
public unwindCheckpoints(from: CheckpointNumber, checkpointsToUnwind: number): Promise<boolean> {
|
|
1264
|
+
// TODO: Create store and apis for checkpoints.
|
|
1265
|
+
// This only works when we have one block per checkpoint.
|
|
1266
|
+
return this.store.unwindBlocks(BlockNumber.fromCheckpointNumber(from), checkpointsToUnwind);
|
|
1267
|
+
}
|
|
1268
|
+
|
|
1269
|
+
public getLastBlockNumberInCheckpoint(checkpointNumber: CheckpointNumber): Promise<BlockNumber> {
|
|
1270
|
+
// TODO: Create store and apis for checkpoints.
|
|
1271
|
+
// Checkpoint number will no longer be the same as the block number once we support multiple blocks per checkpoint.
|
|
1272
|
+
return Promise.resolve(BlockNumber.fromCheckpointNumber(checkpointNumber));
|
|
1273
|
+
}
|
|
1274
|
+
|
|
1275
|
+
public addCheckpoints(
|
|
1276
|
+
checkpoints: PublishedCheckpoint[],
|
|
1277
|
+
pendingChainValidationStatus?: ValidateBlockResult,
|
|
1278
|
+
): Promise<boolean> {
|
|
1279
|
+
// TODO: Create store and apis for checkpoints.
|
|
1280
|
+
// This only works when we have one block per checkpoint.
|
|
1281
|
+
return this.store.addBlocks(
|
|
1282
|
+
checkpoints.map(p => PublishedL2Block.fromPublishedCheckpoint(p)),
|
|
1283
|
+
pendingChainValidationStatus,
|
|
1284
|
+
);
|
|
1285
|
+
}
|
|
1286
|
+
|
|
1287
|
+
public async getCheckpointsForEpoch(epochNumber: EpochNumber): Promise<Checkpoint[]> {
|
|
1288
|
+
// TODO: Create store and apis for checkpoints.
|
|
1289
|
+
// This only works when we have one block per checkpoint.
|
|
1290
|
+
const blocks = await this.getBlocksForEpoch(epochNumber);
|
|
1291
|
+
return blocks.map(b => b.toCheckpoint());
|
|
1292
|
+
}
|
|
1293
|
+
|
|
1067
1294
|
/**
|
|
1068
1295
|
* Gets up to `limit` amount of L2 blocks starting from `from`.
|
|
1069
1296
|
* @param from - Number of the first block to return (inclusive).
|
|
@@ -1071,24 +1298,40 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
|
|
|
1071
1298
|
* @param proven - If true, only return blocks that have been proven.
|
|
1072
1299
|
* @returns The requested L2 blocks.
|
|
1073
1300
|
*/
|
|
1074
|
-
public getBlocks(from:
|
|
1301
|
+
public getBlocks(from: BlockNumber, limit: number, proven?: boolean): Promise<L2Block[]> {
|
|
1075
1302
|
return this.getPublishedBlocks(from, limit, proven).then(blocks => blocks.map(b => b.block));
|
|
1076
1303
|
}
|
|
1077
1304
|
|
|
1078
1305
|
/** Equivalent to getBlocks but includes publish data. */
|
|
1079
|
-
public async getPublishedBlocks(from:
|
|
1306
|
+
public async getPublishedBlocks(from: BlockNumber, limit: number, proven?: boolean): Promise<PublishedL2Block[]> {
|
|
1080
1307
|
const limitWithProven = proven
|
|
1081
1308
|
? Math.min(limit, Math.max((await this.store.getProvenL2BlockNumber()) - from + 1, 0))
|
|
1082
1309
|
: limit;
|
|
1083
1310
|
return limitWithProven === 0 ? [] : await this.store.getPublishedBlocks(from, limitWithProven);
|
|
1084
1311
|
}
|
|
1085
1312
|
|
|
1313
|
+
public getPublishedBlockByHash(blockHash: Fr): Promise<PublishedL2Block | undefined> {
|
|
1314
|
+
return this.store.getPublishedBlockByHash(blockHash);
|
|
1315
|
+
}
|
|
1316
|
+
|
|
1317
|
+
public getPublishedBlockByArchive(archive: Fr): Promise<PublishedL2Block | undefined> {
|
|
1318
|
+
return this.store.getPublishedBlockByArchive(archive);
|
|
1319
|
+
}
|
|
1320
|
+
|
|
1321
|
+
public getBlockHeaderByHash(blockHash: Fr): Promise<BlockHeader | undefined> {
|
|
1322
|
+
return this.store.getBlockHeaderByHash(blockHash);
|
|
1323
|
+
}
|
|
1324
|
+
|
|
1325
|
+
public getBlockHeaderByArchive(archive: Fr): Promise<BlockHeader | undefined> {
|
|
1326
|
+
return this.store.getBlockHeaderByArchive(archive);
|
|
1327
|
+
}
|
|
1328
|
+
|
|
1086
1329
|
/**
|
|
1087
1330
|
* Gets an l2 block.
|
|
1088
1331
|
* @param number - The block number to return.
|
|
1089
1332
|
* @returns The requested L2 block.
|
|
1090
1333
|
*/
|
|
1091
|
-
public async getBlock(number:
|
|
1334
|
+
public async getBlock(number: BlockNumber): Promise<L2Block | undefined> {
|
|
1092
1335
|
// If the number provided is -ve, then return the latest block.
|
|
1093
1336
|
if (number < 0) {
|
|
1094
1337
|
number = await this.store.getSynchedL2BlockNumber();
|
|
@@ -1100,7 +1343,7 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
|
|
|
1100
1343
|
return publishedBlock?.block;
|
|
1101
1344
|
}
|
|
1102
1345
|
|
|
1103
|
-
public async getBlockHeader(number:
|
|
1346
|
+
public async getBlockHeader(number: BlockNumber | 'latest'): Promise<BlockHeader | undefined> {
|
|
1104
1347
|
if (number === 'latest') {
|
|
1105
1348
|
number = await this.store.getSynchedL2BlockNumber();
|
|
1106
1349
|
}
|
|
@@ -1119,16 +1362,6 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
|
|
|
1119
1362
|
return this.store.getSettledTxReceipt(txHash);
|
|
1120
1363
|
}
|
|
1121
1364
|
|
|
1122
|
-
/**
|
|
1123
|
-
* Retrieves all private logs from up to `limit` blocks, starting from the block number `from`.
|
|
1124
|
-
* @param from - The block number from which to begin retrieving logs.
|
|
1125
|
-
* @param limit - The maximum number of blocks to retrieve logs from.
|
|
1126
|
-
* @returns An array of private logs from the specified range of blocks.
|
|
1127
|
-
*/
|
|
1128
|
-
public getPrivateLogs(from: number, limit: number): Promise<PrivateLog[]> {
|
|
1129
|
-
return this.store.getPrivateLogs(from, limit);
|
|
1130
|
-
}
|
|
1131
|
-
|
|
1132
1365
|
/**
|
|
1133
1366
|
* Gets all logs that match any of the received tags (i.e. logs with their first field equal to a tag).
|
|
1134
1367
|
* @param tags - The tags to filter the logs by.
|
|
@@ -1161,16 +1394,16 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
|
|
|
1161
1394
|
* Gets the number of the latest L2 block processed by the block source implementation.
|
|
1162
1395
|
* @returns The number of the latest L2 block processed by the block source implementation.
|
|
1163
1396
|
*/
|
|
1164
|
-
public getBlockNumber(): Promise<
|
|
1397
|
+
public getBlockNumber(): Promise<BlockNumber> {
|
|
1165
1398
|
return this.store.getSynchedL2BlockNumber();
|
|
1166
1399
|
}
|
|
1167
1400
|
|
|
1168
|
-
public getProvenBlockNumber(): Promise<
|
|
1401
|
+
public getProvenBlockNumber(): Promise<BlockNumber> {
|
|
1169
1402
|
return this.store.getProvenL2BlockNumber();
|
|
1170
1403
|
}
|
|
1171
1404
|
|
|
1172
1405
|
/** Forcefully updates the last proven block number. Use for testing. */
|
|
1173
|
-
public setProvenBlockNumber(blockNumber:
|
|
1406
|
+
public setProvenBlockNumber(blockNumber: BlockNumber): Promise<void> {
|
|
1174
1407
|
return this.store.setProvenL2BlockNumber(blockNumber);
|
|
1175
1408
|
}
|
|
1176
1409
|
|
|
@@ -1199,12 +1432,12 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
|
|
|
1199
1432
|
}
|
|
1200
1433
|
|
|
1201
1434
|
/**
|
|
1202
|
-
* Gets L1 to L2 message (to be) included in a given
|
|
1203
|
-
* @param
|
|
1435
|
+
* Gets L1 to L2 message (to be) included in a given checkpoint.
|
|
1436
|
+
* @param checkpointNumber - Checkpoint number to get messages for.
|
|
1204
1437
|
* @returns The L1 to L2 messages/leaves of the messages subtree (throws if not found).
|
|
1205
1438
|
*/
|
|
1206
|
-
getL1ToL2Messages(
|
|
1207
|
-
return this.store.getL1ToL2Messages(
|
|
1439
|
+
getL1ToL2Messages(checkpointNumber: CheckpointNumber): Promise<Fr[]> {
|
|
1440
|
+
return this.store.getL1ToL2Messages(checkpointNumber);
|
|
1208
1441
|
}
|
|
1209
1442
|
|
|
1210
1443
|
/**
|
|
@@ -1228,12 +1461,12 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
|
|
|
1228
1461
|
return this.store.getDebugFunctionName(address, selector);
|
|
1229
1462
|
}
|
|
1230
1463
|
|
|
1231
|
-
getPendingChainValidationStatus(): Promise<ValidateBlockResult> {
|
|
1232
|
-
return
|
|
1464
|
+
async getPendingChainValidationStatus(): Promise<ValidateBlockResult> {
|
|
1465
|
+
return (await this.store.getPendingChainValidationStatus()) ?? { valid: true };
|
|
1233
1466
|
}
|
|
1234
1467
|
|
|
1235
1468
|
isPendingChainInvalid(): Promise<boolean> {
|
|
1236
|
-
return
|
|
1469
|
+
return this.getPendingChainValidationStatus().then(status => !status.valid);
|
|
1237
1470
|
}
|
|
1238
1471
|
|
|
1239
1472
|
async getL2Tips(): Promise<L2Tips> {
|
|
@@ -1245,7 +1478,7 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
|
|
|
1245
1478
|
// TODO(#13569): Compute proper finalized block number based on L1 finalized block.
|
|
1246
1479
|
// We just force it 2 epochs worth of proven data for now.
|
|
1247
1480
|
// NOTE: update end-to-end/src/e2e_epochs/epochs_empty_blocks.test.ts as that uses finalized blocks in computations
|
|
1248
|
-
const finalizedBlockNumber = Math.max(provenBlockNumber - this.l1constants.epochDuration * 2, 0);
|
|
1481
|
+
const finalizedBlockNumber = BlockNumber(Math.max(provenBlockNumber - this.l1constants.epochDuration * 2, 0));
|
|
1249
1482
|
|
|
1250
1483
|
const [latestBlockHeader, provenBlockHeader, finalizedBlockHeader] = await Promise.all([
|
|
1251
1484
|
latestBlockNumber > 0 ? this.getBlockHeader(latestBlockNumber) : undefined,
|
|
@@ -1269,27 +1502,18 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
|
|
|
1269
1502
|
);
|
|
1270
1503
|
}
|
|
1271
1504
|
|
|
1272
|
-
const latestBlockHeaderHash = await latestBlockHeader?.hash();
|
|
1273
|
-
const provenBlockHeaderHash = await provenBlockHeader?.hash();
|
|
1274
|
-
const finalizedBlockHeaderHash = await finalizedBlockHeader?.hash();
|
|
1505
|
+
const latestBlockHeaderHash = (await latestBlockHeader?.hash()) ?? GENESIS_BLOCK_HEADER_HASH;
|
|
1506
|
+
const provenBlockHeaderHash = (await provenBlockHeader?.hash()) ?? GENESIS_BLOCK_HEADER_HASH;
|
|
1507
|
+
const finalizedBlockHeaderHash = (await finalizedBlockHeader?.hash()) ?? GENESIS_BLOCK_HEADER_HASH;
|
|
1275
1508
|
|
|
1276
1509
|
return {
|
|
1277
|
-
latest: {
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
} as L2BlockId,
|
|
1281
|
-
proven: {
|
|
1282
|
-
number: provenBlockNumber,
|
|
1283
|
-
hash: provenBlockHeaderHash?.toString(),
|
|
1284
|
-
} as L2BlockId,
|
|
1285
|
-
finalized: {
|
|
1286
|
-
number: finalizedBlockNumber,
|
|
1287
|
-
hash: finalizedBlockHeaderHash?.toString(),
|
|
1288
|
-
} as L2BlockId,
|
|
1510
|
+
latest: { number: latestBlockNumber, hash: latestBlockHeaderHash.toString() },
|
|
1511
|
+
proven: { number: provenBlockNumber, hash: provenBlockHeaderHash.toString() },
|
|
1512
|
+
finalized: { number: finalizedBlockNumber, hash: finalizedBlockHeaderHash.toString() },
|
|
1289
1513
|
};
|
|
1290
1514
|
}
|
|
1291
1515
|
|
|
1292
|
-
public async rollbackTo(targetL2BlockNumber:
|
|
1516
|
+
public async rollbackTo(targetL2BlockNumber: BlockNumber): Promise<void> {
|
|
1293
1517
|
const currentBlocks = await this.getL2Tips();
|
|
1294
1518
|
const currentL2Block = currentBlocks.latest.number;
|
|
1295
1519
|
const currentProvenBlock = currentBlocks.proven.number;
|
|
@@ -1304,17 +1528,18 @@ export class Archiver extends (EventEmitter as new () => ArchiverEmitter) implem
|
|
|
1304
1528
|
throw new Error(`Target L2 block ${targetL2BlockNumber} not found`);
|
|
1305
1529
|
}
|
|
1306
1530
|
const targetL1BlockNumber = targetL2Block.l1.blockNumber;
|
|
1531
|
+
const targetCheckpointNumber = CheckpointNumber.fromBlockNumber(targetL2BlockNumber);
|
|
1307
1532
|
const targetL1BlockHash = await this.getL1BlockHash(targetL1BlockNumber);
|
|
1308
1533
|
this.log.info(`Unwinding ${blocksToUnwind} blocks from L2 block ${currentL2Block}`);
|
|
1309
|
-
await this.store.unwindBlocks(currentL2Block, blocksToUnwind);
|
|
1310
|
-
this.log.info(`Unwinding L1 to L2 messages to ${
|
|
1311
|
-
await this.store.
|
|
1534
|
+
await this.store.unwindBlocks(BlockNumber(currentL2Block), blocksToUnwind);
|
|
1535
|
+
this.log.info(`Unwinding L1 to L2 messages to checkpoint ${targetCheckpointNumber}`);
|
|
1536
|
+
await this.store.rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber);
|
|
1312
1537
|
this.log.info(`Setting L1 syncpoints to ${targetL1BlockNumber}`);
|
|
1313
1538
|
await this.store.setBlockSynchedL1BlockNumber(targetL1BlockNumber);
|
|
1314
1539
|
await this.store.setMessageSynchedL1Block({ l1BlockNumber: targetL1BlockNumber, l1BlockHash: targetL1BlockHash });
|
|
1315
1540
|
if (targetL2BlockNumber < currentProvenBlock) {
|
|
1316
1541
|
this.log.info(`Clearing proven L2 block number`);
|
|
1317
|
-
await this.store.setProvenL2BlockNumber(
|
|
1542
|
+
await this.store.setProvenL2BlockNumber(BlockNumber.ZERO);
|
|
1318
1543
|
}
|
|
1319
1544
|
// TODO(palla/reorg): Set the finalized block when we add support for it.
|
|
1320
1545
|
// if (targetL2BlockNumber < currentFinalizedBlock) {
|
|
@@ -1351,6 +1576,7 @@ export class ArchiverStoreHelper
|
|
|
1351
1576
|
| 'backupTo'
|
|
1352
1577
|
| 'close'
|
|
1353
1578
|
| 'transactionAsync'
|
|
1579
|
+
| 'addBlocks'
|
|
1354
1580
|
>
|
|
1355
1581
|
{
|
|
1356
1582
|
#log = createLogger('archiver:block-helper');
|
|
@@ -1361,7 +1587,7 @@ export class ArchiverStoreHelper
|
|
|
1361
1587
|
* Extracts and stores contract classes out of ContractClassPublished events emitted by the class registry contract.
|
|
1362
1588
|
* @param allLogs - All logs emitted in a bunch of blocks.
|
|
1363
1589
|
*/
|
|
1364
|
-
async #updatePublishedContractClasses(allLogs: ContractClassLog[], blockNum:
|
|
1590
|
+
async #updatePublishedContractClasses(allLogs: ContractClassLog[], blockNum: BlockNumber, operation: Operation) {
|
|
1365
1591
|
const contractClassPublishedEvents = allLogs
|
|
1366
1592
|
.filter(log => ContractClassPublishedEvent.isContractClassPublishedEvent(log))
|
|
1367
1593
|
.map(log => ContractClassPublishedEvent.fromLog(log));
|
|
@@ -1386,7 +1612,7 @@ export class ArchiverStoreHelper
|
|
|
1386
1612
|
* Extracts and stores contract instances out of ContractInstancePublished events emitted by the canonical deployer contract.
|
|
1387
1613
|
* @param allLogs - All logs emitted in a bunch of blocks.
|
|
1388
1614
|
*/
|
|
1389
|
-
async #updateDeployedContractInstances(allLogs: PrivateLog[], blockNum:
|
|
1615
|
+
async #updateDeployedContractInstances(allLogs: PrivateLog[], blockNum: BlockNumber, operation: Operation) {
|
|
1390
1616
|
const contractInstances = allLogs
|
|
1391
1617
|
.filter(log => ContractInstancePublishedEvent.isContractInstancePublishedEvent(log))
|
|
1392
1618
|
.map(log => ContractInstancePublishedEvent.fromLog(log))
|
|
@@ -1439,7 +1665,7 @@ export class ArchiverStoreHelper
|
|
|
1439
1665
|
* @param _blockNum - The block number
|
|
1440
1666
|
* @returns
|
|
1441
1667
|
*/
|
|
1442
|
-
async #storeBroadcastedIndividualFunctions(allLogs: ContractClassLog[], _blockNum:
|
|
1668
|
+
async #storeBroadcastedIndividualFunctions(allLogs: ContractClassLog[], _blockNum: BlockNumber) {
|
|
1443
1669
|
// Filter out private and utility function broadcast events
|
|
1444
1670
|
const privateFnEvents = allLogs
|
|
1445
1671
|
.filter(log => PrivateFunctionBroadcastedEvent.isPrivateFunctionBroadcastedEvent(log))
|
|
@@ -1493,13 +1719,16 @@ export class ArchiverStoreHelper
|
|
|
1493
1719
|
return true;
|
|
1494
1720
|
}
|
|
1495
1721
|
|
|
1496
|
-
public addBlocks(blocks: PublishedL2Block[]): Promise<boolean> {
|
|
1722
|
+
public addBlocks(blocks: PublishedL2Block[], pendingChainValidationStatus?: ValidateBlockResult): Promise<boolean> {
|
|
1497
1723
|
// Add the blocks to the store. Store will throw if the blocks are not in order, there are gaps,
|
|
1498
1724
|
// or if the previous block is not in the store.
|
|
1499
1725
|
return this.store.transactionAsync(async () => {
|
|
1500
1726
|
await this.store.addBlocks(blocks);
|
|
1501
1727
|
|
|
1502
1728
|
const opResults = await Promise.all([
|
|
1729
|
+
// Update the pending chain validation status if provided
|
|
1730
|
+
pendingChainValidationStatus && this.store.setPendingChainValidationStatus(pendingChainValidationStatus),
|
|
1731
|
+
// Add any logs emitted during the retrieved blocks
|
|
1503
1732
|
this.store.addLogs(blocks.map(block => block.block)),
|
|
1504
1733
|
// Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
|
|
1505
1734
|
...blocks.map(async block => {
|
|
@@ -1526,7 +1755,7 @@ export class ArchiverStoreHelper
|
|
|
1526
1755
|
});
|
|
1527
1756
|
}
|
|
1528
1757
|
|
|
1529
|
-
public async unwindBlocks(from:
|
|
1758
|
+
public async unwindBlocks(from: BlockNumber, blocksToUnwind: number): Promise<boolean> {
|
|
1530
1759
|
const last = await this.getSynchedL2BlockNumber();
|
|
1531
1760
|
if (from != last) {
|
|
1532
1761
|
throw new Error(`Cannot unwind blocks from block ${from} when the last block is ${last}`);
|
|
@@ -1536,9 +1765,11 @@ export class ArchiverStoreHelper
|
|
|
1536
1765
|
}
|
|
1537
1766
|
|
|
1538
1767
|
// from - blocksToUnwind = the new head, so + 1 for what we need to remove
|
|
1539
|
-
const blocks = await this.getPublishedBlocks(from - blocksToUnwind + 1, blocksToUnwind);
|
|
1768
|
+
const blocks = await this.getPublishedBlocks(BlockNumber(from - blocksToUnwind + 1), blocksToUnwind);
|
|
1540
1769
|
|
|
1541
1770
|
const opResults = await Promise.all([
|
|
1771
|
+
// Prune rolls back to the last proven block, which is by definition valid
|
|
1772
|
+
this.store.setPendingChainValidationStatus({ valid: true }),
|
|
1542
1773
|
// Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
|
|
1543
1774
|
...blocks.map(async block => {
|
|
1544
1775
|
const contractClassLogs = block.block.body.txEffects.flatMap(txEffect => txEffect.contractClassLogs);
|
|
@@ -1566,15 +1797,27 @@ export class ArchiverStoreHelper
|
|
|
1566
1797
|
return opResults.every(Boolean);
|
|
1567
1798
|
}
|
|
1568
1799
|
|
|
1569
|
-
getPublishedBlocks(from:
|
|
1800
|
+
getPublishedBlocks(from: BlockNumber, limit: number): Promise<PublishedL2Block[]> {
|
|
1570
1801
|
return this.store.getPublishedBlocks(from, limit);
|
|
1571
1802
|
}
|
|
1572
|
-
getPublishedBlock(number:
|
|
1803
|
+
getPublishedBlock(number: BlockNumber): Promise<PublishedL2Block | undefined> {
|
|
1573
1804
|
return this.store.getPublishedBlock(number);
|
|
1574
1805
|
}
|
|
1575
|
-
|
|
1806
|
+
getPublishedBlockByHash(blockHash: Fr): Promise<PublishedL2Block | undefined> {
|
|
1807
|
+
return this.store.getPublishedBlockByHash(blockHash);
|
|
1808
|
+
}
|
|
1809
|
+
getPublishedBlockByArchive(archive: Fr): Promise<PublishedL2Block | undefined> {
|
|
1810
|
+
return this.store.getPublishedBlockByArchive(archive);
|
|
1811
|
+
}
|
|
1812
|
+
getBlockHeaders(from: BlockNumber, limit: number): Promise<BlockHeader[]> {
|
|
1576
1813
|
return this.store.getBlockHeaders(from, limit);
|
|
1577
1814
|
}
|
|
1815
|
+
getBlockHeaderByHash(blockHash: Fr): Promise<BlockHeader | undefined> {
|
|
1816
|
+
return this.store.getBlockHeaderByHash(blockHash);
|
|
1817
|
+
}
|
|
1818
|
+
getBlockHeaderByArchive(archive: Fr): Promise<BlockHeader | undefined> {
|
|
1819
|
+
return this.store.getBlockHeaderByArchive(archive);
|
|
1820
|
+
}
|
|
1578
1821
|
getTxEffect(txHash: TxHash): Promise<IndexedTxEffect | undefined> {
|
|
1579
1822
|
return this.store.getTxEffect(txHash);
|
|
1580
1823
|
}
|
|
@@ -1584,15 +1827,12 @@ export class ArchiverStoreHelper
|
|
|
1584
1827
|
addL1ToL2Messages(messages: InboxMessage[]): Promise<void> {
|
|
1585
1828
|
return this.store.addL1ToL2Messages(messages);
|
|
1586
1829
|
}
|
|
1587
|
-
getL1ToL2Messages(
|
|
1588
|
-
return this.store.getL1ToL2Messages(
|
|
1830
|
+
getL1ToL2Messages(checkpointNumber: CheckpointNumber): Promise<Fr[]> {
|
|
1831
|
+
return this.store.getL1ToL2Messages(checkpointNumber);
|
|
1589
1832
|
}
|
|
1590
1833
|
getL1ToL2MessageIndex(l1ToL2Message: Fr): Promise<bigint | undefined> {
|
|
1591
1834
|
return this.store.getL1ToL2MessageIndex(l1ToL2Message);
|
|
1592
1835
|
}
|
|
1593
|
-
getPrivateLogs(from: number, limit: number): Promise<PrivateLog[]> {
|
|
1594
|
-
return this.store.getPrivateLogs(from, limit);
|
|
1595
|
-
}
|
|
1596
1836
|
getLogsByTags(tags: Fr[], logsPerTag?: number): Promise<TxScopedL2Log[][]> {
|
|
1597
1837
|
return this.store.getLogsByTags(tags, logsPerTag);
|
|
1598
1838
|
}
|
|
@@ -1602,13 +1842,13 @@ export class ArchiverStoreHelper
|
|
|
1602
1842
|
getContractClassLogs(filter: LogFilter): Promise<GetContractClassLogsResponse> {
|
|
1603
1843
|
return this.store.getContractClassLogs(filter);
|
|
1604
1844
|
}
|
|
1605
|
-
getSynchedL2BlockNumber(): Promise<
|
|
1845
|
+
getSynchedL2BlockNumber(): Promise<BlockNumber> {
|
|
1606
1846
|
return this.store.getSynchedL2BlockNumber();
|
|
1607
1847
|
}
|
|
1608
|
-
getProvenL2BlockNumber(): Promise<
|
|
1848
|
+
getProvenL2BlockNumber(): Promise<BlockNumber> {
|
|
1609
1849
|
return this.store.getProvenL2BlockNumber();
|
|
1610
1850
|
}
|
|
1611
|
-
setProvenL2BlockNumber(l2BlockNumber:
|
|
1851
|
+
setProvenL2BlockNumber(l2BlockNumber: BlockNumber): Promise<void> {
|
|
1612
1852
|
return this.store.setProvenL2BlockNumber(l2BlockNumber);
|
|
1613
1853
|
}
|
|
1614
1854
|
setBlockSynchedL1BlockNumber(l1BlockNumber: bigint): Promise<void> {
|
|
@@ -1644,8 +1884,8 @@ export class ArchiverStoreHelper
|
|
|
1644
1884
|
estimateSize(): Promise<{ mappingSize: number; physicalFileSize: number; actualSize: number; numItems: number }> {
|
|
1645
1885
|
return this.store.estimateSize();
|
|
1646
1886
|
}
|
|
1647
|
-
|
|
1648
|
-
return this.store.
|
|
1887
|
+
rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber: CheckpointNumber): Promise<void> {
|
|
1888
|
+
return this.store.rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber);
|
|
1649
1889
|
}
|
|
1650
1890
|
iterateL1ToL2Messages(range: CustomRange<bigint> = {}): AsyncIterableIterator<InboxMessage> {
|
|
1651
1891
|
return this.store.iterateL1ToL2Messages(range);
|
|
@@ -1656,4 +1896,11 @@ export class ArchiverStoreHelper
|
|
|
1656
1896
|
getLastL1ToL2Message(): Promise<InboxMessage | undefined> {
|
|
1657
1897
|
return this.store.getLastL1ToL2Message();
|
|
1658
1898
|
}
|
|
1899
|
+
getPendingChainValidationStatus(): Promise<ValidateBlockResult | undefined> {
|
|
1900
|
+
return this.store.getPendingChainValidationStatus();
|
|
1901
|
+
}
|
|
1902
|
+
setPendingChainValidationStatus(status: ValidateBlockResult | undefined): Promise<void> {
|
|
1903
|
+
this.#log.debug(`Setting pending chain validation status to valid ${status?.valid}`, status);
|
|
1904
|
+
return this.store.setPendingChainValidationStatus(status);
|
|
1905
|
+
}
|
|
1659
1906
|
}
|