@aztec/archiver 0.0.1-commit.f295ac2 → 0.0.1-commit.f504929
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 +9 -0
- package/dest/archiver.d.ts +10 -6
- package/dest/archiver.d.ts.map +1 -1
- package/dest/archiver.js +50 -111
- package/dest/errors.d.ts +6 -1
- package/dest/errors.d.ts.map +1 -1
- package/dest/errors.js +8 -0
- package/dest/factory.d.ts +5 -2
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +16 -13
- package/dest/index.d.ts +2 -1
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +1 -0
- package/dest/l1/bin/retrieve-calldata.js +35 -32
- package/dest/l1/calldata_retriever.d.ts +73 -50
- package/dest/l1/calldata_retriever.d.ts.map +1 -1
- package/dest/l1/calldata_retriever.js +190 -259
- package/dest/l1/data_retrieval.d.ts +9 -9
- package/dest/l1/data_retrieval.d.ts.map +1 -1
- package/dest/l1/data_retrieval.js +24 -22
- package/dest/l1/spire_proposer.d.ts +5 -5
- package/dest/l1/spire_proposer.d.ts.map +1 -1
- package/dest/l1/spire_proposer.js +9 -17
- package/dest/l1/validate_trace.d.ts +6 -3
- package/dest/l1/validate_trace.d.ts.map +1 -1
- package/dest/l1/validate_trace.js +13 -9
- package/dest/modules/data_source_base.d.ts +25 -21
- package/dest/modules/data_source_base.d.ts.map +1 -1
- package/dest/modules/data_source_base.js +48 -123
- package/dest/modules/data_store_updater.d.ts +31 -20
- package/dest/modules/data_store_updater.d.ts.map +1 -1
- package/dest/modules/data_store_updater.js +79 -60
- package/dest/modules/instrumentation.d.ts +17 -4
- package/dest/modules/instrumentation.d.ts.map +1 -1
- package/dest/modules/instrumentation.js +36 -12
- package/dest/modules/l1_synchronizer.d.ts +4 -8
- package/dest/modules/l1_synchronizer.d.ts.map +1 -1
- package/dest/modules/l1_synchronizer.js +23 -19
- package/dest/store/block_store.d.ts +50 -32
- package/dest/store/block_store.d.ts.map +1 -1
- package/dest/store/block_store.js +147 -54
- package/dest/store/contract_class_store.d.ts +1 -1
- package/dest/store/contract_class_store.d.ts.map +1 -1
- package/dest/store/contract_class_store.js +11 -7
- package/dest/store/kv_archiver_store.d.ts +52 -29
- package/dest/store/kv_archiver_store.d.ts.map +1 -1
- package/dest/store/kv_archiver_store.js +49 -23
- package/dest/store/l2_tips_cache.d.ts +19 -0
- package/dest/store/l2_tips_cache.d.ts.map +1 -0
- package/dest/store/l2_tips_cache.js +89 -0
- package/dest/store/log_store.d.ts +17 -8
- package/dest/store/log_store.d.ts.map +1 -1
- package/dest/store/log_store.js +77 -43
- package/dest/test/fake_l1_state.d.ts +9 -4
- package/dest/test/fake_l1_state.d.ts.map +1 -1
- package/dest/test/fake_l1_state.js +56 -18
- package/dest/test/index.js +3 -1
- package/dest/test/mock_archiver.d.ts +1 -1
- package/dest/test/mock_archiver.d.ts.map +1 -1
- package/dest/test/mock_archiver.js +3 -2
- package/dest/test/mock_l2_block_source.d.ts +36 -21
- package/dest/test/mock_l2_block_source.d.ts.map +1 -1
- package/dest/test/mock_l2_block_source.js +151 -109
- package/dest/test/mock_structs.d.ts +3 -2
- package/dest/test/mock_structs.d.ts.map +1 -1
- package/dest/test/mock_structs.js +11 -9
- package/dest/test/noop_l1_archiver.d.ts +23 -0
- package/dest/test/noop_l1_archiver.d.ts.map +1 -0
- package/dest/test/noop_l1_archiver.js +68 -0
- package/package.json +14 -13
- package/src/archiver.ts +71 -136
- package/src/errors.ts +12 -0
- package/src/factory.ts +30 -14
- package/src/index.ts +1 -0
- package/src/l1/README.md +25 -68
- package/src/l1/bin/retrieve-calldata.ts +45 -33
- package/src/l1/calldata_retriever.ts +249 -379
- package/src/l1/data_retrieval.ts +27 -29
- package/src/l1/spire_proposer.ts +7 -15
- package/src/l1/validate_trace.ts +24 -6
- package/src/modules/data_source_base.ts +81 -167
- package/src/modules/data_store_updater.ts +92 -63
- package/src/modules/instrumentation.ts +46 -14
- package/src/modules/l1_synchronizer.ts +26 -24
- package/src/store/block_store.ts +188 -92
- package/src/store/contract_class_store.ts +11 -7
- package/src/store/kv_archiver_store.ts +85 -36
- package/src/store/l2_tips_cache.ts +89 -0
- package/src/store/log_store.ts +134 -49
- package/src/test/fake_l1_state.ts +77 -19
- package/src/test/index.ts +3 -0
- package/src/test/mock_archiver.ts +3 -2
- package/src/test/mock_l2_block_source.ts +196 -126
- package/src/test/mock_structs.ts +26 -10
- package/src/test/noop_l1_archiver.ts +109 -0
|
@@ -6,8 +6,14 @@ import { createLogger } from '@aztec/foundation/log';
|
|
|
6
6
|
import type { AztecAsyncKVStore, CustomRange, StoreSize } from '@aztec/kv-store';
|
|
7
7
|
import { FunctionSelector } from '@aztec/stdlib/abi';
|
|
8
8
|
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
9
|
-
import {
|
|
10
|
-
|
|
9
|
+
import {
|
|
10
|
+
type BlockData,
|
|
11
|
+
BlockHash,
|
|
12
|
+
CheckpointedL2Block,
|
|
13
|
+
L2Block,
|
|
14
|
+
type ValidateCheckpointResult,
|
|
15
|
+
} from '@aztec/stdlib/block';
|
|
16
|
+
import type { CheckpointData, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
|
|
11
17
|
import type {
|
|
12
18
|
ContractClassPublic,
|
|
13
19
|
ContractDataSource,
|
|
@@ -16,6 +22,7 @@ import type {
|
|
|
16
22
|
ExecutablePrivateFunctionWithMembershipProof,
|
|
17
23
|
UtilityFunctionWithMembershipProof,
|
|
18
24
|
} from '@aztec/stdlib/contract';
|
|
25
|
+
import type { L1RollupConstants } from '@aztec/stdlib/epoch-helpers';
|
|
19
26
|
import type { GetContractClassLogsResponse, GetPublicLogsResponse } from '@aztec/stdlib/interfaces/client';
|
|
20
27
|
import type { LogFilter, SiloedTag, Tag, TxScopedL2Log } from '@aztec/stdlib/logs';
|
|
21
28
|
import type { BlockHeader, TxHash, TxReceipt } from '@aztec/stdlib/tx';
|
|
@@ -24,7 +31,7 @@ import type { UInt64 } from '@aztec/stdlib/types';
|
|
|
24
31
|
import { join } from 'path';
|
|
25
32
|
|
|
26
33
|
import type { InboxMessage } from '../structs/inbox_message.js';
|
|
27
|
-
import { BlockStore, type
|
|
34
|
+
import { BlockStore, type RemoveCheckpointsResult } from './block_store.js';
|
|
28
35
|
import { ContractClassStore } from './contract_class_store.js';
|
|
29
36
|
import { ContractInstanceStore } from './contract_instance_store.js';
|
|
30
37
|
import { LogStore } from './log_store.js';
|
|
@@ -64,14 +71,20 @@ export class KVArchiverDataStore implements ContractDataSource {
|
|
|
64
71
|
constructor(
|
|
65
72
|
private db: AztecAsyncKVStore,
|
|
66
73
|
logsMaxPageSize: number = 1000,
|
|
74
|
+
l1Constants: Pick<L1RollupConstants, 'epochDuration'>,
|
|
67
75
|
) {
|
|
68
|
-
this.#blockStore = new BlockStore(db);
|
|
76
|
+
this.#blockStore = new BlockStore(db, l1Constants);
|
|
69
77
|
this.#logStore = new LogStore(db, this.#blockStore, logsMaxPageSize);
|
|
70
78
|
this.#messageStore = new MessageStore(db);
|
|
71
79
|
this.#contractClassStore = new ContractClassStore(db);
|
|
72
80
|
this.#contractInstanceStore = new ContractInstanceStore(db);
|
|
73
81
|
}
|
|
74
82
|
|
|
83
|
+
/** Returns the underlying block store. Used by L2TipsCache. */
|
|
84
|
+
get blockStore(): BlockStore {
|
|
85
|
+
return this.#blockStore;
|
|
86
|
+
}
|
|
87
|
+
|
|
75
88
|
/** Opens a new transaction to the underlying store and runs all operations within it. */
|
|
76
89
|
public transactionAsync<T>(callback: () => Promise<T>): Promise<T> {
|
|
77
90
|
return this.db.transactionAsync(callback);
|
|
@@ -101,6 +114,11 @@ export class KVArchiverDataStore implements ContractDataSource {
|
|
|
101
114
|
return this.db.close();
|
|
102
115
|
}
|
|
103
116
|
|
|
117
|
+
/** Computes the finalized block number based on the proven block number. */
|
|
118
|
+
getFinalizedL2BlockNumber(): Promise<BlockNumber> {
|
|
119
|
+
return this.#blockStore.getFinalizedL2BlockNumber();
|
|
120
|
+
}
|
|
121
|
+
|
|
104
122
|
/** Looks up a public function name given a selector. */
|
|
105
123
|
getDebugFunctionName(_address: AztecAddress, selector: FunctionSelector): Promise<string | undefined> {
|
|
106
124
|
return Promise.resolve(this.functionNames.get(selector.toString()));
|
|
@@ -228,12 +246,14 @@ export class KVArchiverDataStore implements ContractDataSource {
|
|
|
228
246
|
}
|
|
229
247
|
|
|
230
248
|
/**
|
|
231
|
-
* Append new blocks to the store's list.
|
|
232
|
-
*
|
|
249
|
+
* Append new proposed blocks to the store's list.
|
|
250
|
+
* These are uncheckpointed blocks that have been proposed by the sequencer but not yet included in a checkpoint on L1.
|
|
251
|
+
* For checkpointed blocks (already published to L1), use addCheckpoints() instead.
|
|
252
|
+
* @param blocks - The proposed L2 blocks to be added to the store.
|
|
233
253
|
* @returns True if the operation is successful.
|
|
234
254
|
*/
|
|
235
|
-
|
|
236
|
-
return this.#blockStore.
|
|
255
|
+
addProposedBlocks(blocks: L2Block[], opts: { force?: boolean; checkpointNumber?: number } = {}): Promise<boolean> {
|
|
256
|
+
return this.#blockStore.addProposedBlocks(blocks, opts);
|
|
237
257
|
}
|
|
238
258
|
|
|
239
259
|
/**
|
|
@@ -254,14 +274,12 @@ export class KVArchiverDataStore implements ContractDataSource {
|
|
|
254
274
|
}
|
|
255
275
|
|
|
256
276
|
/**
|
|
257
|
-
*
|
|
258
|
-
*
|
|
259
|
-
*
|
|
260
|
-
* @param checkpointsToUnwind - The number of checkpoints we are to unwind
|
|
261
|
-
* @returns True if the operation is successful
|
|
277
|
+
* Removes all checkpoints with checkpoint number > checkpointNumber.
|
|
278
|
+
* Also removes ALL blocks (both checkpointed and uncheckpointed) after the last block of the given checkpoint.
|
|
279
|
+
* @param checkpointNumber - Remove all checkpoints strictly after this one.
|
|
262
280
|
*/
|
|
263
|
-
|
|
264
|
-
return this.#blockStore.
|
|
281
|
+
removeCheckpointsAfter(checkpointNumber: CheckpointNumber): Promise<RemoveCheckpointsResult> {
|
|
282
|
+
return this.#blockStore.removeCheckpointsAfter(checkpointNumber);
|
|
265
283
|
}
|
|
266
284
|
|
|
267
285
|
/**
|
|
@@ -284,7 +302,7 @@ export class KVArchiverDataStore implements ContractDataSource {
|
|
|
284
302
|
* Returns the block for the given hash, or undefined if not exists.
|
|
285
303
|
* @param blockHash - The block hash to return.
|
|
286
304
|
*/
|
|
287
|
-
getCheckpointedBlockByHash(blockHash:
|
|
305
|
+
getCheckpointedBlockByHash(blockHash: BlockHash): Promise<CheckpointedL2Block | undefined> {
|
|
288
306
|
return this.#blockStore.getCheckpointedBlockByHash(blockHash);
|
|
289
307
|
}
|
|
290
308
|
/**
|
|
@@ -298,21 +316,21 @@ export class KVArchiverDataStore implements ContractDataSource {
|
|
|
298
316
|
* Returns the block for the given number, or undefined if not exists.
|
|
299
317
|
* @param number - The block number to return.
|
|
300
318
|
*/
|
|
301
|
-
getBlock(number: BlockNumber): Promise<
|
|
319
|
+
getBlock(number: BlockNumber): Promise<L2Block | undefined> {
|
|
302
320
|
return this.#blockStore.getBlock(number);
|
|
303
321
|
}
|
|
304
322
|
/**
|
|
305
323
|
* Returns the block for the given hash, or undefined if not exists.
|
|
306
324
|
* @param blockHash - The block hash to return.
|
|
307
325
|
*/
|
|
308
|
-
getBlockByHash(blockHash:
|
|
309
|
-
return this.#blockStore.getBlockByHash(
|
|
326
|
+
getBlockByHash(blockHash: BlockHash): Promise<L2Block | undefined> {
|
|
327
|
+
return this.#blockStore.getBlockByHash(blockHash);
|
|
310
328
|
}
|
|
311
329
|
/**
|
|
312
330
|
* Returns the block for the given archive root, or undefined if not exists.
|
|
313
331
|
* @param archive - The archive root to return.
|
|
314
332
|
*/
|
|
315
|
-
getBlockByArchive(archive: Fr): Promise<
|
|
333
|
+
getBlockByArchive(archive: Fr): Promise<L2Block | undefined> {
|
|
316
334
|
return this.#blockStore.getBlockByArchive(archive);
|
|
317
335
|
}
|
|
318
336
|
|
|
@@ -322,7 +340,7 @@ export class KVArchiverDataStore implements ContractDataSource {
|
|
|
322
340
|
* @param limit - The number of blocks to return.
|
|
323
341
|
* @returns The requested L2 blocks.
|
|
324
342
|
*/
|
|
325
|
-
getBlocks(from: BlockNumber, limit: number): Promise<
|
|
343
|
+
getBlocks(from: BlockNumber, limit: number): Promise<L2Block[]> {
|
|
326
344
|
return toArray(this.#blockStore.getBlocks(from, limit));
|
|
327
345
|
}
|
|
328
346
|
|
|
@@ -350,8 +368,8 @@ export class KVArchiverDataStore implements ContractDataSource {
|
|
|
350
368
|
* Returns the block header for the given hash, or undefined if not exists.
|
|
351
369
|
* @param blockHash - The block hash to return.
|
|
352
370
|
*/
|
|
353
|
-
getBlockHeaderByHash(blockHash:
|
|
354
|
-
return this.#blockStore.getBlockHeaderByHash(
|
|
371
|
+
getBlockHeaderByHash(blockHash: BlockHash): Promise<BlockHeader | undefined> {
|
|
372
|
+
return this.#blockStore.getBlockHeaderByHash(blockHash);
|
|
355
373
|
}
|
|
356
374
|
|
|
357
375
|
/**
|
|
@@ -362,6 +380,22 @@ export class KVArchiverDataStore implements ContractDataSource {
|
|
|
362
380
|
return this.#blockStore.getBlockHeaderByArchive(archive);
|
|
363
381
|
}
|
|
364
382
|
|
|
383
|
+
/**
|
|
384
|
+
* Gets block metadata (without tx data) by block number.
|
|
385
|
+
* @param blockNumber - The block number to return.
|
|
386
|
+
*/
|
|
387
|
+
getBlockData(blockNumber: BlockNumber): Promise<BlockData | undefined> {
|
|
388
|
+
return this.#blockStore.getBlockData(blockNumber);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* Gets block metadata (without tx data) by archive root.
|
|
393
|
+
* @param archive - The archive root to return.
|
|
394
|
+
*/
|
|
395
|
+
getBlockDataByArchive(archive: Fr): Promise<BlockData | undefined> {
|
|
396
|
+
return this.#blockStore.getBlockDataByArchive(archive);
|
|
397
|
+
}
|
|
398
|
+
|
|
365
399
|
/**
|
|
366
400
|
* Gets a tx effect.
|
|
367
401
|
* @param txHash - The hash of the tx corresponding to the tx effect.
|
|
@@ -385,11 +419,11 @@ export class KVArchiverDataStore implements ContractDataSource {
|
|
|
385
419
|
* @param blocks - The blocks for which to add the logs.
|
|
386
420
|
* @returns True if the operation is successful.
|
|
387
421
|
*/
|
|
388
|
-
addLogs(blocks:
|
|
422
|
+
addLogs(blocks: L2Block[]): Promise<boolean> {
|
|
389
423
|
return this.#logStore.addLogs(blocks);
|
|
390
424
|
}
|
|
391
425
|
|
|
392
|
-
deleteLogs(blocks:
|
|
426
|
+
deleteLogs(blocks: L2Block[]): Promise<boolean> {
|
|
393
427
|
return this.#logStore.deleteLogs(blocks);
|
|
394
428
|
}
|
|
395
429
|
|
|
@@ -434,24 +468,33 @@ export class KVArchiverDataStore implements ContractDataSource {
|
|
|
434
468
|
}
|
|
435
469
|
|
|
436
470
|
/**
|
|
437
|
-
* Gets
|
|
471
|
+
* Gets private logs that match any of the `tags`. For each tag, an array of matching logs is returned. An empty
|
|
438
472
|
* array implies no logs match that tag.
|
|
473
|
+
* @param tags - The tags to search for.
|
|
474
|
+
* @param page - The page number (0-indexed) for pagination. Returns at most 10 logs per tag per page.
|
|
439
475
|
*/
|
|
440
|
-
getPrivateLogsByTags(tags: SiloedTag[]): Promise<TxScopedL2Log[][]> {
|
|
476
|
+
getPrivateLogsByTags(tags: SiloedTag[], page?: number): Promise<TxScopedL2Log[][]> {
|
|
441
477
|
try {
|
|
442
|
-
return this.#logStore.getPrivateLogsByTags(tags);
|
|
478
|
+
return this.#logStore.getPrivateLogsByTags(tags, page);
|
|
443
479
|
} catch (err) {
|
|
444
480
|
return Promise.reject(err);
|
|
445
481
|
}
|
|
446
482
|
}
|
|
447
483
|
|
|
448
484
|
/**
|
|
449
|
-
* Gets
|
|
485
|
+
* Gets public logs that match any of the `tags` from the specified contract. For each tag, an array of matching
|
|
450
486
|
* logs is returned. An empty array implies no logs match that tag.
|
|
451
|
-
|
|
452
|
-
|
|
487
|
+
* @param contractAddress - The contract address to search logs for.
|
|
488
|
+
* @param tags - The tags to search for.
|
|
489
|
+
* @param page - The page number (0-indexed) for pagination. Returns at most 10 logs per tag per page.
|
|
490
|
+
*/
|
|
491
|
+
getPublicLogsByTagsFromContract(
|
|
492
|
+
contractAddress: AztecAddress,
|
|
493
|
+
tags: Tag[],
|
|
494
|
+
page?: number,
|
|
495
|
+
): Promise<TxScopedL2Log[][]> {
|
|
453
496
|
try {
|
|
454
|
-
return this.#logStore.getPublicLogsByTagsFromContract(contractAddress, tags);
|
|
497
|
+
return this.#logStore.getPublicLogsByTagsFromContract(contractAddress, tags, page);
|
|
455
498
|
} catch (err) {
|
|
456
499
|
return Promise.reject(err);
|
|
457
500
|
}
|
|
@@ -589,7 +632,7 @@ export class KVArchiverDataStore implements ContractDataSource {
|
|
|
589
632
|
* @param checkpointNumber Retrieves all blocks for the given checkpoint
|
|
590
633
|
* @returns The collection of blocks for the requested checkpoint if available (undefined otherwise)
|
|
591
634
|
*/
|
|
592
|
-
getBlocksForCheckpoint(checkpointNumber: CheckpointNumber): Promise<
|
|
635
|
+
getBlocksForCheckpoint(checkpointNumber: CheckpointNumber): Promise<L2Block[] | undefined> {
|
|
593
636
|
return this.#blockStore.getBlocksForCheckpoint(checkpointNumber);
|
|
594
637
|
}
|
|
595
638
|
|
|
@@ -602,21 +645,27 @@ export class KVArchiverDataStore implements ContractDataSource {
|
|
|
602
645
|
return this.#blockStore.getCheckpointData(checkpointNumber);
|
|
603
646
|
}
|
|
604
647
|
|
|
648
|
+
/** Returns checkpoint data for all checkpoints whose slot falls within the given range (inclusive). */
|
|
649
|
+
getCheckpointDataForSlotRange(startSlot: SlotNumber, endSlot: SlotNumber): Promise<CheckpointData[]> {
|
|
650
|
+
return this.#blockStore.getCheckpointDataForSlotRange(startSlot, endSlot);
|
|
651
|
+
}
|
|
652
|
+
|
|
605
653
|
/**
|
|
606
654
|
* Gets all blocks that have the given slot number.
|
|
607
655
|
* @param slotNumber - The slot number to search for.
|
|
608
656
|
* @returns All blocks with the given slot number.
|
|
609
657
|
*/
|
|
610
|
-
getBlocksForSlot(slotNumber: SlotNumber): Promise<
|
|
658
|
+
getBlocksForSlot(slotNumber: SlotNumber): Promise<L2Block[]> {
|
|
611
659
|
return this.#blockStore.getBlocksForSlot(slotNumber);
|
|
612
660
|
}
|
|
613
661
|
|
|
614
662
|
/**
|
|
615
663
|
* Removes all blocks with block number > blockNumber.
|
|
664
|
+
* Does not remove any associated checkpoints.
|
|
616
665
|
* @param blockNumber - The block number to remove after.
|
|
617
666
|
* @returns The removed blocks (for event emission).
|
|
618
667
|
*/
|
|
619
|
-
removeBlocksAfter(blockNumber: BlockNumber): Promise<
|
|
620
|
-
return this.#blockStore.
|
|
668
|
+
removeBlocksAfter(blockNumber: BlockNumber): Promise<L2Block[]> {
|
|
669
|
+
return this.#blockStore.removeBlocksAfter(blockNumber);
|
|
621
670
|
}
|
|
622
671
|
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { GENESIS_BLOCK_HEADER_HASH, INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
|
|
2
|
+
import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
|
|
3
|
+
import { type BlockData, type CheckpointId, GENESIS_CHECKPOINT_HEADER_HASH, type L2Tips } from '@aztec/stdlib/block';
|
|
4
|
+
|
|
5
|
+
import type { BlockStore } from './block_store.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* In-memory cache for L2 chain tips (proposed, checkpointed, proven, finalized).
|
|
9
|
+
* Populated from the BlockStore on first access, then kept up-to-date by the ArchiverDataStoreUpdater.
|
|
10
|
+
* Refresh calls should happen within the store transaction that mutates block data to ensure consistency.
|
|
11
|
+
*/
|
|
12
|
+
export class L2TipsCache {
|
|
13
|
+
#tipsPromise: Promise<L2Tips> | undefined;
|
|
14
|
+
|
|
15
|
+
constructor(private blockStore: BlockStore) {}
|
|
16
|
+
|
|
17
|
+
/** Returns the cached L2 tips. Loads from the block store on first call. */
|
|
18
|
+
public getL2Tips(): Promise<L2Tips> {
|
|
19
|
+
return (this.#tipsPromise ??= this.loadFromStore());
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Reloads the L2 tips from the block store. Should be called within the store transaction that mutates data. */
|
|
23
|
+
public async refresh(): Promise<void> {
|
|
24
|
+
this.#tipsPromise = this.loadFromStore();
|
|
25
|
+
await this.#tipsPromise;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
private async loadFromStore(): Promise<L2Tips> {
|
|
29
|
+
const [latestBlockNumber, provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber] = await Promise.all([
|
|
30
|
+
this.blockStore.getLatestBlockNumber(),
|
|
31
|
+
this.blockStore.getProvenBlockNumber(),
|
|
32
|
+
this.blockStore.getCheckpointedL2BlockNumber(),
|
|
33
|
+
this.blockStore.getFinalizedL2BlockNumber(),
|
|
34
|
+
]);
|
|
35
|
+
|
|
36
|
+
const genesisBlockHeader = {
|
|
37
|
+
blockHash: GENESIS_BLOCK_HEADER_HASH,
|
|
38
|
+
checkpointNumber: CheckpointNumber.ZERO,
|
|
39
|
+
} as const;
|
|
40
|
+
const beforeInitialBlockNumber = BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
41
|
+
|
|
42
|
+
const getBlockData = (blockNumber: BlockNumber) =>
|
|
43
|
+
blockNumber > beforeInitialBlockNumber ? this.blockStore.getBlockData(blockNumber) : genesisBlockHeader;
|
|
44
|
+
|
|
45
|
+
const [latestBlockData, provenBlockData, checkpointedBlockData, finalizedBlockData] = await Promise.all(
|
|
46
|
+
[latestBlockNumber, provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber].map(getBlockData),
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
if (!latestBlockData || !provenBlockData || !finalizedBlockData || !checkpointedBlockData) {
|
|
50
|
+
throw new Error('Failed to load block data for L2 tips');
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const [provenCheckpointId, finalizedCheckpointId, checkpointedCheckpointId] = await Promise.all([
|
|
54
|
+
this.getCheckpointIdForBlock(provenBlockData),
|
|
55
|
+
this.getCheckpointIdForBlock(finalizedBlockData),
|
|
56
|
+
this.getCheckpointIdForBlock(checkpointedBlockData),
|
|
57
|
+
]);
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
proposed: { number: latestBlockNumber, hash: latestBlockData.blockHash.toString() },
|
|
61
|
+
proven: {
|
|
62
|
+
block: { number: provenBlockNumber, hash: provenBlockData.blockHash.toString() },
|
|
63
|
+
checkpoint: provenCheckpointId,
|
|
64
|
+
},
|
|
65
|
+
finalized: {
|
|
66
|
+
block: { number: finalizedBlockNumber, hash: finalizedBlockData.blockHash.toString() },
|
|
67
|
+
checkpoint: finalizedCheckpointId,
|
|
68
|
+
},
|
|
69
|
+
checkpointed: {
|
|
70
|
+
block: { number: checkpointedBlockNumber, hash: checkpointedBlockData.blockHash.toString() },
|
|
71
|
+
checkpoint: checkpointedCheckpointId,
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
private async getCheckpointIdForBlock(blockData: Pick<BlockData, 'checkpointNumber'>): Promise<CheckpointId> {
|
|
77
|
+
const checkpointData = await this.blockStore.getCheckpointData(blockData.checkpointNumber);
|
|
78
|
+
if (!checkpointData) {
|
|
79
|
+
return {
|
|
80
|
+
number: CheckpointNumber.ZERO,
|
|
81
|
+
hash: GENESIS_CHECKPOINT_HEADER_HASH.toString(),
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
return {
|
|
85
|
+
number: checkpointData.checkpointNumber,
|
|
86
|
+
hash: checkpointData.header.hash().toString(),
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
}
|