@aztec/archiver 0.0.1-commit.d3ec352c → 0.0.1-commit.e3c1de76

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.
Files changed (205) hide show
  1. package/README.md +156 -22
  2. package/dest/archiver.d.ts +136 -0
  3. package/dest/archiver.d.ts.map +1 -0
  4. package/dest/archiver.js +781 -0
  5. package/dest/config.d.ts +30 -0
  6. package/dest/config.d.ts.map +1 -0
  7. package/dest/{archiver/config.js → config.js} +21 -5
  8. package/dest/errors.d.ts +41 -0
  9. package/dest/errors.d.ts.map +1 -0
  10. package/dest/errors.js +62 -0
  11. package/dest/factory.d.ts +9 -7
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +89 -11
  14. package/dest/index.d.ts +10 -4
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +8 -3
  17. package/dest/interfaces.d.ts +9 -0
  18. package/dest/interfaces.d.ts.map +1 -0
  19. package/dest/interfaces.js +3 -0
  20. package/dest/l1/bin/retrieve-calldata.d.ts +3 -0
  21. package/dest/l1/bin/retrieve-calldata.d.ts.map +1 -0
  22. package/dest/l1/bin/retrieve-calldata.js +149 -0
  23. package/dest/l1/calldata_retriever.d.ts +112 -0
  24. package/dest/l1/calldata_retriever.d.ts.map +1 -0
  25. package/dest/l1/calldata_retriever.js +471 -0
  26. package/dest/l1/data_retrieval.d.ts +88 -0
  27. package/dest/l1/data_retrieval.d.ts.map +1 -0
  28. package/dest/{archiver → l1}/data_retrieval.js +75 -150
  29. package/dest/l1/debug_tx.d.ts +19 -0
  30. package/dest/l1/debug_tx.d.ts.map +1 -0
  31. package/dest/l1/debug_tx.js +73 -0
  32. package/dest/l1/spire_proposer.d.ts +70 -0
  33. package/dest/l1/spire_proposer.d.ts.map +1 -0
  34. package/dest/l1/spire_proposer.js +157 -0
  35. package/dest/l1/trace_tx.d.ts +97 -0
  36. package/dest/l1/trace_tx.d.ts.map +1 -0
  37. package/dest/l1/trace_tx.js +91 -0
  38. package/dest/l1/types.d.ts +12 -0
  39. package/dest/l1/types.d.ts.map +1 -0
  40. package/dest/l1/types.js +3 -0
  41. package/dest/l1/validate_trace.d.ts +32 -0
  42. package/dest/l1/validate_trace.d.ts.map +1 -0
  43. package/dest/l1/validate_trace.js +154 -0
  44. package/dest/modules/data_source_base.d.ts +84 -0
  45. package/dest/modules/data_source_base.d.ts.map +1 -0
  46. package/dest/modules/data_source_base.js +260 -0
  47. package/dest/modules/data_store_updater.d.ts +73 -0
  48. package/dest/modules/data_store_updater.d.ts.map +1 -0
  49. package/dest/modules/data_store_updater.js +302 -0
  50. package/dest/modules/instrumentation.d.ts +37 -0
  51. package/dest/modules/instrumentation.d.ts.map +1 -0
  52. package/dest/{archiver → modules}/instrumentation.js +30 -60
  53. package/dest/modules/l1_synchronizer.d.ts +75 -0
  54. package/dest/modules/l1_synchronizer.d.ts.map +1 -0
  55. package/dest/modules/l1_synchronizer.js +1112 -0
  56. package/dest/modules/validation.d.ts +17 -0
  57. package/dest/modules/validation.d.ts.map +1 -0
  58. package/dest/{archiver → modules}/validation.js +7 -1
  59. package/dest/store/block_store.d.ts +192 -0
  60. package/dest/store/block_store.d.ts.map +1 -0
  61. package/dest/store/block_store.js +721 -0
  62. package/dest/store/contract_class_store.d.ts +18 -0
  63. package/dest/store/contract_class_store.d.ts.map +1 -0
  64. package/dest/{archiver/kv_archiver_store → store}/contract_class_store.js +13 -9
  65. package/dest/store/contract_instance_store.d.ts +24 -0
  66. package/dest/store/contract_instance_store.d.ts.map +1 -0
  67. package/dest/{archiver/kv_archiver_store → store}/contract_instance_store.js +1 -1
  68. package/dest/store/kv_archiver_store.d.ts +340 -0
  69. package/dest/store/kv_archiver_store.d.ts.map +1 -0
  70. package/dest/store/kv_archiver_store.js +446 -0
  71. package/dest/store/log_store.d.ts +54 -0
  72. package/dest/store/log_store.d.ts.map +1 -0
  73. package/dest/store/log_store.js +436 -0
  74. package/dest/store/message_store.d.ts +40 -0
  75. package/dest/store/message_store.d.ts.map +1 -0
  76. package/dest/{archiver/kv_archiver_store → store}/message_store.js +15 -14
  77. package/dest/{archiver/structs → structs}/data_retrieval.d.ts +1 -1
  78. package/dest/structs/data_retrieval.d.ts.map +1 -0
  79. package/dest/structs/inbox_message.d.ts +15 -0
  80. package/dest/structs/inbox_message.d.ts.map +1 -0
  81. package/dest/{archiver/structs → structs}/inbox_message.js +6 -6
  82. package/dest/structs/published.d.ts +2 -0
  83. package/dest/structs/published.d.ts.map +1 -0
  84. package/dest/test/fake_l1_state.d.ts +190 -0
  85. package/dest/test/fake_l1_state.d.ts.map +1 -0
  86. package/dest/test/fake_l1_state.js +383 -0
  87. package/dest/test/index.d.ts +2 -1
  88. package/dest/test/index.d.ts.map +1 -1
  89. package/dest/test/index.js +4 -1
  90. package/dest/test/mock_archiver.d.ts +5 -6
  91. package/dest/test/mock_archiver.d.ts.map +1 -1
  92. package/dest/test/mock_archiver.js +6 -11
  93. package/dest/test/mock_l1_to_l2_message_source.d.ts +6 -7
  94. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
  95. package/dest/test/mock_l1_to_l2_message_source.js +19 -14
  96. package/dest/test/mock_l2_block_source.d.ts +30 -16
  97. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  98. package/dest/test/mock_l2_block_source.js +175 -68
  99. package/dest/test/mock_structs.d.ts +80 -4
  100. package/dest/test/mock_structs.d.ts.map +1 -1
  101. package/dest/test/mock_structs.js +145 -12
  102. package/dest/test/noop_l1_archiver.d.ts +23 -0
  103. package/dest/test/noop_l1_archiver.d.ts.map +1 -0
  104. package/dest/test/noop_l1_archiver.js +68 -0
  105. package/package.json +18 -18
  106. package/src/archiver.ts +543 -0
  107. package/src/{archiver/config.ts → config.ts} +28 -12
  108. package/src/errors.ts +102 -0
  109. package/src/factory.ts +125 -11
  110. package/src/index.ts +10 -3
  111. package/src/interfaces.ts +9 -0
  112. package/src/l1/README.md +98 -0
  113. package/src/l1/bin/retrieve-calldata.ts +187 -0
  114. package/src/l1/calldata_retriever.ts +641 -0
  115. package/src/{archiver → l1}/data_retrieval.ts +138 -220
  116. package/src/l1/debug_tx.ts +99 -0
  117. package/src/l1/spire_proposer.ts +160 -0
  118. package/src/l1/trace_tx.ts +128 -0
  119. package/src/l1/types.ts +13 -0
  120. package/src/l1/validate_trace.ts +229 -0
  121. package/src/modules/data_source_base.ts +367 -0
  122. package/src/modules/data_store_updater.ts +423 -0
  123. package/src/{archiver → modules}/instrumentation.ts +34 -64
  124. package/src/modules/l1_synchronizer.ts +930 -0
  125. package/src/{archiver → modules}/validation.ts +11 -6
  126. package/src/store/block_store.ts +966 -0
  127. package/src/{archiver/kv_archiver_store → store}/contract_class_store.ts +13 -9
  128. package/src/{archiver/kv_archiver_store → store}/contract_instance_store.ts +2 -2
  129. package/src/store/kv_archiver_store.ts +639 -0
  130. package/src/store/log_store.ts +575 -0
  131. package/src/{archiver/kv_archiver_store → store}/message_store.ts +21 -18
  132. package/src/{archiver/structs → structs}/inbox_message.ts +7 -8
  133. package/src/{archiver/structs → structs}/published.ts +0 -1
  134. package/src/test/fake_l1_state.ts +599 -0
  135. package/src/test/fixtures/debug_traceTransaction-multicall3.json +88 -0
  136. package/src/test/fixtures/debug_traceTransaction-multiplePropose.json +153 -0
  137. package/src/test/fixtures/debug_traceTransaction-proxied.json +122 -0
  138. package/src/test/fixtures/trace_transaction-multicall3.json +65 -0
  139. package/src/test/fixtures/trace_transaction-multiplePropose.json +319 -0
  140. package/src/test/fixtures/trace_transaction-proxied.json +128 -0
  141. package/src/test/fixtures/trace_transaction-randomRevert.json +216 -0
  142. package/src/test/index.ts +4 -0
  143. package/src/test/mock_archiver.ts +8 -13
  144. package/src/test/mock_l1_to_l2_message_source.ts +16 -15
  145. package/src/test/mock_l2_block_source.ts +191 -81
  146. package/src/test/mock_structs.ts +275 -14
  147. package/src/test/noop_l1_archiver.ts +109 -0
  148. package/dest/archiver/archiver.d.ts +0 -290
  149. package/dest/archiver/archiver.d.ts.map +0 -1
  150. package/dest/archiver/archiver.js +0 -1434
  151. package/dest/archiver/archiver_store.d.ts +0 -256
  152. package/dest/archiver/archiver_store.d.ts.map +0 -1
  153. package/dest/archiver/archiver_store.js +0 -4
  154. package/dest/archiver/archiver_store_test_suite.d.ts +0 -8
  155. package/dest/archiver/archiver_store_test_suite.d.ts.map +0 -1
  156. package/dest/archiver/archiver_store_test_suite.js +0 -1289
  157. package/dest/archiver/config.d.ts +0 -21
  158. package/dest/archiver/config.d.ts.map +0 -1
  159. package/dest/archiver/data_retrieval.d.ts +0 -80
  160. package/dest/archiver/data_retrieval.d.ts.map +0 -1
  161. package/dest/archiver/errors.d.ts +0 -12
  162. package/dest/archiver/errors.d.ts.map +0 -1
  163. package/dest/archiver/errors.js +0 -17
  164. package/dest/archiver/index.d.ts +0 -7
  165. package/dest/archiver/index.d.ts.map +0 -1
  166. package/dest/archiver/index.js +0 -4
  167. package/dest/archiver/instrumentation.d.ts +0 -35
  168. package/dest/archiver/instrumentation.d.ts.map +0 -1
  169. package/dest/archiver/kv_archiver_store/block_store.d.ts +0 -125
  170. package/dest/archiver/kv_archiver_store/block_store.d.ts.map +0 -1
  171. package/dest/archiver/kv_archiver_store/block_store.js +0 -371
  172. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts +0 -18
  173. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts.map +0 -1
  174. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts +0 -24
  175. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +0 -1
  176. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +0 -169
  177. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +0 -1
  178. package/dest/archiver/kv_archiver_store/kv_archiver_store.js +0 -296
  179. package/dest/archiver/kv_archiver_store/log_store.d.ts +0 -49
  180. package/dest/archiver/kv_archiver_store/log_store.d.ts.map +0 -1
  181. package/dest/archiver/kv_archiver_store/log_store.js +0 -337
  182. package/dest/archiver/kv_archiver_store/message_store.d.ts +0 -39
  183. package/dest/archiver/kv_archiver_store/message_store.d.ts.map +0 -1
  184. package/dest/archiver/structs/data_retrieval.d.ts.map +0 -1
  185. package/dest/archiver/structs/inbox_message.d.ts +0 -15
  186. package/dest/archiver/structs/inbox_message.d.ts.map +0 -1
  187. package/dest/archiver/structs/published.d.ts +0 -3
  188. package/dest/archiver/structs/published.d.ts.map +0 -1
  189. package/dest/archiver/validation.d.ts +0 -17
  190. package/dest/archiver/validation.d.ts.map +0 -1
  191. package/dest/rpc/index.d.ts +0 -9
  192. package/dest/rpc/index.d.ts.map +0 -1
  193. package/dest/rpc/index.js +0 -15
  194. package/src/archiver/archiver.ts +0 -1880
  195. package/src/archiver/archiver_store.ts +0 -310
  196. package/src/archiver/archiver_store_test_suite.ts +0 -1295
  197. package/src/archiver/errors.ts +0 -26
  198. package/src/archiver/index.ts +0 -6
  199. package/src/archiver/kv_archiver_store/block_store.ts +0 -482
  200. package/src/archiver/kv_archiver_store/kv_archiver_store.ts +0 -423
  201. package/src/archiver/kv_archiver_store/log_store.ts +0 -407
  202. package/src/rpc/index.ts +0 -16
  203. /package/dest/{archiver/structs → structs}/data_retrieval.js +0 -0
  204. /package/dest/{archiver/structs → structs}/published.js +0 -0
  205. /package/src/{archiver/structs → structs}/data_retrieval.ts +0 -0
