@aztec/archiver 0.0.1-commit.fce3e4f → 0.0.1-commit.fffb133c
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 +156 -22
- package/dest/archiver.d.ts +135 -0
- package/dest/archiver.d.ts.map +1 -0
- package/dest/archiver.js +768 -0
- package/dest/config.d.ts +30 -0
- package/dest/config.d.ts.map +1 -0
- package/dest/{archiver/config.js → config.js} +21 -5
- package/dest/errors.d.ts +41 -0
- package/dest/errors.d.ts.map +1 -0
- package/dest/errors.js +62 -0
- package/dest/factory.d.ts +7 -7
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +90 -10
- package/dest/index.d.ts +10 -4
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +8 -3
- package/dest/interfaces.d.ts +9 -0
- package/dest/interfaces.d.ts.map +1 -0
- package/dest/interfaces.js +3 -0
- package/dest/l1/bin/retrieve-calldata.d.ts +3 -0
- package/dest/l1/bin/retrieve-calldata.d.ts.map +1 -0
- package/dest/l1/bin/retrieve-calldata.js +149 -0
- package/dest/l1/calldata_retriever.d.ts +112 -0
- package/dest/l1/calldata_retriever.d.ts.map +1 -0
- package/dest/l1/calldata_retriever.js +471 -0
- package/dest/l1/data_retrieval.d.ts +88 -0
- package/dest/l1/data_retrieval.d.ts.map +1 -0
- package/dest/{archiver → l1}/data_retrieval.js +76 -150
- package/dest/l1/debug_tx.d.ts +19 -0
- package/dest/l1/debug_tx.d.ts.map +1 -0
- package/dest/l1/debug_tx.js +73 -0
- package/dest/l1/spire_proposer.d.ts +70 -0
- package/dest/l1/spire_proposer.d.ts.map +1 -0
- package/dest/l1/spire_proposer.js +157 -0
- package/dest/l1/trace_tx.d.ts +97 -0
- package/dest/l1/trace_tx.d.ts.map +1 -0
- package/dest/l1/trace_tx.js +91 -0
- package/dest/l1/types.d.ts +12 -0
- package/dest/l1/types.d.ts.map +1 -0
- package/dest/l1/types.js +3 -0
- package/dest/l1/validate_trace.d.ts +29 -0
- package/dest/l1/validate_trace.d.ts.map +1 -0
- package/dest/l1/validate_trace.js +150 -0
- package/dest/modules/data_source_base.d.ts +84 -0
- package/dest/modules/data_source_base.d.ts.map +1 -0
- package/dest/modules/data_source_base.js +260 -0
- package/dest/modules/data_store_updater.d.ts +73 -0
- package/dest/modules/data_store_updater.d.ts.map +1 -0
- package/dest/modules/data_store_updater.js +302 -0
- package/dest/modules/instrumentation.d.ts +37 -0
- package/dest/modules/instrumentation.d.ts.map +1 -0
- package/dest/{archiver → modules}/instrumentation.js +22 -59
- package/dest/modules/l1_synchronizer.d.ts +75 -0
- package/dest/modules/l1_synchronizer.d.ts.map +1 -0
- package/dest/modules/l1_synchronizer.js +1113 -0
- package/dest/modules/validation.d.ts +17 -0
- package/dest/modules/validation.d.ts.map +1 -0
- package/dest/{archiver → modules}/validation.js +7 -1
- package/dest/store/block_store.d.ts +192 -0
- package/dest/store/block_store.d.ts.map +1 -0
- package/dest/store/block_store.js +721 -0
- package/dest/store/contract_class_store.d.ts +18 -0
- package/dest/store/contract_class_store.d.ts.map +1 -0
- package/dest/{archiver/kv_archiver_store → store}/contract_class_store.js +2 -2
- package/dest/store/contract_instance_store.d.ts +24 -0
- package/dest/store/contract_instance_store.d.ts.map +1 -0
- package/dest/{archiver/kv_archiver_store → store}/contract_instance_store.js +1 -1
- package/dest/store/kv_archiver_store.d.ts +340 -0
- package/dest/store/kv_archiver_store.d.ts.map +1 -0
- package/dest/store/kv_archiver_store.js +447 -0
- package/dest/store/log_store.d.ts +54 -0
- package/dest/store/log_store.d.ts.map +1 -0
- package/dest/store/log_store.js +436 -0
- package/dest/store/message_store.d.ts +40 -0
- package/dest/store/message_store.d.ts.map +1 -0
- package/dest/{archiver/kv_archiver_store → store}/message_store.js +15 -14
- package/dest/{archiver/structs → structs}/data_retrieval.d.ts +1 -1
- package/dest/structs/data_retrieval.d.ts.map +1 -0
- package/dest/structs/inbox_message.d.ts +15 -0
- package/dest/structs/inbox_message.d.ts.map +1 -0
- package/dest/{archiver/structs → structs}/inbox_message.js +6 -5
- package/dest/structs/published.d.ts +2 -0
- package/dest/structs/published.d.ts.map +1 -0
- package/dest/test/fake_l1_state.d.ts +190 -0
- package/dest/test/fake_l1_state.d.ts.map +1 -0
- package/dest/test/fake_l1_state.js +383 -0
- package/dest/test/index.d.ts +2 -1
- package/dest/test/index.d.ts.map +1 -1
- package/dest/test/index.js +1 -0
- 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 +18 -14
- package/dest/test/mock_l1_to_l2_message_source.d.ts +7 -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 +21 -11
- package/dest/test/mock_l2_block_source.d.ts +34 -16
- package/dest/test/mock_l2_block_source.d.ts.map +1 -1
- package/dest/test/mock_l2_block_source.js +179 -59
- package/dest/test/mock_structs.d.ts +78 -3
- package/dest/test/mock_structs.d.ts.map +1 -1
- package/dest/test/mock_structs.js +141 -9
- package/package.json +19 -20
- package/src/archiver.ts +523 -0
- package/src/{archiver/config.ts → config.ts} +28 -12
- package/src/errors.ts +102 -0
- package/src/factory.ts +125 -10
- package/src/index.ts +10 -3
- package/src/interfaces.ts +9 -0
- package/src/l1/README.md +98 -0
- package/src/l1/bin/retrieve-calldata.ts +187 -0
- package/src/l1/calldata_retriever.ts +641 -0
- package/src/{archiver → l1}/data_retrieval.ts +142 -223
- package/src/l1/debug_tx.ts +99 -0
- package/src/l1/spire_proposer.ts +160 -0
- package/src/l1/trace_tx.ts +128 -0
- package/src/l1/types.ts +13 -0
- package/src/l1/validate_trace.ts +211 -0
- package/src/modules/data_source_base.ts +367 -0
- package/src/modules/data_store_updater.ts +423 -0
- package/src/{archiver → modules}/instrumentation.ts +26 -61
- package/src/modules/l1_synchronizer.ts +931 -0
- package/src/{archiver → modules}/validation.ts +11 -6
- package/src/store/block_store.ts +966 -0
- package/src/{archiver/kv_archiver_store → store}/contract_class_store.ts +2 -2
- package/src/{archiver/kv_archiver_store → store}/contract_instance_store.ts +2 -2
- package/src/store/kv_archiver_store.ts +639 -0
- package/src/store/log_store.ts +575 -0
- package/src/{archiver/kv_archiver_store → store}/message_store.ts +21 -18
- package/src/{archiver/structs → structs}/inbox_message.ts +8 -8
- package/src/{archiver/structs → structs}/published.ts +0 -1
- package/src/test/fake_l1_state.ts +599 -0
- 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/index.ts +1 -0
- package/src/test/mock_archiver.ts +22 -16
- package/src/test/mock_l1_to_l2_message_source.ts +18 -11
- package/src/test/mock_l2_block_source.ts +195 -69
- package/src/test/mock_structs.ts +256 -10
- package/dest/archiver/archiver.d.ts +0 -287
- package/dest/archiver/archiver.d.ts.map +0 -1
- package/dest/archiver/archiver.js +0 -1408
- package/dest/archiver/archiver_store.d.ts +0 -255
- package/dest/archiver/archiver_store.d.ts.map +0 -1
- package/dest/archiver/archiver_store.js +0 -4
- package/dest/archiver/archiver_store_test_suite.d.ts +0 -8
- package/dest/archiver/archiver_store_test_suite.d.ts.map +0 -1
- package/dest/archiver/archiver_store_test_suite.js +0 -1289
- package/dest/archiver/config.d.ts +0 -21
- package/dest/archiver/config.d.ts.map +0 -1
- package/dest/archiver/data_retrieval.d.ts +0 -79
- package/dest/archiver/data_retrieval.d.ts.map +0 -1
- package/dest/archiver/errors.d.ts +0 -12
- package/dest/archiver/errors.d.ts.map +0 -1
- package/dest/archiver/errors.js +0 -17
- package/dest/archiver/index.d.ts +0 -7
- package/dest/archiver/index.d.ts.map +0 -1
- package/dest/archiver/index.js +0 -4
- package/dest/archiver/instrumentation.d.ts +0 -35
- package/dest/archiver/instrumentation.d.ts.map +0 -1
- package/dest/archiver/kv_archiver_store/block_store.d.ts +0 -124
- package/dest/archiver/kv_archiver_store/block_store.d.ts.map +0 -1
- package/dest/archiver/kv_archiver_store/block_store.js +0 -370
- package/dest/archiver/kv_archiver_store/contract_class_store.d.ts +0 -18
- package/dest/archiver/kv_archiver_store/contract_class_store.d.ts.map +0 -1
- package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts +0 -24
- package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +0 -1
- package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +0 -168
- package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +0 -1
- package/dest/archiver/kv_archiver_store/kv_archiver_store.js +0 -296
- package/dest/archiver/kv_archiver_store/log_store.d.ts +0 -49
- package/dest/archiver/kv_archiver_store/log_store.d.ts.map +0 -1
- package/dest/archiver/kv_archiver_store/log_store.js +0 -336
- package/dest/archiver/kv_archiver_store/message_store.d.ts +0 -39
- package/dest/archiver/kv_archiver_store/message_store.d.ts.map +0 -1
- package/dest/archiver/structs/data_retrieval.d.ts.map +0 -1
- package/dest/archiver/structs/inbox_message.d.ts +0 -15
- package/dest/archiver/structs/inbox_message.d.ts.map +0 -1
- package/dest/archiver/structs/published.d.ts +0 -3
- package/dest/archiver/structs/published.d.ts.map +0 -1
- package/dest/archiver/validation.d.ts +0 -17
- package/dest/archiver/validation.d.ts.map +0 -1
- package/dest/rpc/index.d.ts +0 -9
- package/dest/rpc/index.d.ts.map +0 -1
- package/dest/rpc/index.js +0 -15
- package/src/archiver/archiver.ts +0 -1858
- package/src/archiver/archiver_store.ts +0 -305
- package/src/archiver/archiver_store_test_suite.ts +0 -1264
- package/src/archiver/errors.ts +0 -26
- package/src/archiver/index.ts +0 -6
- package/src/archiver/kv_archiver_store/block_store.ts +0 -481
- package/src/archiver/kv_archiver_store/kv_archiver_store.ts +0 -422
- package/src/archiver/kv_archiver_store/log_store.ts +0 -406
- package/src/rpc/index.ts +0 -16
- /package/dest/{archiver/structs → structs}/data_retrieval.js +0 -0
- /package/dest/{archiver/structs → structs}/published.js +0 -0
- /package/src/{archiver/structs → structs}/data_retrieval.ts +0 -0
|
@@ -0,0 +1,966 @@
|
|
|
1
|
+
import { INITIAL_CHECKPOINT_NUMBER, INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
|
|
2
|
+
import { BlockNumber, CheckpointNumber, IndexWithinCheckpoint, SlotNumber } from '@aztec/foundation/branded-types';
|
|
3
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
4
|
+
import { toArray } from '@aztec/foundation/iterable';
|
|
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';
|
|
9
|
+
import type { AztecAsyncKVStore, AztecAsyncMap, AztecAsyncSingleton, Range } from '@aztec/kv-store';
|
|
10
|
+
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
11
|
+
import {
|
|
12
|
+
Body,
|
|
13
|
+
CheckpointedL2Block,
|
|
14
|
+
CommitteeAttestation,
|
|
15
|
+
L2Block,
|
|
16
|
+
L2BlockHash,
|
|
17
|
+
type ValidateCheckpointResult,
|
|
18
|
+
deserializeValidateCheckpointResult,
|
|
19
|
+
serializeValidateCheckpointResult,
|
|
20
|
+
} from '@aztec/stdlib/block';
|
|
21
|
+
import { L1PublishedData, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
|
|
22
|
+
import type { L1RollupConstants } from '@aztec/stdlib/epoch-helpers';
|
|
23
|
+
import { CheckpointHeader } from '@aztec/stdlib/rollup';
|
|
24
|
+
import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
|
|
25
|
+
import {
|
|
26
|
+
BlockHeader,
|
|
27
|
+
type IndexedTxEffect,
|
|
28
|
+
TxEffect,
|
|
29
|
+
TxHash,
|
|
30
|
+
TxReceipt,
|
|
31
|
+
TxStatus,
|
|
32
|
+
deserializeIndexedTxEffect,
|
|
33
|
+
serializeIndexedTxEffect,
|
|
34
|
+
} from '@aztec/stdlib/tx';
|
|
35
|
+
|
|
36
|
+
import {
|
|
37
|
+
BlockArchiveNotConsistentError,
|
|
38
|
+
BlockIndexNotSequentialError,
|
|
39
|
+
BlockNotFoundError,
|
|
40
|
+
BlockNumberNotSequentialError,
|
|
41
|
+
CannotOverwriteCheckpointedBlockError,
|
|
42
|
+
CheckpointNotFoundError,
|
|
43
|
+
CheckpointNumberNotConsistentError,
|
|
44
|
+
CheckpointNumberNotSequentialError,
|
|
45
|
+
InitialBlockNumberNotSequentialError,
|
|
46
|
+
InitialCheckpointNumberNotSequentialError,
|
|
47
|
+
} from '../errors.js';
|
|
48
|
+
|
|
49
|
+
export { TxReceipt, type TxEffect, type TxHash } from '@aztec/stdlib/tx';
|
|
50
|
+
|
|
51
|
+
type BlockIndexValue = [blockNumber: number, index: number];
|
|
52
|
+
|
|
53
|
+
type BlockStorage = {
|
|
54
|
+
header: Buffer;
|
|
55
|
+
blockHash: Buffer;
|
|
56
|
+
archive: Buffer;
|
|
57
|
+
checkpointNumber: number;
|
|
58
|
+
indexWithinCheckpoint: number;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
type CheckpointStorage = {
|
|
62
|
+
header: Buffer;
|
|
63
|
+
archive: Buffer;
|
|
64
|
+
checkpointNumber: number;
|
|
65
|
+
startBlock: number;
|
|
66
|
+
numBlocks: number;
|
|
67
|
+
l1: Buffer;
|
|
68
|
+
attestations: Buffer[];
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export type CheckpointData = {
|
|
72
|
+
checkpointNumber: CheckpointNumber;
|
|
73
|
+
header: CheckpointHeader;
|
|
74
|
+
archive: AppendOnlyTreeSnapshot;
|
|
75
|
+
startBlock: number;
|
|
76
|
+
numBlocks: number;
|
|
77
|
+
l1: L1PublishedData;
|
|
78
|
+
attestations: Buffer[];
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
export type RemoveCheckpointsResult = { blocksRemoved: L2Block[] | undefined };
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* LMDB-based block storage for the archiver.
|
|
85
|
+
*/
|
|
86
|
+
export class BlockStore {
|
|
87
|
+
/** Map block number to block data */
|
|
88
|
+
#blocks: AztecAsyncMap<number, BlockStorage>;
|
|
89
|
+
|
|
90
|
+
/** Map checkpoint number to checkpoint data */
|
|
91
|
+
#checkpoints: AztecAsyncMap<number, CheckpointStorage>;
|
|
92
|
+
|
|
93
|
+
/** Map block hash to list of tx hashes */
|
|
94
|
+
#blockTxs: AztecAsyncMap<string, Buffer>;
|
|
95
|
+
|
|
96
|
+
/** Tx hash to serialized IndexedTxEffect */
|
|
97
|
+
#txEffects: AztecAsyncMap<string, Buffer>;
|
|
98
|
+
|
|
99
|
+
/** Stores L1 block number in which the last processed L2 block was included */
|
|
100
|
+
#lastSynchedL1Block: AztecAsyncSingleton<bigint>;
|
|
101
|
+
|
|
102
|
+
/** Stores last proven checkpoint */
|
|
103
|
+
#lastProvenCheckpoint: AztecAsyncSingleton<number>;
|
|
104
|
+
|
|
105
|
+
/** Stores the pending chain validation status */
|
|
106
|
+
#pendingChainValidationStatus: AztecAsyncSingleton<Buffer>;
|
|
107
|
+
|
|
108
|
+
/** Index mapping a contract's address (as a string) to its location in a block */
|
|
109
|
+
#contractIndex: AztecAsyncMap<string, BlockIndexValue>;
|
|
110
|
+
|
|
111
|
+
/** Index mapping block hash to block number */
|
|
112
|
+
#blockHashIndex: AztecAsyncMap<string, number>;
|
|
113
|
+
|
|
114
|
+
/** Index mapping block archive to block number */
|
|
115
|
+
#blockArchiveIndex: AztecAsyncMap<string, number>;
|
|
116
|
+
|
|
117
|
+
#log = createLogger('archiver:block_store');
|
|
118
|
+
|
|
119
|
+
constructor(
|
|
120
|
+
private db: AztecAsyncKVStore,
|
|
121
|
+
private l1Constants: Pick<L1RollupConstants, 'epochDuration'>,
|
|
122
|
+
) {
|
|
123
|
+
this.#blocks = db.openMap('archiver_blocks');
|
|
124
|
+
this.#blockTxs = db.openMap('archiver_block_txs');
|
|
125
|
+
this.#txEffects = db.openMap('archiver_tx_effects');
|
|
126
|
+
this.#contractIndex = db.openMap('archiver_contract_index');
|
|
127
|
+
this.#blockHashIndex = db.openMap('archiver_block_hash_index');
|
|
128
|
+
this.#blockArchiveIndex = db.openMap('archiver_block_archive_index');
|
|
129
|
+
this.#lastSynchedL1Block = db.openSingleton('archiver_last_synched_l1_block');
|
|
130
|
+
this.#lastProvenCheckpoint = db.openSingleton('archiver_last_proven_l2_checkpoint');
|
|
131
|
+
this.#pendingChainValidationStatus = db.openSingleton('archiver_pending_chain_validation_status');
|
|
132
|
+
this.#checkpoints = db.openMap('archiver_checkpoints');
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Computes the finalized block number based on the proven block number.
|
|
137
|
+
* A block is considered finalized when it's 2 epochs behind the proven block.
|
|
138
|
+
* TODO(#13569): Compute proper finalized block number based on L1 finalized block.
|
|
139
|
+
* TODO(palla/mbps): Even the provisional computation is wrong, since it should subtract checkpoints, not blocks
|
|
140
|
+
* @returns The finalized block number.
|
|
141
|
+
*/
|
|
142
|
+
async getFinalizedL2BlockNumber(): Promise<BlockNumber> {
|
|
143
|
+
const provenBlockNumber = await this.getProvenBlockNumber();
|
|
144
|
+
return BlockNumber(Math.max(provenBlockNumber - this.l1Constants.epochDuration * 2, 0));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Append new proposed blocks to the store's list. All blocks must be for the 'current' checkpoint.
|
|
149
|
+
* These are uncheckpointed blocks that have been proposed by the sequencer but not yet included in a checkpoint on L1.
|
|
150
|
+
* For checkpointed blocks (already published to L1), use addCheckpoints() instead.
|
|
151
|
+
* @param blocks - The proposed L2 blocks to be added to the store.
|
|
152
|
+
* @returns True if the operation is successful.
|
|
153
|
+
*/
|
|
154
|
+
async addProposedBlocks(blocks: L2Block[], opts: { force?: boolean } = {}): Promise<boolean> {
|
|
155
|
+
if (blocks.length === 0) {
|
|
156
|
+
return true;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return await this.db.transactionAsync(async () => {
|
|
160
|
+
// Check that the block immediately before the first block to be added is present in the store.
|
|
161
|
+
const firstBlockNumber = blocks[0].number;
|
|
162
|
+
const firstBlockCheckpointNumber = blocks[0].checkpointNumber;
|
|
163
|
+
const firstBlockIndex = blocks[0].indexWithinCheckpoint;
|
|
164
|
+
const firstBlockLastArchive = blocks[0].header.lastArchive.root;
|
|
165
|
+
|
|
166
|
+
// Extract the latest block and checkpoint numbers
|
|
167
|
+
const previousBlockNumber = await this.getLatestBlockNumber();
|
|
168
|
+
const previousCheckpointNumber = await this.getLatestCheckpointNumber();
|
|
169
|
+
|
|
170
|
+
// Verify we're not overwriting checkpointed blocks
|
|
171
|
+
const lastCheckpointedBlockNumber = await this.getCheckpointedL2BlockNumber();
|
|
172
|
+
if (!opts.force && firstBlockNumber <= lastCheckpointedBlockNumber) {
|
|
173
|
+
throw new CannotOverwriteCheckpointedBlockError(firstBlockNumber, lastCheckpointedBlockNumber);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Check that the first block number is the expected one
|
|
177
|
+
if (!opts.force && previousBlockNumber !== firstBlockNumber - 1) {
|
|
178
|
+
throw new InitialBlockNumberNotSequentialError(firstBlockNumber, previousBlockNumber);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// The same check as above but for checkpoints
|
|
182
|
+
if (!opts.force && previousCheckpointNumber !== firstBlockCheckpointNumber - 1) {
|
|
183
|
+
throw new InitialCheckpointNumberNotSequentialError(firstBlockCheckpointNumber, previousCheckpointNumber);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Extract the previous block if there is one and see if it is for the same checkpoint or not
|
|
187
|
+
const previousBlockResult = await this.getBlock(previousBlockNumber);
|
|
188
|
+
|
|
189
|
+
let expectedFirstblockIndex = 0;
|
|
190
|
+
let previousBlockIndex: number | undefined = undefined;
|
|
191
|
+
if (previousBlockResult !== undefined) {
|
|
192
|
+
if (previousBlockResult.checkpointNumber === firstBlockCheckpointNumber) {
|
|
193
|
+
// The previous block is for the same checkpoint, therefore our index should follow it
|
|
194
|
+
previousBlockIndex = previousBlockResult.indexWithinCheckpoint;
|
|
195
|
+
expectedFirstblockIndex = previousBlockIndex + 1;
|
|
196
|
+
}
|
|
197
|
+
if (!previousBlockResult.archive.root.equals(firstBlockLastArchive)) {
|
|
198
|
+
throw new BlockArchiveNotConsistentError(
|
|
199
|
+
firstBlockNumber,
|
|
200
|
+
previousBlockResult.number,
|
|
201
|
+
firstBlockLastArchive,
|
|
202
|
+
previousBlockResult.archive.root,
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// Now check that the first block has the expected index value
|
|
208
|
+
if (!opts.force && expectedFirstblockIndex !== firstBlockIndex) {
|
|
209
|
+
throw new BlockIndexNotSequentialError(firstBlockIndex, previousBlockIndex);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// Iterate over blocks array and insert them, checking that the block numbers and indexes are sequential. Also check they are for the correct checkpoint.
|
|
213
|
+
let previousBlock: L2Block | undefined = undefined;
|
|
214
|
+
for (const block of blocks) {
|
|
215
|
+
if (!opts.force && previousBlock) {
|
|
216
|
+
if (previousBlock.number + 1 !== block.number) {
|
|
217
|
+
throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
|
|
218
|
+
}
|
|
219
|
+
if (previousBlock.indexWithinCheckpoint + 1 !== block.indexWithinCheckpoint) {
|
|
220
|
+
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
|
|
221
|
+
}
|
|
222
|
+
if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
|
|
223
|
+
throw new BlockArchiveNotConsistentError(
|
|
224
|
+
block.number,
|
|
225
|
+
previousBlock.number,
|
|
226
|
+
block.header.lastArchive.root,
|
|
227
|
+
previousBlock.archive.root,
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
if (!opts.force && firstBlockCheckpointNumber !== block.checkpointNumber) {
|
|
232
|
+
throw new CheckpointNumberNotConsistentError(block.checkpointNumber, firstBlockCheckpointNumber);
|
|
233
|
+
}
|
|
234
|
+
previousBlock = block;
|
|
235
|
+
await this.addBlockToDatabase(block, block.checkpointNumber, block.indexWithinCheckpoint);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return true;
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Append new cheskpoints to the store's list.
|
|
244
|
+
* @param checkpoints - The L2 checkpoints to be added to the store.
|
|
245
|
+
* @returns True if the operation is successful.
|
|
246
|
+
*/
|
|
247
|
+
async addCheckpoints(checkpoints: PublishedCheckpoint[], opts: { force?: boolean } = {}): Promise<boolean> {
|
|
248
|
+
if (checkpoints.length === 0) {
|
|
249
|
+
return true;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
return await this.db.transactionAsync(async () => {
|
|
253
|
+
// Check that the checkpoint immediately before the first block to be added is present in the store.
|
|
254
|
+
const firstCheckpointNumber = checkpoints[0].checkpoint.number;
|
|
255
|
+
const previousCheckpointNumber = await this.getLatestCheckpointNumber();
|
|
256
|
+
|
|
257
|
+
if (previousCheckpointNumber !== firstCheckpointNumber - 1 && !opts.force) {
|
|
258
|
+
throw new InitialCheckpointNumberNotSequentialError(firstCheckpointNumber, previousCheckpointNumber);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// Extract the previous checkpoint if there is one
|
|
262
|
+
let previousCheckpointData: CheckpointData | undefined = undefined;
|
|
263
|
+
if (previousCheckpointNumber !== INITIAL_CHECKPOINT_NUMBER - 1) {
|
|
264
|
+
// There should be a previous checkpoint
|
|
265
|
+
previousCheckpointData = await this.getCheckpointData(previousCheckpointNumber);
|
|
266
|
+
if (previousCheckpointData === undefined) {
|
|
267
|
+
throw new CheckpointNotFoundError(previousCheckpointNumber);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
let previousBlockNumber: BlockNumber | undefined = undefined;
|
|
272
|
+
let previousBlock: L2Block | undefined = undefined;
|
|
273
|
+
|
|
274
|
+
// If we have a previous checkpoint then we need to get the previous block number
|
|
275
|
+
if (previousCheckpointData !== undefined) {
|
|
276
|
+
previousBlockNumber = BlockNumber(previousCheckpointData.startBlock + previousCheckpointData.numBlocks - 1);
|
|
277
|
+
previousBlock = await this.getBlock(previousBlockNumber);
|
|
278
|
+
if (previousBlock === undefined) {
|
|
279
|
+
// We should be able to get the required previous block
|
|
280
|
+
throw new BlockNotFoundError(previousBlockNumber);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// Iterate over checkpoints array and insert them, checking that the block numbers are sequential.
|
|
285
|
+
let previousCheckpoint: PublishedCheckpoint | undefined = undefined;
|
|
286
|
+
for (const checkpoint of checkpoints) {
|
|
287
|
+
if (
|
|
288
|
+
!opts.force &&
|
|
289
|
+
previousCheckpoint &&
|
|
290
|
+
previousCheckpoint.checkpoint.number + 1 !== checkpoint.checkpoint.number
|
|
291
|
+
) {
|
|
292
|
+
throw new CheckpointNumberNotSequentialError(
|
|
293
|
+
checkpoint.checkpoint.number,
|
|
294
|
+
previousCheckpoint.checkpoint.number,
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
previousCheckpoint = checkpoint;
|
|
298
|
+
|
|
299
|
+
// Store every block in the database. the block may already exist, but this has come from chain and is assumed to be correct.
|
|
300
|
+
for (let i = 0; i < checkpoint.checkpoint.blocks.length; i++) {
|
|
301
|
+
const block = checkpoint.checkpoint.blocks[i];
|
|
302
|
+
if (previousBlock) {
|
|
303
|
+
// The blocks should have a sequential block number
|
|
304
|
+
if (previousBlock.number !== block.number - 1) {
|
|
305
|
+
throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
|
|
306
|
+
}
|
|
307
|
+
// If the blocks are for the same checkpoint then they should have sequential indexes
|
|
308
|
+
if (
|
|
309
|
+
previousBlock.checkpointNumber === block.checkpointNumber &&
|
|
310
|
+
previousBlock.indexWithinCheckpoint !== block.indexWithinCheckpoint - 1
|
|
311
|
+
) {
|
|
312
|
+
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
|
|
313
|
+
}
|
|
314
|
+
if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
|
|
315
|
+
throw new BlockArchiveNotConsistentError(
|
|
316
|
+
block.number,
|
|
317
|
+
previousBlock.number,
|
|
318
|
+
block.header.lastArchive.root,
|
|
319
|
+
previousBlock.archive.root,
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
} else {
|
|
323
|
+
// No previous block, must be block 1 at checkpoint index 0
|
|
324
|
+
if (block.indexWithinCheckpoint !== 0) {
|
|
325
|
+
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, undefined);
|
|
326
|
+
}
|
|
327
|
+
if (block.number !== INITIAL_L2_BLOCK_NUM) {
|
|
328
|
+
throw new BlockNumberNotSequentialError(block.number, undefined);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
previousBlock = block;
|
|
333
|
+
await this.addBlockToDatabase(block, checkpoint.checkpoint.number, i);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// Store the checkpoint in the database
|
|
337
|
+
await this.#checkpoints.set(checkpoint.checkpoint.number, {
|
|
338
|
+
header: checkpoint.checkpoint.header.toBuffer(),
|
|
339
|
+
archive: checkpoint.checkpoint.archive.toBuffer(),
|
|
340
|
+
l1: checkpoint.l1.toBuffer(),
|
|
341
|
+
attestations: checkpoint.attestations.map(attestation => attestation.toBuffer()),
|
|
342
|
+
checkpointNumber: checkpoint.checkpoint.number,
|
|
343
|
+
startBlock: checkpoint.checkpoint.blocks[0].number,
|
|
344
|
+
numBlocks: checkpoint.checkpoint.blocks.length,
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
await this.#lastSynchedL1Block.set(checkpoints[checkpoints.length - 1].l1.blockNumber);
|
|
349
|
+
return true;
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
private async addBlockToDatabase(block: L2Block, checkpointNumber: number, indexWithinCheckpoint: number) {
|
|
354
|
+
const blockHash = L2BlockHash.fromField(await block.hash());
|
|
355
|
+
|
|
356
|
+
await this.#blocks.set(block.number, {
|
|
357
|
+
header: block.header.toBuffer(),
|
|
358
|
+
blockHash: blockHash.toBuffer(),
|
|
359
|
+
archive: block.archive.toBuffer(),
|
|
360
|
+
checkpointNumber,
|
|
361
|
+
indexWithinCheckpoint,
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
for (let i = 0; i < block.body.txEffects.length; i++) {
|
|
365
|
+
const txEffect: IndexedTxEffect = {
|
|
366
|
+
data: block.body.txEffects[i],
|
|
367
|
+
l2BlockNumber: block.number,
|
|
368
|
+
l2BlockHash: blockHash,
|
|
369
|
+
txIndexInBlock: i,
|
|
370
|
+
};
|
|
371
|
+
await this.#txEffects.set(txEffect.data.txHash.toString(), serializeIndexedTxEffect(txEffect));
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
await this.#blockTxs.set(blockHash.toString(), Buffer.concat(block.body.txEffects.map(tx => tx.txHash.toBuffer())));
|
|
375
|
+
|
|
376
|
+
// Update indices for block hash and archive
|
|
377
|
+
await this.#blockHashIndex.set(blockHash.toString(), block.number);
|
|
378
|
+
await this.#blockArchiveIndex.set(block.archive.root.toString(), block.number);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/** Deletes a block and all associated data (tx effects, indices). */
|
|
382
|
+
private async deleteBlock(block: L2Block): Promise<void> {
|
|
383
|
+
// Delete the block from the main blocks map
|
|
384
|
+
await this.#blocks.delete(block.number);
|
|
385
|
+
|
|
386
|
+
// Delete all tx effects for this block
|
|
387
|
+
await Promise.all(block.body.txEffects.map(tx => this.#txEffects.delete(tx.txHash.toString())));
|
|
388
|
+
|
|
389
|
+
// Delete block txs mapping
|
|
390
|
+
const blockHash = (await block.hash()).toString();
|
|
391
|
+
await this.#blockTxs.delete(blockHash);
|
|
392
|
+
|
|
393
|
+
// Clean up indices
|
|
394
|
+
await this.#blockHashIndex.delete(blockHash);
|
|
395
|
+
await this.#blockArchiveIndex.delete(block.archive.root.toString());
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Removes all checkpoints with checkpoint number > checkpointNumber.
|
|
400
|
+
* Also removes ALL blocks (both checkpointed and uncheckpointed) after the last block of the given checkpoint.
|
|
401
|
+
* @param checkpointNumber - Remove all checkpoints strictly after this one.
|
|
402
|
+
*/
|
|
403
|
+
async removeCheckpointsAfter(checkpointNumber: CheckpointNumber): Promise<RemoveCheckpointsResult> {
|
|
404
|
+
return await this.db.transactionAsync(async () => {
|
|
405
|
+
const latestCheckpointNumber = await this.getLatestCheckpointNumber();
|
|
406
|
+
|
|
407
|
+
if (checkpointNumber >= latestCheckpointNumber) {
|
|
408
|
+
this.#log.warn(`No checkpoints to remove after ${checkpointNumber} (latest is ${latestCheckpointNumber})`);
|
|
409
|
+
return { blocksRemoved: undefined };
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
// If the proven checkpoint is beyond the target, update it
|
|
413
|
+
const proven = await this.getProvenCheckpointNumber();
|
|
414
|
+
if (proven > checkpointNumber) {
|
|
415
|
+
this.#log.warn(`Updating proven checkpoint ${proven} to last valid checkpoint ${checkpointNumber}`);
|
|
416
|
+
await this.setProvenCheckpointNumber(checkpointNumber);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
// Find the last block number to keep (last block of the given checkpoint, or 0 if no checkpoint)
|
|
420
|
+
let lastBlockToKeep: BlockNumber;
|
|
421
|
+
if (checkpointNumber <= 0) {
|
|
422
|
+
lastBlockToKeep = BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
423
|
+
} else {
|
|
424
|
+
const targetCheckpoint = await this.#checkpoints.getAsync(checkpointNumber);
|
|
425
|
+
if (!targetCheckpoint) {
|
|
426
|
+
throw new Error(`Target checkpoint ${checkpointNumber} not found in store`);
|
|
427
|
+
}
|
|
428
|
+
lastBlockToKeep = BlockNumber(targetCheckpoint.startBlock + targetCheckpoint.numBlocks - 1);
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
// Remove all blocks after lastBlockToKeep (both checkpointed and uncheckpointed)
|
|
432
|
+
const blocksRemoved = await this.removeBlocksAfter(lastBlockToKeep);
|
|
433
|
+
|
|
434
|
+
// Remove all checkpoints after the target
|
|
435
|
+
for (let c = latestCheckpointNumber; c > checkpointNumber; c = CheckpointNumber(c - 1)) {
|
|
436
|
+
await this.#checkpoints.delete(c);
|
|
437
|
+
this.#log.debug(`Removed checkpoint ${c}`);
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
return { blocksRemoved };
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
async getCheckpointData(checkpointNumber: CheckpointNumber): Promise<CheckpointData | undefined> {
|
|
445
|
+
const checkpointStorage = await this.#checkpoints.getAsync(checkpointNumber);
|
|
446
|
+
if (!checkpointStorage) {
|
|
447
|
+
return undefined;
|
|
448
|
+
}
|
|
449
|
+
return this.checkpointDataFromCheckpointStorage(checkpointStorage);
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
async getRangeOfCheckpoints(from: CheckpointNumber, limit: number): Promise<CheckpointData[]> {
|
|
453
|
+
const checkpoints: CheckpointData[] = [];
|
|
454
|
+
for (let checkpointNumber = from; checkpointNumber < from + limit; checkpointNumber++) {
|
|
455
|
+
const checkpoint = await this.#checkpoints.getAsync(checkpointNumber);
|
|
456
|
+
if (!checkpoint) {
|
|
457
|
+
break;
|
|
458
|
+
}
|
|
459
|
+
checkpoints.push(this.checkpointDataFromCheckpointStorage(checkpoint));
|
|
460
|
+
}
|
|
461
|
+
return checkpoints;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
private checkpointDataFromCheckpointStorage(checkpointStorage: CheckpointStorage) {
|
|
465
|
+
const data: CheckpointData = {
|
|
466
|
+
header: CheckpointHeader.fromBuffer(checkpointStorage.header),
|
|
467
|
+
archive: AppendOnlyTreeSnapshot.fromBuffer(checkpointStorage.archive),
|
|
468
|
+
checkpointNumber: CheckpointNumber(checkpointStorage.checkpointNumber),
|
|
469
|
+
startBlock: checkpointStorage.startBlock,
|
|
470
|
+
numBlocks: checkpointStorage.numBlocks,
|
|
471
|
+
l1: L1PublishedData.fromBuffer(checkpointStorage.l1),
|
|
472
|
+
attestations: checkpointStorage.attestations,
|
|
473
|
+
};
|
|
474
|
+
return data;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
async getBlocksForCheckpoint(checkpointNumber: CheckpointNumber): Promise<L2Block[] | undefined> {
|
|
478
|
+
const checkpoint = await this.#checkpoints.getAsync(checkpointNumber);
|
|
479
|
+
if (!checkpoint) {
|
|
480
|
+
return undefined;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
const blocksForCheckpoint = await toArray(
|
|
484
|
+
this.#blocks.entriesAsync({
|
|
485
|
+
start: checkpoint.startBlock,
|
|
486
|
+
end: checkpoint.startBlock + checkpoint.numBlocks,
|
|
487
|
+
}),
|
|
488
|
+
);
|
|
489
|
+
|
|
490
|
+
const converted = await Promise.all(blocksForCheckpoint.map(x => this.getBlockFromBlockStorage(x[0], x[1])));
|
|
491
|
+
return converted.filter(isDefined);
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
/**
|
|
495
|
+
* Gets all blocks that have the given slot number.
|
|
496
|
+
* Iterates backwards through blocks for efficiency since we usually query for the last slot.
|
|
497
|
+
* @param slotNumber - The slot number to search for.
|
|
498
|
+
* @returns All blocks with the given slot number, in ascending block number order.
|
|
499
|
+
*/
|
|
500
|
+
async getBlocksForSlot(slotNumber: SlotNumber): Promise<L2Block[]> {
|
|
501
|
+
const blocks: L2Block[] = [];
|
|
502
|
+
|
|
503
|
+
// Iterate backwards through all blocks and filter by slot number
|
|
504
|
+
// This is more efficient since we usually query for the most recent slot
|
|
505
|
+
for await (const [blockNumber, blockStorage] of this.#blocks.entriesAsync({ reverse: true })) {
|
|
506
|
+
const block = await this.getBlockFromBlockStorage(blockNumber, blockStorage);
|
|
507
|
+
const blockSlot = block?.header.globalVariables.slotNumber;
|
|
508
|
+
if (block && blockSlot === slotNumber) {
|
|
509
|
+
blocks.push(block);
|
|
510
|
+
} else if (blockSlot && blockSlot < slotNumber) {
|
|
511
|
+
break; // Blocks are stored in slot ascending order, so we can stop searching
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
// Reverse to return blocks in ascending order (block number order)
|
|
516
|
+
return blocks.reverse();
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
/**
|
|
520
|
+
* Removes all blocks with block number > blockNumber.
|
|
521
|
+
* Does not remove any associated checkpoints.
|
|
522
|
+
* @param blockNumber - The block number to remove after.
|
|
523
|
+
* @returns The removed blocks (for event emission).
|
|
524
|
+
*/
|
|
525
|
+
async removeBlocksAfter(blockNumber: BlockNumber): Promise<L2Block[]> {
|
|
526
|
+
return await this.db.transactionAsync(async () => {
|
|
527
|
+
const removedBlocks: L2Block[] = [];
|
|
528
|
+
|
|
529
|
+
// Get the latest block number to determine the range
|
|
530
|
+
const latestBlockNumber = await this.getLatestBlockNumber();
|
|
531
|
+
|
|
532
|
+
// Iterate from blockNumber + 1 to latestBlockNumber
|
|
533
|
+
for (let bn = blockNumber + 1; bn <= latestBlockNumber; bn++) {
|
|
534
|
+
const block = await this.getBlock(BlockNumber(bn));
|
|
535
|
+
|
|
536
|
+
if (block === undefined) {
|
|
537
|
+
this.#log.warn(`Cannot remove block ${bn} from the store since we don't have it`);
|
|
538
|
+
continue;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
removedBlocks.push(block);
|
|
542
|
+
await this.deleteBlock(block);
|
|
543
|
+
this.#log.debug(`Removed block ${bn} ${(await block.hash()).toString()}`);
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
return removedBlocks;
|
|
547
|
+
});
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
async getProvenBlockNumber(): Promise<BlockNumber> {
|
|
551
|
+
const provenCheckpointNumber = await this.getProvenCheckpointNumber();
|
|
552
|
+
if (provenCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
|
|
553
|
+
return BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
554
|
+
}
|
|
555
|
+
const checkpointStorage = await this.#checkpoints.getAsync(provenCheckpointNumber);
|
|
556
|
+
if (!checkpointStorage) {
|
|
557
|
+
throw new CheckpointNotFoundError(provenCheckpointNumber);
|
|
558
|
+
} else {
|
|
559
|
+
return BlockNumber(checkpointStorage.startBlock + checkpointStorage.numBlocks - 1);
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
async getLatestBlockNumber(): Promise<BlockNumber> {
|
|
564
|
+
const [latestBlocknumber] = await toArray(this.#blocks.keysAsync({ reverse: true, limit: 1 }));
|
|
565
|
+
return typeof latestBlocknumber === 'number'
|
|
566
|
+
? BlockNumber(latestBlocknumber)
|
|
567
|
+
: BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
async getLatestCheckpointNumber(): Promise<CheckpointNumber> {
|
|
571
|
+
const [latestCheckpointNumber] = await toArray(this.#checkpoints.keysAsync({ reverse: true, limit: 1 }));
|
|
572
|
+
if (latestCheckpointNumber === undefined) {
|
|
573
|
+
return CheckpointNumber(INITIAL_CHECKPOINT_NUMBER - 1);
|
|
574
|
+
}
|
|
575
|
+
return CheckpointNumber(latestCheckpointNumber);
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
async getCheckpointedBlock(number: BlockNumber): Promise<CheckpointedL2Block | undefined> {
|
|
579
|
+
const blockStorage = await this.#blocks.getAsync(number);
|
|
580
|
+
if (!blockStorage) {
|
|
581
|
+
return undefined;
|
|
582
|
+
}
|
|
583
|
+
const checkpoint = await this.#checkpoints.getAsync(blockStorage.checkpointNumber);
|
|
584
|
+
if (!checkpoint) {
|
|
585
|
+
return undefined;
|
|
586
|
+
}
|
|
587
|
+
const block = await this.getBlockFromBlockStorage(number, blockStorage);
|
|
588
|
+
if (!block) {
|
|
589
|
+
return undefined;
|
|
590
|
+
}
|
|
591
|
+
return new CheckpointedL2Block(
|
|
592
|
+
CheckpointNumber(checkpoint.checkpointNumber),
|
|
593
|
+
block,
|
|
594
|
+
L1PublishedData.fromBuffer(checkpoint.l1),
|
|
595
|
+
checkpoint.attestations.map(buf => CommitteeAttestation.fromBuffer(buf)),
|
|
596
|
+
);
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
/**
|
|
600
|
+
* Gets up to `limit` amount of Checkpointed L2 blocks starting from `from`.
|
|
601
|
+
* @param start - Number of the first block to return (inclusive).
|
|
602
|
+
* @param limit - The number of blocks to return.
|
|
603
|
+
* @returns The requested L2 blocks
|
|
604
|
+
*/
|
|
605
|
+
async *getCheckpointedBlocks(start: BlockNumber, limit: number): AsyncIterableIterator<CheckpointedL2Block> {
|
|
606
|
+
const checkpointCache = new Map<CheckpointNumber, CheckpointStorage>();
|
|
607
|
+
for await (const [blockNumber, blockStorage] of this.getBlockStorages(start, limit)) {
|
|
608
|
+
const block = await this.getBlockFromBlockStorage(blockNumber, blockStorage);
|
|
609
|
+
if (block) {
|
|
610
|
+
const checkpoint =
|
|
611
|
+
checkpointCache.get(CheckpointNumber(blockStorage.checkpointNumber)) ??
|
|
612
|
+
(await this.#checkpoints.getAsync(blockStorage.checkpointNumber));
|
|
613
|
+
if (checkpoint) {
|
|
614
|
+
checkpointCache.set(CheckpointNumber(blockStorage.checkpointNumber), checkpoint);
|
|
615
|
+
const checkpointedBlock = new CheckpointedL2Block(
|
|
616
|
+
CheckpointNumber(checkpoint.checkpointNumber),
|
|
617
|
+
block,
|
|
618
|
+
L1PublishedData.fromBuffer(checkpoint.l1),
|
|
619
|
+
checkpoint.attestations.map(buf => CommitteeAttestation.fromBuffer(buf)),
|
|
620
|
+
);
|
|
621
|
+
yield checkpointedBlock;
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
async getCheckpointedBlockByHash(blockHash: Fr): Promise<CheckpointedL2Block | undefined> {
|
|
628
|
+
const blockNumber = await this.#blockHashIndex.getAsync(blockHash.toString());
|
|
629
|
+
if (blockNumber === undefined) {
|
|
630
|
+
return undefined;
|
|
631
|
+
}
|
|
632
|
+
return this.getCheckpointedBlock(BlockNumber(blockNumber));
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
async getCheckpointedBlockByArchive(archive: Fr): Promise<CheckpointedL2Block | undefined> {
|
|
636
|
+
const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
|
|
637
|
+
if (blockNumber === undefined) {
|
|
638
|
+
return undefined;
|
|
639
|
+
}
|
|
640
|
+
return this.getCheckpointedBlock(BlockNumber(blockNumber));
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
/**
|
|
644
|
+
* Gets up to `limit` amount of L2 blocks starting from `from`.
|
|
645
|
+
* @param start - Number of the first block to return (inclusive).
|
|
646
|
+
* @param limit - The number of blocks to return.
|
|
647
|
+
* @returns The requested L2 blocks
|
|
648
|
+
*/
|
|
649
|
+
async *getBlocks(start: BlockNumber, limit: number): AsyncIterableIterator<L2Block> {
|
|
650
|
+
for await (const [blockNumber, blockStorage] of this.getBlockStorages(start, limit)) {
|
|
651
|
+
const block = await this.getBlockFromBlockStorage(blockNumber, blockStorage);
|
|
652
|
+
if (block) {
|
|
653
|
+
yield block;
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
/**
|
|
659
|
+
* Gets an L2 block.
|
|
660
|
+
* @param blockNumber - The number of the block to return.
|
|
661
|
+
* @returns The requested L2 block.
|
|
662
|
+
*/
|
|
663
|
+
async getBlock(blockNumber: BlockNumber): Promise<L2Block | undefined> {
|
|
664
|
+
const blockStorage = await this.#blocks.getAsync(blockNumber);
|
|
665
|
+
if (!blockStorage || !blockStorage.header) {
|
|
666
|
+
return Promise.resolve(undefined);
|
|
667
|
+
}
|
|
668
|
+
return this.getBlockFromBlockStorage(blockNumber, blockStorage);
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
/**
|
|
672
|
+
* Gets an L2 block by its hash.
|
|
673
|
+
* @param blockHash - The hash of the block to return.
|
|
674
|
+
* @returns The requested L2 block.
|
|
675
|
+
*/
|
|
676
|
+
async getBlockByHash(blockHash: L2BlockHash): Promise<L2Block | undefined> {
|
|
677
|
+
const blockNumber = await this.#blockHashIndex.getAsync(blockHash.toString());
|
|
678
|
+
if (blockNumber === undefined) {
|
|
679
|
+
return undefined;
|
|
680
|
+
}
|
|
681
|
+
return this.getBlock(BlockNumber(blockNumber));
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
/**
|
|
685
|
+
* Gets an L2 block by its archive root.
|
|
686
|
+
* @param archive - The archive root of the block to return.
|
|
687
|
+
* @returns The requested L2 block.
|
|
688
|
+
*/
|
|
689
|
+
async getBlockByArchive(archive: Fr): Promise<L2Block | undefined> {
|
|
690
|
+
const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
|
|
691
|
+
if (blockNumber === undefined) {
|
|
692
|
+
return undefined;
|
|
693
|
+
}
|
|
694
|
+
return this.getBlock(BlockNumber(blockNumber));
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
/**
|
|
698
|
+
* Gets a block header by its hash.
|
|
699
|
+
* @param blockHash - The hash of the block to return.
|
|
700
|
+
* @returns The requested block header.
|
|
701
|
+
*/
|
|
702
|
+
async getBlockHeaderByHash(blockHash: L2BlockHash): Promise<BlockHeader | undefined> {
|
|
703
|
+
const blockNumber = await this.#blockHashIndex.getAsync(blockHash.toString());
|
|
704
|
+
if (blockNumber === undefined) {
|
|
705
|
+
return undefined;
|
|
706
|
+
}
|
|
707
|
+
const blockStorage = await this.#blocks.getAsync(blockNumber);
|
|
708
|
+
if (!blockStorage || !blockStorage.header) {
|
|
709
|
+
return undefined;
|
|
710
|
+
}
|
|
711
|
+
return BlockHeader.fromBuffer(blockStorage.header);
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
/**
|
|
715
|
+
* Gets a block header by its archive root.
|
|
716
|
+
* @param archive - The archive root of the block to return.
|
|
717
|
+
* @returns The requested block header.
|
|
718
|
+
*/
|
|
719
|
+
async getBlockHeaderByArchive(archive: Fr): Promise<BlockHeader | undefined> {
|
|
720
|
+
const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
|
|
721
|
+
if (blockNumber === undefined) {
|
|
722
|
+
return undefined;
|
|
723
|
+
}
|
|
724
|
+
const blockStorage = await this.#blocks.getAsync(blockNumber);
|
|
725
|
+
if (!blockStorage || !blockStorage.header) {
|
|
726
|
+
return undefined;
|
|
727
|
+
}
|
|
728
|
+
return BlockHeader.fromBuffer(blockStorage.header);
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
/**
|
|
732
|
+
* Gets the headers for a sequence of L2 blocks.
|
|
733
|
+
* @param start - Number of the first block to return (inclusive).
|
|
734
|
+
* @param limit - The number of blocks to return.
|
|
735
|
+
* @returns The requested L2 block headers
|
|
736
|
+
*/
|
|
737
|
+
async *getBlockHeaders(start: BlockNumber, limit: number): AsyncIterableIterator<BlockHeader> {
|
|
738
|
+
for await (const [blockNumber, blockStorage] of this.getBlockStorages(start, limit)) {
|
|
739
|
+
const header = BlockHeader.fromBuffer(blockStorage.header);
|
|
740
|
+
if (header.getBlockNumber() !== blockNumber) {
|
|
741
|
+
throw new Error(
|
|
742
|
+
`Block number mismatch when retrieving block header from archive (expected ${blockNumber} but got ${header.getBlockNumber()})`,
|
|
743
|
+
);
|
|
744
|
+
}
|
|
745
|
+
yield header;
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
private async *getBlockStorages(start: BlockNumber, limit: number) {
|
|
750
|
+
let expectedBlockNumber = start;
|
|
751
|
+
for await (const [blockNumber, blockStorage] of this.#blocks.entriesAsync(this.#computeBlockRange(start, limit))) {
|
|
752
|
+
if (blockNumber !== expectedBlockNumber) {
|
|
753
|
+
throw new Error(
|
|
754
|
+
`Block number mismatch when iterating blocks from archive (expected ${expectedBlockNumber} but got ${blockNumber})`,
|
|
755
|
+
);
|
|
756
|
+
}
|
|
757
|
+
expectedBlockNumber++;
|
|
758
|
+
yield [blockNumber, blockStorage] as const;
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
private async getBlockFromBlockStorage(
|
|
763
|
+
blockNumber: number,
|
|
764
|
+
blockStorage: BlockStorage,
|
|
765
|
+
): Promise<L2Block | undefined> {
|
|
766
|
+
const header = BlockHeader.fromBuffer(blockStorage.header);
|
|
767
|
+
const archive = AppendOnlyTreeSnapshot.fromBuffer(blockStorage.archive);
|
|
768
|
+
const blockHash = blockStorage.blockHash;
|
|
769
|
+
header.setHash(Fr.fromBuffer(blockHash));
|
|
770
|
+
const blockHashString = bufferToHex(blockHash);
|
|
771
|
+
const blockTxsBuffer = await this.#blockTxs.getAsync(blockHashString);
|
|
772
|
+
if (blockTxsBuffer === undefined) {
|
|
773
|
+
this.#log.warn(`Could not find body for block ${header.globalVariables.blockNumber} ${blockHash}`);
|
|
774
|
+
return undefined;
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
const txEffects: TxEffect[] = [];
|
|
778
|
+
const reader = BufferReader.asReader(blockTxsBuffer);
|
|
779
|
+
while (!reader.isEmpty()) {
|
|
780
|
+
const txHash = reader.readObject(TxHash);
|
|
781
|
+
const txEffect = await this.#txEffects.getAsync(txHash.toString());
|
|
782
|
+
if (txEffect === undefined) {
|
|
783
|
+
this.#log.warn(`Could not find tx effect for tx ${txHash} in block ${blockNumber}`);
|
|
784
|
+
return undefined;
|
|
785
|
+
}
|
|
786
|
+
txEffects.push(deserializeIndexedTxEffect(txEffect).data);
|
|
787
|
+
}
|
|
788
|
+
const body = new Body(txEffects);
|
|
789
|
+
const block = new L2Block(
|
|
790
|
+
archive,
|
|
791
|
+
header,
|
|
792
|
+
body,
|
|
793
|
+
CheckpointNumber(blockStorage.checkpointNumber!),
|
|
794
|
+
IndexWithinCheckpoint(blockStorage.indexWithinCheckpoint),
|
|
795
|
+
);
|
|
796
|
+
|
|
797
|
+
if (block.number !== blockNumber) {
|
|
798
|
+
throw new Error(
|
|
799
|
+
`Block number mismatch when retrieving block from archive (expected ${blockNumber} but got ${
|
|
800
|
+
block.number
|
|
801
|
+
} with hash ${blockHashString})`,
|
|
802
|
+
);
|
|
803
|
+
}
|
|
804
|
+
return block;
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
/**
|
|
808
|
+
* Gets a tx effect.
|
|
809
|
+
* @param txHash - The hash of the tx corresponding to the tx effect.
|
|
810
|
+
* @returns The requested tx effect with block info (or undefined if not found).
|
|
811
|
+
*/
|
|
812
|
+
async getTxEffect(txHash: TxHash): Promise<IndexedTxEffect | undefined> {
|
|
813
|
+
const buffer = await this.#txEffects.getAsync(txHash.toString());
|
|
814
|
+
if (!buffer) {
|
|
815
|
+
return undefined;
|
|
816
|
+
}
|
|
817
|
+
return deserializeIndexedTxEffect(buffer);
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
/**
|
|
821
|
+
* Gets a receipt of a settled tx.
|
|
822
|
+
* @param txHash - The hash of a tx we try to get the receipt for.
|
|
823
|
+
* @returns The requested tx receipt (or undefined if not found).
|
|
824
|
+
*/
|
|
825
|
+
async getSettledTxReceipt(txHash: TxHash): Promise<TxReceipt | undefined> {
|
|
826
|
+
const txEffect = await this.getTxEffect(txHash);
|
|
827
|
+
if (!txEffect) {
|
|
828
|
+
return undefined;
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
const blockNumber = BlockNumber(txEffect.l2BlockNumber);
|
|
832
|
+
|
|
833
|
+
// Use existing archiver methods to determine finalization level
|
|
834
|
+
const [provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber] = await Promise.all([
|
|
835
|
+
this.getProvenBlockNumber(),
|
|
836
|
+
this.getCheckpointedL2BlockNumber(),
|
|
837
|
+
this.getFinalizedL2BlockNumber(),
|
|
838
|
+
]);
|
|
839
|
+
|
|
840
|
+
let status: TxStatus;
|
|
841
|
+
if (blockNumber <= finalizedBlockNumber) {
|
|
842
|
+
status = TxStatus.FINALIZED;
|
|
843
|
+
} else if (blockNumber <= provenBlockNumber) {
|
|
844
|
+
status = TxStatus.PROVEN;
|
|
845
|
+
} else if (blockNumber <= checkpointedBlockNumber) {
|
|
846
|
+
status = TxStatus.CHECKPOINTED;
|
|
847
|
+
} else {
|
|
848
|
+
status = TxStatus.PROPOSED;
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
return new TxReceipt(
|
|
852
|
+
txHash,
|
|
853
|
+
status,
|
|
854
|
+
TxReceipt.executionResultFromRevertCode(txEffect.data.revertCode),
|
|
855
|
+
undefined,
|
|
856
|
+
txEffect.data.transactionFee.toBigInt(),
|
|
857
|
+
txEffect.l2BlockHash,
|
|
858
|
+
blockNumber,
|
|
859
|
+
);
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
/**
|
|
863
|
+
* Looks up which block included the requested tx effect.
|
|
864
|
+
* @param txHash - The txHash of the tx.
|
|
865
|
+
* @returns The block number and index of the tx.
|
|
866
|
+
*/
|
|
867
|
+
public async getTxLocation(txHash: TxHash): Promise<[blockNumber: number, txIndex: number] | undefined> {
|
|
868
|
+
const txEffect = await this.#txEffects.getAsync(txHash.toString());
|
|
869
|
+
if (!txEffect) {
|
|
870
|
+
return undefined;
|
|
871
|
+
}
|
|
872
|
+
const { l2BlockNumber, txIndexInBlock } = deserializeIndexedTxEffect(txEffect);
|
|
873
|
+
return [l2BlockNumber, txIndexInBlock];
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
/**
|
|
877
|
+
* Looks up which block deployed a particular contract.
|
|
878
|
+
* @param contractAddress - The address of the contract to look up.
|
|
879
|
+
* @returns The block number and index of the contract.
|
|
880
|
+
*/
|
|
881
|
+
getContractLocation(contractAddress: AztecAddress): Promise<[blockNumber: number, index: number] | undefined> {
|
|
882
|
+
return this.#contractIndex.getAsync(contractAddress.toString());
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
/**
|
|
886
|
+
* Gets the number of the latest L2 block checkpointed.
|
|
887
|
+
* @returns The number of the latest L2 block checkpointed.
|
|
888
|
+
*/
|
|
889
|
+
async getCheckpointedL2BlockNumber(): Promise<BlockNumber> {
|
|
890
|
+
const latestCheckpointNumber = await this.getLatestCheckpointNumber();
|
|
891
|
+
const checkpoint = await this.getCheckpointData(latestCheckpointNumber);
|
|
892
|
+
if (!checkpoint) {
|
|
893
|
+
return BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
894
|
+
}
|
|
895
|
+
return BlockNumber(checkpoint.startBlock + checkpoint.numBlocks - 1);
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
async getLatestL2BlockNumber(): Promise<BlockNumber> {
|
|
899
|
+
const [lastBlockNumber] = await toArray(this.#blocks.keysAsync({ reverse: true, limit: 1 }));
|
|
900
|
+
return typeof lastBlockNumber === 'number' ? BlockNumber(lastBlockNumber) : BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
/**
|
|
904
|
+
* Gets the most recent L1 block processed.
|
|
905
|
+
* @returns The L1 block that published the latest L2 block
|
|
906
|
+
*/
|
|
907
|
+
getSynchedL1BlockNumber(): Promise<bigint | undefined> {
|
|
908
|
+
return this.#lastSynchedL1Block.getAsync();
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
setSynchedL1BlockNumber(l1BlockNumber: bigint) {
|
|
912
|
+
return this.#lastSynchedL1Block.set(l1BlockNumber);
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
async getProvenCheckpointNumber(): Promise<CheckpointNumber> {
|
|
916
|
+
const [latestCheckpointNumber, provenCheckpointNumber] = await Promise.all([
|
|
917
|
+
this.getLatestCheckpointNumber(),
|
|
918
|
+
this.#lastProvenCheckpoint.getAsync(),
|
|
919
|
+
]);
|
|
920
|
+
return (provenCheckpointNumber ?? 0) > latestCheckpointNumber
|
|
921
|
+
? latestCheckpointNumber
|
|
922
|
+
: CheckpointNumber(provenCheckpointNumber ?? 0);
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
async setProvenCheckpointNumber(checkpointNumber: CheckpointNumber) {
|
|
926
|
+
const result = await this.#lastProvenCheckpoint.set(checkpointNumber);
|
|
927
|
+
return result;
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
#computeBlockRange(start: BlockNumber, limit: number): Required<Pick<Range<number>, 'start' | 'limit'>> {
|
|
931
|
+
if (limit < 1) {
|
|
932
|
+
throw new Error(`Invalid limit: ${limit}`);
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
if (start < INITIAL_L2_BLOCK_NUM) {
|
|
936
|
+
throw new Error(`Invalid start: ${start}`);
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
return { start, limit };
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
/**
|
|
943
|
+
* Gets the pending chain validation status.
|
|
944
|
+
* @returns The validation status or undefined if not set.
|
|
945
|
+
*/
|
|
946
|
+
async getPendingChainValidationStatus(): Promise<ValidateCheckpointResult | undefined> {
|
|
947
|
+
const buffer = await this.#pendingChainValidationStatus.getAsync();
|
|
948
|
+
if (!buffer) {
|
|
949
|
+
return undefined;
|
|
950
|
+
}
|
|
951
|
+
return deserializeValidateCheckpointResult(buffer);
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
/**
|
|
955
|
+
* Sets the pending chain validation status.
|
|
956
|
+
* @param status - The validation status to store.
|
|
957
|
+
*/
|
|
958
|
+
async setPendingChainValidationStatus(status: ValidateCheckpointResult | undefined): Promise<void> {
|
|
959
|
+
if (status) {
|
|
960
|
+
const buffer = serializeValidateCheckpointResult(status);
|
|
961
|
+
await this.#pendingChainValidationStatus.set(buffer);
|
|
962
|
+
} else {
|
|
963
|
+
await this.#pendingChainValidationStatus.delete();
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
}
|