@aztec/archiver 0.0.1-commit.fce3e4f → 0.0.1-commit.fffb133c

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (201) hide show
  1. package/README.md +156 -22
  2. package/dest/archiver.d.ts +135 -0
  3. package/dest/archiver.d.ts.map +1 -0
  4. package/dest/archiver.js +768 -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 +7 -7
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +90 -10
  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 +76 -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 +29 -0
  42. package/dest/l1/validate_trace.d.ts.map +1 -0
  43. package/dest/l1/validate_trace.js +150 -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 +22 -59
  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 +1113 -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 +2 -2
  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 +447 -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 -5
  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 +1 -0
  90. package/dest/test/mock_archiver.d.ts +16 -8
  91. package/dest/test/mock_archiver.d.ts.map +1 -1
  92. package/dest/test/mock_archiver.js +18 -14
  93. package/dest/test/mock_l1_to_l2_message_source.d.ts +7 -6
  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 +21 -11
  96. package/dest/test/mock_l2_block_source.d.ts +34 -16
  97. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  98. package/dest/test/mock_l2_block_source.js +179 -59
  99. package/dest/test/mock_structs.d.ts +78 -3
  100. package/dest/test/mock_structs.d.ts.map +1 -1
  101. package/dest/test/mock_structs.js +141 -9
  102. package/package.json +19 -20
  103. package/src/archiver.ts +523 -0
  104. package/src/{archiver/config.ts → config.ts} +28 -12
  105. package/src/errors.ts +102 -0
  106. package/src/factory.ts +125 -10
  107. package/src/index.ts +10 -3
  108. package/src/interfaces.ts +9 -0
  109. package/src/l1/README.md +98 -0
  110. package/src/l1/bin/retrieve-calldata.ts +187 -0
  111. package/src/l1/calldata_retriever.ts +641 -0
  112. package/src/{archiver → l1}/data_retrieval.ts +142 -223
  113. package/src/l1/debug_tx.ts +99 -0
  114. package/src/l1/spire_proposer.ts +160 -0
  115. package/src/l1/trace_tx.ts +128 -0
  116. package/src/l1/types.ts +13 -0
  117. package/src/l1/validate_trace.ts +211 -0
  118. package/src/modules/data_source_base.ts +367 -0
  119. package/src/modules/data_store_updater.ts +423 -0
  120. package/src/{archiver → modules}/instrumentation.ts +26 -61
  121. package/src/modules/l1_synchronizer.ts +931 -0
  122. package/src/{archiver → modules}/validation.ts +11 -6
  123. package/src/store/block_store.ts +966 -0
  124. package/src/{archiver/kv_archiver_store → store}/contract_class_store.ts +2 -2
  125. package/src/{archiver/kv_archiver_store → store}/contract_instance_store.ts +2 -2
  126. package/src/store/kv_archiver_store.ts +639 -0
  127. package/src/store/log_store.ts +575 -0
  128. package/src/{archiver/kv_archiver_store → store}/message_store.ts +21 -18
  129. package/src/{archiver/structs → structs}/inbox_message.ts +8 -8
  130. package/src/{archiver/structs → structs}/published.ts +0 -1
  131. package/src/test/fake_l1_state.ts +599 -0
  132. package/src/test/fixtures/debug_traceTransaction-multicall3.json +88 -0
  133. package/src/test/fixtures/debug_traceTransaction-multiplePropose.json +153 -0
  134. package/src/test/fixtures/debug_traceTransaction-proxied.json +122 -0
  135. package/src/test/fixtures/trace_transaction-multicall3.json +65 -0
  136. package/src/test/fixtures/trace_transaction-multiplePropose.json +319 -0
  137. package/src/test/fixtures/trace_transaction-proxied.json +128 -0
  138. package/src/test/fixtures/trace_transaction-randomRevert.json +216 -0
  139. package/src/test/index.ts +1 -0
  140. package/src/test/mock_archiver.ts +22 -16
  141. package/src/test/mock_l1_to_l2_message_source.ts +18 -11
  142. package/src/test/mock_l2_block_source.ts +195 -69
  143. package/src/test/mock_structs.ts +256 -10
  144. package/dest/archiver/archiver.d.ts +0 -287
  145. package/dest/archiver/archiver.d.ts.map +0 -1
  146. package/dest/archiver/archiver.js +0 -1408
  147. package/dest/archiver/archiver_store.d.ts +0 -255
  148. package/dest/archiver/archiver_store.d.ts.map +0 -1
  149. package/dest/archiver/archiver_store.js +0 -4
  150. package/dest/archiver/archiver_store_test_suite.d.ts +0 -8
  151. package/dest/archiver/archiver_store_test_suite.d.ts.map +0 -1
  152. package/dest/archiver/archiver_store_test_suite.js +0 -1289
  153. package/dest/archiver/config.d.ts +0 -21
  154. package/dest/archiver/config.d.ts.map +0 -1
  155. package/dest/archiver/data_retrieval.d.ts +0 -79
  156. package/dest/archiver/data_retrieval.d.ts.map +0 -1
  157. package/dest/archiver/errors.d.ts +0 -12
  158. package/dest/archiver/errors.d.ts.map +0 -1
  159. package/dest/archiver/errors.js +0 -17
  160. package/dest/archiver/index.d.ts +0 -7
  161. package/dest/archiver/index.d.ts.map +0 -1
  162. package/dest/archiver/index.js +0 -4
  163. package/dest/archiver/instrumentation.d.ts +0 -35
  164. package/dest/archiver/instrumentation.d.ts.map +0 -1
  165. package/dest/archiver/kv_archiver_store/block_store.d.ts +0 -124
  166. package/dest/archiver/kv_archiver_store/block_store.d.ts.map +0 -1
  167. package/dest/archiver/kv_archiver_store/block_store.js +0 -370
  168. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts +0 -18
  169. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts.map +0 -1
  170. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts +0 -24
  171. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +0 -1
  172. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +0 -168
  173. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +0 -1
  174. package/dest/archiver/kv_archiver_store/kv_archiver_store.js +0 -296
  175. package/dest/archiver/kv_archiver_store/log_store.d.ts +0 -49
  176. package/dest/archiver/kv_archiver_store/log_store.d.ts.map +0 -1
  177. package/dest/archiver/kv_archiver_store/log_store.js +0 -336
  178. package/dest/archiver/kv_archiver_store/message_store.d.ts +0 -39
  179. package/dest/archiver/kv_archiver_store/message_store.d.ts.map +0 -1
  180. package/dest/archiver/structs/data_retrieval.d.ts.map +0 -1
  181. package/dest/archiver/structs/inbox_message.d.ts +0 -15
  182. package/dest/archiver/structs/inbox_message.d.ts.map +0 -1
  183. package/dest/archiver/structs/published.d.ts +0 -3
  184. package/dest/archiver/structs/published.d.ts.map +0 -1
  185. package/dest/archiver/validation.d.ts +0 -17
  186. package/dest/archiver/validation.d.ts.map +0 -1
  187. package/dest/rpc/index.d.ts +0 -9
  188. package/dest/rpc/index.d.ts.map +0 -1
  189. package/dest/rpc/index.js +0 -15
  190. package/src/archiver/archiver.ts +0 -1858
  191. package/src/archiver/archiver_store.ts +0 -305
  192. package/src/archiver/archiver_store_test_suite.ts +0 -1264
  193. package/src/archiver/errors.ts +0 -26
  194. package/src/archiver/index.ts +0 -6
  195. package/src/archiver/kv_archiver_store/block_store.ts +0 -481
  196. package/src/archiver/kv_archiver_store/kv_archiver_store.ts +0 -422
  197. package/src/archiver/kv_archiver_store/log_store.ts +0 -406
  198. package/src/rpc/index.ts +0 -16
  199. /package/dest/{archiver/structs → structs}/data_retrieval.js +0 -0
  200. /package/dest/{archiver/structs → structs}/published.js +0 -0
  201. /package/src/{archiver/structs → structs}/data_retrieval.ts +0 -0
