@aztec/archiver 0.0.1-commit.42ee6df9b → 0.0.1-commit.431c48d
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 +7 -7
- package/dest/archiver.d.ts +70 -15
- package/dest/archiver.d.ts.map +1 -1
- package/dest/archiver.js +273 -71
- package/dest/config.d.ts +7 -3
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +44 -16
- package/dest/errors.d.ts +35 -13
- package/dest/errors.d.ts.map +1 -1
- package/dest/errors.js +54 -16
- package/dest/factory.d.ts +18 -6
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +66 -21
- 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 +20 -10
- package/dest/l1/data_retrieval.d.ts +24 -10
- package/dest/l1/data_retrieval.d.ts.map +1 -1
- package/dest/l1/data_retrieval.js +13 -19
- 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 -50
- package/dest/modules/data_source_base.d.ts.map +1 -1
- package/dest/modules/data_source_base.js +250 -150
- package/dest/modules/data_store_updater.d.ts +28 -9
- package/dest/modules/data_store_updater.d.ts.map +1 -1
- package/dest/modules/data_store_updater.js +129 -64
- 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 +11 -7
- package/dest/modules/l1_synchronizer.d.ts.map +1 -1
- package/dest/modules/l1_synchronizer.js +347 -118
- package/dest/modules/outbox_trees_resolver.d.ts +64 -0
- package/dest/modules/outbox_trees_resolver.d.ts.map +1 -0
- package/dest/modules/outbox_trees_resolver.js +184 -0
- package/dest/modules/validation.d.ts +30 -7
- package/dest/modules/validation.d.ts.map +1 -1
- package/dest/modules/validation.js +33 -15
- package/dest/store/block_store.d.ts +169 -94
- package/dest/store/block_store.d.ts.map +1 -1
- package/dest/store/block_store.js +618 -270
- package/dest/store/contract_class_store.d.ts +17 -3
- package/dest/store/contract_class_store.d.ts.map +1 -1
- package/dest/store/contract_class_store.js +36 -2
- 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 +53 -5
- 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 -8
- package/dest/store/l2_tips_cache.d.ts.map +1 -1
- package/dest/store/l2_tips_cache.js +13 -96
- 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 +31 -1
- package/dest/test/fake_l1_state.d.ts +14 -3
- package/dest/test/fake_l1_state.d.ts.map +1 -1
- package/dest/test/fake_l1_state.js +58 -18
- package/dest/test/mock_l1_to_l2_message_source.d.ts +1 -1
- package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
- package/dest/test/mock_l1_to_l2_message_source.js +1 -2
- package/dest/test/mock_l2_block_source.d.ts +47 -54
- package/dest/test/mock_l2_block_source.d.ts.map +1 -1
- package/dest/test/mock_l2_block_source.js +229 -205
- 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 +15 -14
- package/src/archiver.ts +355 -77
- package/src/config.ts +51 -20
- package/src/errors.ts +80 -23
- package/src/factory.ts +80 -19
- package/src/index.ts +18 -2
- package/src/l1/calldata_retriever.ts +27 -10
- package/src/l1/data_retrieval.ts +38 -36
- 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 +319 -200
- package/src/modules/data_store_updater.ts +156 -65
- package/src/modules/instrumentation.ts +43 -10
- package/src/modules/l1_synchronizer.ts +499 -136
- package/src/modules/outbox_trees_resolver.ts +220 -0
- package/src/modules/validation.ts +78 -21
- package/src/store/block_store.ts +741 -333
- package/src/store/contract_class_store.ts +47 -3
- package/src/store/contract_instance_store.ts +67 -7
- 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 -109
- package/src/store/log_store.ts +296 -652
- package/src/store/log_store_codec.ts +143 -0
- package/src/store/message_store.ts +35 -2
- package/src/test/fake_l1_state.ts +65 -27
- package/src/test/mock_l1_to_l2_message_source.ts +0 -1
- package/src/test/mock_l2_block_source.ts +276 -258
- 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 -377
- package/dest/store/kv_archiver_store.d.ts.map +0 -1
- package/dest/store/kv_archiver_store.js +0 -494
- package/src/store/kv_archiver_store.ts +0 -713
|
@@ -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 {
|
|
9
|
+
import { BlockHash, Body, CommitteeAttestation, GENESIS_CHECKPOINT_HEADER_HASH, L2Block, deserializeValidateCheckpointResult, serializeValidateCheckpointResult } from '@aztec/stdlib/block';
|
|
10
10
|
import { Checkpoint, L1PublishedData } from '@aztec/stdlib/checkpoint';
|
|
11
|
-
import { getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
|
|
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, ProposedCheckpointNotSequentialError,
|
|
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,7 +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;
|
|
33
|
-
/**
|
|
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;
|
|
34
34
|
#log;
|
|
35
35
|
constructor(db){
|
|
36
36
|
this.db = db;
|
|
@@ -47,7 +47,9 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
47
47
|
this.#pendingChainValidationStatus = db.openSingleton('archiver_pending_chain_validation_status');
|
|
48
48
|
this.#checkpoints = db.openMap('archiver_checkpoints');
|
|
49
49
|
this.#slotToCheckpoint = db.openMap('archiver_slot_to_checkpoint');
|
|
50
|
-
this.#
|
|
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');
|
|
51
53
|
}
|
|
52
54
|
/**
|
|
53
55
|
* Returns the finalized L2 block number. An L2 block is finalized when it was proven
|
|
@@ -78,13 +80,14 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
78
80
|
const blockLastArchive = block.header.lastArchive.root;
|
|
79
81
|
// Extract the latest block and checkpoint numbers
|
|
80
82
|
const previousBlockNumber = await this.getLatestL2BlockNumber();
|
|
81
|
-
const
|
|
82
|
-
const previousCheckpointNumber = await this.getLatestCheckpointNumber();
|
|
83
|
+
const latestCheckpointNumber = await this.getLatestCheckpointNumber();
|
|
83
84
|
// Verify we're not overwriting checkpointed blocks
|
|
84
85
|
const lastCheckpointedBlockNumber = await this.getCheckpointedL2BlockNumber();
|
|
85
86
|
if (!opts.force && blockNumber <= lastCheckpointedBlockNumber) {
|
|
86
87
|
// Check if the proposed block matches the already-checkpointed one
|
|
87
|
-
const existingBlock = await this.
|
|
88
|
+
const existingBlock = await this.getBlockData({
|
|
89
|
+
number: BlockNumber(blockNumber)
|
|
90
|
+
});
|
|
88
91
|
if (existingBlock && existingBlock.archive.root.equals(block.archive.root)) {
|
|
89
92
|
throw new BlockAlreadyCheckpointedError(blockNumber);
|
|
90
93
|
}
|
|
@@ -94,21 +97,22 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
94
97
|
if (!opts.force && previousBlockNumber !== blockNumber - 1) {
|
|
95
98
|
throw new BlockNumberNotSequentialError(blockNumber, previousBlockNumber);
|
|
96
99
|
}
|
|
97
|
-
//
|
|
98
|
-
//
|
|
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.
|
|
99
102
|
const expectedCheckpointNumber = blockCheckpointNumber - 1;
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
throw new CheckpointNumberNotSequentialError(blockCheckpointNumber, reported, source);
|
|
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);
|
|
109
111
|
}
|
|
110
112
|
// Extract the previous block if there is one and see if it is for the same checkpoint or not
|
|
111
|
-
const previousBlockResult = await this.
|
|
113
|
+
const previousBlockResult = await this.getBlockData({
|
|
114
|
+
number: previousBlockNumber
|
|
115
|
+
});
|
|
112
116
|
let expectedBlockIndex = 0;
|
|
113
117
|
let previousBlockIndex = undefined;
|
|
114
118
|
if (previousBlockResult !== undefined) {
|
|
@@ -118,7 +122,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
118
122
|
expectedBlockIndex = previousBlockIndex + 1;
|
|
119
123
|
}
|
|
120
124
|
if (!previousBlockResult.archive.root.equals(blockLastArchive)) {
|
|
121
|
-
throw new BlockArchiveNotConsistentError(blockNumber, previousBlockResult.
|
|
125
|
+
throw new BlockArchiveNotConsistentError(blockNumber, previousBlockResult.header.globalVariables.blockNumber, blockLastArchive, previousBlockResult.archive.root);
|
|
122
126
|
}
|
|
123
127
|
}
|
|
124
128
|
// Now check that the block has the expected index value
|
|
@@ -131,21 +135,36 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
131
135
|
}
|
|
132
136
|
/**
|
|
133
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.
|
|
134
141
|
* @param checkpoints - The L2 checkpoints to be added to the store.
|
|
135
|
-
* @returns
|
|
142
|
+
* @returns The checkpoints that were actually inserted (excluding already-stored ones).
|
|
136
143
|
*/ async addCheckpoints(checkpoints, opts = {}) {
|
|
137
144
|
if (checkpoints.length === 0) {
|
|
138
|
-
return
|
|
145
|
+
return [];
|
|
139
146
|
}
|
|
140
147
|
return await this.db.transactionAsync(async ()=>{
|
|
141
|
-
// Check that the checkpoint immediately before the first block to be added is present in the store.
|
|
142
148
|
const firstCheckpointNumber = checkpoints[0].checkpoint.number;
|
|
143
149
|
const previousCheckpointNumber = await this.getLatestCheckpointNumber();
|
|
144
|
-
|
|
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 [];
|
|
157
|
+
}
|
|
158
|
+
// Re-check sequentiality after skipping
|
|
159
|
+
const newFirstNumber = checkpoints[0].checkpoint.number;
|
|
160
|
+
if (previousCheckpointNumber !== newFirstNumber - 1) {
|
|
161
|
+
throw new InitialCheckpointNumberNotSequentialError(newFirstNumber, previousCheckpointNumber);
|
|
162
|
+
}
|
|
163
|
+
} else if (previousCheckpointNumber !== firstCheckpointNumber - 1 && !opts.force) {
|
|
145
164
|
throw new InitialCheckpointNumberNotSequentialError(firstCheckpointNumber, previousCheckpointNumber);
|
|
146
165
|
}
|
|
147
166
|
// Get the last block of the previous checkpoint for archive chaining
|
|
148
|
-
let previousBlock = await this.getPreviousCheckpointBlock(
|
|
167
|
+
let previousBlock = await this.getPreviousCheckpointBlock(checkpoints[0].checkpoint.number);
|
|
149
168
|
// Iterate over checkpoints array and insert them, checking that the block numbers are sequential.
|
|
150
169
|
let previousCheckpoint = undefined;
|
|
151
170
|
for (const checkpoint of checkpoints){
|
|
@@ -169,19 +188,55 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
169
188
|
attestations: checkpoint.attestations.map((attestation)=>attestation.toBuffer()),
|
|
170
189
|
checkpointNumber: checkpoint.checkpoint.number,
|
|
171
190
|
startBlock: checkpoint.checkpoint.blocks[0].number,
|
|
172
|
-
blockCount: checkpoint.checkpoint.blocks.length
|
|
191
|
+
blockCount: checkpoint.checkpoint.blocks.length,
|
|
192
|
+
feeAssetPriceModifier: checkpoint.checkpoint.feeAssetPriceModifier.toString()
|
|
173
193
|
});
|
|
174
194
|
// Update slot-to-checkpoint index
|
|
175
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);
|
|
176
201
|
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
await this.clearProposedCheckpointIfSuperseded(lastConfirmedCheckpointNumber);
|
|
180
|
-
await this.#lastSynchedL1Block.set(checkpoints[checkpoints.length - 1].l1.blockNumber);
|
|
181
|
-
return true;
|
|
202
|
+
await this.advanceSynchedL1BlockNumber(checkpoints[checkpoints.length - 1].l1.blockNumber);
|
|
203
|
+
return checkpoints;
|
|
182
204
|
});
|
|
183
205
|
}
|
|
184
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
|
+
/**
|
|
185
240
|
* Gets the last block of the checkpoint before the given one.
|
|
186
241
|
* Returns undefined if there is no previous checkpoint (i.e. genesis).
|
|
187
242
|
*/ async getPreviousCheckpointBlock(checkpointNumber) {
|
|
@@ -189,12 +244,15 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
189
244
|
if (previousCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
|
|
190
245
|
return undefined;
|
|
191
246
|
}
|
|
192
|
-
|
|
193
|
-
|
|
247
|
+
// Check across both proposed and mined checkpoints
|
|
248
|
+
const predecessor = await this.getProposedCheckpointByNumber(previousCheckpointNumber) ?? await this.getCheckpointData(previousCheckpointNumber);
|
|
249
|
+
if (!predecessor) {
|
|
194
250
|
throw new CheckpointNotFoundError(previousCheckpointNumber);
|
|
195
251
|
}
|
|
196
|
-
const previousBlockNumber = BlockNumber(
|
|
197
|
-
const previousBlock = await this.getBlock(
|
|
252
|
+
const previousBlockNumber = BlockNumber(predecessor.startBlock + predecessor.blockCount - 1);
|
|
253
|
+
const previousBlock = await this.getBlock({
|
|
254
|
+
number: previousBlockNumber
|
|
255
|
+
});
|
|
198
256
|
if (previousBlock === undefined) {
|
|
199
257
|
throw new BlockNotFoundError(previousBlockNumber);
|
|
200
258
|
}
|
|
@@ -203,7 +261,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
203
261
|
/**
|
|
204
262
|
* Validates that blocks are sequential, have correct indexes, and chain via archive roots.
|
|
205
263
|
* This is the same validation used for both confirmed checkpoints (addCheckpoints) and
|
|
206
|
-
* proposed checkpoints (
|
|
264
|
+
* proposed checkpoints (addProposedCheckpoint).
|
|
207
265
|
*/ validateCheckpointBlocks(blocks, previousBlock) {
|
|
208
266
|
for (const block of blocks){
|
|
209
267
|
if (previousBlock) {
|
|
@@ -245,7 +303,8 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
245
303
|
data: block.body.txEffects[i],
|
|
246
304
|
l2BlockNumber: block.number,
|
|
247
305
|
l2BlockHash: blockHash,
|
|
248
|
-
txIndexInBlock: i
|
|
306
|
+
txIndexInBlock: i,
|
|
307
|
+
slotNumber: block.header.globalVariables.slotNumber
|
|
249
308
|
};
|
|
250
309
|
await this.#txEffects.set(txEffect.data.txHash.toString(), serializeIndexedTxEffect(txEffect));
|
|
251
310
|
}
|
|
@@ -254,17 +313,51 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
254
313
|
await this.#blockHashIndex.set(blockHash.toString(), block.number);
|
|
255
314
|
await this.#blockArchiveIndex.set(block.archive.root.toString(), block.number);
|
|
256
315
|
}
|
|
257
|
-
/** Deletes a block and all associated data (tx effects, indices). */ async deleteBlock(
|
|
316
|
+
/** Deletes a block and all associated data (tx effects, indices). */ async deleteBlock(blockNumber, blockStorage) {
|
|
258
317
|
// Delete the block from the main blocks map
|
|
259
|
-
await this.#blocks.delete(
|
|
260
|
-
|
|
261
|
-
|
|
318
|
+
await this.#blocks.delete(blockNumber);
|
|
319
|
+
const blockHash = bufferToHex(blockStorage.blockHash);
|
|
320
|
+
// Delete the tx effects of the block's txs, skipping entries that no longer point at this block: if
|
|
321
|
+
// another stored block also contains the tx, the entry points at that block, and deleting it here would
|
|
322
|
+
// orphan that block's index. Honest chains never store the same tx twice (duplicate nullifiers are
|
|
323
|
+
// rejected at build, re-execution, and world-state sync), so the store must not turn such a state into
|
|
324
|
+
// corruption of the surviving block if it ever appears.
|
|
325
|
+
const blockTxsBuffer = await this.#blockTxs.getAsync(blockHash);
|
|
326
|
+
if (blockTxsBuffer !== undefined) {
|
|
327
|
+
const reader = BufferReader.asReader(blockTxsBuffer);
|
|
328
|
+
const txHashes = [];
|
|
329
|
+
while(!reader.isEmpty()){
|
|
330
|
+
txHashes.push(reader.readObject(TxHash).toString());
|
|
331
|
+
}
|
|
332
|
+
await Promise.all(txHashes.map((txHash)=>this.deleteTxEffect(txHash, blockStorage.blockHash)));
|
|
333
|
+
}
|
|
262
334
|
// Delete block txs mapping
|
|
263
|
-
const blockHash = (await block.hash()).toString();
|
|
264
335
|
await this.#blockTxs.delete(blockHash);
|
|
265
336
|
// Clean up indices
|
|
266
337
|
await this.#blockHashIndex.delete(blockHash);
|
|
267
|
-
await this.#blockArchiveIndex.delete(
|
|
338
|
+
await this.#blockArchiveIndex.delete(AppendOnlyTreeSnapshot.fromBuffer(blockStorage.archive).root.toString());
|
|
339
|
+
}
|
|
340
|
+
/** Deletes a tx effect only if it is still owned by (points at) the given block. */ async deleteTxEffect(txHash, blockHash) {
|
|
341
|
+
const stored = await this.#txEffects.getAsync(txHash);
|
|
342
|
+
if (stored === undefined) {
|
|
343
|
+
this.#log.warn(`Missing tx effect for tx ${txHash} while removing its block`, {
|
|
344
|
+
txHash,
|
|
345
|
+
blockHash: bufferToHex(blockHash)
|
|
346
|
+
});
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
// An IndexedTxEffect starts with the owning block hash (32 bytes) — see getTxLocation.
|
|
350
|
+
if (!Buffer.from(stored.buffer, stored.byteOffset, 32).equals(blockHash)) {
|
|
351
|
+
// Fires only when two stored blocks listed the same tx — a state upstream validation should make
|
|
352
|
+
// unreachable. Keep the entry (it belongs to the surviving block) but flag the duplication loudly.
|
|
353
|
+
this.#log.warn(`Tx effect for tx ${txHash} is owned by a block other than the one being removed`, {
|
|
354
|
+
txHash,
|
|
355
|
+
removedBlockHash: bufferToHex(blockHash),
|
|
356
|
+
owningBlockHash: bufferToHex(Buffer.from(stored.buffer, stored.byteOffset, 32))
|
|
357
|
+
});
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
await this.#txEffects.delete(txHash);
|
|
268
361
|
}
|
|
269
362
|
/**
|
|
270
363
|
* Removes all checkpoints with checkpoint number > checkpointNumber.
|
|
@@ -308,11 +401,8 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
308
401
|
await this.#checkpoints.delete(c);
|
|
309
402
|
this.#log.debug(`Removed checkpoint ${c}`);
|
|
310
403
|
}
|
|
311
|
-
//
|
|
312
|
-
|
|
313
|
-
if (proposedCheckpointNumber > checkpointNumber) {
|
|
314
|
-
await this.#proposedCheckpoint.delete();
|
|
315
|
-
}
|
|
404
|
+
// Evict all pending checkpoints > checkpointNumber (their base chain no longer exists)
|
|
405
|
+
await this.evictProposedCheckpointsFrom(CheckpointNumber(checkpointNumber + 1));
|
|
316
406
|
return {
|
|
317
407
|
blocksRemoved
|
|
318
408
|
};
|
|
@@ -336,6 +426,31 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
336
426
|
}
|
|
337
427
|
return checkpoints;
|
|
338
428
|
}
|
|
429
|
+
/**
|
|
430
|
+
* Returns up to `limit` checkpoints anchored at `fromSlot`, ordered nearest-first, walking the slot index.
|
|
431
|
+
* With `reverse`, takes the checkpoints at or before `fromSlot` (descending by slot); otherwise the
|
|
432
|
+
* checkpoints at or after it (ascending). `limit: 1, reverse: true` yields the latest checkpoint at or
|
|
433
|
+
* before the slot in a single range scan.
|
|
434
|
+
*/ async getCheckpointsBySlot(fromSlot, limit, reverse) {
|
|
435
|
+
// The KV range bounds are direction-dependent: forward is [start, end), reverse is (start, end], so a
|
|
436
|
+
// reverse scan uses `end: fromSlot` (inclusive) with no +1 to include the checkpoint at fromSlot itself.
|
|
437
|
+
const range = reverse ? {
|
|
438
|
+
end: fromSlot,
|
|
439
|
+
reverse: true,
|
|
440
|
+
limit
|
|
441
|
+
} : {
|
|
442
|
+
start: fromSlot,
|
|
443
|
+
limit
|
|
444
|
+
};
|
|
445
|
+
const result = [];
|
|
446
|
+
for await (const [, checkpointNumber] of this.#slotToCheckpoint.entriesAsync(range)){
|
|
447
|
+
const checkpointStorage = await this.#checkpoints.getAsync(checkpointNumber);
|
|
448
|
+
if (checkpointStorage) {
|
|
449
|
+
result.push(this.checkpointDataFromCheckpointStorage(checkpointStorage));
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
return result;
|
|
453
|
+
}
|
|
339
454
|
/** Returns checkpoint data for all checkpoints whose slot falls within the given range (inclusive). */ async getCheckpointDataForSlotRange(startSlot, endSlot) {
|
|
340
455
|
const result = [];
|
|
341
456
|
for await (const [, checkpointNumber] of this.#slotToCheckpoint.entriesAsync({
|
|
@@ -349,6 +464,20 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
349
464
|
}
|
|
350
465
|
return result;
|
|
351
466
|
}
|
|
467
|
+
/**
|
|
468
|
+
* Returns the checkpoint numbers for all checkpoints whose slot falls within the given range (inclusive).
|
|
469
|
+
* Lighter than {@link getCheckpointDataForSlotRange} when callers only need to identify which
|
|
470
|
+
* checkpoints fall in the range and will fetch full data for at most a few of them.
|
|
471
|
+
*/ async getCheckpointNumbersForSlotRange(startSlot, endSlot) {
|
|
472
|
+
const result = [];
|
|
473
|
+
for await (const [, checkpointNumber] of this.#slotToCheckpoint.entriesAsync({
|
|
474
|
+
start: startSlot,
|
|
475
|
+
end: endSlot + 1
|
|
476
|
+
})){
|
|
477
|
+
result.push(CheckpointNumber(checkpointNumber));
|
|
478
|
+
}
|
|
479
|
+
return result;
|
|
480
|
+
}
|
|
352
481
|
checkpointDataFromCheckpointStorage(checkpointStorage) {
|
|
353
482
|
return {
|
|
354
483
|
header: CheckpointHeader.fromBuffer(checkpointStorage.header),
|
|
@@ -357,6 +486,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
357
486
|
checkpointNumber: CheckpointNumber(checkpointStorage.checkpointNumber),
|
|
358
487
|
startBlock: BlockNumber(checkpointStorage.startBlock),
|
|
359
488
|
blockCount: checkpointStorage.blockCount,
|
|
489
|
+
feeAssetPriceModifier: BigInt(checkpointStorage.feeAssetPriceModifier),
|
|
360
490
|
l1: L1PublishedData.fromBuffer(checkpointStorage.l1),
|
|
361
491
|
attestations: checkpointStorage.attestations.map((buf)=>CommitteeAttestation.fromBuffer(buf))
|
|
362
492
|
};
|
|
@@ -408,14 +538,22 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
408
538
|
const latestBlockNumber = await this.getLatestL2BlockNumber();
|
|
409
539
|
// Iterate from blockNumber + 1 to latestBlockNumber
|
|
410
540
|
for(let bn = blockNumber + 1; bn <= latestBlockNumber; bn++){
|
|
411
|
-
const
|
|
412
|
-
if (
|
|
541
|
+
const blockStorage = await this.#blocks.getAsync(bn);
|
|
542
|
+
if (blockStorage === undefined) {
|
|
413
543
|
this.#log.warn(`Cannot remove block ${bn} from the store since we don't have it`);
|
|
414
544
|
continue;
|
|
415
545
|
}
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
this
|
|
546
|
+
// Load the full block for the returned list when possible, but clean up from the raw row regardless:
|
|
547
|
+
// a block whose body can no longer be fully loaded must still release its row, tx effects, and indices,
|
|
548
|
+
// or later inserts at this number leave stale tx-effect entries pointing into the new chain.
|
|
549
|
+
const block = await this.getBlockFromBlockStorage(bn, blockStorage);
|
|
550
|
+
if (block !== undefined) {
|
|
551
|
+
removedBlocks.push(block);
|
|
552
|
+
} else {
|
|
553
|
+
this.#log.warn(`Removing block ${bn} whose body could not be fully loaded`);
|
|
554
|
+
}
|
|
555
|
+
await this.deleteBlock(bn, blockStorage);
|
|
556
|
+
this.#log.debug(`Removed block ${bn} ${bufferToHex(blockStorage.blockHash)}`);
|
|
419
557
|
}
|
|
420
558
|
return removedBlocks;
|
|
421
559
|
});
|
|
@@ -443,36 +581,117 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
443
581
|
return CheckpointNumber(latestCheckpointNumber);
|
|
444
582
|
}
|
|
445
583
|
async hasProposedCheckpoint() {
|
|
446
|
-
const
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
await this.#proposedCheckpoint.delete();
|
|
584
|
+
const [key] = await toArray(this.#proposedCheckpoints.keysAsync({
|
|
585
|
+
limit: 1
|
|
586
|
+
}));
|
|
587
|
+
return key !== undefined;
|
|
451
588
|
}
|
|
452
|
-
/**
|
|
453
|
-
const
|
|
454
|
-
|
|
455
|
-
await this.#proposedCheckpoint.delete();
|
|
589
|
+
/** Deletes all pending proposed checkpoints from storage. */ async deleteProposedCheckpoints() {
|
|
590
|
+
for await (const key of this.#proposedCheckpoints.keysAsync()){
|
|
591
|
+
await this.#proposedCheckpoints.delete(key);
|
|
456
592
|
}
|
|
457
593
|
}
|
|
458
|
-
/**
|
|
459
|
-
|
|
460
|
-
|
|
594
|
+
/**
|
|
595
|
+
* Promotes a specific pending checkpoint to a confirmed checkpoint entry.
|
|
596
|
+
* This persists the checkpoint to the store, removes only that pending entry, and updates the L1 sync point.
|
|
597
|
+
* Remaining pending entries (e.g. N+1, N+2) are left intact — they chain off the just-promoted one.
|
|
598
|
+
* @param checkpointNumber - The checkpoint number to promote.
|
|
599
|
+
* @param l1 - L1 published data for the checkpoint.
|
|
600
|
+
* @param attestations - Committee attestations.
|
|
601
|
+
* @param expectedArchiveRoot - Archive root guard against races.
|
|
602
|
+
*/ async promoteProposedToCheckpointed(checkpointNumber, l1, attestations, expectedArchiveRoot) {
|
|
603
|
+
return await this.db.transactionAsync(async ()=>{
|
|
604
|
+
const proposed = await this.getProposedCheckpointByNumber(checkpointNumber);
|
|
605
|
+
if (!proposed) {
|
|
606
|
+
throw new NoProposedCheckpointToPromoteError();
|
|
607
|
+
}
|
|
608
|
+
if (!proposed.archive.root.equals(expectedArchiveRoot)) {
|
|
609
|
+
throw new ProposedCheckpointArchiveRootMismatchError(expectedArchiveRoot, proposed.archive.root);
|
|
610
|
+
}
|
|
611
|
+
// Verify sequentiality: promoted checkpoint must follow the latest confirmed one
|
|
612
|
+
const latestCheckpointNumber = await this.getLatestCheckpointNumber();
|
|
613
|
+
if (latestCheckpointNumber !== proposed.checkpointNumber - 1) {
|
|
614
|
+
throw new ProposedCheckpointPromotionNotSequentialError(proposed.checkpointNumber, latestCheckpointNumber);
|
|
615
|
+
}
|
|
616
|
+
// Write the checkpoint entry
|
|
617
|
+
await this.#checkpoints.set(proposed.checkpointNumber, {
|
|
618
|
+
header: proposed.header.toBuffer(),
|
|
619
|
+
archive: proposed.archive.toBuffer(),
|
|
620
|
+
checkpointOutHash: proposed.checkpointOutHash.toBuffer(),
|
|
621
|
+
l1: l1.toBuffer(),
|
|
622
|
+
attestations: attestations.map((attestation)=>attestation.toBuffer()),
|
|
623
|
+
checkpointNumber: proposed.checkpointNumber,
|
|
624
|
+
startBlock: proposed.startBlock,
|
|
625
|
+
blockCount: proposed.blockCount,
|
|
626
|
+
feeAssetPriceModifier: proposed.feeAssetPriceModifier.toString()
|
|
627
|
+
});
|
|
628
|
+
// Update the slot-to-checkpoint index
|
|
629
|
+
await this.#slotToCheckpoint.set(proposed.header.slotNumber, proposed.checkpointNumber);
|
|
630
|
+
// Remove only this pending entry — remaining entries N+1, N+2, ... stay valid
|
|
631
|
+
await this.#proposedCheckpoints.delete(proposed.checkpointNumber);
|
|
632
|
+
// Drop any rejected entry for this archive root: a checkpoint that was previously rejected
|
|
633
|
+
// (e.g. invalid attestations) is now being promoted as valid, so its descendants are allowed.
|
|
634
|
+
await this.removeRejectedCheckpointByArchiveRoot(proposed.archive.root);
|
|
635
|
+
// Update the last synced L1 block
|
|
636
|
+
await this.advanceSynchedL1BlockNumber(l1.blockNumber);
|
|
637
|
+
});
|
|
638
|
+
}
|
|
639
|
+
/**
|
|
640
|
+
* Returns the latest pending checkpoint (highest-numbered entry), or undefined if none.
|
|
641
|
+
* No fallback to confirmed.
|
|
642
|
+
*/ async getLastProposedCheckpoint() {
|
|
643
|
+
const [key] = await toArray(this.#proposedCheckpoints.keysAsync({
|
|
644
|
+
reverse: true,
|
|
645
|
+
limit: 1
|
|
646
|
+
}));
|
|
647
|
+
if (key === undefined) {
|
|
461
648
|
return undefined;
|
|
462
649
|
}
|
|
463
|
-
|
|
650
|
+
const stored = await this.#proposedCheckpoints.getAsync(key);
|
|
651
|
+
return stored ? this.convertToProposedCheckpointData(stored) : undefined;
|
|
652
|
+
}
|
|
653
|
+
/** Returns the pending checkpoint for a specific checkpoint number, or undefined if not found. */ async getProposedCheckpointByNumber(n) {
|
|
654
|
+
const stored = await this.#proposedCheckpoints.getAsync(n);
|
|
655
|
+
return stored ? this.convertToProposedCheckpointData(stored) : undefined;
|
|
656
|
+
}
|
|
657
|
+
/**
|
|
658
|
+
* Returns the pending checkpoint whose header slot matches the given slot, or undefined if not found.
|
|
659
|
+
* Iterates `#proposedCheckpoints` rather than reading an index because the map carries 0–1 entries
|
|
660
|
+
* in normal operation (bounded by the proposer pipelining window). Returns the first match.
|
|
661
|
+
*/ async getProposedCheckpointBySlot(slot) {
|
|
662
|
+
for await (const [, stored] of this.#proposedCheckpoints.entriesAsync()){
|
|
663
|
+
const header = CheckpointHeader.fromBuffer(stored.header);
|
|
664
|
+
if (header.slotNumber === slot) {
|
|
665
|
+
return this.convertToProposedCheckpointData(stored);
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
return undefined;
|
|
669
|
+
}
|
|
670
|
+
/**
|
|
671
|
+
* Evicts all pending checkpoints with checkpoint number >= fromNumber.
|
|
672
|
+
* Used for divergent-mined-checkpoint cleanup: when L1 mines checkpoint N with a different archive,
|
|
673
|
+
* all pending >= N must be evicted since they chain off the now-invalid pending N.
|
|
674
|
+
*/ async evictProposedCheckpointsFrom(fromNumber) {
|
|
675
|
+
const keysToDelete = [];
|
|
676
|
+
for await (const key of this.#proposedCheckpoints.keysAsync()){
|
|
677
|
+
if (key >= fromNumber) {
|
|
678
|
+
keysToDelete.push(key);
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
for (const key of keysToDelete){
|
|
682
|
+
await this.#proposedCheckpoints.delete(key);
|
|
683
|
+
}
|
|
464
684
|
}
|
|
465
685
|
/**
|
|
466
|
-
* Gets the checkpoint at the proposed tip
|
|
467
|
-
* - pending checkpoint if
|
|
686
|
+
* Gets the checkpoint at the proposed tip:
|
|
687
|
+
* - latest pending checkpoint if any exist
|
|
468
688
|
* - fallsback to latest confirmed checkpoint otherwise
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
if (!stored) {
|
|
689
|
+
*/ async getLastCheckpoint() {
|
|
690
|
+
const latest = await this.getLastProposedCheckpoint();
|
|
691
|
+
if (!latest) {
|
|
473
692
|
return this.getCheckpointData(await this.getLatestCheckpointNumber());
|
|
474
693
|
}
|
|
475
|
-
return
|
|
694
|
+
return latest;
|
|
476
695
|
}
|
|
477
696
|
convertToProposedCheckpointData(stored) {
|
|
478
697
|
return {
|
|
@@ -490,7 +709,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
490
709
|
* Attempts to get the proposedCheckpoint's number, if there is not one, then fallback to the latest confirmed checkpoint number.
|
|
491
710
|
* @returns CheckpointNumber
|
|
492
711
|
*/ async getProposedCheckpointNumber() {
|
|
493
|
-
const proposed = await this.
|
|
712
|
+
const proposed = await this.getLastCheckpoint();
|
|
494
713
|
if (!proposed) {
|
|
495
714
|
return await this.getLatestCheckpointNumber();
|
|
496
715
|
}
|
|
@@ -500,134 +719,32 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
500
719
|
* Attempts to get the proposedCheckpoint's block number, if there is not one, then fallback to the checkpointed block number
|
|
501
720
|
* @returns BlockNumber
|
|
502
721
|
*/ async getProposedCheckpointL2BlockNumber() {
|
|
503
|
-
const proposed = await this.
|
|
722
|
+
const proposed = await this.getLastCheckpoint();
|
|
504
723
|
if (!proposed) {
|
|
505
724
|
return await this.getCheckpointedL2BlockNumber();
|
|
506
725
|
}
|
|
507
726
|
return BlockNumber(proposed.startBlock + proposed.blockCount - 1);
|
|
508
727
|
}
|
|
509
|
-
async
|
|
510
|
-
const
|
|
511
|
-
|
|
512
|
-
return undefined;
|
|
513
|
-
}
|
|
514
|
-
const checkpoint = await this.#checkpoints.getAsync(blockStorage.checkpointNumber);
|
|
515
|
-
if (!checkpoint) {
|
|
516
|
-
return undefined;
|
|
517
|
-
}
|
|
518
|
-
const block = await this.getBlockFromBlockStorage(number, blockStorage);
|
|
519
|
-
if (!block) {
|
|
520
|
-
return undefined;
|
|
521
|
-
}
|
|
522
|
-
return new CheckpointedL2Block(CheckpointNumber(checkpoint.checkpointNumber), block, L1PublishedData.fromBuffer(checkpoint.l1), checkpoint.attestations.map((buf)=>CommitteeAttestation.fromBuffer(buf)));
|
|
523
|
-
}
|
|
524
|
-
/**
|
|
525
|
-
* Gets up to `limit` amount of Checkpointed L2 blocks starting from `from`.
|
|
526
|
-
* @param start - Number of the first block to return (inclusive).
|
|
527
|
-
* @param limit - The number of blocks to return.
|
|
528
|
-
* @returns The requested L2 blocks
|
|
529
|
-
*/ async *getCheckpointedBlocks(start, limit) {
|
|
530
|
-
const checkpointCache = new Map();
|
|
531
|
-
for await (const [blockNumber, blockStorage] of this.getBlockStorages(start, limit)){
|
|
532
|
-
const block = await this.getBlockFromBlockStorage(blockNumber, blockStorage);
|
|
533
|
-
if (block) {
|
|
534
|
-
const checkpoint = checkpointCache.get(CheckpointNumber(blockStorage.checkpointNumber)) ?? await this.#checkpoints.getAsync(blockStorage.checkpointNumber);
|
|
535
|
-
if (checkpoint) {
|
|
536
|
-
checkpointCache.set(CheckpointNumber(blockStorage.checkpointNumber), checkpoint);
|
|
537
|
-
const checkpointedBlock = new CheckpointedL2Block(CheckpointNumber(checkpoint.checkpointNumber), block, L1PublishedData.fromBuffer(checkpoint.l1), checkpoint.attestations.map((buf)=>CommitteeAttestation.fromBuffer(buf)));
|
|
538
|
-
yield checkpointedBlock;
|
|
539
|
-
}
|
|
540
|
-
}
|
|
541
|
-
}
|
|
728
|
+
/** Returns the checkpoint number that contains the given slot (or undefined if not found). */ async getCheckpointNumberBySlot(slot) {
|
|
729
|
+
const checkpointNumber = await this.#slotToCheckpoint.getAsync(slot);
|
|
730
|
+
return checkpointNumber === undefined ? undefined : CheckpointNumber(checkpointNumber);
|
|
542
731
|
}
|
|
543
|
-
async
|
|
544
|
-
const blockNumber = await this
|
|
732
|
+
/** Gets a single L2 block matching the given resolved query. */ async getBlock(query) {
|
|
733
|
+
const blockNumber = await this.getBlockNumber(query);
|
|
545
734
|
if (blockNumber === undefined) {
|
|
546
735
|
return undefined;
|
|
547
736
|
}
|
|
548
|
-
return this.getCheckpointedBlock(BlockNumber(blockNumber));
|
|
549
|
-
}
|
|
550
|
-
async getCheckpointedBlockByArchive(archive) {
|
|
551
|
-
const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
|
|
552
|
-
if (blockNumber === undefined) {
|
|
553
|
-
return undefined;
|
|
554
|
-
}
|
|
555
|
-
return this.getCheckpointedBlock(BlockNumber(blockNumber));
|
|
556
|
-
}
|
|
557
|
-
/**
|
|
558
|
-
* Gets up to `limit` amount of L2 blocks starting from `from`.
|
|
559
|
-
* @param start - Number of the first block to return (inclusive).
|
|
560
|
-
* @param limit - The number of blocks to return.
|
|
561
|
-
* @returns The requested L2 blocks
|
|
562
|
-
*/ async *getBlocks(start, limit) {
|
|
563
|
-
for await (const [blockNumber, blockStorage] of this.getBlockStorages(start, limit)){
|
|
564
|
-
const block = await this.getBlockFromBlockStorage(blockNumber, blockStorage);
|
|
565
|
-
if (block) {
|
|
566
|
-
yield block;
|
|
567
|
-
}
|
|
568
|
-
}
|
|
569
|
-
}
|
|
570
|
-
/**
|
|
571
|
-
* Gets block metadata (without tx data) by block number.
|
|
572
|
-
* @param blockNumber - The number of the block to return.
|
|
573
|
-
* @returns The requested block data.
|
|
574
|
-
*/ async getBlockData(blockNumber) {
|
|
575
737
|
const blockStorage = await this.#blocks.getAsync(blockNumber);
|
|
576
|
-
if (!blockStorage
|
|
577
|
-
return undefined;
|
|
578
|
-
}
|
|
579
|
-
return this.getBlockDataFromBlockStorage(blockStorage);
|
|
580
|
-
}
|
|
581
|
-
/**
|
|
582
|
-
* Gets block metadata (without tx data) by archive root.
|
|
583
|
-
* @param archive - The archive root of the block to return.
|
|
584
|
-
* @returns The requested block data.
|
|
585
|
-
*/ async getBlockDataByArchive(archive) {
|
|
586
|
-
const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
|
|
587
|
-
if (blockNumber === undefined) {
|
|
738
|
+
if (!blockStorage) {
|
|
588
739
|
return undefined;
|
|
589
740
|
}
|
|
590
|
-
return this.getBlockData(BlockNumber(blockNumber));
|
|
591
|
-
}
|
|
592
|
-
/**
|
|
593
|
-
* Gets an L2 block.
|
|
594
|
-
* @param blockNumber - The number of the block to return.
|
|
595
|
-
* @returns The requested L2 block.
|
|
596
|
-
*/ async getBlock(blockNumber) {
|
|
597
|
-
const blockStorage = await this.#blocks.getAsync(blockNumber);
|
|
598
|
-
if (!blockStorage || !blockStorage.header) {
|
|
599
|
-
return Promise.resolve(undefined);
|
|
600
|
-
}
|
|
601
741
|
return this.getBlockFromBlockStorage(blockNumber, blockStorage);
|
|
602
742
|
}
|
|
603
|
-
/**
|
|
604
|
-
|
|
605
|
-
* @param blockHash - The hash of the block to return.
|
|
606
|
-
* @returns The requested L2 block.
|
|
607
|
-
*/ async getBlockByHash(blockHash) {
|
|
608
|
-
const blockNumber = await this.#blockHashIndex.getAsync(blockHash.toString());
|
|
609
|
-
if (blockNumber === undefined) {
|
|
610
|
-
return undefined;
|
|
611
|
-
}
|
|
612
|
-
return this.getBlock(BlockNumber(blockNumber));
|
|
613
|
-
}
|
|
614
|
-
/**
|
|
615
|
-
* Gets an L2 block by its archive root.
|
|
616
|
-
* @param archive - The archive root of the block to return.
|
|
617
|
-
* @returns The requested L2 block.
|
|
618
|
-
*/ async getBlockByArchive(archive) {
|
|
619
|
-
const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
|
|
620
|
-
if (blockNumber === undefined) {
|
|
621
|
-
return undefined;
|
|
622
|
-
}
|
|
623
|
-
return this.getBlock(BlockNumber(blockNumber));
|
|
743
|
+
/** Gets a collection of L2 blocks for a resolved range. */ getBlocks(query) {
|
|
744
|
+
return toArray(this.iterateBlocks(query));
|
|
624
745
|
}
|
|
625
|
-
/**
|
|
626
|
-
|
|
627
|
-
* @param blockHash - The hash of the block to return.
|
|
628
|
-
* @returns The requested block header.
|
|
629
|
-
*/ async getBlockHeaderByHash(blockHash) {
|
|
630
|
-
const blockNumber = await this.#blockHashIndex.getAsync(blockHash.toString());
|
|
746
|
+
/** Gets single block metadata matching the given resolved query. */ async getBlockData(query) {
|
|
747
|
+
const blockNumber = await this.getBlockNumber(query);
|
|
631
748
|
if (blockNumber === undefined) {
|
|
632
749
|
return undefined;
|
|
633
750
|
}
|
|
@@ -635,35 +752,30 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
635
752
|
if (!blockStorage || !blockStorage.header) {
|
|
636
753
|
return undefined;
|
|
637
754
|
}
|
|
638
|
-
return
|
|
755
|
+
return this.getBlockDataFromBlockStorage(blockStorage);
|
|
639
756
|
}
|
|
640
|
-
/**
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
const blockNumber
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
757
|
+
/** Gets a collection of block metadata entries for a resolved range. */ getBlocksData(query) {
|
|
758
|
+
return toArray(this.iterateBlocksData(query));
|
|
759
|
+
}
|
|
760
|
+
/** Async iterator over L2 blocks for a resolved range. */ async *iterateBlocks(query) {
|
|
761
|
+
const cap = query.onlyCheckpointed ? await this.getCheckpointedL2BlockNumber() : undefined;
|
|
762
|
+
for await (const [blockNumber, blockStorage] of this.getBlockStorages(query.from, query.limit)){
|
|
763
|
+
if (cap !== undefined && blockNumber > cap) {
|
|
764
|
+
break;
|
|
765
|
+
}
|
|
766
|
+
const block = await this.getBlockFromBlockStorage(blockNumber, blockStorage);
|
|
767
|
+
if (block) {
|
|
768
|
+
yield block;
|
|
769
|
+
}
|
|
652
770
|
}
|
|
653
|
-
return BlockHeader.fromBuffer(blockStorage.header);
|
|
654
771
|
}
|
|
655
|
-
/**
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
*/ async *getBlockHeaders(start, limit) {
|
|
661
|
-
for await (const [blockNumber, blockStorage] of this.getBlockStorages(start, limit)){
|
|
662
|
-
const header = BlockHeader.fromBuffer(blockStorage.header);
|
|
663
|
-
if (header.getBlockNumber() !== blockNumber) {
|
|
664
|
-
throw new Error(`Block number mismatch when retrieving block header from archive (expected ${blockNumber} but got ${header.getBlockNumber()})`);
|
|
772
|
+
/** Async iterator over block metadata for a resolved range. */ async *iterateBlocksData(query) {
|
|
773
|
+
const cap = query.onlyCheckpointed ? await this.getCheckpointedL2BlockNumber() : undefined;
|
|
774
|
+
for await (const [blockNumber, blockStorage] of this.getBlockStorages(query.from, query.limit)){
|
|
775
|
+
if (cap !== undefined && blockNumber > cap) {
|
|
776
|
+
break;
|
|
665
777
|
}
|
|
666
|
-
yield
|
|
778
|
+
yield this.getBlockDataFromBlockStorage(blockStorage);
|
|
667
779
|
}
|
|
668
780
|
}
|
|
669
781
|
async *getBlockStorages(start, limit) {
|
|
@@ -679,11 +791,27 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
679
791
|
];
|
|
680
792
|
}
|
|
681
793
|
}
|
|
794
|
+
/** Resolves a ResolvedBlockQuery discriminant to a block number, or undefined if not found. */ async getBlockNumber(query) {
|
|
795
|
+
let blockNumber;
|
|
796
|
+
if ('number' in query) {
|
|
797
|
+
blockNumber = query.number;
|
|
798
|
+
} else if ('hash' in query) {
|
|
799
|
+
const n = await this.#blockHashIndex.getAsync(query.hash.toString());
|
|
800
|
+
blockNumber = n !== undefined ? BlockNumber(n) : undefined;
|
|
801
|
+
} else {
|
|
802
|
+
const n = await this.#blockArchiveIndex.getAsync(query.archive.toString());
|
|
803
|
+
blockNumber = n !== undefined ? BlockNumber(n) : undefined;
|
|
804
|
+
}
|
|
805
|
+
if (blockNumber === undefined) {
|
|
806
|
+
return undefined;
|
|
807
|
+
}
|
|
808
|
+
return blockNumber;
|
|
809
|
+
}
|
|
682
810
|
getBlockDataFromBlockStorage(blockStorage) {
|
|
683
811
|
return {
|
|
684
812
|
header: BlockHeader.fromBuffer(blockStorage.header),
|
|
685
813
|
archive: AppendOnlyTreeSnapshot.fromBuffer(blockStorage.archive),
|
|
686
|
-
blockHash:
|
|
814
|
+
blockHash: BlockHash.fromBuffer(blockStorage.blockHash),
|
|
687
815
|
checkpointNumber: CheckpointNumber(blockStorage.checkpointNumber),
|
|
688
816
|
indexWithinCheckpoint: IndexWithinCheckpoint(blockStorage.indexWithinCheckpoint)
|
|
689
817
|
};
|
|
@@ -727,36 +855,6 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
727
855
|
return deserializeIndexedTxEffect(buffer);
|
|
728
856
|
}
|
|
729
857
|
/**
|
|
730
|
-
* Gets a receipt of a settled tx.
|
|
731
|
-
* @param txHash - The hash of a tx we try to get the receipt for.
|
|
732
|
-
* @returns The requested tx receipt (or undefined if not found).
|
|
733
|
-
*/ async getSettledTxReceipt(txHash, l1Constants) {
|
|
734
|
-
const txEffect = await this.getTxEffect(txHash);
|
|
735
|
-
if (!txEffect) {
|
|
736
|
-
return undefined;
|
|
737
|
-
}
|
|
738
|
-
const blockNumber = BlockNumber(txEffect.l2BlockNumber);
|
|
739
|
-
// Use existing archiver methods to determine finalization level
|
|
740
|
-
const [provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber, blockData] = await Promise.all([
|
|
741
|
-
this.getProvenBlockNumber(),
|
|
742
|
-
this.getCheckpointedL2BlockNumber(),
|
|
743
|
-
this.getFinalizedL2BlockNumber(),
|
|
744
|
-
this.getBlockData(blockNumber)
|
|
745
|
-
]);
|
|
746
|
-
let status;
|
|
747
|
-
if (blockNumber <= finalizedBlockNumber) {
|
|
748
|
-
status = TxStatus.FINALIZED;
|
|
749
|
-
} else if (blockNumber <= provenBlockNumber) {
|
|
750
|
-
status = TxStatus.PROVEN;
|
|
751
|
-
} else if (blockNumber <= checkpointedBlockNumber) {
|
|
752
|
-
status = TxStatus.CHECKPOINTED;
|
|
753
|
-
} else {
|
|
754
|
-
status = TxStatus.PROPOSED;
|
|
755
|
-
}
|
|
756
|
-
const epochNumber = blockData && l1Constants ? getEpochAtSlot(blockData.header.globalVariables.slotNumber, l1Constants) : undefined;
|
|
757
|
-
return new TxReceipt(txHash, status, TxReceipt.executionResultFromRevertCode(txEffect.data.revertCode), undefined, txEffect.data.transactionFee.toBigInt(), txEffect.l2BlockHash, blockNumber, epochNumber);
|
|
758
|
-
}
|
|
759
|
-
/**
|
|
760
858
|
* Looks up which block included the requested tx effect.
|
|
761
859
|
* @param txHash - The txHash of the tx.
|
|
762
860
|
* @returns The block number and index of the tx.
|
|
@@ -765,13 +863,50 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
765
863
|
if (!txEffect) {
|
|
766
864
|
return undefined;
|
|
767
865
|
}
|
|
768
|
-
|
|
866
|
+
// Read only the IndexedTxEffect header (`blockHash(32) + l2BlockNumber(4) + txIndexInBlock(4)`); the
|
|
867
|
+
// large tail (the full TxEffect with logs etc.) is irrelevant here.
|
|
868
|
+
const view = Buffer.from(txEffect.buffer, txEffect.byteOffset, txEffect.byteLength);
|
|
869
|
+
const l2BlockNumber = view.readUInt32BE(32);
|
|
870
|
+
const txIndexInBlock = view.readUInt32BE(36);
|
|
769
871
|
return [
|
|
770
872
|
l2BlockNumber,
|
|
771
873
|
txIndexInBlock
|
|
772
874
|
];
|
|
773
875
|
}
|
|
774
876
|
/**
|
|
877
|
+
* Batched, partial deserializer that fetches `noteHashes` and `nullifiers` (all of them) for the given
|
|
878
|
+
* txs. For each input txHash, returns a `[noteHashes, nullifiers]` tuple. Returns `[[], []]` for any
|
|
879
|
+
* unknown txHash. Preserves input order. Used by the log read path when `includeEffects` is set to
|
|
880
|
+
* attach effect data on demand without paying for a full {@link TxEffect} deserialization.
|
|
881
|
+
*
|
|
882
|
+
* The on-disk `IndexedTxEffect` layout starts with a fixed-length header
|
|
883
|
+
* (`blockHash(32) + l2BlockNumber(4) + txIndexInBlock(4) + slotNumber(4) + revertCode(1) + txHash(32) +
|
|
884
|
+
* transactionFee(32)` = 109 bytes), followed by `noteHashes` and `nullifiers` (both u8-length-prefixed `Fr`
|
|
885
|
+
* vectors). We
|
|
886
|
+
* skip the header, then read the two vectors, and stop — the large tail (`l2ToL1Msgs`,
|
|
887
|
+
* `publicDataWrites`, `privateLogs`, `publicLogs`, `contractClassLogs`) is never touched.
|
|
888
|
+
*/ getNoteHashesAndNullifiers(txHashes) {
|
|
889
|
+
return Promise.all(txHashes.map(async (txHash)=>{
|
|
890
|
+
const buffer = await this.#txEffects.getAsync(txHash.toString());
|
|
891
|
+
if (!buffer) {
|
|
892
|
+
return [
|
|
893
|
+
[],
|
|
894
|
+
[]
|
|
895
|
+
];
|
|
896
|
+
}
|
|
897
|
+
const reader = BufferReader.asReader(buffer);
|
|
898
|
+
// Skip the fixed-length header: blockHash + l2BlockNumber + txIndexInBlock + slotNumber + revertCode +
|
|
899
|
+
// txHash + transactionFee.
|
|
900
|
+
reader.readBytes(32 + 4 + 4 + 4 + 1 + 32 + 32);
|
|
901
|
+
const noteHashes = reader.readVectorUint8Prefix(Fr);
|
|
902
|
+
const nullifiers = reader.readVectorUint8Prefix(Fr);
|
|
903
|
+
return [
|
|
904
|
+
noteHashes,
|
|
905
|
+
nullifiers
|
|
906
|
+
];
|
|
907
|
+
}));
|
|
908
|
+
}
|
|
909
|
+
/**
|
|
775
910
|
* Looks up which block deployed a particular contract.
|
|
776
911
|
* @param contractAddress - The address of the contract to look up.
|
|
777
912
|
* @returns The block number and index of the contract.
|
|
@@ -797,6 +932,142 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
797
932
|
return typeof lastBlockNumber === 'number' ? BlockNumber(lastBlockNumber) : BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
798
933
|
}
|
|
799
934
|
/**
|
|
935
|
+
* Resolves all four L2 chain tips (proposed, checkpointed, proven, finalized) in a single
|
|
936
|
+
* read-only transaction so the snapshot is internally consistent. Each underlying record is
|
|
937
|
+
* read at most once: latest block and latest confirmed checkpoint are loaded directly (no
|
|
938
|
+
* separate "find the number, then look up data" hop), the proven/finalized checkpoint
|
|
939
|
+
* singletons are read once and their storage entries are reused if they coincide with the
|
|
940
|
+
* latest checkpoint, and per-tip block hashes are deduped when two tips land on the same block
|
|
941
|
+
* (e.g. finalized == proven).
|
|
942
|
+
*
|
|
943
|
+
* The result is guaranteed to satisfy `finalized <= proven <= checkpointed <= proposed` (by
|
|
944
|
+
* block number). Genesis is represented by `(INITIAL_L2_BLOCK_NUM - 1)` and the supplied
|
|
945
|
+
* `genesisBlockHash`, paired with the synthetic genesis checkpoint id.
|
|
946
|
+
*
|
|
947
|
+
* @param genesisBlockHash - Block hash to report for the synthetic pre-initial block (used when
|
|
948
|
+
* a tip is still at genesis).
|
|
949
|
+
*/ async getL2TipsData(genesisBlockHash) {
|
|
950
|
+
return await this.db.transactionAsync(async ()=>{
|
|
951
|
+
// Define genesis tips
|
|
952
|
+
const genesisBlockNumber = BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
953
|
+
const genesisCheckpointNumber = CheckpointNumber(INITIAL_CHECKPOINT_NUMBER - 1);
|
|
954
|
+
const genesisBlockId = {
|
|
955
|
+
number: genesisBlockNumber,
|
|
956
|
+
hash: genesisBlockHash.toString()
|
|
957
|
+
};
|
|
958
|
+
const genesisCheckpointId = {
|
|
959
|
+
number: genesisCheckpointNumber,
|
|
960
|
+
hash: GENESIS_CHECKPOINT_HEADER_HASH.toString()
|
|
961
|
+
};
|
|
962
|
+
const genesisTip = {
|
|
963
|
+
block: genesisBlockId,
|
|
964
|
+
checkpoint: genesisCheckpointId
|
|
965
|
+
};
|
|
966
|
+
// Load latest block and checkpoint entries
|
|
967
|
+
const [latestBlockEntry] = await toArray(this.#blocks.entriesAsync({
|
|
968
|
+
reverse: true,
|
|
969
|
+
limit: 1
|
|
970
|
+
}));
|
|
971
|
+
const [latestCheckpointEntry] = await toArray(this.#checkpoints.entriesAsync({
|
|
972
|
+
reverse: true,
|
|
973
|
+
limit: 1
|
|
974
|
+
}));
|
|
975
|
+
const latestCheckpointNumber = latestCheckpointEntry ? CheckpointNumber(latestCheckpointEntry[0]) : genesisCheckpointNumber;
|
|
976
|
+
// Load proven and finalized checkpoint number pointers
|
|
977
|
+
const [provenRaw, finalizedRaw] = await Promise.all([
|
|
978
|
+
this.#lastProvenCheckpoint.getAsync(),
|
|
979
|
+
this.#lastFinalizedCheckpoint.getAsync()
|
|
980
|
+
]);
|
|
981
|
+
// Clamp to enforce finalized <= proven <= checkpointed.
|
|
982
|
+
const provenCheckpointNumber = CheckpointNumber(Math.min(provenRaw ?? 0, latestCheckpointNumber));
|
|
983
|
+
const finalizedCheckpointNumber = CheckpointNumber(Math.min(finalizedRaw ?? 0, provenCheckpointNumber));
|
|
984
|
+
// Avoid loading the same checkpoint more than once
|
|
985
|
+
const checkpointStorageCache = new Map();
|
|
986
|
+
if (latestCheckpointEntry) {
|
|
987
|
+
checkpointStorageCache.set(CheckpointNumber(latestCheckpointEntry[0]), latestCheckpointEntry[1]);
|
|
988
|
+
}
|
|
989
|
+
const loadCheckpointStorage = async (n)=>{
|
|
990
|
+
if (n === 0) {
|
|
991
|
+
return undefined;
|
|
992
|
+
}
|
|
993
|
+
if (!checkpointStorageCache.has(n)) {
|
|
994
|
+
const checkpointStorage = await this.#checkpoints.getAsync(n);
|
|
995
|
+
if (!checkpointStorage) {
|
|
996
|
+
throw new CheckpointNotFoundError(n);
|
|
997
|
+
}
|
|
998
|
+
checkpointStorageCache.set(n, checkpointStorage);
|
|
999
|
+
}
|
|
1000
|
+
return checkpointStorageCache.get(n);
|
|
1001
|
+
};
|
|
1002
|
+
// Load proven and finalized checkpoint storage entries
|
|
1003
|
+
const provenCheckpoint = await loadCheckpointStorage(provenCheckpointNumber);
|
|
1004
|
+
const finalizedCheckpoint = await loadCheckpointStorage(finalizedCheckpointNumber);
|
|
1005
|
+
// Avoid loading the same block hash multiple times when tips land on the same block
|
|
1006
|
+
const blockHashCache = new Map();
|
|
1007
|
+
blockHashCache.set(genesisBlockNumber, genesisBlockHash.toString());
|
|
1008
|
+
if (latestBlockEntry) {
|
|
1009
|
+
blockHashCache.set(latestBlockEntry[0], BlockHash.fromBuffer(latestBlockEntry[1].blockHash).toString());
|
|
1010
|
+
}
|
|
1011
|
+
const loadBlockHash = async (n)=>{
|
|
1012
|
+
if (!blockHashCache.has(n)) {
|
|
1013
|
+
const blockStorage = await this.#blocks.getAsync(n);
|
|
1014
|
+
if (!blockStorage) {
|
|
1015
|
+
throw new BlockNotFoundError(n);
|
|
1016
|
+
}
|
|
1017
|
+
const blockHash = BlockHash.fromBuffer(blockStorage.blockHash).toString();
|
|
1018
|
+
blockHashCache.set(n, blockHash);
|
|
1019
|
+
}
|
|
1020
|
+
return blockHashCache.get(n);
|
|
1021
|
+
};
|
|
1022
|
+
// Build proposed chain tip (this one has block only, no checkpoint)
|
|
1023
|
+
const proposedBlockId = latestBlockEntry === undefined ? genesisBlockId : {
|
|
1024
|
+
number: BlockNumber(latestBlockEntry[0]),
|
|
1025
|
+
hash: BlockHash.fromBuffer(latestBlockEntry[1].blockHash).toString()
|
|
1026
|
+
};
|
|
1027
|
+
// Build other tips from checkpoint data, reading corresponding block data from the cache
|
|
1028
|
+
const buildTipFromCheckpoint = async (stored)=>{
|
|
1029
|
+
if (!stored) {
|
|
1030
|
+
return genesisTip;
|
|
1031
|
+
}
|
|
1032
|
+
const blockNumber = BlockNumber(stored.startBlock + stored.blockCount - 1);
|
|
1033
|
+
const blockHash = await loadBlockHash(blockNumber);
|
|
1034
|
+
const header = CheckpointHeader.fromBuffer(stored.header);
|
|
1035
|
+
return {
|
|
1036
|
+
block: {
|
|
1037
|
+
number: blockNumber,
|
|
1038
|
+
hash: blockHash
|
|
1039
|
+
},
|
|
1040
|
+
checkpoint: {
|
|
1041
|
+
number: CheckpointNumber(stored.checkpointNumber),
|
|
1042
|
+
hash: header.hash().toString()
|
|
1043
|
+
}
|
|
1044
|
+
};
|
|
1045
|
+
};
|
|
1046
|
+
const checkpointedTip = await buildTipFromCheckpoint(latestCheckpointEntry?.[1]);
|
|
1047
|
+
const provenTip = await buildTipFromCheckpoint(provenCheckpoint);
|
|
1048
|
+
const finalizedTip = await buildTipFromCheckpoint(finalizedCheckpoint);
|
|
1049
|
+
// A checkpointed block past the latest stored block would mean a checkpoint
|
|
1050
|
+
// references blocks that aren't in blocks.
|
|
1051
|
+
if (proposedBlockId.number < checkpointedTip.block.number) {
|
|
1052
|
+
throw new Error(`Inconsistent block store: latest block ${proposedBlockId.number} is behind checkpointed block ${checkpointedTip.block.number}`);
|
|
1053
|
+
}
|
|
1054
|
+
// Assert that checkpoint numbers are increasing
|
|
1055
|
+
if (finalizedTip.checkpoint.number > provenTip.checkpoint.number || provenTip.checkpoint.number > checkpointedTip.checkpoint.number) {
|
|
1056
|
+
throw new Error(`Inconsistent checkpoint numbers in chain tips: finalized=${finalizedTip.checkpoint.number} proven=${provenTip.checkpoint.number} checkpointed=${checkpointedTip.checkpoint.number}`);
|
|
1057
|
+
}
|
|
1058
|
+
// Assert block numbers are increasing
|
|
1059
|
+
if (finalizedTip.block.number > provenTip.block.number || provenTip.block.number > checkpointedTip.block.number || checkpointedTip.block.number > proposedBlockId.number) {
|
|
1060
|
+
throw new Error(`Inconsistent block numbers in chain tips: finalized=${finalizedTip.block.number} proven=${provenTip.block.number} checkpointed=${checkpointedTip.block.number} proposed=${proposedBlockId.number}`);
|
|
1061
|
+
}
|
|
1062
|
+
return {
|
|
1063
|
+
proposed: proposedBlockId,
|
|
1064
|
+
checkpointed: checkpointedTip,
|
|
1065
|
+
proven: provenTip,
|
|
1066
|
+
finalized: finalizedTip
|
|
1067
|
+
};
|
|
1068
|
+
});
|
|
1069
|
+
}
|
|
1070
|
+
/**
|
|
800
1071
|
* Gets the most recent L1 block processed.
|
|
801
1072
|
* @returns The L1 block that published the latest L2 block
|
|
802
1073
|
*/ getSynchedL1BlockNumber() {
|
|
@@ -805,22 +1076,29 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
805
1076
|
setSynchedL1BlockNumber(l1BlockNumber) {
|
|
806
1077
|
return this.#lastSynchedL1Block.set(l1BlockNumber);
|
|
807
1078
|
}
|
|
808
|
-
/**
|
|
809
|
-
*
|
|
1079
|
+
/**
|
|
1080
|
+
* Adds a proposed checkpoint to the pending queue.
|
|
1081
|
+
* Accepts proposed.checkpointNumber === latestTip + 1, where latestTip is the highest of
|
|
1082
|
+
* confirmed and the highest pending checkpoint number.
|
|
1083
|
+
* Computes archive and checkpointOutHash from the stored blocks.
|
|
1084
|
+
*/ async addProposedCheckpoint(proposed) {
|
|
810
1085
|
return await this.db.transactionAsync(async ()=>{
|
|
811
|
-
const current = await this.getProposedCheckpointNumber();
|
|
812
|
-
if (proposed.checkpointNumber <= current) {
|
|
813
|
-
throw new ProposedCheckpointStaleError(proposed.checkpointNumber, current);
|
|
814
|
-
}
|
|
815
1086
|
const confirmed = await this.getLatestCheckpointNumber();
|
|
816
|
-
|
|
817
|
-
|
|
1087
|
+
const [latestPendingKey] = await toArray(this.#proposedCheckpoints.keysAsync({
|
|
1088
|
+
reverse: true,
|
|
1089
|
+
limit: 1
|
|
1090
|
+
}));
|
|
1091
|
+
const latestTip = CheckpointNumber(latestPendingKey !== undefined ? Math.max(latestPendingKey, confirmed) : confirmed);
|
|
1092
|
+
if (proposed.checkpointNumber !== latestTip + 1) {
|
|
1093
|
+
throw new ProposedCheckpointNotSequentialError(proposed.checkpointNumber, latestTip);
|
|
818
1094
|
}
|
|
819
|
-
// Ensure the
|
|
1095
|
+
// Ensure the predecessor block (from pending or confirmed chain) exists
|
|
820
1096
|
const previousBlock = await this.getPreviousCheckpointBlock(proposed.checkpointNumber);
|
|
821
1097
|
const blocks = [];
|
|
822
1098
|
for(let i = 0; i < proposed.blockCount; i++){
|
|
823
|
-
const block = await this.getBlock(
|
|
1099
|
+
const block = await this.getBlock({
|
|
1100
|
+
number: BlockNumber(proposed.startBlock + i)
|
|
1101
|
+
});
|
|
824
1102
|
if (!block) {
|
|
825
1103
|
throw new BlockNotFoundError(proposed.startBlock + i);
|
|
826
1104
|
}
|
|
@@ -829,7 +1107,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
829
1107
|
this.validateCheckpointBlocks(blocks, previousBlock);
|
|
830
1108
|
const archive = blocks[blocks.length - 1].archive;
|
|
831
1109
|
const checkpointOutHash = Checkpoint.getCheckpointOutHash(blocks);
|
|
832
|
-
await this.#
|
|
1110
|
+
await this.#proposedCheckpoints.set(proposed.checkpointNumber, {
|
|
833
1111
|
header: proposed.header.toBuffer(),
|
|
834
1112
|
archive: archive.toBuffer(),
|
|
835
1113
|
checkpointOutHash: checkpointOutHash.toBuffer(),
|
|
@@ -842,22 +1120,26 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
842
1120
|
});
|
|
843
1121
|
}
|
|
844
1122
|
async getProvenCheckpointNumber() {
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
1123
|
+
return await this.db.transactionAsync(async ()=>{
|
|
1124
|
+
const [latestCheckpointNumber, provenCheckpointNumber] = await Promise.all([
|
|
1125
|
+
this.getLatestCheckpointNumber(),
|
|
1126
|
+
this.#lastProvenCheckpoint.getAsync()
|
|
1127
|
+
]);
|
|
1128
|
+
return (provenCheckpointNumber ?? 0) > latestCheckpointNumber ? latestCheckpointNumber : CheckpointNumber(provenCheckpointNumber ?? 0);
|
|
1129
|
+
});
|
|
850
1130
|
}
|
|
851
1131
|
async setProvenCheckpointNumber(checkpointNumber) {
|
|
852
1132
|
const result = await this.#lastProvenCheckpoint.set(checkpointNumber);
|
|
853
1133
|
return result;
|
|
854
1134
|
}
|
|
855
1135
|
async getFinalizedCheckpointNumber() {
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
1136
|
+
return await this.db.transactionAsync(async ()=>{
|
|
1137
|
+
const [provenCheckpointNumber, finalizedCheckpointNumber] = await Promise.all([
|
|
1138
|
+
this.getProvenCheckpointNumber(),
|
|
1139
|
+
this.#lastFinalizedCheckpoint.getAsync()
|
|
1140
|
+
]);
|
|
1141
|
+
return (finalizedCheckpointNumber ?? 0) > provenCheckpointNumber ? provenCheckpointNumber : CheckpointNumber(finalizedCheckpointNumber ?? 0);
|
|
1142
|
+
});
|
|
861
1143
|
}
|
|
862
1144
|
setFinalizedCheckpointNumber(checkpointNumber) {
|
|
863
1145
|
return this.#lastFinalizedCheckpoint.set(checkpointNumber);
|
|
@@ -895,4 +1177,70 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
895
1177
|
await this.#pendingChainValidationStatus.delete();
|
|
896
1178
|
}
|
|
897
1179
|
}
|
|
1180
|
+
/** Records a rejected-checkpoint entry, keyed by its own archive root. */ async addRejectedCheckpoint(entry) {
|
|
1181
|
+
const archiveRootHex = entry.archiveRoot.toString();
|
|
1182
|
+
await this.#rejectedCheckpoints.set(archiveRootHex, {
|
|
1183
|
+
checkpointNumber: entry.checkpointNumber,
|
|
1184
|
+
archiveRoot: entry.archiveRoot.toBuffer(),
|
|
1185
|
+
parentArchiveRoot: entry.parentArchiveRoot.toBuffer(),
|
|
1186
|
+
slotNumber: entry.slotNumber,
|
|
1187
|
+
l1: entry.l1.toBuffer(),
|
|
1188
|
+
reason: entry.reason
|
|
1189
|
+
});
|
|
1190
|
+
await this.#rejectedCheckpointsByNumber.set(entry.checkpointNumber, archiveRootHex);
|
|
1191
|
+
await this.advanceSynchedL1BlockNumber(entry.l1.blockNumber);
|
|
1192
|
+
}
|
|
1193
|
+
/** Returns the rejected-checkpoint entry with the given archive root, or undefined if not present. */ async getRejectedCheckpointByArchiveRoot(archiveRoot) {
|
|
1194
|
+
const stored = await this.#rejectedCheckpoints.getAsync(archiveRoot.toString());
|
|
1195
|
+
return stored ? this.rejectedCheckpointFromStorage(stored) : undefined;
|
|
1196
|
+
}
|
|
1197
|
+
/** Returns the rejected-checkpoint entry recorded for the given checkpoint number, or undefined if none. */ async getRejectedCheckpointByNumber(checkpointNumber) {
|
|
1198
|
+
const archiveRootHex = await this.#rejectedCheckpointsByNumber.getAsync(checkpointNumber);
|
|
1199
|
+
if (archiveRootHex === undefined) {
|
|
1200
|
+
return undefined;
|
|
1201
|
+
}
|
|
1202
|
+
const stored = await this.#rejectedCheckpoints.getAsync(archiveRootHex);
|
|
1203
|
+
return stored ? this.rejectedCheckpointFromStorage(stored) : undefined;
|
|
1204
|
+
}
|
|
1205
|
+
/** Returns the highest checkpoint number recorded across all rejected entries, or `INITIAL_CHECKPOINT_NUMBER - 1` if none. */ async getLatestRejectedCheckpointNumber() {
|
|
1206
|
+
const [latest] = await toArray(this.#rejectedCheckpointsByNumber.keysAsync({
|
|
1207
|
+
reverse: true,
|
|
1208
|
+
limit: 1
|
|
1209
|
+
}));
|
|
1210
|
+
return CheckpointNumber(latest ?? INITIAL_CHECKPOINT_NUMBER - 1);
|
|
1211
|
+
}
|
|
1212
|
+
/** Removes a rejected-checkpoint entry by its archive root (used when an entry no longer matches L1). */ async removeRejectedCheckpointByArchiveRoot(archiveRoot) {
|
|
1213
|
+
const archiveRootHex = archiveRoot.toString();
|
|
1214
|
+
const stored = await this.#rejectedCheckpoints.getAsync(archiveRootHex);
|
|
1215
|
+
await this.#rejectedCheckpoints.delete(archiveRootHex);
|
|
1216
|
+
if (stored) {
|
|
1217
|
+
// Only clear the by-number index if it still points at this archive root, so a distinct
|
|
1218
|
+
// entry that shares the checkpoint number (e.g. an L1 reorg replacement) is not dropped.
|
|
1219
|
+
const indexed = await this.#rejectedCheckpointsByNumber.getAsync(stored.checkpointNumber);
|
|
1220
|
+
if (indexed === archiveRootHex) {
|
|
1221
|
+
await this.#rejectedCheckpointsByNumber.delete(stored.checkpointNumber);
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1224
|
+
}
|
|
1225
|
+
/**
|
|
1226
|
+
* Advances the stored last-synched L1 block number to `l1BlockNumber` only if it is strictly
|
|
1227
|
+
* greater than the current value. Use this whenever ingesting checkpoint-shaped data so the
|
|
1228
|
+
* sync pointer never walks backwards on out-of-order writes (e.g. an invalid checkpoint
|
|
1229
|
+
* advance followed by a valid-checkpoint commit landing at an earlier L1 block).
|
|
1230
|
+
*/ async advanceSynchedL1BlockNumber(l1BlockNumber) {
|
|
1231
|
+
const current = await this.#lastSynchedL1Block.getAsync();
|
|
1232
|
+
if (current === undefined || l1BlockNumber > current) {
|
|
1233
|
+
await this.#lastSynchedL1Block.set(l1BlockNumber);
|
|
1234
|
+
}
|
|
1235
|
+
}
|
|
1236
|
+
rejectedCheckpointFromStorage(stored) {
|
|
1237
|
+
return {
|
|
1238
|
+
checkpointNumber: CheckpointNumber(stored.checkpointNumber),
|
|
1239
|
+
archiveRoot: Fr.fromBuffer(stored.archiveRoot),
|
|
1240
|
+
parentArchiveRoot: Fr.fromBuffer(stored.parentArchiveRoot),
|
|
1241
|
+
slotNumber: SlotNumber(stored.slotNumber),
|
|
1242
|
+
l1: L1PublishedData.fromBuffer(stored.l1),
|
|
1243
|
+
reason: stored.reason
|
|
1244
|
+
};
|
|
1245
|
+
}
|
|
898
1246
|
}
|