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