@@ -1,26 +0,0 @@
1
- export class NoBlobBodiesFoundError extends Error {
2
- constructor(l2BlockNum: number) {
3
- super(`No blob bodies found for block ${l2BlockNum}`);
4
- }
5
- }
6
-
7
- export class InitialBlockNumberNotSequentialError extends Error {
8
- constructor(
9
- public readonly newBlockNumber: number,
10
- public readonly previousBlockNumber: number | undefined,
11
- ) {
12
- super(
13
- `Cannot insert new block ${newBlockNumber} given previous block number in store is ${
14
- previousBlockNumber ?? 'undefined'
15
- }`,
16
- );
17
- }
18
- }
19
-
20
- export class BlockNumberNotSequentialError extends Error {
21
- constructor(newBlockNumber: number, previous: number | undefined) {
22
- super(
23
- `Cannot insert new block ${newBlockNumber} given previous block number in batch is ${previous ?? 'undefined'}`,
24
- );
25
- }
26
- }
@@ -1,6 +0,0 @@
1
- export * from './archiver.js';
2
- export * from './config.js';
3
- export { type PublishedL2Block, type L1PublishedData } from './structs/published.js';
4
- export type { ArchiverDataStore } from './archiver_store.js';
5
- export { KVArchiverDataStore, ARCHIVER_DB_VERSION } from './kv_archiver_store/kv_archiver_store.js';
6
- export { ContractInstanceStore } from './kv_archiver_store/contract_instance_store.js';
@@ -1,482 +0,0 @@
1
- import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
2
- import { BlockNumber } from '@aztec/foundation/branded-types';
3
- import { Fr } from '@aztec/foundation/fields';
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 type { AztecAsyncKVStore, AztecAsyncMap, AztecAsyncSingleton, Range } from '@aztec/kv-store';
9
- import type { AztecAddress } from '@aztec/stdlib/aztec-address';
10
- import {
11
- Body,
12
- CommitteeAttestation,
13
- L2Block,
14
- L2BlockHash,
15
- PublishedL2Block,
16
- type ValidateBlockResult,
17
- } from '@aztec/stdlib/block';
18
- import { L2BlockHeader, deserializeValidateBlockResult, serializeValidateBlockResult } from '@aztec/stdlib/block';
19
- import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
20
- import {
21
- BlockHeader,
22
- type IndexedTxEffect,
23
- TxEffect,
24
- TxHash,
25
- TxReceipt,
26
- deserializeIndexedTxEffect,
27
- serializeIndexedTxEffect,
28
- } from '@aztec/stdlib/tx';
29
-
30
- import { BlockNumberNotSequentialError, InitialBlockNumberNotSequentialError } from '../errors.js';
31
- import type { L1PublishedData } from '../structs/published.js';
32
-
33
- export { TxReceipt, type TxEffect, type TxHash } from '@aztec/stdlib/tx';
34
-
35
- type BlockIndexValue = [blockNumber: number, index: number];
36
-
37
- type BlockStorage = {
38
- header: Buffer;
39
- blockHash: Buffer;
40
- archive: Buffer;
41
- l1: L1PublishedData;
42
- attestations: Buffer[];
43
- };
44
-
45
- /**
46
- * LMDB implementation of the ArchiverDataStore interface.
47
- */
48
- export class BlockStore {
49
- /** Map block number to block data */
50
- #blocks: AztecAsyncMap<number, BlockStorage>;
51
-
52
- /** Map block hash to list of tx hashes */
53
- #blockTxs: AztecAsyncMap<string, Buffer>;
54
-
55
- /** Tx hash to serialized IndexedTxEffect */
56
- #txEffects: AztecAsyncMap<string, Buffer>;
57
-
58
- /** Stores L1 block number in which the last processed L2 block was included */
59
- #lastSynchedL1Block: AztecAsyncSingleton<bigint>;
60
-
61
- /** Stores l2 block number of the last proven block */
62
- #lastProvenL2Block: AztecAsyncSingleton<number>;
63
-
64
- /** Stores the pending chain validation status */
65
- #pendingChainValidationStatus: AztecAsyncSingleton<Buffer>;
66
-
67
- /** Index mapping a contract's address (as a string) to its location in a block */
68
- #contractIndex: AztecAsyncMap<string, BlockIndexValue>;
69
-
70
- /** Index mapping block hash to block number */
71
- #blockHashIndex: AztecAsyncMap<string, number>;
72
-
73
- /** Index mapping block archive to block number */
74
- #blockArchiveIndex: AztecAsyncMap<string, number>;
75
-
76
- #log = createLogger('archiver:block_store');
77
-
78
- constructor(private db: AztecAsyncKVStore) {
79
- this.#blocks = db.openMap('archiver_blocks');
80
- this.#blockTxs = db.openMap('archiver_block_txs');
81
- this.#txEffects = db.openMap('archiver_tx_effects');
82
- this.#contractIndex = db.openMap('archiver_contract_index');
83
- this.#blockHashIndex = db.openMap('archiver_block_hash_index');
84
- this.#blockArchiveIndex = db.openMap('archiver_block_archive_index');
85
- this.#lastSynchedL1Block = db.openSingleton('archiver_last_synched_l1_block');
86
- this.#lastProvenL2Block = db.openSingleton('archiver_last_proven_l2_block');
87
- this.#pendingChainValidationStatus = db.openSingleton('archiver_pending_chain_validation_status');
88
- }
89
-
90
- /**
91
- * Append new blocks to the store's list.
92
- * @param blocks - The L2 blocks to be added to the store.
93
- * @returns True if the operation is successful.
94
- */
95
- async addBlocks(blocks: PublishedL2Block[], opts: { force?: boolean } = {}): Promise<boolean> {
96
- if (blocks.length === 0) {
97
- return true;
98
- }
99
-
100
- return await this.db.transactionAsync(async () => {
101
- // Check that the block immediately before the first block to be added is present in the store.
102
- const firstBlockNumber = blocks[0].block.number;
103
- const [previousBlockNumber] = await toArray(
104
- this.#blocks.keysAsync({ reverse: true, limit: 1, end: firstBlockNumber - 1 }),
105
- );
106
- const hasPreviousBlock =
107
- firstBlockNumber === INITIAL_L2_BLOCK_NUM ||
108
- (previousBlockNumber !== undefined && previousBlockNumber === firstBlockNumber - 1);
109
- if (!opts.force && !hasPreviousBlock) {
110
- throw new InitialBlockNumberNotSequentialError(firstBlockNumber, previousBlockNumber);
111
- }
112
-
113
- // Iterate over blocks array and insert them, checking that the block numbers are sequential.
114
- let previousBlock: PublishedL2Block | undefined = undefined;
115
- for (const block of blocks) {
116
- if (!opts.force && previousBlock && previousBlock.block.number + 1 !== block.block.number) {
117
- throw new BlockNumberNotSequentialError(block.block.number, previousBlock.block.number);
118
- }
119
- previousBlock = block;
120
- const blockHash = L2BlockHash.fromField(await block.block.hash());
121
-
122
- await this.#blocks.set(block.block.number, {
123
- header: block.block.header.toBuffer(),
124
- blockHash: blockHash.toBuffer(),
125
- archive: block.block.archive.toBuffer(),
126
- l1: block.l1,
127
- attestations: block.attestations.map(attestation => attestation.toBuffer()),
128
- });
129
-
130
- for (let i = 0; i < block.block.body.txEffects.length; i++) {
131
- const txEffect: IndexedTxEffect = {
132
- data: block.block.body.txEffects[i],
133
- l2BlockNumber: block.block.number,
134
- l2BlockHash: blockHash,
135
- txIndexInBlock: i,
136
- };
137
- await this.#txEffects.set(txEffect.data.txHash.toString(), serializeIndexedTxEffect(txEffect));
138
- }
139
-
140
- await this.#blockTxs.set(
141
- blockHash.toString(),
142
- Buffer.concat(block.block.body.txEffects.map(tx => tx.txHash.toBuffer())),
143
- );
144
-
145
- // Update indices for block hash and archive
146
- await this.#blockHashIndex.set(blockHash.toString(), block.block.number);
147
- await this.#blockArchiveIndex.set(block.block.archive.root.toString(), block.block.number);
148
- }
149
-
150
- await this.#lastSynchedL1Block.set(blocks[blocks.length - 1].l1.blockNumber);
151
- return true;
152
- });
153
- }
154
-
155
- /**
156
- * Unwinds blocks from the database
157
- * @param from - The tip of the chain, passed for verification purposes,
158
- * ensuring that we don't end up deleting something we did not intend
159
- * @param blocksToUnwind - The number of blocks we are to unwind
160
- * @returns True if the operation is successful
161
- */
162
- async unwindBlocks(from: BlockNumber, blocksToUnwind: number) {
163
- return await this.db.transactionAsync(async () => {
164
- const last = await this.getSynchedL2BlockNumber();
165
- if (from !== last) {
166
- throw new Error(`Can only unwind blocks from the tip (requested ${from} but current tip is ${last})`);
167
- }
168
-
169
- const proven = await this.getProvenL2BlockNumber();
170
- if (from - blocksToUnwind < proven) {
171
- await this.setProvenL2BlockNumber(BlockNumber(from - blocksToUnwind));
172
- }
173
-
174
- for (let i = 0; i < blocksToUnwind; i++) {
175
- const blockNumber = from - i;
176
- const block = await this.getBlock(BlockNumber(blockNumber));
177
-
178
- if (block === undefined) {
179
- this.#log.warn(`Cannot remove block ${blockNumber} from the store since we don't have it`);
180
- continue;
181
- }
182
- await this.#blocks.delete(block.block.number);
183
- await Promise.all(block.block.body.txEffects.map(tx => this.#txEffects.delete(tx.txHash.toString())));
184
- const blockHash = (await block.block.hash()).toString();
185
- await this.#blockTxs.delete(blockHash);
186
-
187
- // Clean up indices
188
- await this.#blockHashIndex.delete(blockHash);
189
- await this.#blockArchiveIndex.delete(block.block.archive.root.toString());
190
-
191
- this.#log.debug(`Unwound block ${blockNumber} ${blockHash}`);
192
- }
193
-
194
- return true;
195
- });
196
- }
197
-
198
- /**
199
- * Gets up to `limit` amount of L2 blocks starting from `from`.
200
- * @param start - Number of the first block to return (inclusive).
201
- * @param limit - The number of blocks to return.
202
- * @returns The requested L2 blocks
203
- */
204
- async *getBlocks(start: BlockNumber, limit: number): AsyncIterableIterator<PublishedL2Block> {
205
- for await (const [blockNumber, blockStorage] of this.getBlockStorages(start, limit)) {
206
- const block = await this.getBlockFromBlockStorage(blockNumber, blockStorage);
207
- if (block) {
208
- yield block;
209
- }
210
- }
211
- }
212
-
213
- /**
214
- * Gets an L2 block.
215
- * @param blockNumber - The number of the block to return.
216
- * @returns The requested L2 block.
217
- */
218
- async getBlock(blockNumber: BlockNumber): Promise<PublishedL2Block | undefined> {
219
- const blockStorage = await this.#blocks.getAsync(blockNumber);
220
- if (!blockStorage || !blockStorage.header) {
221
- return Promise.resolve(undefined);
222
- }
223
- return this.getBlockFromBlockStorage(blockNumber, blockStorage);
224
- }
225
-
226
- /**
227
- * Gets an L2 block by its hash.
228
- * @param blockHash - The hash of the block to return.
229
- * @returns The requested L2 block.
230
- */
231
- async getBlockByHash(blockHash: L2BlockHash): Promise<PublishedL2Block | undefined> {
232
- const blockNumber = await this.#blockHashIndex.getAsync(blockHash.toString());
233
- if (blockNumber === undefined) {
234
- return undefined;
235
- }
236
- return this.getBlock(BlockNumber(blockNumber));
237
- }
238
-
239
- /**
240
- * Gets an L2 block by its archive root.
241
- * @param archive - The archive root of the block to return.
242
- * @returns The requested L2 block.
243
- */
244
- async getBlockByArchive(archive: Fr): Promise<PublishedL2Block | undefined> {
245
- const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
246
- if (blockNumber === undefined) {
247
- return undefined;
248
- }
249
- return this.getBlock(BlockNumber(blockNumber));
250
- }
251
-
252
- /**
253
- * Gets a block header by its hash.
254
- * @param blockHash - The hash of the block to return.
255
- * @returns The requested block header.
256
- */
257
- async getBlockHeaderByHash(blockHash: L2BlockHash): Promise<BlockHeader | undefined> {
258
- const blockNumber = await this.#blockHashIndex.getAsync(blockHash.toString());
259
- if (blockNumber === undefined) {
260
- return undefined;
261
- }
262
- const blockStorage = await this.#blocks.getAsync(blockNumber);
263
- if (!blockStorage || !blockStorage.header) {
264
- return undefined;
265
- }
266
- return L2BlockHeader.fromBuffer(blockStorage.header).toBlockHeader();
267
- }
268
-
269
- /**
270
- * Gets a block header by its archive root.
271
- * @param archive - The archive root of the block to return.
272
- * @returns The requested block header.
273
- */
274
- async getBlockHeaderByArchive(archive: Fr): Promise<BlockHeader | undefined> {
275
- const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
276
- if (blockNumber === undefined) {
277
- return undefined;
278
- }
279
- const blockStorage = await this.#blocks.getAsync(blockNumber);
280
- if (!blockStorage || !blockStorage.header) {
281
- return undefined;
282
- }
283
- return L2BlockHeader.fromBuffer(blockStorage.header).toBlockHeader();
284
- }
285
-
286
- /**
287
- * Gets the headers for a sequence of L2 blocks.
288
- * @param start - Number of the first block to return (inclusive).
289
- * @param limit - The number of blocks to return.
290
- * @returns The requested L2 block headers
291
- */
292
- async *getBlockHeaders(start: BlockNumber, limit: number): AsyncIterableIterator<BlockHeader> {
293
- for await (const [blockNumber, blockStorage] of this.getBlockStorages(start, limit)) {
294
- const header = L2BlockHeader.fromBuffer(blockStorage.header).toBlockHeader();
295
- if (header.getBlockNumber() !== blockNumber) {
296
- throw new Error(
297
- `Block number mismatch when retrieving block header from archive (expected ${blockNumber} but got ${header.getBlockNumber()})`,
298
- );
299
- }
300
- yield header;
301
- }
302
- }
303
-
304
- private async *getBlockStorages(start: BlockNumber, limit: number) {
305
- let expectedBlockNumber = start;
306
- for await (const [blockNumber, blockStorage] of this.#blocks.entriesAsync(this.#computeBlockRange(start, limit))) {
307
- if (blockNumber !== expectedBlockNumber) {
308
- throw new Error(
309
- `Block number mismatch when iterating blocks from archive (expected ${expectedBlockNumber} but got ${blockNumber})`,
310
- );
311
- }
312
- expectedBlockNumber++;
313
- yield [blockNumber, blockStorage] as const;
314
- }
315
- }
316
-
317
- private async getBlockFromBlockStorage(
318
- blockNumber: number,
319
- blockStorage: BlockStorage,
320
- ): Promise<PublishedL2Block | undefined> {
321
- const header = L2BlockHeader.fromBuffer(blockStorage.header);
322
- const archive = AppendOnlyTreeSnapshot.fromBuffer(blockStorage.archive);
323
- const blockHash = blockStorage.blockHash;
324
- const blockHashString = bufferToHex(blockHash);
325
- const blockTxsBuffer = await this.#blockTxs.getAsync(blockHashString);
326
- if (blockTxsBuffer === undefined) {
327
- this.#log.warn(`Could not find body for block ${header.globalVariables.blockNumber} ${blockHash}`);
328
- return undefined;
329
- }
330
-
331
- const txEffects: TxEffect[] = [];
332
- const reader = BufferReader.asReader(blockTxsBuffer);
333
- while (!reader.isEmpty()) {
334
- const txHash = reader.readObject(TxHash);
335
- const txEffect = await this.#txEffects.getAsync(txHash.toString());
336
- if (txEffect === undefined) {
337
- this.#log.warn(`Could not find tx effect for tx ${txHash} in block ${blockNumber}`);
338
- return undefined;
339
- }
340
- txEffects.push(deserializeIndexedTxEffect(txEffect).data);
341
- }
342
- const body = new Body(txEffects);
343
- const block = new L2Block(archive, header, body, Fr.fromBuffer(blockHash));
344
-
345
- if (block.number !== blockNumber) {
346
- throw new Error(
347
- `Block number mismatch when retrieving block from archive (expected ${blockNumber} but got ${
348
- block.number
349
- } with hash ${blockHashString})`,
350
- );
351
- }
352
- const attestations = blockStorage.attestations.map(CommitteeAttestation.fromBuffer);
353
- return PublishedL2Block.fromFields({ block, l1: blockStorage.l1, attestations });
354
- }
355
-
356
- /**
357
- * Gets a tx effect.
358
- * @param txHash - The hash of the tx corresponding to the tx effect.
359
- * @returns The requested tx effect with block info (or undefined if not found).
360
- */
361
- async getTxEffect(txHash: TxHash): Promise<IndexedTxEffect | undefined> {
362
- const buffer = await this.#txEffects.getAsync(txHash.toString());
363
- if (!buffer) {
364
- return undefined;
365
- }
366
- return deserializeIndexedTxEffect(buffer);
367
- }
368
-
369
- /**
370
- * Gets a receipt of a settled tx.
371
- * @param txHash - The hash of a tx we try to get the receipt for.
372
- * @returns The requested tx receipt (or undefined if not found).
373
- */
374
- async getSettledTxReceipt(txHash: TxHash): Promise<TxReceipt | undefined> {
375
- const txEffect = await this.getTxEffect(txHash);
376
- if (!txEffect) {
377
- return undefined;
378
- }
379
-
380
- return new TxReceipt(
381
- txHash,
382
- TxReceipt.statusFromRevertCode(txEffect.data.revertCode),
383
- '',
384
- txEffect.data.transactionFee.toBigInt(),
385
- txEffect.l2BlockHash,
386
- BlockNumber(txEffect.l2BlockNumber),
387
- );
388
- }
389
-
390
- /**
391
- * Looks up which block included the requested tx effect.
392
- * @param txHash - The txHash of the tx.
393
- * @returns The block number and index of the tx.
394
- */
395
- public async getTxLocation(txHash: TxHash): Promise<[blockNumber: number, txIndex: number] | undefined> {
396
- const txEffect = await this.#txEffects.getAsync(txHash.toString());
397
- if (!txEffect) {
398
- return undefined;
399
- }
400
- const { l2BlockNumber, txIndexInBlock } = deserializeIndexedTxEffect(txEffect);
401
- return [l2BlockNumber, txIndexInBlock];
402
- }
403
-
404
- /**
405
- * Looks up which block deployed a particular contract.
406
- * @param contractAddress - The address of the contract to look up.
407
- * @returns The block number and index of the contract.
408
- */
409
- getContractLocation(contractAddress: AztecAddress): Promise<[blockNumber: number, index: number] | undefined> {
410
- return this.#contractIndex.getAsync(contractAddress.toString());
411
- }
412
-
413
- /**
414
- * Gets the number of the latest L2 block processed.
415
- * @returns The number of the latest L2 block processed.
416
- */
417
- async getSynchedL2BlockNumber(): Promise<BlockNumber> {
418
- const [lastBlockNumber] = await toArray(this.#blocks.keysAsync({ reverse: true, limit: 1 }));
419
- return typeof lastBlockNumber === 'number' ? BlockNumber(lastBlockNumber) : BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
420
- }
421
-
422
- /**
423
- * Gets the most recent L1 block processed.
424
- * @returns The L1 block that published the latest L2 block
425
- */
426
- getSynchedL1BlockNumber(): Promise<bigint | undefined> {
427
- return this.#lastSynchedL1Block.getAsync();
428
- }
429
-
430
- setSynchedL1BlockNumber(l1BlockNumber: bigint) {
431
- return this.#lastSynchedL1Block.set(l1BlockNumber);
432
- }
433
-
434
- async getProvenL2BlockNumber(): Promise<BlockNumber> {
435
- const [latestBlockNumber, provenBlockNumber] = await Promise.all([
436
- this.getSynchedL2BlockNumber(),
437
- this.#lastProvenL2Block.getAsync(),
438
- ]);
439
- return (provenBlockNumber ?? 0) > latestBlockNumber ? latestBlockNumber : BlockNumber(provenBlockNumber ?? 0);
440
- }
441
-
442
- setProvenL2BlockNumber(blockNumber: BlockNumber) {
443
- return this.#lastProvenL2Block.set(blockNumber);
444
- }
445
-
446
- #computeBlockRange(start: BlockNumber, limit: number): Required<Pick<Range<number>, 'start' | 'limit'>> {
447
- if (limit < 1) {
448
- throw new Error(`Invalid limit: ${limit}`);
449
- }
450
-
451
- if (start < INITIAL_L2_BLOCK_NUM) {
452
- throw new Error(`Invalid start: ${start}`);
453
- }
454
-
455
- return { start, limit };
456
- }
457
-
458
- /**
459
- * Gets the pending chain validation status.
460
- * @returns The validation status or undefined if not set.
461
- */
462
- async getPendingChainValidationStatus(): Promise<ValidateBlockResult | undefined> {
463
- const buffer = await this.#pendingChainValidationStatus.getAsync();
464
- if (!buffer) {
465
- return undefined;
466
- }
467
- return deserializeValidateBlockResult(buffer);
468
- }
469
-
470
- /**
471
- * Sets the pending chain validation status.
472
- * @param status - The validation status to store.
473
- */
474
- async setPendingChainValidationStatus(status: ValidateBlockResult | undefined): Promise<void> {
475
- if (status) {
476
- const buffer = serializeValidateBlockResult(status);
477
- await this.#pendingChainValidationStatus.set(buffer);
478
- } else {
479
- await this.#pendingChainValidationStatus.delete();
480
- }
481
- }
482
- }