@aztec/archiver 0.0.1-commit.d431d1c → 0.0.1-commit.d939eb5aa
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -6
- package/dest/archiver.d.ts +17 -12
- package/dest/archiver.d.ts.map +1 -1
- package/dest/archiver.js +108 -127
- package/dest/config.d.ts +3 -3
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +3 -2
- package/dest/errors.d.ts +39 -10
- package/dest/errors.d.ts.map +1 -1
- package/dest/errors.js +52 -15
- package/dest/factory.d.ts +4 -2
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +29 -23
- package/dest/index.d.ts +2 -1
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +1 -0
- package/dest/l1/bin/retrieve-calldata.js +35 -32
- package/dest/l1/calldata_retriever.d.ts +73 -50
- package/dest/l1/calldata_retriever.d.ts.map +1 -1
- package/dest/l1/calldata_retriever.js +191 -259
- package/dest/l1/data_retrieval.d.ts +11 -11
- package/dest/l1/data_retrieval.d.ts.map +1 -1
- package/dest/l1/data_retrieval.js +38 -37
- package/dest/l1/spire_proposer.d.ts +5 -5
- package/dest/l1/spire_proposer.d.ts.map +1 -1
- package/dest/l1/spire_proposer.js +9 -17
- package/dest/l1/validate_trace.d.ts +6 -3
- package/dest/l1/validate_trace.d.ts.map +1 -1
- package/dest/l1/validate_trace.js +13 -9
- package/dest/modules/data_source_base.d.ts +29 -23
- package/dest/modules/data_source_base.d.ts.map +1 -1
- package/dest/modules/data_source_base.js +55 -124
- package/dest/modules/data_store_updater.d.ts +43 -26
- package/dest/modules/data_store_updater.d.ts.map +1 -1
- package/dest/modules/data_store_updater.js +158 -129
- package/dest/modules/instrumentation.d.ts +19 -4
- package/dest/modules/instrumentation.d.ts.map +1 -1
- package/dest/modules/instrumentation.js +53 -18
- package/dest/modules/l1_synchronizer.d.ts +7 -9
- package/dest/modules/l1_synchronizer.d.ts.map +1 -1
- package/dest/modules/l1_synchronizer.js +186 -145
- 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 +86 -34
- package/dest/store/block_store.d.ts.map +1 -1
- package/dest/store/block_store.js +414 -152
- 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 +16 -72
- 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 +80 -39
- package/dest/store/kv_archiver_store.d.ts.map +1 -1
- package/dest/store/kv_archiver_store.js +86 -35
- package/dest/store/l2_tips_cache.d.ts +20 -0
- package/dest/store/l2_tips_cache.d.ts.map +1 -0
- package/dest/store/l2_tips_cache.js +109 -0
- package/dest/store/log_store.d.ts +9 -6
- package/dest/store/log_store.d.ts.map +1 -1
- package/dest/store/log_store.js +151 -56
- 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 +21 -9
- package/dest/test/fake_l1_state.d.ts +24 -4
- package/dest/test/fake_l1_state.d.ts.map +1 -1
- package/dest/test/fake_l1_state.js +133 -26
- package/dest/test/index.js +3 -1
- package/dest/test/mock_archiver.d.ts +1 -1
- package/dest/test/mock_archiver.d.ts.map +1 -1
- package/dest/test/mock_archiver.js +3 -2
- 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 +44 -23
- package/dest/test/mock_l2_block_source.d.ts.map +1 -1
- package/dest/test/mock_l2_block_source.js +185 -115
- package/dest/test/mock_structs.d.ts +6 -2
- package/dest/test/mock_structs.d.ts.map +1 -1
- package/dest/test/mock_structs.js +24 -10
- package/dest/test/noop_l1_archiver.d.ts +26 -0
- package/dest/test/noop_l1_archiver.d.ts.map +1 -0
- package/dest/test/noop_l1_archiver.js +71 -0
- package/package.json +14 -13
- package/src/archiver.ts +144 -159
- package/src/config.ts +9 -2
- package/src/errors.ts +82 -26
- package/src/factory.ts +46 -22
- package/src/index.ts +1 -0
- package/src/l1/README.md +25 -68
- package/src/l1/bin/retrieve-calldata.ts +45 -33
- package/src/l1/calldata_retriever.ts +250 -379
- package/src/l1/data_retrieval.ts +35 -41
- package/src/l1/spire_proposer.ts +7 -15
- package/src/l1/validate_trace.ts +24 -6
- package/src/modules/data_source_base.ts +98 -169
- package/src/modules/data_store_updater.ts +178 -160
- package/src/modules/instrumentation.ts +64 -20
- package/src/modules/l1_synchronizer.ts +212 -182
- package/src/modules/validation.ts +2 -2
- package/src/store/block_store.ts +533 -207
- package/src/store/contract_class_store.ts +16 -110
- package/src/store/contract_instance_store.ts +8 -5
- package/src/store/kv_archiver_store.ts +141 -59
- package/src/store/l2_tips_cache.ts +134 -0
- package/src/store/log_store.ts +232 -74
- package/src/store/message_store.ts +27 -10
- package/src/structs/inbox_message.ts +1 -1
- package/src/test/fake_l1_state.ts +180 -32
- package/src/test/index.ts +3 -0
- package/src/test/mock_archiver.ts +3 -2
- package/src/test/mock_l1_to_l2_message_source.ts +1 -0
- package/src/test/mock_l2_block_source.ts +247 -130
- package/src/test/mock_structs.ts +45 -15
- package/src/test/noop_l1_archiver.ts +114 -0
|
@@ -6,12 +6,13 @@ import { createLogger } from '@aztec/foundation/log';
|
|
|
6
6
|
import { BufferReader } from '@aztec/foundation/serialize';
|
|
7
7
|
import { bufferToHex } from '@aztec/foundation/string';
|
|
8
8
|
import { isDefined } from '@aztec/foundation/types';
|
|
9
|
-
import { Body, CheckpointedL2Block, CommitteeAttestation,
|
|
10
|
-
import { L1PublishedData } from '@aztec/stdlib/checkpoint';
|
|
9
|
+
import { BlockHash, Body, CheckpointedL2Block, CommitteeAttestation, L2Block, deserializeValidateCheckpointResult, serializeValidateCheckpointResult } from '@aztec/stdlib/block';
|
|
10
|
+
import { Checkpoint, L1PublishedData } from '@aztec/stdlib/checkpoint';
|
|
11
|
+
import { getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
|
|
11
12
|
import { CheckpointHeader } from '@aztec/stdlib/rollup';
|
|
12
13
|
import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
|
|
13
|
-
import { BlockHeader, TxHash, TxReceipt, deserializeIndexedTxEffect, serializeIndexedTxEffect } from '@aztec/stdlib/tx';
|
|
14
|
-
import { BlockArchiveNotConsistentError, BlockIndexNotSequentialError, BlockNotFoundError, BlockNumberNotSequentialError,
|
|
14
|
+
import { BlockHeader, TxHash, TxReceipt, TxStatus, deserializeIndexedTxEffect, serializeIndexedTxEffect } from '@aztec/stdlib/tx';
|
|
15
|
+
import { BlockAlreadyCheckpointedError, BlockArchiveNotConsistentError, BlockIndexNotSequentialError, BlockNotFoundError, BlockNumberNotSequentialError, CannotOverwriteCheckpointedBlockError, CheckpointNotFoundError, CheckpointNumberNotSequentialError, InitialCheckpointNumberNotSequentialError, ProposedCheckpointNotSequentialError, ProposedCheckpointStaleError } from '../errors.js';
|
|
15
16
|
export { TxReceipt } from '@aztec/stdlib/tx';
|
|
16
17
|
/**
|
|
17
18
|
* LMDB-based block storage for the archiver.
|
|
@@ -19,14 +20,17 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
19
20
|
db;
|
|
20
21
|
/** Map block number to block data */ #blocks;
|
|
21
22
|
/** Map checkpoint number to checkpoint data */ #checkpoints;
|
|
23
|
+
/** Map slot number to checkpoint number, for looking up checkpoints by slot range. */ #slotToCheckpoint;
|
|
22
24
|
/** Map block hash to list of tx hashes */ #blockTxs;
|
|
23
25
|
/** Tx hash to serialized IndexedTxEffect */ #txEffects;
|
|
24
26
|
/** Stores L1 block number in which the last processed L2 block was included */ #lastSynchedL1Block;
|
|
25
27
|
/** Stores last proven checkpoint */ #lastProvenCheckpoint;
|
|
28
|
+
/** Stores last finalized checkpoint (proven at or before the finalized L1 block) */ #lastFinalizedCheckpoint;
|
|
26
29
|
/** Stores the pending chain validation status */ #pendingChainValidationStatus;
|
|
27
30
|
/** Index mapping a contract's address (as a string) to its location in a block */ #contractIndex;
|
|
28
31
|
/** Index mapping block hash to block number */ #blockHashIndex;
|
|
29
32
|
/** Index mapping block archive to block number */ #blockArchiveIndex;
|
|
33
|
+
/** Singleton: assumes max 1-deep pipeline. For deeper pipelining, replace with a map keyed by checkpoint number. */ #proposedCheckpoint;
|
|
30
34
|
#log;
|
|
31
35
|
constructor(db){
|
|
32
36
|
this.db = db;
|
|
@@ -39,77 +43,94 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
39
43
|
this.#blockArchiveIndex = db.openMap('archiver_block_archive_index');
|
|
40
44
|
this.#lastSynchedL1Block = db.openSingleton('archiver_last_synched_l1_block');
|
|
41
45
|
this.#lastProvenCheckpoint = db.openSingleton('archiver_last_proven_l2_checkpoint');
|
|
46
|
+
this.#lastFinalizedCheckpoint = db.openSingleton('archiver_last_finalized_l2_checkpoint');
|
|
42
47
|
this.#pendingChainValidationStatus = db.openSingleton('archiver_pending_chain_validation_status');
|
|
43
48
|
this.#checkpoints = db.openMap('archiver_checkpoints');
|
|
49
|
+
this.#slotToCheckpoint = db.openMap('archiver_slot_to_checkpoint');
|
|
50
|
+
this.#proposedCheckpoint = db.openSingleton('proposed_checkpoint_data');
|
|
44
51
|
}
|
|
45
52
|
/**
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
* @returns
|
|
49
|
-
*/ async
|
|
50
|
-
|
|
51
|
-
|
|
53
|
+
* Returns the finalized L2 block number. An L2 block is finalized when it was proven
|
|
54
|
+
* in an L1 block that has itself been finalized on Ethereum.
|
|
55
|
+
* @returns The finalized block number.
|
|
56
|
+
*/ async getFinalizedL2BlockNumber() {
|
|
57
|
+
const finalizedCheckpointNumber = await this.getFinalizedCheckpointNumber();
|
|
58
|
+
if (finalizedCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
|
|
59
|
+
return BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
52
60
|
}
|
|
61
|
+
const checkpointStorage = await this.#checkpoints.getAsync(finalizedCheckpointNumber);
|
|
62
|
+
if (!checkpointStorage) {
|
|
63
|
+
throw new CheckpointNotFoundError(finalizedCheckpointNumber);
|
|
64
|
+
}
|
|
65
|
+
return BlockNumber(checkpointStorage.startBlock + checkpointStorage.blockCount - 1);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Append a new proposed block to the store.
|
|
69
|
+
* This is an uncheckpointed block that has been proposed by the sequencer but not yet included in a checkpoint on L1.
|
|
70
|
+
* For checkpointed blocks (already published to L1), use addCheckpoints() instead.
|
|
71
|
+
* @param block - The proposed L2 block to be added to the store.
|
|
72
|
+
* @returns True if the operation is successful.
|
|
73
|
+
*/ async addProposedBlock(block, opts = {}) {
|
|
53
74
|
return await this.db.transactionAsync(async ()=>{
|
|
54
|
-
|
|
55
|
-
const
|
|
56
|
-
const
|
|
57
|
-
const
|
|
58
|
-
const firstBlockLastArchive = blocks[0].header.lastArchive.root;
|
|
75
|
+
const blockNumber = block.number;
|
|
76
|
+
const blockCheckpointNumber = block.checkpointNumber;
|
|
77
|
+
const blockIndex = block.indexWithinCheckpoint;
|
|
78
|
+
const blockLastArchive = block.header.lastArchive.root;
|
|
59
79
|
// Extract the latest block and checkpoint numbers
|
|
60
|
-
const previousBlockNumber = await this.
|
|
80
|
+
const previousBlockNumber = await this.getLatestL2BlockNumber();
|
|
81
|
+
const proposedCheckpointNumber = await this.getProposedCheckpointNumber();
|
|
61
82
|
const previousCheckpointNumber = await this.getLatestCheckpointNumber();
|
|
62
|
-
//
|
|
63
|
-
|
|
64
|
-
|
|
83
|
+
// Verify we're not overwriting checkpointed blocks
|
|
84
|
+
const lastCheckpointedBlockNumber = await this.getCheckpointedL2BlockNumber();
|
|
85
|
+
if (!opts.force && blockNumber <= lastCheckpointedBlockNumber) {
|
|
86
|
+
// Check if the proposed block matches the already-checkpointed one
|
|
87
|
+
const existingBlock = await this.getBlock(BlockNumber(blockNumber));
|
|
88
|
+
if (existingBlock && existingBlock.archive.root.equals(block.archive.root)) {
|
|
89
|
+
throw new BlockAlreadyCheckpointedError(blockNumber);
|
|
90
|
+
}
|
|
91
|
+
throw new CannotOverwriteCheckpointedBlockError(blockNumber, lastCheckpointedBlockNumber);
|
|
65
92
|
}
|
|
66
|
-
//
|
|
67
|
-
if (!opts.force &&
|
|
68
|
-
throw new
|
|
93
|
+
// Check that the block number is the expected one
|
|
94
|
+
if (!opts.force && previousBlockNumber !== blockNumber - 1) {
|
|
95
|
+
throw new BlockNumberNotSequentialError(blockNumber, previousBlockNumber);
|
|
96
|
+
}
|
|
97
|
+
// The same check as above but for checkpoints. Accept the block if either the confirmed
|
|
98
|
+
// checkpoint or the pending (locally validated but not yet confirmed) checkpoint matches.
|
|
99
|
+
const expectedCheckpointNumber = blockCheckpointNumber - 1;
|
|
100
|
+
if (!opts.force && previousCheckpointNumber !== expectedCheckpointNumber && proposedCheckpointNumber !== expectedCheckpointNumber) {
|
|
101
|
+
const [reported, source] = proposedCheckpointNumber > previousCheckpointNumber ? [
|
|
102
|
+
proposedCheckpointNumber,
|
|
103
|
+
'proposed'
|
|
104
|
+
] : [
|
|
105
|
+
previousCheckpointNumber,
|
|
106
|
+
'confirmed'
|
|
107
|
+
];
|
|
108
|
+
throw new CheckpointNumberNotSequentialError(blockCheckpointNumber, reported, source);
|
|
69
109
|
}
|
|
70
110
|
// Extract the previous block if there is one and see if it is for the same checkpoint or not
|
|
71
111
|
const previousBlockResult = await this.getBlock(previousBlockNumber);
|
|
72
|
-
let
|
|
112
|
+
let expectedBlockIndex = 0;
|
|
73
113
|
let previousBlockIndex = undefined;
|
|
74
114
|
if (previousBlockResult !== undefined) {
|
|
75
|
-
if (previousBlockResult.checkpointNumber ===
|
|
115
|
+
if (previousBlockResult.checkpointNumber === blockCheckpointNumber) {
|
|
76
116
|
// The previous block is for the same checkpoint, therefore our index should follow it
|
|
77
117
|
previousBlockIndex = previousBlockResult.indexWithinCheckpoint;
|
|
78
|
-
|
|
118
|
+
expectedBlockIndex = previousBlockIndex + 1;
|
|
79
119
|
}
|
|
80
|
-
if (!previousBlockResult.archive.root.equals(
|
|
81
|
-
throw new BlockArchiveNotConsistentError(
|
|
120
|
+
if (!previousBlockResult.archive.root.equals(blockLastArchive)) {
|
|
121
|
+
throw new BlockArchiveNotConsistentError(blockNumber, previousBlockResult.number, blockLastArchive, previousBlockResult.archive.root);
|
|
82
122
|
}
|
|
83
123
|
}
|
|
84
|
-
// Now check that the
|
|
85
|
-
if (!opts.force &&
|
|
86
|
-
throw new BlockIndexNotSequentialError(
|
|
87
|
-
}
|
|
88
|
-
// Iterate over blocks array and insert them, checking that the block numbers and indexes are sequential. Also check they are for the correct checkpoint.
|
|
89
|
-
let previousBlock = undefined;
|
|
90
|
-
for (const block of blocks){
|
|
91
|
-
if (!opts.force && previousBlock) {
|
|
92
|
-
if (previousBlock.number + 1 !== block.number) {
|
|
93
|
-
throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
|
|
94
|
-
}
|
|
95
|
-
if (previousBlock.indexWithinCheckpoint + 1 !== block.indexWithinCheckpoint) {
|
|
96
|
-
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
|
|
97
|
-
}
|
|
98
|
-
if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
|
|
99
|
-
throw new BlockArchiveNotConsistentError(block.number, previousBlock.number, block.header.lastArchive.root, previousBlock.archive.root);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
if (!opts.force && firstBlockCheckpointNumber !== block.checkpointNumber) {
|
|
103
|
-
throw new CheckpointNumberNotConsistentError(block.checkpointNumber, firstBlockCheckpointNumber);
|
|
104
|
-
}
|
|
105
|
-
previousBlock = block;
|
|
106
|
-
await this.addBlockToDatabase(block, block.checkpointNumber, block.indexWithinCheckpoint);
|
|
124
|
+
// Now check that the block has the expected index value
|
|
125
|
+
if (!opts.force && expectedBlockIndex !== blockIndex) {
|
|
126
|
+
throw new BlockIndexNotSequentialError(blockIndex, previousBlockIndex);
|
|
107
127
|
}
|
|
128
|
+
await this.addBlockToDatabase(block, block.checkpointNumber, block.indexWithinCheckpoint);
|
|
108
129
|
return true;
|
|
109
130
|
});
|
|
110
131
|
}
|
|
111
132
|
/**
|
|
112
|
-
* Append new
|
|
133
|
+
* Append new checkpoints to the store's list.
|
|
113
134
|
* @param checkpoints - The L2 checkpoints to be added to the store.
|
|
114
135
|
* @returns True if the operation is successful.
|
|
115
136
|
*/ async addCheckpoints(checkpoints, opts = {}) {
|
|
@@ -117,32 +138,26 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
117
138
|
return true;
|
|
118
139
|
}
|
|
119
140
|
return await this.db.transactionAsync(async ()=>{
|
|
120
|
-
// Check that the checkpoint immediately before the first block to be added is present in the store.
|
|
121
141
|
const firstCheckpointNumber = checkpoints[0].checkpoint.number;
|
|
122
142
|
const previousCheckpointNumber = await this.getLatestCheckpointNumber();
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
previousCheckpointData = await this.getCheckpointData(previousCheckpointNumber);
|
|
131
|
-
if (previousCheckpointData === undefined) {
|
|
132
|
-
throw new CheckpointNotFoundError(previousCheckpointNumber);
|
|
143
|
+
// Handle already-stored checkpoints at the start of the batch.
|
|
144
|
+
// This can happen after an L1 reorg re-includes a checkpoint in a different L1 block.
|
|
145
|
+
// We accept them if archives match (same content) and update their L1 metadata.
|
|
146
|
+
if (!opts.force && firstCheckpointNumber <= previousCheckpointNumber) {
|
|
147
|
+
checkpoints = await this.skipOrUpdateAlreadyStoredCheckpoints(checkpoints, previousCheckpointNumber);
|
|
148
|
+
if (checkpoints.length === 0) {
|
|
149
|
+
return true;
|
|
133
150
|
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
if (previousCheckpointData !== undefined) {
|
|
139
|
-
previousBlockNumber = BlockNumber(previousCheckpointData.startBlock + previousCheckpointData.numBlocks - 1);
|
|
140
|
-
previousBlock = await this.getBlock(previousBlockNumber);
|
|
141
|
-
if (previousBlock === undefined) {
|
|
142
|
-
// We should be able to get the required previous block
|
|
143
|
-
throw new BlockNotFoundError(previousBlockNumber);
|
|
151
|
+
// Re-check sequentiality after skipping
|
|
152
|
+
const newFirstNumber = checkpoints[0].checkpoint.number;
|
|
153
|
+
if (previousCheckpointNumber !== newFirstNumber - 1) {
|
|
154
|
+
throw new InitialCheckpointNumberNotSequentialError(newFirstNumber, previousCheckpointNumber);
|
|
144
155
|
}
|
|
156
|
+
} else if (previousCheckpointNumber !== firstCheckpointNumber - 1 && !opts.force) {
|
|
157
|
+
throw new InitialCheckpointNumberNotSequentialError(firstCheckpointNumber, previousCheckpointNumber);
|
|
145
158
|
}
|
|
159
|
+
// Get the last block of the previous checkpoint for archive chaining
|
|
160
|
+
let previousBlock = await this.getPreviousCheckpointBlock(checkpoints[0].checkpoint.number);
|
|
146
161
|
// Iterate over checkpoints array and insert them, checking that the block numbers are sequential.
|
|
147
162
|
let previousCheckpoint = undefined;
|
|
148
163
|
for (const checkpoint of checkpoints){
|
|
@@ -150,50 +165,118 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
150
165
|
throw new CheckpointNumberNotSequentialError(checkpoint.checkpoint.number, previousCheckpoint.checkpoint.number);
|
|
151
166
|
}
|
|
152
167
|
previousCheckpoint = checkpoint;
|
|
153
|
-
//
|
|
168
|
+
// Validate block sequencing, indexes, and archive chaining
|
|
169
|
+
this.validateCheckpointBlocks(checkpoint.checkpoint.blocks, previousBlock);
|
|
170
|
+
// Store every block in the database (may already exist, but L1 data is authoritative)
|
|
154
171
|
for(let i = 0; i < checkpoint.checkpoint.blocks.length; i++){
|
|
155
|
-
|
|
156
|
-
if (previousBlock) {
|
|
157
|
-
// The blocks should have a sequential block number
|
|
158
|
-
if (previousBlock.number !== block.number - 1) {
|
|
159
|
-
throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
|
|
160
|
-
}
|
|
161
|
-
// If the blocks are for the same checkpoint then they should have sequential indexes
|
|
162
|
-
if (previousBlock.checkpointNumber === block.checkpointNumber && previousBlock.indexWithinCheckpoint !== block.indexWithinCheckpoint - 1) {
|
|
163
|
-
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
|
|
164
|
-
}
|
|
165
|
-
if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
|
|
166
|
-
throw new BlockArchiveNotConsistentError(block.number, previousBlock.number, block.header.lastArchive.root, previousBlock.archive.root);
|
|
167
|
-
}
|
|
168
|
-
} else {
|
|
169
|
-
// No previous block, must be block 1 at checkpoint index 0
|
|
170
|
-
if (block.indexWithinCheckpoint !== 0) {
|
|
171
|
-
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, undefined);
|
|
172
|
-
}
|
|
173
|
-
if (block.number !== INITIAL_L2_BLOCK_NUM) {
|
|
174
|
-
throw new BlockNumberNotSequentialError(block.number, undefined);
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
previousBlock = block;
|
|
178
|
-
await this.addBlockToDatabase(block, checkpoint.checkpoint.number, i);
|
|
172
|
+
await this.addBlockToDatabase(checkpoint.checkpoint.blocks[i], checkpoint.checkpoint.number, i);
|
|
179
173
|
}
|
|
174
|
+
previousBlock = checkpoint.checkpoint.blocks.at(-1);
|
|
180
175
|
// Store the checkpoint in the database
|
|
181
176
|
await this.#checkpoints.set(checkpoint.checkpoint.number, {
|
|
182
177
|
header: checkpoint.checkpoint.header.toBuffer(),
|
|
183
178
|
archive: checkpoint.checkpoint.archive.toBuffer(),
|
|
179
|
+
checkpointOutHash: checkpoint.checkpoint.getCheckpointOutHash().toBuffer(),
|
|
184
180
|
l1: checkpoint.l1.toBuffer(),
|
|
185
181
|
attestations: checkpoint.attestations.map((attestation)=>attestation.toBuffer()),
|
|
186
182
|
checkpointNumber: checkpoint.checkpoint.number,
|
|
187
183
|
startBlock: checkpoint.checkpoint.blocks[0].number,
|
|
188
|
-
|
|
184
|
+
blockCount: checkpoint.checkpoint.blocks.length
|
|
189
185
|
});
|
|
186
|
+
// Update slot-to-checkpoint index
|
|
187
|
+
await this.#slotToCheckpoint.set(checkpoint.checkpoint.header.slotNumber, checkpoint.checkpoint.number);
|
|
190
188
|
}
|
|
189
|
+
// Clear the proposed checkpoint if any of the confirmed checkpoints match or supersede it
|
|
190
|
+
const lastConfirmedCheckpointNumber = checkpoints[checkpoints.length - 1].checkpoint.number;
|
|
191
|
+
await this.clearProposedCheckpointIfSuperseded(lastConfirmedCheckpointNumber);
|
|
191
192
|
await this.#lastSynchedL1Block.set(checkpoints[checkpoints.length - 1].l1.blockNumber);
|
|
192
193
|
return true;
|
|
193
194
|
});
|
|
194
195
|
}
|
|
196
|
+
/**
|
|
197
|
+
* Handles checkpoints at the start of a batch that are already stored (e.g. due to L1 reorg).
|
|
198
|
+
* Verifies the archive root matches, updates L1 metadata, and returns only the new checkpoints.
|
|
199
|
+
*/ async skipOrUpdateAlreadyStoredCheckpoints(checkpoints, latestStored) {
|
|
200
|
+
let i = 0;
|
|
201
|
+
for(; i < checkpoints.length && checkpoints[i].checkpoint.number <= latestStored; i++){
|
|
202
|
+
const incoming = checkpoints[i];
|
|
203
|
+
const stored = await this.getCheckpointData(incoming.checkpoint.number);
|
|
204
|
+
if (!stored) {
|
|
205
|
+
break;
|
|
206
|
+
}
|
|
207
|
+
// Verify the checkpoint content matches (archive root)
|
|
208
|
+
if (!stored.archive.root.equals(incoming.checkpoint.archive.root)) {
|
|
209
|
+
throw new Error(`Checkpoint ${incoming.checkpoint.number} already exists in store but with a different archive root. ` + `Stored: ${stored.archive.root}, incoming: ${incoming.checkpoint.archive.root}`);
|
|
210
|
+
}
|
|
211
|
+
// Update L1 metadata and attestations for the already-stored checkpoint
|
|
212
|
+
this.#log.warn(`Checkpoint ${incoming.checkpoint.number} already stored, updating L1 info ` + `(L1 block ${stored.l1.blockNumber} -> ${incoming.l1.blockNumber})`);
|
|
213
|
+
await this.#checkpoints.set(incoming.checkpoint.number, {
|
|
214
|
+
header: incoming.checkpoint.header.toBuffer(),
|
|
215
|
+
archive: incoming.checkpoint.archive.toBuffer(),
|
|
216
|
+
checkpointOutHash: incoming.checkpoint.getCheckpointOutHash().toBuffer(),
|
|
217
|
+
l1: incoming.l1.toBuffer(),
|
|
218
|
+
attestations: incoming.attestations.map((a)=>a.toBuffer()),
|
|
219
|
+
checkpointNumber: incoming.checkpoint.number,
|
|
220
|
+
startBlock: incoming.checkpoint.blocks[0].number,
|
|
221
|
+
blockCount: incoming.checkpoint.blocks.length
|
|
222
|
+
});
|
|
223
|
+
// Update the sync point to reflect the new L1 block
|
|
224
|
+
await this.#lastSynchedL1Block.set(incoming.l1.blockNumber);
|
|
225
|
+
}
|
|
226
|
+
return checkpoints.slice(i);
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Gets the last block of the checkpoint before the given one.
|
|
230
|
+
* Returns undefined if there is no previous checkpoint (i.e. genesis).
|
|
231
|
+
*/ async getPreviousCheckpointBlock(checkpointNumber) {
|
|
232
|
+
const previousCheckpointNumber = CheckpointNumber(checkpointNumber - 1);
|
|
233
|
+
if (previousCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
|
|
234
|
+
return undefined;
|
|
235
|
+
}
|
|
236
|
+
const previousCheckpointData = await this.getCheckpointData(previousCheckpointNumber);
|
|
237
|
+
if (previousCheckpointData === undefined) {
|
|
238
|
+
throw new CheckpointNotFoundError(previousCheckpointNumber);
|
|
239
|
+
}
|
|
240
|
+
const previousBlockNumber = BlockNumber(previousCheckpointData.startBlock + previousCheckpointData.blockCount - 1);
|
|
241
|
+
const previousBlock = await this.getBlock(previousBlockNumber);
|
|
242
|
+
if (previousBlock === undefined) {
|
|
243
|
+
throw new BlockNotFoundError(previousBlockNumber);
|
|
244
|
+
}
|
|
245
|
+
return previousBlock;
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Validates that blocks are sequential, have correct indexes, and chain via archive roots.
|
|
249
|
+
* This is the same validation used for both confirmed checkpoints (addCheckpoints) and
|
|
250
|
+
* proposed checkpoints (setProposedCheckpoint).
|
|
251
|
+
*/ validateCheckpointBlocks(blocks, previousBlock) {
|
|
252
|
+
for (const block of blocks){
|
|
253
|
+
if (previousBlock) {
|
|
254
|
+
if (previousBlock.number !== block.number - 1) {
|
|
255
|
+
throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
|
|
256
|
+
}
|
|
257
|
+
if (previousBlock.checkpointNumber === block.checkpointNumber) {
|
|
258
|
+
if (previousBlock.indexWithinCheckpoint !== block.indexWithinCheckpoint - 1) {
|
|
259
|
+
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
|
|
260
|
+
}
|
|
261
|
+
} else if (block.indexWithinCheckpoint !== 0) {
|
|
262
|
+
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
|
|
263
|
+
}
|
|
264
|
+
if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
|
|
265
|
+
throw new BlockArchiveNotConsistentError(block.number, previousBlock.number, block.header.lastArchive.root, previousBlock.archive.root);
|
|
266
|
+
}
|
|
267
|
+
} else {
|
|
268
|
+
if (block.indexWithinCheckpoint !== 0) {
|
|
269
|
+
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, undefined);
|
|
270
|
+
}
|
|
271
|
+
if (block.number !== INITIAL_L2_BLOCK_NUM) {
|
|
272
|
+
throw new BlockNumberNotSequentialError(block.number, undefined);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
previousBlock = block;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
195
278
|
async addBlockToDatabase(block, checkpointNumber, indexWithinCheckpoint) {
|
|
196
|
-
const blockHash =
|
|
279
|
+
const blockHash = await block.hash();
|
|
197
280
|
await this.#blocks.set(block.number, {
|
|
198
281
|
header: block.header.toBuffer(),
|
|
199
282
|
blockHash: blockHash.toBuffer(),
|
|
@@ -228,41 +311,55 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
228
311
|
await this.#blockArchiveIndex.delete(block.archive.root.toString());
|
|
229
312
|
}
|
|
230
313
|
/**
|
|
231
|
-
*
|
|
232
|
-
*
|
|
233
|
-
*
|
|
234
|
-
|
|
235
|
-
* @returns True if the operation is successful
|
|
236
|
-
*/ async unwindCheckpoints(from, checkpointsToUnwind) {
|
|
314
|
+
* Removes all checkpoints with checkpoint number > checkpointNumber.
|
|
315
|
+
* Also removes ALL blocks (both checkpointed and uncheckpointed) after the last block of the given checkpoint.
|
|
316
|
+
* @param checkpointNumber - Remove all checkpoints strictly after this one.
|
|
317
|
+
*/ async removeCheckpointsAfter(checkpointNumber) {
|
|
237
318
|
return await this.db.transactionAsync(async ()=>{
|
|
238
|
-
const
|
|
239
|
-
if (
|
|
240
|
-
|
|
319
|
+
const latestCheckpointNumber = await this.getLatestCheckpointNumber();
|
|
320
|
+
if (checkpointNumber >= latestCheckpointNumber) {
|
|
321
|
+
this.#log.warn(`No checkpoints to remove after ${checkpointNumber} (latest is ${latestCheckpointNumber})`);
|
|
322
|
+
return {
|
|
323
|
+
blocksRemoved: undefined
|
|
324
|
+
};
|
|
241
325
|
}
|
|
326
|
+
// If the proven checkpoint is beyond the target, update it
|
|
242
327
|
const proven = await this.getProvenCheckpointNumber();
|
|
243
|
-
if (
|
|
244
|
-
|
|
328
|
+
if (proven > checkpointNumber) {
|
|
329
|
+
this.#log.warn(`Updating proven checkpoint ${proven} to last valid checkpoint ${checkpointNumber}`);
|
|
330
|
+
await this.setProvenCheckpointNumber(checkpointNumber);
|
|
245
331
|
}
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
332
|
+
// Find the last block number to keep (last block of the given checkpoint, or 0 if no checkpoint)
|
|
333
|
+
let lastBlockToKeep;
|
|
334
|
+
if (checkpointNumber <= 0) {
|
|
335
|
+
lastBlockToKeep = BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
336
|
+
} else {
|
|
337
|
+
const targetCheckpoint = await this.#checkpoints.getAsync(checkpointNumber);
|
|
338
|
+
if (!targetCheckpoint) {
|
|
339
|
+
throw new Error(`Target checkpoint ${checkpointNumber} not found in store`);
|
|
252
340
|
}
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
this.#
|
|
341
|
+
lastBlockToKeep = BlockNumber(targetCheckpoint.startBlock + targetCheckpoint.blockCount - 1);
|
|
342
|
+
}
|
|
343
|
+
// Remove all blocks after lastBlockToKeep (both checkpointed and uncheckpointed)
|
|
344
|
+
const blocksRemoved = await this.removeBlocksAfter(lastBlockToKeep);
|
|
345
|
+
// Remove all checkpoints after the target
|
|
346
|
+
for(let c = latestCheckpointNumber; c > checkpointNumber; c = CheckpointNumber(c - 1)){
|
|
347
|
+
const checkpointStorage = await this.#checkpoints.getAsync(c);
|
|
348
|
+
if (checkpointStorage) {
|
|
349
|
+
const slotNumber = CheckpointHeader.fromBuffer(checkpointStorage.header).slotNumber;
|
|
350
|
+
await this.#slotToCheckpoint.delete(slotNumber);
|
|
263
351
|
}
|
|
352
|
+
await this.#checkpoints.delete(c);
|
|
353
|
+
this.#log.debug(`Removed checkpoint ${c}`);
|
|
264
354
|
}
|
|
265
|
-
|
|
355
|
+
// Clear any proposed checkpoint that was orphaned by the removal (its base chain no longer exists)
|
|
356
|
+
const proposedCheckpointNumber = await this.getProposedCheckpointNumber();
|
|
357
|
+
if (proposedCheckpointNumber > checkpointNumber) {
|
|
358
|
+
await this.#proposedCheckpoint.delete();
|
|
359
|
+
}
|
|
360
|
+
return {
|
|
361
|
+
blocksRemoved
|
|
362
|
+
};
|
|
266
363
|
});
|
|
267
364
|
}
|
|
268
365
|
async getCheckpointData(checkpointNumber) {
|
|
@@ -283,17 +380,30 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
283
380
|
}
|
|
284
381
|
return checkpoints;
|
|
285
382
|
}
|
|
383
|
+
/** Returns checkpoint data for all checkpoints whose slot falls within the given range (inclusive). */ async getCheckpointDataForSlotRange(startSlot, endSlot) {
|
|
384
|
+
const result = [];
|
|
385
|
+
for await (const [, checkpointNumber] of this.#slotToCheckpoint.entriesAsync({
|
|
386
|
+
start: startSlot,
|
|
387
|
+
end: endSlot + 1
|
|
388
|
+
})){
|
|
389
|
+
const checkpointStorage = await this.#checkpoints.getAsync(checkpointNumber);
|
|
390
|
+
if (checkpointStorage) {
|
|
391
|
+
result.push(this.checkpointDataFromCheckpointStorage(checkpointStorage));
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
return result;
|
|
395
|
+
}
|
|
286
396
|
checkpointDataFromCheckpointStorage(checkpointStorage) {
|
|
287
|
-
|
|
397
|
+
return {
|
|
288
398
|
header: CheckpointHeader.fromBuffer(checkpointStorage.header),
|
|
289
399
|
archive: AppendOnlyTreeSnapshot.fromBuffer(checkpointStorage.archive),
|
|
400
|
+
checkpointOutHash: Fr.fromBuffer(checkpointStorage.checkpointOutHash),
|
|
290
401
|
checkpointNumber: CheckpointNumber(checkpointStorage.checkpointNumber),
|
|
291
|
-
startBlock: checkpointStorage.startBlock,
|
|
292
|
-
|
|
402
|
+
startBlock: BlockNumber(checkpointStorage.startBlock),
|
|
403
|
+
blockCount: checkpointStorage.blockCount,
|
|
293
404
|
l1: L1PublishedData.fromBuffer(checkpointStorage.l1),
|
|
294
|
-
attestations: checkpointStorage.attestations
|
|
405
|
+
attestations: checkpointStorage.attestations.map((buf)=>CommitteeAttestation.fromBuffer(buf))
|
|
295
406
|
};
|
|
296
|
-
return data;
|
|
297
407
|
}
|
|
298
408
|
async getBlocksForCheckpoint(checkpointNumber) {
|
|
299
409
|
const checkpoint = await this.#checkpoints.getAsync(checkpointNumber);
|
|
@@ -302,7 +412,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
302
412
|
}
|
|
303
413
|
const blocksForCheckpoint = await toArray(this.#blocks.entriesAsync({
|
|
304
414
|
start: checkpoint.startBlock,
|
|
305
|
-
end: checkpoint.startBlock + checkpoint.
|
|
415
|
+
end: checkpoint.startBlock + checkpoint.blockCount
|
|
306
416
|
}));
|
|
307
417
|
const converted = await Promise.all(blocksForCheckpoint.map((x)=>this.getBlockFromBlockStorage(x[0], x[1])));
|
|
308
418
|
return converted.filter(isDefined);
|
|
@@ -332,13 +442,14 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
332
442
|
}
|
|
333
443
|
/**
|
|
334
444
|
* Removes all blocks with block number > blockNumber.
|
|
445
|
+
* Does not remove any associated checkpoints.
|
|
335
446
|
* @param blockNumber - The block number to remove after.
|
|
336
447
|
* @returns The removed blocks (for event emission).
|
|
337
|
-
*/ async
|
|
448
|
+
*/ async removeBlocksAfter(blockNumber) {
|
|
338
449
|
return await this.db.transactionAsync(async ()=>{
|
|
339
450
|
const removedBlocks = [];
|
|
340
451
|
// Get the latest block number to determine the range
|
|
341
|
-
const latestBlockNumber = await this.
|
|
452
|
+
const latestBlockNumber = await this.getLatestL2BlockNumber();
|
|
342
453
|
// Iterate from blockNumber + 1 to latestBlockNumber
|
|
343
454
|
for(let bn = blockNumber + 1; bn <= latestBlockNumber; bn++){
|
|
344
455
|
const block = await this.getBlock(BlockNumber(bn));
|
|
@@ -362,16 +473,9 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
362
473
|
if (!checkpointStorage) {
|
|
363
474
|
throw new CheckpointNotFoundError(provenCheckpointNumber);
|
|
364
475
|
} else {
|
|
365
|
-
return BlockNumber(checkpointStorage.startBlock + checkpointStorage.
|
|
476
|
+
return BlockNumber(checkpointStorage.startBlock + checkpointStorage.blockCount - 1);
|
|
366
477
|
}
|
|
367
478
|
}
|
|
368
|
-
async getLatestBlockNumber() {
|
|
369
|
-
const [latestBlocknumber] = await toArray(this.#blocks.keysAsync({
|
|
370
|
-
reverse: true,
|
|
371
|
-
limit: 1
|
|
372
|
-
}));
|
|
373
|
-
return typeof latestBlocknumber === 'number' ? BlockNumber(latestBlocknumber) : BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
374
|
-
}
|
|
375
479
|
async getLatestCheckpointNumber() {
|
|
376
480
|
const [latestCheckpointNumber] = await toArray(this.#checkpoints.keysAsync({
|
|
377
481
|
reverse: true,
|
|
@@ -382,6 +486,70 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
382
486
|
}
|
|
383
487
|
return CheckpointNumber(latestCheckpointNumber);
|
|
384
488
|
}
|
|
489
|
+
async hasProposedCheckpoint() {
|
|
490
|
+
const proposed = await this.#proposedCheckpoint.getAsync();
|
|
491
|
+
return proposed !== undefined;
|
|
492
|
+
}
|
|
493
|
+
/** Deletes the proposed checkpoint from storage. */ async deleteProposedCheckpoint() {
|
|
494
|
+
await this.#proposedCheckpoint.delete();
|
|
495
|
+
}
|
|
496
|
+
/** Clears the proposed checkpoint if the given confirmed checkpoint number supersedes it. */ async clearProposedCheckpointIfSuperseded(confirmedCheckpointNumber) {
|
|
497
|
+
const proposedCheckpointNumber = await this.getProposedCheckpointNumber();
|
|
498
|
+
if (proposedCheckpointNumber <= confirmedCheckpointNumber) {
|
|
499
|
+
await this.#proposedCheckpoint.delete();
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
/** Returns the proposed checkpoint data, or undefined if no proposed checkpoint exists. No fallback to confirmed. */ async getProposedCheckpointOnly() {
|
|
503
|
+
const stored = await this.#proposedCheckpoint.getAsync();
|
|
504
|
+
if (!stored) {
|
|
505
|
+
return undefined;
|
|
506
|
+
}
|
|
507
|
+
return this.convertToProposedCheckpointData(stored);
|
|
508
|
+
}
|
|
509
|
+
/**
|
|
510
|
+
* Gets the checkpoint at the proposed tip
|
|
511
|
+
* - pending checkpoint if it exists
|
|
512
|
+
* - fallsback to latest confirmed checkpoint otherwise
|
|
513
|
+
* @returns CommonCheckpointData
|
|
514
|
+
*/ async getProposedCheckpoint() {
|
|
515
|
+
const stored = await this.#proposedCheckpoint.getAsync();
|
|
516
|
+
if (!stored) {
|
|
517
|
+
return this.getCheckpointData(await this.getLatestCheckpointNumber());
|
|
518
|
+
}
|
|
519
|
+
return this.convertToProposedCheckpointData(stored);
|
|
520
|
+
}
|
|
521
|
+
convertToProposedCheckpointData(stored) {
|
|
522
|
+
return {
|
|
523
|
+
checkpointNumber: CheckpointNumber(stored.checkpointNumber),
|
|
524
|
+
header: CheckpointHeader.fromBuffer(stored.header),
|
|
525
|
+
archive: AppendOnlyTreeSnapshot.fromBuffer(stored.archive),
|
|
526
|
+
checkpointOutHash: Fr.fromBuffer(stored.checkpointOutHash),
|
|
527
|
+
startBlock: BlockNumber(stored.startBlock),
|
|
528
|
+
blockCount: stored.blockCount,
|
|
529
|
+
totalManaUsed: BigInt(stored.totalManaUsed),
|
|
530
|
+
feeAssetPriceModifier: BigInt(stored.feeAssetPriceModifier)
|
|
531
|
+
};
|
|
532
|
+
}
|
|
533
|
+
/**
|
|
534
|
+
* Attempts to get the proposedCheckpoint's number, if there is not one, then fallback to the latest confirmed checkpoint number.
|
|
535
|
+
* @returns CheckpointNumber
|
|
536
|
+
*/ async getProposedCheckpointNumber() {
|
|
537
|
+
const proposed = await this.getProposedCheckpoint();
|
|
538
|
+
if (!proposed) {
|
|
539
|
+
return await this.getLatestCheckpointNumber();
|
|
540
|
+
}
|
|
541
|
+
return CheckpointNumber(proposed.checkpointNumber);
|
|
542
|
+
}
|
|
543
|
+
/**
|
|
544
|
+
* Attempts to get the proposedCheckpoint's block number, if there is not one, then fallback to the checkpointed block number
|
|
545
|
+
* @returns BlockNumber
|
|
546
|
+
*/ async getProposedCheckpointL2BlockNumber() {
|
|
547
|
+
const proposed = await this.getProposedCheckpoint();
|
|
548
|
+
if (!proposed) {
|
|
549
|
+
return await this.getCheckpointedL2BlockNumber();
|
|
550
|
+
}
|
|
551
|
+
return BlockNumber(proposed.startBlock + proposed.blockCount - 1);
|
|
552
|
+
}
|
|
385
553
|
async getCheckpointedBlock(number) {
|
|
386
554
|
const blockStorage = await this.#blocks.getAsync(number);
|
|
387
555
|
if (!blockStorage) {
|
|
@@ -444,6 +612,28 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
444
612
|
}
|
|
445
613
|
}
|
|
446
614
|
/**
|
|
615
|
+
* Gets block metadata (without tx data) by block number.
|
|
616
|
+
* @param blockNumber - The number of the block to return.
|
|
617
|
+
* @returns The requested block data.
|
|
618
|
+
*/ async getBlockData(blockNumber) {
|
|
619
|
+
const blockStorage = await this.#blocks.getAsync(blockNumber);
|
|
620
|
+
if (!blockStorage || !blockStorage.header) {
|
|
621
|
+
return undefined;
|
|
622
|
+
}
|
|
623
|
+
return this.getBlockDataFromBlockStorage(blockStorage);
|
|
624
|
+
}
|
|
625
|
+
/**
|
|
626
|
+
* Gets block metadata (without tx data) by archive root.
|
|
627
|
+
* @param archive - The archive root of the block to return.
|
|
628
|
+
* @returns The requested block data.
|
|
629
|
+
*/ async getBlockDataByArchive(archive) {
|
|
630
|
+
const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
|
|
631
|
+
if (blockNumber === undefined) {
|
|
632
|
+
return undefined;
|
|
633
|
+
}
|
|
634
|
+
return this.getBlockData(BlockNumber(blockNumber));
|
|
635
|
+
}
|
|
636
|
+
/**
|
|
447
637
|
* Gets an L2 block.
|
|
448
638
|
* @param blockNumber - The number of the block to return.
|
|
449
639
|
* @returns The requested L2 block.
|
|
@@ -533,12 +723,19 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
533
723
|
];
|
|
534
724
|
}
|
|
535
725
|
}
|
|
726
|
+
getBlockDataFromBlockStorage(blockStorage) {
|
|
727
|
+
return {
|
|
728
|
+
header: BlockHeader.fromBuffer(blockStorage.header),
|
|
729
|
+
archive: AppendOnlyTreeSnapshot.fromBuffer(blockStorage.archive),
|
|
730
|
+
blockHash: BlockHash.fromBuffer(blockStorage.blockHash),
|
|
731
|
+
checkpointNumber: CheckpointNumber(blockStorage.checkpointNumber),
|
|
732
|
+
indexWithinCheckpoint: IndexWithinCheckpoint(blockStorage.indexWithinCheckpoint)
|
|
733
|
+
};
|
|
734
|
+
}
|
|
536
735
|
async getBlockFromBlockStorage(blockNumber, blockStorage) {
|
|
537
|
-
const header =
|
|
538
|
-
|
|
539
|
-
const
|
|
540
|
-
header.setHash(Fr.fromBuffer(blockHash));
|
|
541
|
-
const blockHashString = bufferToHex(blockHash);
|
|
736
|
+
const { header, archive, blockHash, checkpointNumber, indexWithinCheckpoint } = this.getBlockDataFromBlockStorage(blockStorage);
|
|
737
|
+
header.setHash(blockHash);
|
|
738
|
+
const blockHashString = bufferToHex(blockStorage.blockHash);
|
|
542
739
|
const blockTxsBuffer = await this.#blockTxs.getAsync(blockHashString);
|
|
543
740
|
if (blockTxsBuffer === undefined) {
|
|
544
741
|
this.#log.warn(`Could not find body for block ${header.globalVariables.blockNumber} ${blockHash}`);
|
|
@@ -556,7 +753,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
556
753
|
txEffects.push(deserializeIndexedTxEffect(txEffect).data);
|
|
557
754
|
}
|
|
558
755
|
const body = new Body(txEffects);
|
|
559
|
-
const block = new
|
|
756
|
+
const block = new L2Block(archive, header, body, checkpointNumber, indexWithinCheckpoint);
|
|
560
757
|
if (block.number !== blockNumber) {
|
|
561
758
|
throw new Error(`Block number mismatch when retrieving block from archive (expected ${blockNumber} but got ${block.number} with hash ${blockHashString})`);
|
|
562
759
|
}
|
|
@@ -577,12 +774,31 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
577
774
|
* Gets a receipt of a settled tx.
|
|
578
775
|
* @param txHash - The hash of a tx we try to get the receipt for.
|
|
579
776
|
* @returns The requested tx receipt (or undefined if not found).
|
|
580
|
-
*/ async getSettledTxReceipt(txHash) {
|
|
777
|
+
*/ async getSettledTxReceipt(txHash, l1Constants) {
|
|
581
778
|
const txEffect = await this.getTxEffect(txHash);
|
|
582
779
|
if (!txEffect) {
|
|
583
780
|
return undefined;
|
|
584
781
|
}
|
|
585
|
-
|
|
782
|
+
const blockNumber = BlockNumber(txEffect.l2BlockNumber);
|
|
783
|
+
// Use existing archiver methods to determine finalization level
|
|
784
|
+
const [provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber, blockData] = await Promise.all([
|
|
785
|
+
this.getProvenBlockNumber(),
|
|
786
|
+
this.getCheckpointedL2BlockNumber(),
|
|
787
|
+
this.getFinalizedL2BlockNumber(),
|
|
788
|
+
this.getBlockData(blockNumber)
|
|
789
|
+
]);
|
|
790
|
+
let status;
|
|
791
|
+
if (blockNumber <= finalizedBlockNumber) {
|
|
792
|
+
status = TxStatus.FINALIZED;
|
|
793
|
+
} else if (blockNumber <= provenBlockNumber) {
|
|
794
|
+
status = TxStatus.PROVEN;
|
|
795
|
+
} else if (blockNumber <= checkpointedBlockNumber) {
|
|
796
|
+
status = TxStatus.CHECKPOINTED;
|
|
797
|
+
} else {
|
|
798
|
+
status = TxStatus.PROPOSED;
|
|
799
|
+
}
|
|
800
|
+
const epochNumber = blockData && l1Constants ? getEpochAtSlot(blockData.header.globalVariables.slotNumber, l1Constants) : undefined;
|
|
801
|
+
return new TxReceipt(txHash, status, TxReceipt.executionResultFromRevertCode(txEffect.data.revertCode), undefined, txEffect.data.transactionFee.toBigInt(), txEffect.l2BlockHash, blockNumber, epochNumber);
|
|
586
802
|
}
|
|
587
803
|
/**
|
|
588
804
|
* Looks up which block included the requested tx effect.
|
|
@@ -615,7 +831,7 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
615
831
|
if (!checkpoint) {
|
|
616
832
|
return BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
617
833
|
}
|
|
618
|
-
return BlockNumber(checkpoint.startBlock + checkpoint.
|
|
834
|
+
return BlockNumber(checkpoint.startBlock + checkpoint.blockCount - 1);
|
|
619
835
|
}
|
|
620
836
|
async getLatestL2BlockNumber() {
|
|
621
837
|
const [lastBlockNumber] = await toArray(this.#blocks.keysAsync({
|
|
@@ -633,6 +849,42 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
633
849
|
setSynchedL1BlockNumber(l1BlockNumber) {
|
|
634
850
|
return this.#lastSynchedL1Block.set(l1BlockNumber);
|
|
635
851
|
}
|
|
852
|
+
/** Sets the proposed checkpoint (not yet L1-confirmed). Only accepts confirmed + 1.
|
|
853
|
+
* Computes archive and checkpointOutHash from the stored blocks. */ async setProposedCheckpoint(proposed) {
|
|
854
|
+
return await this.db.transactionAsync(async ()=>{
|
|
855
|
+
const current = await this.getProposedCheckpointNumber();
|
|
856
|
+
if (proposed.checkpointNumber <= current) {
|
|
857
|
+
throw new ProposedCheckpointStaleError(proposed.checkpointNumber, current);
|
|
858
|
+
}
|
|
859
|
+
const confirmed = await this.getLatestCheckpointNumber();
|
|
860
|
+
if (proposed.checkpointNumber !== confirmed + 1) {
|
|
861
|
+
throw new ProposedCheckpointNotSequentialError(proposed.checkpointNumber, confirmed);
|
|
862
|
+
}
|
|
863
|
+
// Ensure the previous checkpoint + blocks exist
|
|
864
|
+
const previousBlock = await this.getPreviousCheckpointBlock(proposed.checkpointNumber);
|
|
865
|
+
const blocks = [];
|
|
866
|
+
for(let i = 0; i < proposed.blockCount; i++){
|
|
867
|
+
const block = await this.getBlock(BlockNumber(proposed.startBlock + i));
|
|
868
|
+
if (!block) {
|
|
869
|
+
throw new BlockNotFoundError(proposed.startBlock + i);
|
|
870
|
+
}
|
|
871
|
+
blocks.push(block);
|
|
872
|
+
}
|
|
873
|
+
this.validateCheckpointBlocks(blocks, previousBlock);
|
|
874
|
+
const archive = blocks[blocks.length - 1].archive;
|
|
875
|
+
const checkpointOutHash = Checkpoint.getCheckpointOutHash(blocks);
|
|
876
|
+
await this.#proposedCheckpoint.set({
|
|
877
|
+
header: proposed.header.toBuffer(),
|
|
878
|
+
archive: archive.toBuffer(),
|
|
879
|
+
checkpointOutHash: checkpointOutHash.toBuffer(),
|
|
880
|
+
checkpointNumber: proposed.checkpointNumber,
|
|
881
|
+
startBlock: proposed.startBlock,
|
|
882
|
+
blockCount: proposed.blockCount,
|
|
883
|
+
totalManaUsed: proposed.totalManaUsed.toString(),
|
|
884
|
+
feeAssetPriceModifier: proposed.feeAssetPriceModifier.toString()
|
|
885
|
+
});
|
|
886
|
+
});
|
|
887
|
+
}
|
|
636
888
|
async getProvenCheckpointNumber() {
|
|
637
889
|
const [latestCheckpointNumber, provenCheckpointNumber] = await Promise.all([
|
|
638
890
|
this.getLatestCheckpointNumber(),
|
|
@@ -644,6 +896,16 @@ export { TxReceipt } from '@aztec/stdlib/tx';
|
|
|
644
896
|
const result = await this.#lastProvenCheckpoint.set(checkpointNumber);
|
|
645
897
|
return result;
|
|
646
898
|
}
|
|
899
|
+
async getFinalizedCheckpointNumber() {
|
|
900
|
+
const [latestCheckpointNumber, finalizedCheckpointNumber] = await Promise.all([
|
|
901
|
+
this.getLatestCheckpointNumber(),
|
|
902
|
+
this.#lastFinalizedCheckpoint.getAsync()
|
|
903
|
+
]);
|
|
904
|
+
return (finalizedCheckpointNumber ?? 0) > latestCheckpointNumber ? latestCheckpointNumber : CheckpointNumber(finalizedCheckpointNumber ?? 0);
|
|
905
|
+
}
|
|
906
|
+
setFinalizedCheckpointNumber(checkpointNumber) {
|
|
907
|
+
return this.#lastFinalizedCheckpoint.set(checkpointNumber);
|
|
908
|
+
}
|
|
647
909
|
#computeBlockRange(start, limit) {
|
|
648
910
|
if (limit < 1) {
|
|
649
911
|
throw new Error(`Invalid limit: ${limit}`);
|