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