@aztec/archiver 0.0.1-commit.86469d5 → 0.0.1-commit.8655d4a
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 +19 -11
- package/dest/archiver.d.ts +39 -17
- package/dest/archiver.d.ts.map +1 -1
- package/dest/archiver.js +260 -160
- package/dest/config.d.ts +6 -3
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +23 -15
- package/dest/errors.d.ts +55 -9
- package/dest/errors.d.ts.map +1 -1
- package/dest/errors.js +81 -14
- package/dest/factory.d.ts +13 -9
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +51 -37
- package/dest/index.d.ts +12 -3
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +11 -2
- package/dest/l1/bin/retrieve-calldata.js +36 -33
- 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 +201 -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/trace_tx.d.ts +12 -66
- package/dest/l1/trace_tx.d.ts.map +1 -1
- 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 +40 -0
- package/dest/modules/data_source_base.d.ts +71 -42
- package/dest/modules/data_source_base.d.ts.map +1 -1
- package/dest/modules/data_source_base.js +270 -179
- package/dest/modules/data_store_updater.d.ts +49 -17
- package/dest/modules/data_store_updater.d.ts.map +1 -1
- package/dest/modules/data_store_updater.js +211 -121
- package/dest/modules/instrumentation.d.ts +21 -3
- package/dest/modules/instrumentation.d.ts.map +1 -1
- package/dest/modules/instrumentation.js +44 -9
- package/dest/modules/l1_synchronizer.d.ts +14 -12
- package/dest/modules/l1_synchronizer.d.ts.map +1 -1
- package/dest/modules/l1_synchronizer.js +443 -211
- 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 +174 -66
- package/dest/store/block_store.d.ts.map +1 -1
- package/dest/store/block_store.js +743 -245
- 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 +54 -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 +25 -0
- package/dest/store/l2_tips_cache.d.ts.map +1 -0
- package/dest/store/l2_tips_cache.js +26 -0
- package/dest/store/log_store.d.ts +42 -37
- package/dest/store/log_store.d.ts.map +1 -1
- package/dest/store/log_store.js +262 -388
- package/dest/store/log_store_codec.d.ts +70 -0
- package/dest/store/log_store_codec.d.ts.map +1 -0
- package/dest/store/log_store_codec.js +101 -0
- 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 +62 -41
- package/dest/test/mock_l2_block_source.d.ts.map +1 -1
- package/dest/test/mock_l2_block_source.js +321 -202
- 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 +12 -6
- package/dest/test/noop_l1_archiver.d.ts.map +1 -1
- package/dest/test/noop_l1_archiver.js +26 -9
- package/package.json +14 -14
- package/src/archiver.ts +319 -181
- package/src/config.ts +32 -12
- package/src/errors.ts +122 -21
- package/src/factory.ts +75 -36
- package/src/index.ts +19 -2
- package/src/l1/README.md +25 -68
- package/src/l1/bin/retrieve-calldata.ts +46 -39
- package/src/l1/calldata_retriever.ts +260 -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 +55 -0
- package/src/modules/data_source_base.ts +347 -221
- package/src/modules/data_store_updater.ts +248 -153
- package/src/modules/instrumentation.ts +56 -9
- package/src/modules/l1_synchronizer.ts +585 -258
- package/src/modules/validation.ts +10 -9
- package/src/store/block_store.ts +924 -300
- package/src/store/contract_class_store.ts +31 -103
- package/src/store/contract_instance_store.ts +51 -5
- package/src/store/data_stores.ts +104 -0
- package/src/store/function_names_cache.ts +37 -0
- package/src/store/l2_tips_cache.ts +35 -0
- package/src/store/log_store.ts +303 -499
- package/src/store/log_store_codec.ts +132 -0
- 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 +394 -210
- package/src/test/mock_structs.ts +20 -6
- package/src/test/noop_l1_archiver.ts +39 -9
- 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
|
@@ -1,37 +1,41 @@
|
|
|
1
1
|
import { INITIAL_CHECKPOINT_NUMBER, INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
|
|
2
|
-
import { BlockNumber, CheckpointNumber, IndexWithinCheckpoint } from '@aztec/foundation/branded-types';
|
|
2
|
+
import { BlockNumber, CheckpointNumber, IndexWithinCheckpoint, SlotNumber } from '@aztec/foundation/branded-types';
|
|
3
3
|
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
4
4
|
import { toArray } from '@aztec/foundation/iterable';
|
|
5
5
|
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 {
|
|
10
|
-
import { L1PublishedData } from '@aztec/stdlib/checkpoint';
|
|
9
|
+
import { BlockHash, Body, CommitteeAttestation, GENESIS_CHECKPOINT_HEADER_HASH, 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;
|
|
34
|
+
/** Map rejected checkpoints (due to invalid attestations) by archive root */ #rejectedCheckpoints;
|
|
35
|
+
/** Index mapping a rejected checkpoint's number to its archive root, so the latest can be read in reverse order */ #rejectedCheckpointsByNumber;
|
|
31
36
|
#log;
|
|
32
|
-
constructor(db
|
|
37
|
+
constructor(db){
|
|
33
38
|
this.db = db;
|
|
34
|
-
this.l1Constants = l1Constants;
|
|
35
39
|
this.#log = createLogger('archiver:block_store');
|
|
36
40
|
this.#blocks = db.openMap('archiver_blocks');
|
|
37
41
|
this.#blockTxs = db.openMap('archiver_block_txs');
|
|
@@ -41,94 +45,98 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
41
45
|
this.#blockArchiveIndex = db.openMap('archiver_block_archive_index');
|
|
42
46
|
this.#lastSynchedL1Block = db.openSingleton('archiver_last_synched_l1_block');
|
|
43
47
|
this.#lastProvenCheckpoint = db.openSingleton('archiver_last_proven_l2_checkpoint');
|
|
48
|
+
this.#lastFinalizedCheckpoint = db.openSingleton('archiver_last_finalized_l2_checkpoint');
|
|
44
49
|
this.#pendingChainValidationStatus = db.openSingleton('archiver_pending_chain_validation_status');
|
|
45
50
|
this.#checkpoints = db.openMap('archiver_checkpoints');
|
|
51
|
+
this.#slotToCheckpoint = db.openMap('archiver_slot_to_checkpoint');
|
|
52
|
+
this.#proposedCheckpoints = db.openMap('archiver_proposed_checkpoints');
|
|
53
|
+
this.#rejectedCheckpoints = db.openMap('archiver_rejected_checkpoints');
|
|
54
|
+
this.#rejectedCheckpointsByNumber = db.openMap('archiver_rejected_checkpoints_by_number');
|
|
46
55
|
}
|
|
47
56
|
/**
|
|
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
|
|
57
|
+
* Returns the finalized L2 block number. An L2 block is finalized when it was proven
|
|
58
|
+
* in an L1 block that has itself been finalized on Ethereum.
|
|
52
59
|
* @returns The finalized block number.
|
|
53
60
|
*/ async getFinalizedL2BlockNumber() {
|
|
54
|
-
const
|
|
55
|
-
|
|
61
|
+
const finalizedCheckpointNumber = await this.getFinalizedCheckpointNumber();
|
|
62
|
+
if (finalizedCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
|
|
63
|
+
return BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
64
|
+
}
|
|
65
|
+
const checkpointStorage = await this.#checkpoints.getAsync(finalizedCheckpointNumber);
|
|
66
|
+
if (!checkpointStorage) {
|
|
67
|
+
throw new CheckpointNotFoundError(finalizedCheckpointNumber);
|
|
68
|
+
}
|
|
69
|
+
return BlockNumber(checkpointStorage.startBlock + checkpointStorage.blockCount - 1);
|
|
56
70
|
}
|
|
57
71
|
/**
|
|
58
|
-
* Append new proposed
|
|
59
|
-
*
|
|
72
|
+
* Append a new proposed block to the store.
|
|
73
|
+
* This is an uncheckpointed block that has been proposed by the sequencer but not yet included in a checkpoint on L1.
|
|
60
74
|
* For checkpointed blocks (already published to L1), use addCheckpoints() instead.
|
|
61
|
-
* @param
|
|
75
|
+
* @param block - The proposed L2 block to be added to the store.
|
|
62
76
|
* @returns True if the operation is successful.
|
|
63
|
-
*/ async
|
|
64
|
-
if (blocks.length === 0) {
|
|
65
|
-
return true;
|
|
66
|
-
}
|
|
77
|
+
*/ async addProposedBlock(block, opts = {}) {
|
|
67
78
|
return await this.db.transactionAsync(async ()=>{
|
|
68
|
-
|
|
69
|
-
const
|
|
70
|
-
const
|
|
71
|
-
const
|
|
72
|
-
const firstBlockLastArchive = blocks[0].header.lastArchive.root;
|
|
79
|
+
const blockNumber = block.number;
|
|
80
|
+
const blockCheckpointNumber = block.checkpointNumber;
|
|
81
|
+
const blockIndex = block.indexWithinCheckpoint;
|
|
82
|
+
const blockLastArchive = block.header.lastArchive.root;
|
|
73
83
|
// Extract the latest block and checkpoint numbers
|
|
74
|
-
const previousBlockNumber = await this.
|
|
75
|
-
const
|
|
84
|
+
const previousBlockNumber = await this.getLatestL2BlockNumber();
|
|
85
|
+
const latestCheckpointNumber = await this.getLatestCheckpointNumber();
|
|
76
86
|
// Verify we're not overwriting checkpointed blocks
|
|
77
87
|
const lastCheckpointedBlockNumber = await this.getCheckpointedL2BlockNumber();
|
|
78
|
-
if (!opts.force &&
|
|
79
|
-
|
|
88
|
+
if (!opts.force && blockNumber <= lastCheckpointedBlockNumber) {
|
|
89
|
+
// Check if the proposed block matches the already-checkpointed one
|
|
90
|
+
const existingBlock = await this.getBlockData({
|
|
91
|
+
number: BlockNumber(blockNumber)
|
|
92
|
+
});
|
|
93
|
+
if (existingBlock && existingBlock.archive.root.equals(block.archive.root)) {
|
|
94
|
+
throw new BlockAlreadyCheckpointedError(blockNumber);
|
|
95
|
+
}
|
|
96
|
+
throw new CannotOverwriteCheckpointedBlockError(blockNumber, lastCheckpointedBlockNumber);
|
|
80
97
|
}
|
|
81
|
-
// Check that the
|
|
82
|
-
if (!opts.force && previousBlockNumber !==
|
|
83
|
-
throw new
|
|
98
|
+
// Check that the block number is the expected one
|
|
99
|
+
if (!opts.force && previousBlockNumber !== blockNumber - 1) {
|
|
100
|
+
throw new BlockNumberNotSequentialError(blockNumber, previousBlockNumber);
|
|
84
101
|
}
|
|
85
|
-
//
|
|
86
|
-
|
|
87
|
-
|
|
102
|
+
// Accept the block if either the confirmed checkpoint or a pending checkpoint matches
|
|
103
|
+
// the expected predecessor. We look for a pending entry at exactly blockCheckpointNumber - 1.
|
|
104
|
+
const expectedCheckpointNumber = blockCheckpointNumber - 1;
|
|
105
|
+
const hasPendingAtExpected = await this.#proposedCheckpoints.hasAsync(expectedCheckpointNumber);
|
|
106
|
+
if (!opts.force && latestCheckpointNumber !== expectedCheckpointNumber && !hasPendingAtExpected) {
|
|
107
|
+
const [latestPendingKey] = await toArray(this.#proposedCheckpoints.keysAsync({
|
|
108
|
+
reverse: true,
|
|
109
|
+
limit: 1
|
|
110
|
+
}));
|
|
111
|
+
const previous = CheckpointNumber(Math.max(latestCheckpointNumber, latestPendingKey ?? 0));
|
|
112
|
+
throw new BlockCheckpointNumberNotSequentialError(BlockNumber(blockNumber), blockCheckpointNumber, previous);
|
|
88
113
|
}
|
|
89
114
|
// Extract the previous block if there is one and see if it is for the same checkpoint or not
|
|
90
|
-
const previousBlockResult = await this.
|
|
91
|
-
|
|
115
|
+
const previousBlockResult = await this.getBlockData({
|
|
116
|
+
number: previousBlockNumber
|
|
117
|
+
});
|
|
118
|
+
let expectedBlockIndex = 0;
|
|
92
119
|
let previousBlockIndex = undefined;
|
|
93
120
|
if (previousBlockResult !== undefined) {
|
|
94
|
-
if (previousBlockResult.checkpointNumber ===
|
|
121
|
+
if (previousBlockResult.checkpointNumber === blockCheckpointNumber) {
|
|
95
122
|
// The previous block is for the same checkpoint, therefore our index should follow it
|
|
96
123
|
previousBlockIndex = previousBlockResult.indexWithinCheckpoint;
|
|
97
|
-
|
|
124
|
+
expectedBlockIndex = previousBlockIndex + 1;
|
|
98
125
|
}
|
|
99
|
-
if (!previousBlockResult.archive.root.equals(
|
|
100
|
-
throw new BlockArchiveNotConsistentError(
|
|
126
|
+
if (!previousBlockResult.archive.root.equals(blockLastArchive)) {
|
|
127
|
+
throw new BlockArchiveNotConsistentError(blockNumber, previousBlockResult.header.globalVariables.blockNumber, blockLastArchive, previousBlockResult.archive.root);
|
|
101
128
|
}
|
|
102
129
|
}
|
|
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);
|
|
130
|
+
// Now check that the block has the expected index value
|
|
131
|
+
if (!opts.force && expectedBlockIndex !== blockIndex) {
|
|
132
|
+
throw new BlockIndexNotSequentialError(blockIndex, previousBlockIndex);
|
|
126
133
|
}
|
|
134
|
+
await this.addBlockToDatabase(block, block.checkpointNumber, block.indexWithinCheckpoint);
|
|
127
135
|
return true;
|
|
128
136
|
});
|
|
129
137
|
}
|
|
130
138
|
/**
|
|
131
|
-
* Append new
|
|
139
|
+
* Append new checkpoints to the store's list.
|
|
132
140
|
* @param checkpoints - The L2 checkpoints to be added to the store.
|
|
133
141
|
* @returns True if the operation is successful.
|
|
134
142
|
*/ async addCheckpoints(checkpoints, opts = {}) {
|
|
@@ -136,32 +144,26 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
136
144
|
return true;
|
|
137
145
|
}
|
|
138
146
|
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
147
|
const firstCheckpointNumber = checkpoints[0].checkpoint.number;
|
|
141
148
|
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);
|
|
149
|
+
// Handle already-stored checkpoints at the start of the batch.
|
|
150
|
+
// This can happen after an L1 reorg re-includes a checkpoint in a different L1 block.
|
|
151
|
+
// We accept them if archives match (same content) and update their L1 metadata.
|
|
152
|
+
if (!opts.force && firstCheckpointNumber <= previousCheckpointNumber) {
|
|
153
|
+
checkpoints = await this.skipOrUpdateAlreadyStoredCheckpoints(checkpoints, previousCheckpointNumber);
|
|
154
|
+
if (checkpoints.length === 0) {
|
|
155
|
+
return true;
|
|
152
156
|
}
|
|
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);
|
|
157
|
+
// Re-check sequentiality after skipping
|
|
158
|
+
const newFirstNumber = checkpoints[0].checkpoint.number;
|
|
159
|
+
if (previousCheckpointNumber !== newFirstNumber - 1) {
|
|
160
|
+
throw new InitialCheckpointNumberNotSequentialError(newFirstNumber, previousCheckpointNumber);
|
|
163
161
|
}
|
|
162
|
+
} else if (previousCheckpointNumber !== firstCheckpointNumber - 1 && !opts.force) {
|
|
163
|
+
throw new InitialCheckpointNumberNotSequentialError(firstCheckpointNumber, previousCheckpointNumber);
|
|
164
164
|
}
|
|
165
|
+
// Get the last block of the previous checkpoint for archive chaining
|
|
166
|
+
let previousBlock = await this.getPreviousCheckpointBlock(checkpoints[0].checkpoint.number);
|
|
165
167
|
// Iterate over checkpoints array and insert them, checking that the block numbers are sequential.
|
|
166
168
|
let previousCheckpoint = undefined;
|
|
167
169
|
for (const checkpoint of checkpoints){
|
|
@@ -169,48 +171,123 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
169
171
|
throw new CheckpointNumberNotSequentialError(checkpoint.checkpoint.number, previousCheckpoint.checkpoint.number);
|
|
170
172
|
}
|
|
171
173
|
previousCheckpoint = checkpoint;
|
|
172
|
-
//
|
|
174
|
+
// Validate block sequencing, indexes, and archive chaining
|
|
175
|
+
this.validateCheckpointBlocks(checkpoint.checkpoint.blocks, previousBlock);
|
|
176
|
+
// Store every block in the database (may already exist, but L1 data is authoritative)
|
|
173
177
|
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);
|
|
178
|
+
await this.addBlockToDatabase(checkpoint.checkpoint.blocks[i], checkpoint.checkpoint.number, i);
|
|
198
179
|
}
|
|
180
|
+
previousBlock = checkpoint.checkpoint.blocks.at(-1);
|
|
199
181
|
// Store the checkpoint in the database
|
|
200
182
|
await this.#checkpoints.set(checkpoint.checkpoint.number, {
|
|
201
183
|
header: checkpoint.checkpoint.header.toBuffer(),
|
|
202
184
|
archive: checkpoint.checkpoint.archive.toBuffer(),
|
|
185
|
+
checkpointOutHash: checkpoint.checkpoint.getCheckpointOutHash().toBuffer(),
|
|
203
186
|
l1: checkpoint.l1.toBuffer(),
|
|
204
187
|
attestations: checkpoint.attestations.map((attestation)=>attestation.toBuffer()),
|
|
205
188
|
checkpointNumber: checkpoint.checkpoint.number,
|
|
206
189
|
startBlock: checkpoint.checkpoint.blocks[0].number,
|
|
207
|
-
|
|
190
|
+
blockCount: checkpoint.checkpoint.blocks.length,
|
|
191
|
+
feeAssetPriceModifier: checkpoint.checkpoint.feeAssetPriceModifier.toString()
|
|
208
192
|
});
|
|
193
|
+
// Update slot-to-checkpoint index
|
|
194
|
+
await this.#slotToCheckpoint.set(checkpoint.checkpoint.header.slotNumber, checkpoint.checkpoint.number);
|
|
195
|
+
// Remove proposed checkpoint if it exists, since L1 is authoritative
|
|
196
|
+
await this.#proposedCheckpoints.delete(checkpoint.checkpoint.number);
|
|
197
|
+
// Drop any rejected entry for this archive root: a checkpoint that was previously rejected
|
|
198
|
+
// (e.g. invalid attestations) is now being ingested as valid, so its descendants are allowed.
|
|
199
|
+
await this.removeRejectedCheckpointByArchiveRoot(checkpoint.checkpoint.archive.root);
|
|
209
200
|
}
|
|
210
|
-
await this
|
|
201
|
+
await this.advanceSynchedL1BlockNumber(checkpoints[checkpoints.length - 1].l1.blockNumber);
|
|
211
202
|
return true;
|
|
212
203
|
});
|
|
213
204
|
}
|
|
205
|
+
/**
|
|
206
|
+
* Handles checkpoints at the start of a batch that are already stored (e.g. due to L1 reorg).
|
|
207
|
+
* Verifies the archive root matches, updates L1 metadata, and returns only the new checkpoints.
|
|
208
|
+
*/ async skipOrUpdateAlreadyStoredCheckpoints(checkpoints, latestStored) {
|
|
209
|
+
let i = 0;
|
|
210
|
+
for(; i < checkpoints.length && checkpoints[i].checkpoint.number <= latestStored; i++){
|
|
211
|
+
const incoming = checkpoints[i];
|
|
212
|
+
const stored = await this.getCheckpointData(incoming.checkpoint.number);
|
|
213
|
+
if (!stored) {
|
|
214
|
+
break;
|
|
215
|
+
}
|
|
216
|
+
// Verify the checkpoint content matches (archive root)
|
|
217
|
+
if (!stored.archive.root.equals(incoming.checkpoint.archive.root)) {
|
|
218
|
+
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}`);
|
|
219
|
+
}
|
|
220
|
+
// Update L1 metadata and attestations for the already-stored checkpoint
|
|
221
|
+
this.#log.warn(`Checkpoint ${incoming.checkpoint.number} already stored, updating L1 info ` + `(L1 block ${stored.l1.blockNumber} -> ${incoming.l1.blockNumber})`);
|
|
222
|
+
await this.#checkpoints.set(incoming.checkpoint.number, {
|
|
223
|
+
header: incoming.checkpoint.header.toBuffer(),
|
|
224
|
+
archive: incoming.checkpoint.archive.toBuffer(),
|
|
225
|
+
checkpointOutHash: incoming.checkpoint.getCheckpointOutHash().toBuffer(),
|
|
226
|
+
l1: incoming.l1.toBuffer(),
|
|
227
|
+
attestations: incoming.attestations.map((a)=>a.toBuffer()),
|
|
228
|
+
checkpointNumber: incoming.checkpoint.number,
|
|
229
|
+
startBlock: incoming.checkpoint.blocks[0].number,
|
|
230
|
+
blockCount: incoming.checkpoint.blocks.length,
|
|
231
|
+
feeAssetPriceModifier: incoming.checkpoint.feeAssetPriceModifier.toString()
|
|
232
|
+
});
|
|
233
|
+
// Update the sync point to reflect the new L1 block
|
|
234
|
+
await this.advanceSynchedL1BlockNumber(incoming.l1.blockNumber);
|
|
235
|
+
}
|
|
236
|
+
return checkpoints.slice(i);
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Gets the last block of the checkpoint before the given one.
|
|
240
|
+
* Returns undefined if there is no previous checkpoint (i.e. genesis).
|
|
241
|
+
*/ async getPreviousCheckpointBlock(checkpointNumber) {
|
|
242
|
+
const previousCheckpointNumber = CheckpointNumber(checkpointNumber - 1);
|
|
243
|
+
if (previousCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
|
|
244
|
+
return undefined;
|
|
245
|
+
}
|
|
246
|
+
// Check across both proposed and mined checkpoints
|
|
247
|
+
const predecessor = await this.getProposedCheckpointByNumber(previousCheckpointNumber) ?? await this.getCheckpointData(previousCheckpointNumber);
|
|
248
|
+
if (!predecessor) {
|
|
249
|
+
throw new CheckpointNotFoundError(previousCheckpointNumber);
|
|
250
|
+
}
|
|
251
|
+
const previousBlockNumber = BlockNumber(predecessor.startBlock + predecessor.blockCount - 1);
|
|
252
|
+
const previousBlock = await this.getBlock({
|
|
253
|
+
number: previousBlockNumber
|
|
254
|
+
});
|
|
255
|
+
if (previousBlock === undefined) {
|
|
256
|
+
throw new BlockNotFoundError(previousBlockNumber);
|
|
257
|
+
}
|
|
258
|
+
return previousBlock;
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Validates that blocks are sequential, have correct indexes, and chain via archive roots.
|
|
262
|
+
* This is the same validation used for both confirmed checkpoints (addCheckpoints) and
|
|
263
|
+
* proposed checkpoints (addProposedCheckpoint).
|
|
264
|
+
*/ validateCheckpointBlocks(blocks, previousBlock) {
|
|
265
|
+
for (const block of blocks){
|
|
266
|
+
if (previousBlock) {
|
|
267
|
+
if (previousBlock.number !== block.number - 1) {
|
|
268
|
+
throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
|
|
269
|
+
}
|
|
270
|
+
if (previousBlock.checkpointNumber === block.checkpointNumber) {
|
|
271
|
+
if (previousBlock.indexWithinCheckpoint !== block.indexWithinCheckpoint - 1) {
|
|
272
|
+
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
|
|
273
|
+
}
|
|
274
|
+
} else if (block.indexWithinCheckpoint !== 0) {
|
|
275
|
+
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
|
|
276
|
+
}
|
|
277
|
+
if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
|
|
278
|
+
throw new BlockArchiveNotConsistentError(block.number, previousBlock.number, block.header.lastArchive.root, previousBlock.archive.root);
|
|
279
|
+
}
|
|
280
|
+
} else {
|
|
281
|
+
if (block.indexWithinCheckpoint !== 0) {
|
|
282
|
+
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, undefined);
|
|
283
|
+
}
|
|
284
|
+
if (block.number !== INITIAL_L2_BLOCK_NUM) {
|
|
285
|
+
throw new BlockNumberNotSequentialError(block.number, undefined);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
previousBlock = block;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
214
291
|
async addBlockToDatabase(block, checkpointNumber, indexWithinCheckpoint) {
|
|
215
292
|
const blockHash = await block.hash();
|
|
216
293
|
await this.#blocks.set(block.number, {
|
|
@@ -274,15 +351,22 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
274
351
|
if (!targetCheckpoint) {
|
|
275
352
|
throw new Error(`Target checkpoint ${checkpointNumber} not found in store`);
|
|
276
353
|
}
|
|
277
|
-
lastBlockToKeep = BlockNumber(targetCheckpoint.startBlock + targetCheckpoint.
|
|
354
|
+
lastBlockToKeep = BlockNumber(targetCheckpoint.startBlock + targetCheckpoint.blockCount - 1);
|
|
278
355
|
}
|
|
279
356
|
// Remove all blocks after lastBlockToKeep (both checkpointed and uncheckpointed)
|
|
280
357
|
const blocksRemoved = await this.removeBlocksAfter(lastBlockToKeep);
|
|
281
358
|
// Remove all checkpoints after the target
|
|
282
359
|
for(let c = latestCheckpointNumber; c > checkpointNumber; c = CheckpointNumber(c - 1)){
|
|
360
|
+
const checkpointStorage = await this.#checkpoints.getAsync(c);
|
|
361
|
+
if (checkpointStorage) {
|
|
362
|
+
const slotNumber = CheckpointHeader.fromBuffer(checkpointStorage.header).slotNumber;
|
|
363
|
+
await this.#slotToCheckpoint.delete(slotNumber);
|
|
364
|
+
}
|
|
283
365
|
await this.#checkpoints.delete(c);
|
|
284
366
|
this.#log.debug(`Removed checkpoint ${c}`);
|
|
285
367
|
}
|
|
368
|
+
// Evict all pending checkpoints > checkpointNumber (their base chain no longer exists)
|
|
369
|
+
await this.evictProposedCheckpointsFrom(CheckpointNumber(checkpointNumber + 1));
|
|
286
370
|
return {
|
|
287
371
|
blocksRemoved
|
|
288
372
|
};
|
|
@@ -306,17 +390,45 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
306
390
|
}
|
|
307
391
|
return checkpoints;
|
|
308
392
|
}
|
|
393
|
+
/** Returns checkpoint data for all checkpoints whose slot falls within the given range (inclusive). */ async getCheckpointDataForSlotRange(startSlot, endSlot) {
|
|
394
|
+
const result = [];
|
|
395
|
+
for await (const [, checkpointNumber] of this.#slotToCheckpoint.entriesAsync({
|
|
396
|
+
start: startSlot,
|
|
397
|
+
end: endSlot + 1
|
|
398
|
+
})){
|
|
399
|
+
const checkpointStorage = await this.#checkpoints.getAsync(checkpointNumber);
|
|
400
|
+
if (checkpointStorage) {
|
|
401
|
+
result.push(this.checkpointDataFromCheckpointStorage(checkpointStorage));
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
return result;
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* Returns the checkpoint numbers for all checkpoints whose slot falls within the given range (inclusive).
|
|
408
|
+
* Lighter than {@link getCheckpointDataForSlotRange} when callers only need to identify which
|
|
409
|
+
* checkpoints fall in the range and will fetch full data for at most a few of them.
|
|
410
|
+
*/ async getCheckpointNumbersForSlotRange(startSlot, endSlot) {
|
|
411
|
+
const result = [];
|
|
412
|
+
for await (const [, checkpointNumber] of this.#slotToCheckpoint.entriesAsync({
|
|
413
|
+
start: startSlot,
|
|
414
|
+
end: endSlot + 1
|
|
415
|
+
})){
|
|
416
|
+
result.push(CheckpointNumber(checkpointNumber));
|
|
417
|
+
}
|
|
418
|
+
return result;
|
|
419
|
+
}
|
|
309
420
|
checkpointDataFromCheckpointStorage(checkpointStorage) {
|
|
310
|
-
|
|
421
|
+
return {
|
|
311
422
|
header: CheckpointHeader.fromBuffer(checkpointStorage.header),
|
|
312
423
|
archive: AppendOnlyTreeSnapshot.fromBuffer(checkpointStorage.archive),
|
|
424
|
+
checkpointOutHash: Fr.fromBuffer(checkpointStorage.checkpointOutHash),
|
|
313
425
|
checkpointNumber: CheckpointNumber(checkpointStorage.checkpointNumber),
|
|
314
|
-
startBlock: checkpointStorage.startBlock,
|
|
315
|
-
|
|
426
|
+
startBlock: BlockNumber(checkpointStorage.startBlock),
|
|
427
|
+
blockCount: checkpointStorage.blockCount,
|
|
428
|
+
feeAssetPriceModifier: BigInt(checkpointStorage.feeAssetPriceModifier),
|
|
316
429
|
l1: L1PublishedData.fromBuffer(checkpointStorage.l1),
|
|
317
|
-
attestations: checkpointStorage.attestations
|
|
430
|
+
attestations: checkpointStorage.attestations.map((buf)=>CommitteeAttestation.fromBuffer(buf))
|
|
318
431
|
};
|
|
319
|
-
return data;
|
|
320
432
|
}
|
|
321
433
|
async getBlocksForCheckpoint(checkpointNumber) {
|
|
322
434
|
const checkpoint = await this.#checkpoints.getAsync(checkpointNumber);
|
|
@@ -325,7 +437,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
325
437
|
}
|
|
326
438
|
const blocksForCheckpoint = await toArray(this.#blocks.entriesAsync({
|
|
327
439
|
start: checkpoint.startBlock,
|
|
328
|
-
end: checkpoint.startBlock + checkpoint.
|
|
440
|
+
end: checkpoint.startBlock + checkpoint.blockCount
|
|
329
441
|
}));
|
|
330
442
|
const converted = await Promise.all(blocksForCheckpoint.map((x)=>this.getBlockFromBlockStorage(x[0], x[1])));
|
|
331
443
|
return converted.filter(isDefined);
|
|
@@ -362,10 +474,12 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
362
474
|
return await this.db.transactionAsync(async ()=>{
|
|
363
475
|
const removedBlocks = [];
|
|
364
476
|
// Get the latest block number to determine the range
|
|
365
|
-
const latestBlockNumber = await this.
|
|
477
|
+
const latestBlockNumber = await this.getLatestL2BlockNumber();
|
|
366
478
|
// Iterate from blockNumber + 1 to latestBlockNumber
|
|
367
479
|
for(let bn = blockNumber + 1; bn <= latestBlockNumber; bn++){
|
|
368
|
-
const block = await this.getBlock(
|
|
480
|
+
const block = await this.getBlock({
|
|
481
|
+
number: BlockNumber(bn)
|
|
482
|
+
});
|
|
369
483
|
if (block === undefined) {
|
|
370
484
|
this.#log.warn(`Cannot remove block ${bn} from the store since we don't have it`);
|
|
371
485
|
continue;
|
|
@@ -386,16 +500,9 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
386
500
|
if (!checkpointStorage) {
|
|
387
501
|
throw new CheckpointNotFoundError(provenCheckpointNumber);
|
|
388
502
|
} else {
|
|
389
|
-
return BlockNumber(checkpointStorage.startBlock + checkpointStorage.
|
|
503
|
+
return BlockNumber(checkpointStorage.startBlock + checkpointStorage.blockCount - 1);
|
|
390
504
|
}
|
|
391
505
|
}
|
|
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
506
|
async getLatestCheckpointNumber() {
|
|
400
507
|
const [latestCheckpointNumber] = await toArray(this.#checkpoints.keysAsync({
|
|
401
508
|
reverse: true,
|
|
@@ -406,121 +513,171 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
406
513
|
}
|
|
407
514
|
return CheckpointNumber(latestCheckpointNumber);
|
|
408
515
|
}
|
|
409
|
-
async
|
|
410
|
-
const
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
const block = await this.getBlockFromBlockStorage(number, blockStorage);
|
|
419
|
-
if (!block) {
|
|
420
|
-
return undefined;
|
|
516
|
+
async hasProposedCheckpoint() {
|
|
517
|
+
const [key] = await toArray(this.#proposedCheckpoints.keysAsync({
|
|
518
|
+
limit: 1
|
|
519
|
+
}));
|
|
520
|
+
return key !== undefined;
|
|
521
|
+
}
|
|
522
|
+
/** Deletes all pending proposed checkpoints from storage. */ async deleteProposedCheckpoints() {
|
|
523
|
+
for await (const key of this.#proposedCheckpoints.keysAsync()){
|
|
524
|
+
await this.#proposedCheckpoints.delete(key);
|
|
421
525
|
}
|
|
422
|
-
return new CheckpointedL2Block(CheckpointNumber(checkpoint.checkpointNumber), block, L1PublishedData.fromBuffer(checkpoint.l1), checkpoint.attestations.map((buf)=>CommitteeAttestation.fromBuffer(buf)));
|
|
423
526
|
}
|
|
424
527
|
/**
|
|
425
|
-
*
|
|
426
|
-
*
|
|
427
|
-
*
|
|
428
|
-
* @
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
const checkpointedBlock = new CheckpointedL2Block(CheckpointNumber(checkpoint.checkpointNumber), block, L1PublishedData.fromBuffer(checkpoint.l1), checkpoint.attestations.map((buf)=>CommitteeAttestation.fromBuffer(buf)));
|
|
438
|
-
yield checkpointedBlock;
|
|
439
|
-
}
|
|
528
|
+
* Promotes a specific pending checkpoint to a confirmed checkpoint entry.
|
|
529
|
+
* This persists the checkpoint to the store, removes only that pending entry, and updates the L1 sync point.
|
|
530
|
+
* Remaining pending entries (e.g. N+1, N+2) are left intact — they chain off the just-promoted one.
|
|
531
|
+
* @param checkpointNumber - The checkpoint number to promote.
|
|
532
|
+
* @param l1 - L1 published data for the checkpoint.
|
|
533
|
+
* @param attestations - Committee attestations.
|
|
534
|
+
* @param expectedArchiveRoot - Archive root guard against races.
|
|
535
|
+
*/ async promoteProposedToCheckpointed(checkpointNumber, l1, attestations, expectedArchiveRoot) {
|
|
536
|
+
return await this.db.transactionAsync(async ()=>{
|
|
537
|
+
const proposed = await this.getProposedCheckpointByNumber(checkpointNumber);
|
|
538
|
+
if (!proposed) {
|
|
539
|
+
throw new NoProposedCheckpointToPromoteError();
|
|
440
540
|
}
|
|
441
|
-
|
|
541
|
+
if (!proposed.archive.root.equals(expectedArchiveRoot)) {
|
|
542
|
+
throw new ProposedCheckpointArchiveRootMismatchError(expectedArchiveRoot, proposed.archive.root);
|
|
543
|
+
}
|
|
544
|
+
// Verify sequentiality: promoted checkpoint must follow the latest confirmed one
|
|
545
|
+
const latestCheckpointNumber = await this.getLatestCheckpointNumber();
|
|
546
|
+
if (latestCheckpointNumber !== proposed.checkpointNumber - 1) {
|
|
547
|
+
throw new ProposedCheckpointPromotionNotSequentialError(proposed.checkpointNumber, latestCheckpointNumber);
|
|
548
|
+
}
|
|
549
|
+
// Write the checkpoint entry
|
|
550
|
+
await this.#checkpoints.set(proposed.checkpointNumber, {
|
|
551
|
+
header: proposed.header.toBuffer(),
|
|
552
|
+
archive: proposed.archive.toBuffer(),
|
|
553
|
+
checkpointOutHash: proposed.checkpointOutHash.toBuffer(),
|
|
554
|
+
l1: l1.toBuffer(),
|
|
555
|
+
attestations: attestations.map((attestation)=>attestation.toBuffer()),
|
|
556
|
+
checkpointNumber: proposed.checkpointNumber,
|
|
557
|
+
startBlock: proposed.startBlock,
|
|
558
|
+
blockCount: proposed.blockCount,
|
|
559
|
+
feeAssetPriceModifier: proposed.feeAssetPriceModifier.toString()
|
|
560
|
+
});
|
|
561
|
+
// Update the slot-to-checkpoint index
|
|
562
|
+
await this.#slotToCheckpoint.set(proposed.header.slotNumber, proposed.checkpointNumber);
|
|
563
|
+
// Remove only this pending entry — remaining entries N+1, N+2, ... stay valid
|
|
564
|
+
await this.#proposedCheckpoints.delete(proposed.checkpointNumber);
|
|
565
|
+
// Drop any rejected entry for this archive root: a checkpoint that was previously rejected
|
|
566
|
+
// (e.g. invalid attestations) is now being promoted as valid, so its descendants are allowed.
|
|
567
|
+
await this.removeRejectedCheckpointByArchiveRoot(proposed.archive.root);
|
|
568
|
+
// Update the last synced L1 block
|
|
569
|
+
await this.advanceSynchedL1BlockNumber(l1.blockNumber);
|
|
570
|
+
});
|
|
442
571
|
}
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
572
|
+
/**
|
|
573
|
+
* Returns the latest pending checkpoint (highest-numbered entry), or undefined if none.
|
|
574
|
+
* No fallback to confirmed.
|
|
575
|
+
*/ async getLastProposedCheckpoint() {
|
|
576
|
+
const [key] = await toArray(this.#proposedCheckpoints.keysAsync({
|
|
577
|
+
reverse: true,
|
|
578
|
+
limit: 1
|
|
579
|
+
}));
|
|
580
|
+
if (key === undefined) {
|
|
446
581
|
return undefined;
|
|
447
582
|
}
|
|
448
|
-
|
|
583
|
+
const stored = await this.#proposedCheckpoints.getAsync(key);
|
|
584
|
+
return stored ? this.convertToProposedCheckpointData(stored) : undefined;
|
|
449
585
|
}
|
|
450
|
-
async
|
|
451
|
-
const
|
|
452
|
-
|
|
453
|
-
return undefined;
|
|
454
|
-
}
|
|
455
|
-
return this.getCheckpointedBlock(BlockNumber(blockNumber));
|
|
586
|
+
/** Returns the pending checkpoint for a specific checkpoint number, or undefined if not found. */ async getProposedCheckpointByNumber(n) {
|
|
587
|
+
const stored = await this.#proposedCheckpoints.getAsync(n);
|
|
588
|
+
return stored ? this.convertToProposedCheckpointData(stored) : undefined;
|
|
456
589
|
}
|
|
457
590
|
/**
|
|
458
|
-
*
|
|
459
|
-
*
|
|
460
|
-
*
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
yield block;
|
|
591
|
+
* Returns the pending checkpoint whose header slot matches the given slot, or undefined if not found.
|
|
592
|
+
* Iterates `#proposedCheckpoints` rather than reading an index because the map carries 0–1 entries
|
|
593
|
+
* in normal operation (bounded by the proposer pipelining window). Returns the first match.
|
|
594
|
+
*/ async getProposedCheckpointBySlot(slot) {
|
|
595
|
+
for await (const [, stored] of this.#proposedCheckpoints.entriesAsync()){
|
|
596
|
+
const header = CheckpointHeader.fromBuffer(stored.header);
|
|
597
|
+
if (header.slotNumber === slot) {
|
|
598
|
+
return this.convertToProposedCheckpointData(stored);
|
|
467
599
|
}
|
|
468
600
|
}
|
|
601
|
+
return undefined;
|
|
469
602
|
}
|
|
470
603
|
/**
|
|
471
|
-
*
|
|
472
|
-
*
|
|
473
|
-
*
|
|
474
|
-
*/ async
|
|
475
|
-
const
|
|
476
|
-
|
|
477
|
-
|
|
604
|
+
* Evicts all pending checkpoints with checkpoint number >= fromNumber.
|
|
605
|
+
* Used for divergent-mined-checkpoint cleanup: when L1 mines checkpoint N with a different archive,
|
|
606
|
+
* all pending >= N must be evicted since they chain off the now-invalid pending N.
|
|
607
|
+
*/ async evictProposedCheckpointsFrom(fromNumber) {
|
|
608
|
+
const keysToDelete = [];
|
|
609
|
+
for await (const key of this.#proposedCheckpoints.keysAsync()){
|
|
610
|
+
if (key >= fromNumber) {
|
|
611
|
+
keysToDelete.push(key);
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
for (const key of keysToDelete){
|
|
615
|
+
await this.#proposedCheckpoints.delete(key);
|
|
478
616
|
}
|
|
479
|
-
return this.getBlockFromBlockStorage(blockNumber, blockStorage);
|
|
480
617
|
}
|
|
481
618
|
/**
|
|
482
|
-
* Gets
|
|
483
|
-
*
|
|
484
|
-
*
|
|
485
|
-
*/ async
|
|
486
|
-
const
|
|
487
|
-
if (
|
|
488
|
-
return
|
|
619
|
+
* Gets the checkpoint at the proposed tip:
|
|
620
|
+
* - latest pending checkpoint if any exist
|
|
621
|
+
* - fallsback to latest confirmed checkpoint otherwise
|
|
622
|
+
*/ async getLastCheckpoint() {
|
|
623
|
+
const latest = await this.getLastProposedCheckpoint();
|
|
624
|
+
if (!latest) {
|
|
625
|
+
return this.getCheckpointData(await this.getLatestCheckpointNumber());
|
|
489
626
|
}
|
|
490
|
-
return
|
|
627
|
+
return latest;
|
|
628
|
+
}
|
|
629
|
+
convertToProposedCheckpointData(stored) {
|
|
630
|
+
return {
|
|
631
|
+
checkpointNumber: CheckpointNumber(stored.checkpointNumber),
|
|
632
|
+
header: CheckpointHeader.fromBuffer(stored.header),
|
|
633
|
+
archive: AppendOnlyTreeSnapshot.fromBuffer(stored.archive),
|
|
634
|
+
checkpointOutHash: Fr.fromBuffer(stored.checkpointOutHash),
|
|
635
|
+
startBlock: BlockNumber(stored.startBlock),
|
|
636
|
+
blockCount: stored.blockCount,
|
|
637
|
+
totalManaUsed: BigInt(stored.totalManaUsed),
|
|
638
|
+
feeAssetPriceModifier: BigInt(stored.feeAssetPriceModifier)
|
|
639
|
+
};
|
|
491
640
|
}
|
|
492
641
|
/**
|
|
493
|
-
*
|
|
494
|
-
* @
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
return undefined;
|
|
642
|
+
* Attempts to get the proposedCheckpoint's number, if there is not one, then fallback to the latest confirmed checkpoint number.
|
|
643
|
+
* @returns CheckpointNumber
|
|
644
|
+
*/ async getProposedCheckpointNumber() {
|
|
645
|
+
const proposed = await this.getLastCheckpoint();
|
|
646
|
+
if (!proposed) {
|
|
647
|
+
return await this.getLatestCheckpointNumber();
|
|
500
648
|
}
|
|
501
|
-
return
|
|
649
|
+
return CheckpointNumber(proposed.checkpointNumber);
|
|
502
650
|
}
|
|
503
651
|
/**
|
|
504
|
-
*
|
|
505
|
-
* @
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
652
|
+
* Attempts to get the proposedCheckpoint's block number, if there is not one, then fallback to the checkpointed block number
|
|
653
|
+
* @returns BlockNumber
|
|
654
|
+
*/ async getProposedCheckpointL2BlockNumber() {
|
|
655
|
+
const proposed = await this.getLastCheckpoint();
|
|
656
|
+
if (!proposed) {
|
|
657
|
+
return await this.getCheckpointedL2BlockNumber();
|
|
658
|
+
}
|
|
659
|
+
return BlockNumber(proposed.startBlock + proposed.blockCount - 1);
|
|
660
|
+
}
|
|
661
|
+
/** Returns the checkpoint number that contains the given slot (or undefined if not found). */ async getCheckpointNumberBySlot(slot) {
|
|
662
|
+
const checkpointNumber = await this.#slotToCheckpoint.getAsync(slot);
|
|
663
|
+
return checkpointNumber === undefined ? undefined : CheckpointNumber(checkpointNumber);
|
|
664
|
+
}
|
|
665
|
+
/** Gets a single L2 block matching the given resolved query. */ async getBlock(query) {
|
|
666
|
+
const blockNumber = await this.getBlockNumber(query);
|
|
509
667
|
if (blockNumber === undefined) {
|
|
510
668
|
return undefined;
|
|
511
669
|
}
|
|
512
670
|
const blockStorage = await this.#blocks.getAsync(blockNumber);
|
|
513
|
-
if (!blockStorage
|
|
671
|
+
if (!blockStorage) {
|
|
514
672
|
return undefined;
|
|
515
673
|
}
|
|
516
|
-
return
|
|
674
|
+
return this.getBlockFromBlockStorage(blockNumber, blockStorage);
|
|
517
675
|
}
|
|
518
|
-
/**
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
|
|
676
|
+
/** Gets a collection of L2 blocks for a resolved range. */ getBlocks(query) {
|
|
677
|
+
return toArray(this.iterateBlocks(query));
|
|
678
|
+
}
|
|
679
|
+
/** Gets single block metadata matching the given resolved query. */ async getBlockData(query) {
|
|
680
|
+
const blockNumber = await this.getBlockNumber(query);
|
|
524
681
|
if (blockNumber === undefined) {
|
|
525
682
|
return undefined;
|
|
526
683
|
}
|
|
@@ -528,20 +685,30 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
528
685
|
if (!blockStorage || !blockStorage.header) {
|
|
529
686
|
return undefined;
|
|
530
687
|
}
|
|
531
|
-
return
|
|
688
|
+
return this.getBlockDataFromBlockStorage(blockStorage);
|
|
532
689
|
}
|
|
533
|
-
/**
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
690
|
+
/** Gets a collection of block metadata entries for a resolved range. */ getBlocksData(query) {
|
|
691
|
+
return toArray(this.iterateBlocksData(query));
|
|
692
|
+
}
|
|
693
|
+
/** Async iterator over L2 blocks for a resolved range. */ async *iterateBlocks(query) {
|
|
694
|
+
const cap = query.onlyCheckpointed ? await this.getCheckpointedL2BlockNumber() : undefined;
|
|
695
|
+
for await (const [blockNumber, blockStorage] of this.getBlockStorages(query.from, query.limit)){
|
|
696
|
+
if (cap !== undefined && blockNumber > cap) {
|
|
697
|
+
break;
|
|
698
|
+
}
|
|
699
|
+
const block = await this.getBlockFromBlockStorage(blockNumber, blockStorage);
|
|
700
|
+
if (block) {
|
|
701
|
+
yield block;
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
/** Async iterator over block metadata for a resolved range. */ async *iterateBlocksData(query) {
|
|
706
|
+
const cap = query.onlyCheckpointed ? await this.getCheckpointedL2BlockNumber() : undefined;
|
|
707
|
+
for await (const [blockNumber, blockStorage] of this.getBlockStorages(query.from, query.limit)){
|
|
708
|
+
if (cap !== undefined && blockNumber > cap) {
|
|
709
|
+
break;
|
|
543
710
|
}
|
|
544
|
-
yield
|
|
711
|
+
yield this.getBlockDataFromBlockStorage(blockStorage);
|
|
545
712
|
}
|
|
546
713
|
}
|
|
547
714
|
async *getBlockStorages(start, limit) {
|
|
@@ -557,12 +724,35 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
557
724
|
];
|
|
558
725
|
}
|
|
559
726
|
}
|
|
727
|
+
/** Resolves a ResolvedBlockQuery discriminant to a block number, or undefined if not found. */ async getBlockNumber(query) {
|
|
728
|
+
let blockNumber;
|
|
729
|
+
if ('number' in query) {
|
|
730
|
+
blockNumber = query.number;
|
|
731
|
+
} else if ('hash' in query) {
|
|
732
|
+
const n = await this.#blockHashIndex.getAsync(query.hash.toString());
|
|
733
|
+
blockNumber = n !== undefined ? BlockNumber(n) : undefined;
|
|
734
|
+
} else {
|
|
735
|
+
const n = await this.#blockArchiveIndex.getAsync(query.archive.toString());
|
|
736
|
+
blockNumber = n !== undefined ? BlockNumber(n) : undefined;
|
|
737
|
+
}
|
|
738
|
+
if (blockNumber === undefined) {
|
|
739
|
+
return undefined;
|
|
740
|
+
}
|
|
741
|
+
return blockNumber;
|
|
742
|
+
}
|
|
743
|
+
getBlockDataFromBlockStorage(blockStorage) {
|
|
744
|
+
return {
|
|
745
|
+
header: BlockHeader.fromBuffer(blockStorage.header),
|
|
746
|
+
archive: AppendOnlyTreeSnapshot.fromBuffer(blockStorage.archive),
|
|
747
|
+
blockHash: BlockHash.fromBuffer(blockStorage.blockHash),
|
|
748
|
+
checkpointNumber: CheckpointNumber(blockStorage.checkpointNumber),
|
|
749
|
+
indexWithinCheckpoint: IndexWithinCheckpoint(blockStorage.indexWithinCheckpoint)
|
|
750
|
+
};
|
|
751
|
+
}
|
|
560
752
|
async getBlockFromBlockStorage(blockNumber, blockStorage) {
|
|
561
|
-
const header =
|
|
562
|
-
|
|
563
|
-
const
|
|
564
|
-
header.setHash(Fr.fromBuffer(blockHash));
|
|
565
|
-
const blockHashString = bufferToHex(blockHash);
|
|
753
|
+
const { header, archive, blockHash, checkpointNumber, indexWithinCheckpoint } = this.getBlockDataFromBlockStorage(blockStorage);
|
|
754
|
+
header.setHash(blockHash);
|
|
755
|
+
const blockHashString = bufferToHex(blockStorage.blockHash);
|
|
566
756
|
const blockTxsBuffer = await this.#blockTxs.getAsync(blockHashString);
|
|
567
757
|
if (blockTxsBuffer === undefined) {
|
|
568
758
|
this.#log.warn(`Could not find body for block ${header.globalVariables.blockNumber} ${blockHash}`);
|
|
@@ -580,7 +770,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
580
770
|
txEffects.push(deserializeIndexedTxEffect(txEffect).data);
|
|
581
771
|
}
|
|
582
772
|
const body = new Body(txEffects);
|
|
583
|
-
const block = new L2Block(archive, header, body,
|
|
773
|
+
const block = new L2Block(archive, header, body, checkpointNumber, indexWithinCheckpoint);
|
|
584
774
|
if (block.number !== blockNumber) {
|
|
585
775
|
throw new Error(`Block number mismatch when retrieving block from archive (expected ${blockNumber} but got ${block.number} with hash ${blockHashString})`);
|
|
586
776
|
}
|
|
@@ -601,17 +791,20 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
601
791
|
* Gets a receipt of a settled tx.
|
|
602
792
|
* @param txHash - The hash of a tx we try to get the receipt for.
|
|
603
793
|
* @returns The requested tx receipt (or undefined if not found).
|
|
604
|
-
*/ async getSettledTxReceipt(txHash) {
|
|
794
|
+
*/ async getSettledTxReceipt(txHash, l1Constants) {
|
|
605
795
|
const txEffect = await this.getTxEffect(txHash);
|
|
606
796
|
if (!txEffect) {
|
|
607
797
|
return undefined;
|
|
608
798
|
}
|
|
609
799
|
const blockNumber = BlockNumber(txEffect.l2BlockNumber);
|
|
610
800
|
// Use existing archiver methods to determine finalization level
|
|
611
|
-
const [provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber] = await Promise.all([
|
|
801
|
+
const [provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber, blockData] = await Promise.all([
|
|
612
802
|
this.getProvenBlockNumber(),
|
|
613
803
|
this.getCheckpointedL2BlockNumber(),
|
|
614
|
-
this.getFinalizedL2BlockNumber()
|
|
804
|
+
this.getFinalizedL2BlockNumber(),
|
|
805
|
+
this.getBlockData({
|
|
806
|
+
number: blockNumber
|
|
807
|
+
})
|
|
615
808
|
]);
|
|
616
809
|
let status;
|
|
617
810
|
if (blockNumber <= finalizedBlockNumber) {
|
|
@@ -623,7 +816,8 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
623
816
|
} else {
|
|
624
817
|
status = TxStatus.PROPOSED;
|
|
625
818
|
}
|
|
626
|
-
|
|
819
|
+
const epochNumber = blockData && l1Constants ? getEpochAtSlot(blockData.header.globalVariables.slotNumber, l1Constants) : undefined;
|
|
820
|
+
return new TxReceipt(txHash, status, TxReceipt.executionResultFromRevertCode(txEffect.data.revertCode), undefined, txEffect.data.transactionFee.toBigInt(), txEffect.l2BlockHash, blockNumber, epochNumber);
|
|
627
821
|
}
|
|
628
822
|
/**
|
|
629
823
|
* Looks up which block included the requested tx effect.
|
|
@@ -634,13 +828,48 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
634
828
|
if (!txEffect) {
|
|
635
829
|
return undefined;
|
|
636
830
|
}
|
|
637
|
-
|
|
831
|
+
// Read only the IndexedTxEffect header (`blockHash(32) + l2BlockNumber(4) + txIndexInBlock(4)`); the
|
|
832
|
+
// large tail (the full TxEffect with logs etc.) is irrelevant here.
|
|
833
|
+
const view = Buffer.from(txEffect.buffer, txEffect.byteOffset, txEffect.byteLength);
|
|
834
|
+
const l2BlockNumber = view.readUInt32BE(32);
|
|
835
|
+
const txIndexInBlock = view.readUInt32BE(36);
|
|
638
836
|
return [
|
|
639
837
|
l2BlockNumber,
|
|
640
838
|
txIndexInBlock
|
|
641
839
|
];
|
|
642
840
|
}
|
|
643
841
|
/**
|
|
842
|
+
* Batched, partial deserializer that fetches `noteHashes` and `nullifiers` (all of them) for the given
|
|
843
|
+
* txs. For each input txHash, returns a `[noteHashes, nullifiers]` tuple. Returns `[[], []]` for any
|
|
844
|
+
* unknown txHash. Preserves input order. Used by the log read path when `includeEffects` is set to
|
|
845
|
+
* attach effect data on demand without paying for a full {@link TxEffect} deserialization.
|
|
846
|
+
*
|
|
847
|
+
* The on-disk `IndexedTxEffect` layout starts with a fixed-length header
|
|
848
|
+
* (`blockHash(32) + l2BlockNumber(4) + txIndexInBlock(4) + revertCode(1) + txHash(32) + transactionFee(32)` =
|
|
849
|
+
* 105 bytes), followed by `noteHashes` and `nullifiers` (both u8-length-prefixed `Fr` vectors). We
|
|
850
|
+
* skip the header, then read the two vectors, and stop — the large tail (`l2ToL1Msgs`,
|
|
851
|
+
* `publicDataWrites`, `privateLogs`, `publicLogs`, `contractClassLogs`) is never touched.
|
|
852
|
+
*/ getNoteHashesAndNullifiers(txHashes) {
|
|
853
|
+
return Promise.all(txHashes.map(async (txHash)=>{
|
|
854
|
+
const buffer = await this.#txEffects.getAsync(txHash.toString());
|
|
855
|
+
if (!buffer) {
|
|
856
|
+
return [
|
|
857
|
+
[],
|
|
858
|
+
[]
|
|
859
|
+
];
|
|
860
|
+
}
|
|
861
|
+
const reader = BufferReader.asReader(buffer);
|
|
862
|
+
// Skip the fixed-length header: blockHash + l2BlockNumber + txIndexInBlock + revertCode + txHash + transactionFee.
|
|
863
|
+
reader.readBytes(32 + 4 + 4 + 1 + 32 + 32);
|
|
864
|
+
const noteHashes = reader.readVectorUint8Prefix(Fr);
|
|
865
|
+
const nullifiers = reader.readVectorUint8Prefix(Fr);
|
|
866
|
+
return [
|
|
867
|
+
noteHashes,
|
|
868
|
+
nullifiers
|
|
869
|
+
];
|
|
870
|
+
}));
|
|
871
|
+
}
|
|
872
|
+
/**
|
|
644
873
|
* Looks up which block deployed a particular contract.
|
|
645
874
|
* @param contractAddress - The address of the contract to look up.
|
|
646
875
|
* @returns The block number and index of the contract.
|
|
@@ -656,7 +885,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
656
885
|
if (!checkpoint) {
|
|
657
886
|
return BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
658
887
|
}
|
|
659
|
-
return BlockNumber(checkpoint.startBlock + checkpoint.
|
|
888
|
+
return BlockNumber(checkpoint.startBlock + checkpoint.blockCount - 1);
|
|
660
889
|
}
|
|
661
890
|
async getLatestL2BlockNumber() {
|
|
662
891
|
const [lastBlockNumber] = await toArray(this.#blocks.keysAsync({
|
|
@@ -666,6 +895,152 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
666
895
|
return typeof lastBlockNumber === 'number' ? BlockNumber(lastBlockNumber) : BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
667
896
|
}
|
|
668
897
|
/**
|
|
898
|
+
* Resolves all five L2 chain tips (proposed, proposedCheckpoint, checkpointed, proven, finalized)
|
|
899
|
+
* in a single read-only transaction so the snapshot is internally consistent. Each underlying
|
|
900
|
+
* record is read at most once: latest block, latest confirmed checkpoint, and latest pending
|
|
901
|
+
* checkpoint are each loaded directly (no separate "find the number, then look up data" hop),
|
|
902
|
+
* the proven/finalized checkpoint singletons are read once and their storage entries are
|
|
903
|
+
* reused if they coincide with the latest checkpoint, and per-tip block hashes are deduped
|
|
904
|
+
* when two tips land on the same block (e.g. finalized == proven, or proposedCheckpoint falls
|
|
905
|
+
* back to checkpointed when no pending checkpoint exists).
|
|
906
|
+
*
|
|
907
|
+
* The result is guaranteed to satisfy `finalized <= proven <= checkpointed <= proposed` (by
|
|
908
|
+
* block number). Genesis is represented by `(INITIAL_L2_BLOCK_NUM - 1)` and the supplied
|
|
909
|
+
* `genesisBlockHash`, paired with the synthetic genesis checkpoint id.
|
|
910
|
+
*
|
|
911
|
+
* @param genesisBlockHash - Block hash to report for the synthetic pre-initial block (used when
|
|
912
|
+
* a tip is still at genesis).
|
|
913
|
+
*/ async getL2TipsData(genesisBlockHash) {
|
|
914
|
+
return await this.db.transactionAsync(async ()=>{
|
|
915
|
+
// Define genesis tips
|
|
916
|
+
const genesisBlockNumber = BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
917
|
+
const genesisCheckpointNumber = CheckpointNumber(INITIAL_CHECKPOINT_NUMBER - 1);
|
|
918
|
+
const genesisBlockId = {
|
|
919
|
+
number: genesisBlockNumber,
|
|
920
|
+
hash: genesisBlockHash.toString()
|
|
921
|
+
};
|
|
922
|
+
const genesisCheckpointId = {
|
|
923
|
+
number: genesisCheckpointNumber,
|
|
924
|
+
hash: GENESIS_CHECKPOINT_HEADER_HASH.toString()
|
|
925
|
+
};
|
|
926
|
+
const genesisTip = {
|
|
927
|
+
block: genesisBlockId,
|
|
928
|
+
checkpoint: genesisCheckpointId
|
|
929
|
+
};
|
|
930
|
+
// Load latest block and checkpoint entries
|
|
931
|
+
const [latestBlockEntry] = await toArray(this.#blocks.entriesAsync({
|
|
932
|
+
reverse: true,
|
|
933
|
+
limit: 1
|
|
934
|
+
}));
|
|
935
|
+
const [proposedCheckpointEntry] = await toArray(this.#proposedCheckpoints.entriesAsync({
|
|
936
|
+
reverse: true,
|
|
937
|
+
limit: 1
|
|
938
|
+
}));
|
|
939
|
+
const [latestCheckpointEntry] = await toArray(this.#checkpoints.entriesAsync({
|
|
940
|
+
reverse: true,
|
|
941
|
+
limit: 1
|
|
942
|
+
}));
|
|
943
|
+
const latestCheckpointNumber = latestCheckpointEntry ? CheckpointNumber(latestCheckpointEntry[0]) : genesisCheckpointNumber;
|
|
944
|
+
// Load proven and finalized checkpoint number pointers
|
|
945
|
+
const [provenRaw, finalizedRaw] = await Promise.all([
|
|
946
|
+
this.#lastProvenCheckpoint.getAsync(),
|
|
947
|
+
this.#lastFinalizedCheckpoint.getAsync()
|
|
948
|
+
]);
|
|
949
|
+
// Clamp to enforce finalized <= proven <= checkpointed.
|
|
950
|
+
const provenCheckpointNumber = CheckpointNumber(Math.min(provenRaw ?? 0, latestCheckpointNumber));
|
|
951
|
+
const finalizedCheckpointNumber = CheckpointNumber(Math.min(finalizedRaw ?? 0, provenCheckpointNumber));
|
|
952
|
+
// Avoid loading the same checkpoint more than once
|
|
953
|
+
const checkpointStorageCache = new Map();
|
|
954
|
+
if (latestCheckpointEntry) {
|
|
955
|
+
checkpointStorageCache.set(CheckpointNumber(latestCheckpointEntry[0]), latestCheckpointEntry[1]);
|
|
956
|
+
}
|
|
957
|
+
const loadCheckpointStorage = async (n)=>{
|
|
958
|
+
if (n === 0) {
|
|
959
|
+
return undefined;
|
|
960
|
+
}
|
|
961
|
+
if (!checkpointStorageCache.has(n)) {
|
|
962
|
+
const checkpointStorage = await this.#checkpoints.getAsync(n);
|
|
963
|
+
if (!checkpointStorage) {
|
|
964
|
+
throw new CheckpointNotFoundError(n);
|
|
965
|
+
}
|
|
966
|
+
checkpointStorageCache.set(n, checkpointStorage);
|
|
967
|
+
}
|
|
968
|
+
return checkpointStorageCache.get(n);
|
|
969
|
+
};
|
|
970
|
+
// Load proven and finalized checkpoint storage entries
|
|
971
|
+
const provenCheckpoint = await loadCheckpointStorage(provenCheckpointNumber);
|
|
972
|
+
const finalizedCheckpoint = await loadCheckpointStorage(finalizedCheckpointNumber);
|
|
973
|
+
// Avoid loading the same block hash multiple times when tips land on the same block
|
|
974
|
+
const blockHashCache = new Map();
|
|
975
|
+
blockHashCache.set(genesisBlockNumber, genesisBlockHash.toString());
|
|
976
|
+
if (latestBlockEntry) {
|
|
977
|
+
blockHashCache.set(latestBlockEntry[0], BlockHash.fromBuffer(latestBlockEntry[1].blockHash).toString());
|
|
978
|
+
}
|
|
979
|
+
const loadBlockHash = async (n)=>{
|
|
980
|
+
if (!blockHashCache.has(n)) {
|
|
981
|
+
const blockStorage = await this.#blocks.getAsync(n);
|
|
982
|
+
if (!blockStorage) {
|
|
983
|
+
throw new BlockNotFoundError(n);
|
|
984
|
+
}
|
|
985
|
+
const blockHash = BlockHash.fromBuffer(blockStorage.blockHash).toString();
|
|
986
|
+
blockHashCache.set(n, blockHash);
|
|
987
|
+
}
|
|
988
|
+
return blockHashCache.get(n);
|
|
989
|
+
};
|
|
990
|
+
// Build proposed chain tip (this one has block only, no checkpoint)
|
|
991
|
+
const proposedBlockId = latestBlockEntry === undefined ? genesisBlockId : {
|
|
992
|
+
number: BlockNumber(latestBlockEntry[0]),
|
|
993
|
+
hash: BlockHash.fromBuffer(latestBlockEntry[1].blockHash).toString()
|
|
994
|
+
};
|
|
995
|
+
// Build other tips from checkpoint data, reading corresponding block data from the cache
|
|
996
|
+
const buildTipFromCheckpoint = async (stored)=>{
|
|
997
|
+
if (!stored) {
|
|
998
|
+
return genesisTip;
|
|
999
|
+
}
|
|
1000
|
+
const blockNumber = BlockNumber(stored.startBlock + stored.blockCount - 1);
|
|
1001
|
+
const blockHash = await loadBlockHash(blockNumber);
|
|
1002
|
+
const header = CheckpointHeader.fromBuffer(stored.header);
|
|
1003
|
+
return {
|
|
1004
|
+
block: {
|
|
1005
|
+
number: blockNumber,
|
|
1006
|
+
hash: blockHash
|
|
1007
|
+
},
|
|
1008
|
+
checkpoint: {
|
|
1009
|
+
number: CheckpointNumber(stored.checkpointNumber),
|
|
1010
|
+
hash: header.hash().toString()
|
|
1011
|
+
}
|
|
1012
|
+
};
|
|
1013
|
+
};
|
|
1014
|
+
const checkpointedTip = await buildTipFromCheckpoint(latestCheckpointEntry?.[1]);
|
|
1015
|
+
const provenTip = await buildTipFromCheckpoint(provenCheckpoint);
|
|
1016
|
+
const finalizedTip = await buildTipFromCheckpoint(finalizedCheckpoint);
|
|
1017
|
+
// Proposed checkpoint falls back to the checkpoint tip if it's not set. And if local storage is
|
|
1018
|
+
// inconsistent and the proposed checkpoint is behind the checkpointed tip, we patch that and
|
|
1019
|
+
// report the checkpointed tip as the proposed checkpoint to maintain the invariant.
|
|
1020
|
+
const proposedCheckpointTip = proposedCheckpointEntry === undefined || proposedCheckpointEntry[0] <= latestCheckpointNumber ? checkpointedTip : await buildTipFromCheckpoint(proposedCheckpointEntry[1]);
|
|
1021
|
+
// A checkpointed block past the latest stored block would mean a checkpoint
|
|
1022
|
+
// references blocks that aren't in blocks.
|
|
1023
|
+
if (proposedBlockId.number < checkpointedTip.block.number) {
|
|
1024
|
+
throw new Error(`Inconsistent block store: latest block ${proposedBlockId.number} is behind checkpointed block ${checkpointedTip.block.number}`);
|
|
1025
|
+
}
|
|
1026
|
+
// Assert that checkpoint numbers are increasing
|
|
1027
|
+
if (finalizedTip.checkpoint.number > provenTip.checkpoint.number || provenTip.checkpoint.number > checkpointedTip.checkpoint.number || checkpointedTip.checkpoint.number > proposedCheckpointTip.checkpoint.number) {
|
|
1028
|
+
throw new Error(`Inconsistent checkpoint numbers in chain tips: finalized=${finalizedTip.checkpoint.number} proven=${provenTip.checkpoint.number} checkpointed=${checkpointedTip.checkpoint.number} proposed=${proposedCheckpointTip.checkpoint.number}`);
|
|
1029
|
+
}
|
|
1030
|
+
// Assert block numbers are increasing
|
|
1031
|
+
if (finalizedTip.block.number > provenTip.block.number || provenTip.block.number > checkpointedTip.block.number || checkpointedTip.block.number > proposedCheckpointTip.block.number || proposedCheckpointTip.block.number > proposedBlockId.number) {
|
|
1032
|
+
throw new Error(`Inconsistent block numbers in chain tips: finalized=${finalizedTip.block.number} proven=${provenTip.block.number} checkpointed=${checkpointedTip.block.number} proposedCheckpoint=${proposedCheckpointTip.block.number} proposed=${proposedBlockId.number}`);
|
|
1033
|
+
}
|
|
1034
|
+
return {
|
|
1035
|
+
proposed: proposedBlockId,
|
|
1036
|
+
proposedCheckpoint: proposedCheckpointTip,
|
|
1037
|
+
checkpointed: checkpointedTip,
|
|
1038
|
+
proven: provenTip,
|
|
1039
|
+
finalized: finalizedTip
|
|
1040
|
+
};
|
|
1041
|
+
});
|
|
1042
|
+
}
|
|
1043
|
+
/**
|
|
669
1044
|
* Gets the most recent L1 block processed.
|
|
670
1045
|
* @returns The L1 block that published the latest L2 block
|
|
671
1046
|
*/ getSynchedL1BlockNumber() {
|
|
@@ -674,17 +1049,74 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
674
1049
|
setSynchedL1BlockNumber(l1BlockNumber) {
|
|
675
1050
|
return this.#lastSynchedL1Block.set(l1BlockNumber);
|
|
676
1051
|
}
|
|
1052
|
+
/**
|
|
1053
|
+
* Adds a proposed checkpoint to the pending queue.
|
|
1054
|
+
* Accepts proposed.checkpointNumber === latestTip + 1, where latestTip is the highest of
|
|
1055
|
+
* confirmed and the highest pending checkpoint number.
|
|
1056
|
+
* Computes archive and checkpointOutHash from the stored blocks.
|
|
1057
|
+
*/ async addProposedCheckpoint(proposed) {
|
|
1058
|
+
return await this.db.transactionAsync(async ()=>{
|
|
1059
|
+
const confirmed = await this.getLatestCheckpointNumber();
|
|
1060
|
+
const [latestPendingKey] = await toArray(this.#proposedCheckpoints.keysAsync({
|
|
1061
|
+
reverse: true,
|
|
1062
|
+
limit: 1
|
|
1063
|
+
}));
|
|
1064
|
+
const latestTip = CheckpointNumber(latestPendingKey !== undefined ? Math.max(latestPendingKey, confirmed) : confirmed);
|
|
1065
|
+
if (proposed.checkpointNumber !== latestTip + 1) {
|
|
1066
|
+
throw new ProposedCheckpointNotSequentialError(proposed.checkpointNumber, latestTip);
|
|
1067
|
+
}
|
|
1068
|
+
// Ensure the predecessor block (from pending or confirmed chain) exists
|
|
1069
|
+
const previousBlock = await this.getPreviousCheckpointBlock(proposed.checkpointNumber);
|
|
1070
|
+
const blocks = [];
|
|
1071
|
+
for(let i = 0; i < proposed.blockCount; i++){
|
|
1072
|
+
const block = await this.getBlock({
|
|
1073
|
+
number: BlockNumber(proposed.startBlock + i)
|
|
1074
|
+
});
|
|
1075
|
+
if (!block) {
|
|
1076
|
+
throw new BlockNotFoundError(proposed.startBlock + i);
|
|
1077
|
+
}
|
|
1078
|
+
blocks.push(block);
|
|
1079
|
+
}
|
|
1080
|
+
this.validateCheckpointBlocks(blocks, previousBlock);
|
|
1081
|
+
const archive = blocks[blocks.length - 1].archive;
|
|
1082
|
+
const checkpointOutHash = Checkpoint.getCheckpointOutHash(blocks);
|
|
1083
|
+
await this.#proposedCheckpoints.set(proposed.checkpointNumber, {
|
|
1084
|
+
header: proposed.header.toBuffer(),
|
|
1085
|
+
archive: archive.toBuffer(),
|
|
1086
|
+
checkpointOutHash: checkpointOutHash.toBuffer(),
|
|
1087
|
+
checkpointNumber: proposed.checkpointNumber,
|
|
1088
|
+
startBlock: proposed.startBlock,
|
|
1089
|
+
blockCount: proposed.blockCount,
|
|
1090
|
+
totalManaUsed: proposed.totalManaUsed.toString(),
|
|
1091
|
+
feeAssetPriceModifier: proposed.feeAssetPriceModifier.toString()
|
|
1092
|
+
});
|
|
1093
|
+
});
|
|
1094
|
+
}
|
|
677
1095
|
async getProvenCheckpointNumber() {
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
1096
|
+
return await this.db.transactionAsync(async ()=>{
|
|
1097
|
+
const [latestCheckpointNumber, provenCheckpointNumber] = await Promise.all([
|
|
1098
|
+
this.getLatestCheckpointNumber(),
|
|
1099
|
+
this.#lastProvenCheckpoint.getAsync()
|
|
1100
|
+
]);
|
|
1101
|
+
return (provenCheckpointNumber ?? 0) > latestCheckpointNumber ? latestCheckpointNumber : CheckpointNumber(provenCheckpointNumber ?? 0);
|
|
1102
|
+
});
|
|
683
1103
|
}
|
|
684
1104
|
async setProvenCheckpointNumber(checkpointNumber) {
|
|
685
1105
|
const result = await this.#lastProvenCheckpoint.set(checkpointNumber);
|
|
686
1106
|
return result;
|
|
687
1107
|
}
|
|
1108
|
+
async getFinalizedCheckpointNumber() {
|
|
1109
|
+
return await this.db.transactionAsync(async ()=>{
|
|
1110
|
+
const [provenCheckpointNumber, finalizedCheckpointNumber] = await Promise.all([
|
|
1111
|
+
this.getProvenCheckpointNumber(),
|
|
1112
|
+
this.#lastFinalizedCheckpoint.getAsync()
|
|
1113
|
+
]);
|
|
1114
|
+
return (finalizedCheckpointNumber ?? 0) > provenCheckpointNumber ? provenCheckpointNumber : CheckpointNumber(finalizedCheckpointNumber ?? 0);
|
|
1115
|
+
});
|
|
1116
|
+
}
|
|
1117
|
+
setFinalizedCheckpointNumber(checkpointNumber) {
|
|
1118
|
+
return this.#lastFinalizedCheckpoint.set(checkpointNumber);
|
|
1119
|
+
}
|
|
688
1120
|
#computeBlockRange(start, limit) {
|
|
689
1121
|
if (limit < 1) {
|
|
690
1122
|
throw new Error(`Invalid limit: ${limit}`);
|
|
@@ -718,4 +1150,70 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
718
1150
|
await this.#pendingChainValidationStatus.delete();
|
|
719
1151
|
}
|
|
720
1152
|
}
|
|
1153
|
+
/** Records a rejected-checkpoint entry, keyed by its own archive root. */ async addRejectedCheckpoint(entry) {
|
|
1154
|
+
const archiveRootHex = entry.archiveRoot.toString();
|
|
1155
|
+
await this.#rejectedCheckpoints.set(archiveRootHex, {
|
|
1156
|
+
checkpointNumber: entry.checkpointNumber,
|
|
1157
|
+
archiveRoot: entry.archiveRoot.toBuffer(),
|
|
1158
|
+
parentArchiveRoot: entry.parentArchiveRoot.toBuffer(),
|
|
1159
|
+
slotNumber: entry.slotNumber,
|
|
1160
|
+
l1: entry.l1.toBuffer(),
|
|
1161
|
+
reason: entry.reason
|
|
1162
|
+
});
|
|
1163
|
+
await this.#rejectedCheckpointsByNumber.set(entry.checkpointNumber, archiveRootHex);
|
|
1164
|
+
await this.advanceSynchedL1BlockNumber(entry.l1.blockNumber);
|
|
1165
|
+
}
|
|
1166
|
+
/** Returns the rejected-checkpoint entry with the given archive root, or undefined if not present. */ async getRejectedCheckpointByArchiveRoot(archiveRoot) {
|
|
1167
|
+
const stored = await this.#rejectedCheckpoints.getAsync(archiveRoot.toString());
|
|
1168
|
+
return stored ? this.rejectedCheckpointFromStorage(stored) : undefined;
|
|
1169
|
+
}
|
|
1170
|
+
/** Returns the rejected-checkpoint entry recorded for the given checkpoint number, or undefined if none. */ async getRejectedCheckpointByNumber(checkpointNumber) {
|
|
1171
|
+
const archiveRootHex = await this.#rejectedCheckpointsByNumber.getAsync(checkpointNumber);
|
|
1172
|
+
if (archiveRootHex === undefined) {
|
|
1173
|
+
return undefined;
|
|
1174
|
+
}
|
|
1175
|
+
const stored = await this.#rejectedCheckpoints.getAsync(archiveRootHex);
|
|
1176
|
+
return stored ? this.rejectedCheckpointFromStorage(stored) : undefined;
|
|
1177
|
+
}
|
|
1178
|
+
/** Returns the highest checkpoint number recorded across all rejected entries, or `INITIAL_CHECKPOINT_NUMBER - 1` if none. */ async getLatestRejectedCheckpointNumber() {
|
|
1179
|
+
const [latest] = await toArray(this.#rejectedCheckpointsByNumber.keysAsync({
|
|
1180
|
+
reverse: true,
|
|
1181
|
+
limit: 1
|
|
1182
|
+
}));
|
|
1183
|
+
return CheckpointNumber(latest ?? INITIAL_CHECKPOINT_NUMBER - 1);
|
|
1184
|
+
}
|
|
1185
|
+
/** Removes a rejected-checkpoint entry by its archive root (used when an entry no longer matches L1). */ async removeRejectedCheckpointByArchiveRoot(archiveRoot) {
|
|
1186
|
+
const archiveRootHex = archiveRoot.toString();
|
|
1187
|
+
const stored = await this.#rejectedCheckpoints.getAsync(archiveRootHex);
|
|
1188
|
+
await this.#rejectedCheckpoints.delete(archiveRootHex);
|
|
1189
|
+
if (stored) {
|
|
1190
|
+
// Only clear the by-number index if it still points at this archive root, so a distinct
|
|
1191
|
+
// entry that shares the checkpoint number (e.g. an L1 reorg replacement) is not dropped.
|
|
1192
|
+
const indexed = await this.#rejectedCheckpointsByNumber.getAsync(stored.checkpointNumber);
|
|
1193
|
+
if (indexed === archiveRootHex) {
|
|
1194
|
+
await this.#rejectedCheckpointsByNumber.delete(stored.checkpointNumber);
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
/**
|
|
1199
|
+
* Advances the stored last-synched L1 block number to `l1BlockNumber` only if it is strictly
|
|
1200
|
+
* greater than the current value. Use this whenever ingesting checkpoint-shaped data so the
|
|
1201
|
+
* sync pointer never walks backwards on out-of-order writes (e.g. an invalid checkpoint
|
|
1202
|
+
* advance followed by a valid-checkpoint commit landing at an earlier L1 block).
|
|
1203
|
+
*/ async advanceSynchedL1BlockNumber(l1BlockNumber) {
|
|
1204
|
+
const current = await this.#lastSynchedL1Block.getAsync();
|
|
1205
|
+
if (current === undefined || l1BlockNumber > current) {
|
|
1206
|
+
await this.#lastSynchedL1Block.set(l1BlockNumber);
|
|
1207
|
+
}
|
|
1208
|
+
}
|
|
1209
|
+
rejectedCheckpointFromStorage(stored) {
|
|
1210
|
+
return {
|
|
1211
|
+
checkpointNumber: CheckpointNumber(stored.checkpointNumber),
|
|
1212
|
+
archiveRoot: Fr.fromBuffer(stored.archiveRoot),
|
|
1213
|
+
parentArchiveRoot: Fr.fromBuffer(stored.parentArchiveRoot),
|
|
1214
|
+
slotNumber: SlotNumber(stored.slotNumber),
|
|
1215
|
+
l1: L1PublishedData.fromBuffer(stored.l1),
|
|
1216
|
+
reason: stored.reason
|
|
1217
|
+
};
|
|
1218
|
+
}
|
|
721
1219
|
}
|