@aztec/archiver 0.0.1-commit.e558bd1c → 0.0.1-commit.e5a3663dd
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 +26 -15
- package/dest/archiver.d.ts.map +1 -1
- package/dest/archiver.js +161 -153
- package/dest/config.d.ts +5 -3
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +16 -4
- package/dest/errors.d.ts +61 -10
- package/dest/errors.d.ts.map +1 -1
- package/dest/errors.js +88 -14
- package/dest/factory.d.ts +6 -7
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +40 -32
- package/dest/index.d.ts +11 -3
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +10 -2
- package/dest/l1/bin/retrieve-calldata.js +32 -28
- package/dest/l1/calldata_retriever.d.ts +74 -50
- package/dest/l1/calldata_retriever.d.ts.map +1 -1
- package/dest/l1/calldata_retriever.js +197 -260
- package/dest/l1/data_retrieval.d.ts +26 -17
- package/dest/l1/data_retrieval.d.ts.map +1 -1
- package/dest/l1/data_retrieval.js +42 -47
- 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_historical_logs.d.ts +23 -0
- package/dest/l1/validate_historical_logs.d.ts.map +1 -0
- package/dest/l1/validate_historical_logs.js +108 -0
- package/dest/modules/contract_data_source_adapter.d.ts +25 -0
- package/dest/modules/contract_data_source_adapter.d.ts.map +1 -0
- package/dest/modules/contract_data_source_adapter.js +42 -0
- package/dest/modules/data_source_base.d.ts +27 -14
- package/dest/modules/data_source_base.d.ts.map +1 -1
- package/dest/modules/data_source_base.js +98 -125
- package/dest/modules/data_store_updater.d.ts +37 -17
- package/dest/modules/data_store_updater.d.ts.map +1 -1
- package/dest/modules/data_store_updater.js +155 -112
- package/dest/modules/instrumentation.d.ts +21 -3
- package/dest/modules/instrumentation.d.ts.map +1 -1
- package/dest/modules/instrumentation.js +41 -8
- package/dest/modules/l1_synchronizer.d.ts +13 -11
- package/dest/modules/l1_synchronizer.d.ts.map +1 -1
- package/dest/modules/l1_synchronizer.js +355 -182
- package/dest/modules/validation.d.ts +4 -3
- package/dest/modules/validation.d.ts.map +1 -1
- package/dest/modules/validation.js +6 -6
- package/dest/store/block_store.d.ts +107 -31
- package/dest/store/block_store.d.ts.map +1 -1
- package/dest/store/block_store.js +477 -141
- package/dest/store/contract_class_store.d.ts +17 -4
- package/dest/store/contract_class_store.d.ts.map +1 -1
- package/dest/store/contract_class_store.js +24 -68
- package/dest/store/contract_instance_store.d.ts +28 -1
- package/dest/store/contract_instance_store.d.ts.map +1 -1
- package/dest/store/contract_instance_store.js +37 -2
- package/dest/store/data_stores.d.ts +68 -0
- package/dest/store/data_stores.d.ts.map +1 -0
- package/dest/store/data_stores.js +50 -0
- package/dest/store/function_names_cache.d.ts +17 -0
- package/dest/store/function_names_cache.d.ts.map +1 -0
- package/dest/store/function_names_cache.js +30 -0
- 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 +95 -20
- package/dest/store/message_store.d.ts +11 -1
- package/dest/store/message_store.d.ts.map +1 -1
- package/dest/store/message_store.js +51 -9
- package/dest/test/fake_l1_state.d.ts +25 -1
- package/dest/test/fake_l1_state.d.ts.map +1 -1
- package/dest/test/fake_l1_state.js +166 -32
- 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 +35 -5
- package/dest/test/mock_l2_block_source.d.ts.map +1 -1
- package/dest/test/mock_l2_block_source.js +182 -89
- package/dest/test/mock_structs.d.ts +4 -1
- package/dest/test/mock_structs.d.ts.map +1 -1
- package/dest/test/mock_structs.js +13 -1
- package/dest/test/noop_l1_archiver.d.ts +7 -4
- package/dest/test/noop_l1_archiver.d.ts.map +1 -1
- package/dest/test/noop_l1_archiver.js +14 -8
- package/package.json +13 -13
- package/src/archiver.ts +199 -174
- package/src/config.ts +23 -2
- package/src/errors.ts +133 -22
- package/src/factory.ts +53 -30
- package/src/index.ts +18 -2
- package/src/l1/README.md +25 -68
- package/src/l1/bin/retrieve-calldata.ts +40 -27
- package/src/l1/calldata_retriever.ts +261 -379
- package/src/l1/data_retrieval.ts +58 -69
- package/src/l1/spire_proposer.ts +7 -15
- package/src/l1/validate_historical_logs.ts +140 -0
- package/src/modules/contract_data_source_adapter.ts +59 -0
- package/src/modules/data_source_base.ts +142 -144
- package/src/modules/data_store_updater.ts +187 -141
- package/src/modules/instrumentation.ts +56 -9
- package/src/modules/l1_synchronizer.ts +463 -218
- package/src/modules/validation.ts +10 -9
- package/src/store/block_store.ts +587 -177
- package/src/store/contract_class_store.ts +31 -103
- package/src/store/contract_instance_store.ts +51 -5
- package/src/store/data_stores.ts +108 -0
- package/src/store/function_names_cache.ts +37 -0
- package/src/store/l2_tips_cache.ts +134 -0
- package/src/store/log_store.ts +128 -32
- package/src/store/message_store.ts +60 -10
- package/src/structs/inbox_message.ts +1 -1
- package/src/test/fake_l1_state.ts +213 -42
- 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 +230 -82
- package/src/test/mock_structs.ts +20 -6
- package/src/test/noop_l1_archiver.ts +16 -8
- package/dest/store/kv_archiver_store.d.ts +0 -340
- package/dest/store/kv_archiver_store.d.ts.map +0 -1
- package/dest/store/kv_archiver_store.js +0 -446
- package/src/store/kv_archiver_store.ts +0 -639
|
@@ -6,32 +6,34 @@ import { createLogger } from '@aztec/foundation/log';
|
|
|
6
6
|
import { BufferReader } from '@aztec/foundation/serialize';
|
|
7
7
|
import { bufferToHex } from '@aztec/foundation/string';
|
|
8
8
|
import { isDefined } from '@aztec/foundation/types';
|
|
9
|
-
import { Body, CheckpointedL2Block, CommitteeAttestation, L2Block, deserializeValidateCheckpointResult, serializeValidateCheckpointResult } from '@aztec/stdlib/block';
|
|
10
|
-
import { L1PublishedData } from '@aztec/stdlib/checkpoint';
|
|
9
|
+
import { BlockHash, Body, CheckpointedL2Block, CommitteeAttestation, L2Block, deserializeValidateCheckpointResult, serializeValidateCheckpointResult } from '@aztec/stdlib/block';
|
|
10
|
+
import { Checkpoint, L1PublishedData } from '@aztec/stdlib/checkpoint';
|
|
11
|
+
import { getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
|
|
11
12
|
import { CheckpointHeader } from '@aztec/stdlib/rollup';
|
|
12
13
|
import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
|
|
13
14
|
import { BlockHeader, TxHash, TxReceipt, TxStatus, deserializeIndexedTxEffect, serializeIndexedTxEffect } from '@aztec/stdlib/tx';
|
|
14
|
-
import { BlockArchiveNotConsistentError, BlockIndexNotSequentialError, BlockNotFoundError, BlockNumberNotSequentialError, CannotOverwriteCheckpointedBlockError, CheckpointNotFoundError,
|
|
15
|
+
import { BlockAlreadyCheckpointedError, BlockArchiveNotConsistentError, BlockCheckpointNumberNotSequentialError, BlockIndexNotSequentialError, BlockNotFoundError, BlockNumberNotSequentialError, CannotOverwriteCheckpointedBlockError, CheckpointNotFoundError, CheckpointNumberNotSequentialError, InitialCheckpointNumberNotSequentialError, NoProposedCheckpointToPromoteError, ProposedCheckpointArchiveRootMismatchError, ProposedCheckpointNotSequentialError, ProposedCheckpointPromotionNotSequentialError } from '../errors.js';
|
|
15
16
|
export { TxReceipt } from '@aztec/stdlib/tx';
|
|
16
17
|
/**
|
|
17
18
|
* LMDB-based block storage for the archiver.
|
|
18
19
|
*/ export class BlockStore {
|
|
19
20
|
db;
|
|
20
|
-
l1Constants;
|
|
21
21
|
/** Map block number to block data */ #blocks;
|
|
22
|
-
/** Map checkpoint number
|
|
22
|
+
/** Map keyed by checkpoint number holding proposed (locally-validated, not yet L1-confirmed) checkpoints. */ #proposedCheckpoints;
|
|
23
|
+
/** Map checkpoint number to checkpoint data for mined checkpoints only */ #checkpoints;
|
|
24
|
+
/** Map slot number to checkpoint number, for looking up checkpoints by slot range. */ #slotToCheckpoint;
|
|
23
25
|
/** Map block hash to list of tx hashes */ #blockTxs;
|
|
24
26
|
/** Tx hash to serialized IndexedTxEffect */ #txEffects;
|
|
25
27
|
/** Stores L1 block number in which the last processed L2 block was included */ #lastSynchedL1Block;
|
|
26
28
|
/** Stores last proven checkpoint */ #lastProvenCheckpoint;
|
|
29
|
+
/** Stores last finalized checkpoint (proven at or before the finalized L1 block) */ #lastFinalizedCheckpoint;
|
|
27
30
|
/** Stores the pending chain validation status */ #pendingChainValidationStatus;
|
|
28
31
|
/** Index mapping a contract's address (as a string) to its location in a block */ #contractIndex;
|
|
29
32
|
/** Index mapping block hash to block number */ #blockHashIndex;
|
|
30
33
|
/** Index mapping block archive to block number */ #blockArchiveIndex;
|
|
31
34
|
#log;
|
|
32
|
-
constructor(db
|
|
35
|
+
constructor(db){
|
|
33
36
|
this.db = db;
|
|
34
|
-
this.l1Constants = l1Constants;
|
|
35
37
|
this.#log = createLogger('archiver:block_store');
|
|
36
38
|
this.#blocks = db.openMap('archiver_blocks');
|
|
37
39
|
this.#blockTxs = db.openMap('archiver_block_txs');
|
|
@@ -41,94 +43,92 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
41
43
|
this.#blockArchiveIndex = db.openMap('archiver_block_archive_index');
|
|
42
44
|
this.#lastSynchedL1Block = db.openSingleton('archiver_last_synched_l1_block');
|
|
43
45
|
this.#lastProvenCheckpoint = db.openSingleton('archiver_last_proven_l2_checkpoint');
|
|
46
|
+
this.#lastFinalizedCheckpoint = db.openSingleton('archiver_last_finalized_l2_checkpoint');
|
|
44
47
|
this.#pendingChainValidationStatus = db.openSingleton('archiver_pending_chain_validation_status');
|
|
45
48
|
this.#checkpoints = db.openMap('archiver_checkpoints');
|
|
49
|
+
this.#slotToCheckpoint = db.openMap('archiver_slot_to_checkpoint');
|
|
50
|
+
this.#proposedCheckpoints = db.openMap('archiver_proposed_checkpoints');
|
|
46
51
|
}
|
|
47
52
|
/**
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
* TODO(#13569): Compute proper finalized block number based on L1 finalized block.
|
|
51
|
-
* TODO(palla/mbps): Even the provisional computation is wrong, since it should subtract checkpoints, not blocks
|
|
53
|
+
* Returns the finalized L2 block number. An L2 block is finalized when it was proven
|
|
54
|
+
* in an L1 block that has itself been finalized on Ethereum.
|
|
52
55
|
* @returns The finalized block number.
|
|
53
56
|
*/ async getFinalizedL2BlockNumber() {
|
|
54
|
-
const
|
|
55
|
-
|
|
57
|
+
const finalizedCheckpointNumber = await this.getFinalizedCheckpointNumber();
|
|
58
|
+
if (finalizedCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
|
|
59
|
+
return BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
60
|
+
}
|
|
61
|
+
const checkpointStorage = await this.#checkpoints.getAsync(finalizedCheckpointNumber);
|
|
62
|
+
if (!checkpointStorage) {
|
|
63
|
+
throw new CheckpointNotFoundError(finalizedCheckpointNumber);
|
|
64
|
+
}
|
|
65
|
+
return BlockNumber(checkpointStorage.startBlock + checkpointStorage.blockCount - 1);
|
|
56
66
|
}
|
|
57
67
|
/**
|
|
58
|
-
* Append new proposed
|
|
59
|
-
*
|
|
68
|
+
* Append a new proposed block to the store.
|
|
69
|
+
* This is an uncheckpointed block that has been proposed by the sequencer but not yet included in a checkpoint on L1.
|
|
60
70
|
* For checkpointed blocks (already published to L1), use addCheckpoints() instead.
|
|
61
|
-
* @param
|
|
71
|
+
* @param block - The proposed L2 block to be added to the store.
|
|
62
72
|
* @returns True if the operation is successful.
|
|
63
|
-
*/ async
|
|
64
|
-
if (blocks.length === 0) {
|
|
65
|
-
return true;
|
|
66
|
-
}
|
|
73
|
+
*/ async addProposedBlock(block, opts = {}) {
|
|
67
74
|
return await this.db.transactionAsync(async ()=>{
|
|
68
|
-
|
|
69
|
-
const
|
|
70
|
-
const
|
|
71
|
-
const
|
|
72
|
-
const firstBlockLastArchive = blocks[0].header.lastArchive.root;
|
|
75
|
+
const blockNumber = block.number;
|
|
76
|
+
const blockCheckpointNumber = block.checkpointNumber;
|
|
77
|
+
const blockIndex = block.indexWithinCheckpoint;
|
|
78
|
+
const blockLastArchive = block.header.lastArchive.root;
|
|
73
79
|
// Extract the latest block and checkpoint numbers
|
|
74
|
-
const previousBlockNumber = await this.
|
|
75
|
-
const
|
|
80
|
+
const previousBlockNumber = await this.getLatestL2BlockNumber();
|
|
81
|
+
const latestCheckpointNumber = await this.getLatestCheckpointNumber();
|
|
76
82
|
// Verify we're not overwriting checkpointed blocks
|
|
77
83
|
const lastCheckpointedBlockNumber = await this.getCheckpointedL2BlockNumber();
|
|
78
|
-
if (!opts.force &&
|
|
79
|
-
|
|
84
|
+
if (!opts.force && blockNumber <= lastCheckpointedBlockNumber) {
|
|
85
|
+
// Check if the proposed block matches the already-checkpointed one
|
|
86
|
+
const existingBlock = await this.getBlock(BlockNumber(blockNumber));
|
|
87
|
+
if (existingBlock && existingBlock.archive.root.equals(block.archive.root)) {
|
|
88
|
+
throw new BlockAlreadyCheckpointedError(blockNumber);
|
|
89
|
+
}
|
|
90
|
+
throw new CannotOverwriteCheckpointedBlockError(blockNumber, lastCheckpointedBlockNumber);
|
|
80
91
|
}
|
|
81
|
-
// Check that the
|
|
82
|
-
if (!opts.force && previousBlockNumber !==
|
|
83
|
-
throw new
|
|
92
|
+
// Check that the block number is the expected one
|
|
93
|
+
if (!opts.force && previousBlockNumber !== blockNumber - 1) {
|
|
94
|
+
throw new BlockNumberNotSequentialError(blockNumber, previousBlockNumber);
|
|
84
95
|
}
|
|
85
|
-
//
|
|
86
|
-
|
|
87
|
-
|
|
96
|
+
// Accept the block if either the confirmed checkpoint or a pending checkpoint matches
|
|
97
|
+
// the expected predecessor. We look for a pending entry at exactly blockCheckpointNumber - 1.
|
|
98
|
+
const expectedCheckpointNumber = blockCheckpointNumber - 1;
|
|
99
|
+
const hasPendingAtExpected = await this.#proposedCheckpoints.hasAsync(expectedCheckpointNumber);
|
|
100
|
+
if (!opts.force && latestCheckpointNumber !== expectedCheckpointNumber && !hasPendingAtExpected) {
|
|
101
|
+
const [latestPendingKey] = await toArray(this.#proposedCheckpoints.keysAsync({
|
|
102
|
+
reverse: true,
|
|
103
|
+
limit: 1
|
|
104
|
+
}));
|
|
105
|
+
const previous = CheckpointNumber(Math.max(latestCheckpointNumber, latestPendingKey ?? 0));
|
|
106
|
+
throw new BlockCheckpointNumberNotSequentialError(blockNumber, blockCheckpointNumber, previous);
|
|
88
107
|
}
|
|
89
108
|
// Extract the previous block if there is one and see if it is for the same checkpoint or not
|
|
90
109
|
const previousBlockResult = await this.getBlock(previousBlockNumber);
|
|
91
|
-
let
|
|
110
|
+
let expectedBlockIndex = 0;
|
|
92
111
|
let previousBlockIndex = undefined;
|
|
93
112
|
if (previousBlockResult !== undefined) {
|
|
94
|
-
if (previousBlockResult.checkpointNumber ===
|
|
113
|
+
if (previousBlockResult.checkpointNumber === blockCheckpointNumber) {
|
|
95
114
|
// The previous block is for the same checkpoint, therefore our index should follow it
|
|
96
115
|
previousBlockIndex = previousBlockResult.indexWithinCheckpoint;
|
|
97
|
-
|
|
116
|
+
expectedBlockIndex = previousBlockIndex + 1;
|
|
98
117
|
}
|
|
99
|
-
if (!previousBlockResult.archive.root.equals(
|
|
100
|
-
throw new BlockArchiveNotConsistentError(
|
|
118
|
+
if (!previousBlockResult.archive.root.equals(blockLastArchive)) {
|
|
119
|
+
throw new BlockArchiveNotConsistentError(blockNumber, previousBlockResult.number, blockLastArchive, previousBlockResult.archive.root);
|
|
101
120
|
}
|
|
102
121
|
}
|
|
103
|
-
// Now check that the
|
|
104
|
-
if (!opts.force &&
|
|
105
|
-
throw new BlockIndexNotSequentialError(
|
|
106
|
-
}
|
|
107
|
-
// Iterate over blocks array and insert them, checking that the block numbers and indexes are sequential. Also check they are for the correct checkpoint.
|
|
108
|
-
let previousBlock = undefined;
|
|
109
|
-
for (const block of blocks){
|
|
110
|
-
if (!opts.force && previousBlock) {
|
|
111
|
-
if (previousBlock.number + 1 !== block.number) {
|
|
112
|
-
throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
|
|
113
|
-
}
|
|
114
|
-
if (previousBlock.indexWithinCheckpoint + 1 !== block.indexWithinCheckpoint) {
|
|
115
|
-
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
|
|
116
|
-
}
|
|
117
|
-
if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
|
|
118
|
-
throw new BlockArchiveNotConsistentError(block.number, previousBlock.number, block.header.lastArchive.root, previousBlock.archive.root);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
if (!opts.force && firstBlockCheckpointNumber !== block.checkpointNumber) {
|
|
122
|
-
throw new CheckpointNumberNotConsistentError(block.checkpointNumber, firstBlockCheckpointNumber);
|
|
123
|
-
}
|
|
124
|
-
previousBlock = block;
|
|
125
|
-
await this.addBlockToDatabase(block, block.checkpointNumber, block.indexWithinCheckpoint);
|
|
122
|
+
// Now check that the block has the expected index value
|
|
123
|
+
if (!opts.force && expectedBlockIndex !== blockIndex) {
|
|
124
|
+
throw new BlockIndexNotSequentialError(blockIndex, previousBlockIndex);
|
|
126
125
|
}
|
|
126
|
+
await this.addBlockToDatabase(block, block.checkpointNumber, block.indexWithinCheckpoint);
|
|
127
127
|
return true;
|
|
128
128
|
});
|
|
129
129
|
}
|
|
130
130
|
/**
|
|
131
|
-
* Append new
|
|
131
|
+
* Append new checkpoints to the store's list.
|
|
132
132
|
* @param checkpoints - The L2 checkpoints to be added to the store.
|
|
133
133
|
* @returns True if the operation is successful.
|
|
134
134
|
*/ async addCheckpoints(checkpoints, opts = {}) {
|
|
@@ -136,32 +136,26 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
136
136
|
return true;
|
|
137
137
|
}
|
|
138
138
|
return await this.db.transactionAsync(async ()=>{
|
|
139
|
-
// Check that the checkpoint immediately before the first block to be added is present in the store.
|
|
140
139
|
const firstCheckpointNumber = checkpoints[0].checkpoint.number;
|
|
141
140
|
const previousCheckpointNumber = await this.getLatestCheckpointNumber();
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
previousCheckpointData = await this.getCheckpointData(previousCheckpointNumber);
|
|
150
|
-
if (previousCheckpointData === undefined) {
|
|
151
|
-
throw new CheckpointNotFoundError(previousCheckpointNumber);
|
|
141
|
+
// Handle already-stored checkpoints at the start of the batch.
|
|
142
|
+
// This can happen after an L1 reorg re-includes a checkpoint in a different L1 block.
|
|
143
|
+
// We accept them if archives match (same content) and update their L1 metadata.
|
|
144
|
+
if (!opts.force && firstCheckpointNumber <= previousCheckpointNumber) {
|
|
145
|
+
checkpoints = await this.skipOrUpdateAlreadyStoredCheckpoints(checkpoints, previousCheckpointNumber);
|
|
146
|
+
if (checkpoints.length === 0) {
|
|
147
|
+
return true;
|
|
152
148
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
if (previousCheckpointData !== undefined) {
|
|
158
|
-
previousBlockNumber = BlockNumber(previousCheckpointData.startBlock + previousCheckpointData.numBlocks - 1);
|
|
159
|
-
previousBlock = await this.getBlock(previousBlockNumber);
|
|
160
|
-
if (previousBlock === undefined) {
|
|
161
|
-
// We should be able to get the required previous block
|
|
162
|
-
throw new BlockNotFoundError(previousBlockNumber);
|
|
149
|
+
// Re-check sequentiality after skipping
|
|
150
|
+
const newFirstNumber = checkpoints[0].checkpoint.number;
|
|
151
|
+
if (previousCheckpointNumber !== newFirstNumber - 1) {
|
|
152
|
+
throw new InitialCheckpointNumberNotSequentialError(newFirstNumber, previousCheckpointNumber);
|
|
163
153
|
}
|
|
154
|
+
} else if (previousCheckpointNumber !== firstCheckpointNumber - 1 && !opts.force) {
|
|
155
|
+
throw new InitialCheckpointNumberNotSequentialError(firstCheckpointNumber, previousCheckpointNumber);
|
|
164
156
|
}
|
|
157
|
+
// Get the last block of the previous checkpoint for archive chaining
|
|
158
|
+
let previousBlock = await this.getPreviousCheckpointBlock(checkpoints[0].checkpoint.number);
|
|
165
159
|
// Iterate over checkpoints array and insert them, checking that the block numbers are sequential.
|
|
166
160
|
let previousCheckpoint = undefined;
|
|
167
161
|
for (const checkpoint of checkpoints){
|
|
@@ -169,48 +163,118 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
169
163
|
throw new CheckpointNumberNotSequentialError(checkpoint.checkpoint.number, previousCheckpoint.checkpoint.number);
|
|
170
164
|
}
|
|
171
165
|
previousCheckpoint = checkpoint;
|
|
172
|
-
//
|
|
166
|
+
// Validate block sequencing, indexes, and archive chaining
|
|
167
|
+
this.validateCheckpointBlocks(checkpoint.checkpoint.blocks, previousBlock);
|
|
168
|
+
// Store every block in the database (may already exist, but L1 data is authoritative)
|
|
173
169
|
for(let i = 0; i < checkpoint.checkpoint.blocks.length; i++){
|
|
174
|
-
|
|
175
|
-
if (previousBlock) {
|
|
176
|
-
// The blocks should have a sequential block number
|
|
177
|
-
if (previousBlock.number !== block.number - 1) {
|
|
178
|
-
throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
|
|
179
|
-
}
|
|
180
|
-
// If the blocks are for the same checkpoint then they should have sequential indexes
|
|
181
|
-
if (previousBlock.checkpointNumber === block.checkpointNumber && previousBlock.indexWithinCheckpoint !== block.indexWithinCheckpoint - 1) {
|
|
182
|
-
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
|
|
183
|
-
}
|
|
184
|
-
if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
|
|
185
|
-
throw new BlockArchiveNotConsistentError(block.number, previousBlock.number, block.header.lastArchive.root, previousBlock.archive.root);
|
|
186
|
-
}
|
|
187
|
-
} else {
|
|
188
|
-
// No previous block, must be block 1 at checkpoint index 0
|
|
189
|
-
if (block.indexWithinCheckpoint !== 0) {
|
|
190
|
-
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, undefined);
|
|
191
|
-
}
|
|
192
|
-
if (block.number !== INITIAL_L2_BLOCK_NUM) {
|
|
193
|
-
throw new BlockNumberNotSequentialError(block.number, undefined);
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
previousBlock = block;
|
|
197
|
-
await this.addBlockToDatabase(block, checkpoint.checkpoint.number, i);
|
|
170
|
+
await this.addBlockToDatabase(checkpoint.checkpoint.blocks[i], checkpoint.checkpoint.number, i);
|
|
198
171
|
}
|
|
172
|
+
previousBlock = checkpoint.checkpoint.blocks.at(-1);
|
|
199
173
|
// Store the checkpoint in the database
|
|
200
174
|
await this.#checkpoints.set(checkpoint.checkpoint.number, {
|
|
201
175
|
header: checkpoint.checkpoint.header.toBuffer(),
|
|
202
176
|
archive: checkpoint.checkpoint.archive.toBuffer(),
|
|
177
|
+
checkpointOutHash: checkpoint.checkpoint.getCheckpointOutHash().toBuffer(),
|
|
203
178
|
l1: checkpoint.l1.toBuffer(),
|
|
204
179
|
attestations: checkpoint.attestations.map((attestation)=>attestation.toBuffer()),
|
|
205
180
|
checkpointNumber: checkpoint.checkpoint.number,
|
|
206
181
|
startBlock: checkpoint.checkpoint.blocks[0].number,
|
|
207
|
-
|
|
182
|
+
blockCount: checkpoint.checkpoint.blocks.length,
|
|
183
|
+
feeAssetPriceModifier: checkpoint.checkpoint.feeAssetPriceModifier.toString()
|
|
208
184
|
});
|
|
185
|
+
// Update slot-to-checkpoint index
|
|
186
|
+
await this.#slotToCheckpoint.set(checkpoint.checkpoint.header.slotNumber, checkpoint.checkpoint.number);
|
|
187
|
+
// Remove proposed checkpoint if it exists, since L1 is authoritative
|
|
188
|
+
await this.#proposedCheckpoints.delete(checkpoint.checkpoint.number);
|
|
209
189
|
}
|
|
210
190
|
await this.#lastSynchedL1Block.set(checkpoints[checkpoints.length - 1].l1.blockNumber);
|
|
211
191
|
return true;
|
|
212
192
|
});
|
|
213
193
|
}
|
|
194
|
+
/**
|
|
195
|
+
* Handles checkpoints at the start of a batch that are already stored (e.g. due to L1 reorg).
|
|
196
|
+
* Verifies the archive root matches, updates L1 metadata, and returns only the new checkpoints.
|
|
197
|
+
*/ async skipOrUpdateAlreadyStoredCheckpoints(checkpoints, latestStored) {
|
|
198
|
+
let i = 0;
|
|
199
|
+
for(; i < checkpoints.length && checkpoints[i].checkpoint.number <= latestStored; i++){
|
|
200
|
+
const incoming = checkpoints[i];
|
|
201
|
+
const stored = await this.getCheckpointData(incoming.checkpoint.number);
|
|
202
|
+
if (!stored) {
|
|
203
|
+
break;
|
|
204
|
+
}
|
|
205
|
+
// Verify the checkpoint content matches (archive root)
|
|
206
|
+
if (!stored.archive.root.equals(incoming.checkpoint.archive.root)) {
|
|
207
|
+
throw new Error(`Checkpoint ${incoming.checkpoint.number} already exists in store but with a different archive root. ` + `Stored: ${stored.archive.root}, incoming: ${incoming.checkpoint.archive.root}`);
|
|
208
|
+
}
|
|
209
|
+
// Update L1 metadata and attestations for the already-stored checkpoint
|
|
210
|
+
this.#log.warn(`Checkpoint ${incoming.checkpoint.number} already stored, updating L1 info ` + `(L1 block ${stored.l1.blockNumber} -> ${incoming.l1.blockNumber})`);
|
|
211
|
+
await this.#checkpoints.set(incoming.checkpoint.number, {
|
|
212
|
+
header: incoming.checkpoint.header.toBuffer(),
|
|
213
|
+
archive: incoming.checkpoint.archive.toBuffer(),
|
|
214
|
+
checkpointOutHash: incoming.checkpoint.getCheckpointOutHash().toBuffer(),
|
|
215
|
+
l1: incoming.l1.toBuffer(),
|
|
216
|
+
attestations: incoming.attestations.map((a)=>a.toBuffer()),
|
|
217
|
+
checkpointNumber: incoming.checkpoint.number,
|
|
218
|
+
startBlock: incoming.checkpoint.blocks[0].number,
|
|
219
|
+
blockCount: incoming.checkpoint.blocks.length,
|
|
220
|
+
feeAssetPriceModifier: incoming.checkpoint.feeAssetPriceModifier.toString()
|
|
221
|
+
});
|
|
222
|
+
// Update the sync point to reflect the new L1 block
|
|
223
|
+
await this.#lastSynchedL1Block.set(incoming.l1.blockNumber);
|
|
224
|
+
}
|
|
225
|
+
return checkpoints.slice(i);
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Gets the last block of the checkpoint before the given one.
|
|
229
|
+
* Returns undefined if there is no previous checkpoint (i.e. genesis).
|
|
230
|
+
*/ async getPreviousCheckpointBlock(checkpointNumber) {
|
|
231
|
+
const previousCheckpointNumber = CheckpointNumber(checkpointNumber - 1);
|
|
232
|
+
if (previousCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
|
|
233
|
+
return undefined;
|
|
234
|
+
}
|
|
235
|
+
// Check across both proposed and mined checkpoints
|
|
236
|
+
const predecessor = await this.getProposedCheckpointByNumber(previousCheckpointNumber) ?? await this.getCheckpointData(previousCheckpointNumber);
|
|
237
|
+
if (!predecessor) {
|
|
238
|
+
throw new CheckpointNotFoundError(previousCheckpointNumber);
|
|
239
|
+
}
|
|
240
|
+
const previousBlockNumber = BlockNumber(predecessor.startBlock + predecessor.blockCount - 1);
|
|
241
|
+
const previousBlock = await this.getBlock(previousBlockNumber);
|
|
242
|
+
if (previousBlock === undefined) {
|
|
243
|
+
throw new BlockNotFoundError(previousBlockNumber);
|
|
244
|
+
}
|
|
245
|
+
return previousBlock;
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Validates that blocks are sequential, have correct indexes, and chain via archive roots.
|
|
249
|
+
* This is the same validation used for both confirmed checkpoints (addCheckpoints) and
|
|
250
|
+
* proposed checkpoints (addProposedCheckpoint).
|
|
251
|
+
*/ validateCheckpointBlocks(blocks, previousBlock) {
|
|
252
|
+
for (const block of blocks){
|
|
253
|
+
if (previousBlock) {
|
|
254
|
+
if (previousBlock.number !== block.number - 1) {
|
|
255
|
+
throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
|
|
256
|
+
}
|
|
257
|
+
if (previousBlock.checkpointNumber === block.checkpointNumber) {
|
|
258
|
+
if (previousBlock.indexWithinCheckpoint !== block.indexWithinCheckpoint - 1) {
|
|
259
|
+
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
|
|
260
|
+
}
|
|
261
|
+
} else if (block.indexWithinCheckpoint !== 0) {
|
|
262
|
+
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
|
|
263
|
+
}
|
|
264
|
+
if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
|
|
265
|
+
throw new BlockArchiveNotConsistentError(block.number, previousBlock.number, block.header.lastArchive.root, previousBlock.archive.root);
|
|
266
|
+
}
|
|
267
|
+
} else {
|
|
268
|
+
if (block.indexWithinCheckpoint !== 0) {
|
|
269
|
+
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, undefined);
|
|
270
|
+
}
|
|
271
|
+
if (block.number !== INITIAL_L2_BLOCK_NUM) {
|
|
272
|
+
throw new BlockNumberNotSequentialError(block.number, undefined);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
previousBlock = block;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
214
278
|
async addBlockToDatabase(block, checkpointNumber, indexWithinCheckpoint) {
|
|
215
279
|
const blockHash = await block.hash();
|
|
216
280
|
await this.#blocks.set(block.number, {
|
|
@@ -274,15 +338,22 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
274
338
|
if (!targetCheckpoint) {
|
|
275
339
|
throw new Error(`Target checkpoint ${checkpointNumber} not found in store`);
|
|
276
340
|
}
|
|
277
|
-
lastBlockToKeep = BlockNumber(targetCheckpoint.startBlock + targetCheckpoint.
|
|
341
|
+
lastBlockToKeep = BlockNumber(targetCheckpoint.startBlock + targetCheckpoint.blockCount - 1);
|
|
278
342
|
}
|
|
279
343
|
// Remove all blocks after lastBlockToKeep (both checkpointed and uncheckpointed)
|
|
280
344
|
const blocksRemoved = await this.removeBlocksAfter(lastBlockToKeep);
|
|
281
345
|
// Remove all checkpoints after the target
|
|
282
346
|
for(let c = latestCheckpointNumber; c > checkpointNumber; c = CheckpointNumber(c - 1)){
|
|
347
|
+
const checkpointStorage = await this.#checkpoints.getAsync(c);
|
|
348
|
+
if (checkpointStorage) {
|
|
349
|
+
const slotNumber = CheckpointHeader.fromBuffer(checkpointStorage.header).slotNumber;
|
|
350
|
+
await this.#slotToCheckpoint.delete(slotNumber);
|
|
351
|
+
}
|
|
283
352
|
await this.#checkpoints.delete(c);
|
|
284
353
|
this.#log.debug(`Removed checkpoint ${c}`);
|
|
285
354
|
}
|
|
355
|
+
// Evict all pending checkpoints > checkpointNumber (their base chain no longer exists)
|
|
356
|
+
await this.evictProposedCheckpointsFrom(CheckpointNumber(checkpointNumber + 1));
|
|
286
357
|
return {
|
|
287
358
|
blocksRemoved
|
|
288
359
|
};
|
|
@@ -306,17 +377,31 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
306
377
|
}
|
|
307
378
|
return checkpoints;
|
|
308
379
|
}
|
|
380
|
+
/** Returns checkpoint data for all checkpoints whose slot falls within the given range (inclusive). */ async getCheckpointDataForSlotRange(startSlot, endSlot) {
|
|
381
|
+
const result = [];
|
|
382
|
+
for await (const [, checkpointNumber] of this.#slotToCheckpoint.entriesAsync({
|
|
383
|
+
start: startSlot,
|
|
384
|
+
end: endSlot + 1
|
|
385
|
+
})){
|
|
386
|
+
const checkpointStorage = await this.#checkpoints.getAsync(checkpointNumber);
|
|
387
|
+
if (checkpointStorage) {
|
|
388
|
+
result.push(this.checkpointDataFromCheckpointStorage(checkpointStorage));
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
return result;
|
|
392
|
+
}
|
|
309
393
|
checkpointDataFromCheckpointStorage(checkpointStorage) {
|
|
310
|
-
|
|
394
|
+
return {
|
|
311
395
|
header: CheckpointHeader.fromBuffer(checkpointStorage.header),
|
|
312
396
|
archive: AppendOnlyTreeSnapshot.fromBuffer(checkpointStorage.archive),
|
|
397
|
+
checkpointOutHash: Fr.fromBuffer(checkpointStorage.checkpointOutHash),
|
|
313
398
|
checkpointNumber: CheckpointNumber(checkpointStorage.checkpointNumber),
|
|
314
|
-
startBlock: checkpointStorage.startBlock,
|
|
315
|
-
|
|
399
|
+
startBlock: BlockNumber(checkpointStorage.startBlock),
|
|
400
|
+
blockCount: checkpointStorage.blockCount,
|
|
401
|
+
feeAssetPriceModifier: BigInt(checkpointStorage.feeAssetPriceModifier),
|
|
316
402
|
l1: L1PublishedData.fromBuffer(checkpointStorage.l1),
|
|
317
|
-
attestations: checkpointStorage.attestations
|
|
403
|
+
attestations: checkpointStorage.attestations.map((buf)=>CommitteeAttestation.fromBuffer(buf))
|
|
318
404
|
};
|
|
319
|
-
return data;
|
|
320
405
|
}
|
|
321
406
|
async getBlocksForCheckpoint(checkpointNumber) {
|
|
322
407
|
const checkpoint = await this.#checkpoints.getAsync(checkpointNumber);
|
|
@@ -325,7 +410,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
325
410
|
}
|
|
326
411
|
const blocksForCheckpoint = await toArray(this.#blocks.entriesAsync({
|
|
327
412
|
start: checkpoint.startBlock,
|
|
328
|
-
end: checkpoint.startBlock + checkpoint.
|
|
413
|
+
end: checkpoint.startBlock + checkpoint.blockCount
|
|
329
414
|
}));
|
|
330
415
|
const converted = await Promise.all(blocksForCheckpoint.map((x)=>this.getBlockFromBlockStorage(x[0], x[1])));
|
|
331
416
|
return converted.filter(isDefined);
|
|
@@ -362,7 +447,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
362
447
|
return await this.db.transactionAsync(async ()=>{
|
|
363
448
|
const removedBlocks = [];
|
|
364
449
|
// Get the latest block number to determine the range
|
|
365
|
-
const latestBlockNumber = await this.
|
|
450
|
+
const latestBlockNumber = await this.getLatestL2BlockNumber();
|
|
366
451
|
// Iterate from blockNumber + 1 to latestBlockNumber
|
|
367
452
|
for(let bn = blockNumber + 1; bn <= latestBlockNumber; bn++){
|
|
368
453
|
const block = await this.getBlock(BlockNumber(bn));
|
|
@@ -386,16 +471,9 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
386
471
|
if (!checkpointStorage) {
|
|
387
472
|
throw new CheckpointNotFoundError(provenCheckpointNumber);
|
|
388
473
|
} else {
|
|
389
|
-
return BlockNumber(checkpointStorage.startBlock + checkpointStorage.
|
|
474
|
+
return BlockNumber(checkpointStorage.startBlock + checkpointStorage.blockCount - 1);
|
|
390
475
|
}
|
|
391
476
|
}
|
|
392
|
-
async getLatestBlockNumber() {
|
|
393
|
-
const [latestBlocknumber] = await toArray(this.#blocks.keysAsync({
|
|
394
|
-
reverse: true,
|
|
395
|
-
limit: 1
|
|
396
|
-
}));
|
|
397
|
-
return typeof latestBlocknumber === 'number' ? BlockNumber(latestBlocknumber) : BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
398
|
-
}
|
|
399
477
|
async getLatestCheckpointNumber() {
|
|
400
478
|
const [latestCheckpointNumber] = await toArray(this.#checkpoints.keysAsync({
|
|
401
479
|
reverse: true,
|
|
@@ -406,6 +484,142 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
406
484
|
}
|
|
407
485
|
return CheckpointNumber(latestCheckpointNumber);
|
|
408
486
|
}
|
|
487
|
+
async hasProposedCheckpoint() {
|
|
488
|
+
const [key] = await toArray(this.#proposedCheckpoints.keysAsync({
|
|
489
|
+
limit: 1
|
|
490
|
+
}));
|
|
491
|
+
return key !== undefined;
|
|
492
|
+
}
|
|
493
|
+
/** Deletes all pending proposed checkpoints from storage. */ async deleteProposedCheckpoints() {
|
|
494
|
+
for await (const key of this.#proposedCheckpoints.keysAsync()){
|
|
495
|
+
await this.#proposedCheckpoints.delete(key);
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
/**
|
|
499
|
+
* Promotes a specific pending checkpoint to a confirmed checkpoint entry.
|
|
500
|
+
* This persists the checkpoint to the store, removes only that pending entry, and updates the L1 sync point.
|
|
501
|
+
* Remaining pending entries (e.g. N+1, N+2) are left intact — they chain off the just-promoted one.
|
|
502
|
+
* @param checkpointNumber - The checkpoint number to promote.
|
|
503
|
+
* @param l1 - L1 published data for the checkpoint.
|
|
504
|
+
* @param attestations - Committee attestations.
|
|
505
|
+
* @param expectedArchiveRoot - Archive root guard against races.
|
|
506
|
+
*/ async promoteProposedToCheckpointed(checkpointNumber, l1, attestations, expectedArchiveRoot) {
|
|
507
|
+
return await this.db.transactionAsync(async ()=>{
|
|
508
|
+
const proposed = await this.getProposedCheckpointByNumber(checkpointNumber);
|
|
509
|
+
if (!proposed) {
|
|
510
|
+
throw new NoProposedCheckpointToPromoteError();
|
|
511
|
+
}
|
|
512
|
+
if (!proposed.archive.root.equals(expectedArchiveRoot)) {
|
|
513
|
+
throw new ProposedCheckpointArchiveRootMismatchError(expectedArchiveRoot, proposed.archive.root);
|
|
514
|
+
}
|
|
515
|
+
// Verify sequentiality: promoted checkpoint must follow the latest confirmed one
|
|
516
|
+
const latestCheckpointNumber = await this.getLatestCheckpointNumber();
|
|
517
|
+
if (latestCheckpointNumber !== proposed.checkpointNumber - 1) {
|
|
518
|
+
throw new ProposedCheckpointPromotionNotSequentialError(proposed.checkpointNumber, latestCheckpointNumber);
|
|
519
|
+
}
|
|
520
|
+
// Write the checkpoint entry
|
|
521
|
+
await this.#checkpoints.set(proposed.checkpointNumber, {
|
|
522
|
+
header: proposed.header.toBuffer(),
|
|
523
|
+
archive: proposed.archive.toBuffer(),
|
|
524
|
+
checkpointOutHash: proposed.checkpointOutHash.toBuffer(),
|
|
525
|
+
l1: l1.toBuffer(),
|
|
526
|
+
attestations: attestations.map((attestation)=>attestation.toBuffer()),
|
|
527
|
+
checkpointNumber: proposed.checkpointNumber,
|
|
528
|
+
startBlock: proposed.startBlock,
|
|
529
|
+
blockCount: proposed.blockCount,
|
|
530
|
+
feeAssetPriceModifier: proposed.feeAssetPriceModifier.toString()
|
|
531
|
+
});
|
|
532
|
+
// Update the slot-to-checkpoint index
|
|
533
|
+
await this.#slotToCheckpoint.set(proposed.header.slotNumber, proposed.checkpointNumber);
|
|
534
|
+
// Remove only this pending entry — remaining entries N+1, N+2, ... stay valid
|
|
535
|
+
await this.#proposedCheckpoints.delete(proposed.checkpointNumber);
|
|
536
|
+
// Update the last synced L1 block
|
|
537
|
+
await this.#lastSynchedL1Block.set(l1.blockNumber);
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
/**
|
|
541
|
+
* Returns the latest pending checkpoint (highest-numbered entry), or undefined if none.
|
|
542
|
+
* No fallback to confirmed.
|
|
543
|
+
*/ async getLastProposedCheckpoint() {
|
|
544
|
+
const [key] = await toArray(this.#proposedCheckpoints.keysAsync({
|
|
545
|
+
reverse: true,
|
|
546
|
+
limit: 1
|
|
547
|
+
}));
|
|
548
|
+
if (key === undefined) {
|
|
549
|
+
return undefined;
|
|
550
|
+
}
|
|
551
|
+
const stored = await this.#proposedCheckpoints.getAsync(key);
|
|
552
|
+
return stored ? this.convertToProposedCheckpointData(stored) : undefined;
|
|
553
|
+
}
|
|
554
|
+
/** Returns the pending checkpoint for a specific checkpoint number, or undefined if not found. */ async getProposedCheckpointByNumber(n) {
|
|
555
|
+
const stored = await this.#proposedCheckpoints.getAsync(n);
|
|
556
|
+
return stored ? this.convertToProposedCheckpointData(stored) : undefined;
|
|
557
|
+
}
|
|
558
|
+
/** Returns all pending checkpoints in ascending checkpoint-number order. */ async getProposedCheckpoints() {
|
|
559
|
+
const results = [];
|
|
560
|
+
for await (const [, stored] of this.#proposedCheckpoints.entriesAsync()){
|
|
561
|
+
results.push(this.convertToProposedCheckpointData(stored));
|
|
562
|
+
}
|
|
563
|
+
return results;
|
|
564
|
+
}
|
|
565
|
+
/**
|
|
566
|
+
* Evicts all pending checkpoints with checkpoint number >= fromNumber.
|
|
567
|
+
* Used for divergent-mined-checkpoint cleanup: when L1 mines checkpoint N with a different archive,
|
|
568
|
+
* all pending >= N must be evicted since they chain off the now-invalid pending N.
|
|
569
|
+
*/ async evictProposedCheckpointsFrom(fromNumber) {
|
|
570
|
+
const keysToDelete = [];
|
|
571
|
+
for await (const key of this.#proposedCheckpoints.keysAsync()){
|
|
572
|
+
if (key >= fromNumber) {
|
|
573
|
+
keysToDelete.push(key);
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
for (const key of keysToDelete){
|
|
577
|
+
await this.#proposedCheckpoints.delete(key);
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
/**
|
|
581
|
+
* Gets the checkpoint at the proposed tip:
|
|
582
|
+
* - latest pending checkpoint if any exist
|
|
583
|
+
* - fallsback to latest confirmed checkpoint otherwise
|
|
584
|
+
*/ async getLastCheckpoint() {
|
|
585
|
+
const latest = await this.getLastProposedCheckpoint();
|
|
586
|
+
if (!latest) {
|
|
587
|
+
return this.getCheckpointData(await this.getLatestCheckpointNumber());
|
|
588
|
+
}
|
|
589
|
+
return latest;
|
|
590
|
+
}
|
|
591
|
+
convertToProposedCheckpointData(stored) {
|
|
592
|
+
return {
|
|
593
|
+
checkpointNumber: CheckpointNumber(stored.checkpointNumber),
|
|
594
|
+
header: CheckpointHeader.fromBuffer(stored.header),
|
|
595
|
+
archive: AppendOnlyTreeSnapshot.fromBuffer(stored.archive),
|
|
596
|
+
checkpointOutHash: Fr.fromBuffer(stored.checkpointOutHash),
|
|
597
|
+
startBlock: BlockNumber(stored.startBlock),
|
|
598
|
+
blockCount: stored.blockCount,
|
|
599
|
+
totalManaUsed: BigInt(stored.totalManaUsed),
|
|
600
|
+
feeAssetPriceModifier: BigInt(stored.feeAssetPriceModifier)
|
|
601
|
+
};
|
|
602
|
+
}
|
|
603
|
+
/**
|
|
604
|
+
* Attempts to get the proposedCheckpoint's number, if there is not one, then fallback to the latest confirmed checkpoint number.
|
|
605
|
+
* @returns CheckpointNumber
|
|
606
|
+
*/ async getProposedCheckpointNumber() {
|
|
607
|
+
const proposed = await this.getLastCheckpoint();
|
|
608
|
+
if (!proposed) {
|
|
609
|
+
return await this.getLatestCheckpointNumber();
|
|
610
|
+
}
|
|
611
|
+
return CheckpointNumber(proposed.checkpointNumber);
|
|
612
|
+
}
|
|
613
|
+
/**
|
|
614
|
+
* Attempts to get the proposedCheckpoint's block number, if there is not one, then fallback to the checkpointed block number
|
|
615
|
+
* @returns BlockNumber
|
|
616
|
+
*/ async getProposedCheckpointL2BlockNumber() {
|
|
617
|
+
const proposed = await this.getLastCheckpoint();
|
|
618
|
+
if (!proposed) {
|
|
619
|
+
return await this.getCheckpointedL2BlockNumber();
|
|
620
|
+
}
|
|
621
|
+
return BlockNumber(proposed.startBlock + proposed.blockCount - 1);
|
|
622
|
+
}
|
|
409
623
|
async getCheckpointedBlock(number) {
|
|
410
624
|
const blockStorage = await this.#blocks.getAsync(number);
|
|
411
625
|
if (!blockStorage) {
|
|
@@ -426,7 +640,10 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
426
640
|
* @param start - Number of the first block to return (inclusive).
|
|
427
641
|
* @param limit - The number of blocks to return.
|
|
428
642
|
* @returns The requested L2 blocks
|
|
429
|
-
*/
|
|
643
|
+
*/ getCheckpointedBlocks(start, limit) {
|
|
644
|
+
return toArray(this.iterateCheckpointedBlocks(start, limit));
|
|
645
|
+
}
|
|
646
|
+
/** Async iterator variant of {@link getCheckpointedBlocks}. */ async *iterateCheckpointedBlocks(start, limit) {
|
|
430
647
|
const checkpointCache = new Map();
|
|
431
648
|
for await (const [blockNumber, blockStorage] of this.getBlockStorages(start, limit)){
|
|
432
649
|
const block = await this.getBlockFromBlockStorage(blockNumber, blockStorage);
|
|
@@ -459,7 +676,10 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
459
676
|
* @param start - Number of the first block to return (inclusive).
|
|
460
677
|
* @param limit - The number of blocks to return.
|
|
461
678
|
* @returns The requested L2 blocks
|
|
462
|
-
*/
|
|
679
|
+
*/ getBlocks(start, limit) {
|
|
680
|
+
return toArray(this.iterateBlocks(start, limit));
|
|
681
|
+
}
|
|
682
|
+
/** Async iterator variant of {@link getBlocks}. */ async *iterateBlocks(start, limit) {
|
|
463
683
|
for await (const [blockNumber, blockStorage] of this.getBlockStorages(start, limit)){
|
|
464
684
|
const block = await this.getBlockFromBlockStorage(blockNumber, blockStorage);
|
|
465
685
|
if (block) {
|
|
@@ -468,6 +688,59 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
468
688
|
}
|
|
469
689
|
}
|
|
470
690
|
/**
|
|
691
|
+
* Gets block metadata (without tx data) by block number.
|
|
692
|
+
* @param blockNumber - The number of the block to return.
|
|
693
|
+
* @returns The requested block data.
|
|
694
|
+
*/ async getBlockData(blockNumber) {
|
|
695
|
+
const blockStorage = await this.#blocks.getAsync(blockNumber);
|
|
696
|
+
if (!blockStorage || !blockStorage.header) {
|
|
697
|
+
return undefined;
|
|
698
|
+
}
|
|
699
|
+
return this.getBlockDataFromBlockStorage(blockStorage);
|
|
700
|
+
}
|
|
701
|
+
/**
|
|
702
|
+
* Gets block metadata plus checkpoint-derived context (L1 publish info, attestations) without
|
|
703
|
+
* deserializing tx bodies. When the block's containing checkpoint has not yet been L1-confirmed,
|
|
704
|
+
* `checkpoint` and `l1` are `undefined` and `attestations` is empty.
|
|
705
|
+
*/ async getBlockDataWithCheckpointContext(blockNumber) {
|
|
706
|
+
const blockStorage = await this.#blocks.getAsync(blockNumber);
|
|
707
|
+
if (!blockStorage || !blockStorage.header) {
|
|
708
|
+
return undefined;
|
|
709
|
+
}
|
|
710
|
+
const data = this.getBlockDataFromBlockStorage(blockStorage);
|
|
711
|
+
const checkpointStorage = await this.#checkpoints.getAsync(blockStorage.checkpointNumber);
|
|
712
|
+
if (!checkpointStorage) {
|
|
713
|
+
return {
|
|
714
|
+
data,
|
|
715
|
+
checkpoint: undefined,
|
|
716
|
+
l1: undefined,
|
|
717
|
+
attestations: []
|
|
718
|
+
};
|
|
719
|
+
}
|
|
720
|
+
const checkpoint = this.checkpointDataFromCheckpointStorage(checkpointStorage);
|
|
721
|
+
return {
|
|
722
|
+
data,
|
|
723
|
+
checkpoint,
|
|
724
|
+
l1: checkpoint.l1,
|
|
725
|
+
attestations: checkpoint.attestations
|
|
726
|
+
};
|
|
727
|
+
}
|
|
728
|
+
/** Returns the checkpoint number that contains the given slot (or undefined if not found). */ async getCheckpointNumberBySlot(slot) {
|
|
729
|
+
const checkpointNumber = await this.#slotToCheckpoint.getAsync(slot);
|
|
730
|
+
return checkpointNumber === undefined ? undefined : CheckpointNumber(checkpointNumber);
|
|
731
|
+
}
|
|
732
|
+
/**
|
|
733
|
+
* Gets block metadata (without tx data) by archive root.
|
|
734
|
+
* @param archive - The archive root of the block to return.
|
|
735
|
+
* @returns The requested block data.
|
|
736
|
+
*/ async getBlockDataByArchive(archive) {
|
|
737
|
+
const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
|
|
738
|
+
if (blockNumber === undefined) {
|
|
739
|
+
return undefined;
|
|
740
|
+
}
|
|
741
|
+
return this.getBlockData(BlockNumber(blockNumber));
|
|
742
|
+
}
|
|
743
|
+
/**
|
|
471
744
|
* Gets an L2 block.
|
|
472
745
|
* @param blockNumber - The number of the block to return.
|
|
473
746
|
* @returns The requested L2 block.
|
|
@@ -535,7 +808,10 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
535
808
|
* @param start - Number of the first block to return (inclusive).
|
|
536
809
|
* @param limit - The number of blocks to return.
|
|
537
810
|
* @returns The requested L2 block headers
|
|
538
|
-
*/
|
|
811
|
+
*/ getBlockHeaders(start, limit) {
|
|
812
|
+
return toArray(this.iterateBlockHeaders(start, limit));
|
|
813
|
+
}
|
|
814
|
+
/** Async iterator variant of {@link getBlockHeaders}. */ async *iterateBlockHeaders(start, limit) {
|
|
539
815
|
for await (const [blockNumber, blockStorage] of this.getBlockStorages(start, limit)){
|
|
540
816
|
const header = BlockHeader.fromBuffer(blockStorage.header);
|
|
541
817
|
if (header.getBlockNumber() !== blockNumber) {
|
|
@@ -557,12 +833,19 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
557
833
|
];
|
|
558
834
|
}
|
|
559
835
|
}
|
|
836
|
+
getBlockDataFromBlockStorage(blockStorage) {
|
|
837
|
+
return {
|
|
838
|
+
header: BlockHeader.fromBuffer(blockStorage.header),
|
|
839
|
+
archive: AppendOnlyTreeSnapshot.fromBuffer(blockStorage.archive),
|
|
840
|
+
blockHash: BlockHash.fromBuffer(blockStorage.blockHash),
|
|
841
|
+
checkpointNumber: CheckpointNumber(blockStorage.checkpointNumber),
|
|
842
|
+
indexWithinCheckpoint: IndexWithinCheckpoint(blockStorage.indexWithinCheckpoint)
|
|
843
|
+
};
|
|
844
|
+
}
|
|
560
845
|
async getBlockFromBlockStorage(blockNumber, blockStorage) {
|
|
561
|
-
const header =
|
|
562
|
-
|
|
563
|
-
const
|
|
564
|
-
header.setHash(Fr.fromBuffer(blockHash));
|
|
565
|
-
const blockHashString = bufferToHex(blockHash);
|
|
846
|
+
const { header, archive, blockHash, checkpointNumber, indexWithinCheckpoint } = this.getBlockDataFromBlockStorage(blockStorage);
|
|
847
|
+
header.setHash(blockHash);
|
|
848
|
+
const blockHashString = bufferToHex(blockStorage.blockHash);
|
|
566
849
|
const blockTxsBuffer = await this.#blockTxs.getAsync(blockHashString);
|
|
567
850
|
if (blockTxsBuffer === undefined) {
|
|
568
851
|
this.#log.warn(`Could not find body for block ${header.globalVariables.blockNumber} ${blockHash}`);
|
|
@@ -580,7 +863,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
580
863
|
txEffects.push(deserializeIndexedTxEffect(txEffect).data);
|
|
581
864
|
}
|
|
582
865
|
const body = new Body(txEffects);
|
|
583
|
-
const block = new L2Block(archive, header, body,
|
|
866
|
+
const block = new L2Block(archive, header, body, checkpointNumber, indexWithinCheckpoint);
|
|
584
867
|
if (block.number !== blockNumber) {
|
|
585
868
|
throw new Error(`Block number mismatch when retrieving block from archive (expected ${blockNumber} but got ${block.number} with hash ${blockHashString})`);
|
|
586
869
|
}
|
|
@@ -601,17 +884,18 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
601
884
|
* Gets a receipt of a settled tx.
|
|
602
885
|
* @param txHash - The hash of a tx we try to get the receipt for.
|
|
603
886
|
* @returns The requested tx receipt (or undefined if not found).
|
|
604
|
-
*/ async getSettledTxReceipt(txHash) {
|
|
887
|
+
*/ async getSettledTxReceipt(txHash, l1Constants) {
|
|
605
888
|
const txEffect = await this.getTxEffect(txHash);
|
|
606
889
|
if (!txEffect) {
|
|
607
890
|
return undefined;
|
|
608
891
|
}
|
|
609
892
|
const blockNumber = BlockNumber(txEffect.l2BlockNumber);
|
|
610
893
|
// Use existing archiver methods to determine finalization level
|
|
611
|
-
const [provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber] = await Promise.all([
|
|
894
|
+
const [provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber, blockData] = await Promise.all([
|
|
612
895
|
this.getProvenBlockNumber(),
|
|
613
896
|
this.getCheckpointedL2BlockNumber(),
|
|
614
|
-
this.getFinalizedL2BlockNumber()
|
|
897
|
+
this.getFinalizedL2BlockNumber(),
|
|
898
|
+
this.getBlockData(blockNumber)
|
|
615
899
|
]);
|
|
616
900
|
let status;
|
|
617
901
|
if (blockNumber <= finalizedBlockNumber) {
|
|
@@ -623,7 +907,8 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
623
907
|
} else {
|
|
624
908
|
status = TxStatus.PROPOSED;
|
|
625
909
|
}
|
|
626
|
-
|
|
910
|
+
const epochNumber = blockData && l1Constants ? getEpochAtSlot(blockData.header.globalVariables.slotNumber, l1Constants) : undefined;
|
|
911
|
+
return new TxReceipt(txHash, status, TxReceipt.executionResultFromRevertCode(txEffect.data.revertCode), undefined, txEffect.data.transactionFee.toBigInt(), txEffect.l2BlockHash, blockNumber, epochNumber);
|
|
627
912
|
}
|
|
628
913
|
/**
|
|
629
914
|
* Looks up which block included the requested tx effect.
|
|
@@ -656,7 +941,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
656
941
|
if (!checkpoint) {
|
|
657
942
|
return BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
658
943
|
}
|
|
659
|
-
return BlockNumber(checkpoint.startBlock + checkpoint.
|
|
944
|
+
return BlockNumber(checkpoint.startBlock + checkpoint.blockCount - 1);
|
|
660
945
|
}
|
|
661
946
|
async getLatestL2BlockNumber() {
|
|
662
947
|
const [lastBlockNumber] = await toArray(this.#blocks.keysAsync({
|
|
@@ -674,6 +959,47 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
674
959
|
setSynchedL1BlockNumber(l1BlockNumber) {
|
|
675
960
|
return this.#lastSynchedL1Block.set(l1BlockNumber);
|
|
676
961
|
}
|
|
962
|
+
/**
|
|
963
|
+
* Adds a proposed checkpoint to the pending queue.
|
|
964
|
+
* Accepts proposed.checkpointNumber === latestTip + 1, where latestTip is the highest of
|
|
965
|
+
* confirmed and the highest pending checkpoint number.
|
|
966
|
+
* Computes archive and checkpointOutHash from the stored blocks.
|
|
967
|
+
*/ async addProposedCheckpoint(proposed) {
|
|
968
|
+
return await this.db.transactionAsync(async ()=>{
|
|
969
|
+
const confirmed = await this.getLatestCheckpointNumber();
|
|
970
|
+
const [latestPendingKey] = await toArray(this.#proposedCheckpoints.keysAsync({
|
|
971
|
+
reverse: true,
|
|
972
|
+
limit: 1
|
|
973
|
+
}));
|
|
974
|
+
const latestTip = CheckpointNumber(latestPendingKey !== undefined ? Math.max(latestPendingKey, confirmed) : confirmed);
|
|
975
|
+
if (proposed.checkpointNumber !== latestTip + 1) {
|
|
976
|
+
throw new ProposedCheckpointNotSequentialError(proposed.checkpointNumber, latestTip);
|
|
977
|
+
}
|
|
978
|
+
// Ensure the predecessor block (from pending or confirmed chain) exists
|
|
979
|
+
const previousBlock = await this.getPreviousCheckpointBlock(proposed.checkpointNumber);
|
|
980
|
+
const blocks = [];
|
|
981
|
+
for(let i = 0; i < proposed.blockCount; i++){
|
|
982
|
+
const block = await this.getBlock(BlockNumber(proposed.startBlock + i));
|
|
983
|
+
if (!block) {
|
|
984
|
+
throw new BlockNotFoundError(proposed.startBlock + i);
|
|
985
|
+
}
|
|
986
|
+
blocks.push(block);
|
|
987
|
+
}
|
|
988
|
+
this.validateCheckpointBlocks(blocks, previousBlock);
|
|
989
|
+
const archive = blocks[blocks.length - 1].archive;
|
|
990
|
+
const checkpointOutHash = Checkpoint.getCheckpointOutHash(blocks);
|
|
991
|
+
await this.#proposedCheckpoints.set(proposed.checkpointNumber, {
|
|
992
|
+
header: proposed.header.toBuffer(),
|
|
993
|
+
archive: archive.toBuffer(),
|
|
994
|
+
checkpointOutHash: checkpointOutHash.toBuffer(),
|
|
995
|
+
checkpointNumber: proposed.checkpointNumber,
|
|
996
|
+
startBlock: proposed.startBlock,
|
|
997
|
+
blockCount: proposed.blockCount,
|
|
998
|
+
totalManaUsed: proposed.totalManaUsed.toString(),
|
|
999
|
+
feeAssetPriceModifier: proposed.feeAssetPriceModifier.toString()
|
|
1000
|
+
});
|
|
1001
|
+
});
|
|
1002
|
+
}
|
|
677
1003
|
async getProvenCheckpointNumber() {
|
|
678
1004
|
const [latestCheckpointNumber, provenCheckpointNumber] = await Promise.all([
|
|
679
1005
|
this.getLatestCheckpointNumber(),
|
|
@@ -685,6 +1011,16 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
685
1011
|
const result = await this.#lastProvenCheckpoint.set(checkpointNumber);
|
|
686
1012
|
return result;
|
|
687
1013
|
}
|
|
1014
|
+
async getFinalizedCheckpointNumber() {
|
|
1015
|
+
const [latestCheckpointNumber, finalizedCheckpointNumber] = await Promise.all([
|
|
1016
|
+
this.getLatestCheckpointNumber(),
|
|
1017
|
+
this.#lastFinalizedCheckpoint.getAsync()
|
|
1018
|
+
]);
|
|
1019
|
+
return (finalizedCheckpointNumber ?? 0) > latestCheckpointNumber ? latestCheckpointNumber : CheckpointNumber(finalizedCheckpointNumber ?? 0);
|
|
1020
|
+
}
|
|
1021
|
+
setFinalizedCheckpointNumber(checkpointNumber) {
|
|
1022
|
+
return this.#lastFinalizedCheckpoint.set(checkpointNumber);
|
|
1023
|
+
}
|
|
688
1024
|
#computeBlockRange(start, limit) {
|
|
689
1025
|
if (limit < 1) {
|
|
690
1026
|
throw new Error(`Invalid limit: ${limit}`);
|