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