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