@aztec/stdlib 4.0.0-nightly.20260111 → 4.0.0-nightly.20260113
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/dest/block/attestation_info.d.ts +5 -5
- package/dest/block/attestation_info.d.ts.map +1 -1
- package/dest/block/attestation_info.js +4 -4
- package/dest/block/l2_block.d.ts +6 -3
- package/dest/block/l2_block.d.ts.map +1 -1
- package/dest/block/l2_block.js +2 -2
- package/dest/block/l2_block_new.d.ts +1 -2
- package/dest/block/l2_block_new.d.ts.map +1 -1
- package/dest/block/l2_block_new.js +4 -1
- package/dest/block/l2_block_source.d.ts +245 -41
- package/dest/block/l2_block_source.d.ts.map +1 -1
- package/dest/block/l2_block_source.js +23 -5
- package/dest/block/l2_block_stream/index.d.ts +2 -1
- package/dest/block/l2_block_stream/index.d.ts.map +1 -1
- package/dest/block/l2_block_stream/index.js +1 -0
- package/dest/block/l2_block_stream/interfaces.d.ts +16 -5
- package/dest/block/l2_block_stream/interfaces.d.ts.map +1 -1
- package/dest/block/l2_block_stream/l2_block_stream.d.ts +4 -2
- package/dest/block/l2_block_stream/l2_block_stream.d.ts.map +1 -1
- package/dest/block/l2_block_stream/l2_block_stream.js +102 -30
- package/dest/block/l2_block_stream/l2_tips_memory_store.d.ts +24 -16
- package/dest/block/l2_block_stream/l2_tips_memory_store.d.ts.map +1 -1
- package/dest/block/l2_block_stream/l2_tips_memory_store.js +55 -61
- package/dest/block/l2_block_stream/l2_tips_store_base.d.ts +49 -0
- package/dest/block/l2_block_stream/l2_tips_store_base.d.ts.map +1 -0
- package/dest/block/l2_block_stream/l2_tips_store_base.js +179 -0
- package/dest/block/test/l2_tips_store_test_suite.d.ts +1 -1
- package/dest/block/test/l2_tips_store_test_suite.d.ts.map +1 -1
- package/dest/block/test/l2_tips_store_test_suite.js +483 -38
- package/dest/block/validate_block_result.d.ts +24 -24
- package/dest/block/validate_block_result.d.ts.map +1 -1
- package/dest/block/validate_block_result.js +13 -13
- package/dest/checkpoint/checkpoint.d.ts +1 -1
- package/dest/checkpoint/checkpoint.d.ts.map +1 -1
- package/dest/checkpoint/checkpoint.js +1 -0
- package/dest/checkpoint/checkpoint_info.d.ts +32 -3
- package/dest/checkpoint/checkpoint_info.d.ts.map +1 -1
- package/dest/checkpoint/checkpoint_info.js +34 -1
- package/dest/checkpoint/index.d.ts +2 -1
- package/dest/checkpoint/index.d.ts.map +1 -1
- package/dest/checkpoint/index.js +1 -0
- package/dest/interfaces/api_limit.d.ts +2 -1
- package/dest/interfaces/api_limit.d.ts.map +1 -1
- package/dest/interfaces/api_limit.js +1 -0
- package/dest/interfaces/archiver.d.ts +6 -6
- package/dest/interfaces/archiver.d.ts.map +1 -1
- package/dest/interfaces/archiver.js +6 -4
- package/dest/interfaces/aztec-node-admin.d.ts +12 -6
- package/dest/interfaces/aztec-node-admin.d.ts.map +1 -1
- package/dest/interfaces/aztec-node-admin.js +2 -2
- package/dest/interfaces/aztec-node.d.ts +2 -2
- package/dest/interfaces/aztec-node.d.ts.map +1 -1
- package/dest/interfaces/aztec-node.js +8 -3
- package/dest/interfaces/configs.d.ts +6 -1
- package/dest/interfaces/configs.d.ts.map +1 -1
- package/dest/interfaces/configs.js +2 -1
- package/dest/interfaces/p2p.d.ts +7 -9
- package/dest/interfaces/p2p.d.ts.map +1 -1
- package/dest/interfaces/p2p.js +3 -4
- package/dest/interfaces/proving-job.d.ts +166 -166
- package/dest/interfaces/validator.d.ts +41 -7
- package/dest/interfaces/validator.d.ts.map +1 -1
- package/dest/interfaces/validator.js +3 -1
- package/dest/kernel/hints/build_note_hash_read_request_hints.d.ts +6 -5
- package/dest/kernel/hints/build_note_hash_read_request_hints.d.ts.map +1 -1
- package/dest/kernel/hints/build_note_hash_read_request_hints.js +5 -6
- package/dest/p2p/attestation_utils.d.ts +3 -3
- package/dest/p2p/attestation_utils.d.ts.map +1 -1
- package/dest/p2p/attestation_utils.js +1 -1
- package/dest/p2p/block_proposal.d.ts +85 -21
- package/dest/p2p/block_proposal.d.ts.map +1 -1
- package/dest/p2p/block_proposal.js +120 -37
- package/dest/p2p/checkpoint_attestation.d.ts +77 -0
- package/dest/p2p/checkpoint_attestation.d.ts.map +1 -0
- package/dest/p2p/{block_attestation.js → checkpoint_attestation.js} +22 -19
- package/dest/p2p/checkpoint_proposal.d.ts +154 -0
- package/dest/p2p/checkpoint_proposal.d.ts.map +1 -0
- package/dest/p2p/checkpoint_proposal.js +217 -0
- package/dest/p2p/consensus_payload.d.ts +4 -2
- package/dest/p2p/consensus_payload.d.ts.map +1 -1
- package/dest/p2p/consensus_payload.js +3 -2
- package/dest/p2p/index.d.ts +4 -2
- package/dest/p2p/index.d.ts.map +1 -1
- package/dest/p2p/index.js +3 -1
- package/dest/p2p/signature_utils.d.ts +5 -3
- package/dest/p2p/signature_utils.d.ts.map +1 -1
- package/dest/p2p/signature_utils.js +3 -1
- package/dest/p2p/signed_txs.d.ts +40 -0
- package/dest/p2p/signed_txs.d.ts.map +1 -0
- package/dest/p2p/signed_txs.js +70 -0
- package/dest/p2p/topic_type.d.ts +3 -2
- package/dest/p2p/topic_type.d.ts.map +1 -1
- package/dest/p2p/topic_type.js +8 -2
- package/dest/rollup/checkpoint_header.d.ts +5 -1
- package/dest/rollup/checkpoint_header.d.ts.map +1 -1
- package/dest/rollup/checkpoint_header.js +4 -0
- package/dest/tests/factories.d.ts +13 -1
- package/dest/tests/factories.d.ts.map +1 -1
- package/dest/tests/factories.js +50 -1
- package/dest/tests/mocks.d.ts +55 -9
- package/dest/tests/mocks.d.ts.map +1 -1
- package/dest/tests/mocks.js +84 -35
- package/dest/tx/private_execution_result.d.ts +1 -5
- package/dest/tx/private_execution_result.d.ts.map +1 -1
- package/dest/tx/private_execution_result.js +3 -20
- package/package.json +8 -8
- package/src/block/attestation_info.ts +9 -6
- package/src/block/l2_block.ts +3 -3
- package/src/block/l2_block_new.ts +5 -1
- package/src/block/l2_block_source.ts +66 -16
- package/src/block/l2_block_stream/index.ts +1 -0
- package/src/block/l2_block_stream/interfaces.ts +16 -4
- package/src/block/l2_block_stream/l2_block_stream.ts +121 -38
- package/src/block/l2_block_stream/l2_tips_memory_store.ts +62 -56
- package/src/block/l2_block_stream/l2_tips_store_base.ts +226 -0
- package/src/block/test/l2_tips_store_test_suite.ts +485 -36
- package/src/block/validate_block_result.ts +35 -31
- package/src/checkpoint/checkpoint.ts +1 -0
- package/src/checkpoint/checkpoint_info.ts +45 -2
- package/src/checkpoint/index.ts +1 -0
- package/src/interfaces/api_limit.ts +1 -0
- package/src/interfaces/archiver.ts +14 -6
- package/src/interfaces/aztec-node-admin.ts +5 -2
- package/src/interfaces/aztec-node.ts +30 -3
- package/src/interfaces/configs.ts +5 -0
- package/src/interfaces/p2p.ts +8 -12
- package/src/interfaces/validator.ts +57 -7
- package/src/kernel/hints/build_note_hash_read_request_hints.ts +5 -8
- package/src/p2p/attestation_utils.ts +3 -3
- package/src/p2p/block_proposal.ts +185 -41
- package/src/p2p/{block_attestation.ts → checkpoint_attestation.ts} +31 -25
- package/src/p2p/checkpoint_proposal.ts +337 -0
- package/src/p2p/consensus_payload.ts +5 -2
- package/src/p2p/index.ts +3 -1
- package/src/p2p/signature_utils.ts +3 -1
- package/src/p2p/signed_txs.ts +83 -0
- package/src/p2p/topic_type.ts +3 -2
- package/src/rollup/checkpoint_header.ts +13 -0
- package/src/tests/factories.ts +42 -1
- package/src/tests/mocks.ts +146 -50
- package/src/tx/private_execution_result.ts +0 -15
- package/dest/p2p/block_attestation.d.ts +0 -77
- package/dest/p2p/block_attestation.d.ts.map +0 -1
|
@@ -1,78 +1,84 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { BlockNumber } from '@aztec/foundation/branded-types';
|
|
1
|
+
import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
|
|
3
2
|
|
|
4
|
-
import type {
|
|
5
|
-
import type {
|
|
6
|
-
import
|
|
3
|
+
import type { PublishedCheckpoint } from '../../checkpoint/published_checkpoint.js';
|
|
4
|
+
import type { L2BlockTag } from '../l2_block_source.js';
|
|
5
|
+
import { L2TipsStoreBase } from './l2_tips_store_base.js';
|
|
7
6
|
|
|
8
7
|
/**
|
|
9
|
-
*
|
|
10
|
-
* @dev
|
|
8
|
+
* In-memory implementation of L2 tips store. Useful for testing and lightweight clients.
|
|
9
|
+
* @dev Tests in kv-store/src/stores/l2_tips_memory_store.test.ts
|
|
11
10
|
*/
|
|
12
|
-
export class L2TipsMemoryStore
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
export class L2TipsMemoryStore extends L2TipsStoreBase {
|
|
12
|
+
private readonly tips = new Map<L2BlockTag, BlockNumber>();
|
|
13
|
+
private readonly blockHashes = new Map<number, string>();
|
|
14
|
+
private readonly blockToCheckpoint = new Map<number, CheckpointNumber>();
|
|
15
|
+
private readonly checkpoints = new Map<number, PublishedCheckpoint>();
|
|
15
16
|
|
|
16
|
-
|
|
17
|
-
return Promise.resolve(this.
|
|
17
|
+
protected getTip(tag: L2BlockTag): Promise<BlockNumber | undefined> {
|
|
18
|
+
return Promise.resolve(this.tips.get(tag));
|
|
18
19
|
}
|
|
19
20
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
finalized: this.getL2Tip('finalized'),
|
|
24
|
-
proven: this.getL2Tip('proven'),
|
|
25
|
-
});
|
|
21
|
+
protected setTip(tag: L2BlockTag, blockNumber: BlockNumber): Promise<void> {
|
|
22
|
+
this.tips.set(tag, blockNumber);
|
|
23
|
+
return Promise.resolve();
|
|
26
24
|
}
|
|
27
25
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
26
|
+
protected getStoredBlockHash(blockNumber: BlockNumber): Promise<string | undefined> {
|
|
27
|
+
return Promise.resolve(this.blockHashes.get(blockNumber));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
protected setBlockHash(blockNumber: BlockNumber, hash: string): Promise<void> {
|
|
31
|
+
this.blockHashes.set(blockNumber, hash);
|
|
32
|
+
return Promise.resolve();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
protected deleteBlockHashesBefore(blockNumber: BlockNumber): Promise<void> {
|
|
36
|
+
for (const key of this.blockHashes.keys()) {
|
|
37
|
+
if (key < blockNumber) {
|
|
38
|
+
this.blockHashes.delete(key);
|
|
39
|
+
}
|
|
36
40
|
}
|
|
41
|
+
return Promise.resolve();
|
|
42
|
+
}
|
|
37
43
|
|
|
38
|
-
|
|
44
|
+
protected getCheckpointNumberForBlock(blockNumber: BlockNumber): Promise<CheckpointNumber | undefined> {
|
|
45
|
+
return Promise.resolve(this.blockToCheckpoint.get(blockNumber));
|
|
39
46
|
}
|
|
40
47
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
48
|
+
protected setCheckpointNumberForBlock(blockNumber: BlockNumber, checkpointNumber: CheckpointNumber): Promise<void> {
|
|
49
|
+
this.blockToCheckpoint.set(blockNumber, checkpointNumber);
|
|
50
|
+
return Promise.resolve();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
protected deleteBlockToCheckpointBefore(blockNumber: BlockNumber): Promise<void> {
|
|
54
|
+
for (const key of this.blockToCheckpoint.keys()) {
|
|
55
|
+
if (key < blockNumber) {
|
|
56
|
+
this.blockToCheckpoint.delete(key);
|
|
50
57
|
}
|
|
51
|
-
case 'chain-pruned':
|
|
52
|
-
this.saveTag('latest', event.block);
|
|
53
|
-
break;
|
|
54
|
-
case 'chain-proven':
|
|
55
|
-
this.saveTag('proven', event.block);
|
|
56
|
-
break;
|
|
57
|
-
case 'chain-finalized':
|
|
58
|
-
this.saveTag('finalized', event.block);
|
|
59
|
-
for (const key of this.l2BlockHashesStore.keys()) {
|
|
60
|
-
if (key < event.block.number) {
|
|
61
|
-
this.l2BlockHashesStore.delete(key);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
break;
|
|
65
58
|
}
|
|
59
|
+
return Promise.resolve();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
protected getCheckpoint(checkpointNumber: CheckpointNumber): Promise<PublishedCheckpoint | undefined> {
|
|
63
|
+
return Promise.resolve(this.checkpoints.get(checkpointNumber));
|
|
66
64
|
}
|
|
67
65
|
|
|
68
|
-
protected
|
|
69
|
-
this.
|
|
70
|
-
|
|
71
|
-
|
|
66
|
+
protected saveCheckpointData(checkpoint: PublishedCheckpoint): Promise<void> {
|
|
67
|
+
this.checkpoints.set(checkpoint.checkpoint.number, checkpoint);
|
|
68
|
+
return Promise.resolve();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
protected deleteCheckpointsBefore(checkpointNumber: CheckpointNumber): Promise<void> {
|
|
72
|
+
for (const key of this.checkpoints.keys()) {
|
|
73
|
+
if (key < checkpointNumber) {
|
|
74
|
+
this.checkpoints.delete(key);
|
|
75
|
+
}
|
|
72
76
|
}
|
|
77
|
+
return Promise.resolve();
|
|
73
78
|
}
|
|
74
79
|
|
|
75
|
-
protected
|
|
76
|
-
|
|
80
|
+
protected runInTransaction<T>(fn: () => Promise<T>): Promise<T> {
|
|
81
|
+
// Memory store doesn't need transactions - just execute immediately
|
|
82
|
+
return fn();
|
|
77
83
|
}
|
|
78
84
|
}
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import { GENESIS_BLOCK_HEADER_HASH } from '@aztec/constants';
|
|
2
|
+
import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
|
|
3
|
+
|
|
4
|
+
import type { PublishedCheckpoint } from '../../checkpoint/published_checkpoint.js';
|
|
5
|
+
import type { L2BlockNew } from '../l2_block_new.js';
|
|
6
|
+
import {
|
|
7
|
+
type CheckpointId,
|
|
8
|
+
GENESIS_CHECKPOINT_HEADER_HASH,
|
|
9
|
+
type L2BlockId,
|
|
10
|
+
type L2BlockTag,
|
|
11
|
+
type L2Tips,
|
|
12
|
+
} from '../l2_block_source.js';
|
|
13
|
+
import type { L2BlockStreamEvent, L2BlockStreamEventHandler, L2BlockStreamLocalDataProvider } from './interfaces.js';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Abstract base class for L2 tips stores. Provides common event handling logic
|
|
17
|
+
* while delegating storage operations to subclasses.
|
|
18
|
+
*/
|
|
19
|
+
export abstract class L2TipsStoreBase implements L2BlockStreamEventHandler, L2BlockStreamLocalDataProvider {
|
|
20
|
+
// Abstract storage primitives - subclasses implement these based on their backing store
|
|
21
|
+
|
|
22
|
+
/** Gets the block number for a given tag. */
|
|
23
|
+
protected abstract getTip(tag: L2BlockTag): Promise<BlockNumber | undefined>;
|
|
24
|
+
|
|
25
|
+
/** Sets the block number for a given tag. */
|
|
26
|
+
protected abstract setTip(tag: L2BlockTag, blockNumber: BlockNumber): Promise<void>;
|
|
27
|
+
|
|
28
|
+
/** Gets the block hash for a given block number. */
|
|
29
|
+
protected abstract getStoredBlockHash(blockNumber: BlockNumber): Promise<string | undefined>;
|
|
30
|
+
|
|
31
|
+
/** Sets the block hash for a given block number. */
|
|
32
|
+
protected abstract setBlockHash(blockNumber: BlockNumber, hash: string): Promise<void>;
|
|
33
|
+
|
|
34
|
+
/** Deletes all block hashes for blocks before the given block number. */
|
|
35
|
+
protected abstract deleteBlockHashesBefore(blockNumber: BlockNumber): Promise<void>;
|
|
36
|
+
|
|
37
|
+
/** Gets the checkpoint number for a given block number. */
|
|
38
|
+
protected abstract getCheckpointNumberForBlock(blockNumber: BlockNumber): Promise<CheckpointNumber | undefined>;
|
|
39
|
+
|
|
40
|
+
/** Sets the checkpoint number for a given block number. */
|
|
41
|
+
protected abstract setCheckpointNumberForBlock(
|
|
42
|
+
blockNumber: BlockNumber,
|
|
43
|
+
checkpointNumber: CheckpointNumber,
|
|
44
|
+
): Promise<void>;
|
|
45
|
+
|
|
46
|
+
/** Deletes all block-to-checkpoint mappings for blocks before the given block number. */
|
|
47
|
+
protected abstract deleteBlockToCheckpointBefore(blockNumber: BlockNumber): Promise<void>;
|
|
48
|
+
|
|
49
|
+
/** Gets a checkpoint by its number. */
|
|
50
|
+
protected abstract getCheckpoint(checkpointNumber: CheckpointNumber): Promise<PublishedCheckpoint | undefined>;
|
|
51
|
+
|
|
52
|
+
/** Saves a checkpoint. */
|
|
53
|
+
protected abstract saveCheckpointData(checkpoint: PublishedCheckpoint): Promise<void>;
|
|
54
|
+
|
|
55
|
+
/** Deletes all checkpoints before the given checkpoint number. */
|
|
56
|
+
protected abstract deleteCheckpointsBefore(checkpointNumber: CheckpointNumber): Promise<void>;
|
|
57
|
+
|
|
58
|
+
/** Runs the given function in a transaction. Memory stores can just execute immediately. */
|
|
59
|
+
protected abstract runInTransaction<T>(fn: () => Promise<T>): Promise<T>;
|
|
60
|
+
|
|
61
|
+
// Public interface implementation
|
|
62
|
+
|
|
63
|
+
public getL2BlockHash(number: BlockNumber): Promise<string | undefined> {
|
|
64
|
+
return this.getStoredBlockHash(number);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
public getL2Tips(): Promise<L2Tips> {
|
|
68
|
+
return this.runInTransaction(async () => {
|
|
69
|
+
const [proposedBlockId, finalizedBlockId, provenBlockId, checkpointedBlockId] = await Promise.all([
|
|
70
|
+
this.getBlockId('proposed'),
|
|
71
|
+
this.getBlockId('finalized'),
|
|
72
|
+
this.getBlockId('proven'),
|
|
73
|
+
this.getBlockId('checkpointed'),
|
|
74
|
+
]);
|
|
75
|
+
|
|
76
|
+
const [finalizedCheckpointId, provenCheckpointId, checkpointedCheckpointId] = await Promise.all([
|
|
77
|
+
this.getCheckpointId('finalized'),
|
|
78
|
+
this.getCheckpointId('proven'),
|
|
79
|
+
this.getCheckpointId('checkpointed'),
|
|
80
|
+
]);
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
proposed: proposedBlockId,
|
|
84
|
+
finalized: { block: finalizedBlockId, checkpoint: finalizedCheckpointId },
|
|
85
|
+
proven: { block: provenBlockId, checkpoint: provenCheckpointId },
|
|
86
|
+
checkpointed: { block: checkpointedBlockId, checkpoint: checkpointedCheckpointId },
|
|
87
|
+
};
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
public async handleBlockStreamEvent(event: L2BlockStreamEvent): Promise<void> {
|
|
92
|
+
switch (event.type) {
|
|
93
|
+
case 'blocks-added':
|
|
94
|
+
await this.handleBlocksAdded(event);
|
|
95
|
+
break;
|
|
96
|
+
case 'chain-checkpointed':
|
|
97
|
+
await this.handleChainCheckpointed(event);
|
|
98
|
+
break;
|
|
99
|
+
case 'chain-pruned':
|
|
100
|
+
await this.handleChainPruned(event);
|
|
101
|
+
break;
|
|
102
|
+
case 'chain-proven':
|
|
103
|
+
await this.handleChainProven(event);
|
|
104
|
+
break;
|
|
105
|
+
case 'chain-finalized':
|
|
106
|
+
await this.handleChainFinalized(event);
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Protected helper that subclasses can override for block hash computation
|
|
112
|
+
protected computeBlockHash(block: L2BlockNew): Promise<string> {
|
|
113
|
+
return block.hash().then(hash => hash.toString());
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Private implementation
|
|
117
|
+
|
|
118
|
+
private async getBlockId(tag: L2BlockTag): Promise<L2BlockId> {
|
|
119
|
+
const blockNumber = await this.getTip(tag);
|
|
120
|
+
if (blockNumber === undefined || blockNumber === 0) {
|
|
121
|
+
return { number: BlockNumber.ZERO, hash: GENESIS_BLOCK_HEADER_HASH.toString() };
|
|
122
|
+
}
|
|
123
|
+
const blockHash = await this.getStoredBlockHash(blockNumber);
|
|
124
|
+
if (!blockHash) {
|
|
125
|
+
throw new Error(`Block hash not found for block number ${blockNumber}`);
|
|
126
|
+
}
|
|
127
|
+
return { number: blockNumber, hash: blockHash };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
private async getCheckpointId(tag: L2BlockTag): Promise<CheckpointId> {
|
|
131
|
+
const blockNumber = await this.getTip(tag);
|
|
132
|
+
if (blockNumber === undefined || blockNumber === 0) {
|
|
133
|
+
return { number: CheckpointNumber.ZERO, hash: GENESIS_CHECKPOINT_HEADER_HASH.toString() };
|
|
134
|
+
}
|
|
135
|
+
const checkpointNumber = await this.getCheckpointNumberForBlock(blockNumber);
|
|
136
|
+
if (checkpointNumber === undefined) {
|
|
137
|
+
// No checkpoint associated with this block yet
|
|
138
|
+
return { number: CheckpointNumber.ZERO, hash: GENESIS_CHECKPOINT_HEADER_HASH.toString() };
|
|
139
|
+
}
|
|
140
|
+
const checkpoint = await this.getCheckpoint(checkpointNumber);
|
|
141
|
+
if (!checkpoint) {
|
|
142
|
+
throw new Error(`Checkpoint not found for checkpoint number ${checkpointNumber}`);
|
|
143
|
+
}
|
|
144
|
+
return { number: checkpointNumber, hash: checkpoint.checkpoint.hash().toString() };
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
private async handleBlocksAdded(event: L2BlockStreamEvent): Promise<void> {
|
|
148
|
+
if (event.type !== 'blocks-added') {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
await this.runInTransaction(async () => {
|
|
152
|
+
const blocks = event.blocks;
|
|
153
|
+
for (const block of blocks) {
|
|
154
|
+
await this.setBlockHash(block.number, await this.computeBlockHash(block));
|
|
155
|
+
}
|
|
156
|
+
await this.setTip('proposed', blocks.at(-1)!.number);
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
private async handleChainCheckpointed(event: L2BlockStreamEvent): Promise<void> {
|
|
161
|
+
if (event.type !== 'chain-checkpointed') {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
await this.runInTransaction(async () => {
|
|
165
|
+
await this.saveTag('checkpointed', event.block);
|
|
166
|
+
await this.saveCheckpoint(event.checkpoint);
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
private async handleChainPruned(event: L2BlockStreamEvent): Promise<void> {
|
|
171
|
+
if (event.type !== 'chain-pruned') {
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
await this.runInTransaction(async () => {
|
|
175
|
+
await this.saveTag('proposed', event.block);
|
|
176
|
+
await this.saveTag('checkpointed', event.block);
|
|
177
|
+
const storeProven = await this.getBlockId('proven');
|
|
178
|
+
if (storeProven.number > event.block.number) {
|
|
179
|
+
await this.saveTag('proven', event.block);
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
private async handleChainProven(event: L2BlockStreamEvent): Promise<void> {
|
|
185
|
+
if (event.type !== 'chain-proven') {
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
await this.runInTransaction(async () => {
|
|
189
|
+
await this.saveTag('proven', event.block);
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
private async handleChainFinalized(event: L2BlockStreamEvent): Promise<void> {
|
|
194
|
+
if (event.type !== 'chain-finalized') {
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
await this.runInTransaction(async () => {
|
|
198
|
+
await this.saveTag('finalized', event.block);
|
|
199
|
+
const finalizedCheckpointNumber = await this.getCheckpointNumberForBlock(event.block.number);
|
|
200
|
+
|
|
201
|
+
await this.deleteBlockHashesBefore(event.block.number);
|
|
202
|
+
await this.deleteBlockToCheckpointBefore(event.block.number);
|
|
203
|
+
|
|
204
|
+
if (finalizedCheckpointNumber !== undefined) {
|
|
205
|
+
await this.deleteCheckpointsBefore(finalizedCheckpointNumber);
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
private async saveTag(name: L2BlockTag, block: L2BlockId): Promise<void> {
|
|
211
|
+
await this.setTip(name, block.number);
|
|
212
|
+
if (block.hash) {
|
|
213
|
+
await this.setBlockHash(block.number, block.hash);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
private async saveCheckpoint(publishedCheckpoint: PublishedCheckpoint): Promise<void> {
|
|
218
|
+
const checkpoint = publishedCheckpoint.checkpoint;
|
|
219
|
+
const lastBlock = checkpoint.blocks.at(-1)!;
|
|
220
|
+
// Only store the mapping for the last block since tips only point to checkpoint boundaries
|
|
221
|
+
await Promise.all([
|
|
222
|
+
this.setCheckpointNumberForBlock(lastBlock.number, checkpoint.number),
|
|
223
|
+
this.saveCheckpointData(publishedCheckpoint),
|
|
224
|
+
]);
|
|
225
|
+
}
|
|
226
|
+
}
|