@aztec/archiver 0.0.1-commit.9b94fc1 → 0.0.1-commit.9badcec54
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 +162 -22
- package/dest/archiver.d.ts +140 -0
- package/dest/archiver.d.ts.map +1 -0
- package/dest/archiver.js +750 -0
- package/dest/config.d.ts +30 -0
- package/dest/config.d.ts.map +1 -0
- package/dest/{archiver/config.js → config.js} +24 -7
- package/dest/errors.d.ts +65 -0
- package/dest/errors.d.ts.map +1 -0
- package/dest/errors.js +91 -0
- package/dest/factory.d.ts +8 -7
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +99 -15
- package/dest/index.d.ts +11 -4
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +9 -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 +152 -0
- package/dest/l1/calldata_retriever.d.ts +135 -0
- package/dest/l1/calldata_retriever.d.ts.map +1 -0
- package/dest/l1/calldata_retriever.js +403 -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 +87 -160
- 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 +149 -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 +32 -0
- package/dest/l1/validate_trace.d.ts.map +1 -0
- package/dest/l1/validate_trace.js +154 -0
- package/dest/modules/data_source_base.d.ts +91 -0
- package/dest/modules/data_source_base.d.ts.map +1 -0
- package/dest/modules/data_source_base.js +222 -0
- package/dest/modules/data_store_updater.d.ts +86 -0
- package/dest/modules/data_store_updater.d.ts.map +1 -0
- package/dest/modules/data_store_updater.js +333 -0
- package/dest/modules/instrumentation.d.ts +52 -0
- package/dest/modules/instrumentation.d.ts.map +1 -0
- package/dest/modules/instrumentation.js +138 -0
- package/dest/modules/l1_synchronizer.d.ts +73 -0
- package/dest/modules/l1_synchronizer.d.ts.map +1 -0
- package/dest/modules/l1_synchronizer.js +1154 -0
- package/dest/modules/validation.d.ts +17 -0
- package/dest/modules/validation.d.ts.map +1 -0
- package/dest/{archiver → modules}/validation.js +9 -3
- package/dest/store/block_store.d.ts +230 -0
- package/dest/store/block_store.d.ts.map +1 -0
- package/dest/store/block_store.js +942 -0
- package/dest/store/contract_class_store.d.ts +17 -0
- package/dest/store/contract_class_store.d.ts.map +1 -0
- package/dest/store/contract_class_store.js +64 -0
- 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 +7 -3
- package/dest/store/kv_archiver_store.d.ts +377 -0
- package/dest/store/kv_archiver_store.d.ts.map +1 -0
- package/dest/store/kv_archiver_store.js +494 -0
- package/dest/store/l2_tips_cache.d.ts +20 -0
- package/dest/store/l2_tips_cache.d.ts.map +1 -0
- package/dest/store/l2_tips_cache.js +109 -0
- package/dest/store/log_store.d.ts +57 -0
- package/dest/store/log_store.d.ts.map +1 -0
- package/dest/store/log_store.js +531 -0
- package/dest/store/message_store.d.ts +44 -0
- package/dest/store/message_store.d.ts.map +1 -0
- package/dest/{archiver/kv_archiver_store → store}/message_store.js +36 -23
- 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 +210 -0
- package/dest/test/fake_l1_state.d.ts.map +1 -0
- package/dest/test/fake_l1_state.js +490 -0
- package/dest/test/index.d.ts +2 -1
- package/dest/test/index.d.ts.map +1 -1
- package/dest/test/index.js +4 -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 +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 +23 -12
- package/dest/test/mock_l2_block_source.d.ts +59 -20
- package/dest/test/mock_l2_block_source.d.ts.map +1 -1
- package/dest/test/mock_l2_block_source.js +276 -85
- package/dest/test/mock_structs.d.ts +83 -4
- package/dest/test/mock_structs.d.ts.map +1 -1
- package/dest/test/mock_structs.js +157 -11
- package/dest/test/noop_l1_archiver.d.ts +26 -0
- package/dest/test/noop_l1_archiver.d.ts.map +1 -0
- package/dest/test/noop_l1_archiver.js +71 -0
- package/package.json +20 -20
- package/src/archiver.ts +510 -0
- package/src/{archiver/config.ts → config.ts} +37 -14
- package/src/errors.ts +146 -0
- package/src/factory.ts +154 -17
- package/src/index.ts +11 -3
- package/src/interfaces.ts +9 -0
- package/src/l1/README.md +55 -0
- package/src/l1/bin/retrieve-calldata.ts +194 -0
- package/src/l1/calldata_retriever.ts +512 -0
- package/src/{archiver → l1}/data_retrieval.ts +146 -233
- package/src/l1/debug_tx.ts +99 -0
- package/src/l1/spire_proposer.ts +152 -0
- package/src/l1/trace_tx.ts +128 -0
- package/src/l1/types.ts +13 -0
- package/src/l1/validate_trace.ts +229 -0
- package/src/modules/data_source_base.ts +347 -0
- package/src/modules/data_store_updater.ts +437 -0
- package/src/modules/instrumentation.ts +196 -0
- package/src/modules/l1_synchronizer.ts +961 -0
- package/src/{archiver → modules}/validation.ts +13 -8
- package/src/store/block_store.ts +1245 -0
- package/src/store/contract_class_store.ts +82 -0
- package/src/{archiver/kv_archiver_store → store}/contract_instance_store.ts +10 -7
- package/src/store/kv_archiver_store.ts +713 -0
- package/src/store/l2_tips_cache.ts +134 -0
- package/src/store/log_store.ts +733 -0
- package/src/{archiver/kv_archiver_store → store}/message_store.ts +48 -28
- package/src/{archiver/structs → structs}/inbox_message.ts +7 -7
- package/src/{archiver/structs → structs}/published.ts +0 -1
- package/src/test/fake_l1_state.ts +747 -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 +4 -0
- package/src/test/mock_archiver.ts +23 -16
- package/src/test/mock_l1_to_l2_message_source.ts +19 -11
- package/src/test/mock_l2_block_source.ts +346 -93
- package/src/test/mock_structs.ts +289 -13
- package/src/test/noop_l1_archiver.ts +114 -0
- 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/instrumentation.js +0 -140
- 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_class_store.js +0 -120
- 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/instrumentation.ts +0 -187
- package/src/archiver/kv_archiver_store/block_store.ts +0 -481
- package/src/archiver/kv_archiver_store/contract_class_store.ts +0 -176
- 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,1245 @@
|
|
|
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
|
+
type BlockData,
|
|
13
|
+
BlockHash,
|
|
14
|
+
Body,
|
|
15
|
+
CheckpointedL2Block,
|
|
16
|
+
CommitteeAttestation,
|
|
17
|
+
L2Block,
|
|
18
|
+
type ValidateCheckpointResult,
|
|
19
|
+
deserializeValidateCheckpointResult,
|
|
20
|
+
serializeValidateCheckpointResult,
|
|
21
|
+
} from '@aztec/stdlib/block';
|
|
22
|
+
import {
|
|
23
|
+
Checkpoint,
|
|
24
|
+
type CheckpointData,
|
|
25
|
+
type CommonCheckpointData,
|
|
26
|
+
L1PublishedData,
|
|
27
|
+
type ProposedCheckpointData,
|
|
28
|
+
type ProposedCheckpointInput,
|
|
29
|
+
PublishedCheckpoint,
|
|
30
|
+
} from '@aztec/stdlib/checkpoint';
|
|
31
|
+
import { type L1RollupConstants, getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
|
|
32
|
+
import { CheckpointHeader } from '@aztec/stdlib/rollup';
|
|
33
|
+
import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
|
|
34
|
+
import {
|
|
35
|
+
BlockHeader,
|
|
36
|
+
type IndexedTxEffect,
|
|
37
|
+
TxEffect,
|
|
38
|
+
TxHash,
|
|
39
|
+
TxReceipt,
|
|
40
|
+
TxStatus,
|
|
41
|
+
deserializeIndexedTxEffect,
|
|
42
|
+
serializeIndexedTxEffect,
|
|
43
|
+
} from '@aztec/stdlib/tx';
|
|
44
|
+
|
|
45
|
+
import {
|
|
46
|
+
BlockAlreadyCheckpointedError,
|
|
47
|
+
BlockArchiveNotConsistentError,
|
|
48
|
+
BlockIndexNotSequentialError,
|
|
49
|
+
BlockNotFoundError,
|
|
50
|
+
BlockNumberNotSequentialError,
|
|
51
|
+
CannotOverwriteCheckpointedBlockError,
|
|
52
|
+
CheckpointNotFoundError,
|
|
53
|
+
CheckpointNumberNotSequentialError,
|
|
54
|
+
InitialCheckpointNumberNotSequentialError,
|
|
55
|
+
ProposedCheckpointNotSequentialError,
|
|
56
|
+
ProposedCheckpointStaleError,
|
|
57
|
+
} from '../errors.js';
|
|
58
|
+
|
|
59
|
+
export { TxReceipt, type TxEffect, type TxHash } from '@aztec/stdlib/tx';
|
|
60
|
+
|
|
61
|
+
type BlockIndexValue = [blockNumber: number, index: number];
|
|
62
|
+
|
|
63
|
+
type BlockStorage = {
|
|
64
|
+
header: Buffer;
|
|
65
|
+
blockHash: Buffer;
|
|
66
|
+
archive: Buffer;
|
|
67
|
+
checkpointNumber: number;
|
|
68
|
+
indexWithinCheckpoint: number;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/** Checkpoint Storage shared between Checkpoints + Proposed Checkpoints */
|
|
72
|
+
type CommonCheckpointStorage = {
|
|
73
|
+
header: Buffer;
|
|
74
|
+
archive: Buffer;
|
|
75
|
+
checkpointOutHash: Buffer;
|
|
76
|
+
checkpointNumber: number;
|
|
77
|
+
startBlock: number;
|
|
78
|
+
blockCount: number;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
type CheckpointStorage = CommonCheckpointStorage & {
|
|
82
|
+
l1: Buffer;
|
|
83
|
+
attestations: Buffer[];
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/** Storage format for a proposed checkpoint (attested but not yet L1-confirmed). */
|
|
87
|
+
type ProposedCheckpointStorage = CommonCheckpointStorage & {
|
|
88
|
+
totalManaUsed: string;
|
|
89
|
+
feeAssetPriceModifier: string;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
export type RemoveCheckpointsResult = { blocksRemoved: L2Block[] | undefined };
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* LMDB-based block storage for the archiver.
|
|
96
|
+
*/
|
|
97
|
+
export class BlockStore {
|
|
98
|
+
/** Map block number to block data */
|
|
99
|
+
#blocks: AztecAsyncMap<number, BlockStorage>;
|
|
100
|
+
|
|
101
|
+
/** Map checkpoint number to checkpoint data */
|
|
102
|
+
#checkpoints: AztecAsyncMap<number, CheckpointStorage>;
|
|
103
|
+
|
|
104
|
+
/** Map slot number to checkpoint number, for looking up checkpoints by slot range. */
|
|
105
|
+
#slotToCheckpoint: AztecAsyncMap<number, number>;
|
|
106
|
+
|
|
107
|
+
/** Map block hash to list of tx hashes */
|
|
108
|
+
#blockTxs: AztecAsyncMap<string, Buffer>;
|
|
109
|
+
|
|
110
|
+
/** Tx hash to serialized IndexedTxEffect */
|
|
111
|
+
#txEffects: AztecAsyncMap<string, Buffer>;
|
|
112
|
+
|
|
113
|
+
/** Stores L1 block number in which the last processed L2 block was included */
|
|
114
|
+
#lastSynchedL1Block: AztecAsyncSingleton<bigint>;
|
|
115
|
+
|
|
116
|
+
/** Stores last proven checkpoint */
|
|
117
|
+
#lastProvenCheckpoint: AztecAsyncSingleton<number>;
|
|
118
|
+
|
|
119
|
+
/** Stores last finalized checkpoint (proven at or before the finalized L1 block) */
|
|
120
|
+
#lastFinalizedCheckpoint: AztecAsyncSingleton<number>;
|
|
121
|
+
|
|
122
|
+
/** Stores the pending chain validation status */
|
|
123
|
+
#pendingChainValidationStatus: AztecAsyncSingleton<Buffer>;
|
|
124
|
+
|
|
125
|
+
/** Index mapping a contract's address (as a string) to its location in a block */
|
|
126
|
+
#contractIndex: AztecAsyncMap<string, BlockIndexValue>;
|
|
127
|
+
|
|
128
|
+
/** Index mapping block hash to block number */
|
|
129
|
+
#blockHashIndex: AztecAsyncMap<string, number>;
|
|
130
|
+
|
|
131
|
+
/** Index mapping block archive to block number */
|
|
132
|
+
#blockArchiveIndex: AztecAsyncMap<string, number>;
|
|
133
|
+
|
|
134
|
+
/** Singleton: assumes max 1-deep pipeline. For deeper pipelining, replace with a map keyed by checkpoint number. */
|
|
135
|
+
#proposedCheckpoint: AztecAsyncSingleton<ProposedCheckpointStorage>;
|
|
136
|
+
|
|
137
|
+
#log = createLogger('archiver:block_store');
|
|
138
|
+
|
|
139
|
+
constructor(private db: AztecAsyncKVStore) {
|
|
140
|
+
this.#blocks = db.openMap('archiver_blocks');
|
|
141
|
+
this.#blockTxs = db.openMap('archiver_block_txs');
|
|
142
|
+
this.#txEffects = db.openMap('archiver_tx_effects');
|
|
143
|
+
this.#contractIndex = db.openMap('archiver_contract_index');
|
|
144
|
+
this.#blockHashIndex = db.openMap('archiver_block_hash_index');
|
|
145
|
+
this.#blockArchiveIndex = db.openMap('archiver_block_archive_index');
|
|
146
|
+
this.#lastSynchedL1Block = db.openSingleton('archiver_last_synched_l1_block');
|
|
147
|
+
this.#lastProvenCheckpoint = db.openSingleton('archiver_last_proven_l2_checkpoint');
|
|
148
|
+
this.#lastFinalizedCheckpoint = db.openSingleton('archiver_last_finalized_l2_checkpoint');
|
|
149
|
+
this.#pendingChainValidationStatus = db.openSingleton('archiver_pending_chain_validation_status');
|
|
150
|
+
this.#checkpoints = db.openMap('archiver_checkpoints');
|
|
151
|
+
this.#slotToCheckpoint = db.openMap('archiver_slot_to_checkpoint');
|
|
152
|
+
this.#proposedCheckpoint = db.openSingleton('proposed_checkpoint_data');
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Returns the finalized L2 block number. An L2 block is finalized when it was proven
|
|
157
|
+
* in an L1 block that has itself been finalized on Ethereum.
|
|
158
|
+
* @returns The finalized block number.
|
|
159
|
+
*/
|
|
160
|
+
async getFinalizedL2BlockNumber(): Promise<BlockNumber> {
|
|
161
|
+
const finalizedCheckpointNumber = await this.getFinalizedCheckpointNumber();
|
|
162
|
+
if (finalizedCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
|
|
163
|
+
return BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
164
|
+
}
|
|
165
|
+
const checkpointStorage = await this.#checkpoints.getAsync(finalizedCheckpointNumber);
|
|
166
|
+
if (!checkpointStorage) {
|
|
167
|
+
throw new CheckpointNotFoundError(finalizedCheckpointNumber);
|
|
168
|
+
}
|
|
169
|
+
return BlockNumber(checkpointStorage.startBlock + checkpointStorage.blockCount - 1);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Append a new proposed block to the store.
|
|
174
|
+
* This is an uncheckpointed block that has been proposed by the sequencer but not yet included in a checkpoint on L1.
|
|
175
|
+
* For checkpointed blocks (already published to L1), use addCheckpoints() instead.
|
|
176
|
+
* @param block - The proposed L2 block to be added to the store.
|
|
177
|
+
* @returns True if the operation is successful.
|
|
178
|
+
*/
|
|
179
|
+
async addProposedBlock(block: L2Block, opts: { force?: boolean } = {}): Promise<boolean> {
|
|
180
|
+
return await this.db.transactionAsync(async () => {
|
|
181
|
+
const blockNumber = block.number;
|
|
182
|
+
const blockCheckpointNumber = block.checkpointNumber;
|
|
183
|
+
const blockIndex = block.indexWithinCheckpoint;
|
|
184
|
+
const blockLastArchive = block.header.lastArchive.root;
|
|
185
|
+
|
|
186
|
+
// Extract the latest block and checkpoint numbers
|
|
187
|
+
const previousBlockNumber = await this.getLatestL2BlockNumber();
|
|
188
|
+
const proposedCheckpointNumber = await this.getProposedCheckpointNumber();
|
|
189
|
+
const previousCheckpointNumber = await this.getLatestCheckpointNumber();
|
|
190
|
+
|
|
191
|
+
// Verify we're not overwriting checkpointed blocks
|
|
192
|
+
const lastCheckpointedBlockNumber = await this.getCheckpointedL2BlockNumber();
|
|
193
|
+
if (!opts.force && blockNumber <= lastCheckpointedBlockNumber) {
|
|
194
|
+
// Check if the proposed block matches the already-checkpointed one
|
|
195
|
+
const existingBlock = await this.getBlock(BlockNumber(blockNumber));
|
|
196
|
+
if (existingBlock && existingBlock.archive.root.equals(block.archive.root)) {
|
|
197
|
+
throw new BlockAlreadyCheckpointedError(blockNumber);
|
|
198
|
+
}
|
|
199
|
+
throw new CannotOverwriteCheckpointedBlockError(blockNumber, lastCheckpointedBlockNumber);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// Check that the block number is the expected one
|
|
203
|
+
if (!opts.force && previousBlockNumber !== blockNumber - 1) {
|
|
204
|
+
throw new BlockNumberNotSequentialError(blockNumber, previousBlockNumber);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// The same check as above but for checkpoints. Accept the block if either the confirmed
|
|
208
|
+
// checkpoint or the pending (locally validated but not yet confirmed) checkpoint matches.
|
|
209
|
+
const expectedCheckpointNumber = blockCheckpointNumber - 1;
|
|
210
|
+
if (
|
|
211
|
+
!opts.force &&
|
|
212
|
+
previousCheckpointNumber !== expectedCheckpointNumber &&
|
|
213
|
+
proposedCheckpointNumber !== expectedCheckpointNumber
|
|
214
|
+
) {
|
|
215
|
+
const [reported, source]: [CheckpointNumber, 'confirmed' | 'proposed'] =
|
|
216
|
+
proposedCheckpointNumber > previousCheckpointNumber
|
|
217
|
+
? [proposedCheckpointNumber, 'proposed']
|
|
218
|
+
: [previousCheckpointNumber, 'confirmed'];
|
|
219
|
+
throw new CheckpointNumberNotSequentialError(blockCheckpointNumber, reported, source);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// Extract the previous block if there is one and see if it is for the same checkpoint or not
|
|
223
|
+
const previousBlockResult = await this.getBlock(previousBlockNumber);
|
|
224
|
+
|
|
225
|
+
let expectedBlockIndex = 0;
|
|
226
|
+
let previousBlockIndex: number | undefined = undefined;
|
|
227
|
+
if (previousBlockResult !== undefined) {
|
|
228
|
+
if (previousBlockResult.checkpointNumber === blockCheckpointNumber) {
|
|
229
|
+
// The previous block is for the same checkpoint, therefore our index should follow it
|
|
230
|
+
previousBlockIndex = previousBlockResult.indexWithinCheckpoint;
|
|
231
|
+
expectedBlockIndex = previousBlockIndex + 1;
|
|
232
|
+
}
|
|
233
|
+
if (!previousBlockResult.archive.root.equals(blockLastArchive)) {
|
|
234
|
+
throw new BlockArchiveNotConsistentError(
|
|
235
|
+
blockNumber,
|
|
236
|
+
previousBlockResult.number,
|
|
237
|
+
blockLastArchive,
|
|
238
|
+
previousBlockResult.archive.root,
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// Now check that the block has the expected index value
|
|
244
|
+
if (!opts.force && expectedBlockIndex !== blockIndex) {
|
|
245
|
+
throw new BlockIndexNotSequentialError(blockIndex, previousBlockIndex);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
await this.addBlockToDatabase(block, block.checkpointNumber, block.indexWithinCheckpoint);
|
|
249
|
+
|
|
250
|
+
return true;
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Append new checkpoints to the store's list.
|
|
256
|
+
* @param checkpoints - The L2 checkpoints to be added to the store.
|
|
257
|
+
* @returns True if the operation is successful.
|
|
258
|
+
*/
|
|
259
|
+
async addCheckpoints(checkpoints: PublishedCheckpoint[], opts: { force?: boolean } = {}): Promise<boolean> {
|
|
260
|
+
if (checkpoints.length === 0) {
|
|
261
|
+
return true;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
return await this.db.transactionAsync(async () => {
|
|
265
|
+
const firstCheckpointNumber = checkpoints[0].checkpoint.number;
|
|
266
|
+
const previousCheckpointNumber = await this.getLatestCheckpointNumber();
|
|
267
|
+
|
|
268
|
+
// Handle already-stored checkpoints at the start of the batch.
|
|
269
|
+
// This can happen after an L1 reorg re-includes a checkpoint in a different L1 block.
|
|
270
|
+
// We accept them if archives match (same content) and update their L1 metadata.
|
|
271
|
+
if (!opts.force && firstCheckpointNumber <= previousCheckpointNumber) {
|
|
272
|
+
checkpoints = await this.skipOrUpdateAlreadyStoredCheckpoints(checkpoints, previousCheckpointNumber);
|
|
273
|
+
if (checkpoints.length === 0) {
|
|
274
|
+
return true;
|
|
275
|
+
}
|
|
276
|
+
// Re-check sequentiality after skipping
|
|
277
|
+
const newFirstNumber = checkpoints[0].checkpoint.number;
|
|
278
|
+
if (previousCheckpointNumber !== newFirstNumber - 1) {
|
|
279
|
+
throw new InitialCheckpointNumberNotSequentialError(newFirstNumber, previousCheckpointNumber);
|
|
280
|
+
}
|
|
281
|
+
} else if (previousCheckpointNumber !== firstCheckpointNumber - 1 && !opts.force) {
|
|
282
|
+
throw new InitialCheckpointNumberNotSequentialError(firstCheckpointNumber, previousCheckpointNumber);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// Get the last block of the previous checkpoint for archive chaining
|
|
286
|
+
let previousBlock = await this.getPreviousCheckpointBlock(checkpoints[0].checkpoint.number);
|
|
287
|
+
|
|
288
|
+
// Iterate over checkpoints array and insert them, checking that the block numbers are sequential.
|
|
289
|
+
let previousCheckpoint: PublishedCheckpoint | undefined = undefined;
|
|
290
|
+
for (const checkpoint of checkpoints) {
|
|
291
|
+
if (
|
|
292
|
+
!opts.force &&
|
|
293
|
+
previousCheckpoint &&
|
|
294
|
+
previousCheckpoint.checkpoint.number + 1 !== checkpoint.checkpoint.number
|
|
295
|
+
) {
|
|
296
|
+
throw new CheckpointNumberNotSequentialError(
|
|
297
|
+
checkpoint.checkpoint.number,
|
|
298
|
+
previousCheckpoint.checkpoint.number,
|
|
299
|
+
);
|
|
300
|
+
}
|
|
301
|
+
previousCheckpoint = checkpoint;
|
|
302
|
+
|
|
303
|
+
// Validate block sequencing, indexes, and archive chaining
|
|
304
|
+
this.validateCheckpointBlocks(checkpoint.checkpoint.blocks, previousBlock);
|
|
305
|
+
|
|
306
|
+
// Store every block in the database (may already exist, but L1 data is authoritative)
|
|
307
|
+
for (let i = 0; i < checkpoint.checkpoint.blocks.length; i++) {
|
|
308
|
+
await this.addBlockToDatabase(checkpoint.checkpoint.blocks[i], checkpoint.checkpoint.number, i);
|
|
309
|
+
}
|
|
310
|
+
previousBlock = checkpoint.checkpoint.blocks.at(-1);
|
|
311
|
+
|
|
312
|
+
// Store the checkpoint in the database
|
|
313
|
+
await this.#checkpoints.set(checkpoint.checkpoint.number, {
|
|
314
|
+
header: checkpoint.checkpoint.header.toBuffer(),
|
|
315
|
+
archive: checkpoint.checkpoint.archive.toBuffer(),
|
|
316
|
+
checkpointOutHash: checkpoint.checkpoint.getCheckpointOutHash().toBuffer(),
|
|
317
|
+
l1: checkpoint.l1.toBuffer(),
|
|
318
|
+
attestations: checkpoint.attestations.map(attestation => attestation.toBuffer()),
|
|
319
|
+
checkpointNumber: checkpoint.checkpoint.number,
|
|
320
|
+
startBlock: checkpoint.checkpoint.blocks[0].number,
|
|
321
|
+
blockCount: checkpoint.checkpoint.blocks.length,
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
// Update slot-to-checkpoint index
|
|
325
|
+
await this.#slotToCheckpoint.set(checkpoint.checkpoint.header.slotNumber, checkpoint.checkpoint.number);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// Clear the proposed checkpoint if any of the confirmed checkpoints match or supersede it
|
|
329
|
+
const lastConfirmedCheckpointNumber = checkpoints[checkpoints.length - 1].checkpoint.number;
|
|
330
|
+
await this.clearProposedCheckpointIfSuperseded(lastConfirmedCheckpointNumber);
|
|
331
|
+
|
|
332
|
+
await this.#lastSynchedL1Block.set(checkpoints[checkpoints.length - 1].l1.blockNumber);
|
|
333
|
+
return true;
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* Handles checkpoints at the start of a batch that are already stored (e.g. due to L1 reorg).
|
|
339
|
+
* Verifies the archive root matches, updates L1 metadata, and returns only the new checkpoints.
|
|
340
|
+
*/
|
|
341
|
+
private async skipOrUpdateAlreadyStoredCheckpoints(
|
|
342
|
+
checkpoints: PublishedCheckpoint[],
|
|
343
|
+
latestStored: CheckpointNumber,
|
|
344
|
+
): Promise<PublishedCheckpoint[]> {
|
|
345
|
+
let i = 0;
|
|
346
|
+
for (; i < checkpoints.length && checkpoints[i].checkpoint.number <= latestStored; i++) {
|
|
347
|
+
const incoming = checkpoints[i];
|
|
348
|
+
const stored = await this.getCheckpointData(incoming.checkpoint.number);
|
|
349
|
+
if (!stored) {
|
|
350
|
+
// Should not happen if latestStored is correct, but be safe
|
|
351
|
+
break;
|
|
352
|
+
}
|
|
353
|
+
// Verify the checkpoint content matches (archive root)
|
|
354
|
+
if (!stored.archive.root.equals(incoming.checkpoint.archive.root)) {
|
|
355
|
+
throw new Error(
|
|
356
|
+
`Checkpoint ${incoming.checkpoint.number} already exists in store but with a different archive root. ` +
|
|
357
|
+
`Stored: ${stored.archive.root}, incoming: ${incoming.checkpoint.archive.root}`,
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
// Update L1 metadata and attestations for the already-stored checkpoint
|
|
361
|
+
this.#log.warn(
|
|
362
|
+
`Checkpoint ${incoming.checkpoint.number} already stored, updating L1 info ` +
|
|
363
|
+
`(L1 block ${stored.l1.blockNumber} -> ${incoming.l1.blockNumber})`,
|
|
364
|
+
);
|
|
365
|
+
await this.#checkpoints.set(incoming.checkpoint.number, {
|
|
366
|
+
header: incoming.checkpoint.header.toBuffer(),
|
|
367
|
+
archive: incoming.checkpoint.archive.toBuffer(),
|
|
368
|
+
checkpointOutHash: incoming.checkpoint.getCheckpointOutHash().toBuffer(),
|
|
369
|
+
l1: incoming.l1.toBuffer(),
|
|
370
|
+
attestations: incoming.attestations.map(a => a.toBuffer()),
|
|
371
|
+
checkpointNumber: incoming.checkpoint.number,
|
|
372
|
+
startBlock: incoming.checkpoint.blocks[0].number,
|
|
373
|
+
blockCount: incoming.checkpoint.blocks.length,
|
|
374
|
+
});
|
|
375
|
+
// Update the sync point to reflect the new L1 block
|
|
376
|
+
await this.#lastSynchedL1Block.set(incoming.l1.blockNumber);
|
|
377
|
+
}
|
|
378
|
+
return checkpoints.slice(i);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* Gets the last block of the checkpoint before the given one.
|
|
383
|
+
* Returns undefined if there is no previous checkpoint (i.e. genesis).
|
|
384
|
+
*/
|
|
385
|
+
private async getPreviousCheckpointBlock(checkpointNumber: CheckpointNumber): Promise<L2Block | undefined> {
|
|
386
|
+
const previousCheckpointNumber = CheckpointNumber(checkpointNumber - 1);
|
|
387
|
+
if (previousCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
|
|
388
|
+
return undefined;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
const previousCheckpointData = await this.getCheckpointData(previousCheckpointNumber);
|
|
392
|
+
if (previousCheckpointData === undefined) {
|
|
393
|
+
throw new CheckpointNotFoundError(previousCheckpointNumber);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
const previousBlockNumber = BlockNumber(previousCheckpointData.startBlock + previousCheckpointData.blockCount - 1);
|
|
397
|
+
const previousBlock = await this.getBlock(previousBlockNumber);
|
|
398
|
+
if (previousBlock === undefined) {
|
|
399
|
+
throw new BlockNotFoundError(previousBlockNumber);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
return previousBlock;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* Validates that blocks are sequential, have correct indexes, and chain via archive roots.
|
|
407
|
+
* This is the same validation used for both confirmed checkpoints (addCheckpoints) and
|
|
408
|
+
* proposed checkpoints (setProposedCheckpoint).
|
|
409
|
+
*/
|
|
410
|
+
private validateCheckpointBlocks(blocks: L2Block[], previousBlock: L2Block | undefined): void {
|
|
411
|
+
for (const block of blocks) {
|
|
412
|
+
if (previousBlock) {
|
|
413
|
+
if (previousBlock.number !== block.number - 1) {
|
|
414
|
+
throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
|
|
415
|
+
}
|
|
416
|
+
if (previousBlock.checkpointNumber === block.checkpointNumber) {
|
|
417
|
+
if (previousBlock.indexWithinCheckpoint !== block.indexWithinCheckpoint - 1) {
|
|
418
|
+
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
|
|
419
|
+
}
|
|
420
|
+
} else if (block.indexWithinCheckpoint !== 0) {
|
|
421
|
+
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
|
|
422
|
+
}
|
|
423
|
+
if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
|
|
424
|
+
throw new BlockArchiveNotConsistentError(
|
|
425
|
+
block.number,
|
|
426
|
+
previousBlock.number,
|
|
427
|
+
block.header.lastArchive.root,
|
|
428
|
+
previousBlock.archive.root,
|
|
429
|
+
);
|
|
430
|
+
}
|
|
431
|
+
} else {
|
|
432
|
+
if (block.indexWithinCheckpoint !== 0) {
|
|
433
|
+
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, undefined);
|
|
434
|
+
}
|
|
435
|
+
if (block.number !== INITIAL_L2_BLOCK_NUM) {
|
|
436
|
+
throw new BlockNumberNotSequentialError(block.number, undefined);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
previousBlock = block;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
private async addBlockToDatabase(block: L2Block, checkpointNumber: number, indexWithinCheckpoint: number) {
|
|
444
|
+
const blockHash = await block.hash();
|
|
445
|
+
|
|
446
|
+
await this.#blocks.set(block.number, {
|
|
447
|
+
header: block.header.toBuffer(),
|
|
448
|
+
blockHash: blockHash.toBuffer(),
|
|
449
|
+
archive: block.archive.toBuffer(),
|
|
450
|
+
checkpointNumber,
|
|
451
|
+
indexWithinCheckpoint,
|
|
452
|
+
});
|
|
453
|
+
|
|
454
|
+
for (let i = 0; i < block.body.txEffects.length; i++) {
|
|
455
|
+
const txEffect: IndexedTxEffect = {
|
|
456
|
+
data: block.body.txEffects[i],
|
|
457
|
+
l2BlockNumber: block.number,
|
|
458
|
+
l2BlockHash: blockHash,
|
|
459
|
+
txIndexInBlock: i,
|
|
460
|
+
};
|
|
461
|
+
await this.#txEffects.set(txEffect.data.txHash.toString(), serializeIndexedTxEffect(txEffect));
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
await this.#blockTxs.set(blockHash.toString(), Buffer.concat(block.body.txEffects.map(tx => tx.txHash.toBuffer())));
|
|
465
|
+
|
|
466
|
+
// Update indices for block hash and archive
|
|
467
|
+
await this.#blockHashIndex.set(blockHash.toString(), block.number);
|
|
468
|
+
await this.#blockArchiveIndex.set(block.archive.root.toString(), block.number);
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/** Deletes a block and all associated data (tx effects, indices). */
|
|
472
|
+
private async deleteBlock(block: L2Block): Promise<void> {
|
|
473
|
+
// Delete the block from the main blocks map
|
|
474
|
+
await this.#blocks.delete(block.number);
|
|
475
|
+
|
|
476
|
+
// Delete all tx effects for this block
|
|
477
|
+
await Promise.all(block.body.txEffects.map(tx => this.#txEffects.delete(tx.txHash.toString())));
|
|
478
|
+
|
|
479
|
+
// Delete block txs mapping
|
|
480
|
+
const blockHash = (await block.hash()).toString();
|
|
481
|
+
await this.#blockTxs.delete(blockHash);
|
|
482
|
+
|
|
483
|
+
// Clean up indices
|
|
484
|
+
await this.#blockHashIndex.delete(blockHash);
|
|
485
|
+
await this.#blockArchiveIndex.delete(block.archive.root.toString());
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
/**
|
|
489
|
+
* Removes all checkpoints with checkpoint number > checkpointNumber.
|
|
490
|
+
* Also removes ALL blocks (both checkpointed and uncheckpointed) after the last block of the given checkpoint.
|
|
491
|
+
* @param checkpointNumber - Remove all checkpoints strictly after this one.
|
|
492
|
+
*/
|
|
493
|
+
async removeCheckpointsAfter(checkpointNumber: CheckpointNumber): Promise<RemoveCheckpointsResult> {
|
|
494
|
+
return await this.db.transactionAsync(async () => {
|
|
495
|
+
const latestCheckpointNumber = await this.getLatestCheckpointNumber();
|
|
496
|
+
|
|
497
|
+
if (checkpointNumber >= latestCheckpointNumber) {
|
|
498
|
+
this.#log.warn(`No checkpoints to remove after ${checkpointNumber} (latest is ${latestCheckpointNumber})`);
|
|
499
|
+
return { blocksRemoved: undefined };
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
// If the proven checkpoint is beyond the target, update it
|
|
503
|
+
const proven = await this.getProvenCheckpointNumber();
|
|
504
|
+
if (proven > checkpointNumber) {
|
|
505
|
+
this.#log.warn(`Updating proven checkpoint ${proven} to last valid checkpoint ${checkpointNumber}`);
|
|
506
|
+
await this.setProvenCheckpointNumber(checkpointNumber);
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
// Find the last block number to keep (last block of the given checkpoint, or 0 if no checkpoint)
|
|
510
|
+
let lastBlockToKeep: BlockNumber;
|
|
511
|
+
if (checkpointNumber <= 0) {
|
|
512
|
+
lastBlockToKeep = BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
513
|
+
} else {
|
|
514
|
+
const targetCheckpoint = await this.#checkpoints.getAsync(checkpointNumber);
|
|
515
|
+
if (!targetCheckpoint) {
|
|
516
|
+
throw new Error(`Target checkpoint ${checkpointNumber} not found in store`);
|
|
517
|
+
}
|
|
518
|
+
lastBlockToKeep = BlockNumber(targetCheckpoint.startBlock + targetCheckpoint.blockCount - 1);
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
// Remove all blocks after lastBlockToKeep (both checkpointed and uncheckpointed)
|
|
522
|
+
const blocksRemoved = await this.removeBlocksAfter(lastBlockToKeep);
|
|
523
|
+
|
|
524
|
+
// Remove all checkpoints after the target
|
|
525
|
+
for (let c = latestCheckpointNumber; c > checkpointNumber; c = CheckpointNumber(c - 1)) {
|
|
526
|
+
const checkpointStorage = await this.#checkpoints.getAsync(c);
|
|
527
|
+
if (checkpointStorage) {
|
|
528
|
+
const slotNumber = CheckpointHeader.fromBuffer(checkpointStorage.header).slotNumber;
|
|
529
|
+
await this.#slotToCheckpoint.delete(slotNumber);
|
|
530
|
+
}
|
|
531
|
+
await this.#checkpoints.delete(c);
|
|
532
|
+
this.#log.debug(`Removed checkpoint ${c}`);
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
// Clear any proposed checkpoint that was orphaned by the removal (its base chain no longer exists)
|
|
536
|
+
const proposedCheckpointNumber = await this.getProposedCheckpointNumber();
|
|
537
|
+
if (proposedCheckpointNumber > checkpointNumber) {
|
|
538
|
+
await this.#proposedCheckpoint.delete();
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
return { blocksRemoved };
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
async getCheckpointData(checkpointNumber: CheckpointNumber): Promise<CheckpointData | undefined> {
|
|
546
|
+
const checkpointStorage = await this.#checkpoints.getAsync(checkpointNumber);
|
|
547
|
+
if (!checkpointStorage) {
|
|
548
|
+
return undefined;
|
|
549
|
+
}
|
|
550
|
+
return this.checkpointDataFromCheckpointStorage(checkpointStorage);
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
async getRangeOfCheckpoints(from: CheckpointNumber, limit: number): Promise<CheckpointData[]> {
|
|
554
|
+
const checkpoints: CheckpointData[] = [];
|
|
555
|
+
for (let checkpointNumber = from; checkpointNumber < from + limit; checkpointNumber++) {
|
|
556
|
+
const checkpoint = await this.#checkpoints.getAsync(checkpointNumber);
|
|
557
|
+
if (!checkpoint) {
|
|
558
|
+
break;
|
|
559
|
+
}
|
|
560
|
+
checkpoints.push(this.checkpointDataFromCheckpointStorage(checkpoint));
|
|
561
|
+
}
|
|
562
|
+
return checkpoints;
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
/** Returns checkpoint data for all checkpoints whose slot falls within the given range (inclusive). */
|
|
566
|
+
async getCheckpointDataForSlotRange(startSlot: SlotNumber, endSlot: SlotNumber): Promise<CheckpointData[]> {
|
|
567
|
+
const result: CheckpointData[] = [];
|
|
568
|
+
for await (const [, checkpointNumber] of this.#slotToCheckpoint.entriesAsync({
|
|
569
|
+
start: startSlot,
|
|
570
|
+
end: endSlot + 1,
|
|
571
|
+
})) {
|
|
572
|
+
const checkpointStorage = await this.#checkpoints.getAsync(checkpointNumber);
|
|
573
|
+
if (checkpointStorage) {
|
|
574
|
+
result.push(this.checkpointDataFromCheckpointStorage(checkpointStorage));
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
return result;
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
private checkpointDataFromCheckpointStorage(checkpointStorage: CheckpointStorage): CheckpointData {
|
|
581
|
+
return {
|
|
582
|
+
header: CheckpointHeader.fromBuffer(checkpointStorage.header),
|
|
583
|
+
archive: AppendOnlyTreeSnapshot.fromBuffer(checkpointStorage.archive),
|
|
584
|
+
checkpointOutHash: Fr.fromBuffer(checkpointStorage.checkpointOutHash),
|
|
585
|
+
checkpointNumber: CheckpointNumber(checkpointStorage.checkpointNumber),
|
|
586
|
+
startBlock: BlockNumber(checkpointStorage.startBlock),
|
|
587
|
+
blockCount: checkpointStorage.blockCount,
|
|
588
|
+
l1: L1PublishedData.fromBuffer(checkpointStorage.l1),
|
|
589
|
+
attestations: checkpointStorage.attestations.map(buf => CommitteeAttestation.fromBuffer(buf)),
|
|
590
|
+
};
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
async getBlocksForCheckpoint(checkpointNumber: CheckpointNumber): Promise<L2Block[] | undefined> {
|
|
594
|
+
const checkpoint = await this.#checkpoints.getAsync(checkpointNumber);
|
|
595
|
+
if (!checkpoint) {
|
|
596
|
+
return undefined;
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
const blocksForCheckpoint = await toArray(
|
|
600
|
+
this.#blocks.entriesAsync({
|
|
601
|
+
start: checkpoint.startBlock,
|
|
602
|
+
end: checkpoint.startBlock + checkpoint.blockCount,
|
|
603
|
+
}),
|
|
604
|
+
);
|
|
605
|
+
|
|
606
|
+
const converted = await Promise.all(blocksForCheckpoint.map(x => this.getBlockFromBlockStorage(x[0], x[1])));
|
|
607
|
+
return converted.filter(isDefined);
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
/**
|
|
611
|
+
* Gets all blocks that have the given slot number.
|
|
612
|
+
* Iterates backwards through blocks for efficiency since we usually query for the last slot.
|
|
613
|
+
* @param slotNumber - The slot number to search for.
|
|
614
|
+
* @returns All blocks with the given slot number, in ascending block number order.
|
|
615
|
+
*/
|
|
616
|
+
async getBlocksForSlot(slotNumber: SlotNumber): Promise<L2Block[]> {
|
|
617
|
+
const blocks: L2Block[] = [];
|
|
618
|
+
|
|
619
|
+
// Iterate backwards through all blocks and filter by slot number
|
|
620
|
+
// This is more efficient since we usually query for the most recent slot
|
|
621
|
+
for await (const [blockNumber, blockStorage] of this.#blocks.entriesAsync({ reverse: true })) {
|
|
622
|
+
const block = await this.getBlockFromBlockStorage(blockNumber, blockStorage);
|
|
623
|
+
const blockSlot = block?.header.globalVariables.slotNumber;
|
|
624
|
+
if (block && blockSlot === slotNumber) {
|
|
625
|
+
blocks.push(block);
|
|
626
|
+
} else if (blockSlot && blockSlot < slotNumber) {
|
|
627
|
+
break; // Blocks are stored in slot ascending order, so we can stop searching
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
// Reverse to return blocks in ascending order (block number order)
|
|
632
|
+
return blocks.reverse();
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
/**
|
|
636
|
+
* Removes all blocks with block number > blockNumber.
|
|
637
|
+
* Does not remove any associated checkpoints.
|
|
638
|
+
* @param blockNumber - The block number to remove after.
|
|
639
|
+
* @returns The removed blocks (for event emission).
|
|
640
|
+
*/
|
|
641
|
+
async removeBlocksAfter(blockNumber: BlockNumber): Promise<L2Block[]> {
|
|
642
|
+
return await this.db.transactionAsync(async () => {
|
|
643
|
+
const removedBlocks: L2Block[] = [];
|
|
644
|
+
|
|
645
|
+
// Get the latest block number to determine the range
|
|
646
|
+
const latestBlockNumber = await this.getLatestL2BlockNumber();
|
|
647
|
+
|
|
648
|
+
// Iterate from blockNumber + 1 to latestBlockNumber
|
|
649
|
+
for (let bn = blockNumber + 1; bn <= latestBlockNumber; bn++) {
|
|
650
|
+
const block = await this.getBlock(BlockNumber(bn));
|
|
651
|
+
|
|
652
|
+
if (block === undefined) {
|
|
653
|
+
this.#log.warn(`Cannot remove block ${bn} from the store since we don't have it`);
|
|
654
|
+
continue;
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
removedBlocks.push(block);
|
|
658
|
+
await this.deleteBlock(block);
|
|
659
|
+
this.#log.debug(`Removed block ${bn} ${(await block.hash()).toString()}`);
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
return removedBlocks;
|
|
663
|
+
});
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
async getProvenBlockNumber(): Promise<BlockNumber> {
|
|
667
|
+
const provenCheckpointNumber = await this.getProvenCheckpointNumber();
|
|
668
|
+
if (provenCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
|
|
669
|
+
return BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
670
|
+
}
|
|
671
|
+
const checkpointStorage = await this.#checkpoints.getAsync(provenCheckpointNumber);
|
|
672
|
+
if (!checkpointStorage) {
|
|
673
|
+
throw new CheckpointNotFoundError(provenCheckpointNumber);
|
|
674
|
+
} else {
|
|
675
|
+
return BlockNumber(checkpointStorage.startBlock + checkpointStorage.blockCount - 1);
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
async getLatestCheckpointNumber(): Promise<CheckpointNumber> {
|
|
680
|
+
const [latestCheckpointNumber] = await toArray(this.#checkpoints.keysAsync({ reverse: true, limit: 1 }));
|
|
681
|
+
if (latestCheckpointNumber === undefined) {
|
|
682
|
+
return CheckpointNumber(INITIAL_CHECKPOINT_NUMBER - 1);
|
|
683
|
+
}
|
|
684
|
+
return CheckpointNumber(latestCheckpointNumber);
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
async hasProposedCheckpoint(): Promise<boolean> {
|
|
688
|
+
const proposed = await this.#proposedCheckpoint.getAsync();
|
|
689
|
+
return proposed !== undefined;
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
/** Deletes the proposed checkpoint from storage. */
|
|
693
|
+
async deleteProposedCheckpoint(): Promise<void> {
|
|
694
|
+
await this.#proposedCheckpoint.delete();
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
/** Clears the proposed checkpoint if the given confirmed checkpoint number supersedes it. */
|
|
698
|
+
async clearProposedCheckpointIfSuperseded(confirmedCheckpointNumber: CheckpointNumber): Promise<void> {
|
|
699
|
+
const proposedCheckpointNumber = await this.getProposedCheckpointNumber();
|
|
700
|
+
if (proposedCheckpointNumber <= confirmedCheckpointNumber) {
|
|
701
|
+
await this.#proposedCheckpoint.delete();
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
/** Returns the proposed checkpoint data, or undefined if no proposed checkpoint exists. No fallback to confirmed. */
|
|
706
|
+
async getProposedCheckpointOnly(): Promise<ProposedCheckpointData | undefined> {
|
|
707
|
+
const stored = await this.#proposedCheckpoint.getAsync();
|
|
708
|
+
if (!stored) {
|
|
709
|
+
return undefined;
|
|
710
|
+
}
|
|
711
|
+
return this.convertToProposedCheckpointData(stored);
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
/**
|
|
715
|
+
* Gets the checkpoint at the proposed tip
|
|
716
|
+
* - pending checkpoint if it exists
|
|
717
|
+
* - fallsback to latest confirmed checkpoint otherwise
|
|
718
|
+
* @returns CommonCheckpointData
|
|
719
|
+
*/
|
|
720
|
+
async getProposedCheckpoint(): Promise<CommonCheckpointData | undefined> {
|
|
721
|
+
const stored = await this.#proposedCheckpoint.getAsync();
|
|
722
|
+
if (!stored) {
|
|
723
|
+
return this.getCheckpointData(await this.getLatestCheckpointNumber());
|
|
724
|
+
}
|
|
725
|
+
return this.convertToProposedCheckpointData(stored);
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
private convertToProposedCheckpointData(stored: ProposedCheckpointStorage): ProposedCheckpointData {
|
|
729
|
+
return {
|
|
730
|
+
checkpointNumber: CheckpointNumber(stored.checkpointNumber),
|
|
731
|
+
header: CheckpointHeader.fromBuffer(stored.header),
|
|
732
|
+
archive: AppendOnlyTreeSnapshot.fromBuffer(stored.archive),
|
|
733
|
+
checkpointOutHash: Fr.fromBuffer(stored.checkpointOutHash),
|
|
734
|
+
startBlock: BlockNumber(stored.startBlock),
|
|
735
|
+
blockCount: stored.blockCount,
|
|
736
|
+
totalManaUsed: BigInt(stored.totalManaUsed),
|
|
737
|
+
feeAssetPriceModifier: BigInt(stored.feeAssetPriceModifier),
|
|
738
|
+
};
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
/**
|
|
742
|
+
* Attempts to get the proposedCheckpoint's number, if there is not one, then fallback to the latest confirmed checkpoint number.
|
|
743
|
+
* @returns CheckpointNumber
|
|
744
|
+
*/
|
|
745
|
+
async getProposedCheckpointNumber(): Promise<CheckpointNumber> {
|
|
746
|
+
const proposed = await this.getProposedCheckpoint();
|
|
747
|
+
if (!proposed) {
|
|
748
|
+
return await this.getLatestCheckpointNumber();
|
|
749
|
+
}
|
|
750
|
+
return CheckpointNumber(proposed.checkpointNumber);
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
/**
|
|
754
|
+
* Attempts to get the proposedCheckpoint's block number, if there is not one, then fallback to the checkpointed block number
|
|
755
|
+
* @returns BlockNumber
|
|
756
|
+
*/
|
|
757
|
+
async getProposedCheckpointL2BlockNumber(): Promise<BlockNumber> {
|
|
758
|
+
const proposed = await this.getProposedCheckpoint();
|
|
759
|
+
if (!proposed) {
|
|
760
|
+
return await this.getCheckpointedL2BlockNumber();
|
|
761
|
+
}
|
|
762
|
+
return BlockNumber(proposed.startBlock + proposed.blockCount - 1);
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
async getCheckpointedBlock(number: BlockNumber): Promise<CheckpointedL2Block | undefined> {
|
|
766
|
+
const blockStorage = await this.#blocks.getAsync(number);
|
|
767
|
+
if (!blockStorage) {
|
|
768
|
+
return undefined;
|
|
769
|
+
}
|
|
770
|
+
const checkpoint = await this.#checkpoints.getAsync(blockStorage.checkpointNumber);
|
|
771
|
+
if (!checkpoint) {
|
|
772
|
+
return undefined;
|
|
773
|
+
}
|
|
774
|
+
const block = await this.getBlockFromBlockStorage(number, blockStorage);
|
|
775
|
+
if (!block) {
|
|
776
|
+
return undefined;
|
|
777
|
+
}
|
|
778
|
+
return new CheckpointedL2Block(
|
|
779
|
+
CheckpointNumber(checkpoint.checkpointNumber),
|
|
780
|
+
block,
|
|
781
|
+
L1PublishedData.fromBuffer(checkpoint.l1),
|
|
782
|
+
checkpoint.attestations.map(buf => CommitteeAttestation.fromBuffer(buf)),
|
|
783
|
+
);
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
/**
|
|
787
|
+
* Gets up to `limit` amount of Checkpointed L2 blocks starting from `from`.
|
|
788
|
+
* @param start - Number of the first block to return (inclusive).
|
|
789
|
+
* @param limit - The number of blocks to return.
|
|
790
|
+
* @returns The requested L2 blocks
|
|
791
|
+
*/
|
|
792
|
+
async *getCheckpointedBlocks(start: BlockNumber, limit: number): AsyncIterableIterator<CheckpointedL2Block> {
|
|
793
|
+
const checkpointCache = new Map<CheckpointNumber, CheckpointStorage>();
|
|
794
|
+
for await (const [blockNumber, blockStorage] of this.getBlockStorages(start, limit)) {
|
|
795
|
+
const block = await this.getBlockFromBlockStorage(blockNumber, blockStorage);
|
|
796
|
+
if (block) {
|
|
797
|
+
const checkpoint =
|
|
798
|
+
checkpointCache.get(CheckpointNumber(blockStorage.checkpointNumber)) ??
|
|
799
|
+
(await this.#checkpoints.getAsync(blockStorage.checkpointNumber));
|
|
800
|
+
if (checkpoint) {
|
|
801
|
+
checkpointCache.set(CheckpointNumber(blockStorage.checkpointNumber), checkpoint);
|
|
802
|
+
const checkpointedBlock = new CheckpointedL2Block(
|
|
803
|
+
CheckpointNumber(checkpoint.checkpointNumber),
|
|
804
|
+
block,
|
|
805
|
+
L1PublishedData.fromBuffer(checkpoint.l1),
|
|
806
|
+
checkpoint.attestations.map(buf => CommitteeAttestation.fromBuffer(buf)),
|
|
807
|
+
);
|
|
808
|
+
yield checkpointedBlock;
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
async getCheckpointedBlockByHash(blockHash: BlockHash): Promise<CheckpointedL2Block | undefined> {
|
|
815
|
+
const blockNumber = await this.#blockHashIndex.getAsync(blockHash.toString());
|
|
816
|
+
if (blockNumber === undefined) {
|
|
817
|
+
return undefined;
|
|
818
|
+
}
|
|
819
|
+
return this.getCheckpointedBlock(BlockNumber(blockNumber));
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
async getCheckpointedBlockByArchive(archive: Fr): Promise<CheckpointedL2Block | undefined> {
|
|
823
|
+
const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
|
|
824
|
+
if (blockNumber === undefined) {
|
|
825
|
+
return undefined;
|
|
826
|
+
}
|
|
827
|
+
return this.getCheckpointedBlock(BlockNumber(blockNumber));
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
/**
|
|
831
|
+
* Gets up to `limit` amount of L2 blocks starting from `from`.
|
|
832
|
+
* @param start - Number of the first block to return (inclusive).
|
|
833
|
+
* @param limit - The number of blocks to return.
|
|
834
|
+
* @returns The requested L2 blocks
|
|
835
|
+
*/
|
|
836
|
+
async *getBlocks(start: BlockNumber, limit: number): AsyncIterableIterator<L2Block> {
|
|
837
|
+
for await (const [blockNumber, blockStorage] of this.getBlockStorages(start, limit)) {
|
|
838
|
+
const block = await this.getBlockFromBlockStorage(blockNumber, blockStorage);
|
|
839
|
+
if (block) {
|
|
840
|
+
yield block;
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
/**
|
|
846
|
+
* Gets block metadata (without tx data) by block number.
|
|
847
|
+
* @param blockNumber - The number of the block to return.
|
|
848
|
+
* @returns The requested block data.
|
|
849
|
+
*/
|
|
850
|
+
async getBlockData(blockNumber: BlockNumber): Promise<BlockData | undefined> {
|
|
851
|
+
const blockStorage = await this.#blocks.getAsync(blockNumber);
|
|
852
|
+
if (!blockStorage || !blockStorage.header) {
|
|
853
|
+
return undefined;
|
|
854
|
+
}
|
|
855
|
+
return this.getBlockDataFromBlockStorage(blockStorage);
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
/**
|
|
859
|
+
* Gets block metadata (without tx data) by archive root.
|
|
860
|
+
* @param archive - The archive root of the block to return.
|
|
861
|
+
* @returns The requested block data.
|
|
862
|
+
*/
|
|
863
|
+
async getBlockDataByArchive(archive: Fr): Promise<BlockData | undefined> {
|
|
864
|
+
const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
|
|
865
|
+
if (blockNumber === undefined) {
|
|
866
|
+
return undefined;
|
|
867
|
+
}
|
|
868
|
+
return this.getBlockData(BlockNumber(blockNumber));
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
/**
|
|
872
|
+
* Gets an L2 block.
|
|
873
|
+
* @param blockNumber - The number of the block to return.
|
|
874
|
+
* @returns The requested L2 block.
|
|
875
|
+
*/
|
|
876
|
+
async getBlock(blockNumber: BlockNumber): Promise<L2Block | undefined> {
|
|
877
|
+
const blockStorage = await this.#blocks.getAsync(blockNumber);
|
|
878
|
+
if (!blockStorage || !blockStorage.header) {
|
|
879
|
+
return Promise.resolve(undefined);
|
|
880
|
+
}
|
|
881
|
+
return this.getBlockFromBlockStorage(blockNumber, blockStorage);
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
/**
|
|
885
|
+
* Gets an L2 block by its hash.
|
|
886
|
+
* @param blockHash - The hash of the block to return.
|
|
887
|
+
* @returns The requested L2 block.
|
|
888
|
+
*/
|
|
889
|
+
async getBlockByHash(blockHash: BlockHash): Promise<L2Block | undefined> {
|
|
890
|
+
const blockNumber = await this.#blockHashIndex.getAsync(blockHash.toString());
|
|
891
|
+
if (blockNumber === undefined) {
|
|
892
|
+
return undefined;
|
|
893
|
+
}
|
|
894
|
+
return this.getBlock(BlockNumber(blockNumber));
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
/**
|
|
898
|
+
* Gets an L2 block by its archive root.
|
|
899
|
+
* @param archive - The archive root of the block to return.
|
|
900
|
+
* @returns The requested L2 block.
|
|
901
|
+
*/
|
|
902
|
+
async getBlockByArchive(archive: Fr): Promise<L2Block | undefined> {
|
|
903
|
+
const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
|
|
904
|
+
if (blockNumber === undefined) {
|
|
905
|
+
return undefined;
|
|
906
|
+
}
|
|
907
|
+
return this.getBlock(BlockNumber(blockNumber));
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
/**
|
|
911
|
+
* Gets a block header by its hash.
|
|
912
|
+
* @param blockHash - The hash of the block to return.
|
|
913
|
+
* @returns The requested block header.
|
|
914
|
+
*/
|
|
915
|
+
async getBlockHeaderByHash(blockHash: BlockHash): Promise<BlockHeader | undefined> {
|
|
916
|
+
const blockNumber = await this.#blockHashIndex.getAsync(blockHash.toString());
|
|
917
|
+
if (blockNumber === undefined) {
|
|
918
|
+
return undefined;
|
|
919
|
+
}
|
|
920
|
+
const blockStorage = await this.#blocks.getAsync(blockNumber);
|
|
921
|
+
if (!blockStorage || !blockStorage.header) {
|
|
922
|
+
return undefined;
|
|
923
|
+
}
|
|
924
|
+
return BlockHeader.fromBuffer(blockStorage.header);
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
/**
|
|
928
|
+
* Gets a block header by its archive root.
|
|
929
|
+
* @param archive - The archive root of the block to return.
|
|
930
|
+
* @returns The requested block header.
|
|
931
|
+
*/
|
|
932
|
+
async getBlockHeaderByArchive(archive: Fr): Promise<BlockHeader | undefined> {
|
|
933
|
+
const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
|
|
934
|
+
if (blockNumber === undefined) {
|
|
935
|
+
return undefined;
|
|
936
|
+
}
|
|
937
|
+
const blockStorage = await this.#blocks.getAsync(blockNumber);
|
|
938
|
+
if (!blockStorage || !blockStorage.header) {
|
|
939
|
+
return undefined;
|
|
940
|
+
}
|
|
941
|
+
return BlockHeader.fromBuffer(blockStorage.header);
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
/**
|
|
945
|
+
* Gets the headers for a sequence of L2 blocks.
|
|
946
|
+
* @param start - Number of the first block to return (inclusive).
|
|
947
|
+
* @param limit - The number of blocks to return.
|
|
948
|
+
* @returns The requested L2 block headers
|
|
949
|
+
*/
|
|
950
|
+
async *getBlockHeaders(start: BlockNumber, limit: number): AsyncIterableIterator<BlockHeader> {
|
|
951
|
+
for await (const [blockNumber, blockStorage] of this.getBlockStorages(start, limit)) {
|
|
952
|
+
const header = BlockHeader.fromBuffer(blockStorage.header);
|
|
953
|
+
if (header.getBlockNumber() !== blockNumber) {
|
|
954
|
+
throw new Error(
|
|
955
|
+
`Block number mismatch when retrieving block header from archive (expected ${blockNumber} but got ${header.getBlockNumber()})`,
|
|
956
|
+
);
|
|
957
|
+
}
|
|
958
|
+
yield header;
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
private async *getBlockStorages(start: BlockNumber, limit: number) {
|
|
963
|
+
let expectedBlockNumber = start;
|
|
964
|
+
for await (const [blockNumber, blockStorage] of this.#blocks.entriesAsync(this.#computeBlockRange(start, limit))) {
|
|
965
|
+
if (blockNumber !== expectedBlockNumber) {
|
|
966
|
+
throw new Error(
|
|
967
|
+
`Block number mismatch when iterating blocks from archive (expected ${expectedBlockNumber} but got ${blockNumber})`,
|
|
968
|
+
);
|
|
969
|
+
}
|
|
970
|
+
expectedBlockNumber++;
|
|
971
|
+
yield [blockNumber, blockStorage] as const;
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
private getBlockDataFromBlockStorage(blockStorage: BlockStorage): BlockData {
|
|
976
|
+
return {
|
|
977
|
+
header: BlockHeader.fromBuffer(blockStorage.header),
|
|
978
|
+
archive: AppendOnlyTreeSnapshot.fromBuffer(blockStorage.archive),
|
|
979
|
+
blockHash: BlockHash.fromBuffer(blockStorage.blockHash),
|
|
980
|
+
checkpointNumber: CheckpointNumber(blockStorage.checkpointNumber),
|
|
981
|
+
indexWithinCheckpoint: IndexWithinCheckpoint(blockStorage.indexWithinCheckpoint),
|
|
982
|
+
};
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
private async getBlockFromBlockStorage(
|
|
986
|
+
blockNumber: number,
|
|
987
|
+
blockStorage: BlockStorage,
|
|
988
|
+
): Promise<L2Block | undefined> {
|
|
989
|
+
const { header, archive, blockHash, checkpointNumber, indexWithinCheckpoint } =
|
|
990
|
+
this.getBlockDataFromBlockStorage(blockStorage);
|
|
991
|
+
header.setHash(blockHash);
|
|
992
|
+
const blockHashString = bufferToHex(blockStorage.blockHash);
|
|
993
|
+
const blockTxsBuffer = await this.#blockTxs.getAsync(blockHashString);
|
|
994
|
+
if (blockTxsBuffer === undefined) {
|
|
995
|
+
this.#log.warn(`Could not find body for block ${header.globalVariables.blockNumber} ${blockHash}`);
|
|
996
|
+
return undefined;
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
const txEffects: TxEffect[] = [];
|
|
1000
|
+
const reader = BufferReader.asReader(blockTxsBuffer);
|
|
1001
|
+
while (!reader.isEmpty()) {
|
|
1002
|
+
const txHash = reader.readObject(TxHash);
|
|
1003
|
+
const txEffect = await this.#txEffects.getAsync(txHash.toString());
|
|
1004
|
+
if (txEffect === undefined) {
|
|
1005
|
+
this.#log.warn(`Could not find tx effect for tx ${txHash} in block ${blockNumber}`);
|
|
1006
|
+
return undefined;
|
|
1007
|
+
}
|
|
1008
|
+
txEffects.push(deserializeIndexedTxEffect(txEffect).data);
|
|
1009
|
+
}
|
|
1010
|
+
const body = new Body(txEffects);
|
|
1011
|
+
const block = new L2Block(archive, header, body, checkpointNumber, indexWithinCheckpoint);
|
|
1012
|
+
|
|
1013
|
+
if (block.number !== blockNumber) {
|
|
1014
|
+
throw new Error(
|
|
1015
|
+
`Block number mismatch when retrieving block from archive (expected ${blockNumber} but got ${
|
|
1016
|
+
block.number
|
|
1017
|
+
} with hash ${blockHashString})`,
|
|
1018
|
+
);
|
|
1019
|
+
}
|
|
1020
|
+
return block;
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
/**
|
|
1024
|
+
* Gets a tx effect.
|
|
1025
|
+
* @param txHash - The hash of the tx corresponding to the tx effect.
|
|
1026
|
+
* @returns The requested tx effect with block info (or undefined if not found).
|
|
1027
|
+
*/
|
|
1028
|
+
async getTxEffect(txHash: TxHash): Promise<IndexedTxEffect | undefined> {
|
|
1029
|
+
const buffer = await this.#txEffects.getAsync(txHash.toString());
|
|
1030
|
+
if (!buffer) {
|
|
1031
|
+
return undefined;
|
|
1032
|
+
}
|
|
1033
|
+
return deserializeIndexedTxEffect(buffer);
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
/**
|
|
1037
|
+
* Gets a receipt of a settled tx.
|
|
1038
|
+
* @param txHash - The hash of a tx we try to get the receipt for.
|
|
1039
|
+
* @returns The requested tx receipt (or undefined if not found).
|
|
1040
|
+
*/
|
|
1041
|
+
async getSettledTxReceipt(
|
|
1042
|
+
txHash: TxHash,
|
|
1043
|
+
l1Constants?: Pick<L1RollupConstants, 'epochDuration'>,
|
|
1044
|
+
): Promise<TxReceipt | undefined> {
|
|
1045
|
+
const txEffect = await this.getTxEffect(txHash);
|
|
1046
|
+
if (!txEffect) {
|
|
1047
|
+
return undefined;
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
const blockNumber = BlockNumber(txEffect.l2BlockNumber);
|
|
1051
|
+
|
|
1052
|
+
// Use existing archiver methods to determine finalization level
|
|
1053
|
+
const [provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber, blockData] = await Promise.all([
|
|
1054
|
+
this.getProvenBlockNumber(),
|
|
1055
|
+
this.getCheckpointedL2BlockNumber(),
|
|
1056
|
+
this.getFinalizedL2BlockNumber(),
|
|
1057
|
+
this.getBlockData(blockNumber),
|
|
1058
|
+
]);
|
|
1059
|
+
|
|
1060
|
+
let status: TxStatus;
|
|
1061
|
+
if (blockNumber <= finalizedBlockNumber) {
|
|
1062
|
+
status = TxStatus.FINALIZED;
|
|
1063
|
+
} else if (blockNumber <= provenBlockNumber) {
|
|
1064
|
+
status = TxStatus.PROVEN;
|
|
1065
|
+
} else if (blockNumber <= checkpointedBlockNumber) {
|
|
1066
|
+
status = TxStatus.CHECKPOINTED;
|
|
1067
|
+
} else {
|
|
1068
|
+
status = TxStatus.PROPOSED;
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
const epochNumber =
|
|
1072
|
+
blockData && l1Constants ? getEpochAtSlot(blockData.header.globalVariables.slotNumber, l1Constants) : undefined;
|
|
1073
|
+
|
|
1074
|
+
return new TxReceipt(
|
|
1075
|
+
txHash,
|
|
1076
|
+
status,
|
|
1077
|
+
TxReceipt.executionResultFromRevertCode(txEffect.data.revertCode),
|
|
1078
|
+
undefined,
|
|
1079
|
+
txEffect.data.transactionFee.toBigInt(),
|
|
1080
|
+
txEffect.l2BlockHash,
|
|
1081
|
+
blockNumber,
|
|
1082
|
+
epochNumber,
|
|
1083
|
+
);
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
/**
|
|
1087
|
+
* Looks up which block included the requested tx effect.
|
|
1088
|
+
* @param txHash - The txHash of the tx.
|
|
1089
|
+
* @returns The block number and index of the tx.
|
|
1090
|
+
*/
|
|
1091
|
+
public async getTxLocation(txHash: TxHash): Promise<[blockNumber: number, txIndex: number] | undefined> {
|
|
1092
|
+
const txEffect = await this.#txEffects.getAsync(txHash.toString());
|
|
1093
|
+
if (!txEffect) {
|
|
1094
|
+
return undefined;
|
|
1095
|
+
}
|
|
1096
|
+
const { l2BlockNumber, txIndexInBlock } = deserializeIndexedTxEffect(txEffect);
|
|
1097
|
+
return [l2BlockNumber, txIndexInBlock];
|
|
1098
|
+
}
|
|
1099
|
+
|
|
1100
|
+
/**
|
|
1101
|
+
* Looks up which block deployed a particular contract.
|
|
1102
|
+
* @param contractAddress - The address of the contract to look up.
|
|
1103
|
+
* @returns The block number and index of the contract.
|
|
1104
|
+
*/
|
|
1105
|
+
getContractLocation(contractAddress: AztecAddress): Promise<[blockNumber: number, index: number] | undefined> {
|
|
1106
|
+
return this.#contractIndex.getAsync(contractAddress.toString());
|
|
1107
|
+
}
|
|
1108
|
+
|
|
1109
|
+
/**
|
|
1110
|
+
* Gets the number of the latest L2 block checkpointed.
|
|
1111
|
+
* @returns The number of the latest L2 block checkpointed.
|
|
1112
|
+
*/
|
|
1113
|
+
async getCheckpointedL2BlockNumber(): Promise<BlockNumber> {
|
|
1114
|
+
const latestCheckpointNumber = await this.getLatestCheckpointNumber();
|
|
1115
|
+
const checkpoint = await this.getCheckpointData(latestCheckpointNumber);
|
|
1116
|
+
if (!checkpoint) {
|
|
1117
|
+
return BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
1118
|
+
}
|
|
1119
|
+
return BlockNumber(checkpoint.startBlock + checkpoint.blockCount - 1);
|
|
1120
|
+
}
|
|
1121
|
+
|
|
1122
|
+
async getLatestL2BlockNumber(): Promise<BlockNumber> {
|
|
1123
|
+
const [lastBlockNumber] = await toArray(this.#blocks.keysAsync({ reverse: true, limit: 1 }));
|
|
1124
|
+
return typeof lastBlockNumber === 'number' ? BlockNumber(lastBlockNumber) : BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
1125
|
+
}
|
|
1126
|
+
|
|
1127
|
+
/**
|
|
1128
|
+
* Gets the most recent L1 block processed.
|
|
1129
|
+
* @returns The L1 block that published the latest L2 block
|
|
1130
|
+
*/
|
|
1131
|
+
getSynchedL1BlockNumber(): Promise<bigint | undefined> {
|
|
1132
|
+
return this.#lastSynchedL1Block.getAsync();
|
|
1133
|
+
}
|
|
1134
|
+
|
|
1135
|
+
setSynchedL1BlockNumber(l1BlockNumber: bigint) {
|
|
1136
|
+
return this.#lastSynchedL1Block.set(l1BlockNumber);
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
/** Sets the proposed checkpoint (not yet L1-confirmed). Only accepts confirmed + 1.
|
|
1140
|
+
* Computes archive and checkpointOutHash from the stored blocks. */
|
|
1141
|
+
async setProposedCheckpoint(proposed: ProposedCheckpointInput) {
|
|
1142
|
+
return await this.db.transactionAsync(async () => {
|
|
1143
|
+
const current = await this.getProposedCheckpointNumber();
|
|
1144
|
+
if (proposed.checkpointNumber <= current) {
|
|
1145
|
+
throw new ProposedCheckpointStaleError(proposed.checkpointNumber, current);
|
|
1146
|
+
}
|
|
1147
|
+
const confirmed = await this.getLatestCheckpointNumber();
|
|
1148
|
+
if (proposed.checkpointNumber !== confirmed + 1) {
|
|
1149
|
+
throw new ProposedCheckpointNotSequentialError(proposed.checkpointNumber, confirmed);
|
|
1150
|
+
}
|
|
1151
|
+
|
|
1152
|
+
// Ensure the previous checkpoint + blocks exist
|
|
1153
|
+
const previousBlock = await this.getPreviousCheckpointBlock(proposed.checkpointNumber);
|
|
1154
|
+
const blocks: L2Block[] = [];
|
|
1155
|
+
for (let i = 0; i < proposed.blockCount; i++) {
|
|
1156
|
+
const block = await this.getBlock(BlockNumber(proposed.startBlock + i));
|
|
1157
|
+
if (!block) {
|
|
1158
|
+
throw new BlockNotFoundError(proposed.startBlock + i);
|
|
1159
|
+
}
|
|
1160
|
+
blocks.push(block);
|
|
1161
|
+
}
|
|
1162
|
+
this.validateCheckpointBlocks(blocks, previousBlock);
|
|
1163
|
+
|
|
1164
|
+
const archive = blocks[blocks.length - 1].archive;
|
|
1165
|
+
const checkpointOutHash = Checkpoint.getCheckpointOutHash(blocks);
|
|
1166
|
+
|
|
1167
|
+
await this.#proposedCheckpoint.set({
|
|
1168
|
+
header: proposed.header.toBuffer(),
|
|
1169
|
+
archive: archive.toBuffer(),
|
|
1170
|
+
checkpointOutHash: checkpointOutHash.toBuffer(),
|
|
1171
|
+
checkpointNumber: proposed.checkpointNumber,
|
|
1172
|
+
startBlock: proposed.startBlock,
|
|
1173
|
+
blockCount: proposed.blockCount,
|
|
1174
|
+
totalManaUsed: proposed.totalManaUsed.toString(),
|
|
1175
|
+
feeAssetPriceModifier: proposed.feeAssetPriceModifier.toString(),
|
|
1176
|
+
});
|
|
1177
|
+
});
|
|
1178
|
+
}
|
|
1179
|
+
|
|
1180
|
+
async getProvenCheckpointNumber(): Promise<CheckpointNumber> {
|
|
1181
|
+
const [latestCheckpointNumber, provenCheckpointNumber] = await Promise.all([
|
|
1182
|
+
this.getLatestCheckpointNumber(),
|
|
1183
|
+
this.#lastProvenCheckpoint.getAsync(),
|
|
1184
|
+
]);
|
|
1185
|
+
return (provenCheckpointNumber ?? 0) > latestCheckpointNumber
|
|
1186
|
+
? latestCheckpointNumber
|
|
1187
|
+
: CheckpointNumber(provenCheckpointNumber ?? 0);
|
|
1188
|
+
}
|
|
1189
|
+
|
|
1190
|
+
async setProvenCheckpointNumber(checkpointNumber: CheckpointNumber) {
|
|
1191
|
+
const result = await this.#lastProvenCheckpoint.set(checkpointNumber);
|
|
1192
|
+
return result;
|
|
1193
|
+
}
|
|
1194
|
+
|
|
1195
|
+
async getFinalizedCheckpointNumber(): Promise<CheckpointNumber> {
|
|
1196
|
+
const [latestCheckpointNumber, finalizedCheckpointNumber] = await Promise.all([
|
|
1197
|
+
this.getLatestCheckpointNumber(),
|
|
1198
|
+
this.#lastFinalizedCheckpoint.getAsync(),
|
|
1199
|
+
]);
|
|
1200
|
+
return (finalizedCheckpointNumber ?? 0) > latestCheckpointNumber
|
|
1201
|
+
? latestCheckpointNumber
|
|
1202
|
+
: CheckpointNumber(finalizedCheckpointNumber ?? 0);
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1205
|
+
setFinalizedCheckpointNumber(checkpointNumber: CheckpointNumber) {
|
|
1206
|
+
return this.#lastFinalizedCheckpoint.set(checkpointNumber);
|
|
1207
|
+
}
|
|
1208
|
+
|
|
1209
|
+
#computeBlockRange(start: BlockNumber, limit: number): Required<Pick<Range<number>, 'start' | 'limit'>> {
|
|
1210
|
+
if (limit < 1) {
|
|
1211
|
+
throw new Error(`Invalid limit: ${limit}`);
|
|
1212
|
+
}
|
|
1213
|
+
|
|
1214
|
+
if (start < INITIAL_L2_BLOCK_NUM) {
|
|
1215
|
+
throw new Error(`Invalid start: ${start}`);
|
|
1216
|
+
}
|
|
1217
|
+
|
|
1218
|
+
return { start, limit };
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1221
|
+
/**
|
|
1222
|
+
* Gets the pending chain validation status.
|
|
1223
|
+
* @returns The validation status or undefined if not set.
|
|
1224
|
+
*/
|
|
1225
|
+
async getPendingChainValidationStatus(): Promise<ValidateCheckpointResult | undefined> {
|
|
1226
|
+
const buffer = await this.#pendingChainValidationStatus.getAsync();
|
|
1227
|
+
if (!buffer) {
|
|
1228
|
+
return undefined;
|
|
1229
|
+
}
|
|
1230
|
+
return deserializeValidateCheckpointResult(buffer);
|
|
1231
|
+
}
|
|
1232
|
+
|
|
1233
|
+
/**
|
|
1234
|
+
* Sets the pending chain validation status.
|
|
1235
|
+
* @param status - The validation status to store.
|
|
1236
|
+
*/
|
|
1237
|
+
async setPendingChainValidationStatus(status: ValidateCheckpointResult | undefined): Promise<void> {
|
|
1238
|
+
if (status) {
|
|
1239
|
+
const buffer = serializeValidateCheckpointResult(status);
|
|
1240
|
+
await this.#pendingChainValidationStatus.set(buffer);
|
|
1241
|
+
} else {
|
|
1242
|
+
await this.#pendingChainValidationStatus.delete();
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
1245
|
+
}
|