@aztec/archiver 3.0.0-rc.5 → 4.0.0-nightly.20260107
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/dest/archiver/archiver.d.ts +69 -49
- package/dest/archiver/archiver.d.ts.map +1 -1
- package/dest/archiver/archiver.js +777 -214
- package/dest/archiver/archiver_store.d.ts +89 -30
- package/dest/archiver/archiver_store.d.ts.map +1 -1
- package/dest/archiver/archiver_store_test_suite.d.ts +1 -1
- package/dest/archiver/archiver_store_test_suite.d.ts.map +1 -1
- package/dest/archiver/archiver_store_test_suite.js +1785 -288
- package/dest/archiver/config.d.ts +3 -3
- package/dest/archiver/config.d.ts.map +1 -1
- package/dest/archiver/config.js +2 -2
- package/dest/archiver/errors.d.ts +25 -1
- package/dest/archiver/errors.d.ts.map +1 -1
- package/dest/archiver/errors.js +37 -0
- package/dest/archiver/index.d.ts +2 -2
- package/dest/archiver/index.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/block_store.d.ts +49 -17
- package/dest/archiver/kv_archiver_store/block_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/block_store.js +320 -84
- package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +33 -37
- package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/kv_archiver_store.js +60 -35
- package/dest/archiver/kv_archiver_store/log_store.d.ts +14 -11
- package/dest/archiver/kv_archiver_store/log_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/log_store.js +149 -62
- package/dest/archiver/l1/bin/retrieve-calldata.js +5 -3
- package/dest/archiver/l1/calldata_retriever.d.ts +17 -3
- package/dest/archiver/l1/calldata_retriever.d.ts.map +1 -1
- package/dest/archiver/l1/calldata_retriever.js +75 -7
- package/dest/archiver/l1/data_retrieval.d.ts +13 -10
- package/dest/archiver/l1/data_retrieval.d.ts.map +1 -1
- package/dest/archiver/l1/data_retrieval.js +31 -18
- package/dest/archiver/structs/published.d.ts +1 -2
- package/dest/archiver/structs/published.d.ts.map +1 -1
- package/dest/factory.d.ts +1 -1
- package/dest/factory.js +1 -1
- package/dest/test/mock_l2_block_source.d.ts +10 -3
- package/dest/test/mock_l2_block_source.d.ts.map +1 -1
- package/dest/test/mock_l2_block_source.js +16 -15
- package/package.json +13 -13
- package/src/archiver/archiver.ts +509 -260
- package/src/archiver/archiver_store.ts +99 -29
- package/src/archiver/archiver_store_test_suite.ts +1831 -274
- package/src/archiver/config.ts +7 -3
- package/src/archiver/errors.ts +64 -0
- package/src/archiver/index.ts +1 -1
- package/src/archiver/kv_archiver_store/block_store.ts +434 -94
- package/src/archiver/kv_archiver_store/kv_archiver_store.ts +74 -49
- package/src/archiver/kv_archiver_store/log_store.ts +213 -77
- package/src/archiver/l1/bin/retrieve-calldata.ts +3 -3
- package/src/archiver/l1/calldata_retriever.ts +116 -6
- package/src/archiver/l1/data_retrieval.ts +41 -20
- package/src/archiver/structs/published.ts +0 -1
- package/src/factory.ts +1 -1
- package/src/test/mock_l2_block_source.ts +20 -16
|
@@ -1,25 +1,27 @@
|
|
|
1
|
-
import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
|
|
2
|
-
import { BlockNumber } from '@aztec/foundation/branded-types';
|
|
3
|
-
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
1
|
+
import { INITIAL_CHECKPOINT_NUMBER, INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
|
|
2
|
+
import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
|
|
4
3
|
import { toArray } from '@aztec/foundation/iterable';
|
|
5
4
|
import { createLogger } from '@aztec/foundation/log';
|
|
6
5
|
import { BufferReader } from '@aztec/foundation/serialize';
|
|
7
6
|
import { bufferToHex } from '@aztec/foundation/string';
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
7
|
+
import { isDefined } from '@aztec/foundation/types';
|
|
8
|
+
import { Body, CheckpointedL2Block, CommitteeAttestation, L2BlockHash, L2BlockNew, deserializeValidateBlockResult, serializeValidateBlockResult } from '@aztec/stdlib/block';
|
|
9
|
+
import { L1PublishedData } from '@aztec/stdlib/checkpoint';
|
|
10
|
+
import { CheckpointHeader } from '@aztec/stdlib/rollup';
|
|
10
11
|
import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
|
|
11
|
-
import { TxHash, TxReceipt, deserializeIndexedTxEffect, serializeIndexedTxEffect } from '@aztec/stdlib/tx';
|
|
12
|
-
import { BlockNumberNotSequentialError, InitialBlockNumberNotSequentialError } from '../errors.js';
|
|
12
|
+
import { BlockHeader, TxHash, TxReceipt, deserializeIndexedTxEffect, serializeIndexedTxEffect } from '@aztec/stdlib/tx';
|
|
13
|
+
import { BlockArchiveNotConsistentError, BlockIndexNotSequentialError, BlockNotFoundError, BlockNumberNotSequentialError, CheckpointNotFoundError, CheckpointNumberNotConsistentError, CheckpointNumberNotSequentialError, InitialBlockNumberNotSequentialError, InitialCheckpointNumberNotSequentialError } from '../errors.js';
|
|
13
14
|
export { TxReceipt } from '@aztec/stdlib/tx';
|
|
14
15
|
/**
|
|
15
16
|
* LMDB implementation of the ArchiverDataStore interface.
|
|
16
17
|
*/ export class BlockStore {
|
|
17
18
|
db;
|
|
18
19
|
/** Map block number to block data */ #blocks;
|
|
20
|
+
/** Map checkpoint number to checkpoint data */ #checkpoints;
|
|
19
21
|
/** Map block hash to list of tx hashes */ #blockTxs;
|
|
20
22
|
/** Tx hash to serialized IndexedTxEffect */ #txEffects;
|
|
21
23
|
/** Stores L1 block number in which the last processed L2 block was included */ #lastSynchedL1Block;
|
|
22
|
-
/** Stores
|
|
24
|
+
/** Stores last proven checkpoint */ #lastProvenCheckpoint;
|
|
23
25
|
/** Stores the pending chain validation status */ #pendingChainValidationStatus;
|
|
24
26
|
/** Index mapping a contract's address (as a string) to its location in a block */ #contractIndex;
|
|
25
27
|
/** Index mapping block hash to block number */ #blockHashIndex;
|
|
@@ -35,11 +37,12 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
35
37
|
this.#blockHashIndex = db.openMap('archiver_block_hash_index');
|
|
36
38
|
this.#blockArchiveIndex = db.openMap('archiver_block_archive_index');
|
|
37
39
|
this.#lastSynchedL1Block = db.openSingleton('archiver_last_synched_l1_block');
|
|
38
|
-
this.#
|
|
40
|
+
this.#lastProvenCheckpoint = db.openSingleton('archiver_last_proven_l2_checkpoint');
|
|
39
41
|
this.#pendingChainValidationStatus = db.openSingleton('archiver_pending_chain_validation_status');
|
|
42
|
+
this.#checkpoints = db.openMap('archiver_checkpoints');
|
|
40
43
|
}
|
|
41
44
|
/**
|
|
42
|
-
* Append new blocks to the store's list.
|
|
45
|
+
* Append new blocks to the store's list. All blocks must be for the 'current' checkpoint
|
|
43
46
|
* @param blocks - The L2 blocks to be added to the store.
|
|
44
47
|
* @returns True if the operation is successful.
|
|
45
48
|
*/ async addBlocks(blocks, opts = {}) {
|
|
@@ -48,84 +51,313 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
48
51
|
}
|
|
49
52
|
return await this.db.transactionAsync(async ()=>{
|
|
50
53
|
// Check that the block immediately before the first block to be added is present in the store.
|
|
51
|
-
const firstBlockNumber = blocks[0].
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
const
|
|
58
|
-
|
|
54
|
+
const firstBlockNumber = blocks[0].number;
|
|
55
|
+
const firstBlockCheckpointNumber = blocks[0].checkpointNumber;
|
|
56
|
+
const firstBlockIndex = blocks[0].indexWithinCheckpoint;
|
|
57
|
+
const firstBlockLastArchive = blocks[0].header.lastArchive.root;
|
|
58
|
+
// Extract the latest block and checkpoint numbers
|
|
59
|
+
const previousBlockNumber = await this.getLatestBlockNumber();
|
|
60
|
+
const previousCheckpointNumber = await this.getLatestCheckpointNumber();
|
|
61
|
+
// Check that the first block number is the expected one
|
|
62
|
+
if (!opts.force && previousBlockNumber !== firstBlockNumber - 1) {
|
|
59
63
|
throw new InitialBlockNumberNotSequentialError(firstBlockNumber, previousBlockNumber);
|
|
60
64
|
}
|
|
61
|
-
//
|
|
65
|
+
// The same check as above but for checkpoints
|
|
66
|
+
if (!opts.force && previousCheckpointNumber !== firstBlockCheckpointNumber - 1) {
|
|
67
|
+
throw new InitialCheckpointNumberNotSequentialError(firstBlockCheckpointNumber, previousCheckpointNumber);
|
|
68
|
+
}
|
|
69
|
+
// Extract the previous block if there is one and see if it is for the same checkpoint or not
|
|
70
|
+
const previousBlockResult = await this.getBlock(previousBlockNumber);
|
|
71
|
+
let expectedFirstblockIndex = 0;
|
|
72
|
+
let previousBlockIndex = undefined;
|
|
73
|
+
if (previousBlockResult !== undefined) {
|
|
74
|
+
if (previousBlockResult.checkpointNumber === firstBlockCheckpointNumber) {
|
|
75
|
+
// The previous block is for the same checkpoint, therefore our index should follow it
|
|
76
|
+
previousBlockIndex = previousBlockResult.indexWithinCheckpoint;
|
|
77
|
+
expectedFirstblockIndex = previousBlockIndex + 1;
|
|
78
|
+
}
|
|
79
|
+
if (!previousBlockResult.archive.root.equals(firstBlockLastArchive)) {
|
|
80
|
+
throw new BlockArchiveNotConsistentError(firstBlockNumber, previousBlockResult.number, firstBlockLastArchive, previousBlockResult.archive.root);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
// Now check that the first block has the expected index value
|
|
84
|
+
if (!opts.force && expectedFirstblockIndex !== firstBlockIndex) {
|
|
85
|
+
throw new BlockIndexNotSequentialError(firstBlockIndex, previousBlockIndex);
|
|
86
|
+
}
|
|
87
|
+
// Iterate over blocks array and insert them, checking that the block numbers and indexes are sequential. Also check they are for the correct checkpoint.
|
|
62
88
|
let previousBlock = undefined;
|
|
63
89
|
for (const block of blocks){
|
|
64
|
-
if (!opts.force && previousBlock
|
|
65
|
-
|
|
90
|
+
if (!opts.force && previousBlock) {
|
|
91
|
+
if (previousBlock.number + 1 !== block.number) {
|
|
92
|
+
throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
|
|
93
|
+
}
|
|
94
|
+
if (previousBlock.indexWithinCheckpoint + 1 !== block.indexWithinCheckpoint) {
|
|
95
|
+
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
|
|
96
|
+
}
|
|
97
|
+
if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
|
|
98
|
+
throw new BlockArchiveNotConsistentError(block.number, previousBlock.number, block.header.lastArchive.root, previousBlock.archive.root);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (!opts.force && firstBlockCheckpointNumber !== block.checkpointNumber) {
|
|
102
|
+
throw new CheckpointNumberNotConsistentError(block.checkpointNumber, firstBlockCheckpointNumber);
|
|
66
103
|
}
|
|
67
104
|
previousBlock = block;
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
105
|
+
await this.addBlockToDatabase(block, block.checkpointNumber, block.indexWithinCheckpoint);
|
|
106
|
+
}
|
|
107
|
+
return true;
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Append new cheskpoints to the store's list.
|
|
112
|
+
* @param checkpoints - The L2 checkpoints to be added to the store.
|
|
113
|
+
* @returns True if the operation is successful.
|
|
114
|
+
*/ async addCheckpoints(checkpoints, opts = {}) {
|
|
115
|
+
if (checkpoints.length === 0) {
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
return await this.db.transactionAsync(async ()=>{
|
|
119
|
+
// Check that the checkpoint immediately before the first block to be added is present in the store.
|
|
120
|
+
const firstCheckpointNumber = checkpoints[0].checkpoint.number;
|
|
121
|
+
const previousCheckpointNumber = await this.getLatestCheckpointNumber();
|
|
122
|
+
if (previousCheckpointNumber !== firstCheckpointNumber - 1 && !opts.force) {
|
|
123
|
+
throw new InitialCheckpointNumberNotSequentialError(firstCheckpointNumber, previousCheckpointNumber);
|
|
124
|
+
}
|
|
125
|
+
// Extract the previous checkpoint if there is one
|
|
126
|
+
let previousCheckpointData = undefined;
|
|
127
|
+
if (previousCheckpointNumber !== INITIAL_CHECKPOINT_NUMBER - 1) {
|
|
128
|
+
// There should be a previous checkpoint
|
|
129
|
+
previousCheckpointData = await this.getCheckpointData(previousCheckpointNumber);
|
|
130
|
+
if (previousCheckpointData === undefined) {
|
|
131
|
+
throw new CheckpointNotFoundError(previousCheckpointNumber);
|
|
84
132
|
}
|
|
85
|
-
await this.#blockTxs.set(blockHash.toString(), Buffer.concat(block.block.body.txEffects.map((tx)=>tx.txHash.toBuffer())));
|
|
86
|
-
// Update indices for block hash and archive
|
|
87
|
-
await this.#blockHashIndex.set(blockHash.toString(), block.block.number);
|
|
88
|
-
await this.#blockArchiveIndex.set(block.block.archive.root.toString(), block.block.number);
|
|
89
133
|
}
|
|
90
|
-
|
|
134
|
+
let previousBlockNumber = undefined;
|
|
135
|
+
let previousBlock = undefined;
|
|
136
|
+
// If we have a previous checkpoint then we need to get the previous block number
|
|
137
|
+
if (previousCheckpointData !== undefined) {
|
|
138
|
+
previousBlockNumber = BlockNumber(previousCheckpointData.startBlock + previousCheckpointData.numBlocks - 1);
|
|
139
|
+
previousBlock = await this.getBlock(previousBlockNumber);
|
|
140
|
+
if (previousBlock === undefined) {
|
|
141
|
+
// We should be able to get the required previous block
|
|
142
|
+
throw new BlockNotFoundError(previousBlockNumber);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
// Iterate over checkpoints array and insert them, checking that the block numbers are sequential.
|
|
146
|
+
let previousCheckpoint = undefined;
|
|
147
|
+
for (const checkpoint of checkpoints){
|
|
148
|
+
if (!opts.force && previousCheckpoint && previousCheckpoint.checkpoint.number + 1 !== checkpoint.checkpoint.number) {
|
|
149
|
+
throw new CheckpointNumberNotSequentialError(checkpoint.checkpoint.number, previousCheckpoint.checkpoint.number);
|
|
150
|
+
}
|
|
151
|
+
previousCheckpoint = checkpoint;
|
|
152
|
+
// Store every block in the database. the block may already exist, but this has come from chain and is assumed to be correct.
|
|
153
|
+
for(let i = 0; i < checkpoint.checkpoint.blocks.length; i++){
|
|
154
|
+
const block = checkpoint.checkpoint.blocks[i];
|
|
155
|
+
if (previousBlock) {
|
|
156
|
+
// The blocks should have a sequential block number
|
|
157
|
+
if (previousBlock.number !== block.number - 1) {
|
|
158
|
+
throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
|
|
159
|
+
}
|
|
160
|
+
// If the blocks are for the same checkpoint then they should have sequential indexes
|
|
161
|
+
if (previousBlock.checkpointNumber === block.checkpointNumber && previousBlock.indexWithinCheckpoint !== block.indexWithinCheckpoint - 1) {
|
|
162
|
+
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
|
|
163
|
+
}
|
|
164
|
+
if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
|
|
165
|
+
throw new BlockArchiveNotConsistentError(block.number, previousBlock.number, block.header.lastArchive.root, previousBlock.archive.root);
|
|
166
|
+
}
|
|
167
|
+
} else {
|
|
168
|
+
// No previous block, must be block 1 at checkpoint index 0
|
|
169
|
+
if (block.indexWithinCheckpoint !== 0) {
|
|
170
|
+
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, undefined);
|
|
171
|
+
}
|
|
172
|
+
if (block.number !== INITIAL_L2_BLOCK_NUM) {
|
|
173
|
+
throw new BlockNumberNotSequentialError(block.number, undefined);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
previousBlock = block;
|
|
177
|
+
await this.addBlockToDatabase(block, checkpoint.checkpoint.number, i);
|
|
178
|
+
}
|
|
179
|
+
// Store the checkpoint in the database
|
|
180
|
+
await this.#checkpoints.set(checkpoint.checkpoint.number, {
|
|
181
|
+
header: checkpoint.checkpoint.header.toBuffer(),
|
|
182
|
+
archive: checkpoint.checkpoint.archive.toBuffer(),
|
|
183
|
+
l1: checkpoint.l1.toBuffer(),
|
|
184
|
+
attestations: checkpoint.attestations.map((attestation)=>attestation.toBuffer()),
|
|
185
|
+
checkpointNumber: checkpoint.checkpoint.number,
|
|
186
|
+
startBlock: checkpoint.checkpoint.blocks[0].number,
|
|
187
|
+
numBlocks: checkpoint.checkpoint.blocks.length
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
await this.#lastSynchedL1Block.set(checkpoints[checkpoints.length - 1].l1.blockNumber);
|
|
91
191
|
return true;
|
|
92
192
|
});
|
|
93
193
|
}
|
|
194
|
+
async addBlockToDatabase(block, checkpointNumber, indexWithinCheckpoint) {
|
|
195
|
+
const blockHash = L2BlockHash.fromField(await block.hash());
|
|
196
|
+
await this.#blocks.set(block.number, {
|
|
197
|
+
header: block.header.toBuffer(),
|
|
198
|
+
blockHash: blockHash.toBuffer(),
|
|
199
|
+
archive: block.archive.toBuffer(),
|
|
200
|
+
checkpointNumber,
|
|
201
|
+
indexWithinCheckpoint
|
|
202
|
+
});
|
|
203
|
+
for(let i = 0; i < block.body.txEffects.length; i++){
|
|
204
|
+
const txEffect = {
|
|
205
|
+
data: block.body.txEffects[i],
|
|
206
|
+
l2BlockNumber: block.number,
|
|
207
|
+
l2BlockHash: blockHash,
|
|
208
|
+
txIndexInBlock: i
|
|
209
|
+
};
|
|
210
|
+
await this.#txEffects.set(txEffect.data.txHash.toString(), serializeIndexedTxEffect(txEffect));
|
|
211
|
+
}
|
|
212
|
+
await this.#blockTxs.set(blockHash.toString(), Buffer.concat(block.body.txEffects.map((tx)=>tx.txHash.toBuffer())));
|
|
213
|
+
// Update indices for block hash and archive
|
|
214
|
+
await this.#blockHashIndex.set(blockHash.toString(), block.number);
|
|
215
|
+
await this.#blockArchiveIndex.set(block.archive.root.toString(), block.number);
|
|
216
|
+
}
|
|
94
217
|
/**
|
|
95
|
-
* Unwinds
|
|
218
|
+
* Unwinds checkpoints from the database
|
|
96
219
|
* @param from - The tip of the chain, passed for verification purposes,
|
|
97
220
|
* ensuring that we don't end up deleting something we did not intend
|
|
98
|
-
* @param
|
|
221
|
+
* @param checkpointsToUnwind - The number of checkpoints we are to unwind
|
|
99
222
|
* @returns True if the operation is successful
|
|
100
|
-
*/ async
|
|
223
|
+
*/ async unwindCheckpoints(from, checkpointsToUnwind) {
|
|
101
224
|
return await this.db.transactionAsync(async ()=>{
|
|
102
|
-
const last = await this.
|
|
225
|
+
const last = await this.getLatestCheckpointNumber();
|
|
103
226
|
if (from !== last) {
|
|
104
|
-
throw new Error(`Can only unwind
|
|
227
|
+
throw new Error(`Can only unwind checkpoints from the tip (requested ${from} but current tip is ${last})`);
|
|
105
228
|
}
|
|
106
|
-
const proven = await this.
|
|
107
|
-
if (from -
|
|
108
|
-
await this.
|
|
229
|
+
const proven = await this.getProvenCheckpointNumber();
|
|
230
|
+
if (from - checkpointsToUnwind < proven) {
|
|
231
|
+
await this.setProvenCheckpointNumber(CheckpointNumber(from - checkpointsToUnwind));
|
|
109
232
|
}
|
|
110
|
-
for(let i = 0; i <
|
|
111
|
-
const
|
|
112
|
-
const
|
|
113
|
-
if (
|
|
114
|
-
this.#log.warn(`Cannot remove
|
|
233
|
+
for(let i = 0; i < checkpointsToUnwind; i++){
|
|
234
|
+
const checkpointNumber = from - i;
|
|
235
|
+
const checkpoint = await this.#checkpoints.getAsync(checkpointNumber);
|
|
236
|
+
if (checkpoint === undefined) {
|
|
237
|
+
this.#log.warn(`Cannot remove checkpoint ${checkpointNumber} from the store since we don't have it`);
|
|
115
238
|
continue;
|
|
116
239
|
}
|
|
117
|
-
await this.#
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
240
|
+
await this.#checkpoints.delete(checkpointNumber);
|
|
241
|
+
const maxBlock = checkpoint.startBlock + checkpoint.numBlocks - 1;
|
|
242
|
+
for(let blockNumber = checkpoint.startBlock; blockNumber <= maxBlock; blockNumber++){
|
|
243
|
+
const block = await this.getBlock(BlockNumber(blockNumber));
|
|
244
|
+
if (block === undefined) {
|
|
245
|
+
this.#log.warn(`Cannot remove block ${blockNumber} from the store since we don't have it`);
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
await this.#blocks.delete(block.number);
|
|
249
|
+
await Promise.all(block.body.txEffects.map((tx)=>this.#txEffects.delete(tx.txHash.toString())));
|
|
250
|
+
const blockHash = (await block.hash()).toString();
|
|
251
|
+
await this.#blockTxs.delete(blockHash);
|
|
252
|
+
// Clean up indices
|
|
253
|
+
await this.#blockHashIndex.delete(blockHash);
|
|
254
|
+
await this.#blockArchiveIndex.delete(block.archive.root.toString());
|
|
255
|
+
this.#log.debug(`Unwound block ${blockNumber} ${blockHash} for checkpoint ${checkpointNumber}`);
|
|
256
|
+
}
|
|
125
257
|
}
|
|
126
258
|
return true;
|
|
127
259
|
});
|
|
128
260
|
}
|
|
261
|
+
async getCheckpointData(checkpointNumber) {
|
|
262
|
+
const checkpointStorage = await this.#checkpoints.getAsync(checkpointNumber);
|
|
263
|
+
if (!checkpointStorage) {
|
|
264
|
+
return undefined;
|
|
265
|
+
}
|
|
266
|
+
return this.checkpointDataFromCheckpointStorage(checkpointStorage);
|
|
267
|
+
}
|
|
268
|
+
async getRangeOfCheckpoints(from, limit) {
|
|
269
|
+
const checkpoints = [];
|
|
270
|
+
for(let checkpointNumber = from; checkpointNumber < from + limit; checkpointNumber++){
|
|
271
|
+
const checkpoint = await this.#checkpoints.getAsync(checkpointNumber);
|
|
272
|
+
if (!checkpoint) {
|
|
273
|
+
break;
|
|
274
|
+
}
|
|
275
|
+
checkpoints.push(this.checkpointDataFromCheckpointStorage(checkpoint));
|
|
276
|
+
}
|
|
277
|
+
return checkpoints;
|
|
278
|
+
}
|
|
279
|
+
checkpointDataFromCheckpointStorage(checkpointStorage) {
|
|
280
|
+
const data = {
|
|
281
|
+
header: CheckpointHeader.fromBuffer(checkpointStorage.header),
|
|
282
|
+
archive: AppendOnlyTreeSnapshot.fromBuffer(checkpointStorage.archive),
|
|
283
|
+
checkpointNumber: CheckpointNumber(checkpointStorage.checkpointNumber),
|
|
284
|
+
startBlock: checkpointStorage.startBlock,
|
|
285
|
+
numBlocks: checkpointStorage.numBlocks,
|
|
286
|
+
l1: L1PublishedData.fromBuffer(checkpointStorage.l1),
|
|
287
|
+
attestations: checkpointStorage.attestations
|
|
288
|
+
};
|
|
289
|
+
return data;
|
|
290
|
+
}
|
|
291
|
+
async getBlocksForCheckpoint(checkpointNumber) {
|
|
292
|
+
const checkpoint = await this.#checkpoints.getAsync(checkpointNumber);
|
|
293
|
+
if (!checkpoint) {
|
|
294
|
+
return undefined;
|
|
295
|
+
}
|
|
296
|
+
const blocksForCheckpoint = await toArray(this.#blocks.entriesAsync({
|
|
297
|
+
start: checkpoint.startBlock,
|
|
298
|
+
end: checkpoint.startBlock + checkpoint.numBlocks
|
|
299
|
+
}));
|
|
300
|
+
const converted = await Promise.all(blocksForCheckpoint.map((x)=>this.getBlockFromBlockStorage(x[0], x[1])));
|
|
301
|
+
return converted.filter(isDefined);
|
|
302
|
+
}
|
|
303
|
+
async getProvenBlockNumber() {
|
|
304
|
+
const provenCheckpointNumber = await this.getProvenCheckpointNumber();
|
|
305
|
+
if (provenCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
|
|
306
|
+
return BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
307
|
+
}
|
|
308
|
+
const checkpointStorage = await this.#checkpoints.getAsync(provenCheckpointNumber);
|
|
309
|
+
if (!checkpointStorage) {
|
|
310
|
+
throw new CheckpointNotFoundError(provenCheckpointNumber);
|
|
311
|
+
} else {
|
|
312
|
+
return BlockNumber(checkpointStorage.startBlock + checkpointStorage.numBlocks - 1);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
async getLatestBlockNumber() {
|
|
316
|
+
const [latestBlocknumber] = await toArray(this.#blocks.keysAsync({
|
|
317
|
+
reverse: true,
|
|
318
|
+
limit: 1
|
|
319
|
+
}));
|
|
320
|
+
return typeof latestBlocknumber === 'number' ? BlockNumber(latestBlocknumber) : BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
321
|
+
}
|
|
322
|
+
async getLatestCheckpointNumber() {
|
|
323
|
+
const [latestCheckpointNumber] = await toArray(this.#checkpoints.keysAsync({
|
|
324
|
+
reverse: true,
|
|
325
|
+
limit: 1
|
|
326
|
+
}));
|
|
327
|
+
if (latestCheckpointNumber === undefined) {
|
|
328
|
+
return CheckpointNumber(INITIAL_CHECKPOINT_NUMBER - 1);
|
|
329
|
+
}
|
|
330
|
+
return CheckpointNumber(latestCheckpointNumber);
|
|
331
|
+
}
|
|
332
|
+
async getCheckpointedBlock(number) {
|
|
333
|
+
const blockStorage = await this.#blocks.getAsync(number);
|
|
334
|
+
if (!blockStorage) {
|
|
335
|
+
return undefined;
|
|
336
|
+
}
|
|
337
|
+
const checkpoint = await this.#checkpoints.getAsync(blockStorage.checkpointNumber);
|
|
338
|
+
if (!checkpoint) {
|
|
339
|
+
return undefined;
|
|
340
|
+
}
|
|
341
|
+
const block = await this.getBlockFromBlockStorage(number, blockStorage);
|
|
342
|
+
if (!block) {
|
|
343
|
+
return undefined;
|
|
344
|
+
}
|
|
345
|
+
return new CheckpointedL2Block(CheckpointNumber(checkpoint.checkpointNumber), block, L1PublishedData.fromBuffer(checkpoint.l1), checkpoint.attestations.map((buf)=>CommitteeAttestation.fromBuffer(buf)));
|
|
346
|
+
}
|
|
347
|
+
async getCheckpointedBlockByHash(blockHash) {
|
|
348
|
+
const blockNumber = await this.#blockHashIndex.getAsync(blockHash.toString());
|
|
349
|
+
if (blockNumber === undefined) {
|
|
350
|
+
return undefined;
|
|
351
|
+
}
|
|
352
|
+
return this.getCheckpointedBlock(BlockNumber(blockNumber));
|
|
353
|
+
}
|
|
354
|
+
async getCheckpointedBlockByArchive(archive) {
|
|
355
|
+
const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
|
|
356
|
+
if (blockNumber === undefined) {
|
|
357
|
+
return undefined;
|
|
358
|
+
}
|
|
359
|
+
return this.getCheckpointedBlock(BlockNumber(blockNumber));
|
|
360
|
+
}
|
|
129
361
|
/**
|
|
130
362
|
* Gets up to `limit` amount of L2 blocks starting from `from`.
|
|
131
363
|
* @param start - Number of the first block to return (inclusive).
|
|
@@ -185,7 +417,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
185
417
|
if (!blockStorage || !blockStorage.header) {
|
|
186
418
|
return undefined;
|
|
187
419
|
}
|
|
188
|
-
return
|
|
420
|
+
return BlockHeader.fromBuffer(blockStorage.header);
|
|
189
421
|
}
|
|
190
422
|
/**
|
|
191
423
|
* Gets a block header by its archive root.
|
|
@@ -200,7 +432,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
200
432
|
if (!blockStorage || !blockStorage.header) {
|
|
201
433
|
return undefined;
|
|
202
434
|
}
|
|
203
|
-
return
|
|
435
|
+
return BlockHeader.fromBuffer(blockStorage.header);
|
|
204
436
|
}
|
|
205
437
|
/**
|
|
206
438
|
* Gets the headers for a sequence of L2 blocks.
|
|
@@ -209,7 +441,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
209
441
|
* @returns The requested L2 block headers
|
|
210
442
|
*/ async *getBlockHeaders(start, limit) {
|
|
211
443
|
for await (const [blockNumber, blockStorage] of this.getBlockStorages(start, limit)){
|
|
212
|
-
const header =
|
|
444
|
+
const header = BlockHeader.fromBuffer(blockStorage.header);
|
|
213
445
|
if (header.getBlockNumber() !== blockNumber) {
|
|
214
446
|
throw new Error(`Block number mismatch when retrieving block header from archive (expected ${blockNumber} but got ${header.getBlockNumber()})`);
|
|
215
447
|
}
|
|
@@ -230,7 +462,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
230
462
|
}
|
|
231
463
|
}
|
|
232
464
|
async getBlockFromBlockStorage(blockNumber, blockStorage) {
|
|
233
|
-
const header =
|
|
465
|
+
const header = BlockHeader.fromBuffer(blockStorage.header);
|
|
234
466
|
const archive = AppendOnlyTreeSnapshot.fromBuffer(blockStorage.archive);
|
|
235
467
|
const blockHash = blockStorage.blockHash;
|
|
236
468
|
const blockHashString = bufferToHex(blockHash);
|
|
@@ -251,16 +483,11 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
251
483
|
txEffects.push(deserializeIndexedTxEffect(txEffect).data);
|
|
252
484
|
}
|
|
253
485
|
const body = new Body(txEffects);
|
|
254
|
-
const block = new
|
|
486
|
+
const block = new L2BlockNew(archive, header, body, CheckpointNumber(blockStorage.checkpointNumber), blockStorage.indexWithinCheckpoint);
|
|
255
487
|
if (block.number !== blockNumber) {
|
|
256
488
|
throw new Error(`Block number mismatch when retrieving block from archive (expected ${blockNumber} but got ${block.number} with hash ${blockHashString})`);
|
|
257
489
|
}
|
|
258
|
-
|
|
259
|
-
return PublishedL2Block.fromFields({
|
|
260
|
-
block,
|
|
261
|
-
l1: blockStorage.l1,
|
|
262
|
-
attestations
|
|
263
|
-
});
|
|
490
|
+
return block;
|
|
264
491
|
}
|
|
265
492
|
/**
|
|
266
493
|
* Gets a tx effect.
|
|
@@ -307,9 +534,17 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
307
534
|
return this.#contractIndex.getAsync(contractAddress.toString());
|
|
308
535
|
}
|
|
309
536
|
/**
|
|
310
|
-
* Gets the number of the latest L2 block
|
|
311
|
-
* @returns The number of the latest L2 block
|
|
312
|
-
*/ async
|
|
537
|
+
* Gets the number of the latest L2 block checkpointed.
|
|
538
|
+
* @returns The number of the latest L2 block checkpointed.
|
|
539
|
+
*/ async getCheckpointedL2BlockNumber() {
|
|
540
|
+
const latestCheckpointNumber = await this.getLatestCheckpointNumber();
|
|
541
|
+
const checkpoint = await this.getCheckpointData(latestCheckpointNumber);
|
|
542
|
+
if (!checkpoint) {
|
|
543
|
+
return BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
544
|
+
}
|
|
545
|
+
return BlockNumber(checkpoint.startBlock + checkpoint.numBlocks - 1);
|
|
546
|
+
}
|
|
547
|
+
async getLatestL2BlockNumber() {
|
|
313
548
|
const [lastBlockNumber] = await toArray(this.#blocks.keysAsync({
|
|
314
549
|
reverse: true,
|
|
315
550
|
limit: 1
|
|
@@ -325,15 +560,16 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
325
560
|
setSynchedL1BlockNumber(l1BlockNumber) {
|
|
326
561
|
return this.#lastSynchedL1Block.set(l1BlockNumber);
|
|
327
562
|
}
|
|
328
|
-
async
|
|
329
|
-
const [
|
|
330
|
-
this.
|
|
331
|
-
this.#
|
|
563
|
+
async getProvenCheckpointNumber() {
|
|
564
|
+
const [latestCheckpointNumber, provenCheckpointNumber] = await Promise.all([
|
|
565
|
+
this.getLatestCheckpointNumber(),
|
|
566
|
+
this.#lastProvenCheckpoint.getAsync()
|
|
332
567
|
]);
|
|
333
|
-
return (
|
|
568
|
+
return (provenCheckpointNumber ?? 0) > latestCheckpointNumber ? latestCheckpointNumber : CheckpointNumber(provenCheckpointNumber ?? 0);
|
|
334
569
|
}
|
|
335
|
-
|
|
336
|
-
|
|
570
|
+
async setProvenCheckpointNumber(checkpointNumber) {
|
|
571
|
+
const result = await this.#lastProvenCheckpoint.set(checkpointNumber);
|
|
572
|
+
return result;
|
|
337
573
|
}
|
|
338
574
|
#computeBlockRange(start, limit) {
|
|
339
575
|
if (limit < 1) {
|