@@ -0,0 +1,367 @@
1
+ import { BlockNumber, CheckpointNumber, type EpochNumber, type SlotNumber } from '@aztec/foundation/branded-types';
2
+ import type { Fr } from '@aztec/foundation/curves/bn254';
3
+ import type { EthAddress } from '@aztec/foundation/eth-address';
4
+ import { isDefined } from '@aztec/foundation/types';
5
+ import type { FunctionSelector } from '@aztec/stdlib/abi';
6
+ import type { AztecAddress } from '@aztec/stdlib/aztec-address';
7
+ import { CheckpointedL2Block, CommitteeAttestation, L2Block, type L2Tips } from '@aztec/stdlib/block';
8
+ import { Checkpoint, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
9
+ import type { ContractClassPublic, ContractDataSource, ContractInstanceWithAddress } from '@aztec/stdlib/contract';
10
+ import { type L1RollupConstants, getSlotRangeForEpoch } from '@aztec/stdlib/epoch-helpers';
11
+ import type { GetContractClassLogsResponse, GetPublicLogsResponse } from '@aztec/stdlib/interfaces/client';
12
+ import type { L2LogsSource } from '@aztec/stdlib/interfaces/server';
13
+ import type { LogFilter, SiloedTag, Tag, TxScopedL2Log } from '@aztec/stdlib/logs';
14
+ import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
15
+ import type { CheckpointHeader } from '@aztec/stdlib/rollup';
16
+ import type { BlockHeader, IndexedTxEffect, TxHash, TxReceipt } from '@aztec/stdlib/tx';
17
+ import type { UInt64 } from '@aztec/stdlib/types';
18
+
19
+ import type { ArchiverDataSource } from '../interfaces.js';
20
+ import type { CheckpointData } from '../store/block_store.js';
21
+ import type { KVArchiverDataStore } from '../store/kv_archiver_store.js';
22
+ import type { ValidateCheckpointResult } from './validation.js';
23
+
24
+ /**
25
+ * Abstract base class implementing ArchiverDataSource using a KVArchiverDataStore.
26
+ * Provides implementations for all store-delegating methods and declares abstract methods
27
+ * for L1-dependent functionality that subclasses must implement.
28
+ */
29
+ export abstract class ArchiverDataSourceBase
30
+ implements ArchiverDataSource, L2LogsSource, ContractDataSource, L1ToL2MessageSource
31
+ {
32
+ constructor(
33
+ protected readonly store: KVArchiverDataStore,
34
+ protected readonly l1Constants?: L1RollupConstants,
35
+ ) {}
36
+
37
+ abstract getRollupAddress(): Promise<EthAddress>;
38
+
39
+ abstract getRegistryAddress(): Promise<EthAddress>;
40
+
41
+ abstract getL1Constants(): Promise<L1RollupConstants>;
42
+
43
+ abstract getGenesisValues(): Promise<{ genesisArchiveRoot: Fr }>;
44
+
45
+ abstract getL1Timestamp(): Promise<bigint | undefined>;
46
+
47
+ abstract getL2Tips(): Promise<L2Tips>;
48
+
49
+ abstract getL2SlotNumber(): Promise<SlotNumber | undefined>;
50
+
51
+ abstract getL2EpochNumber(): Promise<EpochNumber | undefined>;
52
+
53
+ abstract isEpochComplete(epochNumber: EpochNumber): Promise<boolean>;
54
+
55
+ abstract syncImmediate(): Promise<void>;
56
+
57
+ public getCheckpointNumber(): Promise<CheckpointNumber> {
58
+ return this.store.getSynchedCheckpointNumber();
59
+ }
60
+
61
+ public getSynchedCheckpointNumber(): Promise<CheckpointNumber> {
62
+ return this.store.getSynchedCheckpointNumber();
63
+ }
64
+
65
+ public getProvenCheckpointNumber(): Promise<CheckpointNumber> {
66
+ return this.store.getProvenCheckpointNumber();
67
+ }
68
+
69
+ public getBlockNumber(): Promise<BlockNumber> {
70
+ return this.store.getLatestBlockNumber();
71
+ }
72
+
73
+ public getProvenBlockNumber(): Promise<BlockNumber> {
74
+ return this.store.getProvenBlockNumber();
75
+ }
76
+
77
+ public async getBlockHeader(number: BlockNumber | 'latest'): Promise<BlockHeader | undefined> {
78
+ const blockNumber = number === 'latest' ? await this.store.getLatestBlockNumber() : number;
79
+ if (blockNumber === 0) {
80
+ return undefined;
81
+ }
82
+ const headers = await this.store.getBlockHeaders(blockNumber, 1);
83
+ return headers.length === 0 ? undefined : headers[0];
84
+ }
85
+
86
+ public getCheckpointedBlock(number: BlockNumber): Promise<CheckpointedL2Block | undefined> {
87
+ return this.store.getCheckpointedBlock(number);
88
+ }
89
+
90
+ public getCheckpointedL2BlockNumber(): Promise<BlockNumber> {
91
+ return this.store.getCheckpointedL2BlockNumber();
92
+ }
93
+
94
+ public getFinalizedL2BlockNumber(): Promise<BlockNumber> {
95
+ return this.store.getFinalizedL2BlockNumber();
96
+ }
97
+
98
+ public async getCheckpointHeader(number: CheckpointNumber | 'latest'): Promise<CheckpointHeader | undefined> {
99
+ if (number === 'latest') {
100
+ number = await this.store.getSynchedCheckpointNumber();
101
+ }
102
+ if (number === 0) {
103
+ return undefined;
104
+ }
105
+ const checkpoint = await this.store.getCheckpointData(number);
106
+ if (!checkpoint) {
107
+ return undefined;
108
+ }
109
+ return checkpoint.header;
110
+ }
111
+
112
+ public async getLastBlockNumberInCheckpoint(checkpointNumber: CheckpointNumber): Promise<BlockNumber | undefined> {
113
+ const checkpointData = await this.store.getCheckpointData(checkpointNumber);
114
+ if (!checkpointData) {
115
+ return undefined;
116
+ }
117
+ return BlockNumber(checkpointData.startBlock + checkpointData.numBlocks - 1);
118
+ }
119
+
120
+ public getCheckpointedBlocks(from: BlockNumber, limit: number): Promise<CheckpointedL2Block[]> {
121
+ return this.store.getCheckpointedBlocks(from, limit);
122
+ }
123
+
124
+ public getBlockHeaderByHash(blockHash: Fr): Promise<BlockHeader | undefined> {
125
+ return this.store.getBlockHeaderByHash(blockHash);
126
+ }
127
+
128
+ public getBlockHeaderByArchive(archive: Fr): Promise<BlockHeader | undefined> {
129
+ return this.store.getBlockHeaderByArchive(archive);
130
+ }
131
+
132
+ public async getL2Block(number: BlockNumber): Promise<L2Block | undefined> {
133
+ // If the number provided is -ve, then return the latest block.
134
+ if (number < 0) {
135
+ number = await this.store.getLatestBlockNumber();
136
+ }
137
+ if (number === 0) {
138
+ return undefined;
139
+ }
140
+ const publishedBlock = await this.store.getBlock(number);
141
+ return publishedBlock;
142
+ }
143
+
144
+ public getTxEffect(txHash: TxHash): Promise<IndexedTxEffect | undefined> {
145
+ return this.store.getTxEffect(txHash);
146
+ }
147
+
148
+ public getSettledTxReceipt(txHash: TxHash): Promise<TxReceipt | undefined> {
149
+ return this.store.getSettledTxReceipt(txHash);
150
+ }
151
+
152
+ public isPendingChainInvalid(): Promise<boolean> {
153
+ return this.getPendingChainValidationStatus().then(status => !status.valid);
154
+ }
155
+
156
+ public async getPendingChainValidationStatus(): Promise<ValidateCheckpointResult> {
157
+ return (await this.store.getPendingChainValidationStatus()) ?? { valid: true };
158
+ }
159
+
160
+ public getPrivateLogsByTags(tags: SiloedTag[], page?: number): Promise<TxScopedL2Log[][]> {
161
+ return this.store.getPrivateLogsByTags(tags, page);
162
+ }
163
+
164
+ public getPublicLogsByTagsFromContract(
165
+ contractAddress: AztecAddress,
166
+ tags: Tag[],
167
+ page?: number,
168
+ ): Promise<TxScopedL2Log[][]> {
169
+ return this.store.getPublicLogsByTagsFromContract(contractAddress, tags, page);
170
+ }
171
+
172
+ public getPublicLogs(filter: LogFilter): Promise<GetPublicLogsResponse> {
173
+ return this.store.getPublicLogs(filter);
174
+ }
175
+
176
+ public getContractClassLogs(filter: LogFilter): Promise<GetContractClassLogsResponse> {
177
+ return this.store.getContractClassLogs(filter);
178
+ }
179
+
180
+ public getContractClass(id: Fr): Promise<ContractClassPublic | undefined> {
181
+ return this.store.getContractClass(id);
182
+ }
183
+
184
+ public getBytecodeCommitment(id: Fr): Promise<Fr | undefined> {
185
+ return this.store.getBytecodeCommitment(id);
186
+ }
187
+
188
+ public async getContract(
189
+ address: AztecAddress,
190
+ maybeTimestamp?: UInt64,
191
+ ): Promise<ContractInstanceWithAddress | undefined> {
192
+ let timestamp;
193
+ if (maybeTimestamp === undefined) {
194
+ const latestBlockHeader = await this.getBlockHeader('latest');
195
+ // If we get undefined block header, it means that the archiver has not yet synced any block so we default to 0.
196
+ timestamp = latestBlockHeader ? latestBlockHeader.globalVariables.timestamp : 0n;
197
+ } else {
198
+ timestamp = maybeTimestamp;
199
+ }
200
+
201
+ return this.store.getContractInstance(address, timestamp);
202
+ }
203
+
204
+ public getContractClassIds(): Promise<Fr[]> {
205
+ return this.store.getContractClassIds();
206
+ }
207
+
208
+ public getDebugFunctionName(address: AztecAddress, selector: FunctionSelector): Promise<string | undefined> {
209
+ return this.store.getDebugFunctionName(address, selector);
210
+ }
211
+
212
+ public registerContractFunctionSignatures(signatures: string[]): Promise<void> {
213
+ return this.store.registerContractFunctionSignatures(signatures);
214
+ }
215
+
216
+ public getL1ToL2Messages(checkpointNumber: CheckpointNumber): Promise<Fr[]> {
217
+ return this.store.getL1ToL2Messages(checkpointNumber);
218
+ }
219
+
220
+ public getL1ToL2MessageIndex(l1ToL2Message: Fr): Promise<bigint | undefined> {
221
+ return this.store.getL1ToL2MessageIndex(l1ToL2Message);
222
+ }
223
+
224
+ public async getCheckpoints(checkpointNumber: CheckpointNumber, limit: number): Promise<PublishedCheckpoint[]> {
225
+ const checkpoints = await this.store.getRangeOfCheckpoints(checkpointNumber, limit);
226
+ const blocks = (
227
+ await Promise.all(checkpoints.map(ch => this.store.getBlocksForCheckpoint(ch.checkpointNumber)))
228
+ ).filter(isDefined);
229
+
230
+ const fullCheckpoints: PublishedCheckpoint[] = [];
231
+ for (let i = 0; i < checkpoints.length; i++) {
232
+ const blocksForCheckpoint = blocks[i];
233
+ const checkpoint = checkpoints[i];
234
+ const fullCheckpoint = new Checkpoint(
235
+ checkpoint.archive,
236
+ checkpoint.header,
237
+ blocksForCheckpoint,
238
+ checkpoint.checkpointNumber,
239
+ );
240
+ const publishedCheckpoint = new PublishedCheckpoint(
241
+ fullCheckpoint,
242
+ checkpoint.l1,
243
+ checkpoint.attestations.map(x => CommitteeAttestation.fromBuffer(x)),
244
+ );
245
+ fullCheckpoints.push(publishedCheckpoint);
246
+ }
247
+ return fullCheckpoints;
248
+ }
249
+
250
+ public getBlocksForSlot(slotNumber: SlotNumber): Promise<L2Block[]> {
251
+ return this.store.getBlocksForSlot(slotNumber);
252
+ }
253
+
254
+ public async getCheckpointedBlocksForEpoch(epochNumber: EpochNumber): Promise<CheckpointedL2Block[]> {
255
+ if (!this.l1Constants) {
256
+ throw new Error('L1 constants not set');
257
+ }
258
+
259
+ const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1Constants);
260
+ const blocks: CheckpointedL2Block[] = [];
261
+
262
+ // Walk the list of checkpoints backwards and filter by slots matching the requested epoch.
263
+ // We'll typically ask for checkpoints for a very recent epoch, so we shouldn't need an index here.
264
+ let checkpoint = await this.store.getCheckpointData(await this.store.getSynchedCheckpointNumber());
265
+ const slot = (b: CheckpointData) => b.header.slotNumber;
266
+ while (checkpoint && slot(checkpoint) >= start) {
267
+ if (slot(checkpoint) <= end) {
268
+ // push the blocks on backwards
269
+ const endBlock = checkpoint.startBlock + checkpoint.numBlocks - 1;
270
+ for (let i = endBlock; i >= checkpoint.startBlock; i--) {
271
+ const checkpointedBlock = await this.getCheckpointedBlock(BlockNumber(i));
272
+ if (checkpointedBlock) {
273
+ blocks.push(checkpointedBlock);
274
+ }
275
+ }
276
+ }
277
+ checkpoint = await this.store.getCheckpointData(CheckpointNumber(checkpoint.checkpointNumber - 1));
278
+ }
279
+
280
+ return blocks.reverse();
281
+ }
282
+
283
+ public async getCheckpointedBlockHeadersForEpoch(epochNumber: EpochNumber): Promise<BlockHeader[]> {
284
+ if (!this.l1Constants) {
285
+ throw new Error('L1 constants not set');
286
+ }
287
+
288
+ const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1Constants);
289
+ const blocks: BlockHeader[] = [];
290
+
291
+ // Walk the list of checkpoints backwards and filter by slots matching the requested epoch.
292
+ // We'll typically ask for checkpoints for a very recent epoch, so we shouldn't need an index here.
293
+ let checkpoint = await this.store.getCheckpointData(await this.store.getSynchedCheckpointNumber());
294
+ const slot = (b: CheckpointData) => b.header.slotNumber;
295
+ while (checkpoint && slot(checkpoint) >= start) {
296
+ if (slot(checkpoint) <= end) {
297
+ // push the blocks on backwards
298
+ const endBlock = checkpoint.startBlock + checkpoint.numBlocks - 1;
299
+ for (let i = endBlock; i >= checkpoint.startBlock; i--) {
300
+ const block = await this.getBlockHeader(BlockNumber(i));
301
+ if (block) {
302
+ blocks.push(block);
303
+ }
304
+ }
305
+ }
306
+ checkpoint = await this.store.getCheckpointData(CheckpointNumber(checkpoint.checkpointNumber - 1));
307
+ }
308
+ return blocks.reverse();
309
+ }
310
+
311
+ public async getCheckpointsForEpoch(epochNumber: EpochNumber): Promise<Checkpoint[]> {
312
+ if (!this.l1Constants) {
313
+ throw new Error('L1 constants not set');
314
+ }
315
+
316
+ const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1Constants);
317
+ const checkpoints: Checkpoint[] = [];
318
+
319
+ // Walk the list of checkpoints backwards and filter by slots matching the requested epoch.
320
+ // We'll typically ask for checkpoints for a very recent epoch, so we shouldn't need an index here.
321
+ let checkpointData = await this.store.getCheckpointData(await this.store.getSynchedCheckpointNumber());
322
+ const slot = (b: CheckpointData) => b.header.slotNumber;
323
+ while (checkpointData && slot(checkpointData) >= start) {
324
+ if (slot(checkpointData) <= end) {
325
+ // push the checkpoints on backwards
326
+ const [checkpoint] = await this.getCheckpoints(checkpointData.checkpointNumber, 1);
327
+ checkpoints.push(checkpoint.checkpoint);
328
+ }
329
+ checkpointData = await this.store.getCheckpointData(CheckpointNumber(checkpointData.checkpointNumber - 1));
330
+ }
331
+
332
+ return checkpoints.reverse();
333
+ }
334
+
335
+ public async getBlock(number: BlockNumber): Promise<L2Block | undefined> {
336
+ // If the number provided is -ve, then return the latest block.
337
+ if (number < 0) {
338
+ number = await this.store.getLatestBlockNumber();
339
+ }
340
+ if (number === 0) {
341
+ return undefined;
342
+ }
343
+ return this.store.getBlock(number);
344
+ }
345
+
346
+ public getBlocks(from: BlockNumber, limit: number): Promise<L2Block[]> {
347
+ return this.store.getBlocks(from, limit);
348
+ }
349
+
350
+ public getCheckpointedBlockByHash(blockHash: Fr): Promise<CheckpointedL2Block | undefined> {
351
+ return this.store.getCheckpointedBlockByHash(blockHash);
352
+ }
353
+
354
+ public getCheckpointedBlockByArchive(archive: Fr): Promise<CheckpointedL2Block | undefined> {
355
+ return this.store.getCheckpointedBlockByArchive(archive);
356
+ }
357
+
358
+ public async getL2BlockByHash(blockHash: Fr): Promise<L2Block | undefined> {
359
+ const checkpointedBlock = await this.store.getCheckpointedBlockByHash(blockHash);
360
+ return checkpointedBlock?.block;
361
+ }
362
+
363
+ public async getL2BlockByArchive(archive: Fr): Promise<L2Block | undefined> {
364
+ const checkpointedBlock = await this.store.getCheckpointedBlockByArchive(archive);
365
+ return checkpointedBlock?.block;
366
+ }
367
+ }