@aztec/archiver 0.0.1-commit.e6bd8901 → 0.0.1-commit.ec7ac5448
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 +12 -6
- package/dest/archiver.d.ts +14 -9
- package/dest/archiver.d.ts.map +1 -1
- package/dest/archiver.js +97 -115
- package/dest/config.d.ts +3 -3
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +2 -1
- package/dest/errors.d.ts +34 -10
- package/dest/errors.d.ts.map +1 -1
- package/dest/errors.js +45 -16
- package/dest/factory.d.ts +5 -4
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +32 -28
- package/dest/index.d.ts +2 -1
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +1 -0
- package/dest/l1/bin/retrieve-calldata.js +36 -33
- package/dest/l1/calldata_retriever.d.ts +73 -50
- package/dest/l1/calldata_retriever.d.ts.map +1 -1
- package/dest/l1/calldata_retriever.js +191 -259
- package/dest/l1/data_retrieval.d.ts +11 -11
- package/dest/l1/data_retrieval.d.ts.map +1 -1
- package/dest/l1/data_retrieval.js +36 -35
- package/dest/l1/spire_proposer.d.ts +5 -5
- package/dest/l1/spire_proposer.d.ts.map +1 -1
- package/dest/l1/spire_proposer.js +9 -17
- package/dest/l1/validate_trace.d.ts +6 -3
- package/dest/l1/validate_trace.d.ts.map +1 -1
- package/dest/l1/validate_trace.js +13 -9
- package/dest/modules/data_source_base.d.ts +17 -10
- package/dest/modules/data_source_base.d.ts.map +1 -1
- package/dest/modules/data_source_base.js +39 -77
- package/dest/modules/data_store_updater.d.ts +25 -12
- package/dest/modules/data_store_updater.d.ts.map +1 -1
- package/dest/modules/data_store_updater.js +125 -94
- package/dest/modules/instrumentation.d.ts +18 -3
- package/dest/modules/instrumentation.d.ts.map +1 -1
- package/dest/modules/instrumentation.js +53 -18
- package/dest/modules/l1_synchronizer.d.ts +7 -9
- package/dest/modules/l1_synchronizer.d.ts.map +1 -1
- package/dest/modules/l1_synchronizer.js +180 -139
- package/dest/modules/validation.d.ts +1 -1
- package/dest/modules/validation.d.ts.map +1 -1
- package/dest/modules/validation.js +2 -2
- package/dest/store/block_store.d.ts +69 -31
- package/dest/store/block_store.d.ts.map +1 -1
- package/dest/store/block_store.js +358 -137
- package/dest/store/contract_class_store.d.ts +2 -3
- package/dest/store/contract_class_store.d.ts.map +1 -1
- package/dest/store/contract_class_store.js +16 -72
- package/dest/store/contract_instance_store.d.ts +1 -1
- package/dest/store/contract_instance_store.d.ts.map +1 -1
- package/dest/store/contract_instance_store.js +6 -2
- package/dest/store/kv_archiver_store.d.ts +64 -27
- package/dest/store/kv_archiver_store.d.ts.map +1 -1
- package/dest/store/kv_archiver_store.js +77 -30
- package/dest/store/l2_tips_cache.d.ts +20 -0
- package/dest/store/l2_tips_cache.d.ts.map +1 -0
- package/dest/store/l2_tips_cache.js +109 -0
- package/dest/store/log_store.d.ts +6 -3
- package/dest/store/log_store.d.ts.map +1 -1
- package/dest/store/log_store.js +150 -53
- package/dest/store/message_store.d.ts +5 -1
- package/dest/store/message_store.d.ts.map +1 -1
- package/dest/store/message_store.js +21 -9
- package/dest/test/fake_l1_state.d.ts +21 -1
- package/dest/test/fake_l1_state.d.ts.map +1 -1
- package/dest/test/fake_l1_state.js +133 -26
- package/dest/test/index.js +3 -1
- package/dest/test/mock_archiver.d.ts +1 -1
- package/dest/test/mock_archiver.d.ts.map +1 -1
- package/dest/test/mock_archiver.js +3 -2
- package/dest/test/mock_l1_to_l2_message_source.d.ts +1 -1
- package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
- package/dest/test/mock_l1_to_l2_message_source.js +2 -1
- package/dest/test/mock_l2_block_source.d.ts +30 -9
- package/dest/test/mock_l2_block_source.d.ts.map +1 -1
- package/dest/test/mock_l2_block_source.js +161 -90
- package/dest/test/mock_structs.d.ts +6 -2
- package/dest/test/mock_structs.d.ts.map +1 -1
- package/dest/test/mock_structs.js +20 -6
- package/dest/test/noop_l1_archiver.d.ts +26 -0
- package/dest/test/noop_l1_archiver.d.ts.map +1 -0
- package/dest/test/noop_l1_archiver.js +71 -0
- package/package.json +14 -13
- package/src/archiver.ts +128 -141
- package/src/config.ts +8 -1
- package/src/errors.ts +70 -26
- package/src/factory.ts +48 -26
- package/src/index.ts +1 -0
- package/src/l1/README.md +25 -68
- package/src/l1/bin/retrieve-calldata.ts +46 -39
- package/src/l1/calldata_retriever.ts +250 -379
- package/src/l1/data_retrieval.ts +32 -38
- package/src/l1/spire_proposer.ts +7 -15
- package/src/l1/validate_trace.ts +24 -6
- package/src/modules/data_source_base.ts +81 -101
- package/src/modules/data_store_updater.ts +138 -124
- package/src/modules/instrumentation.ts +63 -19
- package/src/modules/l1_synchronizer.ts +204 -174
- package/src/modules/validation.ts +2 -2
- package/src/store/block_store.ts +456 -177
- package/src/store/contract_class_store.ts +16 -110
- package/src/store/contract_instance_store.ts +8 -5
- package/src/store/kv_archiver_store.ts +120 -46
- package/src/store/l2_tips_cache.ts +128 -0
- package/src/store/log_store.ts +224 -63
- package/src/store/message_store.ts +27 -10
- package/src/structs/inbox_message.ts +1 -1
- package/src/test/fake_l1_state.ts +178 -30
- package/src/test/index.ts +3 -0
- package/src/test/mock_archiver.ts +3 -2
- package/src/test/mock_l1_to_l2_message_source.ts +1 -0
- package/src/test/mock_l2_block_source.ts +215 -88
- package/src/test/mock_structs.ts +42 -12
- package/src/test/noop_l1_archiver.ts +114 -0
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
import type { BlobClientInterface } from '@aztec/blob-client/client';
|
|
2
2
|
import { EpochCache } from '@aztec/epoch-cache';
|
|
3
|
-
import { InboxContract, RollupContract } from '@aztec/ethereum/contracts';
|
|
4
|
-
import type { L1ContractAddresses } from '@aztec/ethereum/l1-contract-addresses';
|
|
3
|
+
import { InboxContract, type InboxContractState, RollupContract } from '@aztec/ethereum/contracts';
|
|
5
4
|
import type { L1BlockId } from '@aztec/ethereum/l1-types';
|
|
6
5
|
import type { ViemPublicClient, ViemPublicDebugClient } from '@aztec/ethereum/types';
|
|
6
|
+
import { asyncPool } from '@aztec/foundation/async-pool';
|
|
7
7
|
import { maxBigint } from '@aztec/foundation/bigint';
|
|
8
8
|
import { BlockNumber, CheckpointNumber, EpochNumber } from '@aztec/foundation/branded-types';
|
|
9
|
-
import { Buffer32 } from '@aztec/foundation/buffer';
|
|
9
|
+
import { Buffer16, Buffer32 } from '@aztec/foundation/buffer';
|
|
10
10
|
import { pick } from '@aztec/foundation/collection';
|
|
11
11
|
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
12
|
-
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
13
12
|
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
13
|
+
import { retryTimes } from '@aztec/foundation/retry';
|
|
14
14
|
import { count } from '@aztec/foundation/string';
|
|
15
15
|
import { DateProvider, Timer, elapsed } from '@aztec/foundation/timer';
|
|
16
|
-
import { isDefined } from '@aztec/foundation/types';
|
|
16
|
+
import { isDefined, isErrorClass } from '@aztec/foundation/types';
|
|
17
17
|
import { type ArchiverEmitter, L2BlockSourceEvents, type ValidateCheckpointResult } from '@aztec/stdlib/block';
|
|
18
18
|
import { PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
|
|
19
|
-
import { type L1RollupConstants, getEpochAtSlot,
|
|
19
|
+
import { type L1RollupConstants, getEpochAtSlot, getSlotAtNextL1Block } from '@aztec/stdlib/epoch-helpers';
|
|
20
20
|
import { computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
|
|
21
21
|
import { type Traceable, type Tracer, execInSpan, trackSpan } from '@aztec/telemetry-client';
|
|
22
22
|
|
|
@@ -28,6 +28,8 @@ import {
|
|
|
28
28
|
retrievedToPublishedCheckpoint,
|
|
29
29
|
} from '../l1/data_retrieval.js';
|
|
30
30
|
import type { KVArchiverDataStore } from '../store/kv_archiver_store.js';
|
|
31
|
+
import type { L2TipsCache } from '../store/l2_tips_cache.js';
|
|
32
|
+
import { MessageStoreError } from '../store/message_store.js';
|
|
31
33
|
import type { InboxMessage } from '../structs/inbox_message.js';
|
|
32
34
|
import { ArchiverDataStoreUpdater } from './data_store_updater.js';
|
|
33
35
|
import type { ArchiverInstrumentation } from './instrumentation.js';
|
|
@@ -60,10 +62,6 @@ export class ArchiverL1Synchronizer implements Traceable {
|
|
|
60
62
|
private readonly debugClient: ViemPublicDebugClient,
|
|
61
63
|
private readonly rollup: RollupContract,
|
|
62
64
|
private readonly inbox: InboxContract,
|
|
63
|
-
private readonly l1Addresses: Pick<
|
|
64
|
-
L1ContractAddresses,
|
|
65
|
-
'registryAddress' | 'governanceProposerAddress' | 'slashFactoryAddress'
|
|
66
|
-
> & { slashingProposerAddress: EthAddress },
|
|
67
65
|
private readonly store: KVArchiverDataStore,
|
|
68
66
|
private config: {
|
|
69
67
|
batchSize: number;
|
|
@@ -74,12 +72,18 @@ export class ArchiverL1Synchronizer implements Traceable {
|
|
|
74
72
|
private readonly epochCache: EpochCache,
|
|
75
73
|
private readonly dateProvider: DateProvider,
|
|
76
74
|
private readonly instrumentation: ArchiverInstrumentation,
|
|
77
|
-
private readonly l1Constants: L1RollupConstants & {
|
|
75
|
+
private readonly l1Constants: L1RollupConstants & {
|
|
76
|
+
l1StartBlockHash: Buffer32;
|
|
77
|
+
genesisArchiveRoot: Fr;
|
|
78
|
+
},
|
|
78
79
|
private readonly events: ArchiverEmitter,
|
|
79
80
|
tracer: Tracer,
|
|
81
|
+
l2TipsCache?: L2TipsCache,
|
|
80
82
|
private readonly log: Logger = createLogger('archiver:l1-sync'),
|
|
81
83
|
) {
|
|
82
|
-
this.updater = new ArchiverDataStoreUpdater(this.store
|
|
84
|
+
this.updater = new ArchiverDataStoreUpdater(this.store, l2TipsCache, {
|
|
85
|
+
rollupManaLimit: l1Constants.rollupManaLimit,
|
|
86
|
+
});
|
|
83
87
|
this.tracer = tracer;
|
|
84
88
|
}
|
|
85
89
|
|
|
@@ -119,10 +123,15 @@ export class ArchiverL1Synchronizer implements Traceable {
|
|
|
119
123
|
|
|
120
124
|
@trackSpan('Archiver.syncFromL1')
|
|
121
125
|
public async syncFromL1(initialSyncComplete: boolean): Promise<void> {
|
|
126
|
+
// In between the various calls to L1, the block number can move meaning some of the following
|
|
127
|
+
// calls will return data for blocks that were not present during earlier calls. To combat this
|
|
128
|
+
// we ensure that all data retrieval methods only retrieve data up to the currentBlockNumber
|
|
129
|
+
// captured at the top of this function.
|
|
122
130
|
const currentL1Block = await this.publicClient.getBlock({ includeTransactions: false });
|
|
123
131
|
const currentL1BlockNumber = currentL1Block.number;
|
|
124
132
|
const currentL1BlockHash = Buffer32.fromString(currentL1Block.hash);
|
|
125
133
|
const currentL1Timestamp = currentL1Block.timestamp;
|
|
134
|
+
const currentL1BlockData = { l1BlockNumber: currentL1BlockNumber, l1BlockHash: currentL1BlockHash };
|
|
126
135
|
|
|
127
136
|
if (this.l1BlockHash && currentL1BlockHash.equals(this.l1BlockHash)) {
|
|
128
137
|
this.log.trace(`No new L1 blocks since last sync at L1 block ${this.l1BlockNumber}`);
|
|
@@ -139,45 +148,15 @@ export class ArchiverL1Synchronizer implements Traceable {
|
|
|
139
148
|
);
|
|
140
149
|
}
|
|
141
150
|
|
|
142
|
-
// Load sync point for blocks
|
|
143
|
-
const {
|
|
144
|
-
|
|
145
|
-
messagesSynchedTo = {
|
|
146
|
-
l1BlockNumber: this.l1Constants.l1StartBlock,
|
|
147
|
-
l1BlockHash: this.l1Constants.l1StartBlockHash,
|
|
148
|
-
},
|
|
149
|
-
} = await this.store.getSynchPoint();
|
|
151
|
+
// Load sync point for blocks defaulting to start block
|
|
152
|
+
const { blocksSynchedTo = this.l1Constants.l1StartBlock } = await this.store.getSynchPoint();
|
|
153
|
+
this.log.debug(`Starting new archiver sync iteration`, { blocksSynchedTo, currentL1BlockData });
|
|
150
154
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
currentL1BlockHash,
|
|
156
|
-
});
|
|
155
|
+
// Sync L1 to L2 messages. We retry this a few times since there are error conditions that reset the sync point, requiring a new iteration.
|
|
156
|
+
// Note that we cannot just wait for the l1 synchronizer to loop again, since the synchronizer would report as synced up to the current L1
|
|
157
|
+
// block, when that wouldn't be the case, since L1 to L2 messages would need another iteration.
|
|
158
|
+
await retryTimes(() => this.handleL1ToL2Messages(currentL1BlockData), 'Handling L1 to L2 messages', 3, 0.1);
|
|
157
159
|
|
|
158
|
-
// ********** Ensuring Consistency of data pulled from L1 **********
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* There are a number of calls in this sync operation to L1 for retrieving
|
|
162
|
-
* events and transaction data. There are a couple of things we need to bear in mind
|
|
163
|
-
* to ensure that data is read exactly once.
|
|
164
|
-
*
|
|
165
|
-
* The first is the problem of eventually consistent ETH service providers like Infura.
|
|
166
|
-
* Each L1 read operation will query data from the last L1 block that it saw emit its kind of data.
|
|
167
|
-
* (so pending L1 to L2 messages will read from the last L1 block that emitted a message and so on)
|
|
168
|
-
* This will mean the archiver will lag behind L1 and will only advance when there's L2-relevant activity on the chain.
|
|
169
|
-
*
|
|
170
|
-
* The second is that in between the various calls to L1, the block number can move meaning some
|
|
171
|
-
* of the following calls will return data for blocks that were not present during earlier calls.
|
|
172
|
-
* To combat this for the time being we simply ensure that all data retrieval methods only retrieve
|
|
173
|
-
* data up to the currentBlockNumber captured at the top of this function. We might want to improve on this
|
|
174
|
-
* in future but for the time being it should give us the guarantees that we need
|
|
175
|
-
*/
|
|
176
|
-
|
|
177
|
-
// ********** Events that are processed per L1 block **********
|
|
178
|
-
await this.handleL1ToL2Messages(messagesSynchedTo, currentL1BlockNumber);
|
|
179
|
-
|
|
180
|
-
// ********** Events that are processed per checkpoint **********
|
|
181
160
|
if (currentL1BlockNumber > blocksSynchedTo) {
|
|
182
161
|
// First we retrieve new checkpoints and L2 blocks and store them in the DB. This will also update the
|
|
183
162
|
// pending chain validation status, proven checkpoint number, and synched L1 block number.
|
|
@@ -215,6 +194,9 @@ export class ArchiverL1Synchronizer implements Traceable {
|
|
|
215
194
|
this.instrumentation.updateL1BlockHeight(currentL1BlockNumber);
|
|
216
195
|
}
|
|
217
196
|
|
|
197
|
+
// Update the finalized L2 checkpoint based on L1 finality.
|
|
198
|
+
await this.updateFinalizedCheckpoint();
|
|
199
|
+
|
|
218
200
|
// After syncing has completed, update the current l1 block number and timestamp,
|
|
219
201
|
// otherwise we risk announcing to the world that we've synced to a given point,
|
|
220
202
|
// but the corresponding blocks have not been processed (see #12631).
|
|
@@ -230,6 +212,30 @@ export class ArchiverL1Synchronizer implements Traceable {
|
|
|
230
212
|
});
|
|
231
213
|
}
|
|
232
214
|
|
|
215
|
+
/** Query L1 for its finalized block and update the finalized checkpoint accordingly. */
|
|
216
|
+
private async updateFinalizedCheckpoint(): Promise<void> {
|
|
217
|
+
try {
|
|
218
|
+
const finalizedL1Block = await this.publicClient.getBlock({ blockTag: 'finalized', includeTransactions: false });
|
|
219
|
+
const finalizedL1BlockNumber = finalizedL1Block.number;
|
|
220
|
+
const finalizedCheckpointNumber = await this.rollup.getProvenCheckpointNumber({
|
|
221
|
+
blockNumber: finalizedL1BlockNumber,
|
|
222
|
+
});
|
|
223
|
+
const localFinalizedCheckpointNumber = await this.store.getFinalizedCheckpointNumber();
|
|
224
|
+
if (localFinalizedCheckpointNumber !== finalizedCheckpointNumber) {
|
|
225
|
+
await this.updater.setFinalizedCheckpointNumber(finalizedCheckpointNumber);
|
|
226
|
+
this.log.info(`Updated finalized chain to checkpoint ${finalizedCheckpointNumber}`, {
|
|
227
|
+
finalizedCheckpointNumber,
|
|
228
|
+
finalizedL1BlockNumber,
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
} catch (err: any) {
|
|
232
|
+
// The rollup contract may not exist at the finalized L1 block right after deployment.
|
|
233
|
+
if (!err?.message?.includes('returned no data')) {
|
|
234
|
+
this.log.warn(`Failed to update finalized checkpoint: ${err}`);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
233
239
|
/** Prune all proposed local blocks that should have been checkpointed by now. */
|
|
234
240
|
private async pruneUncheckpointedBlocks(currentL1Timestamp: bigint) {
|
|
235
241
|
const [lastCheckpointedBlockNumber, lastProposedBlockNumber] = await Promise.all([
|
|
@@ -243,30 +249,33 @@ export class ArchiverL1Synchronizer implements Traceable {
|
|
|
243
249
|
return;
|
|
244
250
|
}
|
|
245
251
|
|
|
246
|
-
// What's the slot
|
|
252
|
+
// What's the slot at the next L1 block? All blocks for slots strictly before this one should've been checkpointed by now.
|
|
253
|
+
const slotAtNextL1Block = getSlotAtNextL1Block(currentL1Timestamp, this.l1Constants);
|
|
247
254
|
const firstUncheckpointedBlockNumber = BlockNumber(lastCheckpointedBlockNumber + 1);
|
|
255
|
+
|
|
256
|
+
// What's the slot of the first uncheckpointed block?
|
|
248
257
|
const [firstUncheckpointedBlockHeader] = await this.store.getBlockHeaders(firstUncheckpointedBlockNumber, 1);
|
|
249
258
|
const firstUncheckpointedBlockSlot = firstUncheckpointedBlockHeader?.getSlot();
|
|
250
259
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
260
|
+
if (firstUncheckpointedBlockSlot === undefined || firstUncheckpointedBlockSlot >= slotAtNextL1Block) {
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
254
263
|
|
|
255
|
-
// Prune provisional blocks from slots that have ended without being checkpointed
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
const prunedBlocks = await this.updater.removeUncheckpointedBlocksAfter(lastCheckpointedBlockNumber);
|
|
264
|
+
// Prune provisional blocks from slots that have ended without being checkpointed.
|
|
265
|
+
// This also clears any proposed checkpoint whose blocks are being pruned.
|
|
266
|
+
this.log.warn(
|
|
267
|
+
`Pruning blocks after block ${lastCheckpointedBlockNumber} due to slot ${firstUncheckpointedBlockSlot} not being checkpointed`,
|
|
268
|
+
{ firstUncheckpointedBlockHeader: firstUncheckpointedBlockHeader.toInspect(), slotAtNextL1Block },
|
|
269
|
+
);
|
|
262
270
|
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
271
|
+
const prunedBlocks = await this.updater.removeUncheckpointedBlocksAfter(lastCheckpointedBlockNumber);
|
|
272
|
+
|
|
273
|
+
if (prunedBlocks.length > 0) {
|
|
274
|
+
this.events.emit(L2BlockSourceEvents.L2PruneUncheckpointed, {
|
|
275
|
+
type: L2BlockSourceEvents.L2PruneUncheckpointed,
|
|
276
|
+
slotNumber: firstUncheckpointedBlockSlot,
|
|
277
|
+
blocks: prunedBlocks,
|
|
278
|
+
});
|
|
270
279
|
}
|
|
271
280
|
}
|
|
272
281
|
|
|
@@ -309,17 +318,20 @@ export class ArchiverL1Synchronizer implements Traceable {
|
|
|
309
318
|
|
|
310
319
|
const checkpointsToUnwind = localPendingCheckpointNumber - provenCheckpointNumber;
|
|
311
320
|
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
const
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
checkpoints
|
|
319
|
-
.filter(isDefined)
|
|
320
|
-
.map(cp => this.store.getBlocksForCheckpoint(CheckpointNumber(cp.checkpointNumber))),
|
|
321
|
+
// Fetch checkpoints and blocks in bounded batches to avoid unbounded concurrent
|
|
322
|
+
// promises when the gap between local pending and proven checkpoint numbers is large.
|
|
323
|
+
const BATCH_SIZE = 10;
|
|
324
|
+
const indices = Array.from({ length: checkpointsToUnwind }, (_, i) => CheckpointNumber(i + pruneFrom));
|
|
325
|
+
const checkpoints = (await asyncPool(BATCH_SIZE, indices, idx => this.store.getCheckpointData(idx))).filter(
|
|
326
|
+
isDefined,
|
|
321
327
|
);
|
|
322
|
-
const newBlocks =
|
|
328
|
+
const newBlocks = (
|
|
329
|
+
await asyncPool(BATCH_SIZE, checkpoints, cp =>
|
|
330
|
+
this.store.getBlocksForCheckpoint(CheckpointNumber(cp.checkpointNumber)),
|
|
331
|
+
)
|
|
332
|
+
)
|
|
333
|
+
.filter(isDefined)
|
|
334
|
+
.flat();
|
|
323
335
|
|
|
324
336
|
// Emit an event for listening services to react to the chain prune
|
|
325
337
|
this.events.emit(L2BlockSourceEvents.L2PruneUnproven, {
|
|
@@ -357,63 +369,87 @@ export class ArchiverL1Synchronizer implements Traceable {
|
|
|
357
369
|
}
|
|
358
370
|
|
|
359
371
|
@trackSpan('Archiver.handleL1ToL2Messages')
|
|
360
|
-
private async handleL1ToL2Messages(
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
372
|
+
private async handleL1ToL2Messages(currentL1Block: L1BlockId): Promise<boolean> {
|
|
373
|
+
// Load the syncpoint, which may have been updated in a previous iteration
|
|
374
|
+
const {
|
|
375
|
+
messagesSynchedTo = {
|
|
376
|
+
l1BlockNumber: this.l1Constants.l1StartBlock,
|
|
377
|
+
l1BlockHash: this.l1Constants.l1StartBlockHash,
|
|
378
|
+
},
|
|
379
|
+
} = await this.store.getSynchPoint();
|
|
380
|
+
|
|
381
|
+
// Nothing to do if L1 block number has not moved forward
|
|
382
|
+
const currentL1BlockNumber = currentL1Block.l1BlockNumber;
|
|
383
|
+
if (currentL1BlockNumber <= messagesSynchedTo.l1BlockNumber) {
|
|
384
|
+
return true;
|
|
364
385
|
}
|
|
365
386
|
|
|
366
|
-
//
|
|
367
|
-
const localMessagesInserted = await this.store.getTotalL1ToL2MessageCount();
|
|
368
|
-
const localLastMessage = await this.store.getLastL1ToL2Message();
|
|
387
|
+
// Compare local message store state with the remote. If they match, we just advance the match pointer.
|
|
369
388
|
const remoteMessagesState = await this.inbox.getState({ blockNumber: currentL1BlockNumber });
|
|
389
|
+
const localLastMessage = await this.store.getLastL1ToL2Message();
|
|
390
|
+
if (await this.localStateMatches(localLastMessage, remoteMessagesState)) {
|
|
391
|
+
this.log.trace(`Local L1 to L2 messages are already in sync with remote at L1 block ${currentL1BlockNumber}`);
|
|
392
|
+
await this.store.setMessageSyncState(currentL1Block, remoteMessagesState.treeInProgress);
|
|
393
|
+
return true;
|
|
394
|
+
}
|
|
370
395
|
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
396
|
+
// If not, then we are out of sync. Most likely there are new messages on the inbox, so we try retrieving them.
|
|
397
|
+
// However, it could also be the case that there was an L1 reorg and our syncpoint is no longer valid.
|
|
398
|
+
// If that's the case, we'd get an exception out of the message store since the rolling hash of the first message
|
|
399
|
+
// we try to insert would not match the one in the db, in which case we rollback to the last common message with L1.
|
|
400
|
+
try {
|
|
401
|
+
await this.retrieveAndStoreMessages(messagesSynchedTo.l1BlockNumber, currentL1BlockNumber);
|
|
402
|
+
} catch (error) {
|
|
403
|
+
if (isErrorClass(error, MessageStoreError)) {
|
|
404
|
+
this.log.warn(
|
|
405
|
+
`Failed to store L1 to L2 messages retrieved from L1: ${error.message}. Rolling back syncpoint to retry.`,
|
|
406
|
+
{ inboxMessage: error.inboxMessage },
|
|
407
|
+
);
|
|
408
|
+
await this.rollbackL1ToL2Messages(remoteMessagesState.treeInProgress);
|
|
409
|
+
return false;
|
|
410
|
+
}
|
|
411
|
+
throw error;
|
|
412
|
+
}
|
|
376
413
|
|
|
377
|
-
//
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
) {
|
|
382
|
-
this.log.
|
|
383
|
-
`
|
|
414
|
+
// Note that, if there are no new messages to insert, but there was an L1 reorg that pruned out last messages,
|
|
415
|
+
// we'd notice by comparing our local state with the remote one again, and seeing they don't match even after
|
|
416
|
+
// our sync attempt. In this case, we also rollback our syncpoint, and trigger a retry.
|
|
417
|
+
const localLastMessageAfterSync = await this.store.getLastL1ToL2Message();
|
|
418
|
+
if (!(await this.localStateMatches(localLastMessageAfterSync, remoteMessagesState))) {
|
|
419
|
+
this.log.warn(
|
|
420
|
+
`Local L1 to L2 messages state does not match remote after sync attempt. Rolling back syncpoint to retry.`,
|
|
421
|
+
{ localLastMessageAfterSync, remoteMessagesState },
|
|
384
422
|
);
|
|
385
|
-
|
|
423
|
+
await this.rollbackL1ToL2Messages(remoteMessagesState.treeInProgress);
|
|
424
|
+
return false;
|
|
386
425
|
}
|
|
387
426
|
|
|
388
|
-
//
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
const remoteLastMessage = await this.retrieveL1ToL2Message(localLastMessage.leaf);
|
|
393
|
-
this.log.trace(`Retrieved remote message for local last`, { remoteLastMessage, localLastMessage });
|
|
394
|
-
if (!remoteLastMessage || !remoteLastMessage.rollingHash.equals(localLastMessage.rollingHash)) {
|
|
395
|
-
this.log.warn(`Rolling back L1 to L2 messages due to hash mismatch or msg not found.`, {
|
|
396
|
-
remoteLastMessage,
|
|
397
|
-
messagesSyncPoint,
|
|
398
|
-
localLastMessage,
|
|
399
|
-
});
|
|
427
|
+
// Advance the syncpoint after a successful sync
|
|
428
|
+
await this.store.setMessageSyncState(currentL1Block, remoteMessagesState.treeInProgress);
|
|
429
|
+
return true;
|
|
430
|
+
}
|
|
400
431
|
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
432
|
+
/** Checks if the local rolling hash and message count matches the remote state */
|
|
433
|
+
private async localStateMatches(localLastMessage: InboxMessage | undefined, remoteState: InboxContractState) {
|
|
434
|
+
const localMessageCount = await this.store.getTotalL1ToL2MessageCount();
|
|
435
|
+
this.log.trace(`Comparing local and remote inbox state`, { localMessageCount, localLastMessage, remoteState });
|
|
436
|
+
|
|
437
|
+
return (
|
|
438
|
+
remoteState.totalMessagesInserted === localMessageCount &&
|
|
439
|
+
remoteState.messagesRollingHash.equals(localLastMessage?.rollingHash ?? Buffer16.ZERO)
|
|
440
|
+
);
|
|
441
|
+
}
|
|
407
442
|
|
|
408
|
-
|
|
443
|
+
/** Retrieves L1 to L2 messages from L1 in batches and stores them. */
|
|
444
|
+
private async retrieveAndStoreMessages(fromL1Block: bigint, toL1Block: bigint): Promise<void> {
|
|
409
445
|
let searchStartBlock: bigint = 0n;
|
|
410
|
-
let searchEndBlock: bigint =
|
|
446
|
+
let searchEndBlock: bigint = fromL1Block;
|
|
411
447
|
|
|
412
448
|
let lastMessage: InboxMessage | undefined;
|
|
413
449
|
let messageCount = 0;
|
|
414
450
|
|
|
415
451
|
do {
|
|
416
|
-
[searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock,
|
|
452
|
+
[searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, toL1Block);
|
|
417
453
|
this.log.trace(`Retrieving L1 to L2 messages in L1 blocks ${searchStartBlock}-${searchEndBlock}`);
|
|
418
454
|
const messages = await retrieveL1ToL2Messages(this.inbox, searchStartBlock, searchEndBlock);
|
|
419
455
|
const timer = new Timer();
|
|
@@ -425,81 +461,65 @@ export class ArchiverL1Synchronizer implements Traceable {
|
|
|
425
461
|
lastMessage = msg;
|
|
426
462
|
messageCount++;
|
|
427
463
|
}
|
|
428
|
-
} while (searchEndBlock <
|
|
464
|
+
} while (searchEndBlock < toL1Block);
|
|
429
465
|
|
|
430
|
-
// Log stats for messages retrieved (if any).
|
|
431
466
|
if (messageCount > 0) {
|
|
432
467
|
this.log.info(
|
|
433
468
|
`Retrieved ${messageCount} new L1 to L2 messages up to message with index ${lastMessage?.index} for checkpoint ${lastMessage?.checkpointNumber}`,
|
|
434
469
|
{ lastMessage, messageCount },
|
|
435
470
|
);
|
|
436
471
|
}
|
|
437
|
-
|
|
438
|
-
// Warn if the resulting rolling hash does not match the remote state we had retrieved.
|
|
439
|
-
if (lastMessage && !lastMessage.rollingHash.equals(remoteMessagesState.messagesRollingHash)) {
|
|
440
|
-
this.log.warn(`Last message retrieved rolling hash does not match remote state.`, {
|
|
441
|
-
lastMessage,
|
|
442
|
-
remoteMessagesState,
|
|
443
|
-
});
|
|
444
|
-
}
|
|
445
472
|
}
|
|
446
473
|
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
do {
|
|
453
|
-
[searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, currentL1BlockNumber);
|
|
454
|
-
|
|
455
|
-
const message = await retrieveL1ToL2Message(this.inbox, leaf, searchStartBlock, searchEndBlock);
|
|
456
|
-
|
|
457
|
-
if (message) {
|
|
458
|
-
return message;
|
|
459
|
-
}
|
|
460
|
-
} while (searchEndBlock < currentL1BlockNumber);
|
|
461
|
-
|
|
462
|
-
return undefined;
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
private async rollbackL1ToL2Messages(
|
|
466
|
-
localLastMessage: InboxMessage,
|
|
467
|
-
messagesSyncPoint: L1BlockId,
|
|
468
|
-
): Promise<L1BlockId> {
|
|
474
|
+
/**
|
|
475
|
+
* Rolls back local L1 to L2 messages to the last common message with L1, and updates the syncpoint to the L1 block of that message.
|
|
476
|
+
* If no common message is found, rolls back all messages and sets the syncpoint to the start block.
|
|
477
|
+
*/
|
|
478
|
+
private async rollbackL1ToL2Messages(remoteTreeInProgress: bigint): Promise<L1BlockId> {
|
|
469
479
|
// Slowly go back through our messages until we find the last common message.
|
|
470
480
|
// We could query the logs in batch as an optimization, but the depth of the reorg should not be deep, and this
|
|
471
481
|
// is a very rare case, so it's fine to query one log at a time.
|
|
472
482
|
let commonMsg: undefined | InboxMessage;
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
const
|
|
477
|
-
|
|
483
|
+
let messagesToDelete = 0;
|
|
484
|
+
this.log.verbose(`Searching most recent common L1 to L2 message`);
|
|
485
|
+
for await (const localMsg of this.store.iterateL1ToL2Messages({ reverse: true })) {
|
|
486
|
+
const remoteMsg = await retrieveL1ToL2Message(this.inbox, localMsg);
|
|
487
|
+
const logCtx = { remoteMsg, localMsg: localMsg };
|
|
488
|
+
if (remoteMsg && remoteMsg.rollingHash.equals(localMsg.rollingHash)) {
|
|
478
489
|
this.log.verbose(
|
|
479
|
-
`Found most recent common L1 to L2 message at index ${
|
|
490
|
+
`Found most recent common L1 to L2 message at index ${localMsg.index} on L1 block ${localMsg.l1BlockNumber}`,
|
|
480
491
|
logCtx,
|
|
481
492
|
);
|
|
482
493
|
commonMsg = remoteMsg;
|
|
483
494
|
break;
|
|
484
495
|
} else if (remoteMsg) {
|
|
485
|
-
this.log.debug(`Local L1 to L2 message with index ${
|
|
496
|
+
this.log.debug(`Local L1 to L2 message with index ${localMsg.index} has different rolling hash`, logCtx);
|
|
497
|
+
messagesToDelete++;
|
|
486
498
|
} else {
|
|
487
|
-
this.log.debug(`Local L1 to L2 message with index ${
|
|
499
|
+
this.log.debug(`Local L1 to L2 message with index ${localMsg.index} not found on L1`, logCtx);
|
|
500
|
+
messagesToDelete++;
|
|
488
501
|
}
|
|
489
502
|
}
|
|
490
503
|
|
|
491
|
-
// Delete everything after the common message we found.
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
504
|
+
// Delete everything after the common message we found, if anything needs to be deleted.
|
|
505
|
+
// Do not exit early if there are no messages to delete, we still want to update the syncpoint.
|
|
506
|
+
if (messagesToDelete > 0) {
|
|
507
|
+
const lastGoodIndex = commonMsg?.index;
|
|
508
|
+
this.log.warn(`Rolling back all local L1 to L2 messages after index ${lastGoodIndex ?? 'initial'}`);
|
|
509
|
+
await this.store.removeL1ToL2Messages(lastGoodIndex !== undefined ? lastGoodIndex + 1n : 0n);
|
|
510
|
+
}
|
|
495
511
|
|
|
496
512
|
// Update the syncpoint so the loop below reprocesses the changed messages. We go to the block before
|
|
497
513
|
// the last common one, so we force reprocessing it, in case new messages were added on that same L1 block
|
|
498
514
|
// after the last common message.
|
|
499
515
|
const syncPointL1BlockNumber = commonMsg ? commonMsg.l1BlockNumber - 1n : this.l1Constants.l1StartBlock;
|
|
500
516
|
const syncPointL1BlockHash = await this.getL1BlockHash(syncPointL1BlockNumber);
|
|
501
|
-
messagesSyncPoint = { l1BlockNumber: syncPointL1BlockNumber, l1BlockHash: syncPointL1BlockHash };
|
|
502
|
-
await this.store.
|
|
517
|
+
const messagesSyncPoint = { l1BlockNumber: syncPointL1BlockNumber, l1BlockHash: syncPointL1BlockHash };
|
|
518
|
+
await this.store.setMessageSyncState(messagesSyncPoint, remoteTreeInProgress);
|
|
519
|
+
this.log.verbose(`Updated messages syncpoint to L1 block ${syncPointL1BlockNumber}`, {
|
|
520
|
+
...messagesSyncPoint,
|
|
521
|
+
remoteTreeInProgress,
|
|
522
|
+
});
|
|
503
523
|
return messagesSyncPoint;
|
|
504
524
|
}
|
|
505
525
|
|
|
@@ -551,7 +571,7 @@ export class ArchiverL1Synchronizer implements Traceable {
|
|
|
551
571
|
if (provenCheckpointNumber === 0) {
|
|
552
572
|
const localProvenCheckpointNumber = await this.store.getProvenCheckpointNumber();
|
|
553
573
|
if (localProvenCheckpointNumber !== provenCheckpointNumber) {
|
|
554
|
-
await this.
|
|
574
|
+
await this.updater.setProvenCheckpointNumber(provenCheckpointNumber);
|
|
555
575
|
this.log.info(`Rolled back proven chain to checkpoint ${provenCheckpointNumber}`, { provenCheckpointNumber });
|
|
556
576
|
}
|
|
557
577
|
}
|
|
@@ -583,13 +603,13 @@ export class ArchiverL1Synchronizer implements Traceable {
|
|
|
583
603
|
) {
|
|
584
604
|
const localProvenCheckpointNumber = await this.store.getProvenCheckpointNumber();
|
|
585
605
|
if (localProvenCheckpointNumber !== provenCheckpointNumber) {
|
|
586
|
-
await this.
|
|
606
|
+
await this.updater.setProvenCheckpointNumber(provenCheckpointNumber);
|
|
587
607
|
this.log.info(`Updated proven chain to checkpoint ${provenCheckpointNumber}`, { provenCheckpointNumber });
|
|
588
608
|
const provenSlotNumber = localCheckpointForDestinationProvenCheckpointNumber.header.slotNumber;
|
|
589
609
|
const provenEpochNumber: EpochNumber = getEpochAtSlot(provenSlotNumber, this.l1Constants);
|
|
590
610
|
const lastBlockNumberInCheckpoint =
|
|
591
611
|
localCheckpointForDestinationProvenCheckpointNumber.startBlock +
|
|
592
|
-
localCheckpointForDestinationProvenCheckpointNumber.
|
|
612
|
+
localCheckpointForDestinationProvenCheckpointNumber.blockCount -
|
|
593
613
|
1;
|
|
594
614
|
|
|
595
615
|
this.events.emit(L2BlockSourceEvents.L2BlockProven, {
|
|
@@ -598,7 +618,7 @@ export class ArchiverL1Synchronizer implements Traceable {
|
|
|
598
618
|
slotNumber: provenSlotNumber,
|
|
599
619
|
epochNumber: provenEpochNumber,
|
|
600
620
|
});
|
|
601
|
-
this.instrumentation.
|
|
621
|
+
this.instrumentation.updateLastProvenCheckpoint(localCheckpointForDestinationProvenCheckpointNumber);
|
|
602
622
|
} else {
|
|
603
623
|
this.log.trace(`Proven checkpoint ${provenCheckpointNumber} already stored.`);
|
|
604
624
|
}
|
|
@@ -707,7 +727,6 @@ export class ArchiverL1Synchronizer implements Traceable {
|
|
|
707
727
|
this.blobClient,
|
|
708
728
|
searchStartBlock, // TODO(palla/reorg): If the L2 reorg was due to an L1 reorg, we need to start search earlier
|
|
709
729
|
searchEndBlock,
|
|
710
|
-
this.l1Addresses,
|
|
711
730
|
this.instrumentation,
|
|
712
731
|
this.log,
|
|
713
732
|
!initialSyncComplete, // isHistoricalSync
|
|
@@ -802,6 +821,14 @@ export class ArchiverL1Synchronizer implements Traceable {
|
|
|
802
821
|
);
|
|
803
822
|
}
|
|
804
823
|
|
|
824
|
+
for (const published of validCheckpoints) {
|
|
825
|
+
this.instrumentation.processCheckpointL1Timing({
|
|
826
|
+
slotNumber: published.checkpoint.header.slotNumber,
|
|
827
|
+
l1Timestamp: published.l1.timestamp,
|
|
828
|
+
l1Constants: this.l1Constants,
|
|
829
|
+
});
|
|
830
|
+
}
|
|
831
|
+
|
|
805
832
|
try {
|
|
806
833
|
const updatedValidationResult =
|
|
807
834
|
rollupStatus.validationResult === initialValidationResult ? undefined : rollupStatus.validationResult;
|
|
@@ -810,17 +837,20 @@ export class ArchiverL1Synchronizer implements Traceable {
|
|
|
810
837
|
this.updater.addCheckpoints(validCheckpoints, updatedValidationResult),
|
|
811
838
|
),
|
|
812
839
|
);
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
840
|
+
|
|
841
|
+
if (validCheckpoints.length > 0) {
|
|
842
|
+
this.instrumentation.processNewCheckpointedBlocks(
|
|
843
|
+
processDuration / validCheckpoints.length,
|
|
844
|
+
validCheckpoints.flatMap(c => c.checkpoint.blocks),
|
|
845
|
+
);
|
|
846
|
+
}
|
|
817
847
|
|
|
818
848
|
// If blocks were pruned due to conflict with L1 checkpoints, emit event
|
|
819
849
|
if (result.prunedBlocks && result.prunedBlocks.length > 0) {
|
|
820
850
|
const prunedCheckpointNumber = result.prunedBlocks[0].checkpointNumber;
|
|
821
851
|
const prunedSlotNumber = result.prunedBlocks[0].header.globalVariables.slotNumber;
|
|
822
852
|
|
|
823
|
-
this.log.
|
|
853
|
+
this.log.info(
|
|
824
854
|
`Pruned ${result.prunedBlocks.length} mismatching blocks for checkpoint ${prunedCheckpointNumber}`,
|
|
825
855
|
{ prunedBlocks: result.prunedBlocks.map(b => b.toBlockInfo()), prunedSlotNumber, prunedCheckpointNumber },
|
|
826
856
|
);
|
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
getAttestationInfoFromPayload,
|
|
10
10
|
} from '@aztec/stdlib/block';
|
|
11
11
|
import type { PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
|
|
12
|
-
import { type L1RollupConstants, getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
|
|
12
|
+
import { type L1RollupConstants, computeQuorum, getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
|
|
13
13
|
import { ConsensusPayload } from '@aztec/stdlib/p2p';
|
|
14
14
|
|
|
15
15
|
export type { ValidateCheckpointResult };
|
|
@@ -66,7 +66,7 @@ export async function validateCheckpointAttestations(
|
|
|
66
66
|
return { valid: true };
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
-
const requiredAttestationCount =
|
|
69
|
+
const requiredAttestationCount = computeQuorum(committee.length);
|
|
70
70
|
|
|
71
71
|
const failedValidationResult = <TReason extends ValidateCheckpointNegativeResult['reason']>(reason: TReason) => ({
|
|
72
72
|
valid: false as const,
|