@aztec/archiver 0.0.1-commit.f5d02921e → 0.0.1-commit.f650c0a5c
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 +3 -3
- package/dest/archiver.d.ts.map +1 -1
- package/dest/archiver.js +3 -3
- package/dest/l1/data_retrieval.d.ts +3 -3
- package/dest/l1/data_retrieval.d.ts.map +1 -1
- package/dest/l1/data_retrieval.js +14 -15
- package/dest/modules/l1_synchronizer.d.ts +3 -2
- package/dest/modules/l1_synchronizer.d.ts.map +1 -1
- package/dest/modules/l1_synchronizer.js +105 -105
- package/dest/store/block_store.d.ts +2 -1
- package/dest/store/block_store.d.ts.map +1 -1
- package/dest/store/block_store.js +47 -3
- package/dest/store/kv_archiver_store.d.ts +3 -7
- package/dest/store/kv_archiver_store.d.ts.map +1 -1
- package/dest/store/kv_archiver_store.js +2 -7
- package/dest/store/message_store.d.ts +3 -3
- package/dest/store/message_store.d.ts.map +1 -1
- package/dest/store/message_store.js +9 -10
- package/dest/test/fake_l1_state.d.ts +9 -1
- package/dest/test/fake_l1_state.d.ts.map +1 -1
- package/dest/test/fake_l1_state.js +41 -6
- package/dest/test/noop_l1_archiver.d.ts +1 -1
- package/dest/test/noop_l1_archiver.d.ts.map +1 -1
- package/dest/test/noop_l1_archiver.js +0 -1
- package/package.json +13 -13
- package/src/archiver.ts +8 -6
- package/src/l1/data_retrieval.ts +8 -12
- package/src/modules/l1_synchronizer.ts +112 -127
- package/src/store/block_store.ts +59 -3
- package/src/store/kv_archiver_store.ts +3 -10
- package/src/store/message_store.ts +10 -12
- package/src/structs/inbox_message.ts +1 -1
- package/src/test/fake_l1_state.ts +56 -7
- package/src/test/noop_l1_archiver.ts +0 -1
package/src/l1/data_retrieval.ts
CHANGED
|
@@ -144,7 +144,7 @@ export async function retrievedToPublishedCheckpoint({
|
|
|
144
144
|
* @param blobClient - The blob client client for fetching blob data.
|
|
145
145
|
* @param searchStartBlock - The block number to use for starting the search.
|
|
146
146
|
* @param searchEndBlock - The highest block number that we should search up to.
|
|
147
|
-
* @param contractAddresses - The contract addresses (governanceProposerAddress,
|
|
147
|
+
* @param contractAddresses - The contract addresses (governanceProposerAddress, slashingProposerAddress).
|
|
148
148
|
* @param instrumentation - The archiver instrumentation instance.
|
|
149
149
|
* @param logger - The logger instance.
|
|
150
150
|
* @param isHistoricalSync - Whether this is a historical sync.
|
|
@@ -344,14 +344,10 @@ export async function getCheckpointBlobDataFromBlobs(
|
|
|
344
344
|
/** Given an L1 to L2 message, retrieves its corresponding event from the Inbox within a specific block range. */
|
|
345
345
|
export async function retrieveL1ToL2Message(
|
|
346
346
|
inbox: InboxContract,
|
|
347
|
-
|
|
348
|
-
fromBlock: bigint,
|
|
349
|
-
toBlock: bigint,
|
|
347
|
+
message: InboxMessage,
|
|
350
348
|
): Promise<InboxMessage | undefined> {
|
|
351
|
-
const
|
|
352
|
-
|
|
353
|
-
const messages = mapLogsInboxMessage(logs);
|
|
354
|
-
return messages.length > 0 ? messages[0] : undefined;
|
|
349
|
+
const log = await inbox.getMessageSentEventByHash(message.leaf.toString(), message.l1BlockHash.toString());
|
|
350
|
+
return log && mapLogInboxMessage(log);
|
|
355
351
|
}
|
|
356
352
|
|
|
357
353
|
/**
|
|
@@ -374,22 +370,22 @@ export async function retrieveL1ToL2Messages(
|
|
|
374
370
|
break;
|
|
375
371
|
}
|
|
376
372
|
|
|
377
|
-
retrievedL1ToL2Messages.push(...
|
|
373
|
+
retrievedL1ToL2Messages.push(...messageSentLogs.map(mapLogInboxMessage));
|
|
378
374
|
searchStartBlock = messageSentLogs.at(-1)!.l1BlockNumber + 1n;
|
|
379
375
|
}
|
|
380
376
|
|
|
381
377
|
return retrievedL1ToL2Messages;
|
|
382
378
|
}
|
|
383
379
|
|
|
384
|
-
function
|
|
385
|
-
return
|
|
380
|
+
function mapLogInboxMessage(log: MessageSentLog): InboxMessage {
|
|
381
|
+
return {
|
|
386
382
|
index: log.args.index,
|
|
387
383
|
leaf: log.args.leaf,
|
|
388
384
|
l1BlockNumber: log.l1BlockNumber,
|
|
389
385
|
l1BlockHash: log.l1BlockHash,
|
|
390
386
|
checkpointNumber: log.args.checkpointNumber,
|
|
391
387
|
rollingHash: log.args.rollingHash,
|
|
392
|
-
}
|
|
388
|
+
};
|
|
393
389
|
}
|
|
394
390
|
|
|
395
391
|
/** Retrieves L2ProofVerified events from the rollup contract. */
|
|
@@ -1,6 +1,6 @@
|
|
|
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';
|
|
3
|
+
import { InboxContract, type InboxContractState, RollupContract } from '@aztec/ethereum/contracts';
|
|
4
4
|
import type { L1BlockId } from '@aztec/ethereum/l1-types';
|
|
5
5
|
import type { ViemPublicClient, ViemPublicDebugClient } from '@aztec/ethereum/types';
|
|
6
6
|
import { asyncPool } from '@aztec/foundation/async-pool';
|
|
@@ -10,9 +10,10 @@ 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
12
|
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
13
|
+
import { retryTimes } from '@aztec/foundation/retry';
|
|
13
14
|
import { count } from '@aztec/foundation/string';
|
|
14
15
|
import { DateProvider, Timer, elapsed } from '@aztec/foundation/timer';
|
|
15
|
-
import { isDefined } from '@aztec/foundation/types';
|
|
16
|
+
import { isDefined, isErrorClass } from '@aztec/foundation/types';
|
|
16
17
|
import { type ArchiverEmitter, L2BlockSourceEvents, type ValidateCheckpointResult } from '@aztec/stdlib/block';
|
|
17
18
|
import { PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
|
|
18
19
|
import { type L1RollupConstants, getEpochAtSlot, getSlotAtNextL1Block } from '@aztec/stdlib/epoch-helpers';
|
|
@@ -28,6 +29,7 @@ import {
|
|
|
28
29
|
} from '../l1/data_retrieval.js';
|
|
29
30
|
import type { KVArchiverDataStore } from '../store/kv_archiver_store.js';
|
|
30
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';
|
|
@@ -121,10 +123,15 @@ export class ArchiverL1Synchronizer implements Traceable {
|
|
|
121
123
|
|
|
122
124
|
@trackSpan('Archiver.syncFromL1')
|
|
123
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.
|
|
124
130
|
const currentL1Block = await this.publicClient.getBlock({ includeTransactions: false });
|
|
125
131
|
const currentL1BlockNumber = currentL1Block.number;
|
|
126
132
|
const currentL1BlockHash = Buffer32.fromString(currentL1Block.hash);
|
|
127
133
|
const currentL1Timestamp = currentL1Block.timestamp;
|
|
134
|
+
const currentL1BlockData = { l1BlockNumber: currentL1BlockNumber, l1BlockHash: currentL1BlockHash };
|
|
128
135
|
|
|
129
136
|
if (this.l1BlockHash && currentL1BlockHash.equals(this.l1BlockHash)) {
|
|
130
137
|
this.log.trace(`No new L1 blocks since last sync at L1 block ${this.l1BlockNumber}`);
|
|
@@ -141,45 +148,15 @@ export class ArchiverL1Synchronizer implements Traceable {
|
|
|
141
148
|
);
|
|
142
149
|
}
|
|
143
150
|
|
|
144
|
-
// Load sync point for blocks
|
|
145
|
-
const {
|
|
146
|
-
|
|
147
|
-
messagesSynchedTo = {
|
|
148
|
-
l1BlockNumber: this.l1Constants.l1StartBlock,
|
|
149
|
-
l1BlockHash: this.l1Constants.l1StartBlockHash,
|
|
150
|
-
},
|
|
151
|
-
} = 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 });
|
|
152
154
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
currentL1BlockHash,
|
|
158
|
-
});
|
|
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);
|
|
159
159
|
|
|
160
|
-
// ********** Ensuring Consistency of data pulled from L1 **********
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* There are a number of calls in this sync operation to L1 for retrieving
|
|
164
|
-
* events and transaction data. There are a couple of things we need to bear in mind
|
|
165
|
-
* to ensure that data is read exactly once.
|
|
166
|
-
*
|
|
167
|
-
* The first is the problem of eventually consistent ETH service providers like Infura.
|
|
168
|
-
* Each L1 read operation will query data from the last L1 block that it saw emit its kind of data.
|
|
169
|
-
* (so pending L1 to L2 messages will read from the last L1 block that emitted a message and so on)
|
|
170
|
-
* This will mean the archiver will lag behind L1 and will only advance when there's L2-relevant activity on the chain.
|
|
171
|
-
*
|
|
172
|
-
* The second is that in between the various calls to L1, the block number can move meaning some
|
|
173
|
-
* of the following calls will return data for blocks that were not present during earlier calls.
|
|
174
|
-
* To combat this for the time being we simply ensure that all data retrieval methods only retrieve
|
|
175
|
-
* data up to the currentBlockNumber captured at the top of this function. We might want to improve on this
|
|
176
|
-
* in future but for the time being it should give us the guarantees that we need
|
|
177
|
-
*/
|
|
178
|
-
|
|
179
|
-
// ********** Events that are processed per L1 block **********
|
|
180
|
-
await this.handleL1ToL2Messages(messagesSynchedTo, currentL1BlockNumber);
|
|
181
|
-
|
|
182
|
-
// ********** Events that are processed per checkpoint **********
|
|
183
160
|
if (currentL1BlockNumber > blocksSynchedTo) {
|
|
184
161
|
// First we retrieve new checkpoints and L2 blocks and store them in the DB. This will also update the
|
|
185
162
|
// pending chain validation status, proven checkpoint number, and synched L1 block number.
|
|
@@ -290,6 +267,7 @@ export class ArchiverL1Synchronizer implements Traceable {
|
|
|
290
267
|
`Pruning blocks after block ${lastCheckpointedBlockNumber} due to slot ${firstUncheckpointedBlockSlot} not being checkpointed`,
|
|
291
268
|
{ firstUncheckpointedBlockHeader: firstUncheckpointedBlockHeader.toInspect(), slotAtNextL1Block },
|
|
292
269
|
);
|
|
270
|
+
|
|
293
271
|
const prunedBlocks = await this.updater.removeUncheckpointedBlocksAfter(lastCheckpointedBlockNumber);
|
|
294
272
|
|
|
295
273
|
if (prunedBlocks.length > 0) {
|
|
@@ -391,64 +369,87 @@ export class ArchiverL1Synchronizer implements Traceable {
|
|
|
391
369
|
}
|
|
392
370
|
|
|
393
371
|
@trackSpan('Archiver.handleL1ToL2Messages')
|
|
394
|
-
private async handleL1ToL2Messages(
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
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;
|
|
398
385
|
}
|
|
399
386
|
|
|
400
|
-
//
|
|
401
|
-
const localMessagesInserted = await this.store.getTotalL1ToL2MessageCount();
|
|
402
|
-
const localLastMessage = await this.store.getLastL1ToL2Message();
|
|
387
|
+
// Compare local message store state with the remote. If they match, we just advance the match pointer.
|
|
403
388
|
const remoteMessagesState = await this.inbox.getState({ blockNumber: currentL1BlockNumber });
|
|
404
|
-
await this.store.
|
|
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
|
+
}
|
|
405
395
|
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
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
|
+
}
|
|
411
413
|
|
|
412
|
-
//
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
) {
|
|
417
|
-
this.log.
|
|
418
|
-
`
|
|
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 },
|
|
419
422
|
);
|
|
420
|
-
|
|
423
|
+
await this.rollbackL1ToL2Messages(remoteMessagesState.treeInProgress);
|
|
424
|
+
return false;
|
|
421
425
|
}
|
|
422
426
|
|
|
423
|
-
//
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
const remoteLastMessage = await this.retrieveL1ToL2Message(localLastMessage.leaf);
|
|
428
|
-
this.log.trace(`Retrieved remote message for local last`, { remoteLastMessage, localLastMessage });
|
|
429
|
-
if (!remoteLastMessage || !remoteLastMessage.rollingHash.equals(localLastMessage.rollingHash)) {
|
|
430
|
-
this.log.warn(`Rolling back L1 to L2 messages due to hash mismatch or msg not found.`, {
|
|
431
|
-
remoteLastMessage,
|
|
432
|
-
messagesSyncPoint,
|
|
433
|
-
localLastMessage,
|
|
434
|
-
});
|
|
427
|
+
// Advance the syncpoint after a successful sync
|
|
428
|
+
await this.store.setMessageSyncState(currentL1Block, remoteMessagesState.treeInProgress);
|
|
429
|
+
return true;
|
|
430
|
+
}
|
|
435
431
|
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
}
|
|
441
|
-
}
|
|
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 });
|
|
442
436
|
|
|
443
|
-
|
|
437
|
+
return (
|
|
438
|
+
remoteState.totalMessagesInserted === localMessageCount &&
|
|
439
|
+
remoteState.messagesRollingHash.equals(localLastMessage?.rollingHash ?? Buffer16.ZERO)
|
|
440
|
+
);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/** Retrieves L1 to L2 messages from L1 in batches and stores them. */
|
|
444
|
+
private async retrieveAndStoreMessages(fromL1Block: bigint, toL1Block: bigint): Promise<void> {
|
|
444
445
|
let searchStartBlock: bigint = 0n;
|
|
445
|
-
let searchEndBlock: bigint =
|
|
446
|
+
let searchEndBlock: bigint = fromL1Block;
|
|
446
447
|
|
|
447
448
|
let lastMessage: InboxMessage | undefined;
|
|
448
449
|
let messageCount = 0;
|
|
449
450
|
|
|
450
451
|
do {
|
|
451
|
-
[searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock,
|
|
452
|
+
[searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, toL1Block);
|
|
452
453
|
this.log.trace(`Retrieving L1 to L2 messages in L1 blocks ${searchStartBlock}-${searchEndBlock}`);
|
|
453
454
|
const messages = await retrieveL1ToL2Messages(this.inbox, searchStartBlock, searchEndBlock);
|
|
454
455
|
const timer = new Timer();
|
|
@@ -460,81 +461,65 @@ export class ArchiverL1Synchronizer implements Traceable {
|
|
|
460
461
|
lastMessage = msg;
|
|
461
462
|
messageCount++;
|
|
462
463
|
}
|
|
463
|
-
} while (searchEndBlock <
|
|
464
|
+
} while (searchEndBlock < toL1Block);
|
|
464
465
|
|
|
465
|
-
// Log stats for messages retrieved (if any).
|
|
466
466
|
if (messageCount > 0) {
|
|
467
467
|
this.log.info(
|
|
468
468
|
`Retrieved ${messageCount} new L1 to L2 messages up to message with index ${lastMessage?.index} for checkpoint ${lastMessage?.checkpointNumber}`,
|
|
469
469
|
{ lastMessage, messageCount },
|
|
470
470
|
);
|
|
471
471
|
}
|
|
472
|
-
|
|
473
|
-
// Warn if the resulting rolling hash does not match the remote state we had retrieved.
|
|
474
|
-
if (lastMessage && !lastMessage.rollingHash.equals(remoteMessagesState.messagesRollingHash)) {
|
|
475
|
-
this.log.warn(`Last message retrieved rolling hash does not match remote state.`, {
|
|
476
|
-
lastMessage,
|
|
477
|
-
remoteMessagesState,
|
|
478
|
-
});
|
|
479
|
-
}
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
private async retrieveL1ToL2Message(leaf: Fr): Promise<InboxMessage | undefined> {
|
|
483
|
-
const currentL1BlockNumber = await this.publicClient.getBlockNumber();
|
|
484
|
-
let searchStartBlock: bigint = 0n;
|
|
485
|
-
let searchEndBlock: bigint = this.l1Constants.l1StartBlock - 1n;
|
|
486
|
-
|
|
487
|
-
do {
|
|
488
|
-
[searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, currentL1BlockNumber);
|
|
489
|
-
|
|
490
|
-
const message = await retrieveL1ToL2Message(this.inbox, leaf, searchStartBlock, searchEndBlock);
|
|
491
|
-
|
|
492
|
-
if (message) {
|
|
493
|
-
return message;
|
|
494
|
-
}
|
|
495
|
-
} while (searchEndBlock < currentL1BlockNumber);
|
|
496
|
-
|
|
497
|
-
return undefined;
|
|
498
472
|
}
|
|
499
473
|
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
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> {
|
|
504
479
|
// Slowly go back through our messages until we find the last common message.
|
|
505
480
|
// We could query the logs in batch as an optimization, but the depth of the reorg should not be deep, and this
|
|
506
481
|
// is a very rare case, so it's fine to query one log at a time.
|
|
507
482
|
let commonMsg: undefined | InboxMessage;
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
const
|
|
512
|
-
|
|
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)) {
|
|
513
489
|
this.log.verbose(
|
|
514
|
-
`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}`,
|
|
515
491
|
logCtx,
|
|
516
492
|
);
|
|
517
493
|
commonMsg = remoteMsg;
|
|
518
494
|
break;
|
|
519
495
|
} else if (remoteMsg) {
|
|
520
|
-
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++;
|
|
521
498
|
} else {
|
|
522
|
-
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++;
|
|
523
501
|
}
|
|
524
502
|
}
|
|
525
503
|
|
|
526
|
-
// Delete everything after the common message we found.
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
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
|
+
}
|
|
530
511
|
|
|
531
512
|
// Update the syncpoint so the loop below reprocesses the changed messages. We go to the block before
|
|
532
513
|
// the last common one, so we force reprocessing it, in case new messages were added on that same L1 block
|
|
533
514
|
// after the last common message.
|
|
534
515
|
const syncPointL1BlockNumber = commonMsg ? commonMsg.l1BlockNumber - 1n : this.l1Constants.l1StartBlock;
|
|
535
516
|
const syncPointL1BlockHash = await this.getL1BlockHash(syncPointL1BlockNumber);
|
|
536
|
-
messagesSyncPoint = { l1BlockNumber: syncPointL1BlockNumber, l1BlockHash: syncPointL1BlockHash };
|
|
537
|
-
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
|
+
});
|
|
538
523
|
return messagesSyncPoint;
|
|
539
524
|
}
|
|
540
525
|
|
package/src/store/block_store.ts
CHANGED
|
@@ -262,16 +262,28 @@ export class BlockStore {
|
|
|
262
262
|
}
|
|
263
263
|
|
|
264
264
|
return await this.db.transactionAsync(async () => {
|
|
265
|
-
// Check that the checkpoint immediately before the first block to be added is present in the store.
|
|
266
265
|
const firstCheckpointNumber = checkpoints[0].checkpoint.number;
|
|
267
266
|
const previousCheckpointNumber = await this.getLatestCheckpointNumber();
|
|
268
267
|
|
|
269
|
-
|
|
268
|
+
// Handle already-stored checkpoints at the start of the batch.
|
|
269
|
+
// This can happen after an L1 reorg re-includes a checkpoint in a different L1 block.
|
|
270
|
+
// We accept them if archives match (same content) and update their L1 metadata.
|
|
271
|
+
if (!opts.force && firstCheckpointNumber <= previousCheckpointNumber) {
|
|
272
|
+
checkpoints = await this.skipOrUpdateAlreadyStoredCheckpoints(checkpoints, previousCheckpointNumber);
|
|
273
|
+
if (checkpoints.length === 0) {
|
|
274
|
+
return true;
|
|
275
|
+
}
|
|
276
|
+
// Re-check sequentiality after skipping
|
|
277
|
+
const newFirstNumber = checkpoints[0].checkpoint.number;
|
|
278
|
+
if (previousCheckpointNumber !== newFirstNumber - 1) {
|
|
279
|
+
throw new InitialCheckpointNumberNotSequentialError(newFirstNumber, previousCheckpointNumber);
|
|
280
|
+
}
|
|
281
|
+
} else if (previousCheckpointNumber !== firstCheckpointNumber - 1 && !opts.force) {
|
|
270
282
|
throw new InitialCheckpointNumberNotSequentialError(firstCheckpointNumber, previousCheckpointNumber);
|
|
271
283
|
}
|
|
272
284
|
|
|
273
285
|
// Get the last block of the previous checkpoint for archive chaining
|
|
274
|
-
let previousBlock = await this.getPreviousCheckpointBlock(
|
|
286
|
+
let previousBlock = await this.getPreviousCheckpointBlock(checkpoints[0].checkpoint.number);
|
|
275
287
|
|
|
276
288
|
// Iterate over checkpoints array and insert them, checking that the block numbers are sequential.
|
|
277
289
|
let previousCheckpoint: PublishedCheckpoint | undefined = undefined;
|
|
@@ -322,6 +334,50 @@ export class BlockStore {
|
|
|
322
334
|
});
|
|
323
335
|
}
|
|
324
336
|
|
|
337
|
+
/**
|
|
338
|
+
* Handles checkpoints at the start of a batch that are already stored (e.g. due to L1 reorg).
|
|
339
|
+
* Verifies the archive root matches, updates L1 metadata, and returns only the new checkpoints.
|
|
340
|
+
*/
|
|
341
|
+
private async skipOrUpdateAlreadyStoredCheckpoints(
|
|
342
|
+
checkpoints: PublishedCheckpoint[],
|
|
343
|
+
latestStored: CheckpointNumber,
|
|
344
|
+
): Promise<PublishedCheckpoint[]> {
|
|
345
|
+
let i = 0;
|
|
346
|
+
for (; i < checkpoints.length && checkpoints[i].checkpoint.number <= latestStored; i++) {
|
|
347
|
+
const incoming = checkpoints[i];
|
|
348
|
+
const stored = await this.getCheckpointData(incoming.checkpoint.number);
|
|
349
|
+
if (!stored) {
|
|
350
|
+
// Should not happen if latestStored is correct, but be safe
|
|
351
|
+
break;
|
|
352
|
+
}
|
|
353
|
+
// Verify the checkpoint content matches (archive root)
|
|
354
|
+
if (!stored.archive.root.equals(incoming.checkpoint.archive.root)) {
|
|
355
|
+
throw new Error(
|
|
356
|
+
`Checkpoint ${incoming.checkpoint.number} already exists in store but with a different archive root. ` +
|
|
357
|
+
`Stored: ${stored.archive.root}, incoming: ${incoming.checkpoint.archive.root}`,
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
// Update L1 metadata and attestations for the already-stored checkpoint
|
|
361
|
+
this.#log.warn(
|
|
362
|
+
`Checkpoint ${incoming.checkpoint.number} already stored, updating L1 info ` +
|
|
363
|
+
`(L1 block ${stored.l1.blockNumber} -> ${incoming.l1.blockNumber})`,
|
|
364
|
+
);
|
|
365
|
+
await this.#checkpoints.set(incoming.checkpoint.number, {
|
|
366
|
+
header: incoming.checkpoint.header.toBuffer(),
|
|
367
|
+
archive: incoming.checkpoint.archive.toBuffer(),
|
|
368
|
+
checkpointOutHash: incoming.checkpoint.getCheckpointOutHash().toBuffer(),
|
|
369
|
+
l1: incoming.l1.toBuffer(),
|
|
370
|
+
attestations: incoming.attestations.map(a => a.toBuffer()),
|
|
371
|
+
checkpointNumber: incoming.checkpoint.number,
|
|
372
|
+
startBlock: incoming.checkpoint.blocks[0].number,
|
|
373
|
+
blockCount: incoming.checkpoint.blocks.length,
|
|
374
|
+
});
|
|
375
|
+
// Update the sync point to reflect the new L1 block
|
|
376
|
+
await this.#lastSynchedL1Block.set(incoming.l1.blockNumber);
|
|
377
|
+
}
|
|
378
|
+
return checkpoints.slice(i);
|
|
379
|
+
}
|
|
380
|
+
|
|
325
381
|
/**
|
|
326
382
|
* Gets the last block of the checkpoint before the given one.
|
|
327
383
|
* Returns undefined if there is no previous checkpoint (i.e. genesis).
|
|
@@ -558,13 +558,6 @@ export class KVArchiverDataStore implements ContractDataSource {
|
|
|
558
558
|
await this.#blockStore.setSynchedL1BlockNumber(l1BlockNumber);
|
|
559
559
|
}
|
|
560
560
|
|
|
561
|
-
/**
|
|
562
|
-
* Stores the l1 block that messages have been synched until
|
|
563
|
-
*/
|
|
564
|
-
async setMessageSynchedL1Block(l1Block: L1BlockId) {
|
|
565
|
-
await this.#messageStore.setSynchedL1Block(l1Block);
|
|
566
|
-
}
|
|
567
|
-
|
|
568
561
|
/**
|
|
569
562
|
* Returns the number of the most recent proven block
|
|
570
563
|
* @returns The number of the most recent proven block
|
|
@@ -597,9 +590,9 @@ export class KVArchiverDataStore implements ContractDataSource {
|
|
|
597
590
|
return this.#messageStore.rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber);
|
|
598
591
|
}
|
|
599
592
|
|
|
600
|
-
/**
|
|
601
|
-
public
|
|
602
|
-
return this.#messageStore.
|
|
593
|
+
/** Atomically updates the message sync state: the L1 sync point and the inbox tree-in-progress marker. */
|
|
594
|
+
public setMessageSyncState(l1Block: L1BlockId, treeInProgress: bigint | undefined): Promise<void> {
|
|
595
|
+
return this.#messageStore.setMessageSyncState(l1Block, treeInProgress);
|
|
603
596
|
}
|
|
604
597
|
|
|
605
598
|
/** Returns an async iterator to all L1 to L2 messages on the range. */
|
|
@@ -161,15 +161,6 @@ export class MessageStore {
|
|
|
161
161
|
lastMessage = message;
|
|
162
162
|
}
|
|
163
163
|
|
|
164
|
-
// Update the L1 sync point to that of the last message added.
|
|
165
|
-
const currentSyncPoint = await this.getSynchedL1Block();
|
|
166
|
-
if (!currentSyncPoint || currentSyncPoint.l1BlockNumber < lastMessage!.l1BlockNumber) {
|
|
167
|
-
await this.setSynchedL1Block({
|
|
168
|
-
l1BlockNumber: lastMessage!.l1BlockNumber,
|
|
169
|
-
l1BlockHash: lastMessage!.l1BlockHash,
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
|
|
173
164
|
// Update total message count with the number of inserted messages.
|
|
174
165
|
await this.increaseTotalMessageCount(messageCount);
|
|
175
166
|
});
|
|
@@ -194,9 +185,16 @@ export class MessageStore {
|
|
|
194
185
|
return this.#inboxTreeInProgress.getAsync();
|
|
195
186
|
}
|
|
196
187
|
|
|
197
|
-
/**
|
|
198
|
-
public
|
|
199
|
-
|
|
188
|
+
/** Atomically updates the message sync state: the L1 sync point and the inbox tree-in-progress marker. */
|
|
189
|
+
public setMessageSyncState(l1Block: L1BlockId, treeInProgress: bigint | undefined): Promise<void> {
|
|
190
|
+
return this.db.transactionAsync(async () => {
|
|
191
|
+
await this.setSynchedL1Block(l1Block);
|
|
192
|
+
if (treeInProgress !== undefined) {
|
|
193
|
+
await this.#inboxTreeInProgress.set(treeInProgress);
|
|
194
|
+
} else {
|
|
195
|
+
await this.#inboxTreeInProgress.delete();
|
|
196
|
+
}
|
|
197
|
+
});
|
|
200
198
|
}
|
|
201
199
|
|
|
202
200
|
public async getL1ToL2Messages(checkpointNumber: CheckpointNumber): Promise<Fr[]> {
|