@aztec/archiver 5.0.0-private.20260319 → 5.0.0-rc.2
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 -13
- package/dest/archiver.d.ts +71 -15
- package/dest/archiver.d.ts.map +1 -1
- package/dest/archiver.js +268 -74
- package/dest/config.d.ts +7 -3
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +39 -16
- package/dest/errors.d.ts +44 -10
- package/dest/errors.d.ts.map +1 -1
- package/dest/errors.js +66 -12
- package/dest/factory.d.ts +12 -7
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +40 -25
- 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 +2 -1
- package/dest/l1/calldata_retriever.d.ts.map +1 -1
- package/dest/l1/calldata_retriever.js +15 -5
- package/dest/l1/data_retrieval.d.ts +19 -10
- 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 +72 -48
- package/dest/modules/data_source_base.d.ts.map +1 -1
- package/dest/modules/data_source_base.js +266 -143
- 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 +172 -117
- package/dest/modules/instrumentation.d.ts +7 -2
- package/dest/modules/instrumentation.d.ts.map +1 -1
- package/dest/modules/instrumentation.js +25 -7
- package/dest/modules/l1_synchronizer.d.ts +11 -6
- package/dest/modules/l1_synchronizer.d.ts.map +1 -1
- package/dest/modules/l1_synchronizer.js +437 -217
- 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 +24 -5
- package/dest/modules/validation.d.ts.map +1 -1
- package/dest/modules/validation.js +40 -15
- package/dest/store/block_store.d.ts +164 -66
- package/dest/store/block_store.d.ts.map +1 -1
- package/dest/store/block_store.js +638 -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 +36 -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 +48 -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 -40
- package/dest/store/log_store.d.ts.map +1 -1
- package/dest/store/log_store.js +257 -480
- 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 +9 -3
- package/dest/store/message_store.d.ts.map +1 -1
- package/dest/store/message_store.js +39 -10
- 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 +80 -18
- 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 +223 -179
- 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 +25 -9
- package/package.json +14 -14
- package/src/archiver.ts +353 -80
- package/src/config.ts +43 -20
- package/src/errors.ts +103 -18
- package/src/factory.ts +56 -24
- package/src/index.ts +18 -2
- package/src/l1/calldata_retriever.ts +16 -5
- package/src/l1/data_retrieval.ts +38 -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 +340 -189
- package/src/modules/data_store_updater.ts +211 -148
- package/src/modules/instrumentation.ts +28 -8
- package/src/modules/l1_synchronizer.ts +583 -255
- package/src/modules/outbox_trees_resolver.ts +199 -0
- package/src/modules/validation.ts +81 -21
- package/src/store/block_store.ts +792 -285
- package/src/store/contract_class_store.ts +43 -103
- package/src/store/contract_instance_store.ts +62 -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 +296 -652
- package/src/store/log_store_codec.ts +143 -0
- package/src/store/message_store.ts +43 -12
- package/src/structs/inbox_message.ts +1 -1
- package/src/test/fake_l1_state.ts +99 -27
- package/src/test/mock_l2_block_source.ts +279 -233
- package/src/test/mock_structs.ts +5 -2
- package/src/test/noop_l1_archiver.ts +52 -9
- package/dest/store/kv_archiver_store.d.ts +0 -367
- package/dest/store/kv_archiver_store.d.ts.map +0 -1
- package/dest/store/kv_archiver_store.js +0 -481
- package/src/store/kv_archiver_store.ts +0 -697
|
@@ -1,25 +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';
|
|
11
|
-
import { getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
|
|
9
|
+
import { BlockHash, Body, CommitteeAttestation, GENESIS_CHECKPOINT_HEADER_HASH, L2Block, deserializeValidateCheckpointResult, serializeValidateCheckpointResult } from '@aztec/stdlib/block';
|
|
10
|
+
import { Checkpoint, L1PublishedData } from '@aztec/stdlib/checkpoint';
|
|
12
11
|
import { CheckpointHeader } from '@aztec/stdlib/rollup';
|
|
13
12
|
import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
|
|
14
|
-
import { BlockHeader, TxHash,
|
|
15
|
-
import { BlockAlreadyCheckpointedError, BlockArchiveNotConsistentError, BlockIndexNotSequentialError, BlockNotFoundError, BlockNumberNotSequentialError, CannotOverwriteCheckpointedBlockError, CheckpointNotFoundError, CheckpointNumberNotSequentialError, InitialCheckpointNumberNotSequentialError } from '../errors.js';
|
|
16
|
-
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';
|
|
17
15
|
/**
|
|
18
16
|
* LMDB-based block storage for the archiver.
|
|
19
17
|
*/ export class BlockStore {
|
|
20
18
|
db;
|
|
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;
|
|
23
22
|
/** Map slot number to checkpoint number, for looking up checkpoints by slot range. */ #slotToCheckpoint;
|
|
24
23
|
/** Map block hash to list of tx hashes */ #blockTxs;
|
|
25
24
|
/** Tx hash to serialized IndexedTxEffect */ #txEffects;
|
|
@@ -30,6 +29,8 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
30
29
|
/** Index mapping a contract's address (as a string) to its location in a block */ #contractIndex;
|
|
31
30
|
/** Index mapping block hash to block number */ #blockHashIndex;
|
|
32
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;
|
|
33
34
|
#log;
|
|
34
35
|
constructor(db){
|
|
35
36
|
this.db = db;
|
|
@@ -46,6 +47,9 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
46
47
|
this.#pendingChainValidationStatus = db.openSingleton('archiver_pending_chain_validation_status');
|
|
47
48
|
this.#checkpoints = db.openMap('archiver_checkpoints');
|
|
48
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');
|
|
49
53
|
}
|
|
50
54
|
/**
|
|
51
55
|
* Returns the finalized L2 block number. An L2 block is finalized when it was proven
|
|
@@ -75,13 +79,15 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
75
79
|
const blockIndex = block.indexWithinCheckpoint;
|
|
76
80
|
const blockLastArchive = block.header.lastArchive.root;
|
|
77
81
|
// Extract the latest block and checkpoint numbers
|
|
78
|
-
const previousBlockNumber = await this.
|
|
79
|
-
const
|
|
82
|
+
const previousBlockNumber = await this.getLatestL2BlockNumber();
|
|
83
|
+
const latestCheckpointNumber = await this.getLatestCheckpointNumber();
|
|
80
84
|
// Verify we're not overwriting checkpointed blocks
|
|
81
85
|
const lastCheckpointedBlockNumber = await this.getCheckpointedL2BlockNumber();
|
|
82
86
|
if (!opts.force && blockNumber <= lastCheckpointedBlockNumber) {
|
|
83
87
|
// Check if the proposed block matches the already-checkpointed one
|
|
84
|
-
const existingBlock = await this.
|
|
88
|
+
const existingBlock = await this.getBlockData({
|
|
89
|
+
number: BlockNumber(blockNumber)
|
|
90
|
+
});
|
|
85
91
|
if (existingBlock && existingBlock.archive.root.equals(block.archive.root)) {
|
|
86
92
|
throw new BlockAlreadyCheckpointedError(blockNumber);
|
|
87
93
|
}
|
|
@@ -91,12 +97,22 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
91
97
|
if (!opts.force && previousBlockNumber !== blockNumber - 1) {
|
|
92
98
|
throw new BlockNumberNotSequentialError(blockNumber, previousBlockNumber);
|
|
93
99
|
}
|
|
94
|
-
//
|
|
95
|
-
|
|
96
|
-
|
|
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);
|
|
97
111
|
}
|
|
98
112
|
// Extract the previous block if there is one and see if it is for the same checkpoint or not
|
|
99
|
-
const previousBlockResult = await this.
|
|
113
|
+
const previousBlockResult = await this.getBlockData({
|
|
114
|
+
number: previousBlockNumber
|
|
115
|
+
});
|
|
100
116
|
let expectedBlockIndex = 0;
|
|
101
117
|
let previousBlockIndex = undefined;
|
|
102
118
|
if (previousBlockResult !== undefined) {
|
|
@@ -106,7 +122,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
106
122
|
expectedBlockIndex = previousBlockIndex + 1;
|
|
107
123
|
}
|
|
108
124
|
if (!previousBlockResult.archive.root.equals(blockLastArchive)) {
|
|
109
|
-
throw new BlockArchiveNotConsistentError(blockNumber, previousBlockResult.
|
|
125
|
+
throw new BlockArchiveNotConsistentError(blockNumber, previousBlockResult.header.globalVariables.blockNumber, blockLastArchive, previousBlockResult.archive.root);
|
|
110
126
|
}
|
|
111
127
|
}
|
|
112
128
|
// Now check that the block has the expected index value
|
|
@@ -118,7 +134,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
118
134
|
});
|
|
119
135
|
}
|
|
120
136
|
/**
|
|
121
|
-
* Append new
|
|
137
|
+
* Append new checkpoints to the store's list.
|
|
122
138
|
* @param checkpoints - The L2 checkpoints to be added to the store.
|
|
123
139
|
* @returns True if the operation is successful.
|
|
124
140
|
*/ async addCheckpoints(checkpoints, opts = {}) {
|
|
@@ -126,32 +142,26 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
126
142
|
return true;
|
|
127
143
|
}
|
|
128
144
|
return await this.db.transactionAsync(async ()=>{
|
|
129
|
-
// Check that the checkpoint immediately before the first block to be added is present in the store.
|
|
130
145
|
const firstCheckpointNumber = checkpoints[0].checkpoint.number;
|
|
131
146
|
const previousCheckpointNumber = await this.getLatestCheckpointNumber();
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
previousCheckpointData = await this.getCheckpointData(previousCheckpointNumber);
|
|
140
|
-
if (previousCheckpointData === undefined) {
|
|
141
|
-
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;
|
|
142
154
|
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
if (previousCheckpointData !== undefined) {
|
|
148
|
-
previousBlockNumber = BlockNumber(previousCheckpointData.startBlock + previousCheckpointData.blockCount - 1);
|
|
149
|
-
previousBlock = await this.getBlock(previousBlockNumber);
|
|
150
|
-
if (previousBlock === undefined) {
|
|
151
|
-
// We should be able to get the required previous block
|
|
152
|
-
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);
|
|
153
159
|
}
|
|
160
|
+
} else if (previousCheckpointNumber !== firstCheckpointNumber - 1 && !opts.force) {
|
|
161
|
+
throw new InitialCheckpointNumberNotSequentialError(firstCheckpointNumber, previousCheckpointNumber);
|
|
154
162
|
}
|
|
163
|
+
// Get the last block of the previous checkpoint for archive chaining
|
|
164
|
+
let previousBlock = await this.getPreviousCheckpointBlock(checkpoints[0].checkpoint.number);
|
|
155
165
|
// Iterate over checkpoints array and insert them, checking that the block numbers are sequential.
|
|
156
166
|
let previousCheckpoint = undefined;
|
|
157
167
|
for (const checkpoint of checkpoints){
|
|
@@ -159,33 +169,13 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
159
169
|
throw new CheckpointNumberNotSequentialError(checkpoint.checkpoint.number, previousCheckpoint.checkpoint.number);
|
|
160
170
|
}
|
|
161
171
|
previousCheckpoint = checkpoint;
|
|
162
|
-
//
|
|
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)
|
|
163
175
|
for(let i = 0; i < checkpoint.checkpoint.blocks.length; i++){
|
|
164
|
-
|
|
165
|
-
if (previousBlock) {
|
|
166
|
-
// The blocks should have a sequential block number
|
|
167
|
-
if (previousBlock.number !== block.number - 1) {
|
|
168
|
-
throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
|
|
169
|
-
}
|
|
170
|
-
// If the blocks are for the same checkpoint then they should have sequential indexes
|
|
171
|
-
if (previousBlock.checkpointNumber === block.checkpointNumber && previousBlock.indexWithinCheckpoint !== block.indexWithinCheckpoint - 1) {
|
|
172
|
-
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
|
|
173
|
-
}
|
|
174
|
-
if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
|
|
175
|
-
throw new BlockArchiveNotConsistentError(block.number, previousBlock.number, block.header.lastArchive.root, previousBlock.archive.root);
|
|
176
|
-
}
|
|
177
|
-
} else {
|
|
178
|
-
// No previous block, must be block 1 at checkpoint index 0
|
|
179
|
-
if (block.indexWithinCheckpoint !== 0) {
|
|
180
|
-
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, undefined);
|
|
181
|
-
}
|
|
182
|
-
if (block.number !== INITIAL_L2_BLOCK_NUM) {
|
|
183
|
-
throw new BlockNumberNotSequentialError(block.number, undefined);
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
previousBlock = block;
|
|
187
|
-
await this.addBlockToDatabase(block, checkpoint.checkpoint.number, i);
|
|
176
|
+
await this.addBlockToDatabase(checkpoint.checkpoint.blocks[i], checkpoint.checkpoint.number, i);
|
|
188
177
|
}
|
|
178
|
+
previousBlock = checkpoint.checkpoint.blocks.at(-1);
|
|
189
179
|
// Store the checkpoint in the database
|
|
190
180
|
await this.#checkpoints.set(checkpoint.checkpoint.number, {
|
|
191
181
|
header: checkpoint.checkpoint.header.toBuffer(),
|
|
@@ -195,15 +185,107 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
195
185
|
attestations: checkpoint.attestations.map((attestation)=>attestation.toBuffer()),
|
|
196
186
|
checkpointNumber: checkpoint.checkpoint.number,
|
|
197
187
|
startBlock: checkpoint.checkpoint.blocks[0].number,
|
|
198
|
-
blockCount: checkpoint.checkpoint.blocks.length
|
|
188
|
+
blockCount: checkpoint.checkpoint.blocks.length,
|
|
189
|
+
feeAssetPriceModifier: checkpoint.checkpoint.feeAssetPriceModifier.toString()
|
|
199
190
|
});
|
|
200
191
|
// Update slot-to-checkpoint index
|
|
201
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);
|
|
202
198
|
}
|
|
203
|
-
await this
|
|
199
|
+
await this.advanceSynchedL1BlockNumber(checkpoints[checkpoints.length - 1].l1.blockNumber);
|
|
204
200
|
return true;
|
|
205
201
|
});
|
|
206
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
|
+
}
|
|
207
289
|
async addBlockToDatabase(block, checkpointNumber, indexWithinCheckpoint) {
|
|
208
290
|
const blockHash = await block.hash();
|
|
209
291
|
await this.#blocks.set(block.number, {
|
|
@@ -218,7 +300,8 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
218
300
|
data: block.body.txEffects[i],
|
|
219
301
|
l2BlockNumber: block.number,
|
|
220
302
|
l2BlockHash: blockHash,
|
|
221
|
-
txIndexInBlock: i
|
|
303
|
+
txIndexInBlock: i,
|
|
304
|
+
slotNumber: block.header.globalVariables.slotNumber
|
|
222
305
|
};
|
|
223
306
|
await this.#txEffects.set(txEffect.data.txHash.toString(), serializeIndexedTxEffect(txEffect));
|
|
224
307
|
}
|
|
@@ -281,6 +364,8 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
281
364
|
await this.#checkpoints.delete(c);
|
|
282
365
|
this.#log.debug(`Removed checkpoint ${c}`);
|
|
283
366
|
}
|
|
367
|
+
// Evict all pending checkpoints > checkpointNumber (their base chain no longer exists)
|
|
368
|
+
await this.evictProposedCheckpointsFrom(CheckpointNumber(checkpointNumber + 1));
|
|
284
369
|
return {
|
|
285
370
|
blocksRemoved
|
|
286
371
|
};
|
|
@@ -317,6 +402,20 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
317
402
|
}
|
|
318
403
|
return result;
|
|
319
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
|
+
}
|
|
320
419
|
checkpointDataFromCheckpointStorage(checkpointStorage) {
|
|
321
420
|
return {
|
|
322
421
|
header: CheckpointHeader.fromBuffer(checkpointStorage.header),
|
|
@@ -325,6 +424,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
325
424
|
checkpointNumber: CheckpointNumber(checkpointStorage.checkpointNumber),
|
|
326
425
|
startBlock: BlockNumber(checkpointStorage.startBlock),
|
|
327
426
|
blockCount: checkpointStorage.blockCount,
|
|
427
|
+
feeAssetPriceModifier: BigInt(checkpointStorage.feeAssetPriceModifier),
|
|
328
428
|
l1: L1PublishedData.fromBuffer(checkpointStorage.l1),
|
|
329
429
|
attestations: checkpointStorage.attestations.map((buf)=>CommitteeAttestation.fromBuffer(buf))
|
|
330
430
|
};
|
|
@@ -373,10 +473,12 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
373
473
|
return await this.db.transactionAsync(async ()=>{
|
|
374
474
|
const removedBlocks = [];
|
|
375
475
|
// Get the latest block number to determine the range
|
|
376
|
-
const latestBlockNumber = await this.
|
|
476
|
+
const latestBlockNumber = await this.getLatestL2BlockNumber();
|
|
377
477
|
// Iterate from blockNumber + 1 to latestBlockNumber
|
|
378
478
|
for(let bn = blockNumber + 1; bn <= latestBlockNumber; bn++){
|
|
379
|
-
const block = await this.getBlock(
|
|
479
|
+
const block = await this.getBlock({
|
|
480
|
+
number: BlockNumber(bn)
|
|
481
|
+
});
|
|
380
482
|
if (block === undefined) {
|
|
381
483
|
this.#log.warn(`Cannot remove block ${bn} from the store since we don't have it`);
|
|
382
484
|
continue;
|
|
@@ -400,13 +502,6 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
400
502
|
return BlockNumber(checkpointStorage.startBlock + checkpointStorage.blockCount - 1);
|
|
401
503
|
}
|
|
402
504
|
}
|
|
403
|
-
async getLatestBlockNumber() {
|
|
404
|
-
const [latestBlocknumber] = await toArray(this.#blocks.keysAsync({
|
|
405
|
-
reverse: true,
|
|
406
|
-
limit: 1
|
|
407
|
-
}));
|
|
408
|
-
return typeof latestBlocknumber === 'number' ? BlockNumber(latestBlocknumber) : BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
409
|
-
}
|
|
410
505
|
async getLatestCheckpointNumber() {
|
|
411
506
|
const [latestCheckpointNumber] = await toArray(this.#checkpoints.keysAsync({
|
|
412
507
|
reverse: true,
|
|
@@ -417,143 +512,171 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
417
512
|
}
|
|
418
513
|
return CheckpointNumber(latestCheckpointNumber);
|
|
419
514
|
}
|
|
420
|
-
async
|
|
421
|
-
const
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
const block = await this.getBlockFromBlockStorage(number, blockStorage);
|
|
430
|
-
if (!block) {
|
|
431
|
-
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);
|
|
432
524
|
}
|
|
433
|
-
return new CheckpointedL2Block(CheckpointNumber(checkpoint.checkpointNumber), block, L1PublishedData.fromBuffer(checkpoint.l1), checkpoint.attestations.map((buf)=>CommitteeAttestation.fromBuffer(buf)));
|
|
434
525
|
}
|
|
435
526
|
/**
|
|
436
|
-
*
|
|
437
|
-
*
|
|
438
|
-
*
|
|
439
|
-
* @
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
const checkpointedBlock = new CheckpointedL2Block(CheckpointNumber(checkpoint.checkpointNumber), block, L1PublishedData.fromBuffer(checkpoint.l1), checkpoint.attestations.map((buf)=>CommitteeAttestation.fromBuffer(buf)));
|
|
449
|
-
yield checkpointedBlock;
|
|
450
|
-
}
|
|
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();
|
|
451
539
|
}
|
|
452
|
-
|
|
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
|
+
});
|
|
453
570
|
}
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
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) {
|
|
457
580
|
return undefined;
|
|
458
581
|
}
|
|
459
|
-
|
|
582
|
+
const stored = await this.#proposedCheckpoints.getAsync(key);
|
|
583
|
+
return stored ? this.convertToProposedCheckpointData(stored) : undefined;
|
|
460
584
|
}
|
|
461
|
-
async
|
|
462
|
-
const
|
|
463
|
-
|
|
464
|
-
return undefined;
|
|
465
|
-
}
|
|
466
|
-
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;
|
|
467
588
|
}
|
|
468
589
|
/**
|
|
469
|
-
*
|
|
470
|
-
*
|
|
471
|
-
*
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
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);
|
|
478
598
|
}
|
|
479
599
|
}
|
|
600
|
+
return undefined;
|
|
480
601
|
}
|
|
481
602
|
/**
|
|
482
|
-
*
|
|
483
|
-
*
|
|
484
|
-
*
|
|
485
|
-
*/ async
|
|
486
|
-
const
|
|
487
|
-
|
|
488
|
-
|
|
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
|
+
}
|
|
489
612
|
}
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
/**
|
|
493
|
-
* Gets block metadata (without tx data) by archive root.
|
|
494
|
-
* @param archive - The archive root of the block to return.
|
|
495
|
-
* @returns The requested block data.
|
|
496
|
-
*/ async getBlockDataByArchive(archive) {
|
|
497
|
-
const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
|
|
498
|
-
if (blockNumber === undefined) {
|
|
499
|
-
return undefined;
|
|
613
|
+
for (const key of keysToDelete){
|
|
614
|
+
await this.#proposedCheckpoints.delete(key);
|
|
500
615
|
}
|
|
501
|
-
return this.getBlockData(BlockNumber(blockNumber));
|
|
502
616
|
}
|
|
503
617
|
/**
|
|
504
|
-
* Gets
|
|
505
|
-
*
|
|
506
|
-
*
|
|
507
|
-
*/ async
|
|
508
|
-
const
|
|
509
|
-
if (!
|
|
510
|
-
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());
|
|
511
625
|
}
|
|
512
|
-
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
|
+
};
|
|
513
639
|
}
|
|
514
640
|
/**
|
|
515
|
-
*
|
|
516
|
-
* @
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
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();
|
|
522
647
|
}
|
|
523
|
-
return
|
|
648
|
+
return CheckpointNumber(proposed.checkpointNumber);
|
|
524
649
|
}
|
|
525
650
|
/**
|
|
526
|
-
*
|
|
527
|
-
* @
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
return undefined;
|
|
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();
|
|
533
657
|
}
|
|
534
|
-
return
|
|
658
|
+
return BlockNumber(proposed.startBlock + proposed.blockCount - 1);
|
|
535
659
|
}
|
|
536
|
-
/**
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
const blockNumber = await this
|
|
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);
|
|
542
666
|
if (blockNumber === undefined) {
|
|
543
667
|
return undefined;
|
|
544
668
|
}
|
|
545
669
|
const blockStorage = await this.#blocks.getAsync(blockNumber);
|
|
546
|
-
if (!blockStorage
|
|
670
|
+
if (!blockStorage) {
|
|
547
671
|
return undefined;
|
|
548
672
|
}
|
|
549
|
-
return
|
|
673
|
+
return this.getBlockFromBlockStorage(blockNumber, blockStorage);
|
|
550
674
|
}
|
|
551
|
-
/**
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
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);
|
|
557
680
|
if (blockNumber === undefined) {
|
|
558
681
|
return undefined;
|
|
559
682
|
}
|
|
@@ -561,20 +684,30 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
561
684
|
if (!blockStorage || !blockStorage.header) {
|
|
562
685
|
return undefined;
|
|
563
686
|
}
|
|
564
|
-
return
|
|
687
|
+
return this.getBlockDataFromBlockStorage(blockStorage);
|
|
565
688
|
}
|
|
566
|
-
/**
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
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;
|
|
701
|
+
}
|
|
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;
|
|
576
709
|
}
|
|
577
|
-
yield
|
|
710
|
+
yield this.getBlockDataFromBlockStorage(blockStorage);
|
|
578
711
|
}
|
|
579
712
|
}
|
|
580
713
|
async *getBlockStorages(start, limit) {
|
|
@@ -590,11 +723,27 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
590
723
|
];
|
|
591
724
|
}
|
|
592
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
|
+
}
|
|
593
742
|
getBlockDataFromBlockStorage(blockStorage) {
|
|
594
743
|
return {
|
|
595
744
|
header: BlockHeader.fromBuffer(blockStorage.header),
|
|
596
745
|
archive: AppendOnlyTreeSnapshot.fromBuffer(blockStorage.archive),
|
|
597
|
-
blockHash:
|
|
746
|
+
blockHash: BlockHash.fromBuffer(blockStorage.blockHash),
|
|
598
747
|
checkpointNumber: CheckpointNumber(blockStorage.checkpointNumber),
|
|
599
748
|
indexWithinCheckpoint: IndexWithinCheckpoint(blockStorage.indexWithinCheckpoint)
|
|
600
749
|
};
|
|
@@ -638,36 +787,6 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
638
787
|
return deserializeIndexedTxEffect(buffer);
|
|
639
788
|
}
|
|
640
789
|
/**
|
|
641
|
-
* Gets a receipt of a settled tx.
|
|
642
|
-
* @param txHash - The hash of a tx we try to get the receipt for.
|
|
643
|
-
* @returns The requested tx receipt (or undefined if not found).
|
|
644
|
-
*/ async getSettledTxReceipt(txHash, l1Constants) {
|
|
645
|
-
const txEffect = await this.getTxEffect(txHash);
|
|
646
|
-
if (!txEffect) {
|
|
647
|
-
return undefined;
|
|
648
|
-
}
|
|
649
|
-
const blockNumber = BlockNumber(txEffect.l2BlockNumber);
|
|
650
|
-
// Use existing archiver methods to determine finalization level
|
|
651
|
-
const [provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber, blockData] = await Promise.all([
|
|
652
|
-
this.getProvenBlockNumber(),
|
|
653
|
-
this.getCheckpointedL2BlockNumber(),
|
|
654
|
-
this.getFinalizedL2BlockNumber(),
|
|
655
|
-
this.getBlockData(blockNumber)
|
|
656
|
-
]);
|
|
657
|
-
let status;
|
|
658
|
-
if (blockNumber <= finalizedBlockNumber) {
|
|
659
|
-
status = TxStatus.FINALIZED;
|
|
660
|
-
} else if (blockNumber <= provenBlockNumber) {
|
|
661
|
-
status = TxStatus.PROVEN;
|
|
662
|
-
} else if (blockNumber <= checkpointedBlockNumber) {
|
|
663
|
-
status = TxStatus.CHECKPOINTED;
|
|
664
|
-
} else {
|
|
665
|
-
status = TxStatus.PROPOSED;
|
|
666
|
-
}
|
|
667
|
-
const epochNumber = blockData && l1Constants ? getEpochAtSlot(blockData.header.globalVariables.slotNumber, l1Constants) : undefined;
|
|
668
|
-
return new TxReceipt(txHash, status, TxReceipt.executionResultFromRevertCode(txEffect.data.revertCode), undefined, txEffect.data.transactionFee.toBigInt(), txEffect.l2BlockHash, blockNumber, epochNumber);
|
|
669
|
-
}
|
|
670
|
-
/**
|
|
671
790
|
* Looks up which block included the requested tx effect.
|
|
672
791
|
* @param txHash - The txHash of the tx.
|
|
673
792
|
* @returns The block number and index of the tx.
|
|
@@ -676,13 +795,50 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
676
795
|
if (!txEffect) {
|
|
677
796
|
return undefined;
|
|
678
797
|
}
|
|
679
|
-
|
|
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);
|
|
680
803
|
return [
|
|
681
804
|
l2BlockNumber,
|
|
682
805
|
txIndexInBlock
|
|
683
806
|
];
|
|
684
807
|
}
|
|
685
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
|
+
/**
|
|
686
842
|
* Looks up which block deployed a particular contract.
|
|
687
843
|
* @param contractAddress - The address of the contract to look up.
|
|
688
844
|
* @returns The block number and index of the contract.
|
|
@@ -708,6 +864,142 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
708
864
|
return typeof lastBlockNumber === 'number' ? BlockNumber(lastBlockNumber) : BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
709
865
|
}
|
|
710
866
|
/**
|
|
867
|
+
* Resolves all four L2 chain tips (proposed, checkpointed, proven, finalized) in a single
|
|
868
|
+
* read-only transaction so the snapshot is internally consistent. Each underlying record is
|
|
869
|
+
* read at most once: latest block and latest confirmed checkpoint are loaded directly (no
|
|
870
|
+
* separate "find the number, then look up data" hop), the proven/finalized checkpoint
|
|
871
|
+
* singletons are read once and their storage entries are reused if they coincide with the
|
|
872
|
+
* latest checkpoint, and per-tip block hashes are deduped when two tips land on the same block
|
|
873
|
+
* (e.g. finalized == proven).
|
|
874
|
+
*
|
|
875
|
+
* The result is guaranteed to satisfy `finalized <= proven <= checkpointed <= proposed` (by
|
|
876
|
+
* block number). Genesis is represented by `(INITIAL_L2_BLOCK_NUM - 1)` and the supplied
|
|
877
|
+
* `genesisBlockHash`, paired with the synthetic genesis checkpoint id.
|
|
878
|
+
*
|
|
879
|
+
* @param genesisBlockHash - Block hash to report for the synthetic pre-initial block (used when
|
|
880
|
+
* a tip is still at genesis).
|
|
881
|
+
*/ async getL2TipsData(genesisBlockHash) {
|
|
882
|
+
return await this.db.transactionAsync(async ()=>{
|
|
883
|
+
// Define genesis tips
|
|
884
|
+
const genesisBlockNumber = BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
885
|
+
const genesisCheckpointNumber = CheckpointNumber(INITIAL_CHECKPOINT_NUMBER - 1);
|
|
886
|
+
const genesisBlockId = {
|
|
887
|
+
number: genesisBlockNumber,
|
|
888
|
+
hash: genesisBlockHash.toString()
|
|
889
|
+
};
|
|
890
|
+
const genesisCheckpointId = {
|
|
891
|
+
number: genesisCheckpointNumber,
|
|
892
|
+
hash: GENESIS_CHECKPOINT_HEADER_HASH.toString()
|
|
893
|
+
};
|
|
894
|
+
const genesisTip = {
|
|
895
|
+
block: genesisBlockId,
|
|
896
|
+
checkpoint: genesisCheckpointId
|
|
897
|
+
};
|
|
898
|
+
// Load latest block and checkpoint entries
|
|
899
|
+
const [latestBlockEntry] = await toArray(this.#blocks.entriesAsync({
|
|
900
|
+
reverse: true,
|
|
901
|
+
limit: 1
|
|
902
|
+
}));
|
|
903
|
+
const [latestCheckpointEntry] = await toArray(this.#checkpoints.entriesAsync({
|
|
904
|
+
reverse: true,
|
|
905
|
+
limit: 1
|
|
906
|
+
}));
|
|
907
|
+
const latestCheckpointNumber = latestCheckpointEntry ? CheckpointNumber(latestCheckpointEntry[0]) : genesisCheckpointNumber;
|
|
908
|
+
// Load proven and finalized checkpoint number pointers
|
|
909
|
+
const [provenRaw, finalizedRaw] = await Promise.all([
|
|
910
|
+
this.#lastProvenCheckpoint.getAsync(),
|
|
911
|
+
this.#lastFinalizedCheckpoint.getAsync()
|
|
912
|
+
]);
|
|
913
|
+
// Clamp to enforce finalized <= proven <= checkpointed.
|
|
914
|
+
const provenCheckpointNumber = CheckpointNumber(Math.min(provenRaw ?? 0, latestCheckpointNumber));
|
|
915
|
+
const finalizedCheckpointNumber = CheckpointNumber(Math.min(finalizedRaw ?? 0, provenCheckpointNumber));
|
|
916
|
+
// Avoid loading the same checkpoint more than once
|
|
917
|
+
const checkpointStorageCache = new Map();
|
|
918
|
+
if (latestCheckpointEntry) {
|
|
919
|
+
checkpointStorageCache.set(CheckpointNumber(latestCheckpointEntry[0]), latestCheckpointEntry[1]);
|
|
920
|
+
}
|
|
921
|
+
const loadCheckpointStorage = async (n)=>{
|
|
922
|
+
if (n === 0) {
|
|
923
|
+
return undefined;
|
|
924
|
+
}
|
|
925
|
+
if (!checkpointStorageCache.has(n)) {
|
|
926
|
+
const checkpointStorage = await this.#checkpoints.getAsync(n);
|
|
927
|
+
if (!checkpointStorage) {
|
|
928
|
+
throw new CheckpointNotFoundError(n);
|
|
929
|
+
}
|
|
930
|
+
checkpointStorageCache.set(n, checkpointStorage);
|
|
931
|
+
}
|
|
932
|
+
return checkpointStorageCache.get(n);
|
|
933
|
+
};
|
|
934
|
+
// Load proven and finalized checkpoint storage entries
|
|
935
|
+
const provenCheckpoint = await loadCheckpointStorage(provenCheckpointNumber);
|
|
936
|
+
const finalizedCheckpoint = await loadCheckpointStorage(finalizedCheckpointNumber);
|
|
937
|
+
// Avoid loading the same block hash multiple times when tips land on the same block
|
|
938
|
+
const blockHashCache = new Map();
|
|
939
|
+
blockHashCache.set(genesisBlockNumber, genesisBlockHash.toString());
|
|
940
|
+
if (latestBlockEntry) {
|
|
941
|
+
blockHashCache.set(latestBlockEntry[0], BlockHash.fromBuffer(latestBlockEntry[1].blockHash).toString());
|
|
942
|
+
}
|
|
943
|
+
const loadBlockHash = async (n)=>{
|
|
944
|
+
if (!blockHashCache.has(n)) {
|
|
945
|
+
const blockStorage = await this.#blocks.getAsync(n);
|
|
946
|
+
if (!blockStorage) {
|
|
947
|
+
throw new BlockNotFoundError(n);
|
|
948
|
+
}
|
|
949
|
+
const blockHash = BlockHash.fromBuffer(blockStorage.blockHash).toString();
|
|
950
|
+
blockHashCache.set(n, blockHash);
|
|
951
|
+
}
|
|
952
|
+
return blockHashCache.get(n);
|
|
953
|
+
};
|
|
954
|
+
// Build proposed chain tip (this one has block only, no checkpoint)
|
|
955
|
+
const proposedBlockId = latestBlockEntry === undefined ? genesisBlockId : {
|
|
956
|
+
number: BlockNumber(latestBlockEntry[0]),
|
|
957
|
+
hash: BlockHash.fromBuffer(latestBlockEntry[1].blockHash).toString()
|
|
958
|
+
};
|
|
959
|
+
// Build other tips from checkpoint data, reading corresponding block data from the cache
|
|
960
|
+
const buildTipFromCheckpoint = async (stored)=>{
|
|
961
|
+
if (!stored) {
|
|
962
|
+
return genesisTip;
|
|
963
|
+
}
|
|
964
|
+
const blockNumber = BlockNumber(stored.startBlock + stored.blockCount - 1);
|
|
965
|
+
const blockHash = await loadBlockHash(blockNumber);
|
|
966
|
+
const header = CheckpointHeader.fromBuffer(stored.header);
|
|
967
|
+
return {
|
|
968
|
+
block: {
|
|
969
|
+
number: blockNumber,
|
|
970
|
+
hash: blockHash
|
|
971
|
+
},
|
|
972
|
+
checkpoint: {
|
|
973
|
+
number: CheckpointNumber(stored.checkpointNumber),
|
|
974
|
+
hash: header.hash().toString()
|
|
975
|
+
}
|
|
976
|
+
};
|
|
977
|
+
};
|
|
978
|
+
const checkpointedTip = await buildTipFromCheckpoint(latestCheckpointEntry?.[1]);
|
|
979
|
+
const provenTip = await buildTipFromCheckpoint(provenCheckpoint);
|
|
980
|
+
const finalizedTip = await buildTipFromCheckpoint(finalizedCheckpoint);
|
|
981
|
+
// A checkpointed block past the latest stored block would mean a checkpoint
|
|
982
|
+
// references blocks that aren't in blocks.
|
|
983
|
+
if (proposedBlockId.number < checkpointedTip.block.number) {
|
|
984
|
+
throw new Error(`Inconsistent block store: latest block ${proposedBlockId.number} is behind checkpointed block ${checkpointedTip.block.number}`);
|
|
985
|
+
}
|
|
986
|
+
// Assert that checkpoint numbers are increasing
|
|
987
|
+
if (finalizedTip.checkpoint.number > provenTip.checkpoint.number || provenTip.checkpoint.number > checkpointedTip.checkpoint.number) {
|
|
988
|
+
throw new Error(`Inconsistent checkpoint numbers in chain tips: finalized=${finalizedTip.checkpoint.number} proven=${provenTip.checkpoint.number} checkpointed=${checkpointedTip.checkpoint.number}`);
|
|
989
|
+
}
|
|
990
|
+
// Assert block numbers are increasing
|
|
991
|
+
if (finalizedTip.block.number > provenTip.block.number || provenTip.block.number > checkpointedTip.block.number || checkpointedTip.block.number > proposedBlockId.number) {
|
|
992
|
+
throw new Error(`Inconsistent block numbers in chain tips: finalized=${finalizedTip.block.number} proven=${provenTip.block.number} checkpointed=${checkpointedTip.block.number} proposed=${proposedBlockId.number}`);
|
|
993
|
+
}
|
|
994
|
+
return {
|
|
995
|
+
proposed: proposedBlockId,
|
|
996
|
+
checkpointed: checkpointedTip,
|
|
997
|
+
proven: provenTip,
|
|
998
|
+
finalized: finalizedTip
|
|
999
|
+
};
|
|
1000
|
+
});
|
|
1001
|
+
}
|
|
1002
|
+
/**
|
|
711
1003
|
* Gets the most recent L1 block processed.
|
|
712
1004
|
* @returns The L1 block that published the latest L2 block
|
|
713
1005
|
*/ getSynchedL1BlockNumber() {
|
|
@@ -716,23 +1008,70 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
716
1008
|
setSynchedL1BlockNumber(l1BlockNumber) {
|
|
717
1009
|
return this.#lastSynchedL1Block.set(l1BlockNumber);
|
|
718
1010
|
}
|
|
1011
|
+
/**
|
|
1012
|
+
* Adds a proposed checkpoint to the pending queue.
|
|
1013
|
+
* Accepts proposed.checkpointNumber === latestTip + 1, where latestTip is the highest of
|
|
1014
|
+
* confirmed and the highest pending checkpoint number.
|
|
1015
|
+
* Computes archive and checkpointOutHash from the stored blocks.
|
|
1016
|
+
*/ async addProposedCheckpoint(proposed) {
|
|
1017
|
+
return await this.db.transactionAsync(async ()=>{
|
|
1018
|
+
const confirmed = await this.getLatestCheckpointNumber();
|
|
1019
|
+
const [latestPendingKey] = await toArray(this.#proposedCheckpoints.keysAsync({
|
|
1020
|
+
reverse: true,
|
|
1021
|
+
limit: 1
|
|
1022
|
+
}));
|
|
1023
|
+
const latestTip = CheckpointNumber(latestPendingKey !== undefined ? Math.max(latestPendingKey, confirmed) : confirmed);
|
|
1024
|
+
if (proposed.checkpointNumber !== latestTip + 1) {
|
|
1025
|
+
throw new ProposedCheckpointNotSequentialError(proposed.checkpointNumber, latestTip);
|
|
1026
|
+
}
|
|
1027
|
+
// Ensure the predecessor block (from pending or confirmed chain) exists
|
|
1028
|
+
const previousBlock = await this.getPreviousCheckpointBlock(proposed.checkpointNumber);
|
|
1029
|
+
const blocks = [];
|
|
1030
|
+
for(let i = 0; i < proposed.blockCount; i++){
|
|
1031
|
+
const block = await this.getBlock({
|
|
1032
|
+
number: BlockNumber(proposed.startBlock + i)
|
|
1033
|
+
});
|
|
1034
|
+
if (!block) {
|
|
1035
|
+
throw new BlockNotFoundError(proposed.startBlock + i);
|
|
1036
|
+
}
|
|
1037
|
+
blocks.push(block);
|
|
1038
|
+
}
|
|
1039
|
+
this.validateCheckpointBlocks(blocks, previousBlock);
|
|
1040
|
+
const archive = blocks[blocks.length - 1].archive;
|
|
1041
|
+
const checkpointOutHash = Checkpoint.getCheckpointOutHash(blocks);
|
|
1042
|
+
await this.#proposedCheckpoints.set(proposed.checkpointNumber, {
|
|
1043
|
+
header: proposed.header.toBuffer(),
|
|
1044
|
+
archive: archive.toBuffer(),
|
|
1045
|
+
checkpointOutHash: checkpointOutHash.toBuffer(),
|
|
1046
|
+
checkpointNumber: proposed.checkpointNumber,
|
|
1047
|
+
startBlock: proposed.startBlock,
|
|
1048
|
+
blockCount: proposed.blockCount,
|
|
1049
|
+
totalManaUsed: proposed.totalManaUsed.toString(),
|
|
1050
|
+
feeAssetPriceModifier: proposed.feeAssetPriceModifier.toString()
|
|
1051
|
+
});
|
|
1052
|
+
});
|
|
1053
|
+
}
|
|
719
1054
|
async getProvenCheckpointNumber() {
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
1055
|
+
return await this.db.transactionAsync(async ()=>{
|
|
1056
|
+
const [latestCheckpointNumber, provenCheckpointNumber] = await Promise.all([
|
|
1057
|
+
this.getLatestCheckpointNumber(),
|
|
1058
|
+
this.#lastProvenCheckpoint.getAsync()
|
|
1059
|
+
]);
|
|
1060
|
+
return (provenCheckpointNumber ?? 0) > latestCheckpointNumber ? latestCheckpointNumber : CheckpointNumber(provenCheckpointNumber ?? 0);
|
|
1061
|
+
});
|
|
725
1062
|
}
|
|
726
1063
|
async setProvenCheckpointNumber(checkpointNumber) {
|
|
727
1064
|
const result = await this.#lastProvenCheckpoint.set(checkpointNumber);
|
|
728
1065
|
return result;
|
|
729
1066
|
}
|
|
730
1067
|
async getFinalizedCheckpointNumber() {
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
1068
|
+
return await this.db.transactionAsync(async ()=>{
|
|
1069
|
+
const [provenCheckpointNumber, finalizedCheckpointNumber] = await Promise.all([
|
|
1070
|
+
this.getProvenCheckpointNumber(),
|
|
1071
|
+
this.#lastFinalizedCheckpoint.getAsync()
|
|
1072
|
+
]);
|
|
1073
|
+
return (finalizedCheckpointNumber ?? 0) > provenCheckpointNumber ? provenCheckpointNumber : CheckpointNumber(finalizedCheckpointNumber ?? 0);
|
|
1074
|
+
});
|
|
736
1075
|
}
|
|
737
1076
|
setFinalizedCheckpointNumber(checkpointNumber) {
|
|
738
1077
|
return this.#lastFinalizedCheckpoint.set(checkpointNumber);
|
|
@@ -770,4 +1109,70 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
770
1109
|
await this.#pendingChainValidationStatus.delete();
|
|
771
1110
|
}
|
|
772
1111
|
}
|
|
1112
|
+
/** Records a rejected-checkpoint entry, keyed by its own archive root. */ async addRejectedCheckpoint(entry) {
|
|
1113
|
+
const archiveRootHex = entry.archiveRoot.toString();
|
|
1114
|
+
await this.#rejectedCheckpoints.set(archiveRootHex, {
|
|
1115
|
+
checkpointNumber: entry.checkpointNumber,
|
|
1116
|
+
archiveRoot: entry.archiveRoot.toBuffer(),
|
|
1117
|
+
parentArchiveRoot: entry.parentArchiveRoot.toBuffer(),
|
|
1118
|
+
slotNumber: entry.slotNumber,
|
|
1119
|
+
l1: entry.l1.toBuffer(),
|
|
1120
|
+
reason: entry.reason
|
|
1121
|
+
});
|
|
1122
|
+
await this.#rejectedCheckpointsByNumber.set(entry.checkpointNumber, archiveRootHex);
|
|
1123
|
+
await this.advanceSynchedL1BlockNumber(entry.l1.blockNumber);
|
|
1124
|
+
}
|
|
1125
|
+
/** Returns the rejected-checkpoint entry with the given archive root, or undefined if not present. */ async getRejectedCheckpointByArchiveRoot(archiveRoot) {
|
|
1126
|
+
const stored = await this.#rejectedCheckpoints.getAsync(archiveRoot.toString());
|
|
1127
|
+
return stored ? this.rejectedCheckpointFromStorage(stored) : undefined;
|
|
1128
|
+
}
|
|
1129
|
+
/** Returns the rejected-checkpoint entry recorded for the given checkpoint number, or undefined if none. */ async getRejectedCheckpointByNumber(checkpointNumber) {
|
|
1130
|
+
const archiveRootHex = await this.#rejectedCheckpointsByNumber.getAsync(checkpointNumber);
|
|
1131
|
+
if (archiveRootHex === undefined) {
|
|
1132
|
+
return undefined;
|
|
1133
|
+
}
|
|
1134
|
+
const stored = await this.#rejectedCheckpoints.getAsync(archiveRootHex);
|
|
1135
|
+
return stored ? this.rejectedCheckpointFromStorage(stored) : undefined;
|
|
1136
|
+
}
|
|
1137
|
+
/** Returns the highest checkpoint number recorded across all rejected entries, or `INITIAL_CHECKPOINT_NUMBER - 1` if none. */ async getLatestRejectedCheckpointNumber() {
|
|
1138
|
+
const [latest] = await toArray(this.#rejectedCheckpointsByNumber.keysAsync({
|
|
1139
|
+
reverse: true,
|
|
1140
|
+
limit: 1
|
|
1141
|
+
}));
|
|
1142
|
+
return CheckpointNumber(latest ?? INITIAL_CHECKPOINT_NUMBER - 1);
|
|
1143
|
+
}
|
|
1144
|
+
/** Removes a rejected-checkpoint entry by its archive root (used when an entry no longer matches L1). */ async removeRejectedCheckpointByArchiveRoot(archiveRoot) {
|
|
1145
|
+
const archiveRootHex = archiveRoot.toString();
|
|
1146
|
+
const stored = await this.#rejectedCheckpoints.getAsync(archiveRootHex);
|
|
1147
|
+
await this.#rejectedCheckpoints.delete(archiveRootHex);
|
|
1148
|
+
if (stored) {
|
|
1149
|
+
// Only clear the by-number index if it still points at this archive root, so a distinct
|
|
1150
|
+
// entry that shares the checkpoint number (e.g. an L1 reorg replacement) is not dropped.
|
|
1151
|
+
const indexed = await this.#rejectedCheckpointsByNumber.getAsync(stored.checkpointNumber);
|
|
1152
|
+
if (indexed === archiveRootHex) {
|
|
1153
|
+
await this.#rejectedCheckpointsByNumber.delete(stored.checkpointNumber);
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
}
|
|
1157
|
+
/**
|
|
1158
|
+
* Advances the stored last-synched L1 block number to `l1BlockNumber` only if it is strictly
|
|
1159
|
+
* greater than the current value. Use this whenever ingesting checkpoint-shaped data so the
|
|
1160
|
+
* sync pointer never walks backwards on out-of-order writes (e.g. an invalid checkpoint
|
|
1161
|
+
* advance followed by a valid-checkpoint commit landing at an earlier L1 block).
|
|
1162
|
+
*/ async advanceSynchedL1BlockNumber(l1BlockNumber) {
|
|
1163
|
+
const current = await this.#lastSynchedL1Block.getAsync();
|
|
1164
|
+
if (current === undefined || l1BlockNumber > current) {
|
|
1165
|
+
await this.#lastSynchedL1Block.set(l1BlockNumber);
|
|
1166
|
+
}
|
|
1167
|
+
}
|
|
1168
|
+
rejectedCheckpointFromStorage(stored) {
|
|
1169
|
+
return {
|
|
1170
|
+
checkpointNumber: CheckpointNumber(stored.checkpointNumber),
|
|
1171
|
+
archiveRoot: Fr.fromBuffer(stored.archiveRoot),
|
|
1172
|
+
parentArchiveRoot: Fr.fromBuffer(stored.parentArchiveRoot),
|
|
1173
|
+
slotNumber: SlotNumber(stored.slotNumber),
|
|
1174
|
+
l1: L1PublishedData.fromBuffer(stored.l1),
|
|
1175
|
+
reason: stored.reason
|
|
1176
|
+
};
|
|
1177
|
+
}
|
|
773
1178
|
}
|