@aztec/archiver 0.0.0-test.1 → 0.0.1-commit.1142ef1
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 +27 -6
- package/dest/archiver/archiver.d.ts +204 -94
- package/dest/archiver/archiver.d.ts.map +1 -1
- package/dest/archiver/archiver.js +1616 -414
- package/dest/archiver/archiver_store.d.ts +178 -83
- 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 +2373 -397
- package/dest/archiver/config.d.ts +7 -22
- package/dest/archiver/config.d.ts.map +1 -1
- package/dest/archiver/config.js +30 -14
- package/dest/archiver/errors.d.ts +33 -1
- package/dest/archiver/errors.d.ts.map +1 -1
- package/dest/archiver/errors.js +49 -0
- package/dest/archiver/index.d.ts +3 -4
- package/dest/archiver/index.d.ts.map +1 -1
- package/dest/archiver/index.js +1 -2
- package/dest/archiver/instrumentation.d.ts +14 -6
- package/dest/archiver/instrumentation.d.ts.map +1 -1
- package/dest/archiver/instrumentation.js +45 -41
- package/dest/archiver/kv_archiver_store/block_store.d.ts +98 -21
- package/dest/archiver/kv_archiver_store/block_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/block_store.js +495 -86
- package/dest/archiver/kv_archiver_store/contract_class_store.d.ts +4 -4
- package/dest/archiver/kv_archiver_store/contract_class_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/contract_class_store.js +13 -19
- package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts +12 -9
- package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/contract_instance_store.js +30 -16
- package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +81 -75
- package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/kv_archiver_store.js +145 -83
- package/dest/archiver/kv_archiver_store/log_store.d.ts +12 -16
- package/dest/archiver/kv_archiver_store/log_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/log_store.js +151 -114
- package/dest/archiver/kv_archiver_store/message_store.d.ts +25 -18
- package/dest/archiver/kv_archiver_store/message_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/message_store.js +152 -49
- package/dest/archiver/l1/bin/retrieve-calldata.d.ts +3 -0
- package/dest/archiver/l1/bin/retrieve-calldata.d.ts.map +1 -0
- package/dest/archiver/l1/bin/retrieve-calldata.js +149 -0
- package/dest/archiver/l1/calldata_retriever.d.ts +112 -0
- package/dest/archiver/l1/calldata_retriever.d.ts.map +1 -0
- package/dest/archiver/l1/calldata_retriever.js +471 -0
- package/dest/archiver/l1/data_retrieval.d.ts +90 -0
- package/dest/archiver/l1/data_retrieval.d.ts.map +1 -0
- package/dest/archiver/l1/data_retrieval.js +331 -0
- package/dest/archiver/l1/debug_tx.d.ts +19 -0
- package/dest/archiver/l1/debug_tx.d.ts.map +1 -0
- package/dest/archiver/l1/debug_tx.js +73 -0
- package/dest/archiver/l1/spire_proposer.d.ts +70 -0
- package/dest/archiver/l1/spire_proposer.d.ts.map +1 -0
- package/dest/archiver/l1/spire_proposer.js +157 -0
- package/dest/archiver/l1/trace_tx.d.ts +97 -0
- package/dest/archiver/l1/trace_tx.d.ts.map +1 -0
- package/dest/archiver/l1/trace_tx.js +91 -0
- package/dest/archiver/l1/types.d.ts +12 -0
- package/dest/archiver/l1/types.d.ts.map +1 -0
- package/dest/archiver/l1/types.js +3 -0
- package/dest/archiver/l1/validate_trace.d.ts +29 -0
- package/dest/archiver/l1/validate_trace.d.ts.map +1 -0
- package/dest/archiver/l1/validate_trace.js +150 -0
- package/dest/archiver/structs/data_retrieval.d.ts +1 -1
- package/dest/archiver/structs/inbox_message.d.ts +15 -0
- package/dest/archiver/structs/inbox_message.d.ts.map +1 -0
- package/dest/archiver/structs/inbox_message.js +39 -0
- package/dest/archiver/structs/published.d.ts +2 -11
- package/dest/archiver/structs/published.d.ts.map +1 -1
- package/dest/archiver/structs/published.js +1 -1
- package/dest/archiver/validation.d.ts +17 -0
- package/dest/archiver/validation.d.ts.map +1 -0
- package/dest/archiver/validation.js +98 -0
- package/dest/factory.d.ts +9 -14
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +22 -52
- package/dest/index.d.ts +2 -2
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +1 -1
- package/dest/rpc/index.d.ts +2 -3
- package/dest/rpc/index.d.ts.map +1 -1
- package/dest/rpc/index.js +1 -4
- package/dest/test/index.d.ts +1 -1
- package/dest/test/mock_archiver.d.ts +16 -8
- package/dest/test/mock_archiver.d.ts.map +1 -1
- package/dest/test/mock_archiver.js +19 -14
- package/dest/test/mock_l1_to_l2_message_source.d.ts +9 -6
- package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
- package/dest/test/mock_l1_to_l2_message_source.js +30 -7
- package/dest/test/mock_l2_block_source.d.ts +56 -13
- package/dest/test/mock_l2_block_source.d.ts.map +1 -1
- package/dest/test/mock_l2_block_source.js +196 -25
- package/dest/test/mock_structs.d.ts +10 -0
- package/dest/test/mock_structs.d.ts.map +1 -0
- package/dest/test/mock_structs.js +38 -0
- package/package.json +29 -30
- package/src/archiver/archiver.ts +1596 -512
- package/src/archiver/archiver_store.ts +205 -88
- package/src/archiver/archiver_store_test_suite.ts +2386 -354
- package/src/archiver/config.ts +38 -46
- package/src/archiver/errors.ts +85 -0
- package/src/archiver/index.ts +2 -3
- package/src/archiver/instrumentation.ts +65 -45
- package/src/archiver/kv_archiver_store/block_store.ts +668 -101
- package/src/archiver/kv_archiver_store/contract_class_store.ts +14 -24
- package/src/archiver/kv_archiver_store/contract_instance_store.ts +36 -28
- package/src/archiver/kv_archiver_store/kv_archiver_store.ts +197 -113
- package/src/archiver/kv_archiver_store/log_store.ts +204 -132
- package/src/archiver/kv_archiver_store/message_store.ts +213 -54
- package/src/archiver/l1/README.md +98 -0
- package/src/archiver/l1/bin/retrieve-calldata.ts +182 -0
- package/src/archiver/l1/calldata_retriever.ts +641 -0
- package/src/archiver/l1/data_retrieval.ts +512 -0
- package/src/archiver/l1/debug_tx.ts +99 -0
- package/src/archiver/l1/spire_proposer.ts +160 -0
- package/src/archiver/l1/trace_tx.ts +128 -0
- package/src/archiver/l1/types.ts +13 -0
- package/src/archiver/l1/validate_trace.ts +211 -0
- package/src/archiver/structs/inbox_message.ts +41 -0
- package/src/archiver/structs/published.ts +1 -11
- package/src/archiver/validation.ts +124 -0
- package/src/factory.ts +28 -69
- package/src/index.ts +1 -1
- package/src/rpc/index.ts +1 -5
- package/src/test/fixtures/debug_traceTransaction-multicall3.json +88 -0
- package/src/test/fixtures/debug_traceTransaction-multiplePropose.json +153 -0
- package/src/test/fixtures/debug_traceTransaction-proxied.json +122 -0
- package/src/test/fixtures/trace_transaction-multicall3.json +65 -0
- package/src/test/fixtures/trace_transaction-multiplePropose.json +319 -0
- package/src/test/fixtures/trace_transaction-proxied.json +128 -0
- package/src/test/fixtures/trace_transaction-randomRevert.json +216 -0
- package/src/test/mock_archiver.ts +22 -16
- package/src/test/mock_l1_to_l2_message_source.ts +26 -8
- package/src/test/mock_l2_block_source.ts +254 -31
- package/src/test/mock_structs.ts +50 -0
- package/dest/archiver/data_retrieval.d.ts +0 -74
- package/dest/archiver/data_retrieval.d.ts.map +0 -1
- package/dest/archiver/data_retrieval.js +0 -283
- package/dest/archiver/kv_archiver_store/nullifier_store.d.ts +0 -12
- package/dest/archiver/kv_archiver_store/nullifier_store.d.ts.map +0 -1
- package/dest/archiver/kv_archiver_store/nullifier_store.js +0 -73
- package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.d.ts +0 -23
- package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.d.ts.map +0 -1
- package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.js +0 -49
- package/dest/archiver/memory_archiver_store/memory_archiver_store.d.ts +0 -175
- package/dest/archiver/memory_archiver_store/memory_archiver_store.d.ts.map +0 -1
- package/dest/archiver/memory_archiver_store/memory_archiver_store.js +0 -636
- package/src/archiver/data_retrieval.ts +0 -422
- package/src/archiver/kv_archiver_store/nullifier_store.ts +0 -97
- package/src/archiver/memory_archiver_store/l1_to_l2_message_store.ts +0 -61
- package/src/archiver/memory_archiver_store/memory_archiver_store.ts +0 -801
|
@@ -1,22 +1,78 @@
|
|
|
1
|
-
import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
|
|
1
|
+
import { INITIAL_CHECKPOINT_NUMBER, INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
|
|
2
|
+
import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
|
|
3
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
2
4
|
import { toArray } from '@aztec/foundation/iterable';
|
|
3
5
|
import { createLogger } from '@aztec/foundation/log';
|
|
6
|
+
import { BufferReader } from '@aztec/foundation/serialize';
|
|
7
|
+
import { bufferToHex } from '@aztec/foundation/string';
|
|
8
|
+
import { isDefined } from '@aztec/foundation/types';
|
|
4
9
|
import type { AztecAsyncKVStore, AztecAsyncMap, AztecAsyncSingleton, Range } from '@aztec/kv-store';
|
|
5
10
|
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
6
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
Body,
|
|
13
|
+
CheckpointedL2Block,
|
|
14
|
+
CommitteeAttestation,
|
|
15
|
+
L2BlockHash,
|
|
16
|
+
L2BlockNew,
|
|
17
|
+
type ValidateCheckpointResult,
|
|
18
|
+
deserializeValidateCheckpointResult,
|
|
19
|
+
serializeValidateCheckpointResult,
|
|
20
|
+
} from '@aztec/stdlib/block';
|
|
21
|
+
import { L1PublishedData, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
|
|
22
|
+
import { CheckpointHeader } from '@aztec/stdlib/rollup';
|
|
7
23
|
import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
24
|
+
import {
|
|
25
|
+
BlockHeader,
|
|
26
|
+
type IndexedTxEffect,
|
|
27
|
+
TxEffect,
|
|
28
|
+
TxHash,
|
|
29
|
+
TxReceipt,
|
|
30
|
+
deserializeIndexedTxEffect,
|
|
31
|
+
serializeIndexedTxEffect,
|
|
32
|
+
} from '@aztec/stdlib/tx';
|
|
33
|
+
|
|
34
|
+
import {
|
|
35
|
+
BlockArchiveNotConsistentError,
|
|
36
|
+
BlockIndexNotSequentialError,
|
|
37
|
+
BlockNotFoundError,
|
|
38
|
+
BlockNumberNotSequentialError,
|
|
39
|
+
CheckpointNotFoundError,
|
|
40
|
+
CheckpointNumberNotConsistentError,
|
|
41
|
+
CheckpointNumberNotSequentialError,
|
|
42
|
+
InitialBlockNumberNotSequentialError,
|
|
43
|
+
InitialCheckpointNumberNotSequentialError,
|
|
44
|
+
} from '../errors.js';
|
|
45
|
+
|
|
46
|
+
export { TxReceipt, type TxEffect, type TxHash } from '@aztec/stdlib/tx';
|
|
13
47
|
|
|
14
48
|
type BlockIndexValue = [blockNumber: number, index: number];
|
|
15
49
|
|
|
16
50
|
type BlockStorage = {
|
|
51
|
+
header: Buffer;
|
|
52
|
+
blockHash: Buffer;
|
|
53
|
+
archive: Buffer;
|
|
54
|
+
checkpointNumber: number;
|
|
55
|
+
indexWithinCheckpoint: number;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
type CheckpointStorage = {
|
|
17
59
|
header: Buffer;
|
|
18
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;
|
|
19
74
|
l1: L1PublishedData;
|
|
75
|
+
attestations: Buffer[];
|
|
20
76
|
};
|
|
21
77
|
|
|
22
78
|
/**
|
|
@@ -26,110 +82,482 @@ export class BlockStore {
|
|
|
26
82
|
/** Map block number to block data */
|
|
27
83
|
#blocks: AztecAsyncMap<number, BlockStorage>;
|
|
28
84
|
|
|
29
|
-
/** Map
|
|
30
|
-
#
|
|
85
|
+
/** Map checkpoint number to checkpoint data */
|
|
86
|
+
#checkpoints: AztecAsyncMap<number, CheckpointStorage>;
|
|
87
|
+
|
|
88
|
+
/** Map block hash to list of tx hashes */
|
|
89
|
+
#blockTxs: AztecAsyncMap<string, Buffer>;
|
|
90
|
+
|
|
91
|
+
/** Tx hash to serialized IndexedTxEffect */
|
|
92
|
+
#txEffects: AztecAsyncMap<string, Buffer>;
|
|
31
93
|
|
|
32
94
|
/** Stores L1 block number in which the last processed L2 block was included */
|
|
33
95
|
#lastSynchedL1Block: AztecAsyncSingleton<bigint>;
|
|
34
96
|
|
|
35
|
-
/** Stores
|
|
36
|
-
#
|
|
37
|
-
|
|
38
|
-
/** Stores l2 epoch number of the last proven epoch */
|
|
39
|
-
#lastProvenL2Epoch: AztecAsyncSingleton<number>;
|
|
97
|
+
/** Stores last proven checkpoint */
|
|
98
|
+
#lastProvenCheckpoint: AztecAsyncSingleton<number>;
|
|
40
99
|
|
|
41
|
-
/**
|
|
42
|
-
#
|
|
100
|
+
/** Stores the pending chain validation status */
|
|
101
|
+
#pendingChainValidationStatus: AztecAsyncSingleton<Buffer>;
|
|
43
102
|
|
|
44
103
|
/** Index mapping a contract's address (as a string) to its location in a block */
|
|
45
104
|
#contractIndex: AztecAsyncMap<string, BlockIndexValue>;
|
|
46
105
|
|
|
106
|
+
/** Index mapping block hash to block number */
|
|
107
|
+
#blockHashIndex: AztecAsyncMap<string, number>;
|
|
108
|
+
|
|
109
|
+
/** Index mapping block archive to block number */
|
|
110
|
+
#blockArchiveIndex: AztecAsyncMap<string, number>;
|
|
111
|
+
|
|
47
112
|
#log = createLogger('archiver:block_store');
|
|
48
113
|
|
|
49
114
|
constructor(private db: AztecAsyncKVStore) {
|
|
50
115
|
this.#blocks = db.openMap('archiver_blocks');
|
|
51
|
-
this.#
|
|
52
|
-
this.#
|
|
116
|
+
this.#blockTxs = db.openMap('archiver_block_txs');
|
|
117
|
+
this.#txEffects = db.openMap('archiver_tx_effects');
|
|
53
118
|
this.#contractIndex = db.openMap('archiver_contract_index');
|
|
119
|
+
this.#blockHashIndex = db.openMap('archiver_block_hash_index');
|
|
120
|
+
this.#blockArchiveIndex = db.openMap('archiver_block_archive_index');
|
|
54
121
|
this.#lastSynchedL1Block = db.openSingleton('archiver_last_synched_l1_block');
|
|
55
|
-
this.#
|
|
56
|
-
this.#
|
|
122
|
+
this.#lastProvenCheckpoint = db.openSingleton('archiver_last_proven_l2_checkpoint');
|
|
123
|
+
this.#pendingChainValidationStatus = db.openSingleton('archiver_pending_chain_validation_status');
|
|
124
|
+
this.#checkpoints = db.openMap('archiver_checkpoints');
|
|
57
125
|
}
|
|
58
126
|
|
|
59
127
|
/**
|
|
60
|
-
* 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
|
|
61
129
|
* @param blocks - The L2 blocks to be added to the store.
|
|
62
130
|
* @returns True if the operation is successful.
|
|
63
131
|
*/
|
|
64
|
-
async addBlocks(blocks:
|
|
132
|
+
async addBlocks(blocks: L2BlockNew[], opts: { force?: boolean } = {}): Promise<boolean> {
|
|
65
133
|
if (blocks.length === 0) {
|
|
66
134
|
return true;
|
|
67
135
|
}
|
|
68
136
|
|
|
69
137
|
return await this.db.transactionAsync(async () => {
|
|
138
|
+
// Check that the block immediately before the first block to be added is present in the store.
|
|
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) {
|
|
150
|
+
throw new InitialBlockNumberNotSequentialError(firstBlockNumber, previousBlockNumber);
|
|
151
|
+
}
|
|
152
|
+
|
|
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;
|
|
70
186
|
for (const block of blocks) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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);
|
|
205
|
+
}
|
|
206
|
+
previousBlock = block;
|
|
207
|
+
await this.addBlockToDatabase(block, block.checkpointNumber, block.indexWithinCheckpoint);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return true;
|
|
211
|
+
});
|
|
212
|
+
}
|
|
76
213
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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);
|
|
80
240
|
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
let previousBlockNumber: BlockNumber | undefined = undefined;
|
|
244
|
+
let previousBlock: L2BlockNew | undefined = undefined;
|
|
81
245
|
|
|
82
|
-
|
|
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
|
+
}
|
|
83
254
|
}
|
|
84
255
|
|
|
85
|
-
|
|
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);
|
|
86
321
|
return true;
|
|
87
322
|
});
|
|
88
323
|
}
|
|
89
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
|
+
|
|
90
353
|
/**
|
|
91
|
-
* Unwinds
|
|
354
|
+
* Unwinds checkpoints from the database
|
|
92
355
|
* @param from - The tip of the chain, passed for verification purposes,
|
|
93
356
|
* ensuring that we don't end up deleting something we did not intend
|
|
94
|
-
* @param
|
|
357
|
+
* @param checkpointsToUnwind - The number of checkpoints we are to unwind
|
|
95
358
|
* @returns True if the operation is successful
|
|
96
359
|
*/
|
|
97
|
-
async
|
|
360
|
+
async unwindCheckpoints(from: CheckpointNumber, checkpointsToUnwind: number) {
|
|
98
361
|
return await this.db.transactionAsync(async () => {
|
|
99
|
-
const last = await this.
|
|
362
|
+
const last = await this.getLatestCheckpointNumber();
|
|
100
363
|
if (from !== last) {
|
|
101
|
-
throw new Error(`Can only unwind
|
|
364
|
+
throw new Error(`Can only unwind checkpoints from the tip (requested ${from} but current tip is ${last})`);
|
|
102
365
|
}
|
|
103
366
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
367
|
+
const proven = await this.getProvenCheckpointNumber();
|
|
368
|
+
if (from - checkpointsToUnwind < proven) {
|
|
369
|
+
await this.setProvenCheckpointNumber(CheckpointNumber(from - checkpointsToUnwind));
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
for (let i = 0; i < checkpointsToUnwind; i++) {
|
|
373
|
+
const checkpointNumber = from - i;
|
|
374
|
+
const checkpoint = await this.#checkpoints.getAsync(checkpointNumber);
|
|
107
375
|
|
|
108
|
-
if (
|
|
109
|
-
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`);
|
|
110
378
|
continue;
|
|
111
379
|
}
|
|
112
|
-
await this.#
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
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
|
+
}
|
|
117
401
|
}
|
|
118
402
|
|
|
119
403
|
return true;
|
|
120
404
|
});
|
|
121
405
|
}
|
|
122
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
|
+
/**
|
|
507
|
+
* Gets up to `limit` amount of Checkpointed L2 blocks starting from `from`.
|
|
508
|
+
* @param start - Number of the first block to return (inclusive).
|
|
509
|
+
* @param limit - The number of blocks to return.
|
|
510
|
+
* @returns The requested L2 blocks
|
|
511
|
+
*/
|
|
512
|
+
async *getCheckpointedBlocks(start: BlockNumber, limit: number): AsyncIterableIterator<CheckpointedL2Block> {
|
|
513
|
+
const checkpointCache = new Map<CheckpointNumber, CheckpointStorage>();
|
|
514
|
+
for await (const [blockNumber, blockStorage] of this.getBlockStorages(start, limit)) {
|
|
515
|
+
const block = await this.getBlockFromBlockStorage(blockNumber, blockStorage);
|
|
516
|
+
if (block) {
|
|
517
|
+
const checkpoint =
|
|
518
|
+
checkpointCache.get(CheckpointNumber(blockStorage.checkpointNumber)) ??
|
|
519
|
+
(await this.#checkpoints.getAsync(blockStorage.checkpointNumber));
|
|
520
|
+
if (checkpoint) {
|
|
521
|
+
checkpointCache.set(CheckpointNumber(blockStorage.checkpointNumber), checkpoint);
|
|
522
|
+
const checkpointedBlock = new CheckpointedL2Block(
|
|
523
|
+
CheckpointNumber(checkpoint.checkpointNumber),
|
|
524
|
+
block,
|
|
525
|
+
L1PublishedData.fromBuffer(checkpoint.l1),
|
|
526
|
+
checkpoint.attestations.map(buf => CommitteeAttestation.fromBuffer(buf)),
|
|
527
|
+
);
|
|
528
|
+
yield checkpointedBlock;
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
async getCheckpointedBlockByHash(blockHash: Fr): Promise<CheckpointedL2Block | undefined> {
|
|
535
|
+
const blockNumber = await this.#blockHashIndex.getAsync(blockHash.toString());
|
|
536
|
+
if (blockNumber === undefined) {
|
|
537
|
+
return undefined;
|
|
538
|
+
}
|
|
539
|
+
return this.getCheckpointedBlock(BlockNumber(blockNumber));
|
|
540
|
+
}
|
|
541
|
+
async getCheckpointedBlockByArchive(archive: Fr): Promise<CheckpointedL2Block | undefined> {
|
|
542
|
+
const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
|
|
543
|
+
if (blockNumber === undefined) {
|
|
544
|
+
return undefined;
|
|
545
|
+
}
|
|
546
|
+
return this.getCheckpointedBlock(BlockNumber(blockNumber));
|
|
547
|
+
}
|
|
548
|
+
|
|
123
549
|
/**
|
|
124
550
|
* Gets up to `limit` amount of L2 blocks starting from `from`.
|
|
125
551
|
* @param start - Number of the first block to return (inclusive).
|
|
126
552
|
* @param limit - The number of blocks to return.
|
|
127
553
|
* @returns The requested L2 blocks
|
|
128
554
|
*/
|
|
129
|
-
async *getBlocks(start:
|
|
130
|
-
for await (const blockStorage of this
|
|
131
|
-
const block = await this.getBlockFromBlockStorage(blockStorage);
|
|
132
|
-
|
|
555
|
+
async *getBlocks(start: BlockNumber, limit: number): AsyncIterableIterator<L2BlockNew> {
|
|
556
|
+
for await (const [blockNumber, blockStorage] of this.getBlockStorages(start, limit)) {
|
|
557
|
+
const block = await this.getBlockFromBlockStorage(blockNumber, blockStorage);
|
|
558
|
+
if (block) {
|
|
559
|
+
yield block;
|
|
560
|
+
}
|
|
133
561
|
}
|
|
134
562
|
}
|
|
135
563
|
|
|
@@ -138,13 +566,72 @@ export class BlockStore {
|
|
|
138
566
|
* @param blockNumber - The number of the block to return.
|
|
139
567
|
* @returns The requested L2 block.
|
|
140
568
|
*/
|
|
141
|
-
async getBlock(blockNumber:
|
|
569
|
+
async getBlock(blockNumber: BlockNumber): Promise<L2BlockNew | undefined> {
|
|
142
570
|
const blockStorage = await this.#blocks.getAsync(blockNumber);
|
|
143
571
|
if (!blockStorage || !blockStorage.header) {
|
|
144
572
|
return Promise.resolve(undefined);
|
|
145
573
|
}
|
|
574
|
+
return this.getBlockFromBlockStorage(blockNumber, blockStorage);
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
/**
|
|
578
|
+
* Gets an L2 block by its hash.
|
|
579
|
+
* @param blockHash - The hash of the block to return.
|
|
580
|
+
* @returns The requested L2 block.
|
|
581
|
+
*/
|
|
582
|
+
async getBlockByHash(blockHash: L2BlockHash): Promise<L2BlockNew | undefined> {
|
|
583
|
+
const blockNumber = await this.#blockHashIndex.getAsync(blockHash.toString());
|
|
584
|
+
if (blockNumber === undefined) {
|
|
585
|
+
return undefined;
|
|
586
|
+
}
|
|
587
|
+
return this.getBlock(BlockNumber(blockNumber));
|
|
588
|
+
}
|
|
146
589
|
|
|
147
|
-
|
|
590
|
+
/**
|
|
591
|
+
* Gets an L2 block by its archive root.
|
|
592
|
+
* @param archive - The archive root of the block to return.
|
|
593
|
+
* @returns The requested L2 block.
|
|
594
|
+
*/
|
|
595
|
+
async getBlockByArchive(archive: Fr): Promise<L2BlockNew | undefined> {
|
|
596
|
+
const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
|
|
597
|
+
if (blockNumber === undefined) {
|
|
598
|
+
return undefined;
|
|
599
|
+
}
|
|
600
|
+
return this.getBlock(BlockNumber(blockNumber));
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
/**
|
|
604
|
+
* Gets a block header by its hash.
|
|
605
|
+
* @param blockHash - The hash of the block to return.
|
|
606
|
+
* @returns The requested block header.
|
|
607
|
+
*/
|
|
608
|
+
async getBlockHeaderByHash(blockHash: L2BlockHash): Promise<BlockHeader | undefined> {
|
|
609
|
+
const blockNumber = await this.#blockHashIndex.getAsync(blockHash.toString());
|
|
610
|
+
if (blockNumber === undefined) {
|
|
611
|
+
return undefined;
|
|
612
|
+
}
|
|
613
|
+
const blockStorage = await this.#blocks.getAsync(blockNumber);
|
|
614
|
+
if (!blockStorage || !blockStorage.header) {
|
|
615
|
+
return undefined;
|
|
616
|
+
}
|
|
617
|
+
return BlockHeader.fromBuffer(blockStorage.header);
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
/**
|
|
621
|
+
* Gets a block header by its archive root.
|
|
622
|
+
* @param archive - The archive root of the block to return.
|
|
623
|
+
* @returns The requested block header.
|
|
624
|
+
*/
|
|
625
|
+
async getBlockHeaderByArchive(archive: Fr): Promise<BlockHeader | undefined> {
|
|
626
|
+
const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
|
|
627
|
+
if (blockNumber === undefined) {
|
|
628
|
+
return undefined;
|
|
629
|
+
}
|
|
630
|
+
const blockStorage = await this.#blocks.getAsync(blockNumber);
|
|
631
|
+
if (!blockStorage || !blockStorage.header) {
|
|
632
|
+
return undefined;
|
|
633
|
+
}
|
|
634
|
+
return BlockHeader.fromBuffer(blockStorage.header);
|
|
148
635
|
}
|
|
149
636
|
|
|
150
637
|
/**
|
|
@@ -153,49 +640,86 @@ export class BlockStore {
|
|
|
153
640
|
* @param limit - The number of blocks to return.
|
|
154
641
|
* @returns The requested L2 block headers
|
|
155
642
|
*/
|
|
156
|
-
async *getBlockHeaders(start:
|
|
157
|
-
for await (const blockStorage of this
|
|
158
|
-
|
|
643
|
+
async *getBlockHeaders(start: BlockNumber, limit: number): AsyncIterableIterator<BlockHeader> {
|
|
644
|
+
for await (const [blockNumber, blockStorage] of this.getBlockStorages(start, limit)) {
|
|
645
|
+
const header = BlockHeader.fromBuffer(blockStorage.header);
|
|
646
|
+
if (header.getBlockNumber() !== blockNumber) {
|
|
647
|
+
throw new Error(
|
|
648
|
+
`Block number mismatch when retrieving block header from archive (expected ${blockNumber} but got ${header.getBlockNumber()})`,
|
|
649
|
+
);
|
|
650
|
+
}
|
|
651
|
+
yield header;
|
|
159
652
|
}
|
|
160
653
|
}
|
|
161
654
|
|
|
162
|
-
private async
|
|
655
|
+
private async *getBlockStorages(start: BlockNumber, limit: number) {
|
|
656
|
+
let expectedBlockNumber = start;
|
|
657
|
+
for await (const [blockNumber, blockStorage] of this.#blocks.entriesAsync(this.#computeBlockRange(start, limit))) {
|
|
658
|
+
if (blockNumber !== expectedBlockNumber) {
|
|
659
|
+
throw new Error(
|
|
660
|
+
`Block number mismatch when iterating blocks from archive (expected ${expectedBlockNumber} but got ${blockNumber})`,
|
|
661
|
+
);
|
|
662
|
+
}
|
|
663
|
+
expectedBlockNumber++;
|
|
664
|
+
yield [blockNumber, blockStorage] as const;
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
private async getBlockFromBlockStorage(
|
|
669
|
+
blockNumber: number,
|
|
670
|
+
blockStorage: BlockStorage,
|
|
671
|
+
): Promise<L2BlockNew | undefined> {
|
|
163
672
|
const header = BlockHeader.fromBuffer(blockStorage.header);
|
|
164
673
|
const archive = AppendOnlyTreeSnapshot.fromBuffer(blockStorage.archive);
|
|
165
|
-
const blockHash =
|
|
166
|
-
const
|
|
167
|
-
|
|
674
|
+
const blockHash = blockStorage.blockHash;
|
|
675
|
+
const blockHashString = bufferToHex(blockHash);
|
|
676
|
+
const blockTxsBuffer = await this.#blockTxs.getAsync(blockHashString);
|
|
677
|
+
if (blockTxsBuffer === undefined) {
|
|
678
|
+
this.#log.warn(`Could not find body for block ${header.globalVariables.blockNumber} ${blockHash}`);
|
|
679
|
+
return undefined;
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
const txEffects: TxEffect[] = [];
|
|
683
|
+
const reader = BufferReader.asReader(blockTxsBuffer);
|
|
684
|
+
while (!reader.isEmpty()) {
|
|
685
|
+
const txHash = reader.readObject(TxHash);
|
|
686
|
+
const txEffect = await this.#txEffects.getAsync(txHash.toString());
|
|
687
|
+
if (txEffect === undefined) {
|
|
688
|
+
this.#log.warn(`Could not find tx effect for tx ${txHash} in block ${blockNumber}`);
|
|
689
|
+
return undefined;
|
|
690
|
+
}
|
|
691
|
+
txEffects.push(deserializeIndexedTxEffect(txEffect).data);
|
|
692
|
+
}
|
|
693
|
+
const body = new Body(txEffects);
|
|
694
|
+
const block = new L2BlockNew(
|
|
695
|
+
archive,
|
|
696
|
+
header,
|
|
697
|
+
body,
|
|
698
|
+
CheckpointNumber(blockStorage.checkpointNumber!),
|
|
699
|
+
blockStorage.indexWithinCheckpoint,
|
|
700
|
+
);
|
|
701
|
+
|
|
702
|
+
if (block.number !== blockNumber) {
|
|
168
703
|
throw new Error(
|
|
169
|
-
`
|
|
704
|
+
`Block number mismatch when retrieving block from archive (expected ${blockNumber} but got ${
|
|
705
|
+
block.number
|
|
706
|
+
} with hash ${blockHashString})`,
|
|
170
707
|
);
|
|
171
708
|
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
const l2Block = new L2Block(archive, header, body);
|
|
175
|
-
return { data: l2Block, l1: blockStorage.l1 };
|
|
709
|
+
return block;
|
|
176
710
|
}
|
|
177
711
|
|
|
178
712
|
/**
|
|
179
713
|
* Gets a tx effect.
|
|
180
|
-
* @param txHash - The
|
|
181
|
-
* @returns The requested tx effect (or undefined if not found).
|
|
714
|
+
* @param txHash - The hash of the tx corresponding to the tx effect.
|
|
715
|
+
* @returns The requested tx effect with block info (or undefined if not found).
|
|
182
716
|
*/
|
|
183
|
-
async getTxEffect(txHash: TxHash): Promise<
|
|
184
|
-
const
|
|
185
|
-
if (
|
|
717
|
+
async getTxEffect(txHash: TxHash): Promise<IndexedTxEffect | undefined> {
|
|
718
|
+
const buffer = await this.#txEffects.getAsync(txHash.toString());
|
|
719
|
+
if (!buffer) {
|
|
186
720
|
return undefined;
|
|
187
721
|
}
|
|
188
|
-
|
|
189
|
-
const block = await this.getBlock(blockNumber);
|
|
190
|
-
if (!block) {
|
|
191
|
-
return undefined;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
return {
|
|
195
|
-
data: block.data.body.txEffects[txIndex],
|
|
196
|
-
l2BlockNumber: block.data.number,
|
|
197
|
-
l2BlockHash: (await block.data.hash()).toString(),
|
|
198
|
-
};
|
|
722
|
+
return deserializeIndexedTxEffect(buffer);
|
|
199
723
|
}
|
|
200
724
|
|
|
201
725
|
/**
|
|
@@ -204,21 +728,18 @@ export class BlockStore {
|
|
|
204
728
|
* @returns The requested tx receipt (or undefined if not found).
|
|
205
729
|
*/
|
|
206
730
|
async getSettledTxReceipt(txHash: TxHash): Promise<TxReceipt | undefined> {
|
|
207
|
-
const
|
|
208
|
-
if (
|
|
731
|
+
const txEffect = await this.getTxEffect(txHash);
|
|
732
|
+
if (!txEffect) {
|
|
209
733
|
return undefined;
|
|
210
734
|
}
|
|
211
735
|
|
|
212
|
-
const block = (await this.getBlock(blockNumber))!;
|
|
213
|
-
const tx = block.data.body.txEffects[txIndex];
|
|
214
|
-
|
|
215
736
|
return new TxReceipt(
|
|
216
737
|
txHash,
|
|
217
|
-
TxReceipt.statusFromRevertCode(
|
|
738
|
+
TxReceipt.statusFromRevertCode(txEffect.data.revertCode),
|
|
218
739
|
'',
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
740
|
+
txEffect.data.transactionFee.toBigInt(),
|
|
741
|
+
txEffect.l2BlockHash,
|
|
742
|
+
BlockNumber(txEffect.l2BlockNumber),
|
|
222
743
|
);
|
|
223
744
|
}
|
|
224
745
|
|
|
@@ -227,8 +748,13 @@ export class BlockStore {
|
|
|
227
748
|
* @param txHash - The txHash of the tx.
|
|
228
749
|
* @returns The block number and index of the tx.
|
|
229
750
|
*/
|
|
230
|
-
getTxLocation(txHash: TxHash): Promise<[blockNumber: number, txIndex: number] | undefined> {
|
|
231
|
-
|
|
751
|
+
public async getTxLocation(txHash: TxHash): Promise<[blockNumber: number, txIndex: number] | undefined> {
|
|
752
|
+
const txEffect = await this.#txEffects.getAsync(txHash.toString());
|
|
753
|
+
if (!txEffect) {
|
|
754
|
+
return undefined;
|
|
755
|
+
}
|
|
756
|
+
const { l2BlockNumber, txIndexInBlock } = deserializeIndexedTxEffect(txEffect);
|
|
757
|
+
return [l2BlockNumber, txIndexInBlock];
|
|
232
758
|
}
|
|
233
759
|
|
|
234
760
|
/**
|
|
@@ -241,12 +767,21 @@ export class BlockStore {
|
|
|
241
767
|
}
|
|
242
768
|
|
|
243
769
|
/**
|
|
244
|
-
* Gets the number of the latest L2 block
|
|
245
|
-
* @returns The number of the latest L2 block
|
|
770
|
+
* Gets the number of the latest L2 block checkpointed.
|
|
771
|
+
* @returns The number of the latest L2 block checkpointed.
|
|
246
772
|
*/
|
|
247
|
-
async
|
|
773
|
+
async getCheckpointedL2BlockNumber(): Promise<BlockNumber> {
|
|
774
|
+
const latestCheckpointNumber = await this.getLatestCheckpointNumber();
|
|
775
|
+
const checkpoint = await this.getCheckpointData(latestCheckpointNumber);
|
|
776
|
+
if (!checkpoint) {
|
|
777
|
+
return BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
778
|
+
}
|
|
779
|
+
return BlockNumber(checkpoint.startBlock + checkpoint.numBlocks - 1);
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
async getLatestL2BlockNumber(): Promise<BlockNumber> {
|
|
248
783
|
const [lastBlockNumber] = await toArray(this.#blocks.keysAsync({ reverse: true, limit: 1 }));
|
|
249
|
-
return typeof lastBlockNumber === 'number' ? lastBlockNumber : INITIAL_L2_BLOCK_NUM - 1;
|
|
784
|
+
return typeof lastBlockNumber === 'number' ? BlockNumber(lastBlockNumber) : BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
250
785
|
}
|
|
251
786
|
|
|
252
787
|
/**
|
|
@@ -261,15 +796,22 @@ export class BlockStore {
|
|
|
261
796
|
return this.#lastSynchedL1Block.set(l1BlockNumber);
|
|
262
797
|
}
|
|
263
798
|
|
|
264
|
-
async
|
|
265
|
-
|
|
799
|
+
async getProvenCheckpointNumber(): Promise<CheckpointNumber> {
|
|
800
|
+
const [latestCheckpointNumber, provenCheckpointNumber] = await Promise.all([
|
|
801
|
+
this.getLatestCheckpointNumber(),
|
|
802
|
+
this.#lastProvenCheckpoint.getAsync(),
|
|
803
|
+
]);
|
|
804
|
+
return (provenCheckpointNumber ?? 0) > latestCheckpointNumber
|
|
805
|
+
? latestCheckpointNumber
|
|
806
|
+
: CheckpointNumber(provenCheckpointNumber ?? 0);
|
|
266
807
|
}
|
|
267
808
|
|
|
268
|
-
|
|
269
|
-
|
|
809
|
+
async setProvenCheckpointNumber(checkpointNumber: CheckpointNumber) {
|
|
810
|
+
const result = await this.#lastProvenCheckpoint.set(checkpointNumber);
|
|
811
|
+
return result;
|
|
270
812
|
}
|
|
271
813
|
|
|
272
|
-
#computeBlockRange(start:
|
|
814
|
+
#computeBlockRange(start: BlockNumber, limit: number): Required<Pick<Range<number>, 'start' | 'limit'>> {
|
|
273
815
|
if (limit < 1) {
|
|
274
816
|
throw new Error(`Invalid limit: ${limit}`);
|
|
275
817
|
}
|
|
@@ -280,4 +822,29 @@ export class BlockStore {
|
|
|
280
822
|
|
|
281
823
|
return { start, limit };
|
|
282
824
|
}
|
|
825
|
+
|
|
826
|
+
/**
|
|
827
|
+
* Gets the pending chain validation status.
|
|
828
|
+
* @returns The validation status or undefined if not set.
|
|
829
|
+
*/
|
|
830
|
+
async getPendingChainValidationStatus(): Promise<ValidateCheckpointResult | undefined> {
|
|
831
|
+
const buffer = await this.#pendingChainValidationStatus.getAsync();
|
|
832
|
+
if (!buffer) {
|
|
833
|
+
return undefined;
|
|
834
|
+
}
|
|
835
|
+
return deserializeValidateCheckpointResult(buffer);
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
/**
|
|
839
|
+
* Sets the pending chain validation status.
|
|
840
|
+
* @param status - The validation status to store.
|
|
841
|
+
*/
|
|
842
|
+
async setPendingChainValidationStatus(status: ValidateCheckpointResult | undefined): Promise<void> {
|
|
843
|
+
if (status) {
|
|
844
|
+
const buffer = serializeValidateCheckpointResult(status);
|
|
845
|
+
await this.#pendingChainValidationStatus.set(buffer);
|
|
846
|
+
} else {
|
|
847
|
+
await this.#pendingChainValidationStatus.delete();
|
|
848
|
+
}
|
|
849
|
+
}
|
|
283
850
|
}
|