@aztec/stdlib 4.0.0-nightly.20260112 → 4.0.0-nightly.20260114
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/abi/utils.d.ts +7 -1
- package/dest/abi/utils.d.ts.map +1 -1
- package/dest/abi/utils.js +7 -0
- package/dest/avm/avm.d.ts +300 -300
- 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/body.d.ts +1 -1
- package/dest/block/body.d.ts.map +1 -1
- package/dest/block/body.js +2 -1
- package/dest/block/checkpointed_l2_block.d.ts +1 -1
- package/dest/block/checkpointed_l2_block.d.ts.map +1 -1
- package/dest/block/checkpointed_l2_block.js +5 -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 +3 -3
- package/dest/block/l2_block_code_to_purge.d.ts +1 -1
- package/dest/block/l2_block_code_to_purge.d.ts.map +1 -1
- package/dest/block/l2_block_code_to_purge.js +1 -1
- package/dest/block/l2_block_header.d.ts +6 -2
- package/dest/block/l2_block_header.d.ts.map +1 -1
- package/dest/block/l2_block_header.js +14 -7
- 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 +247 -42
- 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 +18 -17
- package/dest/checkpoint/checkpoint.d.ts +2 -1
- package/dest/checkpoint/checkpoint.d.ts.map +1 -1
- package/dest/checkpoint/checkpoint.js +11 -1
- 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/checkpoint/published_checkpoint.d.ts +1 -1
- package/dest/checkpoint/published_checkpoint.d.ts.map +1 -1
- package/dest/checkpoint/published_checkpoint.js +4 -3
- package/dest/contract/index.d.ts +1 -3
- package/dest/contract/index.d.ts.map +1 -1
- package/dest/contract/index.js +0 -2
- package/dest/deserialization/index.d.ts +11 -0
- package/dest/deserialization/index.d.ts.map +1 -0
- package/dest/deserialization/index.js +10 -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 -9
- 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/block-builder.d.ts +2 -2
- package/dest/interfaces/block-builder.d.ts.map +1 -1
- 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/validator.d.ts +41 -15
- package/dest/interfaces/validator.d.ts.map +1 -1
- package/dest/interfaces/validator.js +3 -2
- 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/messaging/l2_to_l1_membership.d.ts +1 -1
- package/dest/messaging/l2_to_l1_membership.d.ts.map +1 -1
- package/dest/messaging/l2_to_l1_membership.js +5 -3
- package/dest/messaging/out_hash.d.ts +41 -4
- package/dest/messaging/out_hash.d.ts.map +1 -1
- package/dest/messaging/out_hash.js +52 -26
- package/dest/note/note_dao.d.ts +8 -5
- package/dest/note/note_dao.d.ts.map +1 -1
- package/dest/note/note_dao.js +15 -12
- 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 +126 -38
- package/dest/p2p/checkpoint_attestation.d.ts +78 -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 +155 -0
- package/dest/p2p/checkpoint_proposal.d.ts.map +1 -0
- package/dest/p2p/checkpoint_proposal.js +222 -0
- package/dest/p2p/consensus_payload.d.ts +5 -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 +75 -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/block_headers_hash.js +1 -1
- package/dest/rollup/block_rollup_public_inputs.d.ts +2 -2
- package/dest/rollup/block_rollup_public_inputs.js +2 -2
- package/dest/rollup/checkpoint_header.d.ts +16 -2
- package/dest/rollup/checkpoint_header.d.ts.map +1 -1
- package/dest/rollup/checkpoint_header.js +25 -5
- package/dest/rollup/checkpoint_rollup_public_inputs.d.ts +11 -6
- package/dest/rollup/checkpoint_rollup_public_inputs.d.ts.map +1 -1
- package/dest/rollup/checkpoint_rollup_public_inputs.js +10 -6
- package/dest/rollup/checkpoint_root_rollup_private_inputs.d.ts +14 -3
- package/dest/rollup/checkpoint_root_rollup_private_inputs.d.ts.map +1 -1
- package/dest/rollup/checkpoint_root_rollup_private_inputs.js +13 -2
- package/dest/rollup/root_rollup_public_inputs.d.ts +5 -2
- package/dest/rollup/root_rollup_public_inputs.d.ts.map +1 -1
- package/dest/rollup/root_rollup_public_inputs.js +4 -1
- package/dest/tests/factories.d.ts +13 -1
- package/dest/tests/factories.d.ts.map +1 -1
- package/dest/tests/factories.js +53 -3
- 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/dest/tx/tx_effect.d.ts +1 -6
- package/dest/tx/tx_effect.d.ts.map +1 -1
- package/dest/tx/tx_effect.js +0 -7
- package/package.json +9 -8
- package/src/abi/utils.ts +17 -0
- package/src/block/attestation_info.ts +9 -6
- package/src/block/body.ts +2 -1
- package/src/block/checkpointed_l2_block.ts +5 -4
- package/src/block/l2_block.ts +4 -3
- package/src/block/l2_block_code_to_purge.ts +1 -0
- package/src/block/l2_block_header.ts +13 -0
- package/src/block/l2_block_new.ts +5 -1
- package/src/block/l2_block_source.ts +69 -17
- 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 +40 -35
- package/src/checkpoint/checkpoint.ts +12 -1
- package/src/checkpoint/checkpoint_info.ts +45 -2
- package/src/checkpoint/index.ts +1 -0
- package/src/checkpoint/published_checkpoint.ts +4 -3
- package/src/contract/index.ts +0 -2
- package/src/deserialization/index.ts +21 -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/block-builder.ts +1 -0
- package/src/interfaces/configs.ts +5 -0
- package/src/interfaces/p2p.ts +8 -12
- package/src/interfaces/validator.ts +57 -11
- package/src/kernel/hints/build_note_hash_read_request_hints.ts +5 -8
- package/src/messaging/l2_to_l1_membership.ts +5 -3
- package/src/messaging/out_hash.ts +60 -29
- package/src/note/note_dao.ts +18 -13
- package/src/p2p/attestation_utils.ts +3 -3
- package/src/p2p/block_proposal.ts +191 -42
- package/src/p2p/{block_attestation.ts → checkpoint_attestation.ts} +31 -25
- package/src/p2p/checkpoint_proposal.ts +342 -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 +88 -0
- package/src/p2p/topic_type.ts +3 -2
- package/src/rollup/block_headers_hash.ts +1 -1
- package/src/rollup/block_rollup_public_inputs.ts +2 -2
- package/src/rollup/checkpoint_header.ts +33 -0
- package/src/rollup/checkpoint_rollup_public_inputs.ts +12 -6
- package/src/rollup/checkpoint_root_rollup_private_inputs.ts +14 -1
- package/src/rollup/root_rollup_public_inputs.ts +4 -1
- package/src/tests/factories.ts +46 -2
- package/src/tests/mocks.ts +146 -50
- package/src/tx/private_execution_result.ts +0 -15
- package/src/tx/tx_effect.ts +0 -9
- package/dest/contract/contract_class_metadata.d.ts +0 -8
- package/dest/contract/contract_class_metadata.d.ts.map +0 -1
- package/dest/contract/contract_class_metadata.js +0 -1
- package/dest/contract/contract_metadata.d.ts +0 -7
- package/dest/contract/contract_metadata.d.ts.map +0 -1
- package/dest/contract/contract_metadata.js +0 -1
- package/dest/p2p/block_attestation.d.ts +0 -77
- package/dest/p2p/block_attestation.d.ts.map +0 -1
- package/src/contract/contract_class_metadata.ts +0 -8
- package/src/contract/contract_metadata.ts +0 -7
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { BlockNumber } from '@aztec/foundation/branded-types';
|
|
1
|
+
import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
|
|
2
2
|
import { AbortError } from '@aztec/foundation/error';
|
|
3
3
|
import { createLogger } from '@aztec/foundation/log';
|
|
4
4
|
import { RunningPromise } from '@aztec/foundation/running-promise';
|
|
5
5
|
|
|
6
|
-
import
|
|
6
|
+
import type { PublishedCheckpoint } from '../../checkpoint/published_checkpoint.js';
|
|
7
|
+
import { type L2BlockId, type L2BlockPruneReason, type L2BlockSource, makeL2BlockId } from '../l2_block_source.js';
|
|
7
8
|
import type { L2BlockStreamEvent, L2BlockStreamEventHandler, L2BlockStreamLocalDataProvider } from './interfaces.js';
|
|
8
9
|
|
|
9
10
|
/** Creates a stream of events for new blocks, chain tips updates, and reorgs, out of polling an archiver or a node. */
|
|
@@ -13,7 +14,10 @@ export class L2BlockStream {
|
|
|
13
14
|
private hasStarted = false;
|
|
14
15
|
|
|
15
16
|
constructor(
|
|
16
|
-
private l2BlockSource: Pick<
|
|
17
|
+
private l2BlockSource: Pick<
|
|
18
|
+
L2BlockSource,
|
|
19
|
+
'getL2BlocksNew' | 'getBlockHeader' | 'getL2Tips' | 'getPublishedCheckpoints' | 'getCheckpointedBlocks'
|
|
20
|
+
>,
|
|
17
21
|
private localData: L2BlockStreamLocalDataProvider,
|
|
18
22
|
private handler: L2BlockStreamEventHandler,
|
|
19
23
|
private readonly log = createLogger('types:block_stream'),
|
|
@@ -24,6 +28,8 @@ export class L2BlockStream {
|
|
|
24
28
|
startingBlock?: number;
|
|
25
29
|
/** Instead of downloading all blocks, only fetch the smallest subset that results in reliable reorg detection. */
|
|
26
30
|
skipFinalized?: boolean;
|
|
31
|
+
/** When true, checkpoint events will not be emitted. Blocks are still fetched via checkpoints but only blocks-added events are emitted. */
|
|
32
|
+
ignoreCheckpoints?: boolean;
|
|
27
33
|
} = {},
|
|
28
34
|
) {
|
|
29
35
|
// Note that RunningPromise is in stopped state by default. This promise won't run until someone invokes `start`,
|
|
@@ -61,36 +67,38 @@ export class L2BlockStream {
|
|
|
61
67
|
try {
|
|
62
68
|
const sourceTips = await this.l2BlockSource.getL2Tips();
|
|
63
69
|
const localTips = await this.localData.getL2Tips();
|
|
64
|
-
this.log.trace(`Running L2 block stream`, {
|
|
65
|
-
sourceLatest: sourceTips.latest.number,
|
|
66
|
-
localLatest: localTips.latest.number,
|
|
67
|
-
sourceFinalized: sourceTips.finalized.number,
|
|
68
|
-
localFinalized: localTips.finalized.number,
|
|
69
|
-
sourceProven: sourceTips.proven.number,
|
|
70
|
-
localProven: localTips.proven.number,
|
|
71
|
-
sourceLatestHash: sourceTips.latest.hash,
|
|
72
|
-
localLatestHash: localTips.latest.hash,
|
|
73
|
-
sourceProvenHash: sourceTips.proven.hash,
|
|
74
|
-
localProvenHash: localTips.proven.hash,
|
|
75
|
-
sourceFinalizedHash: sourceTips.finalized.hash,
|
|
76
|
-
localFinalizedHash: localTips.finalized.hash,
|
|
77
|
-
});
|
|
70
|
+
this.log.trace(`Running L2 block stream`, { sourceTips, localTips });
|
|
78
71
|
|
|
79
72
|
// Check if there was a reorg and emit a chain-pruned event if so.
|
|
80
|
-
let latestBlockNumber = localTips.
|
|
81
|
-
const sourceCache = new BlockHashCache([sourceTips.
|
|
73
|
+
let latestBlockNumber = localTips.proposed.number;
|
|
74
|
+
const sourceCache = new BlockHashCache([sourceTips.proposed]);
|
|
82
75
|
while (!(await this.areBlockHashesEqualAt(latestBlockNumber, { sourceCache }))) {
|
|
83
76
|
latestBlockNumber--;
|
|
84
77
|
}
|
|
85
78
|
|
|
86
|
-
if (latestBlockNumber < localTips.
|
|
87
|
-
latestBlockNumber = BlockNumber(Math.min(latestBlockNumber, sourceTips.
|
|
79
|
+
if (latestBlockNumber < localTips.proposed.number) {
|
|
80
|
+
latestBlockNumber = BlockNumber(Math.min(latestBlockNumber, sourceTips.proposed.number)); // see #13471
|
|
88
81
|
const hash = sourceCache.get(latestBlockNumber) ?? (await this.getBlockHashFromSource(latestBlockNumber));
|
|
89
82
|
if (latestBlockNumber !== 0 && !hash) {
|
|
90
83
|
throw new Error(`Block hash not found in block source for block number ${latestBlockNumber}`);
|
|
91
84
|
}
|
|
92
|
-
this.log.verbose(
|
|
93
|
-
|
|
85
|
+
this.log.verbose(
|
|
86
|
+
`Reorg detected. Pruning blocks from ${latestBlockNumber + 1} to ${localTips.proposed.number}.`,
|
|
87
|
+
);
|
|
88
|
+
// This check is not 100% accurate
|
|
89
|
+
// If the local tips are sufficiently behind the source tips, such that we are missing at least one checkpoint
|
|
90
|
+
// that has now been re-orged due to a proof failure then this will indicate a failure to checkpoint rather than a failure to prove
|
|
91
|
+
// TODO: (mbps/PhilWindle): Improve re-org detection accuracy when we come to do re-orgs
|
|
92
|
+
let reason: L2BlockPruneReason = 'unproven';
|
|
93
|
+
if (latestBlockNumber === localTips.checkpointed.block.number && !this.opts.ignoreCheckpoints) {
|
|
94
|
+
reason = 'uncheckpointed';
|
|
95
|
+
}
|
|
96
|
+
await this.emitEvent({
|
|
97
|
+
type: 'chain-pruned',
|
|
98
|
+
block: makeL2BlockId(latestBlockNumber, hash),
|
|
99
|
+
reason,
|
|
100
|
+
checkpoint: sourceTips.checkpointed.checkpoint,
|
|
101
|
+
});
|
|
94
102
|
}
|
|
95
103
|
|
|
96
104
|
// If we are just starting, use the starting block number from the options.
|
|
@@ -105,40 +113,115 @@ export class L2BlockStream {
|
|
|
105
113
|
}
|
|
106
114
|
|
|
107
115
|
let nextBlockNumber = latestBlockNumber + 1;
|
|
116
|
+
let nextCheckpointToEmit = CheckpointNumber(localTips.checkpointed.checkpoint.number + 1);
|
|
108
117
|
if (this.opts.skipFinalized) {
|
|
109
118
|
// When skipping finalized blocks we need to provide reliable reorg detection while fetching as few blocks as
|
|
110
119
|
// possible. Finalized blocks cannot be reorged by definition, so we can skip most of them. We do need the very
|
|
111
120
|
// last finalized block however in order to guarantee that we will eventually find a block in which our local
|
|
112
121
|
// store matches the source.
|
|
113
122
|
// If the last finalized block is behind our local tip, there is nothing to skip.
|
|
114
|
-
nextBlockNumber = Math.max(sourceTips.finalized.number, nextBlockNumber);
|
|
123
|
+
nextBlockNumber = Math.max(sourceTips.finalized.block.number, nextBlockNumber);
|
|
124
|
+
// If the next checkpoint to emit is behind the finalized tip then skip forward
|
|
125
|
+
nextCheckpointToEmit = CheckpointNumber(Math.max(nextCheckpointToEmit, sourceTips.finalized.checkpoint.number));
|
|
115
126
|
}
|
|
116
127
|
|
|
117
|
-
//
|
|
118
|
-
|
|
119
|
-
|
|
128
|
+
// Loop 1: Emit checkpoint events for checkpoints whose blocks are already in local storage.
|
|
129
|
+
// This handles the case where blocks were synced as uncheckpointed and later became checkpointed.
|
|
130
|
+
// The guard `lastBlockInCheckpoint.number > localTips.proposed.number` ensures we don't emit
|
|
131
|
+
// checkpoints for blocks we don't have (e.g., when startingBlock skips earlier blocks).
|
|
132
|
+
// Since only one checkpoint can ever be uncheckpointed, this loop should iterate at most once.
|
|
133
|
+
if (!this.opts.ignoreCheckpoints) {
|
|
134
|
+
let loop1Iterations = 0;
|
|
135
|
+
while (nextCheckpointToEmit <= sourceTips.checkpointed.checkpoint.number) {
|
|
136
|
+
const checkpoints = await this.l2BlockSource.getPublishedCheckpoints(nextCheckpointToEmit, 1);
|
|
137
|
+
if (checkpoints.length === 0) {
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
const lastBlockInCheckpoint = checkpoints[0].checkpoint.blocks.at(-1)!;
|
|
141
|
+
// If this checkpoint has blocks we haven't seen yet, stop - they need to be fetched first
|
|
142
|
+
if (lastBlockInCheckpoint.number > localTips.proposed.number) {
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
loop1Iterations++;
|
|
146
|
+
if (loop1Iterations > 1) {
|
|
147
|
+
this.log.warn(
|
|
148
|
+
`Emitting multiple checkpoints (${loop1Iterations}) for already-local blocks. ` +
|
|
149
|
+
`Next checkpoint: ${nextCheckpointToEmit}, source checkpointed: ${sourceTips.checkpointed.checkpoint.number}`,
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
const lastBlockHash = await lastBlockInCheckpoint.hash();
|
|
153
|
+
await this.emitEvent({
|
|
154
|
+
type: 'chain-checkpointed',
|
|
155
|
+
checkpoint: checkpoints[0],
|
|
156
|
+
block: makeL2BlockId(lastBlockInCheckpoint.number, lastBlockHash.toString()),
|
|
157
|
+
});
|
|
158
|
+
nextCheckpointToEmit = CheckpointNumber(nextCheckpointToEmit + 1);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Loop 2: Fetch new checkpointed blocks. For each block, get its checkpoint, emit all blocks
|
|
163
|
+
// from that checkpoint that we need, then emit the checkpoint event.
|
|
164
|
+
// We cache the current checkpoint to avoid redundant fetches when batchSize < checkpoint size.
|
|
165
|
+
let checkpoint: PublishedCheckpoint | undefined;
|
|
166
|
+
while (nextBlockNumber <= sourceTips.checkpointed.block.number) {
|
|
167
|
+
const limit = Math.min(this.opts.batchSize ?? 50, sourceTips.checkpointed.block.number - nextBlockNumber + 1);
|
|
168
|
+
|
|
169
|
+
// Check if we need to fetch a new checkpoint (nextBlockNumber is beyond the cached one)
|
|
170
|
+
if (!checkpoint || nextBlockNumber > checkpoint.checkpoint.blocks.at(-1)!.number) {
|
|
171
|
+
const blocks = await this.l2BlockSource.getCheckpointedBlocks(BlockNumber(nextBlockNumber), 1);
|
|
172
|
+
if (blocks.length === 0) {
|
|
173
|
+
break;
|
|
174
|
+
}
|
|
175
|
+
const checkpoints = await this.l2BlockSource.getPublishedCheckpoints(blocks[0].checkpointNumber, 1);
|
|
176
|
+
if (checkpoints.length === 0) {
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
checkpoint = checkpoints[0];
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// Get all blocks from this checkpoint that we need, respecting batchSize
|
|
183
|
+
const blocksForCheckpoint = checkpoint.checkpoint.blocks
|
|
184
|
+
.filter(b => b.number >= nextBlockNumber)
|
|
185
|
+
.slice(0, limit);
|
|
186
|
+
if (blocksForCheckpoint.length === 0) {
|
|
187
|
+
break;
|
|
188
|
+
}
|
|
189
|
+
await this.emitEvent({ type: 'blocks-added', blocks: blocksForCheckpoint });
|
|
190
|
+
nextBlockNumber = blocksForCheckpoint.at(-1)!.number + 1;
|
|
191
|
+
|
|
192
|
+
// If we've reached the end of this checkpoint, emit the checkpoint event
|
|
193
|
+
const lastBlockInCheckpoint = checkpoint.checkpoint.blocks.at(-1)!;
|
|
194
|
+
if (!this.opts.ignoreCheckpoints && nextBlockNumber > lastBlockInCheckpoint.number) {
|
|
195
|
+
const lastBlockHash = await lastBlockInCheckpoint.hash();
|
|
196
|
+
await this.emitEvent({
|
|
197
|
+
type: 'chain-checkpointed',
|
|
198
|
+
checkpoint,
|
|
199
|
+
block: makeL2BlockId(lastBlockInCheckpoint.number, lastBlockHash.toString()),
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// Loop 3: Fetch any remaining uncheckpointed (proposed) blocks.
|
|
205
|
+
while (nextBlockNumber <= sourceTips.proposed.number) {
|
|
206
|
+
const limit = Math.min(this.opts.batchSize ?? 50, sourceTips.proposed.number - nextBlockNumber + 1);
|
|
120
207
|
this.log.trace(`Requesting blocks from ${nextBlockNumber} limit ${limit} proven=${this.opts.proven}`);
|
|
121
|
-
const blocks = await this.l2BlockSource.
|
|
122
|
-
BlockNumber(nextBlockNumber),
|
|
123
|
-
limit,
|
|
124
|
-
this.opts.proven,
|
|
125
|
-
);
|
|
208
|
+
const blocks = await this.l2BlockSource.getL2BlocksNew(BlockNumber(nextBlockNumber), limit, this.opts.proven);
|
|
126
209
|
if (blocks.length === 0) {
|
|
127
210
|
break;
|
|
128
211
|
}
|
|
129
212
|
await this.emitEvent({ type: 'blocks-added', blocks });
|
|
130
|
-
nextBlockNumber = blocks.at(-1)!.
|
|
213
|
+
nextBlockNumber = blocks.at(-1)!.number + 1;
|
|
131
214
|
}
|
|
132
215
|
|
|
133
216
|
// Update the proven and finalized tips.
|
|
134
|
-
if (localTips.proven !== undefined && sourceTips.proven.number !== localTips.proven.number) {
|
|
217
|
+
if (localTips.proven !== undefined && sourceTips.proven.block.number !== localTips.proven.block.number) {
|
|
135
218
|
await this.emitEvent({
|
|
136
219
|
type: 'chain-proven',
|
|
137
|
-
block: sourceTips.proven,
|
|
220
|
+
block: sourceTips.proven.block,
|
|
138
221
|
});
|
|
139
222
|
}
|
|
140
|
-
if (localTips.finalized !== undefined && sourceTips.finalized.number !== localTips.finalized.number) {
|
|
141
|
-
await this.emitEvent({ type: 'chain-finalized', block: sourceTips.finalized });
|
|
223
|
+
if (localTips.finalized !== undefined && sourceTips.finalized.block.number !== localTips.finalized.block.number) {
|
|
224
|
+
await this.emitEvent({ type: 'chain-finalized', block: sourceTips.finalized.block });
|
|
142
225
|
}
|
|
143
226
|
} catch (err: any) {
|
|
144
227
|
if (err.name === 'AbortError') {
|
|
@@ -186,7 +269,7 @@ export class L2BlockStream {
|
|
|
186
269
|
|
|
187
270
|
private async emitEvent(event: L2BlockStreamEvent) {
|
|
188
271
|
this.log.debug(
|
|
189
|
-
`Emitting ${event.type} (${event.type === 'blocks-added' ? event.blocks.length : event.block.number})`,
|
|
272
|
+
`Emitting ${event.type} (${event.type === 'blocks-added' ? event.blocks.length : event.type === 'chain-checkpointed' ? event.checkpoint.checkpoint.number : event.block.number})`,
|
|
190
273
|
);
|
|
191
274
|
await this.handler.handleBlockStreamEvent(event);
|
|
192
275
|
if (!this.isRunning() && !this.isSyncing) {
|
|
@@ -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
|
+
}
|