@aztec/archiver 0.0.1-commit.f504929 → 0.0.1-commit.f5a9928
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 +19 -13
- package/dest/archiver.d.ts +73 -18
- package/dest/archiver.d.ts.map +1 -1
- package/dest/archiver.js +321 -81
- package/dest/config.d.ts +7 -3
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +43 -14
- package/dest/errors.d.ts +55 -9
- package/dest/errors.d.ts.map +1 -1
- package/dest/errors.js +81 -14
- package/dest/factory.d.ts +20 -9
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +85 -37
- package/dest/index.d.ts +11 -3
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +10 -2
- package/dest/l1/calldata_retriever.d.ts +9 -1
- package/dest/l1/calldata_retriever.d.ts.map +1 -1
- package/dest/l1/calldata_retriever.js +22 -11
- package/dest/l1/data_retrieval.d.ts +25 -11
- package/dest/l1/data_retrieval.d.ts.map +1 -1
- package/dest/l1/data_retrieval.js +25 -32
- package/dest/l1/trace_tx.d.ts +12 -66
- package/dest/l1/trace_tx.d.ts.map +1 -1
- package/dest/l1/validate_historical_logs.d.ts +23 -0
- package/dest/l1/validate_historical_logs.d.ts.map +1 -0
- package/dest/l1/validate_historical_logs.js +108 -0
- package/dest/modules/contract_data_source_adapter.d.ts +25 -0
- package/dest/modules/contract_data_source_adapter.d.ts.map +1 -0
- package/dest/modules/contract_data_source_adapter.js +32 -0
- package/dest/modules/data_source_base.d.ts +73 -50
- package/dest/modules/data_source_base.d.ts.map +1 -1
- package/dest/modules/data_source_base.js +250 -144
- package/dest/modules/data_store_updater.d.ts +42 -17
- package/dest/modules/data_store_updater.d.ts.map +1 -1
- package/dest/modules/data_store_updater.js +198 -123
- package/dest/modules/instrumentation.d.ts +15 -2
- package/dest/modules/instrumentation.d.ts.map +1 -1
- package/dest/modules/instrumentation.js +48 -9
- package/dest/modules/l1_synchronizer.d.ts +14 -8
- package/dest/modules/l1_synchronizer.d.ts.map +1 -1
- package/dest/modules/l1_synchronizer.js +477 -213
- package/dest/modules/outbox_trees_resolver.d.ts +64 -0
- package/dest/modules/outbox_trees_resolver.d.ts.map +1 -0
- package/dest/modules/outbox_trees_resolver.js +184 -0
- package/dest/modules/validation.d.ts +30 -7
- package/dest/modules/validation.d.ts.map +1 -1
- package/dest/modules/validation.js +35 -17
- package/dest/store/block_store.d.ts +186 -78
- package/dest/store/block_store.d.ts.map +1 -1
- package/dest/store/block_store.js +763 -290
- package/dest/store/contract_class_store.d.ts +17 -4
- package/dest/store/contract_class_store.d.ts.map +1 -1
- package/dest/store/contract_class_store.js +42 -68
- package/dest/store/contract_instance_store.d.ts +28 -1
- package/dest/store/contract_instance_store.d.ts.map +1 -1
- package/dest/store/contract_instance_store.js +58 -6
- package/dest/store/data_stores.d.ts +68 -0
- package/dest/store/data_stores.d.ts.map +1 -0
- package/dest/store/data_stores.js +54 -0
- package/dest/store/function_names_cache.d.ts +17 -0
- package/dest/store/function_names_cache.d.ts.map +1 -0
- package/dest/store/function_names_cache.js +30 -0
- package/dest/store/l2_tips_cache.d.ts +13 -7
- package/dest/store/l2_tips_cache.d.ts.map +1 -1
- package/dest/store/l2_tips_cache.js +13 -76
- package/dest/store/log_store.d.ts +42 -37
- package/dest/store/log_store.d.ts.map +1 -1
- package/dest/store/log_store.js +262 -408
- package/dest/store/log_store_codec.d.ts +78 -0
- package/dest/store/log_store_codec.d.ts.map +1 -0
- package/dest/store/log_store_codec.js +110 -0
- package/dest/store/message_store.d.ts +11 -1
- package/dest/store/message_store.d.ts.map +1 -1
- package/dest/store/message_store.js +51 -9
- package/dest/test/fake_l1_state.d.ts +20 -1
- package/dest/test/fake_l1_state.d.ts.map +1 -1
- package/dest/test/fake_l1_state.js +117 -21
- package/dest/test/mock_l2_block_source.d.ts +51 -52
- package/dest/test/mock_l2_block_source.d.ts.map +1 -1
- package/dest/test/mock_l2_block_source.js +232 -180
- package/dest/test/mock_structs.d.ts +4 -1
- package/dest/test/mock_structs.d.ts.map +1 -1
- package/dest/test/mock_structs.js +15 -3
- package/dest/test/noop_l1_archiver.d.ts +17 -6
- package/dest/test/noop_l1_archiver.d.ts.map +1 -1
- package/dest/test/noop_l1_archiver.js +31 -11
- package/package.json +15 -14
- package/src/archiver.ts +415 -90
- package/src/config.ts +51 -13
- package/src/errors.ts +122 -21
- package/src/factory.ts +99 -30
- package/src/index.ts +18 -2
- package/src/l1/calldata_retriever.ts +29 -11
- package/src/l1/data_retrieval.ts +44 -46
- package/src/l1/validate_historical_logs.ts +140 -0
- package/src/modules/contract_data_source_adapter.ts +46 -0
- package/src/modules/data_source_base.ts +327 -189
- package/src/modules/data_store_updater.ts +235 -155
- package/src/modules/instrumentation.ts +43 -10
- package/src/modules/l1_synchronizer.ts +646 -259
- package/src/modules/outbox_trees_resolver.ts +220 -0
- package/src/modules/validation.ts +80 -23
- package/src/store/block_store.ts +930 -348
- package/src/store/contract_class_store.ts +49 -103
- package/src/store/contract_instance_store.ts +74 -11
- package/src/store/data_stores.ts +104 -0
- package/src/store/function_names_cache.ts +37 -0
- package/src/store/l2_tips_cache.ts +16 -70
- package/src/store/log_store.ts +302 -559
- package/src/store/log_store_codec.ts +143 -0
- package/src/store/message_store.ts +60 -10
- package/src/structs/inbox_message.ts +1 -1
- package/src/test/fake_l1_state.ts +145 -32
- package/src/test/mock_l2_block_source.ts +292 -227
- package/src/test/mock_structs.ts +25 -8
- package/src/test/noop_l1_archiver.ts +61 -12
- package/dest/store/kv_archiver_store.d.ts +0 -354
- package/dest/store/kv_archiver_store.d.ts.map +0 -1
- package/dest/store/kv_archiver_store.js +0 -464
- package/src/store/kv_archiver_store.ts +0 -671
|
@@ -1,30 +1,28 @@
|
|
|
1
1
|
import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
|
|
2
|
-
import {
|
|
2
|
+
import { filterAsync } from '@aztec/foundation/collection';
|
|
3
3
|
import { createLogger } from '@aztec/foundation/log';
|
|
4
|
-
import {
|
|
5
|
-
ContractClassPublishedEvent,
|
|
6
|
-
PrivateFunctionBroadcastedEvent,
|
|
7
|
-
UtilityFunctionBroadcastedEvent,
|
|
8
|
-
} from '@aztec/protocol-contracts/class-registry';
|
|
4
|
+
import { ContractClassPublishedEvent } from '@aztec/protocol-contracts/class-registry';
|
|
9
5
|
import {
|
|
10
6
|
ContractInstancePublishedEvent,
|
|
11
7
|
ContractInstanceUpdatedEvent,
|
|
12
8
|
} from '@aztec/protocol-contracts/instance-registry';
|
|
13
|
-
import type { L2Block, ValidateCheckpointResult } from '@aztec/stdlib/block';
|
|
14
|
-
import
|
|
9
|
+
import type { CommitteeAttestation, L2Block, ValidateCheckpointResult } from '@aztec/stdlib/block';
|
|
10
|
+
import {
|
|
11
|
+
type L1PublishedData,
|
|
12
|
+
type ProposedCheckpointInput,
|
|
13
|
+
type PublishedCheckpoint,
|
|
14
|
+
validateCheckpoint,
|
|
15
|
+
} from '@aztec/stdlib/checkpoint';
|
|
15
16
|
import {
|
|
16
|
-
type
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
isValidPrivateFunctionMembershipProof,
|
|
20
|
-
isValidUtilityFunctionMembershipProof,
|
|
17
|
+
type ContractClassPublicWithCommitment,
|
|
18
|
+
computeContractAddressFromInstance,
|
|
19
|
+
computeContractClassId,
|
|
21
20
|
} from '@aztec/stdlib/contract';
|
|
21
|
+
import { MAX_CAPACITY_BLOCKS_PER_CHECKPOINT } from '@aztec/stdlib/deserialization';
|
|
22
22
|
import type { ContractClassLog, PrivateLog, PublicLog } from '@aztec/stdlib/logs';
|
|
23
23
|
import type { UInt64 } from '@aztec/stdlib/types';
|
|
24
24
|
|
|
25
|
-
import
|
|
26
|
-
|
|
27
|
-
import type { KVArchiverDataStore } from '../store/kv_archiver_store.js';
|
|
25
|
+
import type { ArchiverDataStores } from '../store/data_stores.js';
|
|
28
26
|
import type { L2TipsCache } from '../store/l2_tips_cache.js';
|
|
29
27
|
|
|
30
28
|
/** Operation type for contract data updates. */
|
|
@@ -46,39 +44,40 @@ export class ArchiverDataStoreUpdater {
|
|
|
46
44
|
private readonly log = createLogger('archiver:store_updater');
|
|
47
45
|
|
|
48
46
|
constructor(
|
|
49
|
-
private
|
|
47
|
+
private stores: ArchiverDataStores,
|
|
50
48
|
private l2TipsCache?: L2TipsCache,
|
|
49
|
+
private opts: { rollupManaLimit?: number } = {},
|
|
51
50
|
) {}
|
|
52
51
|
|
|
53
52
|
/**
|
|
54
|
-
* Adds proposed
|
|
55
|
-
*
|
|
56
|
-
* Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events
|
|
57
|
-
* and individually broadcasted functions from the block logs.
|
|
53
|
+
* Adds a proposed block to the store with contract class/instance extraction from logs.
|
|
54
|
+
* This is an uncheckpointed block that has been proposed by the sequencer but not yet included in a checkpoint on L1.
|
|
55
|
+
* Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events from the block logs.
|
|
58
56
|
*
|
|
59
|
-
* @param
|
|
57
|
+
* @param block - The proposed L2 block to add.
|
|
60
58
|
* @param pendingChainValidationStatus - Optional validation status to set.
|
|
61
59
|
* @returns True if the operation is successful.
|
|
62
60
|
*/
|
|
63
|
-
public async
|
|
64
|
-
|
|
61
|
+
public async addProposedBlock(
|
|
62
|
+
block: L2Block,
|
|
65
63
|
pendingChainValidationStatus?: ValidateCheckpointResult,
|
|
66
64
|
): Promise<boolean> {
|
|
67
|
-
const result = await this.
|
|
68
|
-
await this.
|
|
65
|
+
const result = await this.stores.db.transactionAsync(async () => {
|
|
66
|
+
await this.stores.blocks.addProposedBlock(block);
|
|
69
67
|
|
|
70
68
|
const opResults = await Promise.all([
|
|
71
69
|
// Update the pending chain validation status if provided
|
|
72
|
-
pendingChainValidationStatus &&
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
70
|
+
pendingChainValidationStatus &&
|
|
71
|
+
this.stores.blocks.setPendingChainValidationStatus(pendingChainValidationStatus),
|
|
72
|
+
// Add any logs emitted during the retrieved block
|
|
73
|
+
this.stores.logs.addLogs([block]),
|
|
74
|
+
// Unroll all logs emitted during the retrieved block and extract any contract classes and instances from it
|
|
75
|
+
this.addContractDataToDb(block),
|
|
77
76
|
]);
|
|
78
77
|
|
|
79
|
-
await this.l2TipsCache?.refresh();
|
|
80
78
|
return opResults.every(Boolean);
|
|
81
79
|
});
|
|
80
|
+
await this.l2TipsCache?.refresh();
|
|
82
81
|
return result;
|
|
83
82
|
}
|
|
84
83
|
|
|
@@ -86,53 +85,107 @@ export class ArchiverDataStoreUpdater {
|
|
|
86
85
|
* Reconciles local blocks with incoming checkpoints from L1.
|
|
87
86
|
* Adds new checkpoints to the store with contract class/instance extraction from logs.
|
|
88
87
|
* Prunes any local blocks that conflict with checkpoint data (by comparing archive roots).
|
|
89
|
-
* Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events
|
|
90
|
-
*
|
|
88
|
+
* Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events from the checkpoint block logs.
|
|
89
|
+
* If `promoteProposed` is supplied, the proposed-checkpoint promotion runs inside the same transaction
|
|
90
|
+
* as the added checkpoints so both updates are applied atomically.
|
|
91
91
|
*
|
|
92
|
-
* @param checkpoints - The published checkpoints to add.
|
|
92
|
+
* @param checkpoints - The published checkpoints to add (excluding any being promoted from proposed).
|
|
93
93
|
* @param pendingChainValidationStatus - Optional validation status to set.
|
|
94
|
+
* @param promoteProposed - Optional promotion of the current proposed checkpoint (fast path when blocks are already local).
|
|
94
95
|
* @returns Result with information about any pruned blocks.
|
|
95
96
|
*/
|
|
96
97
|
public async addCheckpoints(
|
|
97
98
|
checkpoints: PublishedCheckpoint[],
|
|
98
99
|
pendingChainValidationStatus?: ValidateCheckpointResult,
|
|
100
|
+
promoteProposed?: {
|
|
101
|
+
l1: L1PublishedData;
|
|
102
|
+
attestations: CommitteeAttestation[];
|
|
103
|
+
checkpoint: PublishedCheckpoint;
|
|
104
|
+
},
|
|
105
|
+
evictProposedFrom?: CheckpointNumber,
|
|
99
106
|
): Promise<ReconcileCheckpointsResult> {
|
|
100
|
-
|
|
107
|
+
// These checkpoints are already on L1, so ingest validates against the physical blob capacity rather than
|
|
108
|
+
// the conservative build/attest policy, and tolerates an empty non-first block. Both rules are enforced by
|
|
109
|
+
// proposers and attesters; rejecting an ingest would stall sync rather than undo the checkpoint.
|
|
110
|
+
const validateOpts = {
|
|
111
|
+
rollupManaLimit: this.opts?.rollupManaLimit,
|
|
112
|
+
maxBlocksPerCheckpoint: MAX_CAPACITY_BLOCKS_PER_CHECKPOINT,
|
|
113
|
+
allowEmptyNonFirstBlocks: true,
|
|
114
|
+
};
|
|
115
|
+
for (const checkpoint of checkpoints) {
|
|
116
|
+
validateCheckpoint(checkpoint.checkpoint, validateOpts);
|
|
117
|
+
}
|
|
118
|
+
if (promoteProposed) {
|
|
119
|
+
validateCheckpoint(promoteProposed.checkpoint.checkpoint, validateOpts);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const result = await this.stores.db.transactionAsync(async () => {
|
|
101
123
|
// Before adding checkpoints, check for conflicts with local blocks if any
|
|
102
124
|
const { prunedBlocks, lastAlreadyInsertedBlockNumber } = await this.pruneMismatchingLocalBlocks(checkpoints);
|
|
103
125
|
|
|
104
|
-
await this.
|
|
126
|
+
const insertedCheckpoints = await this.stores.blocks.addCheckpoints(checkpoints);
|
|
105
127
|
|
|
106
|
-
//
|
|
107
|
-
|
|
128
|
+
// Skip blocks already inserted via addProposedBlock() and blocks of already-stored checkpoints
|
|
129
|
+
// re-included by an L1 reorg: their logs/contract data were extracted when first inserted.
|
|
130
|
+
const newBlocks = insertedCheckpoints
|
|
108
131
|
.flatMap((ch: PublishedCheckpoint) => ch.checkpoint.blocks)
|
|
109
132
|
.filter(b => lastAlreadyInsertedBlockNumber === undefined || b.number > lastAlreadyInsertedBlockNumber);
|
|
110
133
|
|
|
111
134
|
await Promise.all([
|
|
112
135
|
// Update the pending chain validation status if provided
|
|
113
|
-
pendingChainValidationStatus &&
|
|
136
|
+
pendingChainValidationStatus &&
|
|
137
|
+
this.stores.blocks.setPendingChainValidationStatus(pendingChainValidationStatus),
|
|
114
138
|
// Add any logs emitted during the retrieved blocks
|
|
115
|
-
this.
|
|
139
|
+
this.stores.logs.addLogs(newBlocks),
|
|
116
140
|
// Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
|
|
117
141
|
...newBlocks.map(block => this.addContractDataToDb(block)),
|
|
142
|
+
// Promote the proposed checkpoint if requested (uses explicit checkpoint number)
|
|
143
|
+
promoteProposed
|
|
144
|
+
? this.stores.blocks.promoteProposedToCheckpointed(
|
|
145
|
+
promoteProposed.checkpoint.checkpoint.number,
|
|
146
|
+
promoteProposed.l1,
|
|
147
|
+
promoteProposed.attestations,
|
|
148
|
+
promoteProposed.checkpoint.checkpoint.archive.root,
|
|
149
|
+
)
|
|
150
|
+
: undefined,
|
|
151
|
+
// Evict pending checkpoints that diverged from what L1 mined
|
|
152
|
+
evictProposedFrom !== undefined
|
|
153
|
+
? this.stores.blocks.evictProposedCheckpointsFrom(evictProposedFrom)
|
|
154
|
+
: undefined,
|
|
118
155
|
]);
|
|
119
156
|
|
|
120
|
-
await this.l2TipsCache?.refresh();
|
|
121
157
|
return { prunedBlocks, lastAlreadyInsertedBlockNumber };
|
|
122
158
|
});
|
|
159
|
+
await this.l2TipsCache?.refresh();
|
|
160
|
+
return result;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
public async addProposedCheckpoint(proposedCheckpoint: ProposedCheckpointInput) {
|
|
164
|
+
const result = await this.stores.db.transactionAsync(async () => {
|
|
165
|
+
await this.stores.blocks.addProposedCheckpoint(proposedCheckpoint);
|
|
166
|
+
});
|
|
167
|
+
await this.l2TipsCache?.refresh();
|
|
123
168
|
return result;
|
|
124
169
|
}
|
|
125
170
|
|
|
126
171
|
/**
|
|
127
172
|
* Checks for local proposed blocks that do not match the ones to be checkpointed and prunes them.
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
*
|
|
173
|
+
* Conflict detection is keyed on `blockNumber`: when a local proposed block and an L1
|
|
174
|
+
* checkpointed block share a block number but live at different slots (e.g. a different proposer
|
|
175
|
+
* mined the same block number one slot earlier), we still treat them as a conflict and prune.
|
|
176
|
+
* The trailing per-checkpoint prune that handles "local has extra trailing blocks within the
|
|
177
|
+
* same slot as the published checkpoint" remains scoped by slot to preserve pipelining: local
|
|
178
|
+
* blocks that live at a later slot than the checkpoint being processed represent speculation
|
|
179
|
+
* atop the just-confirmed tip (and may be referenced by a pending proposed checkpoint), so we
|
|
180
|
+
* leave them in place. This method handles multiple checkpoints but returns after pruning the
|
|
181
|
+
* first conflict found. This is correct because pruning from the first conflict point removes
|
|
182
|
+
* all subsequent blocks, and when checkpoints are added afterward, they include all the correct
|
|
183
|
+
* blocks.
|
|
131
184
|
*/
|
|
132
185
|
private async pruneMismatchingLocalBlocks(checkpoints: PublishedCheckpoint[]): Promise<ReconcileCheckpointsResult> {
|
|
133
186
|
const [lastCheckpointedBlockNumber, lastBlockNumber] = await Promise.all([
|
|
134
|
-
this.
|
|
135
|
-
this.
|
|
187
|
+
this.stores.blocks.getCheckpointedL2BlockNumber(),
|
|
188
|
+
this.stores.blocks.getLatestL2BlockNumber(),
|
|
136
189
|
]);
|
|
137
190
|
|
|
138
191
|
// Exit early if there are no local uncheckpointed blocks
|
|
@@ -141,29 +194,29 @@ export class ArchiverDataStoreUpdater {
|
|
|
141
194
|
}
|
|
142
195
|
|
|
143
196
|
// Get all uncheckpointed local blocks
|
|
144
|
-
const uncheckpointedLocalBlocks = await this.
|
|
145
|
-
BlockNumber.add(lastCheckpointedBlockNumber, 1),
|
|
146
|
-
lastBlockNumber - lastCheckpointedBlockNumber,
|
|
147
|
-
);
|
|
197
|
+
const uncheckpointedLocalBlocks = await this.stores.blocks.getBlocksData({
|
|
198
|
+
from: BlockNumber.add(lastCheckpointedBlockNumber, 1),
|
|
199
|
+
limit: lastBlockNumber - lastCheckpointedBlockNumber,
|
|
200
|
+
});
|
|
148
201
|
|
|
149
202
|
let lastAlreadyInsertedBlockNumber: BlockNumber | undefined;
|
|
150
203
|
|
|
151
204
|
for (const publishedCheckpoint of checkpoints) {
|
|
152
205
|
const checkpointBlocks = publishedCheckpoint.checkpoint.blocks;
|
|
153
206
|
const slot = publishedCheckpoint.checkpoint.slot;
|
|
154
|
-
const localBlocksInSlot = uncheckpointedLocalBlocks.filter(b => b.slot === slot);
|
|
155
207
|
|
|
156
208
|
if (checkpointBlocks.length === 0) {
|
|
157
209
|
this.log.warn(`Checkpoint ${publishedCheckpoint.checkpoint.number} for slot ${slot} has no blocks`);
|
|
158
210
|
continue;
|
|
159
211
|
}
|
|
160
212
|
|
|
161
|
-
// Find the first checkpoint block that conflicts with an existing local block and prune local afterwards
|
|
213
|
+
// Find the first checkpoint block that conflicts with an existing local block and prune local afterwards.
|
|
214
|
+
// Conflict detection joins on block number only — same block number at a different slot is still a conflict.
|
|
162
215
|
for (const checkpointBlock of checkpointBlocks) {
|
|
163
216
|
const blockNumber = checkpointBlock.number;
|
|
164
|
-
const existingBlock =
|
|
217
|
+
const existingBlock = uncheckpointedLocalBlocks.find(b => b.header.getBlockNumber() === blockNumber);
|
|
165
218
|
const blockInfos = {
|
|
166
|
-
existingBlock: existingBlock?.
|
|
219
|
+
existingBlock: existingBlock?.header.toInspect(),
|
|
167
220
|
checkpointBlock: checkpointBlock.toBlockInfo(),
|
|
168
221
|
};
|
|
169
222
|
|
|
@@ -173,22 +226,26 @@ export class ArchiverDataStoreUpdater {
|
|
|
173
226
|
this.log.verbose(`Block number ${blockNumber} already inserted and matches checkpoint`, blockInfos);
|
|
174
227
|
lastAlreadyInsertedBlockNumber = blockNumber;
|
|
175
228
|
} else {
|
|
176
|
-
this.log.
|
|
229
|
+
this.log.info(`Conflict detected at block ${blockNumber} between checkpointed and local block`, blockInfos);
|
|
177
230
|
const prunedBlocks = await this.removeBlocksAfter(BlockNumber(blockNumber - 1));
|
|
231
|
+
await this.evictProposedCheckpointsForPrunedBlocks(prunedBlocks);
|
|
178
232
|
return { prunedBlocks, lastAlreadyInsertedBlockNumber };
|
|
179
233
|
}
|
|
180
234
|
}
|
|
181
235
|
|
|
182
|
-
// If
|
|
183
|
-
//
|
|
236
|
+
// If the sequencer locally proposed extra blocks within this checkpoint's slot (e.g. local has
|
|
237
|
+
// [N, N+1] but L1 confirmed just [N]), prune the extras. Scoped to the checkpoint's slot so we
|
|
238
|
+
// do not throw away speculative blocks at later slots that belong to a pending proposed checkpoint.
|
|
184
239
|
const lastCheckpointBlockNumber = checkpointBlocks.at(-1)!.number;
|
|
185
|
-
const
|
|
240
|
+
const localBlocksInSlot = uncheckpointedLocalBlocks.filter(b => b.header.getSlot() === slot);
|
|
241
|
+
const lastLocalBlockNumber = localBlocksInSlot.at(-1)?.header.getBlockNumber();
|
|
186
242
|
|
|
187
243
|
if (lastLocalBlockNumber !== undefined && lastLocalBlockNumber > lastCheckpointBlockNumber) {
|
|
188
244
|
this.log.warn(
|
|
189
245
|
`Local chain for slot ${slot} ends at block ${lastLocalBlockNumber} but checkpoint ends at ${lastCheckpointBlockNumber}. Pruning blocks after block ${lastCheckpointBlockNumber}.`,
|
|
190
246
|
);
|
|
191
247
|
const prunedBlocks = await this.removeBlocksAfter(lastCheckpointBlockNumber);
|
|
248
|
+
await this.evictProposedCheckpointsForPrunedBlocks(prunedBlocks);
|
|
192
249
|
return { prunedBlocks, lastAlreadyInsertedBlockNumber };
|
|
193
250
|
}
|
|
194
251
|
}
|
|
@@ -206,33 +263,74 @@ export class ArchiverDataStoreUpdater {
|
|
|
206
263
|
* @throws Error if any block to be removed is checkpointed.
|
|
207
264
|
*/
|
|
208
265
|
public async removeUncheckpointedBlocksAfter(blockNumber: BlockNumber): Promise<L2Block[]> {
|
|
209
|
-
const result = await this.
|
|
266
|
+
const result = await this.stores.db.transactionAsync(async () => {
|
|
210
267
|
// Verify we're only removing uncheckpointed blocks
|
|
211
|
-
const lastCheckpointedBlockNumber = await this.
|
|
268
|
+
const lastCheckpointedBlockNumber = await this.stores.blocks.getCheckpointedL2BlockNumber();
|
|
212
269
|
if (blockNumber < lastCheckpointedBlockNumber) {
|
|
213
270
|
throw new Error(
|
|
214
271
|
`Cannot remove blocks after ${blockNumber} because checkpointed blocks exist up to ${lastCheckpointedBlockNumber}`,
|
|
215
272
|
);
|
|
216
273
|
}
|
|
217
274
|
|
|
218
|
-
const
|
|
219
|
-
await this.
|
|
220
|
-
|
|
275
|
+
const prunedBlocks = await this.removeBlocksAfter(blockNumber);
|
|
276
|
+
await this.evictProposedCheckpointsForPrunedBlocks(prunedBlocks);
|
|
277
|
+
|
|
278
|
+
return prunedBlocks;
|
|
221
279
|
});
|
|
280
|
+
await this.l2TipsCache?.refresh();
|
|
222
281
|
return result;
|
|
223
282
|
}
|
|
224
283
|
|
|
284
|
+
/**
|
|
285
|
+
* Removes all blocks without a proposed checkpoint strictly after the specified block number and cleans up associated contract data.
|
|
286
|
+
* This handles removal of provisionally added blocks along with their contract classes/instances.
|
|
287
|
+
* Verifies that each block being removed is not part of a stored checkpoint (proposed or not).
|
|
288
|
+
* This differs from `removeUncheckpointedBlocksAfter` in that it also checks proposed checkpoints.
|
|
289
|
+
*
|
|
290
|
+
* @param blockNumber - Remove all blocks with number greater than this.
|
|
291
|
+
* @returns The removed blocks.
|
|
292
|
+
* @throws Error if any block to be removed is checkpointed.
|
|
293
|
+
*/
|
|
294
|
+
public async removeBlocksWithoutProposedCheckpointAfter(blockNumber: BlockNumber): Promise<L2Block[]> {
|
|
295
|
+
const result = await this.stores.db.transactionAsync(async () => {
|
|
296
|
+
// Verify we're only removing uncheckpointed blocks
|
|
297
|
+
const lastCheckpointedBlockNumber = await this.stores.blocks.getProposedCheckpointL2BlockNumber();
|
|
298
|
+
if (blockNumber < lastCheckpointedBlockNumber) {
|
|
299
|
+
throw new Error(
|
|
300
|
+
`Cannot remove blocks after ${blockNumber} because proposed checkpointed blocks exist up to ${lastCheckpointedBlockNumber}`,
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
return await this.removeBlocksAfter(blockNumber);
|
|
305
|
+
});
|
|
306
|
+
await this.l2TipsCache?.refresh();
|
|
307
|
+
return result;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Evicts pending proposed checkpoints that referenced any of the just-pruned blocks. Pruned
|
|
312
|
+
* blocks invalidate all proposed checkpoints from the lowest pruned block's checkpoint number
|
|
313
|
+
* onwards: those checkpoints either reference the pruned blocks directly or chain off them.
|
|
314
|
+
*/
|
|
315
|
+
private async evictProposedCheckpointsForPrunedBlocks(prunedBlocks: L2Block[]): Promise<void> {
|
|
316
|
+
if (prunedBlocks.length === 0) {
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
const fromCheckpointNumber = prunedBlocks[0].checkpointNumber;
|
|
320
|
+
await this.stores.blocks.evictProposedCheckpointsFrom(fromCheckpointNumber);
|
|
321
|
+
}
|
|
322
|
+
|
|
225
323
|
/**
|
|
226
324
|
* Removes all blocks strictly after the given block number along with any logs and contract data.
|
|
227
325
|
* Does not remove their checkpoints.
|
|
228
326
|
*/
|
|
229
327
|
private async removeBlocksAfter(blockNumber: BlockNumber): Promise<L2Block[]> {
|
|
230
328
|
// First get the blocks to be removed so we can clean up contract data
|
|
231
|
-
const removedBlocks = await this.
|
|
329
|
+
const removedBlocks = await this.stores.blocks.removeBlocksAfter(blockNumber);
|
|
232
330
|
|
|
233
331
|
// Clean up contract data and logs for the removed blocks
|
|
234
332
|
await Promise.all([
|
|
235
|
-
this.
|
|
333
|
+
this.stores.logs.deleteLogs(removedBlocks),
|
|
236
334
|
...removedBlocks.map(block => this.removeContractDataFromDb(block)),
|
|
237
335
|
]);
|
|
238
336
|
|
|
@@ -249,20 +347,21 @@ export class ArchiverDataStoreUpdater {
|
|
|
249
347
|
* @returns True if the operation is successful.
|
|
250
348
|
*/
|
|
251
349
|
public async removeCheckpointsAfter(checkpointNumber: CheckpointNumber): Promise<boolean> {
|
|
252
|
-
|
|
253
|
-
const { blocksRemoved = [] } = await this.
|
|
350
|
+
const result = await this.stores.db.transactionAsync(async () => {
|
|
351
|
+
const { blocksRemoved = [] } = await this.stores.blocks.removeCheckpointsAfter(checkpointNumber);
|
|
254
352
|
|
|
255
353
|
const opResults = await Promise.all([
|
|
256
354
|
// Prune rolls back to the last proven block, which is by definition valid
|
|
257
|
-
this.
|
|
355
|
+
this.stores.blocks.setPendingChainValidationStatus({ valid: true }),
|
|
258
356
|
// Remove contract data for all blocks being removed
|
|
259
357
|
...blocksRemoved.map(block => this.removeContractDataFromDb(block)),
|
|
260
|
-
this.
|
|
358
|
+
this.stores.logs.deleteLogs(blocksRemoved),
|
|
261
359
|
]);
|
|
262
360
|
|
|
263
|
-
await this.l2TipsCache?.refresh();
|
|
264
361
|
return opResults.every(Boolean);
|
|
265
362
|
});
|
|
363
|
+
await this.l2TipsCache?.refresh();
|
|
364
|
+
return result;
|
|
266
365
|
}
|
|
267
366
|
|
|
268
367
|
/**
|
|
@@ -270,10 +369,21 @@ export class ArchiverDataStoreUpdater {
|
|
|
270
369
|
* @param checkpointNumber - The checkpoint number to set as proven.
|
|
271
370
|
*/
|
|
272
371
|
public async setProvenCheckpointNumber(checkpointNumber: CheckpointNumber): Promise<void> {
|
|
273
|
-
await this.
|
|
274
|
-
await this.
|
|
275
|
-
await this.l2TipsCache?.refresh();
|
|
372
|
+
await this.stores.db.transactionAsync(async () => {
|
|
373
|
+
await this.stores.blocks.setProvenCheckpointNumber(checkpointNumber);
|
|
276
374
|
});
|
|
375
|
+
await this.l2TipsCache?.refresh();
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* Updates the finalized checkpoint number and refreshes the L2 tips cache.
|
|
380
|
+
* @param checkpointNumber - The checkpoint number to set as finalized.
|
|
381
|
+
*/
|
|
382
|
+
public async setFinalizedCheckpointNumber(checkpointNumber: CheckpointNumber): Promise<void> {
|
|
383
|
+
await this.stores.db.transactionAsync(async () => {
|
|
384
|
+
await this.stores.blocks.setFinalizedCheckpointNumber(checkpointNumber);
|
|
385
|
+
});
|
|
386
|
+
await this.l2TipsCache?.refresh();
|
|
277
387
|
}
|
|
278
388
|
|
|
279
389
|
/** Extracts and stores contract data from a single block. */
|
|
@@ -297,9 +407,6 @@ export class ArchiverDataStoreUpdater {
|
|
|
297
407
|
this.updatePublishedContractClasses(contractClassLogs, block.number, operation),
|
|
298
408
|
this.updateDeployedContractInstances(privateLogs, block.number, operation),
|
|
299
409
|
this.updateUpdatedContractInstances(publicLogs, block.header.globalVariables.timestamp, operation),
|
|
300
|
-
operation === Operation.Store
|
|
301
|
-
? this.storeBroadcastedIndividualFunctions(contractClassLogs, block.number)
|
|
302
|
-
: Promise.resolve(true),
|
|
303
410
|
])
|
|
304
411
|
).every(Boolean);
|
|
305
412
|
}
|
|
@@ -316,18 +423,37 @@ export class ArchiverDataStoreUpdater {
|
|
|
316
423
|
.filter(log => ContractClassPublishedEvent.isContractClassPublishedEvent(log))
|
|
317
424
|
.map(log => ContractClassPublishedEvent.fromLog(log));
|
|
318
425
|
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
contractClasses.
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
426
|
+
if (operation == Operation.Delete) {
|
|
427
|
+
const contractClasses = contractClassPublishedEvents.map(e => e.toContractClassPublic());
|
|
428
|
+
if (contractClasses.length > 0) {
|
|
429
|
+
contractClasses.forEach(c => this.log.verbose(`${Operation[operation]} contract class ${c.id.toString()}`));
|
|
430
|
+
return await this.stores.contractClasses.deleteContractClasses(contractClasses, blockNum);
|
|
431
|
+
}
|
|
432
|
+
return true;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
// Compute bytecode commitments and validate class IDs in a single pass.
|
|
436
|
+
const contractClasses: ContractClassPublicWithCommitment[] = [];
|
|
437
|
+
for (const event of contractClassPublishedEvents) {
|
|
438
|
+
const contractClass = await event.toContractClassPublicWithBytecodeCommitment();
|
|
439
|
+
const computedClassId = await computeContractClassId({
|
|
440
|
+
artifactHash: contractClass.artifactHash,
|
|
441
|
+
privateFunctionsRoot: contractClass.privateFunctionsRoot,
|
|
442
|
+
publicBytecodeCommitment: contractClass.publicBytecodeCommitment,
|
|
443
|
+
});
|
|
444
|
+
if (!computedClassId.equals(contractClass.id)) {
|
|
445
|
+
this.log.warn(
|
|
446
|
+
`Skipping contract class with mismatched id at block ${blockNum}. Claimed ${contractClass.id}, computed ${computedClassId}`,
|
|
447
|
+
{ blockNum, contractClassId: event.contractClassId.toString() },
|
|
326
448
|
);
|
|
327
|
-
|
|
328
|
-
} else if (operation == Operation.Delete) {
|
|
329
|
-
return await this.store.deleteContractClasses(contractClasses, blockNum);
|
|
449
|
+
continue;
|
|
330
450
|
}
|
|
451
|
+
contractClasses.push(contractClass);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
if (contractClasses.length > 0) {
|
|
455
|
+
contractClasses.forEach(c => this.log.verbose(`${Operation[operation]} contract class ${c.id.toString()}`));
|
|
456
|
+
return await this.stores.contractClasses.addContractClasses(contractClasses, blockNum);
|
|
331
457
|
}
|
|
332
458
|
return true;
|
|
333
459
|
}
|
|
@@ -340,18 +466,35 @@ export class ArchiverDataStoreUpdater {
|
|
|
340
466
|
blockNum: BlockNumber,
|
|
341
467
|
operation: Operation,
|
|
342
468
|
): Promise<boolean> {
|
|
343
|
-
const
|
|
469
|
+
const allInstances = allLogs
|
|
344
470
|
.filter(log => ContractInstancePublishedEvent.isContractInstancePublishedEvent(log))
|
|
345
471
|
.map(log => ContractInstancePublishedEvent.fromLog(log))
|
|
346
472
|
.map(e => e.toContractInstance());
|
|
473
|
+
|
|
474
|
+
// Verify that each instance's address matches the one derived from its fields if we're adding
|
|
475
|
+
const contractInstances =
|
|
476
|
+
operation === Operation.Delete
|
|
477
|
+
? allInstances
|
|
478
|
+
: await filterAsync(allInstances, async instance => {
|
|
479
|
+
const computedAddress = await computeContractAddressFromInstance(instance);
|
|
480
|
+
if (!computedAddress.equals(instance.address)) {
|
|
481
|
+
this.log.warn(
|
|
482
|
+
`Found contract instance with mismatched address at block ${blockNum}. Claimed ${instance.address} but computed ${computedAddress}.`,
|
|
483
|
+
{ instanceAddress: instance.address.toString(), computedAddress: computedAddress.toString(), blockNum },
|
|
484
|
+
);
|
|
485
|
+
return false;
|
|
486
|
+
}
|
|
487
|
+
return true;
|
|
488
|
+
});
|
|
489
|
+
|
|
347
490
|
if (contractInstances.length > 0) {
|
|
348
491
|
contractInstances.forEach(c =>
|
|
349
492
|
this.log.verbose(`${Operation[operation]} contract instance at ${c.address.toString()}`),
|
|
350
493
|
);
|
|
351
494
|
if (operation == Operation.Store) {
|
|
352
|
-
return await this.
|
|
495
|
+
return await this.stores.contractInstances.addContractInstances(contractInstances, blockNum);
|
|
353
496
|
} else if (operation == Operation.Delete) {
|
|
354
|
-
return await this.
|
|
497
|
+
return await this.stores.contractInstances.deleteContractInstances(contractInstances);
|
|
355
498
|
}
|
|
356
499
|
}
|
|
357
500
|
return true;
|
|
@@ -375,73 +518,10 @@ export class ArchiverDataStoreUpdater {
|
|
|
375
518
|
this.log.verbose(`${Operation[operation]} contract instance update at ${c.address.toString()}`),
|
|
376
519
|
);
|
|
377
520
|
if (operation == Operation.Store) {
|
|
378
|
-
return await this.
|
|
521
|
+
return await this.stores.contractInstances.addContractInstanceUpdates(contractUpdates, timestamp);
|
|
379
522
|
} else if (operation == Operation.Delete) {
|
|
380
|
-
return await this.
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
return true;
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
/**
|
|
387
|
-
* Stores the functions that were broadcasted individually.
|
|
388
|
-
*
|
|
389
|
-
* @dev Beware that there is not a delete variant of this, since they are added to contract classes
|
|
390
|
-
* and will be deleted as part of the class if needed.
|
|
391
|
-
*/
|
|
392
|
-
private async storeBroadcastedIndividualFunctions(
|
|
393
|
-
allLogs: ContractClassLog[],
|
|
394
|
-
_blockNum: BlockNumber,
|
|
395
|
-
): Promise<boolean> {
|
|
396
|
-
// Filter out private and utility function broadcast events
|
|
397
|
-
const privateFnEvents = allLogs
|
|
398
|
-
.filter(log => PrivateFunctionBroadcastedEvent.isPrivateFunctionBroadcastedEvent(log))
|
|
399
|
-
.map(log => PrivateFunctionBroadcastedEvent.fromLog(log));
|
|
400
|
-
const utilityFnEvents = allLogs
|
|
401
|
-
.filter(log => UtilityFunctionBroadcastedEvent.isUtilityFunctionBroadcastedEvent(log))
|
|
402
|
-
.map(log => UtilityFunctionBroadcastedEvent.fromLog(log));
|
|
403
|
-
|
|
404
|
-
// Group all events by contract class id
|
|
405
|
-
for (const [classIdString, classEvents] of Object.entries(
|
|
406
|
-
groupBy([...privateFnEvents, ...utilityFnEvents], e => e.contractClassId.toString()),
|
|
407
|
-
)) {
|
|
408
|
-
const contractClassId = Fr.fromHexString(classIdString);
|
|
409
|
-
const contractClass = await this.store.getContractClass(contractClassId);
|
|
410
|
-
if (!contractClass) {
|
|
411
|
-
this.log.warn(`Skipping broadcasted functions as contract class ${contractClassId.toString()} was not found`);
|
|
412
|
-
continue;
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
// Split private and utility functions, and filter out invalid ones
|
|
416
|
-
const allFns = classEvents.map(e => e.toFunctionWithMembershipProof());
|
|
417
|
-
const privateFns = allFns.filter(
|
|
418
|
-
(fn): fn is ExecutablePrivateFunctionWithMembershipProof => 'utilityFunctionsTreeRoot' in fn,
|
|
419
|
-
);
|
|
420
|
-
const utilityFns = allFns.filter(
|
|
421
|
-
(fn): fn is UtilityFunctionWithMembershipProof => 'privateFunctionsArtifactTreeRoot' in fn,
|
|
422
|
-
);
|
|
423
|
-
|
|
424
|
-
const privateFunctionsWithValidity = await Promise.all(
|
|
425
|
-
privateFns.map(async fn => ({ fn, valid: await isValidPrivateFunctionMembershipProof(fn, contractClass) })),
|
|
426
|
-
);
|
|
427
|
-
const validPrivateFns = privateFunctionsWithValidity.filter(({ valid }) => valid).map(({ fn }) => fn);
|
|
428
|
-
const utilityFunctionsWithValidity = await Promise.all(
|
|
429
|
-
utilityFns.map(async fn => ({
|
|
430
|
-
fn,
|
|
431
|
-
valid: await isValidUtilityFunctionMembershipProof(fn, contractClass),
|
|
432
|
-
})),
|
|
433
|
-
);
|
|
434
|
-
const validUtilityFns = utilityFunctionsWithValidity.filter(({ valid }) => valid).map(({ fn }) => fn);
|
|
435
|
-
const validFnCount = validPrivateFns.length + validUtilityFns.length;
|
|
436
|
-
if (validFnCount !== allFns.length) {
|
|
437
|
-
this.log.warn(`Skipping ${allFns.length - validFnCount} invalid functions`);
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
// Store the functions in the contract class in a single operation
|
|
441
|
-
if (validFnCount > 0) {
|
|
442
|
-
this.log.verbose(`Storing ${validFnCount} functions for contract class ${contractClassId.toString()}`);
|
|
523
|
+
return await this.stores.contractInstances.deleteContractInstanceUpdates(contractUpdates, timestamp);
|
|
443
524
|
}
|
|
444
|
-
return await this.store.addFunctions(contractClassId, validPrivateFns, validUtilityFns);
|
|
445
525
|
}
|
|
446
526
|
return true;
|
|
447
527
|
}
|