@aztec/archiver 0.0.1-commit.f504929 → 0.0.1-commit.f5d02921e
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.d.ts +5 -5
- package/dest/archiver.d.ts.map +1 -1
- package/dest/archiver.js +57 -19
- package/dest/config.d.ts +3 -3
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +2 -1
- package/dest/errors.d.ts +34 -10
- package/dest/errors.d.ts.map +1 -1
- package/dest/errors.js +45 -16
- package/dest/factory.d.ts +4 -5
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +24 -21
- package/dest/l1/calldata_retriever.d.ts +1 -1
- package/dest/l1/calldata_retriever.d.ts.map +1 -1
- package/dest/l1/calldata_retriever.js +2 -1
- package/dest/modules/data_source_base.d.ts +8 -6
- package/dest/modules/data_source_base.d.ts.map +1 -1
- package/dest/modules/data_source_base.js +11 -5
- package/dest/modules/data_store_updater.d.ts +18 -12
- package/dest/modules/data_store_updater.d.ts.map +1 -1
- package/dest/modules/data_store_updater.js +87 -77
- package/dest/modules/l1_synchronizer.d.ts +2 -1
- package/dest/modules/l1_synchronizer.d.ts.map +1 -1
- package/dest/modules/l1_synchronizer.js +60 -25
- package/dest/modules/validation.d.ts +1 -1
- package/dest/modules/validation.d.ts.map +1 -1
- package/dest/modules/validation.js +2 -2
- package/dest/store/block_store.d.ts +49 -16
- package/dest/store/block_store.d.ts.map +1 -1
- package/dest/store/block_store.js +243 -118
- package/dest/store/contract_class_store.d.ts +2 -3
- package/dest/store/contract_class_store.d.ts.map +1 -1
- package/dest/store/contract_class_store.js +7 -67
- package/dest/store/contract_instance_store.d.ts +1 -1
- package/dest/store/contract_instance_store.d.ts.map +1 -1
- package/dest/store/contract_instance_store.js +6 -2
- package/dest/store/kv_archiver_store.d.ts +46 -19
- package/dest/store/kv_archiver_store.d.ts.map +1 -1
- package/dest/store/kv_archiver_store.js +57 -22
- package/dest/store/l2_tips_cache.d.ts +2 -1
- package/dest/store/l2_tips_cache.d.ts.map +1 -1
- package/dest/store/l2_tips_cache.js +25 -5
- package/dest/store/log_store.d.ts +6 -3
- package/dest/store/log_store.d.ts.map +1 -1
- package/dest/store/log_store.js +93 -16
- package/dest/store/message_store.d.ts +5 -1
- package/dest/store/message_store.d.ts.map +1 -1
- package/dest/store/message_store.js +14 -1
- package/dest/test/fake_l1_state.d.ts +8 -1
- package/dest/test/fake_l1_state.d.ts.map +1 -1
- package/dest/test/fake_l1_state.js +39 -5
- 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 +2 -1
- package/dest/test/mock_l2_block_source.d.ts +10 -4
- package/dest/test/mock_l2_block_source.d.ts.map +1 -1
- package/dest/test/mock_l2_block_source.js +35 -7
- package/dest/test/mock_structs.d.ts +4 -1
- package/dest/test/mock_structs.d.ts.map +1 -1
- package/dest/test/mock_structs.js +13 -1
- package/dest/test/noop_l1_archiver.d.ts +4 -1
- package/dest/test/noop_l1_archiver.d.ts.map +1 -1
- package/dest/test/noop_l1_archiver.js +5 -1
- package/package.json +13 -13
- package/src/archiver.ts +65 -21
- package/src/config.ts +8 -1
- package/src/errors.ts +70 -26
- package/src/factory.ts +23 -15
- package/src/l1/calldata_retriever.ts +2 -1
- package/src/modules/data_source_base.ts +26 -7
- package/src/modules/data_store_updater.ts +96 -107
- package/src/modules/l1_synchronizer.ts +72 -32
- package/src/modules/validation.ts +2 -2
- package/src/store/block_store.ts +312 -138
- package/src/store/contract_class_store.ts +8 -106
- package/src/store/contract_instance_store.ts +8 -5
- package/src/store/kv_archiver_store.ts +83 -34
- package/src/store/l2_tips_cache.ts +50 -11
- package/src/store/log_store.ts +126 -27
- package/src/store/message_store.ts +20 -1
- package/src/test/fake_l1_state.ts +50 -9
- package/src/test/mock_l1_to_l2_message_source.ts +1 -0
- package/src/test/mock_l2_block_source.ts +52 -5
- package/src/test/mock_structs.ts +20 -6
- package/src/test/noop_l1_archiver.ts +7 -1
package/src/store/block_store.ts
CHANGED
|
@@ -19,8 +19,16 @@ import {
|
|
|
19
19
|
deserializeValidateCheckpointResult,
|
|
20
20
|
serializeValidateCheckpointResult,
|
|
21
21
|
} from '@aztec/stdlib/block';
|
|
22
|
-
import {
|
|
23
|
-
|
|
22
|
+
import {
|
|
23
|
+
Checkpoint,
|
|
24
|
+
type CheckpointData,
|
|
25
|
+
type CommonCheckpointData,
|
|
26
|
+
L1PublishedData,
|
|
27
|
+
type ProposedCheckpointData,
|
|
28
|
+
type ProposedCheckpointInput,
|
|
29
|
+
PublishedCheckpoint,
|
|
30
|
+
} from '@aztec/stdlib/checkpoint';
|
|
31
|
+
import { type L1RollupConstants, getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
|
|
24
32
|
import { CheckpointHeader } from '@aztec/stdlib/rollup';
|
|
25
33
|
import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
|
|
26
34
|
import {
|
|
@@ -35,16 +43,17 @@ import {
|
|
|
35
43
|
} from '@aztec/stdlib/tx';
|
|
36
44
|
|
|
37
45
|
import {
|
|
46
|
+
BlockAlreadyCheckpointedError,
|
|
38
47
|
BlockArchiveNotConsistentError,
|
|
39
48
|
BlockIndexNotSequentialError,
|
|
40
49
|
BlockNotFoundError,
|
|
41
50
|
BlockNumberNotSequentialError,
|
|
42
51
|
CannotOverwriteCheckpointedBlockError,
|
|
43
52
|
CheckpointNotFoundError,
|
|
44
|
-
CheckpointNumberNotConsistentError,
|
|
45
53
|
CheckpointNumberNotSequentialError,
|
|
46
|
-
InitialBlockNumberNotSequentialError,
|
|
47
54
|
InitialCheckpointNumberNotSequentialError,
|
|
55
|
+
ProposedCheckpointNotSequentialError,
|
|
56
|
+
ProposedCheckpointStaleError,
|
|
48
57
|
} from '../errors.js';
|
|
49
58
|
|
|
50
59
|
export { TxReceipt, type TxEffect, type TxHash } from '@aztec/stdlib/tx';
|
|
@@ -59,17 +68,27 @@ type BlockStorage = {
|
|
|
59
68
|
indexWithinCheckpoint: number;
|
|
60
69
|
};
|
|
61
70
|
|
|
62
|
-
|
|
71
|
+
/** Checkpoint Storage shared between Checkpoints + Proposed Checkpoints */
|
|
72
|
+
type CommonCheckpointStorage = {
|
|
63
73
|
header: Buffer;
|
|
64
74
|
archive: Buffer;
|
|
65
75
|
checkpointOutHash: Buffer;
|
|
66
76
|
checkpointNumber: number;
|
|
67
77
|
startBlock: number;
|
|
68
78
|
blockCount: number;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
type CheckpointStorage = CommonCheckpointStorage & {
|
|
69
82
|
l1: Buffer;
|
|
70
83
|
attestations: Buffer[];
|
|
71
84
|
};
|
|
72
85
|
|
|
86
|
+
/** Storage format for a proposed checkpoint (attested but not yet L1-confirmed). */
|
|
87
|
+
type ProposedCheckpointStorage = CommonCheckpointStorage & {
|
|
88
|
+
totalManaUsed: string;
|
|
89
|
+
feeAssetPriceModifier: string;
|
|
90
|
+
};
|
|
91
|
+
|
|
73
92
|
export type RemoveCheckpointsResult = { blocksRemoved: L2Block[] | undefined };
|
|
74
93
|
|
|
75
94
|
/**
|
|
@@ -97,6 +116,9 @@ export class BlockStore {
|
|
|
97
116
|
/** Stores last proven checkpoint */
|
|
98
117
|
#lastProvenCheckpoint: AztecAsyncSingleton<number>;
|
|
99
118
|
|
|
119
|
+
/** Stores last finalized checkpoint (proven at or before the finalized L1 block) */
|
|
120
|
+
#lastFinalizedCheckpoint: AztecAsyncSingleton<number>;
|
|
121
|
+
|
|
100
122
|
/** Stores the pending chain validation status */
|
|
101
123
|
#pendingChainValidationStatus: AztecAsyncSingleton<Buffer>;
|
|
102
124
|
|
|
@@ -109,12 +131,12 @@ export class BlockStore {
|
|
|
109
131
|
/** Index mapping block archive to block number */
|
|
110
132
|
#blockArchiveIndex: AztecAsyncMap<string, number>;
|
|
111
133
|
|
|
134
|
+
/** Singleton: assumes max 1-deep pipeline. For deeper pipelining, replace with a map keyed by checkpoint number. */
|
|
135
|
+
#proposedCheckpoint: AztecAsyncSingleton<ProposedCheckpointStorage>;
|
|
136
|
+
|
|
112
137
|
#log = createLogger('archiver:block_store');
|
|
113
138
|
|
|
114
|
-
constructor(
|
|
115
|
-
private db: AztecAsyncKVStore,
|
|
116
|
-
private l1Constants: Pick<L1RollupConstants, 'epochDuration'>,
|
|
117
|
-
) {
|
|
139
|
+
constructor(private db: AztecAsyncKVStore) {
|
|
118
140
|
this.#blocks = db.openMap('archiver_blocks');
|
|
119
141
|
this.#blockTxs = db.openMap('archiver_block_txs');
|
|
120
142
|
this.#txEffects = db.openMap('archiver_tx_effects');
|
|
@@ -123,120 +145,114 @@ export class BlockStore {
|
|
|
123
145
|
this.#blockArchiveIndex = db.openMap('archiver_block_archive_index');
|
|
124
146
|
this.#lastSynchedL1Block = db.openSingleton('archiver_last_synched_l1_block');
|
|
125
147
|
this.#lastProvenCheckpoint = db.openSingleton('archiver_last_proven_l2_checkpoint');
|
|
148
|
+
this.#lastFinalizedCheckpoint = db.openSingleton('archiver_last_finalized_l2_checkpoint');
|
|
126
149
|
this.#pendingChainValidationStatus = db.openSingleton('archiver_pending_chain_validation_status');
|
|
127
150
|
this.#checkpoints = db.openMap('archiver_checkpoints');
|
|
128
151
|
this.#slotToCheckpoint = db.openMap('archiver_slot_to_checkpoint');
|
|
152
|
+
this.#proposedCheckpoint = db.openSingleton('proposed_checkpoint_data');
|
|
129
153
|
}
|
|
130
154
|
|
|
131
155
|
/**
|
|
132
|
-
*
|
|
133
|
-
*
|
|
134
|
-
* TODO(#13569): Compute proper finalized block number based on L1 finalized block.
|
|
135
|
-
* TODO(palla/mbps): Even the provisional computation is wrong, since it should subtract checkpoints, not blocks
|
|
156
|
+
* Returns the finalized L2 block number. An L2 block is finalized when it was proven
|
|
157
|
+
* in an L1 block that has itself been finalized on Ethereum.
|
|
136
158
|
* @returns The finalized block number.
|
|
137
159
|
*/
|
|
138
160
|
async getFinalizedL2BlockNumber(): Promise<BlockNumber> {
|
|
139
|
-
const
|
|
140
|
-
|
|
161
|
+
const finalizedCheckpointNumber = await this.getFinalizedCheckpointNumber();
|
|
162
|
+
if (finalizedCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
|
|
163
|
+
return BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
164
|
+
}
|
|
165
|
+
const checkpointStorage = await this.#checkpoints.getAsync(finalizedCheckpointNumber);
|
|
166
|
+
if (!checkpointStorage) {
|
|
167
|
+
throw new CheckpointNotFoundError(finalizedCheckpointNumber);
|
|
168
|
+
}
|
|
169
|
+
return BlockNumber(checkpointStorage.startBlock + checkpointStorage.blockCount - 1);
|
|
141
170
|
}
|
|
142
171
|
|
|
143
172
|
/**
|
|
144
|
-
* Append new proposed
|
|
145
|
-
*
|
|
173
|
+
* Append a new proposed block to the store.
|
|
174
|
+
* This is an uncheckpointed block that has been proposed by the sequencer but not yet included in a checkpoint on L1.
|
|
146
175
|
* For checkpointed blocks (already published to L1), use addCheckpoints() instead.
|
|
147
|
-
* @param
|
|
176
|
+
* @param block - The proposed L2 block to be added to the store.
|
|
148
177
|
* @returns True if the operation is successful.
|
|
149
178
|
*/
|
|
150
|
-
async
|
|
151
|
-
if (blocks.length === 0) {
|
|
152
|
-
return true;
|
|
153
|
-
}
|
|
154
|
-
|
|
179
|
+
async addProposedBlock(block: L2Block, opts: { force?: boolean } = {}): Promise<boolean> {
|
|
155
180
|
return await this.db.transactionAsync(async () => {
|
|
156
|
-
|
|
157
|
-
const
|
|
158
|
-
const
|
|
159
|
-
const
|
|
160
|
-
const firstBlockLastArchive = blocks[0].header.lastArchive.root;
|
|
181
|
+
const blockNumber = block.number;
|
|
182
|
+
const blockCheckpointNumber = block.checkpointNumber;
|
|
183
|
+
const blockIndex = block.indexWithinCheckpoint;
|
|
184
|
+
const blockLastArchive = block.header.lastArchive.root;
|
|
161
185
|
|
|
162
186
|
// Extract the latest block and checkpoint numbers
|
|
163
|
-
const previousBlockNumber = await this.
|
|
187
|
+
const previousBlockNumber = await this.getLatestL2BlockNumber();
|
|
188
|
+
const proposedCheckpointNumber = await this.getProposedCheckpointNumber();
|
|
164
189
|
const previousCheckpointNumber = await this.getLatestCheckpointNumber();
|
|
165
190
|
|
|
166
191
|
// Verify we're not overwriting checkpointed blocks
|
|
167
192
|
const lastCheckpointedBlockNumber = await this.getCheckpointedL2BlockNumber();
|
|
168
|
-
if (!opts.force &&
|
|
169
|
-
|
|
193
|
+
if (!opts.force && blockNumber <= lastCheckpointedBlockNumber) {
|
|
194
|
+
// Check if the proposed block matches the already-checkpointed one
|
|
195
|
+
const existingBlock = await this.getBlock(BlockNumber(blockNumber));
|
|
196
|
+
if (existingBlock && existingBlock.archive.root.equals(block.archive.root)) {
|
|
197
|
+
throw new BlockAlreadyCheckpointedError(blockNumber);
|
|
198
|
+
}
|
|
199
|
+
throw new CannotOverwriteCheckpointedBlockError(blockNumber, lastCheckpointedBlockNumber);
|
|
170
200
|
}
|
|
171
201
|
|
|
172
|
-
// Check that the
|
|
173
|
-
if (!opts.force && previousBlockNumber !==
|
|
174
|
-
throw new
|
|
202
|
+
// Check that the block number is the expected one
|
|
203
|
+
if (!opts.force && previousBlockNumber !== blockNumber - 1) {
|
|
204
|
+
throw new BlockNumberNotSequentialError(blockNumber, previousBlockNumber);
|
|
175
205
|
}
|
|
176
206
|
|
|
177
|
-
// The same check as above but for checkpoints
|
|
178
|
-
|
|
179
|
-
|
|
207
|
+
// The same check as above but for checkpoints. Accept the block if either the confirmed
|
|
208
|
+
// checkpoint or the pending (locally validated but not yet confirmed) checkpoint matches.
|
|
209
|
+
const expectedCheckpointNumber = blockCheckpointNumber - 1;
|
|
210
|
+
if (
|
|
211
|
+
!opts.force &&
|
|
212
|
+
previousCheckpointNumber !== expectedCheckpointNumber &&
|
|
213
|
+
proposedCheckpointNumber !== expectedCheckpointNumber
|
|
214
|
+
) {
|
|
215
|
+
const [reported, source]: [CheckpointNumber, 'confirmed' | 'proposed'] =
|
|
216
|
+
proposedCheckpointNumber > previousCheckpointNumber
|
|
217
|
+
? [proposedCheckpointNumber, 'proposed']
|
|
218
|
+
: [previousCheckpointNumber, 'confirmed'];
|
|
219
|
+
throw new CheckpointNumberNotSequentialError(blockCheckpointNumber, reported, source);
|
|
180
220
|
}
|
|
181
221
|
|
|
182
222
|
// Extract the previous block if there is one and see if it is for the same checkpoint or not
|
|
183
223
|
const previousBlockResult = await this.getBlock(previousBlockNumber);
|
|
184
224
|
|
|
185
|
-
let
|
|
225
|
+
let expectedBlockIndex = 0;
|
|
186
226
|
let previousBlockIndex: number | undefined = undefined;
|
|
187
227
|
if (previousBlockResult !== undefined) {
|
|
188
|
-
if (previousBlockResult.checkpointNumber ===
|
|
228
|
+
if (previousBlockResult.checkpointNumber === blockCheckpointNumber) {
|
|
189
229
|
// The previous block is for the same checkpoint, therefore our index should follow it
|
|
190
230
|
previousBlockIndex = previousBlockResult.indexWithinCheckpoint;
|
|
191
|
-
|
|
231
|
+
expectedBlockIndex = previousBlockIndex + 1;
|
|
192
232
|
}
|
|
193
|
-
if (!previousBlockResult.archive.root.equals(
|
|
233
|
+
if (!previousBlockResult.archive.root.equals(blockLastArchive)) {
|
|
194
234
|
throw new BlockArchiveNotConsistentError(
|
|
195
|
-
|
|
235
|
+
blockNumber,
|
|
196
236
|
previousBlockResult.number,
|
|
197
|
-
|
|
237
|
+
blockLastArchive,
|
|
198
238
|
previousBlockResult.archive.root,
|
|
199
239
|
);
|
|
200
240
|
}
|
|
201
241
|
}
|
|
202
242
|
|
|
203
|
-
// Now check that the
|
|
204
|
-
if (!opts.force &&
|
|
205
|
-
throw new BlockIndexNotSequentialError(
|
|
243
|
+
// Now check that the block has the expected index value
|
|
244
|
+
if (!opts.force && expectedBlockIndex !== blockIndex) {
|
|
245
|
+
throw new BlockIndexNotSequentialError(blockIndex, previousBlockIndex);
|
|
206
246
|
}
|
|
207
247
|
|
|
208
|
-
|
|
209
|
-
let previousBlock: L2Block | undefined = undefined;
|
|
210
|
-
for (const block of blocks) {
|
|
211
|
-
if (!opts.force && previousBlock) {
|
|
212
|
-
if (previousBlock.number + 1 !== block.number) {
|
|
213
|
-
throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
|
|
214
|
-
}
|
|
215
|
-
if (previousBlock.indexWithinCheckpoint + 1 !== block.indexWithinCheckpoint) {
|
|
216
|
-
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
|
|
217
|
-
}
|
|
218
|
-
if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
|
|
219
|
-
throw new BlockArchiveNotConsistentError(
|
|
220
|
-
block.number,
|
|
221
|
-
previousBlock.number,
|
|
222
|
-
block.header.lastArchive.root,
|
|
223
|
-
previousBlock.archive.root,
|
|
224
|
-
);
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
if (!opts.force && firstBlockCheckpointNumber !== block.checkpointNumber) {
|
|
228
|
-
throw new CheckpointNumberNotConsistentError(block.checkpointNumber, firstBlockCheckpointNumber);
|
|
229
|
-
}
|
|
230
|
-
previousBlock = block;
|
|
231
|
-
await this.addBlockToDatabase(block, block.checkpointNumber, block.indexWithinCheckpoint);
|
|
232
|
-
}
|
|
248
|
+
await this.addBlockToDatabase(block, block.checkpointNumber, block.indexWithinCheckpoint);
|
|
233
249
|
|
|
234
250
|
return true;
|
|
235
251
|
});
|
|
236
252
|
}
|
|
237
253
|
|
|
238
254
|
/**
|
|
239
|
-
* Append new
|
|
255
|
+
* Append new checkpoints to the store's list.
|
|
240
256
|
* @param checkpoints - The L2 checkpoints to be added to the store.
|
|
241
257
|
* @returns True if the operation is successful.
|
|
242
258
|
*/
|
|
@@ -254,28 +270,8 @@ export class BlockStore {
|
|
|
254
270
|
throw new InitialCheckpointNumberNotSequentialError(firstCheckpointNumber, previousCheckpointNumber);
|
|
255
271
|
}
|
|
256
272
|
|
|
257
|
-
//
|
|
258
|
-
let
|
|
259
|
-
if (previousCheckpointNumber !== INITIAL_CHECKPOINT_NUMBER - 1) {
|
|
260
|
-
// There should be a previous checkpoint
|
|
261
|
-
previousCheckpointData = await this.getCheckpointData(previousCheckpointNumber);
|
|
262
|
-
if (previousCheckpointData === undefined) {
|
|
263
|
-
throw new CheckpointNotFoundError(previousCheckpointNumber);
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
let previousBlockNumber: BlockNumber | undefined = undefined;
|
|
268
|
-
let previousBlock: L2Block | undefined = undefined;
|
|
269
|
-
|
|
270
|
-
// If we have a previous checkpoint then we need to get the previous block number
|
|
271
|
-
if (previousCheckpointData !== undefined) {
|
|
272
|
-
previousBlockNumber = BlockNumber(previousCheckpointData.startBlock + previousCheckpointData.blockCount - 1);
|
|
273
|
-
previousBlock = await this.getBlock(previousBlockNumber);
|
|
274
|
-
if (previousBlock === undefined) {
|
|
275
|
-
// We should be able to get the required previous block
|
|
276
|
-
throw new BlockNotFoundError(previousBlockNumber);
|
|
277
|
-
}
|
|
278
|
-
}
|
|
273
|
+
// Get the last block of the previous checkpoint for archive chaining
|
|
274
|
+
let previousBlock = await this.getPreviousCheckpointBlock(firstCheckpointNumber);
|
|
279
275
|
|
|
280
276
|
// Iterate over checkpoints array and insert them, checking that the block numbers are sequential.
|
|
281
277
|
let previousCheckpoint: PublishedCheckpoint | undefined = undefined;
|
|
@@ -292,42 +288,14 @@ export class BlockStore {
|
|
|
292
288
|
}
|
|
293
289
|
previousCheckpoint = checkpoint;
|
|
294
290
|
|
|
295
|
-
//
|
|
296
|
-
|
|
297
|
-
const block = checkpoint.checkpoint.blocks[i];
|
|
298
|
-
if (previousBlock) {
|
|
299
|
-
// The blocks should have a sequential block number
|
|
300
|
-
if (previousBlock.number !== block.number - 1) {
|
|
301
|
-
throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
|
|
302
|
-
}
|
|
303
|
-
// If the blocks are for the same checkpoint then they should have sequential indexes
|
|
304
|
-
if (
|
|
305
|
-
previousBlock.checkpointNumber === block.checkpointNumber &&
|
|
306
|
-
previousBlock.indexWithinCheckpoint !== block.indexWithinCheckpoint - 1
|
|
307
|
-
) {
|
|
308
|
-
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
|
|
309
|
-
}
|
|
310
|
-
if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
|
|
311
|
-
throw new BlockArchiveNotConsistentError(
|
|
312
|
-
block.number,
|
|
313
|
-
previousBlock.number,
|
|
314
|
-
block.header.lastArchive.root,
|
|
315
|
-
previousBlock.archive.root,
|
|
316
|
-
);
|
|
317
|
-
}
|
|
318
|
-
} else {
|
|
319
|
-
// No previous block, must be block 1 at checkpoint index 0
|
|
320
|
-
if (block.indexWithinCheckpoint !== 0) {
|
|
321
|
-
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, undefined);
|
|
322
|
-
}
|
|
323
|
-
if (block.number !== INITIAL_L2_BLOCK_NUM) {
|
|
324
|
-
throw new BlockNumberNotSequentialError(block.number, undefined);
|
|
325
|
-
}
|
|
326
|
-
}
|
|
291
|
+
// Validate block sequencing, indexes, and archive chaining
|
|
292
|
+
this.validateCheckpointBlocks(checkpoint.checkpoint.blocks, previousBlock);
|
|
327
293
|
|
|
328
|
-
|
|
329
|
-
|
|
294
|
+
// Store every block in the database (may already exist, but L1 data is authoritative)
|
|
295
|
+
for (let i = 0; i < checkpoint.checkpoint.blocks.length; i++) {
|
|
296
|
+
await this.addBlockToDatabase(checkpoint.checkpoint.blocks[i], checkpoint.checkpoint.number, i);
|
|
330
297
|
}
|
|
298
|
+
previousBlock = checkpoint.checkpoint.blocks.at(-1);
|
|
331
299
|
|
|
332
300
|
// Store the checkpoint in the database
|
|
333
301
|
await this.#checkpoints.set(checkpoint.checkpoint.number, {
|
|
@@ -345,11 +313,77 @@ export class BlockStore {
|
|
|
345
313
|
await this.#slotToCheckpoint.set(checkpoint.checkpoint.header.slotNumber, checkpoint.checkpoint.number);
|
|
346
314
|
}
|
|
347
315
|
|
|
316
|
+
// Clear the proposed checkpoint if any of the confirmed checkpoints match or supersede it
|
|
317
|
+
const lastConfirmedCheckpointNumber = checkpoints[checkpoints.length - 1].checkpoint.number;
|
|
318
|
+
await this.clearProposedCheckpointIfSuperseded(lastConfirmedCheckpointNumber);
|
|
319
|
+
|
|
348
320
|
await this.#lastSynchedL1Block.set(checkpoints[checkpoints.length - 1].l1.blockNumber);
|
|
349
321
|
return true;
|
|
350
322
|
});
|
|
351
323
|
}
|
|
352
324
|
|
|
325
|
+
/**
|
|
326
|
+
* Gets the last block of the checkpoint before the given one.
|
|
327
|
+
* Returns undefined if there is no previous checkpoint (i.e. genesis).
|
|
328
|
+
*/
|
|
329
|
+
private async getPreviousCheckpointBlock(checkpointNumber: CheckpointNumber): Promise<L2Block | undefined> {
|
|
330
|
+
const previousCheckpointNumber = CheckpointNumber(checkpointNumber - 1);
|
|
331
|
+
if (previousCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
|
|
332
|
+
return undefined;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const previousCheckpointData = await this.getCheckpointData(previousCheckpointNumber);
|
|
336
|
+
if (previousCheckpointData === undefined) {
|
|
337
|
+
throw new CheckpointNotFoundError(previousCheckpointNumber);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
const previousBlockNumber = BlockNumber(previousCheckpointData.startBlock + previousCheckpointData.blockCount - 1);
|
|
341
|
+
const previousBlock = await this.getBlock(previousBlockNumber);
|
|
342
|
+
if (previousBlock === undefined) {
|
|
343
|
+
throw new BlockNotFoundError(previousBlockNumber);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
return previousBlock;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* Validates that blocks are sequential, have correct indexes, and chain via archive roots.
|
|
351
|
+
* This is the same validation used for both confirmed checkpoints (addCheckpoints) and
|
|
352
|
+
* proposed checkpoints (setProposedCheckpoint).
|
|
353
|
+
*/
|
|
354
|
+
private validateCheckpointBlocks(blocks: L2Block[], previousBlock: L2Block | undefined): void {
|
|
355
|
+
for (const block of blocks) {
|
|
356
|
+
if (previousBlock) {
|
|
357
|
+
if (previousBlock.number !== block.number - 1) {
|
|
358
|
+
throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
|
|
359
|
+
}
|
|
360
|
+
if (previousBlock.checkpointNumber === block.checkpointNumber) {
|
|
361
|
+
if (previousBlock.indexWithinCheckpoint !== block.indexWithinCheckpoint - 1) {
|
|
362
|
+
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
|
|
363
|
+
}
|
|
364
|
+
} else if (block.indexWithinCheckpoint !== 0) {
|
|
365
|
+
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
|
|
366
|
+
}
|
|
367
|
+
if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
|
|
368
|
+
throw new BlockArchiveNotConsistentError(
|
|
369
|
+
block.number,
|
|
370
|
+
previousBlock.number,
|
|
371
|
+
block.header.lastArchive.root,
|
|
372
|
+
previousBlock.archive.root,
|
|
373
|
+
);
|
|
374
|
+
}
|
|
375
|
+
} else {
|
|
376
|
+
if (block.indexWithinCheckpoint !== 0) {
|
|
377
|
+
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, undefined);
|
|
378
|
+
}
|
|
379
|
+
if (block.number !== INITIAL_L2_BLOCK_NUM) {
|
|
380
|
+
throw new BlockNumberNotSequentialError(block.number, undefined);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
previousBlock = block;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
353
387
|
private async addBlockToDatabase(block: L2Block, checkpointNumber: number, indexWithinCheckpoint: number) {
|
|
354
388
|
const blockHash = await block.hash();
|
|
355
389
|
|
|
@@ -442,6 +476,12 @@ export class BlockStore {
|
|
|
442
476
|
this.#log.debug(`Removed checkpoint ${c}`);
|
|
443
477
|
}
|
|
444
478
|
|
|
479
|
+
// Clear any proposed checkpoint that was orphaned by the removal (its base chain no longer exists)
|
|
480
|
+
const proposedCheckpointNumber = await this.getProposedCheckpointNumber();
|
|
481
|
+
if (proposedCheckpointNumber > checkpointNumber) {
|
|
482
|
+
await this.#proposedCheckpoint.delete();
|
|
483
|
+
}
|
|
484
|
+
|
|
445
485
|
return { blocksRemoved };
|
|
446
486
|
});
|
|
447
487
|
}
|
|
@@ -547,7 +587,7 @@ export class BlockStore {
|
|
|
547
587
|
const removedBlocks: L2Block[] = [];
|
|
548
588
|
|
|
549
589
|
// Get the latest block number to determine the range
|
|
550
|
-
const latestBlockNumber = await this.
|
|
590
|
+
const latestBlockNumber = await this.getLatestL2BlockNumber();
|
|
551
591
|
|
|
552
592
|
// Iterate from blockNumber + 1 to latestBlockNumber
|
|
553
593
|
for (let bn = blockNumber + 1; bn <= latestBlockNumber; bn++) {
|
|
@@ -580,13 +620,6 @@ export class BlockStore {
|
|
|
580
620
|
}
|
|
581
621
|
}
|
|
582
622
|
|
|
583
|
-
async getLatestBlockNumber(): Promise<BlockNumber> {
|
|
584
|
-
const [latestBlocknumber] = await toArray(this.#blocks.keysAsync({ reverse: true, limit: 1 }));
|
|
585
|
-
return typeof latestBlocknumber === 'number'
|
|
586
|
-
? BlockNumber(latestBlocknumber)
|
|
587
|
-
: BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
588
|
-
}
|
|
589
|
-
|
|
590
623
|
async getLatestCheckpointNumber(): Promise<CheckpointNumber> {
|
|
591
624
|
const [latestCheckpointNumber] = await toArray(this.#checkpoints.keysAsync({ reverse: true, limit: 1 }));
|
|
592
625
|
if (latestCheckpointNumber === undefined) {
|
|
@@ -595,6 +628,84 @@ export class BlockStore {
|
|
|
595
628
|
return CheckpointNumber(latestCheckpointNumber);
|
|
596
629
|
}
|
|
597
630
|
|
|
631
|
+
async hasProposedCheckpoint(): Promise<boolean> {
|
|
632
|
+
const proposed = await this.#proposedCheckpoint.getAsync();
|
|
633
|
+
return proposed !== undefined;
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
/** Deletes the proposed checkpoint from storage. */
|
|
637
|
+
async deleteProposedCheckpoint(): Promise<void> {
|
|
638
|
+
await this.#proposedCheckpoint.delete();
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
/** Clears the proposed checkpoint if the given confirmed checkpoint number supersedes it. */
|
|
642
|
+
async clearProposedCheckpointIfSuperseded(confirmedCheckpointNumber: CheckpointNumber): Promise<void> {
|
|
643
|
+
const proposedCheckpointNumber = await this.getProposedCheckpointNumber();
|
|
644
|
+
if (proposedCheckpointNumber <= confirmedCheckpointNumber) {
|
|
645
|
+
await this.#proposedCheckpoint.delete();
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
/** Returns the proposed checkpoint data, or undefined if no proposed checkpoint exists. No fallback to confirmed. */
|
|
650
|
+
async getProposedCheckpointOnly(): Promise<ProposedCheckpointData | undefined> {
|
|
651
|
+
const stored = await this.#proposedCheckpoint.getAsync();
|
|
652
|
+
if (!stored) {
|
|
653
|
+
return undefined;
|
|
654
|
+
}
|
|
655
|
+
return this.convertToProposedCheckpointData(stored);
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
/**
|
|
659
|
+
* Gets the checkpoint at the proposed tip
|
|
660
|
+
* - pending checkpoint if it exists
|
|
661
|
+
* - fallsback to latest confirmed checkpoint otherwise
|
|
662
|
+
* @returns CommonCheckpointData
|
|
663
|
+
*/
|
|
664
|
+
async getProposedCheckpoint(): Promise<CommonCheckpointData | undefined> {
|
|
665
|
+
const stored = await this.#proposedCheckpoint.getAsync();
|
|
666
|
+
if (!stored) {
|
|
667
|
+
return this.getCheckpointData(await this.getLatestCheckpointNumber());
|
|
668
|
+
}
|
|
669
|
+
return this.convertToProposedCheckpointData(stored);
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
private convertToProposedCheckpointData(stored: ProposedCheckpointStorage): ProposedCheckpointData {
|
|
673
|
+
return {
|
|
674
|
+
checkpointNumber: CheckpointNumber(stored.checkpointNumber),
|
|
675
|
+
header: CheckpointHeader.fromBuffer(stored.header),
|
|
676
|
+
archive: AppendOnlyTreeSnapshot.fromBuffer(stored.archive),
|
|
677
|
+
checkpointOutHash: Fr.fromBuffer(stored.checkpointOutHash),
|
|
678
|
+
startBlock: BlockNumber(stored.startBlock),
|
|
679
|
+
blockCount: stored.blockCount,
|
|
680
|
+
totalManaUsed: BigInt(stored.totalManaUsed),
|
|
681
|
+
feeAssetPriceModifier: BigInt(stored.feeAssetPriceModifier),
|
|
682
|
+
};
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
/**
|
|
686
|
+
* Attempts to get the proposedCheckpoint's number, if there is not one, then fallback to the latest confirmed checkpoint number.
|
|
687
|
+
* @returns CheckpointNumber
|
|
688
|
+
*/
|
|
689
|
+
async getProposedCheckpointNumber(): Promise<CheckpointNumber> {
|
|
690
|
+
const proposed = await this.getProposedCheckpoint();
|
|
691
|
+
if (!proposed) {
|
|
692
|
+
return await this.getLatestCheckpointNumber();
|
|
693
|
+
}
|
|
694
|
+
return CheckpointNumber(proposed.checkpointNumber);
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
/**
|
|
698
|
+
* Attempts to get the proposedCheckpoint's block number, if there is not one, then fallback to the checkpointed block number
|
|
699
|
+
* @returns BlockNumber
|
|
700
|
+
*/
|
|
701
|
+
async getProposedCheckpointL2BlockNumber(): Promise<BlockNumber> {
|
|
702
|
+
const proposed = await this.getProposedCheckpoint();
|
|
703
|
+
if (!proposed) {
|
|
704
|
+
return await this.getCheckpointedL2BlockNumber();
|
|
705
|
+
}
|
|
706
|
+
return BlockNumber(proposed.startBlock + proposed.blockCount - 1);
|
|
707
|
+
}
|
|
708
|
+
|
|
598
709
|
async getCheckpointedBlock(number: BlockNumber): Promise<CheckpointedL2Block | undefined> {
|
|
599
710
|
const blockStorage = await this.#blocks.getAsync(number);
|
|
600
711
|
if (!blockStorage) {
|
|
@@ -871,7 +982,10 @@ export class BlockStore {
|
|
|
871
982
|
* @param txHash - The hash of a tx we try to get the receipt for.
|
|
872
983
|
* @returns The requested tx receipt (or undefined if not found).
|
|
873
984
|
*/
|
|
874
|
-
async getSettledTxReceipt(
|
|
985
|
+
async getSettledTxReceipt(
|
|
986
|
+
txHash: TxHash,
|
|
987
|
+
l1Constants?: Pick<L1RollupConstants, 'epochDuration'>,
|
|
988
|
+
): Promise<TxReceipt | undefined> {
|
|
875
989
|
const txEffect = await this.getTxEffect(txHash);
|
|
876
990
|
if (!txEffect) {
|
|
877
991
|
return undefined;
|
|
@@ -880,10 +994,11 @@ export class BlockStore {
|
|
|
880
994
|
const blockNumber = BlockNumber(txEffect.l2BlockNumber);
|
|
881
995
|
|
|
882
996
|
// Use existing archiver methods to determine finalization level
|
|
883
|
-
const [provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber] = await Promise.all([
|
|
997
|
+
const [provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber, blockData] = await Promise.all([
|
|
884
998
|
this.getProvenBlockNumber(),
|
|
885
999
|
this.getCheckpointedL2BlockNumber(),
|
|
886
1000
|
this.getFinalizedL2BlockNumber(),
|
|
1001
|
+
this.getBlockData(blockNumber),
|
|
887
1002
|
]);
|
|
888
1003
|
|
|
889
1004
|
let status: TxStatus;
|
|
@@ -897,6 +1012,9 @@ export class BlockStore {
|
|
|
897
1012
|
status = TxStatus.PROPOSED;
|
|
898
1013
|
}
|
|
899
1014
|
|
|
1015
|
+
const epochNumber =
|
|
1016
|
+
blockData && l1Constants ? getEpochAtSlot(blockData.header.globalVariables.slotNumber, l1Constants) : undefined;
|
|
1017
|
+
|
|
900
1018
|
return new TxReceipt(
|
|
901
1019
|
txHash,
|
|
902
1020
|
status,
|
|
@@ -905,6 +1023,7 @@ export class BlockStore {
|
|
|
905
1023
|
txEffect.data.transactionFee.toBigInt(),
|
|
906
1024
|
txEffect.l2BlockHash,
|
|
907
1025
|
blockNumber,
|
|
1026
|
+
epochNumber,
|
|
908
1027
|
);
|
|
909
1028
|
}
|
|
910
1029
|
|
|
@@ -961,6 +1080,47 @@ export class BlockStore {
|
|
|
961
1080
|
return this.#lastSynchedL1Block.set(l1BlockNumber);
|
|
962
1081
|
}
|
|
963
1082
|
|
|
1083
|
+
/** Sets the proposed checkpoint (not yet L1-confirmed). Only accepts confirmed + 1.
|
|
1084
|
+
* Computes archive and checkpointOutHash from the stored blocks. */
|
|
1085
|
+
async setProposedCheckpoint(proposed: ProposedCheckpointInput) {
|
|
1086
|
+
return await this.db.transactionAsync(async () => {
|
|
1087
|
+
const current = await this.getProposedCheckpointNumber();
|
|
1088
|
+
if (proposed.checkpointNumber <= current) {
|
|
1089
|
+
throw new ProposedCheckpointStaleError(proposed.checkpointNumber, current);
|
|
1090
|
+
}
|
|
1091
|
+
const confirmed = await this.getLatestCheckpointNumber();
|
|
1092
|
+
if (proposed.checkpointNumber !== confirmed + 1) {
|
|
1093
|
+
throw new ProposedCheckpointNotSequentialError(proposed.checkpointNumber, confirmed);
|
|
1094
|
+
}
|
|
1095
|
+
|
|
1096
|
+
// Ensure the previous checkpoint + blocks exist
|
|
1097
|
+
const previousBlock = await this.getPreviousCheckpointBlock(proposed.checkpointNumber);
|
|
1098
|
+
const blocks: L2Block[] = [];
|
|
1099
|
+
for (let i = 0; i < proposed.blockCount; i++) {
|
|
1100
|
+
const block = await this.getBlock(BlockNumber(proposed.startBlock + i));
|
|
1101
|
+
if (!block) {
|
|
1102
|
+
throw new BlockNotFoundError(proposed.startBlock + i);
|
|
1103
|
+
}
|
|
1104
|
+
blocks.push(block);
|
|
1105
|
+
}
|
|
1106
|
+
this.validateCheckpointBlocks(blocks, previousBlock);
|
|
1107
|
+
|
|
1108
|
+
const archive = blocks[blocks.length - 1].archive;
|
|
1109
|
+
const checkpointOutHash = Checkpoint.getCheckpointOutHash(blocks);
|
|
1110
|
+
|
|
1111
|
+
await this.#proposedCheckpoint.set({
|
|
1112
|
+
header: proposed.header.toBuffer(),
|
|
1113
|
+
archive: archive.toBuffer(),
|
|
1114
|
+
checkpointOutHash: checkpointOutHash.toBuffer(),
|
|
1115
|
+
checkpointNumber: proposed.checkpointNumber,
|
|
1116
|
+
startBlock: proposed.startBlock,
|
|
1117
|
+
blockCount: proposed.blockCount,
|
|
1118
|
+
totalManaUsed: proposed.totalManaUsed.toString(),
|
|
1119
|
+
feeAssetPriceModifier: proposed.feeAssetPriceModifier.toString(),
|
|
1120
|
+
});
|
|
1121
|
+
});
|
|
1122
|
+
}
|
|
1123
|
+
|
|
964
1124
|
async getProvenCheckpointNumber(): Promise<CheckpointNumber> {
|
|
965
1125
|
const [latestCheckpointNumber, provenCheckpointNumber] = await Promise.all([
|
|
966
1126
|
this.getLatestCheckpointNumber(),
|
|
@@ -976,6 +1136,20 @@ export class BlockStore {
|
|
|
976
1136
|
return result;
|
|
977
1137
|
}
|
|
978
1138
|
|
|
1139
|
+
async getFinalizedCheckpointNumber(): Promise<CheckpointNumber> {
|
|
1140
|
+
const [latestCheckpointNumber, finalizedCheckpointNumber] = await Promise.all([
|
|
1141
|
+
this.getLatestCheckpointNumber(),
|
|
1142
|
+
this.#lastFinalizedCheckpoint.getAsync(),
|
|
1143
|
+
]);
|
|
1144
|
+
return (finalizedCheckpointNumber ?? 0) > latestCheckpointNumber
|
|
1145
|
+
? latestCheckpointNumber
|
|
1146
|
+
: CheckpointNumber(finalizedCheckpointNumber ?? 0);
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
setFinalizedCheckpointNumber(checkpointNumber: CheckpointNumber) {
|
|
1150
|
+
return this.#lastFinalizedCheckpoint.set(checkpointNumber);
|
|
1151
|
+
}
|
|
1152
|
+
|
|
979
1153
|
#computeBlockRange(start: BlockNumber, limit: number): Required<Pick<Range<number>, 'start' | 'limit'>> {
|
|
980
1154
|
if (limit < 1) {
|
|
981
1155
|
throw new Error(`Invalid limit: ${limit}`);
|