@aztec/archiver 0.0.1-commit.d3ec352c → 0.0.1-commit.fcb71a6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dest/archiver/archiver.d.ts +84 -70
- package/dest/archiver/archiver.d.ts.map +1 -1
- package/dest/archiver/archiver.js +439 -228
- package/dest/archiver/archiver_store.d.ts +95 -43
- 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 +1847 -366
- package/dest/archiver/config.d.ts +5 -4
- package/dest/archiver/config.d.ts.map +1 -1
- package/dest/archiver/config.js +10 -3
- package/dest/archiver/errors.d.ts +25 -1
- package/dest/archiver/errors.d.ts.map +1 -1
- package/dest/archiver/errors.js +37 -0
- package/dest/archiver/index.d.ts +2 -2
- package/dest/archiver/index.d.ts.map +1 -1
- package/dest/archiver/instrumentation.d.ts +3 -1
- package/dest/archiver/instrumentation.d.ts.map +1 -1
- package/dest/archiver/instrumentation.js +11 -0
- package/dest/archiver/kv_archiver_store/block_store.d.ts +50 -18
- package/dest/archiver/kv_archiver_store/block_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/block_store.js +320 -84
- 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 +40 -51
- package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/kv_archiver_store.js +65 -48
- package/dest/archiver/kv_archiver_store/log_store.d.ts +12 -16
- package/dest/archiver/kv_archiver_store/log_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/log_store.js +148 -84
- 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 +149 -0
- package/dest/archiver/l1/calldata_retriever.d.ts +112 -0
- package/dest/archiver/l1/calldata_retriever.d.ts.map +1 -0
- package/dest/archiver/l1/calldata_retriever.js +471 -0
- package/dest/archiver/l1/data_retrieval.d.ts +90 -0
- package/dest/archiver/l1/data_retrieval.d.ts.map +1 -0
- package/dest/archiver/{data_retrieval.js → l1/data_retrieval.js} +50 -106
- 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/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 -6
- package/dest/archiver/structs/published.d.ts +1 -2
- package/dest/archiver/structs/published.d.ts.map +1 -1
- package/dest/factory.d.ts +1 -1
- package/dest/factory.js +1 -1
- package/dest/index.d.ts +2 -2
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +1 -1
- package/dest/test/mock_archiver.d.ts +4 -5
- package/dest/test/mock_archiver.d.ts.map +1 -1
- package/dest/test/mock_archiver.js +5 -9
- package/dest/test/mock_l1_to_l2_message_source.d.ts +5 -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 +7 -11
- package/dest/test/mock_l2_block_source.d.ts +11 -4
- package/dest/test/mock_l2_block_source.d.ts.map +1 -1
- package/dest/test/mock_l2_block_source.js +18 -17
- 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 -9
- package/package.json +15 -14
- package/src/archiver/archiver.ts +567 -290
- package/src/archiver/archiver_store.ts +104 -42
- package/src/archiver/archiver_store_test_suite.ts +1895 -347
- package/src/archiver/config.ts +15 -10
- package/src/archiver/errors.ts +64 -0
- package/src/archiver/index.ts +1 -1
- package/src/archiver/instrumentation.ts +14 -0
- package/src/archiver/kv_archiver_store/block_store.ts +435 -95
- 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 +81 -66
- package/src/archiver/kv_archiver_store/log_store.ts +208 -99
- 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 +641 -0
- package/src/archiver/{data_retrieval.ts → l1/data_retrieval.ts} +96 -161
- 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 +7 -8
- package/src/archiver/structs/published.ts +0 -1
- package/src/factory.ts +1 -1
- 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 +6 -11
- package/src/test/mock_l1_to_l2_message_source.ts +6 -11
- package/src/test/mock_l2_block_source.ts +22 -18
- package/src/test/mock_structs.ts +10 -10
- package/dest/archiver/data_retrieval.d.ts +0 -80
- package/dest/archiver/data_retrieval.d.ts.map +0 -1
|
@@ -6,36 +6,42 @@ function _ts_decorate(decorators, target, key, desc) {
|
|
|
6
6
|
}
|
|
7
7
|
import { GENESIS_BLOCK_HEADER_HASH } from '@aztec/constants';
|
|
8
8
|
import { EpochCache } from '@aztec/epoch-cache';
|
|
9
|
-
import {
|
|
9
|
+
import { createEthereumChain } from '@aztec/ethereum/chain';
|
|
10
|
+
import { BlockTagTooOldError, InboxContract, RollupContract } from '@aztec/ethereum/contracts';
|
|
10
11
|
import { maxBigint } from '@aztec/foundation/bigint';
|
|
11
12
|
import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
|
|
12
13
|
import { Buffer16, Buffer32 } from '@aztec/foundation/buffer';
|
|
13
14
|
import { merge, pick } from '@aztec/foundation/collection';
|
|
14
|
-
import { Fr } from '@aztec/foundation/
|
|
15
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
15
16
|
import { createLogger } from '@aztec/foundation/log';
|
|
16
17
|
import { promiseWithResolvers } from '@aztec/foundation/promise';
|
|
17
18
|
import { RunningPromise, makeLoggingErrorHandler } from '@aztec/foundation/running-promise';
|
|
18
19
|
import { count } from '@aztec/foundation/string';
|
|
19
20
|
import { DateProvider, Timer, elapsed } from '@aztec/foundation/timer';
|
|
21
|
+
import { isDefined } from '@aztec/foundation/types';
|
|
20
22
|
import { ContractClassPublishedEvent, PrivateFunctionBroadcastedEvent, UtilityFunctionBroadcastedEvent } from '@aztec/protocol-contracts/class-registry';
|
|
21
23
|
import { ContractInstancePublishedEvent, ContractInstanceUpdatedEvent } from '@aztec/protocol-contracts/instance-registry';
|
|
22
|
-
import { L2Block, L2BlockSourceEvents, PublishedL2Block } from '@aztec/stdlib/block';
|
|
24
|
+
import { CommitteeAttestation, L2Block, L2BlockSourceEvents, PublishedL2Block } from '@aztec/stdlib/block';
|
|
25
|
+
import { Checkpoint, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
|
|
23
26
|
import { computePublicBytecodeCommitment, isValidPrivateFunctionMembershipProof, isValidUtilityFunctionMembershipProof } from '@aztec/stdlib/contract';
|
|
24
27
|
import { getEpochAtSlot, getEpochNumberAtTimestamp, getSlotAtTimestamp, getSlotRangeForEpoch, getTimestampRangeForEpoch } from '@aztec/stdlib/epoch-helpers';
|
|
28
|
+
import { computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
|
|
25
29
|
import { getTelemetryClient, trackSpan } from '@aztec/telemetry-client';
|
|
26
30
|
import { EventEmitter } from 'events';
|
|
27
31
|
import groupBy from 'lodash.groupby';
|
|
28
32
|
import { createPublicClient, fallback, http } from 'viem';
|
|
29
|
-
import {
|
|
30
|
-
import { InitialBlockNumberNotSequentialError, NoBlobBodiesFoundError } from './errors.js';
|
|
33
|
+
import { InitialCheckpointNumberNotSequentialError, NoBlobBodiesFoundError } from './errors.js';
|
|
31
34
|
import { ArchiverInstrumentation } from './instrumentation.js';
|
|
35
|
+
import { retrieveCheckpointsFromRollup, retrieveL1ToL2Message, retrieveL1ToL2Messages, retrievedToPublishedCheckpoint } from './l1/data_retrieval.js';
|
|
36
|
+
import { validateAndLogTraceAvailability } from './l1/validate_trace.js';
|
|
32
37
|
import { validateCheckpointAttestations } from './validation.js';
|
|
33
38
|
function mapArchiverConfig(config) {
|
|
34
39
|
return {
|
|
35
40
|
pollingIntervalMs: config.archiverPollingIntervalMS,
|
|
36
41
|
batchSize: config.archiverBatchSize,
|
|
37
42
|
skipValidateBlockAttestations: config.skipValidateBlockAttestations,
|
|
38
|
-
maxAllowedEthClientDriftSeconds: config.maxAllowedEthClientDriftSeconds
|
|
43
|
+
maxAllowedEthClientDriftSeconds: config.maxAllowedEthClientDriftSeconds,
|
|
44
|
+
ethereumAllowNoDebugHosts: config.ethereumAllowNoDebugHosts
|
|
39
45
|
};
|
|
40
46
|
}
|
|
41
47
|
/**
|
|
@@ -44,10 +50,11 @@ function mapArchiverConfig(config) {
|
|
|
44
50
|
* concern themselves with it.
|
|
45
51
|
*/ export class Archiver extends EventEmitter {
|
|
46
52
|
publicClient;
|
|
53
|
+
debugClient;
|
|
47
54
|
l1Addresses;
|
|
48
55
|
dataStore;
|
|
49
56
|
config;
|
|
50
|
-
|
|
57
|
+
blobClient;
|
|
51
58
|
epochCache;
|
|
52
59
|
dateProvider;
|
|
53
60
|
instrumentation;
|
|
@@ -61,18 +68,20 @@ function mapArchiverConfig(config) {
|
|
|
61
68
|
l1Timestamp;
|
|
62
69
|
initialSyncComplete;
|
|
63
70
|
initialSyncPromise;
|
|
71
|
+
/** Queue of blocks to be added to the store, processed by the sync loop. */ blockQueue;
|
|
64
72
|
tracer;
|
|
65
73
|
/**
|
|
66
74
|
* Creates a new instance of the Archiver.
|
|
67
75
|
* @param publicClient - A client for interacting with the Ethereum node.
|
|
76
|
+
* @param debugClient - A client for interacting with the Ethereum node for debug/trace methods.
|
|
68
77
|
* @param rollupAddress - Ethereum address of the rollup contract.
|
|
69
78
|
* @param inboxAddress - Ethereum address of the inbox contract.
|
|
70
79
|
* @param registryAddress - Ethereum address of the registry contract.
|
|
71
80
|
* @param pollingIntervalMs - The interval for polling for L1 logs (in milliseconds).
|
|
72
81
|
* @param store - An archiver data store for storage & retrieval of blocks, encrypted logs & contract data.
|
|
73
82
|
* @param log - A logger.
|
|
74
|
-
*/ constructor(publicClient, l1Addresses, dataStore, config,
|
|
75
|
-
super(), this.publicClient = publicClient, this.l1Addresses = l1Addresses, this.dataStore = dataStore, this.config = config, this.
|
|
83
|
+
*/ constructor(publicClient, debugClient, l1Addresses, dataStore, config, blobClient, epochCache, dateProvider, instrumentation, l1constants, log = createLogger('archiver')){
|
|
84
|
+
super(), this.publicClient = publicClient, this.debugClient = debugClient, this.l1Addresses = l1Addresses, this.dataStore = dataStore, this.config = config, this.blobClient = blobClient, this.epochCache = epochCache, this.dateProvider = dateProvider, this.instrumentation = instrumentation, this.l1constants = l1constants, this.log = log, this.initialSyncComplete = false, this.blockQueue = [];
|
|
76
85
|
this.tracer = instrumentation.tracer;
|
|
77
86
|
this.store = new ArchiverStoreHelper(dataStore);
|
|
78
87
|
this.rollup = new RollupContract(publicClient, l1Addresses.rollupAddress);
|
|
@@ -92,15 +101,27 @@ function mapArchiverConfig(config) {
|
|
|
92
101
|
const chain = createEthereumChain(config.l1RpcUrls, config.l1ChainId);
|
|
93
102
|
const publicClient = createPublicClient({
|
|
94
103
|
chain: chain.chainInfo,
|
|
95
|
-
transport: fallback(config.l1RpcUrls.map((url)=>http(url
|
|
104
|
+
transport: fallback(config.l1RpcUrls.map((url)=>http(url, {
|
|
105
|
+
batch: false
|
|
106
|
+
}))),
|
|
107
|
+
pollingInterval: config.viemPollingIntervalMS
|
|
108
|
+
});
|
|
109
|
+
// Create debug client using debug RPC URLs if available, otherwise fall back to regular RPC URLs
|
|
110
|
+
const debugRpcUrls = config.l1DebugRpcUrls.length > 0 ? config.l1DebugRpcUrls : config.l1RpcUrls;
|
|
111
|
+
const debugClient = createPublicClient({
|
|
112
|
+
chain: chain.chainInfo,
|
|
113
|
+
transport: fallback(debugRpcUrls.map((url)=>http(url, {
|
|
114
|
+
batch: false
|
|
115
|
+
}))),
|
|
96
116
|
pollingInterval: config.viemPollingIntervalMS
|
|
97
117
|
});
|
|
98
118
|
const rollup = new RollupContract(publicClient, config.l1Contracts.rollupAddress);
|
|
99
|
-
const [l1StartBlock, l1GenesisTime, proofSubmissionEpochs, genesisArchiveRoot] = await Promise.all([
|
|
119
|
+
const [l1StartBlock, l1GenesisTime, proofSubmissionEpochs, genesisArchiveRoot, slashingProposerAddress] = await Promise.all([
|
|
100
120
|
rollup.getL1StartBlock(),
|
|
101
121
|
rollup.getL1GenesisTime(),
|
|
102
122
|
rollup.getProofSubmissionEpochs(),
|
|
103
|
-
rollup.getGenesisArchiveTreeRoot()
|
|
123
|
+
rollup.getGenesisArchiveTreeRoot(),
|
|
124
|
+
rollup.getSlashingProposerAddress()
|
|
104
125
|
]);
|
|
105
126
|
const l1StartBlockHash = await publicClient.getBlock({
|
|
106
127
|
blockNumber: l1StartBlock,
|
|
@@ -120,11 +141,15 @@ function mapArchiverConfig(config) {
|
|
|
120
141
|
const opts = merge({
|
|
121
142
|
pollingIntervalMs: 10_000,
|
|
122
143
|
batchSize: 100,
|
|
123
|
-
maxAllowedEthClientDriftSeconds: 300
|
|
144
|
+
maxAllowedEthClientDriftSeconds: 300,
|
|
145
|
+
ethereumAllowNoDebugHosts: false
|
|
124
146
|
}, mapArchiverConfig(config));
|
|
125
147
|
const epochCache = deps.epochCache ?? await EpochCache.create(config.l1Contracts.rollupAddress, config, deps);
|
|
126
148
|
const telemetry = deps.telemetry ?? getTelemetryClient();
|
|
127
|
-
const archiver = new Archiver(publicClient,
|
|
149
|
+
const archiver = new Archiver(publicClient, debugClient, {
|
|
150
|
+
...config.l1Contracts,
|
|
151
|
+
slashingProposerAddress
|
|
152
|
+
}, archiverStore, opts, deps.blobClient, epochCache, deps.dateProvider ?? new DateProvider(), await ArchiverInstrumentation.new(telemetry, ()=>archiverStore.estimateSize()), l1Constants);
|
|
128
153
|
await archiver.start(blockUntilSynced);
|
|
129
154
|
return archiver;
|
|
130
155
|
}
|
|
@@ -138,16 +163,17 @@ function mapArchiverConfig(config) {
|
|
|
138
163
|
if (this.runningPromise.isRunning()) {
|
|
139
164
|
throw new Error('Archiver is already running');
|
|
140
165
|
}
|
|
141
|
-
await this.
|
|
166
|
+
await this.blobClient.testSources();
|
|
142
167
|
await this.testEthereumNodeSynced();
|
|
168
|
+
await validateAndLogTraceAvailability(this.debugClient, this.config.ethereumAllowNoDebugHosts ?? false);
|
|
143
169
|
// Log initial state for the archiver
|
|
144
170
|
const { l1StartBlock } = this.l1constants;
|
|
145
171
|
const { blocksSynchedTo = l1StartBlock, messagesSynchedTo = l1StartBlock } = await this.store.getSynchPoint();
|
|
146
|
-
const
|
|
147
|
-
this.log.info(`Starting archiver sync to rollup contract ${this.l1Addresses.rollupAddress.toString()} from L1 block ${blocksSynchedTo} and L2
|
|
172
|
+
const currentL2Checkpoint = await this.getSynchedCheckpointNumber();
|
|
173
|
+
this.log.info(`Starting archiver sync to rollup contract ${this.l1Addresses.rollupAddress.toString()} from L1 block ${blocksSynchedTo} and L2 checkpoint ${currentL2Checkpoint}`, {
|
|
148
174
|
blocksSynchedTo,
|
|
149
175
|
messagesSynchedTo,
|
|
150
|
-
|
|
176
|
+
currentL2Checkpoint
|
|
151
177
|
});
|
|
152
178
|
// Start sync loop, and return the wait for initial sync if we are asked to block until synced
|
|
153
179
|
this.runningPromise.start();
|
|
@@ -158,6 +184,51 @@ function mapArchiverConfig(config) {
|
|
|
158
184
|
syncImmediate() {
|
|
159
185
|
return this.runningPromise.trigger();
|
|
160
186
|
}
|
|
187
|
+
/**
|
|
188
|
+
* Queues a block to be added to the archiver store and triggers processing.
|
|
189
|
+
* The block will be processed by the sync loop.
|
|
190
|
+
* Implements the L2BlockSink interface.
|
|
191
|
+
* @param block - The L2 block to add.
|
|
192
|
+
* @returns A promise that resolves when the block has been added to the store, or rejects on error.
|
|
193
|
+
*/ addBlock(block) {
|
|
194
|
+
return new Promise((resolve, reject)=>{
|
|
195
|
+
this.blockQueue.push({
|
|
196
|
+
block,
|
|
197
|
+
resolve,
|
|
198
|
+
reject
|
|
199
|
+
});
|
|
200
|
+
this.log.debug(`Queued block ${block.number} for processing`);
|
|
201
|
+
// Trigger an immediate sync, but don't wait for it - the promise resolves when the block is processed
|
|
202
|
+
this.syncImmediate().catch((err)=>{
|
|
203
|
+
this.log.error(`Sync immediate call failed: ${err}`);
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Processes all queued blocks, adding them to the store.
|
|
209
|
+
* Called at the beginning of each sync iteration.
|
|
210
|
+
* Blocks are processed in the order they were queued.
|
|
211
|
+
*/ async processQueuedBlocks() {
|
|
212
|
+
if (this.blockQueue.length === 0) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
// Take all blocks from the queue
|
|
216
|
+
const queuedItems = this.blockQueue.splice(0, this.blockQueue.length);
|
|
217
|
+
this.log.debug(`Processing ${queuedItems.length} queued block(s)`);
|
|
218
|
+
// Process each block individually to properly resolve/reject each promise
|
|
219
|
+
for (const { block, resolve, reject } of queuedItems){
|
|
220
|
+
try {
|
|
221
|
+
await this.store.addBlocks([
|
|
222
|
+
block
|
|
223
|
+
]);
|
|
224
|
+
this.log.debug(`Added block ${block.number} to store`);
|
|
225
|
+
resolve();
|
|
226
|
+
} catch (err) {
|
|
227
|
+
this.log.error(`Failed to add block ${block.number} to store: ${err.message}`);
|
|
228
|
+
reject(err);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
161
232
|
waitForInitialSync() {
|
|
162
233
|
return this.initialSyncPromise.promise;
|
|
163
234
|
}
|
|
@@ -174,9 +245,7 @@ function mapArchiverConfig(config) {
|
|
|
174
245
|
throw new Error(`Ethereum node is out of sync (last block synced ${number} at ${l1Timestamp} vs current time ${currentTime})`);
|
|
175
246
|
}
|
|
176
247
|
}
|
|
177
|
-
|
|
178
|
-
* Fetches logs from L1 contracts and processes them.
|
|
179
|
-
*/ async sync() {
|
|
248
|
+
async syncFromL1() {
|
|
180
249
|
/**
|
|
181
250
|
* We keep track of three "pointers" to L1 blocks:
|
|
182
251
|
* 1. the last L1 block that published an L2 block
|
|
@@ -250,7 +319,7 @@ function mapArchiverConfig(config) {
|
|
|
250
319
|
// past it, since otherwise we'll keep downloading it and reprocessing it on every iteration until
|
|
251
320
|
// we get a valid checkpoint to advance the syncpoint.
|
|
252
321
|
if (!rollupStatus.validationResult?.valid && rollupStatus.lastL1BlockWithCheckpoint !== undefined) {
|
|
253
|
-
await this.store.
|
|
322
|
+
await this.store.setCheckpointSynchedL1BlockNumber(rollupStatus.lastL1BlockWithCheckpoint);
|
|
254
323
|
}
|
|
255
324
|
// And lastly we check if we are missing any checkpoints behind us due to a possible L1 reorg.
|
|
256
325
|
// We only do this if rollup cant prune on the next submission. Otherwise we will end up
|
|
@@ -279,6 +348,14 @@ function mapArchiverConfig(config) {
|
|
|
279
348
|
this.initialSyncPromise.resolve();
|
|
280
349
|
}
|
|
281
350
|
}
|
|
351
|
+
/**
|
|
352
|
+
* Fetches logs from L1 contracts and processes them.
|
|
353
|
+
*/ async sync() {
|
|
354
|
+
// Process any queued blocks first, before doing L1 sync
|
|
355
|
+
await this.processQueuedBlocks();
|
|
356
|
+
// Now perform L1 sync
|
|
357
|
+
await this.syncFromL1();
|
|
358
|
+
}
|
|
282
359
|
/** Queries the rollup contract on whether a prune can be executed on the immediate next L1 block. */ async canPrune(currentL1BlockNumber, currentL1Timestamp) {
|
|
283
360
|
const time = (currentL1Timestamp ?? 0n) + BigInt(this.l1constants.ethereumSlotDuration);
|
|
284
361
|
const result = await this.rollup.canPruneAtTime(time, {
|
|
@@ -307,12 +384,19 @@ function mapArchiverConfig(config) {
|
|
|
307
384
|
const pruneFromSlotNumber = header.slotNumber;
|
|
308
385
|
const pruneFromEpochNumber = getEpochAtSlot(pruneFromSlotNumber, this.l1constants);
|
|
309
386
|
const checkpointsToUnwind = localPendingCheckpointNumber - provenCheckpointNumber;
|
|
310
|
-
const
|
|
387
|
+
const checkpointPromises = Array.from({
|
|
388
|
+
length: checkpointsToUnwind
|
|
389
|
+
}).fill(0).map((_, i)=>this.store.getCheckpointData(CheckpointNumber(i + pruneFrom)));
|
|
390
|
+
const checkpoints = await Promise.all(checkpointPromises);
|
|
391
|
+
const blockPromises = await Promise.all(checkpoints.filter(isDefined).map((cp)=>this.store.getBlocksForCheckpoint(CheckpointNumber(cp.checkpointNumber))));
|
|
392
|
+
const newBlocks = blockPromises.filter(isDefined).flat();
|
|
393
|
+
// TODO(pw/mbps): Don't convert to legacy blocks here
|
|
394
|
+
const blocks = (await Promise.all(newBlocks.map((x)=>this.getBlock(x.number)))).filter(isDefined);
|
|
311
395
|
// Emit an event for listening services to react to the chain prune
|
|
312
396
|
this.emit(L2BlockSourceEvents.L2PruneDetected, {
|
|
313
397
|
type: L2BlockSourceEvents.L2PruneDetected,
|
|
314
398
|
epochNumber: pruneFromEpochNumber,
|
|
315
|
-
blocks
|
|
399
|
+
blocks
|
|
316
400
|
});
|
|
317
401
|
this.log.debug(`L2 prune from ${provenCheckpointNumber + 1} to ${localPendingCheckpointNumber} will occur on next checkpoint submission.`);
|
|
318
402
|
await this.unwindCheckpoints(localPendingCheckpointNumber, checkpointsToUnwind);
|
|
@@ -408,7 +492,7 @@ function mapArchiverConfig(config) {
|
|
|
408
492
|
}while (searchEndBlock < currentL1BlockNumber)
|
|
409
493
|
// Log stats for messages retrieved (if any).
|
|
410
494
|
if (messageCount > 0) {
|
|
411
|
-
this.log.info(`Retrieved ${messageCount} new L1 to L2 messages up to message with index ${lastMessage?.index} for
|
|
495
|
+
this.log.info(`Retrieved ${messageCount} new L1 to L2 messages up to message with index ${lastMessage?.index} for checkpoint ${lastMessage?.checkpointNumber}`, {
|
|
412
496
|
lastMessage,
|
|
413
497
|
messageCount
|
|
414
498
|
});
|
|
@@ -520,15 +604,14 @@ function mapArchiverConfig(config) {
|
|
|
520
604
|
});
|
|
521
605
|
}
|
|
522
606
|
}
|
|
523
|
-
const localCheckpointForDestinationProvenCheckpointNumber = await this.
|
|
607
|
+
const localCheckpointForDestinationProvenCheckpointNumber = await this.store.getCheckpointData(provenCheckpointNumber);
|
|
524
608
|
// Sanity check. I've hit what seems to be a state where the proven checkpoint is set to a value greater than the latest
|
|
525
609
|
// synched checkpoint when requesting L2Tips from the archiver. This is the only place where the proven checkpoint is set.
|
|
526
610
|
const synched = await this.getSynchedCheckpointNumber();
|
|
527
|
-
if (localCheckpointForDestinationProvenCheckpointNumber && synched < localCheckpointForDestinationProvenCheckpointNumber.
|
|
528
|
-
this.log.error(`Hit local checkpoint greater than last synched checkpoint: ${localCheckpointForDestinationProvenCheckpointNumber.
|
|
611
|
+
if (localCheckpointForDestinationProvenCheckpointNumber && synched < localCheckpointForDestinationProvenCheckpointNumber.checkpointNumber) {
|
|
612
|
+
this.log.error(`Hit local checkpoint greater than last synched checkpoint: ${localCheckpointForDestinationProvenCheckpointNumber.checkpointNumber} > ${synched}`);
|
|
529
613
|
}
|
|
530
614
|
this.log.trace(`Local checkpoint for remote proven checkpoint ${provenCheckpointNumber} is ${localCheckpointForDestinationProvenCheckpointNumber?.archive.root.toString() ?? 'undefined'}`);
|
|
531
|
-
const lastProvenBlockNumber = await this.getLastBlockNumberInCheckpoint(provenCheckpointNumber);
|
|
532
615
|
if (localCheckpointForDestinationProvenCheckpointNumber && provenArchive === localCheckpointForDestinationProvenCheckpointNumber.archive.root.toString()) {
|
|
533
616
|
const localProvenCheckpointNumber = await this.getProvenCheckpointNumber();
|
|
534
617
|
if (localProvenCheckpointNumber !== provenCheckpointNumber) {
|
|
@@ -538,23 +621,24 @@ function mapArchiverConfig(config) {
|
|
|
538
621
|
});
|
|
539
622
|
const provenSlotNumber = localCheckpointForDestinationProvenCheckpointNumber.header.slotNumber;
|
|
540
623
|
const provenEpochNumber = getEpochAtSlot(provenSlotNumber, this.l1constants);
|
|
624
|
+
const lastBlockNumberInCheckpoint = localCheckpointForDestinationProvenCheckpointNumber.startBlock + localCheckpointForDestinationProvenCheckpointNumber.numBlocks - 1;
|
|
541
625
|
this.emit(L2BlockSourceEvents.L2BlockProven, {
|
|
542
626
|
type: L2BlockSourceEvents.L2BlockProven,
|
|
543
|
-
blockNumber:
|
|
627
|
+
blockNumber: BlockNumber(lastBlockNumberInCheckpoint),
|
|
544
628
|
slotNumber: provenSlotNumber,
|
|
545
629
|
epochNumber: provenEpochNumber
|
|
546
630
|
});
|
|
631
|
+
this.instrumentation.updateLastProvenBlock(lastBlockNumberInCheckpoint);
|
|
547
632
|
} else {
|
|
548
633
|
this.log.trace(`Proven checkpoint ${provenCheckpointNumber} already stored.`);
|
|
549
634
|
}
|
|
550
635
|
}
|
|
551
|
-
this.instrumentation.updateLastProvenBlock(lastProvenBlockNumber);
|
|
552
636
|
};
|
|
553
637
|
// This is an edge case that we only hit if there are no proposed checkpoints.
|
|
554
638
|
// If we have 0 checkpoints locally and there are no checkpoints onchain there is nothing to do.
|
|
555
639
|
const noCheckpoints = localPendingCheckpointNumber === 0 && pendingCheckpointNumber === 0;
|
|
556
640
|
if (noCheckpoints) {
|
|
557
|
-
await this.store.
|
|
641
|
+
await this.store.setCheckpointSynchedL1BlockNumber(currentL1BlockNumber);
|
|
558
642
|
this.log.debug(`No checkpoints to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}, no checkpoints on chain`);
|
|
559
643
|
return rollupStatus;
|
|
560
644
|
}
|
|
@@ -562,7 +646,7 @@ function mapArchiverConfig(config) {
|
|
|
562
646
|
// Related to the L2 reorgs of the pending chain. We are only interested in actually addressing a reorg if there
|
|
563
647
|
// are any state that could be impacted by it. If we have no checkpoints, there is no impact.
|
|
564
648
|
if (localPendingCheckpointNumber > 0) {
|
|
565
|
-
const localPendingCheckpoint = await this.
|
|
649
|
+
const localPendingCheckpoint = await this.store.getCheckpointData(localPendingCheckpointNumber);
|
|
566
650
|
if (localPendingCheckpoint === undefined) {
|
|
567
651
|
throw new Error(`Missing checkpoint ${localPendingCheckpointNumber}`);
|
|
568
652
|
}
|
|
@@ -593,12 +677,12 @@ function mapArchiverConfig(config) {
|
|
|
593
677
|
});
|
|
594
678
|
let tipAfterUnwind = localPendingCheckpointNumber;
|
|
595
679
|
while(true){
|
|
596
|
-
const candidateCheckpoint = await this.
|
|
680
|
+
const candidateCheckpoint = await this.store.getCheckpointData(tipAfterUnwind);
|
|
597
681
|
if (candidateCheckpoint === undefined) {
|
|
598
682
|
break;
|
|
599
683
|
}
|
|
600
|
-
const archiveAtContract = await this.rollup.archiveAt(candidateCheckpoint.
|
|
601
|
-
this.log.trace(`Checking local checkpoint ${candidateCheckpoint.
|
|
684
|
+
const archiveAtContract = await this.rollup.archiveAt(candidateCheckpoint.checkpointNumber);
|
|
685
|
+
this.log.trace(`Checking local checkpoint ${candidateCheckpoint.checkpointNumber} with archive ${candidateCheckpoint.archive.root}`, {
|
|
602
686
|
archiveAtContract,
|
|
603
687
|
archiveLocal: candidateCheckpoint.archive.root.toString()
|
|
604
688
|
});
|
|
@@ -621,8 +705,8 @@ function mapArchiverConfig(config) {
|
|
|
621
705
|
do {
|
|
622
706
|
[searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, currentL1BlockNumber);
|
|
623
707
|
this.log.trace(`Retrieving checkpoints from L1 block ${searchStartBlock} to ${searchEndBlock}`);
|
|
624
|
-
// TODO(md): Retrieve from blob
|
|
625
|
-
const retrievedCheckpoints = await retrieveCheckpointsFromRollup(this.rollup.getContract(), this.publicClient, this.
|
|
708
|
+
// TODO(md): Retrieve from blob client then from consensus client, then from peers
|
|
709
|
+
const retrievedCheckpoints = await retrieveCheckpointsFromRollup(this.rollup.getContract(), this.publicClient, this.debugClient, this.blobClient, searchStartBlock, searchEndBlock, this.l1Addresses, this.instrumentation, this.log, !this.initialSyncComplete);
|
|
626
710
|
if (retrievedCheckpoints.length === 0) {
|
|
627
711
|
// We are not calling `setBlockSynchedL1BlockNumber` because it may cause sync issues if based off infura.
|
|
628
712
|
// See further details in earlier comments.
|
|
@@ -661,6 +745,22 @@ function mapArchiverConfig(config) {
|
|
|
661
745
|
});
|
|
662
746
|
continue;
|
|
663
747
|
}
|
|
748
|
+
// Check the inHash of the checkpoint against the l1->l2 messages.
|
|
749
|
+
// The messages should've been synced up to the currentL1BlockNumber and must be available for the published
|
|
750
|
+
// checkpoints we just retrieved.
|
|
751
|
+
const l1ToL2Messages = await this.getL1ToL2Messages(published.checkpoint.number);
|
|
752
|
+
const computedInHash = computeInHashFromL1ToL2Messages(l1ToL2Messages);
|
|
753
|
+
const publishedInHash = published.checkpoint.header.contentCommitment.inHash;
|
|
754
|
+
if (!computedInHash.equals(publishedInHash)) {
|
|
755
|
+
this.log.fatal(`Mismatch inHash for checkpoint ${published.checkpoint.number}`, {
|
|
756
|
+
checkpointHash: published.checkpoint.hash(),
|
|
757
|
+
l1BlockNumber: published.l1.blockNumber,
|
|
758
|
+
computedInHash,
|
|
759
|
+
publishedInHash
|
|
760
|
+
});
|
|
761
|
+
// Throwing an error since this is most likely caused by a bug.
|
|
762
|
+
throw new Error(`Mismatch inHash for checkpoint ${published.checkpoint.number}. Expected ${computedInHash} but got ${publishedInHash}`);
|
|
763
|
+
}
|
|
664
764
|
validCheckpoints.push(published);
|
|
665
765
|
this.log.debug(`Ingesting new checkpoint ${published.checkpoint.number} with ${published.checkpoint.blocks.length} blocks`, {
|
|
666
766
|
checkpointHash: published.checkpoint.hash(),
|
|
@@ -674,15 +774,14 @@ function mapArchiverConfig(config) {
|
|
|
674
774
|
const [processDuration] = await elapsed(()=>this.addCheckpoints(validCheckpoints, updatedValidationResult));
|
|
675
775
|
this.instrumentation.processNewBlocks(processDuration / validCheckpoints.length, validCheckpoints.flatMap((c)=>c.checkpoint.blocks));
|
|
676
776
|
} catch (err) {
|
|
677
|
-
if (err instanceof
|
|
678
|
-
const {
|
|
679
|
-
const
|
|
680
|
-
const updatedL1SyncPoint =
|
|
777
|
+
if (err instanceof InitialCheckpointNumberNotSequentialError) {
|
|
778
|
+
const { previousCheckpointNumber, newCheckpointNumber } = err;
|
|
779
|
+
const previousCheckpoint = previousCheckpointNumber ? await this.store.getCheckpointData(CheckpointNumber(previousCheckpointNumber)) : undefined;
|
|
780
|
+
const updatedL1SyncPoint = previousCheckpoint?.l1.blockNumber ?? this.l1constants.l1StartBlock;
|
|
681
781
|
await this.store.setBlockSynchedL1BlockNumber(updatedL1SyncPoint);
|
|
682
|
-
this.log.warn(`Attempting to insert
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
newBlockNumber,
|
|
782
|
+
this.log.warn(`Attempting to insert checkpoint ${newCheckpointNumber} with previous block ${previousCheckpointNumber}. Rolling back L1 sync point to ${updatedL1SyncPoint} to try and fetch the missing blocks.`, {
|
|
783
|
+
previousCheckpointNumber,
|
|
784
|
+
newCheckpointNumber,
|
|
686
785
|
updatedL1SyncPoint
|
|
687
786
|
});
|
|
688
787
|
}
|
|
@@ -700,7 +799,7 @@ function mapArchiverConfig(config) {
|
|
|
700
799
|
});
|
|
701
800
|
}
|
|
702
801
|
lastRetrievedCheckpoint = validCheckpoints.at(-1) ?? lastRetrievedCheckpoint;
|
|
703
|
-
lastL1BlockWithCheckpoint =
|
|
802
|
+
lastL1BlockWithCheckpoint = retrievedCheckpoints.at(-1)?.l1.blockNumber ?? lastL1BlockWithCheckpoint;
|
|
704
803
|
}while (searchEndBlock < currentL1BlockNumber)
|
|
705
804
|
// Important that we update AFTER inserting the blocks.
|
|
706
805
|
await updateProvenCheckpoint();
|
|
@@ -721,9 +820,16 @@ function mapArchiverConfig(config) {
|
|
|
721
820
|
// We suspect an L1 reorg that added checkpoints *behind* us. If that is the case, it must have happened between
|
|
722
821
|
// the last checkpoint we saw and the current one, so we reset the last synched L1 block number. In the edge case
|
|
723
822
|
// we don't have one, we go back 2 L1 epochs, which is the deepest possible reorg (assuming Casper is working).
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
823
|
+
let latestLocalCheckpointArchive = undefined;
|
|
824
|
+
let targetL1BlockNumber = maxBigint(currentL1BlockNumber - 64n, 0n);
|
|
825
|
+
if (lastRetrievedCheckpoint) {
|
|
826
|
+
latestLocalCheckpointArchive = lastRetrievedCheckpoint.checkpoint.archive.root.toString();
|
|
827
|
+
targetL1BlockNumber = lastRetrievedCheckpoint.l1.blockNumber;
|
|
828
|
+
} else if (latestLocalCheckpointNumber > 0) {
|
|
829
|
+
const checkpoint = await this.store.getRangeOfCheckpoints(latestLocalCheckpointNumber, 1).then(([c])=>c);
|
|
830
|
+
latestLocalCheckpointArchive = checkpoint.archive.root.toString();
|
|
831
|
+
targetL1BlockNumber = checkpoint.l1.blockNumber;
|
|
832
|
+
}
|
|
727
833
|
this.log.warn(`Failed to reach checkpoint ${pendingCheckpointNumber} at ${currentL1BlockNumber} (latest is ${latestLocalCheckpointNumber}). ` + `Rolling back last synched L1 block number to ${targetL1BlockNumber}.`, {
|
|
728
834
|
latestLocalCheckpointNumber,
|
|
729
835
|
latestLocalCheckpointArchive,
|
|
@@ -787,32 +893,44 @@ function mapArchiverConfig(config) {
|
|
|
787
893
|
async getBlocksForEpoch(epochNumber) {
|
|
788
894
|
const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1constants);
|
|
789
895
|
const blocks = [];
|
|
790
|
-
// Walk the list of
|
|
791
|
-
// We'll typically ask for
|
|
792
|
-
let
|
|
793
|
-
const slot = (b)=>b.header.
|
|
794
|
-
while(
|
|
795
|
-
if (slot(
|
|
796
|
-
blocks
|
|
896
|
+
// Walk the list of checkpoints backwards and filter by slots matching the requested epoch.
|
|
897
|
+
// We'll typically ask for checkpoints for a very recent epoch, so we shouldn't need an index here.
|
|
898
|
+
let checkpoint = await this.store.getCheckpointData(await this.store.getSynchedCheckpointNumber());
|
|
899
|
+
const slot = (b)=>b.header.slotNumber;
|
|
900
|
+
while(checkpoint && slot(checkpoint) >= start){
|
|
901
|
+
if (slot(checkpoint) <= end) {
|
|
902
|
+
// push the blocks on backwards
|
|
903
|
+
const endBlock = checkpoint.startBlock + checkpoint.numBlocks - 1;
|
|
904
|
+
for(let i = endBlock; i >= checkpoint.startBlock; i--){
|
|
905
|
+
const block = await this.getBlock(BlockNumber(i));
|
|
906
|
+
if (block) {
|
|
907
|
+
blocks.push(block);
|
|
908
|
+
}
|
|
909
|
+
}
|
|
797
910
|
}
|
|
798
|
-
|
|
911
|
+
checkpoint = await this.store.getCheckpointData(CheckpointNumber(checkpoint.checkpointNumber - 1));
|
|
799
912
|
}
|
|
800
913
|
return blocks.reverse();
|
|
801
914
|
}
|
|
802
915
|
async getBlockHeadersForEpoch(epochNumber) {
|
|
803
916
|
const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1constants);
|
|
804
917
|
const blocks = [];
|
|
805
|
-
// Walk the list of
|
|
806
|
-
// We'll typically ask for
|
|
807
|
-
let
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
918
|
+
// Walk the list of checkpoints backwards and filter by slots matching the requested epoch.
|
|
919
|
+
// We'll typically ask for checkpoints for a very recent epoch, so we shouldn't need an index here.
|
|
920
|
+
let checkpoint = await this.store.getCheckpointData(await this.store.getSynchedCheckpointNumber());
|
|
921
|
+
const slot = (b)=>b.header.slotNumber;
|
|
922
|
+
while(checkpoint && slot(checkpoint) >= start){
|
|
923
|
+
if (slot(checkpoint) <= end) {
|
|
924
|
+
// push the blocks on backwards
|
|
925
|
+
const endBlock = checkpoint.startBlock + checkpoint.numBlocks - 1;
|
|
926
|
+
for(let i = endBlock; i >= checkpoint.startBlock; i--){
|
|
927
|
+
const block = await this.getBlockHeader(BlockNumber(i));
|
|
928
|
+
if (block) {
|
|
929
|
+
blocks.push(block);
|
|
930
|
+
}
|
|
931
|
+
}
|
|
813
932
|
}
|
|
814
|
-
|
|
815
|
-
header = await this.getBlockHeader(number);
|
|
933
|
+
checkpoint = await this.store.getCheckpointData(CheckpointNumber(checkpoint.checkpointNumber - 1));
|
|
816
934
|
}
|
|
817
935
|
return blocks.reverse();
|
|
818
936
|
}
|
|
@@ -843,29 +961,6 @@ function mapArchiverConfig(config) {
|
|
|
843
961
|
/** Returns whether the archiver has completed an initial sync run successfully. */ isInitialSyncComplete() {
|
|
844
962
|
return this.initialSyncComplete;
|
|
845
963
|
}
|
|
846
|
-
async getPublishedCheckpoints(from, limit, proven) {
|
|
847
|
-
// TODO: Implement this properly. This only works when we have one block per checkpoint.
|
|
848
|
-
const blocks = await this.getPublishedBlocks(BlockNumber(from), limit, proven);
|
|
849
|
-
return blocks.map((b)=>b.toPublishedCheckpoint());
|
|
850
|
-
}
|
|
851
|
-
async getCheckpointByArchive(archive) {
|
|
852
|
-
// TODO: Implement this properly. This only works when we have one block per checkpoint.
|
|
853
|
-
return (await this.getPublishedBlockByArchive(archive))?.block.toCheckpoint();
|
|
854
|
-
}
|
|
855
|
-
async getCheckpoints(from, limit, proven) {
|
|
856
|
-
const published = await this.getPublishedCheckpoints(from, limit, proven);
|
|
857
|
-
return published.map((p)=>p.checkpoint);
|
|
858
|
-
}
|
|
859
|
-
async getCheckpoint(number) {
|
|
860
|
-
if (number < 0) {
|
|
861
|
-
number = await this.getSynchedCheckpointNumber();
|
|
862
|
-
}
|
|
863
|
-
if (number === 0) {
|
|
864
|
-
return undefined;
|
|
865
|
-
}
|
|
866
|
-
const published = await this.getPublishedCheckpoints(number, 1);
|
|
867
|
-
return published[0]?.checkpoint;
|
|
868
|
-
}
|
|
869
964
|
async getCheckpointHeader(number) {
|
|
870
965
|
if (number === 'latest') {
|
|
871
966
|
number = await this.getSynchedCheckpointNumber();
|
|
@@ -873,71 +968,36 @@ function mapArchiverConfig(config) {
|
|
|
873
968
|
if (number === 0) {
|
|
874
969
|
return undefined;
|
|
875
970
|
}
|
|
876
|
-
const checkpoint = await this.
|
|
877
|
-
|
|
971
|
+
const checkpoint = await this.store.getCheckpointData(number);
|
|
972
|
+
if (!checkpoint) {
|
|
973
|
+
return undefined;
|
|
974
|
+
}
|
|
975
|
+
return checkpoint.header;
|
|
878
976
|
}
|
|
879
977
|
getCheckpointNumber() {
|
|
880
978
|
return this.getSynchedCheckpointNumber();
|
|
881
979
|
}
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
// Checkpoint number will no longer be the same as the block number once we support multiple blocks per checkpoint.
|
|
885
|
-
return CheckpointNumber(await this.store.getSynchedL2BlockNumber());
|
|
980
|
+
getSynchedCheckpointNumber() {
|
|
981
|
+
return this.store.getSynchedCheckpointNumber();
|
|
886
982
|
}
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
// Proven checkpoint number will no longer be the same as the proven block number once we support multiple blocks per checkpoint.
|
|
890
|
-
return CheckpointNumber(await this.store.getProvenL2BlockNumber());
|
|
983
|
+
getProvenCheckpointNumber() {
|
|
984
|
+
return this.store.getProvenCheckpointNumber();
|
|
891
985
|
}
|
|
892
986
|
setProvenCheckpointNumber(checkpointNumber) {
|
|
893
|
-
|
|
894
|
-
// Proven checkpoint number will no longer be the same as the proven block number once we support multiple blocks per checkpoint.
|
|
895
|
-
return this.store.setProvenL2BlockNumber(BlockNumber.fromCheckpointNumber(checkpointNumber));
|
|
987
|
+
return this.store.setProvenCheckpointNumber(checkpointNumber);
|
|
896
988
|
}
|
|
897
989
|
unwindCheckpoints(from, checkpointsToUnwind) {
|
|
898
|
-
|
|
899
|
-
// This only works when we have one block per checkpoint.
|
|
900
|
-
return this.store.unwindBlocks(BlockNumber.fromCheckpointNumber(from), checkpointsToUnwind);
|
|
990
|
+
return this.store.unwindCheckpoints(from, checkpointsToUnwind);
|
|
901
991
|
}
|
|
902
|
-
getLastBlockNumberInCheckpoint(checkpointNumber) {
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
992
|
+
async getLastBlockNumberInCheckpoint(checkpointNumber) {
|
|
993
|
+
const checkpointData = await this.store.getCheckpointData(checkpointNumber);
|
|
994
|
+
if (!checkpointData) {
|
|
995
|
+
return undefined;
|
|
996
|
+
}
|
|
997
|
+
return BlockNumber(checkpointData.startBlock + checkpointData.numBlocks - 1);
|
|
906
998
|
}
|
|
907
999
|
addCheckpoints(checkpoints, pendingChainValidationStatus) {
|
|
908
|
-
|
|
909
|
-
// This only works when we have one block per checkpoint.
|
|
910
|
-
return this.store.addBlocks(checkpoints.map((p)=>PublishedL2Block.fromPublishedCheckpoint(p)), pendingChainValidationStatus);
|
|
911
|
-
}
|
|
912
|
-
async getCheckpointsForEpoch(epochNumber) {
|
|
913
|
-
// TODO: Create store and apis for checkpoints.
|
|
914
|
-
// This only works when we have one block per checkpoint.
|
|
915
|
-
const blocks = await this.getBlocksForEpoch(epochNumber);
|
|
916
|
-
return blocks.map((b)=>b.toCheckpoint());
|
|
917
|
-
}
|
|
918
|
-
getL1ToL2MessagesForCheckpoint(checkpointNumber) {
|
|
919
|
-
// TODO: Create dedicated api for checkpoints.
|
|
920
|
-
// This only works when we have one block per checkpoint.
|
|
921
|
-
return this.getL1ToL2Messages(BlockNumber.fromCheckpointNumber(checkpointNumber));
|
|
922
|
-
}
|
|
923
|
-
/**
|
|
924
|
-
* Gets up to `limit` amount of L2 blocks starting from `from`.
|
|
925
|
-
* @param from - Number of the first block to return (inclusive).
|
|
926
|
-
* @param limit - The number of blocks to return.
|
|
927
|
-
* @param proven - If true, only return blocks that have been proven.
|
|
928
|
-
* @returns The requested L2 blocks.
|
|
929
|
-
*/ getBlocks(from, limit, proven) {
|
|
930
|
-
return this.getPublishedBlocks(from, limit, proven).then((blocks)=>blocks.map((b)=>b.block));
|
|
931
|
-
}
|
|
932
|
-
/** Equivalent to getBlocks but includes publish data. */ async getPublishedBlocks(from, limit, proven) {
|
|
933
|
-
const limitWithProven = proven ? Math.min(limit, Math.max(await this.store.getProvenL2BlockNumber() - from + 1, 0)) : limit;
|
|
934
|
-
return limitWithProven === 0 ? [] : await this.store.getPublishedBlocks(from, limitWithProven);
|
|
935
|
-
}
|
|
936
|
-
getPublishedBlockByHash(blockHash) {
|
|
937
|
-
return this.store.getPublishedBlockByHash(blockHash);
|
|
938
|
-
}
|
|
939
|
-
getPublishedBlockByArchive(archive) {
|
|
940
|
-
return this.store.getPublishedBlockByArchive(archive);
|
|
1000
|
+
return this.store.addCheckpoints(checkpoints, pendingChainValidationStatus);
|
|
941
1001
|
}
|
|
942
1002
|
getBlockHeaderByHash(blockHash) {
|
|
943
1003
|
return this.store.getBlockHeaderByHash(blockHash);
|
|
@@ -949,7 +1009,7 @@ function mapArchiverConfig(config) {
|
|
|
949
1009
|
* Gets an l2 block.
|
|
950
1010
|
* @param number - The block number to return.
|
|
951
1011
|
* @returns The requested L2 block.
|
|
952
|
-
*/ async
|
|
1012
|
+
*/ async getL2BlockNew(number) {
|
|
953
1013
|
// If the number provided is -ve, then return the latest block.
|
|
954
1014
|
if (number < 0) {
|
|
955
1015
|
number = await this.store.getSynchedL2BlockNumber();
|
|
@@ -957,8 +1017,8 @@ function mapArchiverConfig(config) {
|
|
|
957
1017
|
if (number === 0) {
|
|
958
1018
|
return undefined;
|
|
959
1019
|
}
|
|
960
|
-
const publishedBlock = await this.store.
|
|
961
|
-
return publishedBlock
|
|
1020
|
+
const publishedBlock = await this.store.store.getBlock(number);
|
|
1021
|
+
return publishedBlock;
|
|
962
1022
|
}
|
|
963
1023
|
async getBlockHeader(number) {
|
|
964
1024
|
if (number === 'latest') {
|
|
@@ -970,27 +1030,29 @@ function mapArchiverConfig(config) {
|
|
|
970
1030
|
const headers = await this.store.getBlockHeaders(number, 1);
|
|
971
1031
|
return headers.length === 0 ? undefined : headers[0];
|
|
972
1032
|
}
|
|
1033
|
+
getCheckpointedBlock(number) {
|
|
1034
|
+
return this.store.getCheckpointedBlock(number);
|
|
1035
|
+
}
|
|
1036
|
+
getCheckpointedBlockByHash(blockHash) {
|
|
1037
|
+
return this.store.getCheckpointedBlockByHash(blockHash);
|
|
1038
|
+
}
|
|
1039
|
+
getProvenBlockNumber() {
|
|
1040
|
+
return this.store.getProvenBlockNumber();
|
|
1041
|
+
}
|
|
1042
|
+
getCheckpointedBlockByArchive(archive) {
|
|
1043
|
+
return this.store.getCheckpointedBlockByArchive(archive);
|
|
1044
|
+
}
|
|
973
1045
|
getTxEffect(txHash) {
|
|
974
1046
|
return this.store.getTxEffect(txHash);
|
|
975
1047
|
}
|
|
976
1048
|
getSettledTxReceipt(txHash) {
|
|
977
1049
|
return this.store.getSettledTxReceipt(txHash);
|
|
978
1050
|
}
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
* @param from - The block number from which to begin retrieving logs.
|
|
982
|
-
* @param limit - The maximum number of blocks to retrieve logs from.
|
|
983
|
-
* @returns An array of private logs from the specified range of blocks.
|
|
984
|
-
*/ getPrivateLogs(from, limit) {
|
|
985
|
-
return this.store.getPrivateLogs(from, limit);
|
|
1051
|
+
getPrivateLogsByTags(tags) {
|
|
1052
|
+
return this.store.getPrivateLogsByTags(tags);
|
|
986
1053
|
}
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
* @param tags - The tags to filter the logs by.
|
|
990
|
-
* @returns For each received tag, an array of matching logs is returned. An empty array implies no logs match
|
|
991
|
-
* that tag.
|
|
992
|
-
*/ getLogsByTags(tags) {
|
|
993
|
-
return this.store.getLogsByTags(tags);
|
|
1054
|
+
getPublicLogsByTagsFromContract(contractAddress, tags) {
|
|
1055
|
+
return this.store.getPublicLogsByTagsFromContract(contractAddress, tags);
|
|
994
1056
|
}
|
|
995
1057
|
/**
|
|
996
1058
|
* Gets public logs based on the provided filter.
|
|
@@ -1008,15 +1070,10 @@ function mapArchiverConfig(config) {
|
|
|
1008
1070
|
}
|
|
1009
1071
|
/**
|
|
1010
1072
|
* Gets the number of the latest L2 block processed by the block source implementation.
|
|
1073
|
+
* This includes both checkpointed and uncheckpointed blocks.
|
|
1011
1074
|
* @returns The number of the latest L2 block processed by the block source implementation.
|
|
1012
1075
|
*/ getBlockNumber() {
|
|
1013
|
-
return this.store.
|
|
1014
|
-
}
|
|
1015
|
-
getProvenBlockNumber() {
|
|
1016
|
-
return this.store.getProvenL2BlockNumber();
|
|
1017
|
-
}
|
|
1018
|
-
/** Forcefully updates the last proven block number. Use for testing. */ setProvenBlockNumber(blockNumber) {
|
|
1019
|
-
return this.store.setProvenL2BlockNumber(blockNumber);
|
|
1076
|
+
return this.store.getLatestBlockNumber();
|
|
1020
1077
|
}
|
|
1021
1078
|
getContractClass(id) {
|
|
1022
1079
|
return this.store.getContractClass(id);
|
|
@@ -1036,11 +1093,11 @@ function mapArchiverConfig(config) {
|
|
|
1036
1093
|
return this.store.getContractInstance(address, timestamp);
|
|
1037
1094
|
}
|
|
1038
1095
|
/**
|
|
1039
|
-
* Gets L1 to L2 message (to be) included in a given
|
|
1040
|
-
* @param
|
|
1096
|
+
* Gets L1 to L2 message (to be) included in a given checkpoint.
|
|
1097
|
+
* @param checkpointNumber - Checkpoint number to get messages for.
|
|
1041
1098
|
* @returns The L1 to L2 messages/leaves of the messages subtree (throws if not found).
|
|
1042
|
-
*/ getL1ToL2Messages(
|
|
1043
|
-
return this.store.getL1ToL2Messages(
|
|
1099
|
+
*/ getL1ToL2Messages(checkpointNumber) {
|
|
1100
|
+
return this.store.getL1ToL2Messages(checkpointNumber);
|
|
1044
1101
|
}
|
|
1045
1102
|
/**
|
|
1046
1103
|
* Gets the L1 to L2 message index in the L1 to L2 message tree.
|
|
@@ -1108,24 +1165,25 @@ function mapArchiverConfig(config) {
|
|
|
1108
1165
|
};
|
|
1109
1166
|
}
|
|
1110
1167
|
async rollbackTo(targetL2BlockNumber) {
|
|
1168
|
+
// TODO(pw/mbps): This still assumes 1 block per checkpoint
|
|
1111
1169
|
const currentBlocks = await this.getL2Tips();
|
|
1112
1170
|
const currentL2Block = currentBlocks.latest.number;
|
|
1113
1171
|
const currentProvenBlock = currentBlocks.proven.number;
|
|
1114
|
-
// const currentFinalizedBlock = currentBlocks.finalized.number;
|
|
1115
1172
|
if (targetL2BlockNumber >= currentL2Block) {
|
|
1116
1173
|
throw new Error(`Target L2 block ${targetL2BlockNumber} must be less than current L2 block ${currentL2Block}`);
|
|
1117
1174
|
}
|
|
1118
1175
|
const blocksToUnwind = currentL2Block - targetL2BlockNumber;
|
|
1119
|
-
const targetL2Block = await this.store.
|
|
1176
|
+
const targetL2Block = await this.store.getCheckpointedBlock(targetL2BlockNumber);
|
|
1120
1177
|
if (!targetL2Block) {
|
|
1121
1178
|
throw new Error(`Target L2 block ${targetL2BlockNumber} not found`);
|
|
1122
1179
|
}
|
|
1123
1180
|
const targetL1BlockNumber = targetL2Block.l1.blockNumber;
|
|
1181
|
+
const targetCheckpointNumber = CheckpointNumber.fromBlockNumber(targetL2BlockNumber);
|
|
1124
1182
|
const targetL1BlockHash = await this.getL1BlockHash(targetL1BlockNumber);
|
|
1125
|
-
this.log.info(`Unwinding ${blocksToUnwind}
|
|
1126
|
-
await this.store.
|
|
1127
|
-
this.log.info(`Unwinding L1 to L2 messages to ${
|
|
1128
|
-
await this.store.
|
|
1183
|
+
this.log.info(`Unwinding ${blocksToUnwind} checkpoints from L2 block ${currentL2Block}`);
|
|
1184
|
+
await this.store.unwindCheckpoints(CheckpointNumber(currentL2Block), blocksToUnwind);
|
|
1185
|
+
this.log.info(`Unwinding L1 to L2 messages to checkpoint ${targetCheckpointNumber}`);
|
|
1186
|
+
await this.store.rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber);
|
|
1129
1187
|
this.log.info(`Setting L1 syncpoints to ${targetL1BlockNumber}`);
|
|
1130
1188
|
await this.store.setBlockSynchedL1BlockNumber(targetL1BlockNumber);
|
|
1131
1189
|
await this.store.setMessageSynchedL1Block({
|
|
@@ -1134,7 +1192,7 @@ function mapArchiverConfig(config) {
|
|
|
1134
1192
|
});
|
|
1135
1193
|
if (targetL2BlockNumber < currentProvenBlock) {
|
|
1136
1194
|
this.log.info(`Clearing proven L2 block number`);
|
|
1137
|
-
await this.store.
|
|
1195
|
+
await this.store.setProvenCheckpointNumber(CheckpointNumber.ZERO);
|
|
1138
1196
|
}
|
|
1139
1197
|
// TODO(palla/reorg): Set the finalized block when we add support for it.
|
|
1140
1198
|
// if (targetL2BlockNumber < currentFinalizedBlock) {
|
|
@@ -1142,6 +1200,103 @@ function mapArchiverConfig(config) {
|
|
|
1142
1200
|
// await this.store.setFinalizedL2BlockNumber(0);
|
|
1143
1201
|
// }
|
|
1144
1202
|
}
|
|
1203
|
+
async getPublishedCheckpoints(checkpointNumber, limit) {
|
|
1204
|
+
const checkpoints = await this.store.getRangeOfCheckpoints(checkpointNumber, limit);
|
|
1205
|
+
const blocks = (await Promise.all(checkpoints.map((ch)=>this.store.getBlocksForCheckpoint(ch.checkpointNumber)))).filter(isDefined);
|
|
1206
|
+
const fullCheckpoints = [];
|
|
1207
|
+
for(let i = 0; i < checkpoints.length; i++){
|
|
1208
|
+
const blocksForCheckpoint = blocks[i];
|
|
1209
|
+
const checkpoint = checkpoints[i];
|
|
1210
|
+
const fullCheckpoint = new Checkpoint(checkpoint.archive, checkpoint.header, blocksForCheckpoint, checkpoint.checkpointNumber);
|
|
1211
|
+
const publishedCheckpoint = new PublishedCheckpoint(fullCheckpoint, checkpoint.l1, checkpoint.attestations.map((x)=>CommitteeAttestation.fromBuffer(x)));
|
|
1212
|
+
fullCheckpoints.push(publishedCheckpoint);
|
|
1213
|
+
}
|
|
1214
|
+
return fullCheckpoints;
|
|
1215
|
+
}
|
|
1216
|
+
async getCheckpointsForEpoch(epochNumber) {
|
|
1217
|
+
const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1constants);
|
|
1218
|
+
const checkpoints = [];
|
|
1219
|
+
// Walk the list of checkpoints backwards and filter by slots matching the requested epoch.
|
|
1220
|
+
// We'll typically ask for checkpoints for a very recent epoch, so we shouldn't need an index here.
|
|
1221
|
+
let checkpointData = await this.store.getCheckpointData(await this.store.getSynchedCheckpointNumber());
|
|
1222
|
+
const slot = (b)=>b.header.slotNumber;
|
|
1223
|
+
while(checkpointData && slot(checkpointData) >= start){
|
|
1224
|
+
if (slot(checkpointData) <= end) {
|
|
1225
|
+
// push the checkpoints on backwards
|
|
1226
|
+
const [checkpoint] = await this.getPublishedCheckpoints(checkpointData.checkpointNumber, 1);
|
|
1227
|
+
checkpoints.push(checkpoint.checkpoint);
|
|
1228
|
+
}
|
|
1229
|
+
checkpointData = await this.store.getCheckpointData(CheckpointNumber(checkpointData.checkpointNumber - 1));
|
|
1230
|
+
}
|
|
1231
|
+
return checkpoints.reverse();
|
|
1232
|
+
}
|
|
1233
|
+
/* Legacy APIs */ async getPublishedBlockByHash(blockHash) {
|
|
1234
|
+
const checkpointedBlock = await this.store.getCheckpointedBlockByHash(blockHash);
|
|
1235
|
+
return this.buildOldBlockFromCheckpointedBlock(checkpointedBlock);
|
|
1236
|
+
}
|
|
1237
|
+
async getPublishedBlockByArchive(archive) {
|
|
1238
|
+
const checkpointedBlock = await this.store.getCheckpointedBlockByArchive(archive);
|
|
1239
|
+
return this.buildOldBlockFromCheckpointedBlock(checkpointedBlock);
|
|
1240
|
+
}
|
|
1241
|
+
/**
|
|
1242
|
+
* Gets up to `limit` amount of L2 blocks starting from `from`.
|
|
1243
|
+
* @param from - Number of the first block to return (inclusive).
|
|
1244
|
+
* @param limit - The number of blocks to return.
|
|
1245
|
+
* @param proven - If true, only return blocks that have been proven.
|
|
1246
|
+
* @returns The requested L2 blocks.
|
|
1247
|
+
*/ async getBlocks(from, limit, proven) {
|
|
1248
|
+
const publishedBlocks = await this.getPublishedBlocks(from, limit, proven);
|
|
1249
|
+
return publishedBlocks.map((x)=>x.block);
|
|
1250
|
+
}
|
|
1251
|
+
async getPublishedBlocks(from, limit, proven) {
|
|
1252
|
+
const checkpoints = await this.store.getRangeOfCheckpoints(CheckpointNumber(from), limit);
|
|
1253
|
+
const provenCheckpointNumber = await this.getProvenCheckpointNumber();
|
|
1254
|
+
const blocks = (await Promise.all(checkpoints.map((ch)=>this.store.getBlocksForCheckpoint(ch.checkpointNumber)))).filter(isDefined);
|
|
1255
|
+
const olbBlocks = [];
|
|
1256
|
+
for(let i = 0; i < checkpoints.length; i++){
|
|
1257
|
+
const blockForCheckpoint = blocks[i][0];
|
|
1258
|
+
const checkpoint = checkpoints[i];
|
|
1259
|
+
if (checkpoint.checkpointNumber > provenCheckpointNumber && proven === true) {
|
|
1260
|
+
continue;
|
|
1261
|
+
}
|
|
1262
|
+
const oldCheckpoint = new Checkpoint(blockForCheckpoint.archive, checkpoint.header, [
|
|
1263
|
+
blockForCheckpoint
|
|
1264
|
+
], checkpoint.checkpointNumber);
|
|
1265
|
+
const oldBlock = L2Block.fromCheckpoint(oldCheckpoint);
|
|
1266
|
+
const publishedBlock = new PublishedL2Block(oldBlock, checkpoint.l1, checkpoint.attestations.map((x)=>CommitteeAttestation.fromBuffer(x)));
|
|
1267
|
+
olbBlocks.push(publishedBlock);
|
|
1268
|
+
}
|
|
1269
|
+
return olbBlocks;
|
|
1270
|
+
}
|
|
1271
|
+
async buildOldBlockFromCheckpointedBlock(checkpointedBlock) {
|
|
1272
|
+
if (!checkpointedBlock) {
|
|
1273
|
+
return undefined;
|
|
1274
|
+
}
|
|
1275
|
+
const checkpoint = await this.store.getCheckpointData(checkpointedBlock.checkpointNumber);
|
|
1276
|
+
if (!checkpoint) {
|
|
1277
|
+
return checkpoint;
|
|
1278
|
+
}
|
|
1279
|
+
const fullCheckpoint = new Checkpoint(checkpointedBlock?.block.archive, checkpoint?.header, [
|
|
1280
|
+
checkpointedBlock.block
|
|
1281
|
+
], checkpoint.checkpointNumber);
|
|
1282
|
+
const oldBlock = L2Block.fromCheckpoint(fullCheckpoint);
|
|
1283
|
+
const published = new PublishedL2Block(oldBlock, checkpoint.l1, checkpoint.attestations.map((x)=>CommitteeAttestation.fromBuffer(x)));
|
|
1284
|
+
return published;
|
|
1285
|
+
}
|
|
1286
|
+
async getBlock(number) {
|
|
1287
|
+
// If the number provided is -ve, then return the latest block.
|
|
1288
|
+
if (number < 0) {
|
|
1289
|
+
number = await this.store.getSynchedL2BlockNumber();
|
|
1290
|
+
}
|
|
1291
|
+
if (number === 0) {
|
|
1292
|
+
return undefined;
|
|
1293
|
+
}
|
|
1294
|
+
const publishedBlocks = await this.getPublishedBlocks(number, 1);
|
|
1295
|
+
if (publishedBlocks.length === 0) {
|
|
1296
|
+
return undefined;
|
|
1297
|
+
}
|
|
1298
|
+
return publishedBlocks[0].block;
|
|
1299
|
+
}
|
|
1145
1300
|
}
|
|
1146
1301
|
_ts_decorate([
|
|
1147
1302
|
trackSpan('Archiver.sync')
|
|
@@ -1263,6 +1418,18 @@ var Operation = /*#__PURE__*/ function(Operation) {
|
|
|
1263
1418
|
}
|
|
1264
1419
|
return true;
|
|
1265
1420
|
}
|
|
1421
|
+
async addBlockDataToDB(block) {
|
|
1422
|
+
const contractClassLogs = block.body.txEffects.flatMap((txEffect)=>txEffect.contractClassLogs);
|
|
1423
|
+
// ContractInstancePublished event logs are broadcast in privateLogs.
|
|
1424
|
+
const privateLogs = block.body.txEffects.flatMap((txEffect)=>txEffect.privateLogs);
|
|
1425
|
+
const publicLogs = block.body.txEffects.flatMap((txEffect)=>txEffect.publicLogs);
|
|
1426
|
+
return (await Promise.all([
|
|
1427
|
+
this.#updatePublishedContractClasses(contractClassLogs, block.number, 0),
|
|
1428
|
+
this.#updateDeployedContractInstances(privateLogs, block.number, 0),
|
|
1429
|
+
this.#updateUpdatedContractInstances(publicLogs, block.header.globalVariables.timestamp, 0),
|
|
1430
|
+
this.#storeBroadcastedIndividualFunctions(contractClassLogs, block.number)
|
|
1431
|
+
])).every(Boolean);
|
|
1432
|
+
}
|
|
1266
1433
|
addBlocks(blocks, pendingChainValidationStatus) {
|
|
1267
1434
|
// Add the blocks to the store. Store will throw if the blocks are not in order, there are gaps,
|
|
1268
1435
|
// or if the previous block is not in the store.
|
|
@@ -1272,34 +1439,51 @@ var Operation = /*#__PURE__*/ function(Operation) {
|
|
|
1272
1439
|
// Update the pending chain validation status if provided
|
|
1273
1440
|
pendingChainValidationStatus && this.store.setPendingChainValidationStatus(pendingChainValidationStatus),
|
|
1274
1441
|
// Add any logs emitted during the retrieved blocks
|
|
1275
|
-
this.store.addLogs(blocks
|
|
1442
|
+
this.store.addLogs(blocks),
|
|
1443
|
+
// Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
|
|
1444
|
+
...blocks.map((block)=>{
|
|
1445
|
+
return this.addBlockDataToDB(block);
|
|
1446
|
+
})
|
|
1447
|
+
]);
|
|
1448
|
+
return opResults.every(Boolean);
|
|
1449
|
+
});
|
|
1450
|
+
}
|
|
1451
|
+
addCheckpoints(checkpoints, pendingChainValidationStatus) {
|
|
1452
|
+
// Add the blocks to the store. Store will throw if the blocks are not in order, there are gaps,
|
|
1453
|
+
// or if the previous block is not in the store.
|
|
1454
|
+
return this.store.transactionAsync(async ()=>{
|
|
1455
|
+
await this.store.addCheckpoints(checkpoints);
|
|
1456
|
+
const allBlocks = checkpoints.flatMap((ch)=>ch.checkpoint.blocks);
|
|
1457
|
+
const opResults = await Promise.all([
|
|
1458
|
+
// Update the pending chain validation status if provided
|
|
1459
|
+
pendingChainValidationStatus && this.store.setPendingChainValidationStatus(pendingChainValidationStatus),
|
|
1460
|
+
// Add any logs emitted during the retrieved blocks
|
|
1461
|
+
this.store.addLogs(allBlocks),
|
|
1276
1462
|
// Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
|
|
1277
|
-
...
|
|
1278
|
-
|
|
1279
|
-
// ContractInstancePublished event logs are broadcast in privateLogs.
|
|
1280
|
-
const privateLogs = block.block.body.txEffects.flatMap((txEffect)=>txEffect.privateLogs);
|
|
1281
|
-
const publicLogs = block.block.body.txEffects.flatMap((txEffect)=>txEffect.publicLogs);
|
|
1282
|
-
return (await Promise.all([
|
|
1283
|
-
this.#updatePublishedContractClasses(contractClassLogs, block.block.number, 0),
|
|
1284
|
-
this.#updateDeployedContractInstances(privateLogs, block.block.number, 0),
|
|
1285
|
-
this.#updateUpdatedContractInstances(publicLogs, block.block.header.globalVariables.timestamp, 0),
|
|
1286
|
-
this.#storeBroadcastedIndividualFunctions(contractClassLogs, block.block.number)
|
|
1287
|
-
])).every(Boolean);
|
|
1463
|
+
...allBlocks.map((block)=>{
|
|
1464
|
+
return this.addBlockDataToDB(block);
|
|
1288
1465
|
})
|
|
1289
1466
|
]);
|
|
1290
1467
|
return opResults.every(Boolean);
|
|
1291
1468
|
});
|
|
1292
1469
|
}
|
|
1293
|
-
async
|
|
1294
|
-
|
|
1470
|
+
async unwindCheckpoints(from, checkpointsToUnwind) {
|
|
1471
|
+
if (checkpointsToUnwind <= 0) {
|
|
1472
|
+
throw new Error(`Cannot unwind ${checkpointsToUnwind} blocks`);
|
|
1473
|
+
}
|
|
1474
|
+
const last = await this.getSynchedCheckpointNumber();
|
|
1295
1475
|
if (from != last) {
|
|
1296
|
-
throw new Error(`Cannot unwind
|
|
1476
|
+
throw new Error(`Cannot unwind checkpoints from checkpoint ${from} when the last checkpoint is ${last}`);
|
|
1297
1477
|
}
|
|
1298
|
-
|
|
1299
|
-
|
|
1478
|
+
const blocks = [];
|
|
1479
|
+
const lastCheckpointNumber = from + checkpointsToUnwind - 1;
|
|
1480
|
+
for(let checkpointNumber = from; checkpointNumber <= lastCheckpointNumber; checkpointNumber++){
|
|
1481
|
+
const blocksForCheckpoint = await this.store.getBlocksForCheckpoint(checkpointNumber);
|
|
1482
|
+
if (!blocksForCheckpoint) {
|
|
1483
|
+
continue;
|
|
1484
|
+
}
|
|
1485
|
+
blocks.push(...blocksForCheckpoint);
|
|
1300
1486
|
}
|
|
1301
|
-
// from - blocksToUnwind = the new head, so + 1 for what we need to remove
|
|
1302
|
-
const blocks = await this.getPublishedBlocks(BlockNumber(from - blocksToUnwind + 1), blocksToUnwind);
|
|
1303
1487
|
const opResults = await Promise.all([
|
|
1304
1488
|
// Prune rolls back to the last proven block, which is by definition valid
|
|
1305
1489
|
this.store.setPendingChainValidationStatus({
|
|
@@ -1307,32 +1491,44 @@ var Operation = /*#__PURE__*/ function(Operation) {
|
|
|
1307
1491
|
}),
|
|
1308
1492
|
// Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
|
|
1309
1493
|
...blocks.map(async (block)=>{
|
|
1310
|
-
const contractClassLogs = block.
|
|
1494
|
+
const contractClassLogs = block.body.txEffects.flatMap((txEffect)=>txEffect.contractClassLogs);
|
|
1311
1495
|
// ContractInstancePublished event logs are broadcast in privateLogs.
|
|
1312
|
-
const privateLogs = block.
|
|
1313
|
-
const publicLogs = block.
|
|
1496
|
+
const privateLogs = block.body.txEffects.flatMap((txEffect)=>txEffect.privateLogs);
|
|
1497
|
+
const publicLogs = block.body.txEffects.flatMap((txEffect)=>txEffect.publicLogs);
|
|
1314
1498
|
return (await Promise.all([
|
|
1315
|
-
this.#updatePublishedContractClasses(contractClassLogs, block.
|
|
1316
|
-
this.#updateDeployedContractInstances(privateLogs, block.
|
|
1317
|
-
this.#updateUpdatedContractInstances(publicLogs, block.
|
|
1499
|
+
this.#updatePublishedContractClasses(contractClassLogs, block.number, 1),
|
|
1500
|
+
this.#updateDeployedContractInstances(privateLogs, block.number, 1),
|
|
1501
|
+
this.#updateUpdatedContractInstances(publicLogs, block.header.globalVariables.timestamp, 1)
|
|
1318
1502
|
])).every(Boolean);
|
|
1319
1503
|
}),
|
|
1320
|
-
this.store.deleteLogs(blocks
|
|
1321
|
-
this.store.
|
|
1504
|
+
this.store.deleteLogs(blocks),
|
|
1505
|
+
this.store.unwindCheckpoints(from, checkpointsToUnwind)
|
|
1322
1506
|
]);
|
|
1323
1507
|
return opResults.every(Boolean);
|
|
1324
1508
|
}
|
|
1325
|
-
|
|
1326
|
-
return this.store.
|
|
1509
|
+
getCheckpointData(checkpointNumber) {
|
|
1510
|
+
return this.store.getCheckpointData(checkpointNumber);
|
|
1511
|
+
}
|
|
1512
|
+
getRangeOfCheckpoints(from, limit) {
|
|
1513
|
+
return this.store.getRangeOfCheckpoints(from, limit);
|
|
1514
|
+
}
|
|
1515
|
+
getCheckpointedL2BlockNumber() {
|
|
1516
|
+
return this.store.getCheckpointedL2BlockNumber();
|
|
1517
|
+
}
|
|
1518
|
+
getSynchedCheckpointNumber() {
|
|
1519
|
+
return this.store.getSynchedCheckpointNumber();
|
|
1327
1520
|
}
|
|
1328
|
-
|
|
1329
|
-
return this.store.
|
|
1521
|
+
setCheckpointSynchedL1BlockNumber(l1BlockNumber) {
|
|
1522
|
+
return this.store.setCheckpointSynchedL1BlockNumber(l1BlockNumber);
|
|
1330
1523
|
}
|
|
1331
|
-
|
|
1332
|
-
return this.store.
|
|
1524
|
+
getCheckpointedBlock(number) {
|
|
1525
|
+
return this.store.getCheckpointedBlock(number);
|
|
1333
1526
|
}
|
|
1334
|
-
|
|
1335
|
-
return this.store.
|
|
1527
|
+
getCheckpointedBlockByHash(blockHash) {
|
|
1528
|
+
return this.store.getCheckpointedBlockByHash(blockHash);
|
|
1529
|
+
}
|
|
1530
|
+
getCheckpointedBlockByArchive(archive) {
|
|
1531
|
+
return this.store.getCheckpointedBlockByArchive(archive);
|
|
1336
1532
|
}
|
|
1337
1533
|
getBlockHeaders(from, limit) {
|
|
1338
1534
|
return this.store.getBlockHeaders(from, limit);
|
|
@@ -1343,6 +1539,18 @@ var Operation = /*#__PURE__*/ function(Operation) {
|
|
|
1343
1539
|
getBlockHeaderByArchive(archive) {
|
|
1344
1540
|
return this.store.getBlockHeaderByArchive(archive);
|
|
1345
1541
|
}
|
|
1542
|
+
getBlockByHash(blockHash) {
|
|
1543
|
+
return this.store.getBlockByHash(blockHash);
|
|
1544
|
+
}
|
|
1545
|
+
getBlockByArchive(archive) {
|
|
1546
|
+
return this.store.getBlockByArchive(archive);
|
|
1547
|
+
}
|
|
1548
|
+
getLatestBlockNumber() {
|
|
1549
|
+
return this.store.getLatestBlockNumber();
|
|
1550
|
+
}
|
|
1551
|
+
getBlocksForCheckpoint(checkpointNumber) {
|
|
1552
|
+
return this.store.getBlocksForCheckpoint(checkpointNumber);
|
|
1553
|
+
}
|
|
1346
1554
|
getTxEffect(txHash) {
|
|
1347
1555
|
return this.store.getTxEffect(txHash);
|
|
1348
1556
|
}
|
|
@@ -1352,17 +1560,17 @@ var Operation = /*#__PURE__*/ function(Operation) {
|
|
|
1352
1560
|
addL1ToL2Messages(messages) {
|
|
1353
1561
|
return this.store.addL1ToL2Messages(messages);
|
|
1354
1562
|
}
|
|
1355
|
-
getL1ToL2Messages(
|
|
1356
|
-
return this.store.getL1ToL2Messages(
|
|
1563
|
+
getL1ToL2Messages(checkpointNumber) {
|
|
1564
|
+
return this.store.getL1ToL2Messages(checkpointNumber);
|
|
1357
1565
|
}
|
|
1358
1566
|
getL1ToL2MessageIndex(l1ToL2Message) {
|
|
1359
1567
|
return this.store.getL1ToL2MessageIndex(l1ToL2Message);
|
|
1360
1568
|
}
|
|
1361
|
-
|
|
1362
|
-
return this.store.
|
|
1569
|
+
getPrivateLogsByTags(tags) {
|
|
1570
|
+
return this.store.getPrivateLogsByTags(tags);
|
|
1363
1571
|
}
|
|
1364
|
-
|
|
1365
|
-
return this.store.
|
|
1572
|
+
getPublicLogsByTagsFromContract(contractAddress, tags) {
|
|
1573
|
+
return this.store.getPublicLogsByTagsFromContract(contractAddress, tags);
|
|
1366
1574
|
}
|
|
1367
1575
|
getPublicLogs(filter) {
|
|
1368
1576
|
return this.store.getPublicLogs(filter);
|
|
@@ -1371,16 +1579,19 @@ var Operation = /*#__PURE__*/ function(Operation) {
|
|
|
1371
1579
|
return this.store.getContractClassLogs(filter);
|
|
1372
1580
|
}
|
|
1373
1581
|
getSynchedL2BlockNumber() {
|
|
1374
|
-
return this.store.
|
|
1582
|
+
return this.store.getCheckpointedL2BlockNumber();
|
|
1583
|
+
}
|
|
1584
|
+
getProvenCheckpointNumber() {
|
|
1585
|
+
return this.store.getProvenCheckpointNumber();
|
|
1375
1586
|
}
|
|
1376
|
-
|
|
1377
|
-
return this.store.
|
|
1587
|
+
getProvenBlockNumber() {
|
|
1588
|
+
return this.store.getProvenBlockNumber();
|
|
1378
1589
|
}
|
|
1379
|
-
|
|
1380
|
-
return this.store.
|
|
1590
|
+
setProvenCheckpointNumber(checkpointNumber) {
|
|
1591
|
+
return this.store.setProvenCheckpointNumber(checkpointNumber);
|
|
1381
1592
|
}
|
|
1382
1593
|
setBlockSynchedL1BlockNumber(l1BlockNumber) {
|
|
1383
|
-
return this.store.
|
|
1594
|
+
return this.store.setCheckpointSynchedL1BlockNumber(l1BlockNumber);
|
|
1384
1595
|
}
|
|
1385
1596
|
setMessageSynchedL1Block(l1Block) {
|
|
1386
1597
|
return this.store.setMessageSynchedL1Block(l1Block);
|
|
@@ -1412,8 +1623,8 @@ var Operation = /*#__PURE__*/ function(Operation) {
|
|
|
1412
1623
|
estimateSize() {
|
|
1413
1624
|
return this.store.estimateSize();
|
|
1414
1625
|
}
|
|
1415
|
-
|
|
1416
|
-
return this.store.
|
|
1626
|
+
rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber) {
|
|
1627
|
+
return this.store.rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber);
|
|
1417
1628
|
}
|
|
1418
1629
|
iterateL1ToL2Messages(range = {}) {
|
|
1419
1630
|
return this.store.iterateL1ToL2Messages(range);
|