@aztec/archiver 0.0.1-commit.684755437 → 0.0.1-commit.6b113946b
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.d.ts +3 -6
- package/dest/archiver.d.ts.map +1 -1
- package/dest/archiver.js +40 -18
- 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 +21 -9
- package/dest/errors.d.ts.map +1 -1
- package/dest/errors.js +27 -14
- package/dest/factory.d.ts +2 -3
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +12 -12
- package/dest/modules/data_source_base.d.ts +5 -5
- package/dest/modules/data_source_base.d.ts.map +1 -1
- package/dest/modules/data_source_base.js +5 -5
- package/dest/modules/data_store_updater.d.ts +10 -5
- package/dest/modules/data_store_updater.d.ts.map +1 -1
- package/dest/modules/data_store_updater.js +23 -12
- package/dest/modules/l1_synchronizer.d.ts +2 -2
- package/dest/modules/l1_synchronizer.d.ts.map +1 -1
- package/dest/modules/l1_synchronizer.js +34 -6
- package/dest/store/block_store.d.ts +12 -13
- package/dest/store/block_store.d.ts.map +1 -1
- package/dest/store/block_store.js +61 -61
- package/dest/store/kv_archiver_store.d.ts +23 -10
- package/dest/store/kv_archiver_store.d.ts.map +1 -1
- package/dest/store/kv_archiver_store.js +30 -13
- 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 +93 -16
- 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 +13 -0
- package/dest/test/fake_l1_state.d.ts +8 -1
- package/dest/test/fake_l1_state.d.ts.map +1 -1
- package/dest/test/fake_l1_state.js +39 -5
- package/dest/test/mock_l2_block_source.d.ts +3 -3
- package/dest/test/mock_l2_block_source.d.ts.map +1 -1
- package/dest/test/mock_l2_block_source.js +4 -4
- package/dest/test/noop_l1_archiver.d.ts +4 -1
- package/dest/test/noop_l1_archiver.d.ts.map +1 -1
- package/dest/test/noop_l1_archiver.js +5 -1
- package/package.json +13 -13
- package/src/archiver.ts +39 -19
- package/src/config.ts +8 -1
- package/src/errors.ts +40 -24
- package/src/factory.ts +7 -7
- package/src/modules/data_source_base.ts +11 -6
- package/src/modules/data_store_updater.ts +24 -13
- package/src/modules/l1_synchronizer.ts +40 -12
- package/src/store/block_store.ts +72 -69
- package/src/store/kv_archiver_store.ts +38 -12
- package/src/store/log_store.ts +126 -27
- package/src/store/message_store.ts +19 -0
- package/src/test/fake_l1_state.ts +50 -9
- package/src/test/mock_l2_block_source.ts +9 -3
- package/src/test/noop_l1_archiver.ts +7 -1
package/src/factory.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { EpochCache } from '@aztec/epoch-cache';
|
|
2
2
|
import { createEthereumChain } from '@aztec/ethereum/chain';
|
|
3
|
+
import { makeL1HttpTransport } from '@aztec/ethereum/client';
|
|
3
4
|
import { InboxContract, RollupContract } from '@aztec/ethereum/contracts';
|
|
4
5
|
import type { ViemPublicDebugClient } from '@aztec/ethereum/types';
|
|
5
6
|
import { BlockNumber } from '@aztec/foundation/branded-types';
|
|
@@ -13,12 +14,11 @@ import { BundledProtocolContractsProvider } from '@aztec/protocol-contracts/prov
|
|
|
13
14
|
import { FunctionType, decodeFunctionSignature } from '@aztec/stdlib/abi';
|
|
14
15
|
import type { ArchiverEmitter } from '@aztec/stdlib/block';
|
|
15
16
|
import { type ContractClassPublic, computePublicBytecodeCommitment } from '@aztec/stdlib/contract';
|
|
16
|
-
import type { L1RollupConstants } from '@aztec/stdlib/epoch-helpers';
|
|
17
17
|
import type { DataStoreConfig } from '@aztec/stdlib/kv-store';
|
|
18
18
|
import { getTelemetryClient } from '@aztec/telemetry-client';
|
|
19
19
|
|
|
20
20
|
import { EventEmitter } from 'events';
|
|
21
|
-
import { createPublicClient
|
|
21
|
+
import { createPublicClient } from 'viem';
|
|
22
22
|
|
|
23
23
|
import { Archiver, type ArchiverDeps } from './archiver.js';
|
|
24
24
|
import { type ArchiverConfig, mapArchiverConfig } from './config.js';
|
|
@@ -32,14 +32,13 @@ export const ARCHIVER_STORE_NAME = 'archiver';
|
|
|
32
32
|
/** Creates an archiver store. */
|
|
33
33
|
export async function createArchiverStore(
|
|
34
34
|
userConfig: Pick<ArchiverConfig, 'archiverStoreMapSizeKb' | 'maxLogs'> & DataStoreConfig,
|
|
35
|
-
l1Constants: Pick<L1RollupConstants, 'epochDuration'>,
|
|
36
35
|
) {
|
|
37
36
|
const config = {
|
|
38
37
|
...userConfig,
|
|
39
38
|
dataStoreMapSizeKb: userConfig.archiverStoreMapSizeKb ?? userConfig.dataStoreMapSizeKb,
|
|
40
39
|
};
|
|
41
40
|
const store = await createStore(ARCHIVER_STORE_NAME, ARCHIVER_DB_VERSION, config);
|
|
42
|
-
return new KVArchiverDataStore(store, config.maxLogs
|
|
41
|
+
return new KVArchiverDataStore(store, config.maxLogs);
|
|
43
42
|
}
|
|
44
43
|
|
|
45
44
|
/**
|
|
@@ -54,14 +53,15 @@ export async function createArchiver(
|
|
|
54
53
|
deps: ArchiverDeps,
|
|
55
54
|
opts: { blockUntilSync: boolean } = { blockUntilSync: true },
|
|
56
55
|
): Promise<Archiver> {
|
|
57
|
-
const archiverStore = await createArchiverStore(config
|
|
56
|
+
const archiverStore = await createArchiverStore(config);
|
|
58
57
|
await registerProtocolContracts(archiverStore);
|
|
59
58
|
|
|
60
59
|
// Create Ethereum clients
|
|
61
60
|
const chain = createEthereumChain(config.l1RpcUrls, config.l1ChainId);
|
|
61
|
+
const httpTimeout = config.l1HttpTimeoutMS;
|
|
62
62
|
const publicClient = createPublicClient({
|
|
63
63
|
chain: chain.chainInfo,
|
|
64
|
-
transport:
|
|
64
|
+
transport: makeL1HttpTransport(config.l1RpcUrls, { timeout: httpTimeout }),
|
|
65
65
|
pollingInterval: config.viemPollingIntervalMS,
|
|
66
66
|
});
|
|
67
67
|
|
|
@@ -69,7 +69,7 @@ export async function createArchiver(
|
|
|
69
69
|
const debugRpcUrls = config.l1DebugRpcUrls.length > 0 ? config.l1DebugRpcUrls : config.l1RpcUrls;
|
|
70
70
|
const debugClient = createPublicClient({
|
|
71
71
|
chain: chain.chainInfo,
|
|
72
|
-
transport:
|
|
72
|
+
transport: makeL1HttpTransport(debugRpcUrls, { timeout: httpTimeout }),
|
|
73
73
|
pollingInterval: config.viemPollingIntervalMS,
|
|
74
74
|
}) as ViemPublicDebugClient;
|
|
75
75
|
|
|
@@ -46,9 +46,9 @@ export abstract class ArchiverDataSourceBase
|
|
|
46
46
|
|
|
47
47
|
abstract getL2Tips(): Promise<L2Tips>;
|
|
48
48
|
|
|
49
|
-
abstract
|
|
49
|
+
abstract getSyncedL2SlotNumber(): Promise<SlotNumber | undefined>;
|
|
50
50
|
|
|
51
|
-
abstract
|
|
51
|
+
abstract getSyncedL2EpochNumber(): Promise<EpochNumber | undefined>;
|
|
52
52
|
|
|
53
53
|
abstract isEpochComplete(epochNumber: EpochNumber): Promise<boolean>;
|
|
54
54
|
|
|
@@ -154,7 +154,7 @@ export abstract class ArchiverDataSourceBase
|
|
|
154
154
|
}
|
|
155
155
|
|
|
156
156
|
public getSettledTxReceipt(txHash: TxHash): Promise<TxReceipt | undefined> {
|
|
157
|
-
return this.store.getSettledTxReceipt(txHash);
|
|
157
|
+
return this.store.getSettledTxReceipt(txHash, this.l1Constants);
|
|
158
158
|
}
|
|
159
159
|
|
|
160
160
|
public isPendingChainInvalid(): Promise<boolean> {
|
|
@@ -165,16 +165,21 @@ export abstract class ArchiverDataSourceBase
|
|
|
165
165
|
return (await this.store.getPendingChainValidationStatus()) ?? { valid: true };
|
|
166
166
|
}
|
|
167
167
|
|
|
168
|
-
public getPrivateLogsByTags(
|
|
169
|
-
|
|
168
|
+
public getPrivateLogsByTags(
|
|
169
|
+
tags: SiloedTag[],
|
|
170
|
+
page?: number,
|
|
171
|
+
upToBlockNumber?: BlockNumber,
|
|
172
|
+
): Promise<TxScopedL2Log[][]> {
|
|
173
|
+
return this.store.getPrivateLogsByTags(tags, page, upToBlockNumber);
|
|
170
174
|
}
|
|
171
175
|
|
|
172
176
|
public getPublicLogsByTagsFromContract(
|
|
173
177
|
contractAddress: AztecAddress,
|
|
174
178
|
tags: Tag[],
|
|
175
179
|
page?: number,
|
|
180
|
+
upToBlockNumber?: BlockNumber,
|
|
176
181
|
): Promise<TxScopedL2Log[][]> {
|
|
177
|
-
return this.store.getPublicLogsByTagsFromContract(contractAddress, tags, page);
|
|
182
|
+
return this.store.getPublicLogsByTagsFromContract(contractAddress, tags, page, upToBlockNumber);
|
|
178
183
|
}
|
|
179
184
|
|
|
180
185
|
public getPublicLogs(filter: LogFilter): Promise<GetPublicLogsResponse> {
|
|
@@ -52,29 +52,29 @@ export class ArchiverDataStoreUpdater {
|
|
|
52
52
|
) {}
|
|
53
53
|
|
|
54
54
|
/**
|
|
55
|
-
* Adds proposed
|
|
56
|
-
*
|
|
55
|
+
* Adds a proposed block to the store with contract class/instance extraction from logs.
|
|
56
|
+
* This is an uncheckpointed block that has been proposed by the sequencer but not yet included in a checkpoint on L1.
|
|
57
57
|
* Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events,
|
|
58
58
|
* and individually broadcasted functions from the block logs.
|
|
59
59
|
*
|
|
60
|
-
* @param
|
|
60
|
+
* @param block - The proposed L2 block to add.
|
|
61
61
|
* @param pendingChainValidationStatus - Optional validation status to set.
|
|
62
62
|
* @returns True if the operation is successful.
|
|
63
63
|
*/
|
|
64
|
-
public async
|
|
65
|
-
|
|
64
|
+
public async addProposedBlock(
|
|
65
|
+
block: L2Block,
|
|
66
66
|
pendingChainValidationStatus?: ValidateCheckpointResult,
|
|
67
67
|
): Promise<boolean> {
|
|
68
68
|
const result = await this.store.transactionAsync(async () => {
|
|
69
|
-
await this.store.
|
|
69
|
+
await this.store.addProposedBlock(block);
|
|
70
70
|
|
|
71
71
|
const opResults = await Promise.all([
|
|
72
72
|
// Update the pending chain validation status if provided
|
|
73
73
|
pendingChainValidationStatus && this.store.setPendingChainValidationStatus(pendingChainValidationStatus),
|
|
74
|
-
// Add any logs emitted during the retrieved
|
|
75
|
-
this.store.addLogs(
|
|
76
|
-
// Unroll all logs emitted during the retrieved
|
|
77
|
-
|
|
74
|
+
// Add any logs emitted during the retrieved block
|
|
75
|
+
this.store.addLogs([block]),
|
|
76
|
+
// Unroll all logs emitted during the retrieved block and extract any contract classes and instances from it
|
|
77
|
+
this.addContractDataToDb(block),
|
|
78
78
|
]);
|
|
79
79
|
|
|
80
80
|
await this.l2TipsCache?.refresh();
|
|
@@ -108,7 +108,7 @@ export class ArchiverDataStoreUpdater {
|
|
|
108
108
|
|
|
109
109
|
await this.store.addCheckpoints(checkpoints);
|
|
110
110
|
|
|
111
|
-
// Filter out blocks that were already inserted via
|
|
111
|
+
// Filter out blocks that were already inserted via addProposedBlock() to avoid duplicating logs/contract data
|
|
112
112
|
const newBlocks = checkpoints
|
|
113
113
|
.flatMap((ch: PublishedCheckpoint) => ch.checkpoint.blocks)
|
|
114
114
|
.filter(b => lastAlreadyInsertedBlockNumber === undefined || b.number > lastAlreadyInsertedBlockNumber);
|
|
@@ -178,7 +178,7 @@ export class ArchiverDataStoreUpdater {
|
|
|
178
178
|
this.log.verbose(`Block number ${blockNumber} already inserted and matches checkpoint`, blockInfos);
|
|
179
179
|
lastAlreadyInsertedBlockNumber = blockNumber;
|
|
180
180
|
} else {
|
|
181
|
-
this.log.
|
|
181
|
+
this.log.info(`Conflict detected at block ${blockNumber} between checkpointed and local block`, blockInfos);
|
|
182
182
|
const prunedBlocks = await this.removeBlocksAfter(BlockNumber(blockNumber - 1));
|
|
183
183
|
return { prunedBlocks, lastAlreadyInsertedBlockNumber };
|
|
184
184
|
}
|
|
@@ -281,6 +281,17 @@ export class ArchiverDataStoreUpdater {
|
|
|
281
281
|
});
|
|
282
282
|
}
|
|
283
283
|
|
|
284
|
+
/**
|
|
285
|
+
* Updates the finalized checkpoint number and refreshes the L2 tips cache.
|
|
286
|
+
* @param checkpointNumber - The checkpoint number to set as finalized.
|
|
287
|
+
*/
|
|
288
|
+
public async setFinalizedCheckpointNumber(checkpointNumber: CheckpointNumber): Promise<void> {
|
|
289
|
+
await this.store.transactionAsync(async () => {
|
|
290
|
+
await this.store.setFinalizedCheckpointNumber(checkpointNumber);
|
|
291
|
+
await this.l2TipsCache?.refresh();
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
|
|
284
295
|
/** Extracts and stores contract data from a single block. */
|
|
285
296
|
private addContractDataToDb(block: L2Block): Promise<boolean> {
|
|
286
297
|
return this.updateContractDataOnDb(block, Operation.Store);
|
|
@@ -446,7 +457,7 @@ export class ArchiverDataStoreUpdater {
|
|
|
446
457
|
if (validFnCount > 0) {
|
|
447
458
|
this.log.verbose(`Storing ${validFnCount} functions for contract class ${contractClassId.toString()}`);
|
|
448
459
|
}
|
|
449
|
-
|
|
460
|
+
await this.store.addFunctions(contractClassId, validPrivateFns, validUtilityFns);
|
|
450
461
|
}
|
|
451
462
|
return true;
|
|
452
463
|
}
|
|
@@ -3,6 +3,7 @@ import { EpochCache } from '@aztec/epoch-cache';
|
|
|
3
3
|
import { InboxContract, 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
|
+
import { asyncPool } from '@aztec/foundation/async-pool';
|
|
6
7
|
import { maxBigint } from '@aztec/foundation/bigint';
|
|
7
8
|
import { BlockNumber, CheckpointNumber, EpochNumber } from '@aztec/foundation/branded-types';
|
|
8
9
|
import { Buffer32 } from '@aztec/foundation/buffer';
|
|
@@ -72,7 +73,6 @@ export class ArchiverL1Synchronizer implements Traceable {
|
|
|
72
73
|
private readonly l1Constants: L1RollupConstants & {
|
|
73
74
|
l1StartBlockHash: Buffer32;
|
|
74
75
|
genesisArchiveRoot: Fr;
|
|
75
|
-
rollupManaLimit?: number;
|
|
76
76
|
},
|
|
77
77
|
private readonly events: ArchiverEmitter,
|
|
78
78
|
tracer: Tracer,
|
|
@@ -217,6 +217,9 @@ export class ArchiverL1Synchronizer implements Traceable {
|
|
|
217
217
|
this.instrumentation.updateL1BlockHeight(currentL1BlockNumber);
|
|
218
218
|
}
|
|
219
219
|
|
|
220
|
+
// Update the finalized L2 checkpoint based on L1 finality.
|
|
221
|
+
await this.updateFinalizedCheckpoint();
|
|
222
|
+
|
|
220
223
|
// After syncing has completed, update the current l1 block number and timestamp,
|
|
221
224
|
// otherwise we risk announcing to the world that we've synced to a given point,
|
|
222
225
|
// but the corresponding blocks have not been processed (see #12631).
|
|
@@ -232,6 +235,27 @@ export class ArchiverL1Synchronizer implements Traceable {
|
|
|
232
235
|
});
|
|
233
236
|
}
|
|
234
237
|
|
|
238
|
+
/** Query L1 for its finalized block and update the finalized checkpoint accordingly. */
|
|
239
|
+
private async updateFinalizedCheckpoint(): Promise<void> {
|
|
240
|
+
try {
|
|
241
|
+
const finalizedL1Block = await this.publicClient.getBlock({ blockTag: 'finalized', includeTransactions: false });
|
|
242
|
+
const finalizedL1BlockNumber = finalizedL1Block.number;
|
|
243
|
+
const finalizedCheckpointNumber = await this.rollup.getProvenCheckpointNumber({
|
|
244
|
+
blockNumber: finalizedL1BlockNumber,
|
|
245
|
+
});
|
|
246
|
+
const localFinalizedCheckpointNumber = await this.store.getFinalizedCheckpointNumber();
|
|
247
|
+
if (localFinalizedCheckpointNumber !== finalizedCheckpointNumber) {
|
|
248
|
+
await this.updater.setFinalizedCheckpointNumber(finalizedCheckpointNumber);
|
|
249
|
+
this.log.info(`Updated finalized chain to checkpoint ${finalizedCheckpointNumber}`, {
|
|
250
|
+
finalizedCheckpointNumber,
|
|
251
|
+
finalizedL1BlockNumber,
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
} catch (err) {
|
|
255
|
+
this.log.warn(`Failed to update finalized checkpoint: ${err}`);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
235
259
|
/** Prune all proposed local blocks that should have been checkpointed by now. */
|
|
236
260
|
private async pruneUncheckpointedBlocks(currentL1Timestamp: bigint) {
|
|
237
261
|
const [lastCheckpointedBlockNumber, lastProposedBlockNumber] = await Promise.all([
|
|
@@ -310,17 +334,20 @@ export class ArchiverL1Synchronizer implements Traceable {
|
|
|
310
334
|
|
|
311
335
|
const checkpointsToUnwind = localPendingCheckpointNumber - provenCheckpointNumber;
|
|
312
336
|
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
const
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
checkpoints
|
|
320
|
-
.filter(isDefined)
|
|
321
|
-
.map(cp => this.store.getBlocksForCheckpoint(CheckpointNumber(cp.checkpointNumber))),
|
|
337
|
+
// Fetch checkpoints and blocks in bounded batches to avoid unbounded concurrent
|
|
338
|
+
// promises when the gap between local pending and proven checkpoint numbers is large.
|
|
339
|
+
const BATCH_SIZE = 10;
|
|
340
|
+
const indices = Array.from({ length: checkpointsToUnwind }, (_, i) => CheckpointNumber(i + pruneFrom));
|
|
341
|
+
const checkpoints = (await asyncPool(BATCH_SIZE, indices, idx => this.store.getCheckpointData(idx))).filter(
|
|
342
|
+
isDefined,
|
|
322
343
|
);
|
|
323
|
-
const newBlocks =
|
|
344
|
+
const newBlocks = (
|
|
345
|
+
await asyncPool(BATCH_SIZE, checkpoints, cp =>
|
|
346
|
+
this.store.getBlocksForCheckpoint(CheckpointNumber(cp.checkpointNumber)),
|
|
347
|
+
)
|
|
348
|
+
)
|
|
349
|
+
.filter(isDefined)
|
|
350
|
+
.flat();
|
|
324
351
|
|
|
325
352
|
// Emit an event for listening services to react to the chain prune
|
|
326
353
|
this.events.emit(L2BlockSourceEvents.L2PruneUnproven, {
|
|
@@ -368,6 +395,7 @@ export class ArchiverL1Synchronizer implements Traceable {
|
|
|
368
395
|
const localMessagesInserted = await this.store.getTotalL1ToL2MessageCount();
|
|
369
396
|
const localLastMessage = await this.store.getLastL1ToL2Message();
|
|
370
397
|
const remoteMessagesState = await this.inbox.getState({ blockNumber: currentL1BlockNumber });
|
|
398
|
+
await this.store.setInboxTreeInProgress(remoteMessagesState.treeInProgress);
|
|
371
399
|
|
|
372
400
|
this.log.trace(`Retrieved remote inbox state at L1 block ${currentL1BlockNumber}.`, {
|
|
373
401
|
localMessagesInserted,
|
|
@@ -828,7 +856,7 @@ export class ArchiverL1Synchronizer implements Traceable {
|
|
|
828
856
|
const prunedCheckpointNumber = result.prunedBlocks[0].checkpointNumber;
|
|
829
857
|
const prunedSlotNumber = result.prunedBlocks[0].header.globalVariables.slotNumber;
|
|
830
858
|
|
|
831
|
-
this.log.
|
|
859
|
+
this.log.info(
|
|
832
860
|
`Pruned ${result.prunedBlocks.length} mismatching blocks for checkpoint ${prunedCheckpointNumber}`,
|
|
833
861
|
{ prunedBlocks: result.prunedBlocks.map(b => b.toBlockInfo()), prunedSlotNumber, prunedCheckpointNumber },
|
|
834
862
|
);
|
package/src/store/block_store.ts
CHANGED
|
@@ -20,7 +20,7 @@ import {
|
|
|
20
20
|
serializeValidateCheckpointResult,
|
|
21
21
|
} from '@aztec/stdlib/block';
|
|
22
22
|
import { type CheckpointData, L1PublishedData, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
|
|
23
|
-
import type
|
|
23
|
+
import { type L1RollupConstants, getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
|
|
24
24
|
import { CheckpointHeader } from '@aztec/stdlib/rollup';
|
|
25
25
|
import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
|
|
26
26
|
import {
|
|
@@ -35,15 +35,14 @@ import {
|
|
|
35
35
|
} from '@aztec/stdlib/tx';
|
|
36
36
|
|
|
37
37
|
import {
|
|
38
|
+
BlockAlreadyCheckpointedError,
|
|
38
39
|
BlockArchiveNotConsistentError,
|
|
39
40
|
BlockIndexNotSequentialError,
|
|
40
41
|
BlockNotFoundError,
|
|
41
42
|
BlockNumberNotSequentialError,
|
|
42
43
|
CannotOverwriteCheckpointedBlockError,
|
|
43
44
|
CheckpointNotFoundError,
|
|
44
|
-
CheckpointNumberNotConsistentError,
|
|
45
45
|
CheckpointNumberNotSequentialError,
|
|
46
|
-
InitialBlockNumberNotSequentialError,
|
|
47
46
|
InitialCheckpointNumberNotSequentialError,
|
|
48
47
|
} from '../errors.js';
|
|
49
48
|
|
|
@@ -97,6 +96,9 @@ export class BlockStore {
|
|
|
97
96
|
/** Stores last proven checkpoint */
|
|
98
97
|
#lastProvenCheckpoint: AztecAsyncSingleton<number>;
|
|
99
98
|
|
|
99
|
+
/** Stores last finalized checkpoint (proven at or before the finalized L1 block) */
|
|
100
|
+
#lastFinalizedCheckpoint: AztecAsyncSingleton<number>;
|
|
101
|
+
|
|
100
102
|
/** Stores the pending chain validation status */
|
|
101
103
|
#pendingChainValidationStatus: AztecAsyncSingleton<Buffer>;
|
|
102
104
|
|
|
@@ -111,10 +113,7 @@ export class BlockStore {
|
|
|
111
113
|
|
|
112
114
|
#log = createLogger('archiver:block_store');
|
|
113
115
|
|
|
114
|
-
constructor(
|
|
115
|
-
private db: AztecAsyncKVStore,
|
|
116
|
-
private l1Constants: Pick<L1RollupConstants, 'epochDuration'>,
|
|
117
|
-
) {
|
|
116
|
+
constructor(private db: AztecAsyncKVStore) {
|
|
118
117
|
this.#blocks = db.openMap('archiver_blocks');
|
|
119
118
|
this.#blockTxs = db.openMap('archiver_block_txs');
|
|
120
119
|
this.#txEffects = db.openMap('archiver_tx_effects');
|
|
@@ -123,41 +122,42 @@ export class BlockStore {
|
|
|
123
122
|
this.#blockArchiveIndex = db.openMap('archiver_block_archive_index');
|
|
124
123
|
this.#lastSynchedL1Block = db.openSingleton('archiver_last_synched_l1_block');
|
|
125
124
|
this.#lastProvenCheckpoint = db.openSingleton('archiver_last_proven_l2_checkpoint');
|
|
125
|
+
this.#lastFinalizedCheckpoint = db.openSingleton('archiver_last_finalized_l2_checkpoint');
|
|
126
126
|
this.#pendingChainValidationStatus = db.openSingleton('archiver_pending_chain_validation_status');
|
|
127
127
|
this.#checkpoints = db.openMap('archiver_checkpoints');
|
|
128
128
|
this.#slotToCheckpoint = db.openMap('archiver_slot_to_checkpoint');
|
|
129
129
|
}
|
|
130
130
|
|
|
131
131
|
/**
|
|
132
|
-
*
|
|
133
|
-
*
|
|
134
|
-
* TODO(#13569): Compute proper finalized block number based on L1 finalized block.
|
|
135
|
-
* TODO(palla/mbps): Even the provisional computation is wrong, since it should subtract checkpoints, not blocks
|
|
132
|
+
* Returns the finalized L2 block number. An L2 block is finalized when it was proven
|
|
133
|
+
* in an L1 block that has itself been finalized on Ethereum.
|
|
136
134
|
* @returns The finalized block number.
|
|
137
135
|
*/
|
|
138
136
|
async getFinalizedL2BlockNumber(): Promise<BlockNumber> {
|
|
139
|
-
const
|
|
140
|
-
|
|
137
|
+
const finalizedCheckpointNumber = await this.getFinalizedCheckpointNumber();
|
|
138
|
+
if (finalizedCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
|
|
139
|
+
return BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
140
|
+
}
|
|
141
|
+
const checkpointStorage = await this.#checkpoints.getAsync(finalizedCheckpointNumber);
|
|
142
|
+
if (!checkpointStorage) {
|
|
143
|
+
throw new CheckpointNotFoundError(finalizedCheckpointNumber);
|
|
144
|
+
}
|
|
145
|
+
return BlockNumber(checkpointStorage.startBlock + checkpointStorage.blockCount - 1);
|
|
141
146
|
}
|
|
142
147
|
|
|
143
148
|
/**
|
|
144
|
-
* Append new proposed
|
|
145
|
-
*
|
|
149
|
+
* Append a new proposed block to the store.
|
|
150
|
+
* This is an uncheckpointed block that has been proposed by the sequencer but not yet included in a checkpoint on L1.
|
|
146
151
|
* For checkpointed blocks (already published to L1), use addCheckpoints() instead.
|
|
147
|
-
* @param
|
|
152
|
+
* @param block - The proposed L2 block to be added to the store.
|
|
148
153
|
* @returns True if the operation is successful.
|
|
149
154
|
*/
|
|
150
|
-
async
|
|
151
|
-
if (blocks.length === 0) {
|
|
152
|
-
return true;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
+
async addProposedBlock(block: L2Block, opts: { force?: boolean } = {}): Promise<boolean> {
|
|
155
156
|
return await this.db.transactionAsync(async () => {
|
|
156
|
-
|
|
157
|
-
const
|
|
158
|
-
const
|
|
159
|
-
const
|
|
160
|
-
const firstBlockLastArchive = blocks[0].header.lastArchive.root;
|
|
157
|
+
const blockNumber = block.number;
|
|
158
|
+
const blockCheckpointNumber = block.checkpointNumber;
|
|
159
|
+
const blockIndex = block.indexWithinCheckpoint;
|
|
160
|
+
const blockLastArchive = block.header.lastArchive.root;
|
|
161
161
|
|
|
162
162
|
// Extract the latest block and checkpoint numbers
|
|
163
163
|
const previousBlockNumber = await this.getLatestBlockNumber();
|
|
@@ -165,71 +165,52 @@ export class BlockStore {
|
|
|
165
165
|
|
|
166
166
|
// Verify we're not overwriting checkpointed blocks
|
|
167
167
|
const lastCheckpointedBlockNumber = await this.getCheckpointedL2BlockNumber();
|
|
168
|
-
if (!opts.force &&
|
|
169
|
-
|
|
168
|
+
if (!opts.force && blockNumber <= lastCheckpointedBlockNumber) {
|
|
169
|
+
// Check if the proposed block matches the already-checkpointed one
|
|
170
|
+
const existingBlock = await this.getBlock(BlockNumber(blockNumber));
|
|
171
|
+
if (existingBlock && existingBlock.archive.root.equals(block.archive.root)) {
|
|
172
|
+
throw new BlockAlreadyCheckpointedError(blockNumber);
|
|
173
|
+
}
|
|
174
|
+
throw new CannotOverwriteCheckpointedBlockError(blockNumber, lastCheckpointedBlockNumber);
|
|
170
175
|
}
|
|
171
176
|
|
|
172
|
-
// Check that the
|
|
173
|
-
if (!opts.force && previousBlockNumber !==
|
|
174
|
-
throw new
|
|
177
|
+
// Check that the block number is the expected one
|
|
178
|
+
if (!opts.force && previousBlockNumber !== blockNumber - 1) {
|
|
179
|
+
throw new BlockNumberNotSequentialError(blockNumber, previousBlockNumber);
|
|
175
180
|
}
|
|
176
181
|
|
|
177
182
|
// The same check as above but for checkpoints
|
|
178
|
-
if (!opts.force && previousCheckpointNumber !==
|
|
179
|
-
throw new
|
|
183
|
+
if (!opts.force && previousCheckpointNumber !== blockCheckpointNumber - 1) {
|
|
184
|
+
throw new CheckpointNumberNotSequentialError(blockCheckpointNumber, previousCheckpointNumber);
|
|
180
185
|
}
|
|
181
186
|
|
|
182
187
|
// Extract the previous block if there is one and see if it is for the same checkpoint or not
|
|
183
188
|
const previousBlockResult = await this.getBlock(previousBlockNumber);
|
|
184
189
|
|
|
185
|
-
let
|
|
190
|
+
let expectedBlockIndex = 0;
|
|
186
191
|
let previousBlockIndex: number | undefined = undefined;
|
|
187
192
|
if (previousBlockResult !== undefined) {
|
|
188
|
-
if (previousBlockResult.checkpointNumber ===
|
|
193
|
+
if (previousBlockResult.checkpointNumber === blockCheckpointNumber) {
|
|
189
194
|
// The previous block is for the same checkpoint, therefore our index should follow it
|
|
190
195
|
previousBlockIndex = previousBlockResult.indexWithinCheckpoint;
|
|
191
|
-
|
|
196
|
+
expectedBlockIndex = previousBlockIndex + 1;
|
|
192
197
|
}
|
|
193
|
-
if (!previousBlockResult.archive.root.equals(
|
|
198
|
+
if (!previousBlockResult.archive.root.equals(blockLastArchive)) {
|
|
194
199
|
throw new BlockArchiveNotConsistentError(
|
|
195
|
-
|
|
200
|
+
blockNumber,
|
|
196
201
|
previousBlockResult.number,
|
|
197
|
-
|
|
202
|
+
blockLastArchive,
|
|
198
203
|
previousBlockResult.archive.root,
|
|
199
204
|
);
|
|
200
205
|
}
|
|
201
206
|
}
|
|
202
207
|
|
|
203
|
-
// Now check that the
|
|
204
|
-
if (!opts.force &&
|
|
205
|
-
throw new BlockIndexNotSequentialError(
|
|
208
|
+
// Now check that the block has the expected index value
|
|
209
|
+
if (!opts.force && expectedBlockIndex !== blockIndex) {
|
|
210
|
+
throw new BlockIndexNotSequentialError(blockIndex, previousBlockIndex);
|
|
206
211
|
}
|
|
207
212
|
|
|
208
|
-
|
|
209
|
-
let previousBlock: L2Block | undefined = undefined;
|
|
210
|
-
for (const block of blocks) {
|
|
211
|
-
if (!opts.force && previousBlock) {
|
|
212
|
-
if (previousBlock.number + 1 !== block.number) {
|
|
213
|
-
throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
|
|
214
|
-
}
|
|
215
|
-
if (previousBlock.indexWithinCheckpoint + 1 !== block.indexWithinCheckpoint) {
|
|
216
|
-
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
|
|
217
|
-
}
|
|
218
|
-
if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
|
|
219
|
-
throw new BlockArchiveNotConsistentError(
|
|
220
|
-
block.number,
|
|
221
|
-
previousBlock.number,
|
|
222
|
-
block.header.lastArchive.root,
|
|
223
|
-
previousBlock.archive.root,
|
|
224
|
-
);
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
if (!opts.force && firstBlockCheckpointNumber !== block.checkpointNumber) {
|
|
228
|
-
throw new CheckpointNumberNotConsistentError(block.checkpointNumber, firstBlockCheckpointNumber);
|
|
229
|
-
}
|
|
230
|
-
previousBlock = block;
|
|
231
|
-
await this.addBlockToDatabase(block, block.checkpointNumber, block.indexWithinCheckpoint);
|
|
232
|
-
}
|
|
213
|
+
await this.addBlockToDatabase(block, block.checkpointNumber, block.indexWithinCheckpoint);
|
|
233
214
|
|
|
234
215
|
return true;
|
|
235
216
|
});
|
|
@@ -871,7 +852,10 @@ export class BlockStore {
|
|
|
871
852
|
* @param txHash - The hash of a tx we try to get the receipt for.
|
|
872
853
|
* @returns The requested tx receipt (or undefined if not found).
|
|
873
854
|
*/
|
|
874
|
-
async getSettledTxReceipt(
|
|
855
|
+
async getSettledTxReceipt(
|
|
856
|
+
txHash: TxHash,
|
|
857
|
+
l1Constants?: Pick<L1RollupConstants, 'epochDuration'>,
|
|
858
|
+
): Promise<TxReceipt | undefined> {
|
|
875
859
|
const txEffect = await this.getTxEffect(txHash);
|
|
876
860
|
if (!txEffect) {
|
|
877
861
|
return undefined;
|
|
@@ -880,10 +864,11 @@ export class BlockStore {
|
|
|
880
864
|
const blockNumber = BlockNumber(txEffect.l2BlockNumber);
|
|
881
865
|
|
|
882
866
|
// Use existing archiver methods to determine finalization level
|
|
883
|
-
const [provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber] = await Promise.all([
|
|
867
|
+
const [provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber, blockData] = await Promise.all([
|
|
884
868
|
this.getProvenBlockNumber(),
|
|
885
869
|
this.getCheckpointedL2BlockNumber(),
|
|
886
870
|
this.getFinalizedL2BlockNumber(),
|
|
871
|
+
this.getBlockData(blockNumber),
|
|
887
872
|
]);
|
|
888
873
|
|
|
889
874
|
let status: TxStatus;
|
|
@@ -897,6 +882,9 @@ export class BlockStore {
|
|
|
897
882
|
status = TxStatus.PROPOSED;
|
|
898
883
|
}
|
|
899
884
|
|
|
885
|
+
const epochNumber =
|
|
886
|
+
blockData && l1Constants ? getEpochAtSlot(blockData.header.globalVariables.slotNumber, l1Constants) : undefined;
|
|
887
|
+
|
|
900
888
|
return new TxReceipt(
|
|
901
889
|
txHash,
|
|
902
890
|
status,
|
|
@@ -905,6 +893,7 @@ export class BlockStore {
|
|
|
905
893
|
txEffect.data.transactionFee.toBigInt(),
|
|
906
894
|
txEffect.l2BlockHash,
|
|
907
895
|
blockNumber,
|
|
896
|
+
epochNumber,
|
|
908
897
|
);
|
|
909
898
|
}
|
|
910
899
|
|
|
@@ -976,6 +965,20 @@ export class BlockStore {
|
|
|
976
965
|
return result;
|
|
977
966
|
}
|
|
978
967
|
|
|
968
|
+
async getFinalizedCheckpointNumber(): Promise<CheckpointNumber> {
|
|
969
|
+
const [latestCheckpointNumber, finalizedCheckpointNumber] = await Promise.all([
|
|
970
|
+
this.getLatestCheckpointNumber(),
|
|
971
|
+
this.#lastFinalizedCheckpoint.getAsync(),
|
|
972
|
+
]);
|
|
973
|
+
return (finalizedCheckpointNumber ?? 0) > latestCheckpointNumber
|
|
974
|
+
? latestCheckpointNumber
|
|
975
|
+
: CheckpointNumber(finalizedCheckpointNumber ?? 0);
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
setFinalizedCheckpointNumber(checkpointNumber: CheckpointNumber) {
|
|
979
|
+
return this.#lastFinalizedCheckpoint.set(checkpointNumber);
|
|
980
|
+
}
|
|
981
|
+
|
|
979
982
|
#computeBlockRange(start: BlockNumber, limit: number): Required<Pick<Range<number>, 'start' | 'limit'>> {
|
|
980
983
|
if (limit < 1) {
|
|
981
984
|
throw new Error(`Invalid limit: ${limit}`);
|