@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,21 +1,25 @@
|
|
|
1
|
-
import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
|
|
2
|
-
import { BlockNumber } from '@aztec/foundation/branded-types';
|
|
1
|
+
import { INITIAL_CHECKPOINT_NUMBER, INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
|
|
2
|
+
import { BlockNumber, CheckpointNumber } 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
|
+
import { isDefined } from '@aztec/foundation/types';
|
|
8
9
|
import type { AztecAsyncKVStore, AztecAsyncMap, AztecAsyncSingleton, Range } from '@aztec/kv-store';
|
|
9
10
|
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
10
11
|
import {
|
|
11
12
|
Body,
|
|
13
|
+
CheckpointedL2Block,
|
|
12
14
|
CommitteeAttestation,
|
|
13
|
-
L2Block,
|
|
14
15
|
L2BlockHash,
|
|
15
|
-
|
|
16
|
+
L2BlockNew,
|
|
16
17
|
type ValidateBlockResult,
|
|
18
|
+
deserializeValidateBlockResult,
|
|
19
|
+
serializeValidateBlockResult,
|
|
17
20
|
} from '@aztec/stdlib/block';
|
|
18
|
-
import {
|
|
21
|
+
import { L1PublishedData, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
|
|
22
|
+
import { CheckpointHeader } from '@aztec/stdlib/rollup';
|
|
19
23
|
import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
|
|
20
24
|
import {
|
|
21
25
|
BlockHeader,
|
|
@@ -27,8 +31,17 @@ import {
|
|
|
27
31
|
serializeIndexedTxEffect,
|
|
28
32
|
} from '@aztec/stdlib/tx';
|
|
29
33
|
|
|
30
|
-
import {
|
|
31
|
-
|
|
34
|
+
import {
|
|
35
|
+
BlockArchiveNotConsistentError,
|
|
36
|
+
BlockIndexNotSequentialError,
|
|
37
|
+
BlockNotFoundError,
|
|
38
|
+
BlockNumberNotSequentialError,
|
|
39
|
+
CheckpointNotFoundError,
|
|
40
|
+
CheckpointNumberNotConsistentError,
|
|
41
|
+
CheckpointNumberNotSequentialError,
|
|
42
|
+
InitialBlockNumberNotSequentialError,
|
|
43
|
+
InitialCheckpointNumberNotSequentialError,
|
|
44
|
+
} from '../errors.js';
|
|
32
45
|
|
|
33
46
|
export { TxReceipt, type TxEffect, type TxHash } from '@aztec/stdlib/tx';
|
|
34
47
|
|
|
@@ -38,6 +51,26 @@ type BlockStorage = {
|
|
|
38
51
|
header: Buffer;
|
|
39
52
|
blockHash: Buffer;
|
|
40
53
|
archive: Buffer;
|
|
54
|
+
checkpointNumber: number;
|
|
55
|
+
indexWithinCheckpoint: number;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
type CheckpointStorage = {
|
|
59
|
+
header: Buffer;
|
|
60
|
+
archive: Buffer;
|
|
61
|
+
checkpointNumber: number;
|
|
62
|
+
startBlock: number;
|
|
63
|
+
numBlocks: number;
|
|
64
|
+
l1: Buffer;
|
|
65
|
+
attestations: Buffer[];
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export type CheckpointData = {
|
|
69
|
+
checkpointNumber: CheckpointNumber;
|
|
70
|
+
header: CheckpointHeader;
|
|
71
|
+
archive: AppendOnlyTreeSnapshot;
|
|
72
|
+
startBlock: number;
|
|
73
|
+
numBlocks: number;
|
|
41
74
|
l1: L1PublishedData;
|
|
42
75
|
attestations: Buffer[];
|
|
43
76
|
};
|
|
@@ -49,6 +82,9 @@ export class BlockStore {
|
|
|
49
82
|
/** Map block number to block data */
|
|
50
83
|
#blocks: AztecAsyncMap<number, BlockStorage>;
|
|
51
84
|
|
|
85
|
+
/** Map checkpoint number to checkpoint data */
|
|
86
|
+
#checkpoints: AztecAsyncMap<number, CheckpointStorage>;
|
|
87
|
+
|
|
52
88
|
/** Map block hash to list of tx hashes */
|
|
53
89
|
#blockTxs: AztecAsyncMap<string, Buffer>;
|
|
54
90
|
|
|
@@ -58,8 +94,8 @@ export class BlockStore {
|
|
|
58
94
|
/** Stores L1 block number in which the last processed L2 block was included */
|
|
59
95
|
#lastSynchedL1Block: AztecAsyncSingleton<bigint>;
|
|
60
96
|
|
|
61
|
-
/** Stores
|
|
62
|
-
#
|
|
97
|
+
/** Stores last proven checkpoint */
|
|
98
|
+
#lastProvenCheckpoint: AztecAsyncSingleton<number>;
|
|
63
99
|
|
|
64
100
|
/** Stores the pending chain validation status */
|
|
65
101
|
#pendingChainValidationStatus: AztecAsyncSingleton<Buffer>;
|
|
@@ -83,125 +119,412 @@ export class BlockStore {
|
|
|
83
119
|
this.#blockHashIndex = db.openMap('archiver_block_hash_index');
|
|
84
120
|
this.#blockArchiveIndex = db.openMap('archiver_block_archive_index');
|
|
85
121
|
this.#lastSynchedL1Block = db.openSingleton('archiver_last_synched_l1_block');
|
|
86
|
-
this.#
|
|
122
|
+
this.#lastProvenCheckpoint = db.openSingleton('archiver_last_proven_l2_checkpoint');
|
|
87
123
|
this.#pendingChainValidationStatus = db.openSingleton('archiver_pending_chain_validation_status');
|
|
124
|
+
this.#checkpoints = db.openMap('archiver_checkpoints');
|
|
88
125
|
}
|
|
89
126
|
|
|
90
127
|
/**
|
|
91
|
-
* Append new blocks to the store's list.
|
|
128
|
+
* Append new blocks to the store's list. All blocks must be for the 'current' checkpoint
|
|
92
129
|
* @param blocks - The L2 blocks to be added to the store.
|
|
93
130
|
* @returns True if the operation is successful.
|
|
94
131
|
*/
|
|
95
|
-
async addBlocks(blocks:
|
|
132
|
+
async addBlocks(blocks: L2BlockNew[], opts: { force?: boolean } = {}): Promise<boolean> {
|
|
96
133
|
if (blocks.length === 0) {
|
|
97
134
|
return true;
|
|
98
135
|
}
|
|
99
136
|
|
|
100
137
|
return await this.db.transactionAsync(async () => {
|
|
101
138
|
// Check that the block immediately before the first block to be added is present in the store.
|
|
102
|
-
const firstBlockNumber = blocks[0].
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
139
|
+
const firstBlockNumber = blocks[0].number;
|
|
140
|
+
const firstBlockCheckpointNumber = blocks[0].checkpointNumber;
|
|
141
|
+
const firstBlockIndex = blocks[0].indexWithinCheckpoint;
|
|
142
|
+
const firstBlockLastArchive = blocks[0].header.lastArchive.root;
|
|
143
|
+
|
|
144
|
+
// Extract the latest block and checkpoint numbers
|
|
145
|
+
const previousBlockNumber = await this.getLatestBlockNumber();
|
|
146
|
+
const previousCheckpointNumber = await this.getLatestCheckpointNumber();
|
|
147
|
+
|
|
148
|
+
// Check that the first block number is the expected one
|
|
149
|
+
if (!opts.force && previousBlockNumber !== firstBlockNumber - 1) {
|
|
110
150
|
throw new InitialBlockNumberNotSequentialError(firstBlockNumber, previousBlockNumber);
|
|
111
151
|
}
|
|
112
152
|
|
|
113
|
-
//
|
|
114
|
-
|
|
153
|
+
// The same check as above but for checkpoints
|
|
154
|
+
if (!opts.force && previousCheckpointNumber !== firstBlockCheckpointNumber - 1) {
|
|
155
|
+
throw new InitialCheckpointNumberNotSequentialError(firstBlockCheckpointNumber, previousCheckpointNumber);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Extract the previous block if there is one and see if it is for the same checkpoint or not
|
|
159
|
+
const previousBlockResult = await this.getBlock(previousBlockNumber);
|
|
160
|
+
|
|
161
|
+
let expectedFirstblockIndex = 0;
|
|
162
|
+
let previousBlockIndex: number | undefined = undefined;
|
|
163
|
+
if (previousBlockResult !== undefined) {
|
|
164
|
+
if (previousBlockResult.checkpointNumber === firstBlockCheckpointNumber) {
|
|
165
|
+
// The previous block is for the same checkpoint, therefore our index should follow it
|
|
166
|
+
previousBlockIndex = previousBlockResult.indexWithinCheckpoint;
|
|
167
|
+
expectedFirstblockIndex = previousBlockIndex + 1;
|
|
168
|
+
}
|
|
169
|
+
if (!previousBlockResult.archive.root.equals(firstBlockLastArchive)) {
|
|
170
|
+
throw new BlockArchiveNotConsistentError(
|
|
171
|
+
firstBlockNumber,
|
|
172
|
+
previousBlockResult.number,
|
|
173
|
+
firstBlockLastArchive,
|
|
174
|
+
previousBlockResult.archive.root,
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// Now check that the first block has the expected index value
|
|
180
|
+
if (!opts.force && expectedFirstblockIndex !== firstBlockIndex) {
|
|
181
|
+
throw new BlockIndexNotSequentialError(firstBlockIndex, previousBlockIndex);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Iterate over blocks array and insert them, checking that the block numbers and indexes are sequential. Also check they are for the correct checkpoint.
|
|
185
|
+
let previousBlock: L2BlockNew | undefined = undefined;
|
|
115
186
|
for (const block of blocks) {
|
|
116
|
-
if (!opts.force && previousBlock
|
|
117
|
-
|
|
187
|
+
if (!opts.force && previousBlock) {
|
|
188
|
+
if (previousBlock.number + 1 !== block.number) {
|
|
189
|
+
throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
|
|
190
|
+
}
|
|
191
|
+
if (previousBlock.indexWithinCheckpoint + 1 !== block.indexWithinCheckpoint) {
|
|
192
|
+
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
|
|
193
|
+
}
|
|
194
|
+
if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
|
|
195
|
+
throw new BlockArchiveNotConsistentError(
|
|
196
|
+
block.number,
|
|
197
|
+
previousBlock.number,
|
|
198
|
+
block.header.lastArchive.root,
|
|
199
|
+
previousBlock.archive.root,
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
if (!opts.force && firstBlockCheckpointNumber !== block.checkpointNumber) {
|
|
204
|
+
throw new CheckpointNumberNotConsistentError(block.checkpointNumber, firstBlockCheckpointNumber);
|
|
118
205
|
}
|
|
119
206
|
previousBlock = block;
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
l1: block.l1,
|
|
127
|
-
attestations: block.attestations.map(attestation => attestation.toBuffer()),
|
|
128
|
-
});
|
|
207
|
+
await this.addBlockToDatabase(block, block.checkpointNumber, block.indexWithinCheckpoint);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return true;
|
|
211
|
+
});
|
|
212
|
+
}
|
|
129
213
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
214
|
+
/**
|
|
215
|
+
* Append new cheskpoints to the store's list.
|
|
216
|
+
* @param checkpoints - The L2 checkpoints to be added to the store.
|
|
217
|
+
* @returns True if the operation is successful.
|
|
218
|
+
*/
|
|
219
|
+
async addCheckpoints(checkpoints: PublishedCheckpoint[], opts: { force?: boolean } = {}): Promise<boolean> {
|
|
220
|
+
if (checkpoints.length === 0) {
|
|
221
|
+
return true;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return await this.db.transactionAsync(async () => {
|
|
225
|
+
// Check that the checkpoint immediately before the first block to be added is present in the store.
|
|
226
|
+
const firstCheckpointNumber = checkpoints[0].checkpoint.number;
|
|
227
|
+
const previousCheckpointNumber = await this.getLatestCheckpointNumber();
|
|
228
|
+
|
|
229
|
+
if (previousCheckpointNumber !== firstCheckpointNumber - 1 && !opts.force) {
|
|
230
|
+
throw new InitialCheckpointNumberNotSequentialError(firstCheckpointNumber, previousCheckpointNumber);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// Extract the previous checkpoint if there is one
|
|
234
|
+
let previousCheckpointData: CheckpointData | undefined = undefined;
|
|
235
|
+
if (previousCheckpointNumber !== INITIAL_CHECKPOINT_NUMBER - 1) {
|
|
236
|
+
// There should be a previous checkpoint
|
|
237
|
+
previousCheckpointData = await this.getCheckpointData(previousCheckpointNumber);
|
|
238
|
+
if (previousCheckpointData === undefined) {
|
|
239
|
+
throw new CheckpointNotFoundError(previousCheckpointNumber);
|
|
138
240
|
}
|
|
241
|
+
}
|
|
139
242
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
Buffer.concat(block.block.body.txEffects.map(tx => tx.txHash.toBuffer())),
|
|
143
|
-
);
|
|
243
|
+
let previousBlockNumber: BlockNumber | undefined = undefined;
|
|
244
|
+
let previousBlock: L2BlockNew | undefined = undefined;
|
|
144
245
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
246
|
+
// If we have a previous checkpoint then we need to get the previous block number
|
|
247
|
+
if (previousCheckpointData !== undefined) {
|
|
248
|
+
previousBlockNumber = BlockNumber(previousCheckpointData.startBlock + previousCheckpointData.numBlocks - 1);
|
|
249
|
+
previousBlock = await this.getBlock(previousBlockNumber);
|
|
250
|
+
if (previousBlock === undefined) {
|
|
251
|
+
// We should be able to get the required previous block
|
|
252
|
+
throw new BlockNotFoundError(previousBlockNumber);
|
|
253
|
+
}
|
|
148
254
|
}
|
|
149
255
|
|
|
150
|
-
|
|
256
|
+
// Iterate over checkpoints array and insert them, checking that the block numbers are sequential.
|
|
257
|
+
let previousCheckpoint: PublishedCheckpoint | undefined = undefined;
|
|
258
|
+
for (const checkpoint of checkpoints) {
|
|
259
|
+
if (
|
|
260
|
+
!opts.force &&
|
|
261
|
+
previousCheckpoint &&
|
|
262
|
+
previousCheckpoint.checkpoint.number + 1 !== checkpoint.checkpoint.number
|
|
263
|
+
) {
|
|
264
|
+
throw new CheckpointNumberNotSequentialError(
|
|
265
|
+
checkpoint.checkpoint.number,
|
|
266
|
+
previousCheckpoint.checkpoint.number,
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
previousCheckpoint = checkpoint;
|
|
270
|
+
|
|
271
|
+
// Store every block in the database. the block may already exist, but this has come from chain and is assumed to be correct.
|
|
272
|
+
for (let i = 0; i < checkpoint.checkpoint.blocks.length; i++) {
|
|
273
|
+
const block = checkpoint.checkpoint.blocks[i];
|
|
274
|
+
if (previousBlock) {
|
|
275
|
+
// The blocks should have a sequential block number
|
|
276
|
+
if (previousBlock.number !== block.number - 1) {
|
|
277
|
+
throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
|
|
278
|
+
}
|
|
279
|
+
// If the blocks are for the same checkpoint then they should have sequential indexes
|
|
280
|
+
if (
|
|
281
|
+
previousBlock.checkpointNumber === block.checkpointNumber &&
|
|
282
|
+
previousBlock.indexWithinCheckpoint !== block.indexWithinCheckpoint - 1
|
|
283
|
+
) {
|
|
284
|
+
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
|
|
285
|
+
}
|
|
286
|
+
if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
|
|
287
|
+
throw new BlockArchiveNotConsistentError(
|
|
288
|
+
block.number,
|
|
289
|
+
previousBlock.number,
|
|
290
|
+
block.header.lastArchive.root,
|
|
291
|
+
previousBlock.archive.root,
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
} else {
|
|
295
|
+
// No previous block, must be block 1 at checkpoint index 0
|
|
296
|
+
if (block.indexWithinCheckpoint !== 0) {
|
|
297
|
+
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, undefined);
|
|
298
|
+
}
|
|
299
|
+
if (block.number !== INITIAL_L2_BLOCK_NUM) {
|
|
300
|
+
throw new BlockNumberNotSequentialError(block.number, undefined);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
previousBlock = block;
|
|
305
|
+
await this.addBlockToDatabase(block, checkpoint.checkpoint.number, i);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// Store the checkpoint in the database
|
|
309
|
+
await this.#checkpoints.set(checkpoint.checkpoint.number, {
|
|
310
|
+
header: checkpoint.checkpoint.header.toBuffer(),
|
|
311
|
+
archive: checkpoint.checkpoint.archive.toBuffer(),
|
|
312
|
+
l1: checkpoint.l1.toBuffer(),
|
|
313
|
+
attestations: checkpoint.attestations.map(attestation => attestation.toBuffer()),
|
|
314
|
+
checkpointNumber: checkpoint.checkpoint.number,
|
|
315
|
+
startBlock: checkpoint.checkpoint.blocks[0].number,
|
|
316
|
+
numBlocks: checkpoint.checkpoint.blocks.length,
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
await this.#lastSynchedL1Block.set(checkpoints[checkpoints.length - 1].l1.blockNumber);
|
|
151
321
|
return true;
|
|
152
322
|
});
|
|
153
323
|
}
|
|
154
324
|
|
|
325
|
+
private async addBlockToDatabase(block: L2BlockNew, checkpointNumber: number, indexWithinCheckpoint: number) {
|
|
326
|
+
const blockHash = L2BlockHash.fromField(await block.hash());
|
|
327
|
+
|
|
328
|
+
await this.#blocks.set(block.number, {
|
|
329
|
+
header: block.header.toBuffer(),
|
|
330
|
+
blockHash: blockHash.toBuffer(),
|
|
331
|
+
archive: block.archive.toBuffer(),
|
|
332
|
+
checkpointNumber,
|
|
333
|
+
indexWithinCheckpoint,
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
for (let i = 0; i < block.body.txEffects.length; i++) {
|
|
337
|
+
const txEffect: IndexedTxEffect = {
|
|
338
|
+
data: block.body.txEffects[i],
|
|
339
|
+
l2BlockNumber: block.number,
|
|
340
|
+
l2BlockHash: blockHash,
|
|
341
|
+
txIndexInBlock: i,
|
|
342
|
+
};
|
|
343
|
+
await this.#txEffects.set(txEffect.data.txHash.toString(), serializeIndexedTxEffect(txEffect));
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
await this.#blockTxs.set(blockHash.toString(), Buffer.concat(block.body.txEffects.map(tx => tx.txHash.toBuffer())));
|
|
347
|
+
|
|
348
|
+
// Update indices for block hash and archive
|
|
349
|
+
await this.#blockHashIndex.set(blockHash.toString(), block.number);
|
|
350
|
+
await this.#blockArchiveIndex.set(block.archive.root.toString(), block.number);
|
|
351
|
+
}
|
|
352
|
+
|
|
155
353
|
/**
|
|
156
|
-
* Unwinds
|
|
354
|
+
* Unwinds checkpoints from the database
|
|
157
355
|
* @param from - The tip of the chain, passed for verification purposes,
|
|
158
356
|
* ensuring that we don't end up deleting something we did not intend
|
|
159
|
-
* @param
|
|
357
|
+
* @param checkpointsToUnwind - The number of checkpoints we are to unwind
|
|
160
358
|
* @returns True if the operation is successful
|
|
161
359
|
*/
|
|
162
|
-
async
|
|
360
|
+
async unwindCheckpoints(from: CheckpointNumber, checkpointsToUnwind: number) {
|
|
163
361
|
return await this.db.transactionAsync(async () => {
|
|
164
|
-
const last = await this.
|
|
362
|
+
const last = await this.getLatestCheckpointNumber();
|
|
165
363
|
if (from !== last) {
|
|
166
|
-
throw new Error(`Can only unwind
|
|
364
|
+
throw new Error(`Can only unwind checkpoints from the tip (requested ${from} but current tip is ${last})`);
|
|
167
365
|
}
|
|
168
366
|
|
|
169
|
-
const proven = await this.
|
|
170
|
-
if (from -
|
|
171
|
-
await this.
|
|
367
|
+
const proven = await this.getProvenCheckpointNumber();
|
|
368
|
+
if (from - checkpointsToUnwind < proven) {
|
|
369
|
+
await this.setProvenCheckpointNumber(CheckpointNumber(from - checkpointsToUnwind));
|
|
172
370
|
}
|
|
173
371
|
|
|
174
|
-
for (let i = 0; i <
|
|
175
|
-
const
|
|
176
|
-
const
|
|
372
|
+
for (let i = 0; i < checkpointsToUnwind; i++) {
|
|
373
|
+
const checkpointNumber = from - i;
|
|
374
|
+
const checkpoint = await this.#checkpoints.getAsync(checkpointNumber);
|
|
177
375
|
|
|
178
|
-
if (
|
|
179
|
-
this.#log.warn(`Cannot remove
|
|
376
|
+
if (checkpoint === undefined) {
|
|
377
|
+
this.#log.warn(`Cannot remove checkpoint ${checkpointNumber} from the store since we don't have it`);
|
|
180
378
|
continue;
|
|
181
379
|
}
|
|
182
|
-
await this.#
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
380
|
+
await this.#checkpoints.delete(checkpointNumber);
|
|
381
|
+
const maxBlock = checkpoint.startBlock + checkpoint.numBlocks - 1;
|
|
382
|
+
|
|
383
|
+
for (let blockNumber = checkpoint.startBlock; blockNumber <= maxBlock; blockNumber++) {
|
|
384
|
+
const block = await this.getBlock(BlockNumber(blockNumber));
|
|
385
|
+
|
|
386
|
+
if (block === undefined) {
|
|
387
|
+
this.#log.warn(`Cannot remove block ${blockNumber} from the store since we don't have it`);
|
|
388
|
+
continue;
|
|
389
|
+
}
|
|
390
|
+
await this.#blocks.delete(block.number);
|
|
391
|
+
await Promise.all(block.body.txEffects.map(tx => this.#txEffects.delete(tx.txHash.toString())));
|
|
392
|
+
const blockHash = (await block.hash()).toString();
|
|
393
|
+
await this.#blockTxs.delete(blockHash);
|
|
394
|
+
|
|
395
|
+
// Clean up indices
|
|
396
|
+
await this.#blockHashIndex.delete(blockHash);
|
|
397
|
+
await this.#blockArchiveIndex.delete(block.archive.root.toString());
|
|
398
|
+
|
|
399
|
+
this.#log.debug(`Unwound block ${blockNumber} ${blockHash} for checkpoint ${checkpointNumber}`);
|
|
400
|
+
}
|
|
192
401
|
}
|
|
193
402
|
|
|
194
403
|
return true;
|
|
195
404
|
});
|
|
196
405
|
}
|
|
197
406
|
|
|
407
|
+
async getCheckpointData(checkpointNumber: CheckpointNumber): Promise<CheckpointData | undefined> {
|
|
408
|
+
const checkpointStorage = await this.#checkpoints.getAsync(checkpointNumber);
|
|
409
|
+
if (!checkpointStorage) {
|
|
410
|
+
return undefined;
|
|
411
|
+
}
|
|
412
|
+
return this.checkpointDataFromCheckpointStorage(checkpointStorage);
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
async getRangeOfCheckpoints(from: CheckpointNumber, limit: number): Promise<CheckpointData[]> {
|
|
416
|
+
const checkpoints: CheckpointData[] = [];
|
|
417
|
+
for (let checkpointNumber = from; checkpointNumber < from + limit; checkpointNumber++) {
|
|
418
|
+
const checkpoint = await this.#checkpoints.getAsync(checkpointNumber);
|
|
419
|
+
if (!checkpoint) {
|
|
420
|
+
break;
|
|
421
|
+
}
|
|
422
|
+
checkpoints.push(this.checkpointDataFromCheckpointStorage(checkpoint));
|
|
423
|
+
}
|
|
424
|
+
return checkpoints;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
private checkpointDataFromCheckpointStorage(checkpointStorage: CheckpointStorage) {
|
|
428
|
+
const data: CheckpointData = {
|
|
429
|
+
header: CheckpointHeader.fromBuffer(checkpointStorage.header),
|
|
430
|
+
archive: AppendOnlyTreeSnapshot.fromBuffer(checkpointStorage.archive),
|
|
431
|
+
checkpointNumber: CheckpointNumber(checkpointStorage.checkpointNumber),
|
|
432
|
+
startBlock: checkpointStorage.startBlock,
|
|
433
|
+
numBlocks: checkpointStorage.numBlocks,
|
|
434
|
+
l1: L1PublishedData.fromBuffer(checkpointStorage.l1),
|
|
435
|
+
attestations: checkpointStorage.attestations,
|
|
436
|
+
};
|
|
437
|
+
return data;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
async getBlocksForCheckpoint(checkpointNumber: CheckpointNumber): Promise<L2BlockNew[] | undefined> {
|
|
441
|
+
const checkpoint = await this.#checkpoints.getAsync(checkpointNumber);
|
|
442
|
+
if (!checkpoint) {
|
|
443
|
+
return undefined;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
const blocksForCheckpoint = await toArray(
|
|
447
|
+
this.#blocks.entriesAsync({
|
|
448
|
+
start: checkpoint.startBlock,
|
|
449
|
+
end: checkpoint.startBlock + checkpoint.numBlocks,
|
|
450
|
+
}),
|
|
451
|
+
);
|
|
452
|
+
|
|
453
|
+
const converted = await Promise.all(blocksForCheckpoint.map(x => this.getBlockFromBlockStorage(x[0], x[1])));
|
|
454
|
+
return converted.filter(isDefined);
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
async getProvenBlockNumber(): Promise<BlockNumber> {
|
|
458
|
+
const provenCheckpointNumber = await this.getProvenCheckpointNumber();
|
|
459
|
+
if (provenCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
|
|
460
|
+
return BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
461
|
+
}
|
|
462
|
+
const checkpointStorage = await this.#checkpoints.getAsync(provenCheckpointNumber);
|
|
463
|
+
if (!checkpointStorage) {
|
|
464
|
+
throw new CheckpointNotFoundError(provenCheckpointNumber);
|
|
465
|
+
} else {
|
|
466
|
+
return BlockNumber(checkpointStorage.startBlock + checkpointStorage.numBlocks - 1);
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
async getLatestBlockNumber(): Promise<BlockNumber> {
|
|
471
|
+
const [latestBlocknumber] = await toArray(this.#blocks.keysAsync({ reverse: true, limit: 1 }));
|
|
472
|
+
return typeof latestBlocknumber === 'number'
|
|
473
|
+
? BlockNumber(latestBlocknumber)
|
|
474
|
+
: BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
async getLatestCheckpointNumber(): Promise<CheckpointNumber> {
|
|
478
|
+
const [latestCheckpointNumber] = await toArray(this.#checkpoints.keysAsync({ reverse: true, limit: 1 }));
|
|
479
|
+
if (latestCheckpointNumber === undefined) {
|
|
480
|
+
return CheckpointNumber(INITIAL_CHECKPOINT_NUMBER - 1);
|
|
481
|
+
}
|
|
482
|
+
return CheckpointNumber(latestCheckpointNumber);
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
async getCheckpointedBlock(number: BlockNumber): Promise<CheckpointedL2Block | undefined> {
|
|
486
|
+
const blockStorage = await this.#blocks.getAsync(number);
|
|
487
|
+
if (!blockStorage) {
|
|
488
|
+
return undefined;
|
|
489
|
+
}
|
|
490
|
+
const checkpoint = await this.#checkpoints.getAsync(blockStorage.checkpointNumber);
|
|
491
|
+
if (!checkpoint) {
|
|
492
|
+
return undefined;
|
|
493
|
+
}
|
|
494
|
+
const block = await this.getBlockFromBlockStorage(number, blockStorage);
|
|
495
|
+
if (!block) {
|
|
496
|
+
return undefined;
|
|
497
|
+
}
|
|
498
|
+
return new CheckpointedL2Block(
|
|
499
|
+
CheckpointNumber(checkpoint.checkpointNumber),
|
|
500
|
+
block,
|
|
501
|
+
L1PublishedData.fromBuffer(checkpoint.l1),
|
|
502
|
+
checkpoint.attestations.map(buf => CommitteeAttestation.fromBuffer(buf)),
|
|
503
|
+
);
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
async getCheckpointedBlockByHash(blockHash: Fr): Promise<CheckpointedL2Block | undefined> {
|
|
507
|
+
const blockNumber = await this.#blockHashIndex.getAsync(blockHash.toString());
|
|
508
|
+
if (blockNumber === undefined) {
|
|
509
|
+
return undefined;
|
|
510
|
+
}
|
|
511
|
+
return this.getCheckpointedBlock(BlockNumber(blockNumber));
|
|
512
|
+
}
|
|
513
|
+
async getCheckpointedBlockByArchive(archive: Fr): Promise<CheckpointedL2Block | undefined> {
|
|
514
|
+
const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
|
|
515
|
+
if (blockNumber === undefined) {
|
|
516
|
+
return undefined;
|
|
517
|
+
}
|
|
518
|
+
return this.getCheckpointedBlock(BlockNumber(blockNumber));
|
|
519
|
+
}
|
|
520
|
+
|
|
198
521
|
/**
|
|
199
522
|
* Gets up to `limit` amount of L2 blocks starting from `from`.
|
|
200
523
|
* @param start - Number of the first block to return (inclusive).
|
|
201
524
|
* @param limit - The number of blocks to return.
|
|
202
525
|
* @returns The requested L2 blocks
|
|
203
526
|
*/
|
|
204
|
-
async *getBlocks(start: BlockNumber, limit: number): AsyncIterableIterator<
|
|
527
|
+
async *getBlocks(start: BlockNumber, limit: number): AsyncIterableIterator<L2BlockNew> {
|
|
205
528
|
for await (const [blockNumber, blockStorage] of this.getBlockStorages(start, limit)) {
|
|
206
529
|
const block = await this.getBlockFromBlockStorage(blockNumber, blockStorage);
|
|
207
530
|
if (block) {
|
|
@@ -215,7 +538,7 @@ export class BlockStore {
|
|
|
215
538
|
* @param blockNumber - The number of the block to return.
|
|
216
539
|
* @returns The requested L2 block.
|
|
217
540
|
*/
|
|
218
|
-
async getBlock(blockNumber: BlockNumber): Promise<
|
|
541
|
+
async getBlock(blockNumber: BlockNumber): Promise<L2BlockNew | undefined> {
|
|
219
542
|
const blockStorage = await this.#blocks.getAsync(blockNumber);
|
|
220
543
|
if (!blockStorage || !blockStorage.header) {
|
|
221
544
|
return Promise.resolve(undefined);
|
|
@@ -228,7 +551,7 @@ export class BlockStore {
|
|
|
228
551
|
* @param blockHash - The hash of the block to return.
|
|
229
552
|
* @returns The requested L2 block.
|
|
230
553
|
*/
|
|
231
|
-
async getBlockByHash(blockHash: L2BlockHash): Promise<
|
|
554
|
+
async getBlockByHash(blockHash: L2BlockHash): Promise<L2BlockNew | undefined> {
|
|
232
555
|
const blockNumber = await this.#blockHashIndex.getAsync(blockHash.toString());
|
|
233
556
|
if (blockNumber === undefined) {
|
|
234
557
|
return undefined;
|
|
@@ -241,7 +564,7 @@ export class BlockStore {
|
|
|
241
564
|
* @param archive - The archive root of the block to return.
|
|
242
565
|
* @returns The requested L2 block.
|
|
243
566
|
*/
|
|
244
|
-
async getBlockByArchive(archive: Fr): Promise<
|
|
567
|
+
async getBlockByArchive(archive: Fr): Promise<L2BlockNew | undefined> {
|
|
245
568
|
const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
|
|
246
569
|
if (blockNumber === undefined) {
|
|
247
570
|
return undefined;
|
|
@@ -263,7 +586,7 @@ export class BlockStore {
|
|
|
263
586
|
if (!blockStorage || !blockStorage.header) {
|
|
264
587
|
return undefined;
|
|
265
588
|
}
|
|
266
|
-
return
|
|
589
|
+
return BlockHeader.fromBuffer(blockStorage.header);
|
|
267
590
|
}
|
|
268
591
|
|
|
269
592
|
/**
|
|
@@ -280,7 +603,7 @@ export class BlockStore {
|
|
|
280
603
|
if (!blockStorage || !blockStorage.header) {
|
|
281
604
|
return undefined;
|
|
282
605
|
}
|
|
283
|
-
return
|
|
606
|
+
return BlockHeader.fromBuffer(blockStorage.header);
|
|
284
607
|
}
|
|
285
608
|
|
|
286
609
|
/**
|
|
@@ -291,7 +614,7 @@ export class BlockStore {
|
|
|
291
614
|
*/
|
|
292
615
|
async *getBlockHeaders(start: BlockNumber, limit: number): AsyncIterableIterator<BlockHeader> {
|
|
293
616
|
for await (const [blockNumber, blockStorage] of this.getBlockStorages(start, limit)) {
|
|
294
|
-
const header =
|
|
617
|
+
const header = BlockHeader.fromBuffer(blockStorage.header);
|
|
295
618
|
if (header.getBlockNumber() !== blockNumber) {
|
|
296
619
|
throw new Error(
|
|
297
620
|
`Block number mismatch when retrieving block header from archive (expected ${blockNumber} but got ${header.getBlockNumber()})`,
|
|
@@ -317,8 +640,8 @@ export class BlockStore {
|
|
|
317
640
|
private async getBlockFromBlockStorage(
|
|
318
641
|
blockNumber: number,
|
|
319
642
|
blockStorage: BlockStorage,
|
|
320
|
-
): Promise<
|
|
321
|
-
const header =
|
|
643
|
+
): Promise<L2BlockNew | undefined> {
|
|
644
|
+
const header = BlockHeader.fromBuffer(blockStorage.header);
|
|
322
645
|
const archive = AppendOnlyTreeSnapshot.fromBuffer(blockStorage.archive);
|
|
323
646
|
const blockHash = blockStorage.blockHash;
|
|
324
647
|
const blockHashString = bufferToHex(blockHash);
|
|
@@ -340,7 +663,13 @@ export class BlockStore {
|
|
|
340
663
|
txEffects.push(deserializeIndexedTxEffect(txEffect).data);
|
|
341
664
|
}
|
|
342
665
|
const body = new Body(txEffects);
|
|
343
|
-
const block = new
|
|
666
|
+
const block = new L2BlockNew(
|
|
667
|
+
archive,
|
|
668
|
+
header,
|
|
669
|
+
body,
|
|
670
|
+
CheckpointNumber(blockStorage.checkpointNumber!),
|
|
671
|
+
blockStorage.indexWithinCheckpoint,
|
|
672
|
+
);
|
|
344
673
|
|
|
345
674
|
if (block.number !== blockNumber) {
|
|
346
675
|
throw new Error(
|
|
@@ -349,8 +678,7 @@ export class BlockStore {
|
|
|
349
678
|
} with hash ${blockHashString})`,
|
|
350
679
|
);
|
|
351
680
|
}
|
|
352
|
-
|
|
353
|
-
return PublishedL2Block.fromFields({ block, l1: blockStorage.l1, attestations });
|
|
681
|
+
return block;
|
|
354
682
|
}
|
|
355
683
|
|
|
356
684
|
/**
|
|
@@ -411,10 +739,19 @@ export class BlockStore {
|
|
|
411
739
|
}
|
|
412
740
|
|
|
413
741
|
/**
|
|
414
|
-
* Gets the number of the latest L2 block
|
|
415
|
-
* @returns The number of the latest L2 block
|
|
742
|
+
* Gets the number of the latest L2 block checkpointed.
|
|
743
|
+
* @returns The number of the latest L2 block checkpointed.
|
|
416
744
|
*/
|
|
417
|
-
async
|
|
745
|
+
async getCheckpointedL2BlockNumber(): Promise<BlockNumber> {
|
|
746
|
+
const latestCheckpointNumber = await this.getLatestCheckpointNumber();
|
|
747
|
+
const checkpoint = await this.getCheckpointData(latestCheckpointNumber);
|
|
748
|
+
if (!checkpoint) {
|
|
749
|
+
return BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
750
|
+
}
|
|
751
|
+
return BlockNumber(checkpoint.startBlock + checkpoint.numBlocks - 1);
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
async getLatestL2BlockNumber(): Promise<BlockNumber> {
|
|
418
755
|
const [lastBlockNumber] = await toArray(this.#blocks.keysAsync({ reverse: true, limit: 1 }));
|
|
419
756
|
return typeof lastBlockNumber === 'number' ? BlockNumber(lastBlockNumber) : BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
420
757
|
}
|
|
@@ -431,16 +768,19 @@ export class BlockStore {
|
|
|
431
768
|
return this.#lastSynchedL1Block.set(l1BlockNumber);
|
|
432
769
|
}
|
|
433
770
|
|
|
434
|
-
async
|
|
435
|
-
const [
|
|
436
|
-
this.
|
|
437
|
-
this.#
|
|
771
|
+
async getProvenCheckpointNumber(): Promise<CheckpointNumber> {
|
|
772
|
+
const [latestCheckpointNumber, provenCheckpointNumber] = await Promise.all([
|
|
773
|
+
this.getLatestCheckpointNumber(),
|
|
774
|
+
this.#lastProvenCheckpoint.getAsync(),
|
|
438
775
|
]);
|
|
439
|
-
return (
|
|
776
|
+
return (provenCheckpointNumber ?? 0) > latestCheckpointNumber
|
|
777
|
+
? latestCheckpointNumber
|
|
778
|
+
: CheckpointNumber(provenCheckpointNumber ?? 0);
|
|
440
779
|
}
|
|
441
780
|
|
|
442
|
-
|
|
443
|
-
|
|
781
|
+
async setProvenCheckpointNumber(checkpointNumber: CheckpointNumber) {
|
|
782
|
+
const result = await this.#lastProvenCheckpoint.set(checkpointNumber);
|
|
783
|
+
return result;
|
|
444
784
|
}
|
|
445
785
|
|
|
446
786
|
#computeBlockRange(start: BlockNumber, limit: number): Required<Pick<Range<number>, 'start' | 'limit'>> {
|