@aztec/archiver 0.0.1-commit.0c875d939 → 0.0.1-commit.0dc957cde
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 +12 -6
- package/dest/archiver.d.ts +13 -8
- package/dest/archiver.d.ts.map +1 -1
- package/dest/archiver.js +90 -114
- package/dest/config.d.ts +5 -3
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +15 -3
- package/dest/errors.d.ts +50 -10
- package/dest/errors.d.ts.map +1 -1
- package/dest/errors.js +67 -16
- package/dest/factory.d.ts +4 -5
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +31 -27
- package/dest/index.d.ts +4 -2
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +3 -1
- package/dest/l1/bin/retrieve-calldata.js +32 -28
- package/dest/l1/calldata_retriever.d.ts +73 -52
- package/dest/l1/calldata_retriever.d.ts.map +1 -1
- package/dest/l1/calldata_retriever.js +191 -261
- package/dest/l1/data_retrieval.d.ts +24 -16
- package/dest/l1/data_retrieval.d.ts.map +1 -1
- package/dest/l1/data_retrieval.js +39 -45
- 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/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/data_source_base.d.ts +14 -7
- package/dest/modules/data_source_base.d.ts.map +1 -1
- package/dest/modules/data_source_base.js +39 -77
- package/dest/modules/data_store_updater.d.ts +35 -15
- package/dest/modules/data_store_updater.d.ts.map +1 -1
- package/dest/modules/data_store_updater.js +137 -96
- package/dest/modules/instrumentation.d.ts +21 -3
- package/dest/modules/instrumentation.d.ts.map +1 -1
- package/dest/modules/instrumentation.js +41 -8
- package/dest/modules/l1_synchronizer.d.ts +10 -9
- package/dest/modules/l1_synchronizer.d.ts.map +1 -1
- package/dest/modules/l1_synchronizer.js +279 -150
- package/dest/modules/validation.d.ts +1 -1
- package/dest/modules/validation.d.ts.map +1 -1
- package/dest/modules/validation.js +2 -2
- package/dest/store/block_store.d.ts +73 -28
- package/dest/store/block_store.d.ts.map +1 -1
- package/dest/store/block_store.js +395 -136
- package/dest/store/contract_class_store.d.ts +2 -3
- package/dest/store/contract_class_store.d.ts.map +1 -1
- package/dest/store/contract_class_store.js +7 -67
- package/dest/store/contract_instance_store.d.ts +1 -1
- package/dest/store/contract_instance_store.d.ts.map +1 -1
- package/dest/store/contract_instance_store.js +6 -2
- package/dest/store/kv_archiver_store.d.ts +67 -24
- package/dest/store/kv_archiver_store.d.ts.map +1 -1
- package/dest/store/kv_archiver_store.js +82 -27
- package/dest/store/l2_tips_cache.d.ts +20 -0
- package/dest/store/l2_tips_cache.d.ts.map +1 -0
- package/dest/store/l2_tips_cache.js +109 -0
- package/dest/store/log_store.d.ts +6 -3
- package/dest/store/log_store.d.ts.map +1 -1
- package/dest/store/log_store.js +95 -20
- package/dest/store/message_store.d.ts +5 -1
- package/dest/store/message_store.d.ts.map +1 -1
- package/dest/store/message_store.js +21 -9
- package/dest/test/fake_l1_state.d.ts +24 -1
- package/dest/test/fake_l1_state.d.ts.map +1 -1
- package/dest/test/fake_l1_state.js +145 -28
- 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 +26 -5
- package/dest/test/mock_l2_block_source.d.ts.map +1 -1
- package/dest/test/mock_l2_block_source.js +160 -89
- 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 +4 -1
- package/dest/test/noop_l1_archiver.d.ts.map +1 -1
- package/dest/test/noop_l1_archiver.js +9 -3
- package/package.json +13 -13
- package/src/archiver.ts +120 -137
- package/src/config.ts +22 -2
- package/src/errors.ts +104 -26
- package/src/factory.ts +31 -16
- package/src/index.ts +3 -1
- package/src/l1/README.md +25 -68
- package/src/l1/bin/retrieve-calldata.ts +40 -27
- package/src/l1/calldata_retriever.ts +250 -383
- package/src/l1/data_retrieval.ts +55 -69
- package/src/l1/spire_proposer.ts +7 -15
- package/src/l1/validate_historical_logs.ts +140 -0
- package/src/modules/data_source_base.ts +78 -98
- package/src/modules/data_store_updater.ts +164 -126
- package/src/modules/instrumentation.ts +56 -9
- package/src/modules/l1_synchronizer.ts +357 -188
- package/src/modules/validation.ts +2 -2
- package/src/store/block_store.ts +503 -172
- package/src/store/contract_class_store.ts +8 -106
- package/src/store/contract_instance_store.ts +8 -5
- package/src/store/kv_archiver_store.ts +130 -41
- package/src/store/l2_tips_cache.ts +134 -0
- package/src/store/log_store.ts +128 -32
- package/src/store/message_store.ts +27 -10
- package/src/structs/inbox_message.ts +1 -1
- package/src/test/fake_l1_state.ts +193 -32
- 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 +209 -82
- package/src/test/mock_structs.ts +20 -6
- package/src/test/noop_l1_archiver.ts +10 -2
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { range } from '@aztec/foundation/array';
|
|
2
|
+
import { BlockNumber } from '@aztec/foundation/branded-types';
|
|
2
3
|
import { isDefined } from '@aztec/foundation/types';
|
|
3
|
-
import { CommitteeAttestation } from '@aztec/stdlib/block';
|
|
4
4
|
import { Checkpoint, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
|
|
5
5
|
import { getSlotRangeForEpoch } from '@aztec/stdlib/epoch-helpers';
|
|
6
6
|
/**
|
|
@@ -64,7 +64,7 @@ import { getSlotRangeForEpoch } from '@aztec/stdlib/epoch-helpers';
|
|
|
64
64
|
if (!checkpointData) {
|
|
65
65
|
return undefined;
|
|
66
66
|
}
|
|
67
|
-
return BlockNumber(checkpointData.startBlock + checkpointData.
|
|
67
|
+
return BlockNumber(checkpointData.startBlock + checkpointData.blockCount - 1);
|
|
68
68
|
}
|
|
69
69
|
getCheckpointedBlocks(from, limit) {
|
|
70
70
|
return this.store.getCheckpointedBlocks(from, limit);
|
|
@@ -75,6 +75,12 @@ import { getSlotRangeForEpoch } from '@aztec/stdlib/epoch-helpers';
|
|
|
75
75
|
getBlockHeaderByArchive(archive) {
|
|
76
76
|
return this.store.getBlockHeaderByArchive(archive);
|
|
77
77
|
}
|
|
78
|
+
getBlockData(number) {
|
|
79
|
+
return this.store.getBlockData(number);
|
|
80
|
+
}
|
|
81
|
+
getBlockDataByArchive(archive) {
|
|
82
|
+
return this.store.getBlockDataByArchive(archive);
|
|
83
|
+
}
|
|
78
84
|
async getL2Block(number) {
|
|
79
85
|
// If the number provided is -ve, then return the latest block.
|
|
80
86
|
if (number < 0) {
|
|
@@ -90,7 +96,13 @@ import { getSlotRangeForEpoch } from '@aztec/stdlib/epoch-helpers';
|
|
|
90
96
|
return this.store.getTxEffect(txHash);
|
|
91
97
|
}
|
|
92
98
|
getSettledTxReceipt(txHash) {
|
|
93
|
-
return this.store.getSettledTxReceipt(txHash);
|
|
99
|
+
return this.store.getSettledTxReceipt(txHash, this.l1Constants);
|
|
100
|
+
}
|
|
101
|
+
getProposedCheckpoint() {
|
|
102
|
+
return this.store.getProposedCheckpoint();
|
|
103
|
+
}
|
|
104
|
+
getProposedCheckpointOnly() {
|
|
105
|
+
return this.store.getProposedCheckpointOnly();
|
|
94
106
|
}
|
|
95
107
|
isPendingChainInvalid() {
|
|
96
108
|
return this.getPendingChainValidationStatus().then((status)=>!status.valid);
|
|
@@ -100,11 +112,11 @@ import { getSlotRangeForEpoch } from '@aztec/stdlib/epoch-helpers';
|
|
|
100
112
|
valid: true
|
|
101
113
|
};
|
|
102
114
|
}
|
|
103
|
-
getPrivateLogsByTags(tags, page) {
|
|
104
|
-
return this.store.getPrivateLogsByTags(tags, page);
|
|
115
|
+
getPrivateLogsByTags(tags, page, upToBlockNumber) {
|
|
116
|
+
return this.store.getPrivateLogsByTags(tags, page, upToBlockNumber);
|
|
105
117
|
}
|
|
106
|
-
getPublicLogsByTagsFromContract(contractAddress, tags, page) {
|
|
107
|
-
return this.store.getPublicLogsByTagsFromContract(contractAddress, tags, page);
|
|
118
|
+
getPublicLogsByTagsFromContract(contractAddress, tags, page, upToBlockNumber) {
|
|
119
|
+
return this.store.getPublicLogsByTagsFromContract(contractAddress, tags, page, upToBlockNumber);
|
|
108
120
|
}
|
|
109
121
|
getPublicLogs(filter) {
|
|
110
122
|
return this.store.getPublicLogs(filter);
|
|
@@ -146,89 +158,39 @@ import { getSlotRangeForEpoch } from '@aztec/stdlib/epoch-helpers';
|
|
|
146
158
|
}
|
|
147
159
|
async getCheckpoints(checkpointNumber, limit) {
|
|
148
160
|
const checkpoints = await this.store.getRangeOfCheckpoints(checkpointNumber, limit);
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
const publishedCheckpoint = new PublishedCheckpoint(fullCheckpoint, checkpoint.l1, checkpoint.attestations.map((x)=>CommitteeAttestation.fromBuffer(x)));
|
|
156
|
-
fullCheckpoints.push(publishedCheckpoint);
|
|
161
|
+
return Promise.all(checkpoints.map((ch)=>this.getPublishedCheckpointFromCheckpointData(ch)));
|
|
162
|
+
}
|
|
163
|
+
async getPublishedCheckpointFromCheckpointData(checkpoint) {
|
|
164
|
+
const blocksForCheckpoint = await this.store.getBlocksForCheckpoint(checkpoint.checkpointNumber);
|
|
165
|
+
if (!blocksForCheckpoint) {
|
|
166
|
+
throw new Error(`Blocks for checkpoint ${checkpoint.checkpointNumber} not found`);
|
|
157
167
|
}
|
|
158
|
-
|
|
168
|
+
const fullCheckpoint = new Checkpoint(checkpoint.archive, checkpoint.header, blocksForCheckpoint, checkpoint.checkpointNumber);
|
|
169
|
+
return new PublishedCheckpoint(fullCheckpoint, checkpoint.l1, checkpoint.attestations);
|
|
159
170
|
}
|
|
160
171
|
getBlocksForSlot(slotNumber) {
|
|
161
172
|
return this.store.getBlocksForSlot(slotNumber);
|
|
162
173
|
}
|
|
163
174
|
async getCheckpointedBlocksForEpoch(epochNumber) {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1Constants);
|
|
168
|
-
const blocks = [];
|
|
169
|
-
// Walk the list of checkpoints backwards and filter by slots matching the requested epoch.
|
|
170
|
-
// We'll typically ask for checkpoints for a very recent epoch, so we shouldn't need an index here.
|
|
171
|
-
let checkpoint = await this.store.getCheckpointData(await this.store.getSynchedCheckpointNumber());
|
|
172
|
-
const slot = (b)=>b.header.slotNumber;
|
|
173
|
-
while(checkpoint && slot(checkpoint) >= start){
|
|
174
|
-
if (slot(checkpoint) <= end) {
|
|
175
|
-
// push the blocks on backwards
|
|
176
|
-
const endBlock = checkpoint.startBlock + checkpoint.numBlocks - 1;
|
|
177
|
-
for(let i = endBlock; i >= checkpoint.startBlock; i--){
|
|
178
|
-
const checkpointedBlock = await this.getCheckpointedBlock(BlockNumber(i));
|
|
179
|
-
if (checkpointedBlock) {
|
|
180
|
-
blocks.push(checkpointedBlock);
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
checkpoint = await this.store.getCheckpointData(CheckpointNumber(checkpoint.checkpointNumber - 1));
|
|
185
|
-
}
|
|
186
|
-
return blocks.reverse();
|
|
175
|
+
const checkpointsData = await this.getCheckpointsDataForEpoch(epochNumber);
|
|
176
|
+
const blocks = await Promise.all(checkpointsData.flatMap((checkpoint)=>range(checkpoint.blockCount, checkpoint.startBlock).map((blockNumber)=>this.getCheckpointedBlock(BlockNumber(blockNumber)))));
|
|
177
|
+
return blocks.filter(isDefined);
|
|
187
178
|
}
|
|
188
179
|
async getCheckpointedBlockHeadersForEpoch(epochNumber) {
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1Constants);
|
|
193
|
-
const blocks = [];
|
|
194
|
-
// Walk the list of checkpoints backwards and filter by slots matching the requested epoch.
|
|
195
|
-
// We'll typically ask for checkpoints for a very recent epoch, so we shouldn't need an index here.
|
|
196
|
-
let checkpoint = await this.store.getCheckpointData(await this.store.getSynchedCheckpointNumber());
|
|
197
|
-
const slot = (b)=>b.header.slotNumber;
|
|
198
|
-
while(checkpoint && slot(checkpoint) >= start){
|
|
199
|
-
if (slot(checkpoint) <= end) {
|
|
200
|
-
// push the blocks on backwards
|
|
201
|
-
const endBlock = checkpoint.startBlock + checkpoint.numBlocks - 1;
|
|
202
|
-
for(let i = endBlock; i >= checkpoint.startBlock; i--){
|
|
203
|
-
const block = await this.getBlockHeader(BlockNumber(i));
|
|
204
|
-
if (block) {
|
|
205
|
-
blocks.push(block);
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
checkpoint = await this.store.getCheckpointData(CheckpointNumber(checkpoint.checkpointNumber - 1));
|
|
210
|
-
}
|
|
211
|
-
return blocks.reverse();
|
|
180
|
+
const checkpointsData = await this.getCheckpointsDataForEpoch(epochNumber);
|
|
181
|
+
const blocks = await Promise.all(checkpointsData.flatMap((checkpoint)=>range(checkpoint.blockCount, checkpoint.startBlock).map((blockNumber)=>this.getBlockHeader(BlockNumber(blockNumber)))));
|
|
182
|
+
return blocks.filter(isDefined);
|
|
212
183
|
}
|
|
213
184
|
async getCheckpointsForEpoch(epochNumber) {
|
|
185
|
+
const checkpointsData = await this.getCheckpointsDataForEpoch(epochNumber);
|
|
186
|
+
return Promise.all(checkpointsData.map((data)=>this.getPublishedCheckpointFromCheckpointData(data).then((p)=>p.checkpoint)));
|
|
187
|
+
}
|
|
188
|
+
/** Returns checkpoint data for all checkpoints whose slot falls within the given epoch. */ getCheckpointsDataForEpoch(epochNumber) {
|
|
214
189
|
if (!this.l1Constants) {
|
|
215
190
|
throw new Error('L1 constants not set');
|
|
216
191
|
}
|
|
217
192
|
const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1Constants);
|
|
218
|
-
|
|
219
|
-
// Walk the list of checkpoints backwards and filter by slots matching the requested epoch.
|
|
220
|
-
// We'll typically ask for checkpoints for a very recent epoch, so we shouldn't need an index here.
|
|
221
|
-
let checkpointData = await this.store.getCheckpointData(await this.store.getSynchedCheckpointNumber());
|
|
222
|
-
const slot = (b)=>b.header.slotNumber;
|
|
223
|
-
while(checkpointData && slot(checkpointData) >= start){
|
|
224
|
-
if (slot(checkpointData) <= end) {
|
|
225
|
-
// push the checkpoints on backwards
|
|
226
|
-
const [checkpoint] = await this.getCheckpoints(checkpointData.checkpointNumber, 1);
|
|
227
|
-
checkpoints.push(checkpoint.checkpoint);
|
|
228
|
-
}
|
|
229
|
-
checkpointData = await this.store.getCheckpointData(CheckpointNumber(checkpointData.checkpointNumber - 1));
|
|
230
|
-
}
|
|
231
|
-
return checkpoints.reverse();
|
|
193
|
+
return this.store.getCheckpointDataForSlotRange(start, end);
|
|
232
194
|
}
|
|
233
195
|
async getBlock(number) {
|
|
234
196
|
// If the number provided is -ve, then return the latest block.
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
|
|
2
|
-
import type { L2Block, ValidateCheckpointResult } from '@aztec/stdlib/block';
|
|
3
|
-
import type
|
|
2
|
+
import type { CommitteeAttestation, L2Block, ValidateCheckpointResult } from '@aztec/stdlib/block';
|
|
3
|
+
import { type L1PublishedData, type ProposedCheckpointInput, type PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
|
|
4
4
|
import type { KVArchiverDataStore } from '../store/kv_archiver_store.js';
|
|
5
|
+
import type { L2TipsCache } from '../store/l2_tips_cache.js';
|
|
5
6
|
/** Result of adding checkpoints with information about any pruned blocks. */
|
|
6
7
|
type ReconcileCheckpointsResult = {
|
|
7
8
|
/** Blocks that were pruned due to conflict with L1 checkpoints. */
|
|
@@ -12,31 +13,41 @@ type ReconcileCheckpointsResult = {
|
|
|
12
13
|
/** Archiver helper module to handle updates to the data store. */
|
|
13
14
|
export declare class ArchiverDataStoreUpdater {
|
|
14
15
|
private store;
|
|
16
|
+
private l2TipsCache?;
|
|
17
|
+
private opts;
|
|
15
18
|
private readonly log;
|
|
16
|
-
constructor(store: KVArchiverDataStore
|
|
19
|
+
constructor(store: KVArchiverDataStore, l2TipsCache?: L2TipsCache | undefined, opts?: {
|
|
20
|
+
rollupManaLimit?: number;
|
|
21
|
+
});
|
|
17
22
|
/**
|
|
18
|
-
* Adds proposed
|
|
19
|
-
*
|
|
20
|
-
* Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events
|
|
21
|
-
* and individually broadcasted functions from the block logs.
|
|
23
|
+
* Adds a proposed block to the store with contract class/instance extraction from logs.
|
|
24
|
+
* This is an uncheckpointed block that has been proposed by the sequencer but not yet included in a checkpoint on L1.
|
|
25
|
+
* Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events from the block logs.
|
|
22
26
|
*
|
|
23
|
-
* @param
|
|
27
|
+
* @param block - The proposed L2 block to add.
|
|
24
28
|
* @param pendingChainValidationStatus - Optional validation status to set.
|
|
25
29
|
* @returns True if the operation is successful.
|
|
26
30
|
*/
|
|
27
|
-
|
|
31
|
+
addProposedBlock(block: L2Block, pendingChainValidationStatus?: ValidateCheckpointResult): Promise<boolean>;
|
|
28
32
|
/**
|
|
29
33
|
* Reconciles local blocks with incoming checkpoints from L1.
|
|
30
34
|
* Adds new checkpoints to the store with contract class/instance extraction from logs.
|
|
31
35
|
* Prunes any local blocks that conflict with checkpoint data (by comparing archive roots).
|
|
32
|
-
* Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events
|
|
33
|
-
*
|
|
36
|
+
* Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events from the checkpoint block logs.
|
|
37
|
+
* If `promoteProposed` is supplied, the proposed-checkpoint promotion runs inside the same transaction
|
|
38
|
+
* as the added checkpoints so both updates are applied atomically.
|
|
34
39
|
*
|
|
35
|
-
* @param checkpoints - The published checkpoints to add.
|
|
40
|
+
* @param checkpoints - The published checkpoints to add (excluding any being promoted from proposed).
|
|
36
41
|
* @param pendingChainValidationStatus - Optional validation status to set.
|
|
42
|
+
* @param promoteProposed - Optional promotion of the current proposed checkpoint (fast path when blocks are already local).
|
|
37
43
|
* @returns Result with information about any pruned blocks.
|
|
38
44
|
*/
|
|
39
|
-
addCheckpoints(checkpoints: PublishedCheckpoint[], pendingChainValidationStatus?: ValidateCheckpointResult
|
|
45
|
+
addCheckpoints(checkpoints: PublishedCheckpoint[], pendingChainValidationStatus?: ValidateCheckpointResult, promoteProposed?: {
|
|
46
|
+
l1: L1PublishedData;
|
|
47
|
+
attestations: CommitteeAttestation[];
|
|
48
|
+
checkpoint: PublishedCheckpoint;
|
|
49
|
+
}): Promise<ReconcileCheckpointsResult>;
|
|
50
|
+
setProposedCheckpoint(proposedCheckpoint: ProposedCheckpointInput): Promise<void>;
|
|
40
51
|
private pruneMismatchingLocalBlocks;
|
|
41
52
|
/**
|
|
42
53
|
* Removes all uncheckpointed blocks strictly after the specified block number and cleans up associated contract data.
|
|
@@ -59,6 +70,16 @@ export declare class ArchiverDataStoreUpdater {
|
|
|
59
70
|
* @returns True if the operation is successful.
|
|
60
71
|
*/
|
|
61
72
|
removeCheckpointsAfter(checkpointNumber: CheckpointNumber): Promise<boolean>;
|
|
73
|
+
/**
|
|
74
|
+
* Updates the proven checkpoint number and refreshes the L2 tips cache.
|
|
75
|
+
* @param checkpointNumber - The checkpoint number to set as proven.
|
|
76
|
+
*/
|
|
77
|
+
setProvenCheckpointNumber(checkpointNumber: CheckpointNumber): Promise<void>;
|
|
78
|
+
/**
|
|
79
|
+
* Updates the finalized checkpoint number and refreshes the L2 tips cache.
|
|
80
|
+
* @param checkpointNumber - The checkpoint number to set as finalized.
|
|
81
|
+
*/
|
|
82
|
+
setFinalizedCheckpointNumber(checkpointNumber: CheckpointNumber): Promise<void>;
|
|
62
83
|
/** Extracts and stores contract data from a single block. */
|
|
63
84
|
private addContractDataToDb;
|
|
64
85
|
/** Removes contract data associated with a block. */
|
|
@@ -67,7 +88,6 @@ export declare class ArchiverDataStoreUpdater {
|
|
|
67
88
|
private updatePublishedContractClasses;
|
|
68
89
|
private updateDeployedContractInstances;
|
|
69
90
|
private updateUpdatedContractInstances;
|
|
70
|
-
private storeBroadcastedIndividualFunctions;
|
|
71
91
|
}
|
|
72
92
|
export {};
|
|
73
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
93
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZGF0YV9zdG9yZV91cGRhdGVyLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvbW9kdWxlcy9kYXRhX3N0b3JlX3VwZGF0ZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLFdBQVcsRUFBRSxnQkFBZ0IsRUFBRSxNQUFNLGlDQUFpQyxDQUFDO0FBUWhGLE9BQU8sS0FBSyxFQUFFLG9CQUFvQixFQUFFLE9BQU8sRUFBRSx3QkFBd0IsRUFBRSxNQUFNLHFCQUFxQixDQUFDO0FBQ25HLE9BQU8sRUFDTCxLQUFLLGVBQWUsRUFDcEIsS0FBSyx1QkFBdUIsRUFDNUIsS0FBSyxtQkFBbUIsRUFFekIsTUFBTSwwQkFBMEIsQ0FBQztBQVNsQyxPQUFPLEtBQUssRUFBRSxtQkFBbUIsRUFBRSxNQUFNLCtCQUErQixDQUFDO0FBQ3pFLE9BQU8sS0FBSyxFQUFFLFdBQVcsRUFBRSxNQUFNLDJCQUEyQixDQUFDO0FBUTdELDZFQUE2RTtBQUM3RSxLQUFLLDBCQUEwQixHQUFHO0lBQ2hDLG1FQUFtRTtJQUNuRSxZQUFZLEVBQUUsT0FBTyxFQUFFLEdBQUcsU0FBUyxDQUFDO0lBQ3BDLGlGQUFpRjtJQUNqRiw4QkFBOEIsRUFBRSxXQUFXLEdBQUcsU0FBUyxDQUFDO0NBQ3pELENBQUM7QUFFRixrRUFBa0U7QUFDbEUscUJBQWEsd0JBQXdCO0lBSWpDLE9BQU8sQ0FBQyxLQUFLO0lBQ2IsT0FBTyxDQUFDLFdBQVcsQ0FBQztJQUNwQixPQUFPLENBQUMsSUFBSTtJQUxkLE9BQU8sQ0FBQyxRQUFRLENBQUMsR0FBRyxDQUEwQztJQUU5RCxZQUNVLEtBQUssRUFBRSxtQkFBbUIsRUFDMUIsV0FBVyxDQUFDLHlCQUFhLEVBQ3pCLElBQUksR0FBRTtRQUFFLGVBQWUsQ0FBQyxFQUFFLE1BQU0sQ0FBQTtLQUFPLEVBQzdDO0lBRUo7Ozs7Ozs7O09BUUc7SUFDVSxnQkFBZ0IsQ0FDM0IsS0FBSyxFQUFFLE9BQU8sRUFDZCw0QkFBNEIsQ0FBQyxFQUFFLHdCQUF3QixHQUN0RCxPQUFPLENBQUMsT0FBTyxDQUFDLENBaUJsQjtJQUVEOzs7Ozs7Ozs7Ozs7T0FZRztJQUNVLGNBQWMsQ0FDekIsV0FBVyxFQUFFLG1CQUFtQixFQUFFLEVBQ2xDLDRCQUE0QixDQUFDLEVBQUUsd0JBQXdCLEVBQ3ZELGVBQWUsQ0FBQyxFQUFFO1FBQ2hCLEVBQUUsRUFBRSxlQUFlLENBQUM7UUFDcEIsWUFBWSxFQUFFLG9CQUFvQixFQUFFLENBQUM7UUFDckMsVUFBVSxFQUFFLG1CQUFtQixDQUFDO0tBQ2pDLEdBQ0EsT0FBTyxDQUFDLDBCQUEwQixDQUFDLENBd0NyQztJQUVZLHFCQUFxQixDQUFDLGtCQUFrQixFQUFFLHVCQUF1QixpQkFPN0U7WUFRYSwyQkFBMkI7SUFtRXpDOzs7Ozs7OztPQVFHO0lBQ1UsK0JBQStCLENBQUMsV0FBVyxFQUFFLFdBQVcsR0FBRyxPQUFPLENBQUMsT0FBTyxFQUFFLENBQUMsQ0FtQnpGO1lBTWEsaUJBQWlCO0lBYS9COzs7Ozs7OztPQVFHO0lBQ1Usc0JBQXNCLENBQUMsZ0JBQWdCLEVBQUUsZ0JBQWdCLEdBQUcsT0FBTyxDQUFDLE9BQU8sQ0FBQyxDQWV4RjtJQUVEOzs7T0FHRztJQUNVLHlCQUF5QixDQUFDLGdCQUFnQixFQUFFLGdCQUFnQixHQUFHLE9BQU8sQ0FBQyxJQUFJLENBQUMsQ0FLeEY7SUFFRDs7O09BR0c7SUFDVSw0QkFBNEIsQ0FBQyxnQkFBZ0IsRUFBRSxnQkFBZ0IsR0FBRyxPQUFPLENBQUMsSUFBSSxDQUFDLENBSzNGO0lBRUQsNkRBQTZEO0lBQzdELE9BQU8sQ0FBQyxtQkFBbUI7SUFJM0IscURBQXFEO0lBQ3JELE9BQU8sQ0FBQyx3QkFBd0I7WUFLbEIsc0JBQXNCO1lBaUJ0Qiw4QkFBOEI7WUErQzlCLCtCQUErQjtZQTBDL0IsOEJBQThCO0NBc0I3QyJ9
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"data_store_updater.d.ts","sourceRoot":"","sources":["../../src/modules/data_store_updater.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,gBAAgB,EAAE,MAAM,iCAAiC,CAAC;
|
|
1
|
+
{"version":3,"file":"data_store_updater.d.ts","sourceRoot":"","sources":["../../src/modules/data_store_updater.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,gBAAgB,EAAE,MAAM,iCAAiC,CAAC;AAQhF,OAAO,KAAK,EAAE,oBAAoB,EAAE,OAAO,EAAE,wBAAwB,EAAE,MAAM,qBAAqB,CAAC;AACnG,OAAO,EACL,KAAK,eAAe,EACpB,KAAK,uBAAuB,EAC5B,KAAK,mBAAmB,EAEzB,MAAM,0BAA0B,CAAC;AASlC,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,+BAA+B,CAAC;AACzE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAQ7D,6EAA6E;AAC7E,KAAK,0BAA0B,GAAG;IAChC,mEAAmE;IACnE,YAAY,EAAE,OAAO,EAAE,GAAG,SAAS,CAAC;IACpC,iFAAiF;IACjF,8BAA8B,EAAE,WAAW,GAAG,SAAS,CAAC;CACzD,CAAC;AAEF,kEAAkE;AAClE,qBAAa,wBAAwB;IAIjC,OAAO,CAAC,KAAK;IACb,OAAO,CAAC,WAAW,CAAC;IACpB,OAAO,CAAC,IAAI;IALd,OAAO,CAAC,QAAQ,CAAC,GAAG,CAA0C;IAE9D,YACU,KAAK,EAAE,mBAAmB,EAC1B,WAAW,CAAC,yBAAa,EACzB,IAAI,GAAE;QAAE,eAAe,CAAC,EAAE,MAAM,CAAA;KAAO,EAC7C;IAEJ;;;;;;;;OAQG;IACU,gBAAgB,CAC3B,KAAK,EAAE,OAAO,EACd,4BAA4B,CAAC,EAAE,wBAAwB,GACtD,OAAO,CAAC,OAAO,CAAC,CAiBlB;IAED;;;;;;;;;;;;OAYG;IACU,cAAc,CACzB,WAAW,EAAE,mBAAmB,EAAE,EAClC,4BAA4B,CAAC,EAAE,wBAAwB,EACvD,eAAe,CAAC,EAAE;QAChB,EAAE,EAAE,eAAe,CAAC;QACpB,YAAY,EAAE,oBAAoB,EAAE,CAAC;QACrC,UAAU,EAAE,mBAAmB,CAAC;KACjC,GACA,OAAO,CAAC,0BAA0B,CAAC,CAwCrC;IAEY,qBAAqB,CAAC,kBAAkB,EAAE,uBAAuB,iBAO7E;YAQa,2BAA2B;IAmEzC;;;;;;;;OAQG;IACU,+BAA+B,CAAC,WAAW,EAAE,WAAW,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC,CAmBzF;YAMa,iBAAiB;IAa/B;;;;;;;;OAQG;IACU,sBAAsB,CAAC,gBAAgB,EAAE,gBAAgB,GAAG,OAAO,CAAC,OAAO,CAAC,CAexF;IAED;;;OAGG;IACU,yBAAyB,CAAC,gBAAgB,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,CAKxF;IAED;;;OAGG;IACU,4BAA4B,CAAC,gBAAgB,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,CAK3F;IAED,6DAA6D;IAC7D,OAAO,CAAC,mBAAmB;IAI3B,qDAAqD;IACrD,OAAO,CAAC,wBAAwB;YAKlB,sBAAsB;YAiBtB,8BAA8B;YA+C9B,+BAA+B;YA0C/B,8BAA8B;CAsB7C"}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { BlockNumber } from '@aztec/foundation/branded-types';
|
|
2
|
-
import {
|
|
2
|
+
import { filterAsync } from '@aztec/foundation/collection';
|
|
3
3
|
import { createLogger } from '@aztec/foundation/log';
|
|
4
|
-
import { ContractClassPublishedEvent
|
|
4
|
+
import { ContractClassPublishedEvent } from '@aztec/protocol-contracts/class-registry';
|
|
5
5
|
import { ContractInstancePublishedEvent, ContractInstanceUpdatedEvent } from '@aztec/protocol-contracts/instance-registry';
|
|
6
|
-
import {
|
|
7
|
-
import
|
|
6
|
+
import { validateCheckpoint } from '@aztec/stdlib/checkpoint';
|
|
7
|
+
import { computeContractAddressFromInstance, computeContractClassId } from '@aztec/stdlib/contract';
|
|
8
8
|
/** Operation type for contract data updates. */ var Operation = /*#__PURE__*/ function(Operation) {
|
|
9
9
|
Operation[Operation["Store"] = 0] = "Store";
|
|
10
10
|
Operation[Operation["Delete"] = 1] = "Delete";
|
|
@@ -12,50 +12,69 @@ import groupBy from 'lodash.groupby';
|
|
|
12
12
|
}(Operation || {});
|
|
13
13
|
/** Archiver helper module to handle updates to the data store. */ export class ArchiverDataStoreUpdater {
|
|
14
14
|
store;
|
|
15
|
+
l2TipsCache;
|
|
16
|
+
opts;
|
|
15
17
|
log;
|
|
16
|
-
constructor(store){
|
|
18
|
+
constructor(store, l2TipsCache, opts = {}){
|
|
17
19
|
this.store = store;
|
|
20
|
+
this.l2TipsCache = l2TipsCache;
|
|
21
|
+
this.opts = opts;
|
|
18
22
|
this.log = createLogger('archiver:store_updater');
|
|
19
23
|
}
|
|
20
24
|
/**
|
|
21
|
-
* Adds proposed
|
|
22
|
-
*
|
|
23
|
-
* Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events
|
|
24
|
-
* and individually broadcasted functions from the block logs.
|
|
25
|
+
* Adds a proposed block to the store with contract class/instance extraction from logs.
|
|
26
|
+
* This is an uncheckpointed block that has been proposed by the sequencer but not yet included in a checkpoint on L1.
|
|
27
|
+
* Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events from the block logs.
|
|
25
28
|
*
|
|
26
|
-
* @param
|
|
29
|
+
* @param block - The proposed L2 block to add.
|
|
27
30
|
* @param pendingChainValidationStatus - Optional validation status to set.
|
|
28
31
|
* @returns True if the operation is successful.
|
|
29
|
-
*/
|
|
30
|
-
|
|
31
|
-
await this.store.
|
|
32
|
+
*/ async addProposedBlock(block, pendingChainValidationStatus) {
|
|
33
|
+
const result = await this.store.transactionAsync(async ()=>{
|
|
34
|
+
await this.store.addProposedBlock(block);
|
|
32
35
|
const opResults = await Promise.all([
|
|
33
36
|
// Update the pending chain validation status if provided
|
|
34
37
|
pendingChainValidationStatus && this.store.setPendingChainValidationStatus(pendingChainValidationStatus),
|
|
35
|
-
// Add any logs emitted during the retrieved
|
|
36
|
-
this.store.addLogs(
|
|
37
|
-
|
|
38
|
-
|
|
38
|
+
// Add any logs emitted during the retrieved block
|
|
39
|
+
this.store.addLogs([
|
|
40
|
+
block
|
|
41
|
+
]),
|
|
42
|
+
// Unroll all logs emitted during the retrieved block and extract any contract classes and instances from it
|
|
43
|
+
this.addContractDataToDb(block)
|
|
39
44
|
]);
|
|
45
|
+
await this.l2TipsCache?.refresh();
|
|
40
46
|
return opResults.every(Boolean);
|
|
41
47
|
});
|
|
48
|
+
return result;
|
|
42
49
|
}
|
|
43
50
|
/**
|
|
44
51
|
* Reconciles local blocks with incoming checkpoints from L1.
|
|
45
52
|
* Adds new checkpoints to the store with contract class/instance extraction from logs.
|
|
46
53
|
* Prunes any local blocks that conflict with checkpoint data (by comparing archive roots).
|
|
47
|
-
* Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events
|
|
48
|
-
*
|
|
54
|
+
* Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events from the checkpoint block logs.
|
|
55
|
+
* If `promoteProposed` is supplied, the proposed-checkpoint promotion runs inside the same transaction
|
|
56
|
+
* as the added checkpoints so both updates are applied atomically.
|
|
49
57
|
*
|
|
50
|
-
* @param checkpoints - The published checkpoints to add.
|
|
58
|
+
* @param checkpoints - The published checkpoints to add (excluding any being promoted from proposed).
|
|
51
59
|
* @param pendingChainValidationStatus - Optional validation status to set.
|
|
60
|
+
* @param promoteProposed - Optional promotion of the current proposed checkpoint (fast path when blocks are already local).
|
|
52
61
|
* @returns Result with information about any pruned blocks.
|
|
53
|
-
*/ addCheckpoints(checkpoints, pendingChainValidationStatus) {
|
|
54
|
-
|
|
62
|
+
*/ async addCheckpoints(checkpoints, pendingChainValidationStatus, promoteProposed) {
|
|
63
|
+
for (const checkpoint of checkpoints){
|
|
64
|
+
validateCheckpoint(checkpoint.checkpoint, {
|
|
65
|
+
rollupManaLimit: this.opts?.rollupManaLimit
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
if (promoteProposed) {
|
|
69
|
+
validateCheckpoint(promoteProposed.checkpoint.checkpoint, {
|
|
70
|
+
rollupManaLimit: this.opts?.rollupManaLimit
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
const result = await this.store.transactionAsync(async ()=>{
|
|
55
74
|
// Before adding checkpoints, check for conflicts with local blocks if any
|
|
56
75
|
const { prunedBlocks, lastAlreadyInsertedBlockNumber } = await this.pruneMismatchingLocalBlocks(checkpoints);
|
|
57
76
|
await this.store.addCheckpoints(checkpoints);
|
|
58
|
-
// Filter out blocks that were already inserted via
|
|
77
|
+
// Filter out blocks that were already inserted via addProposedBlock() to avoid duplicating logs/contract data
|
|
59
78
|
const newBlocks = checkpoints.flatMap((ch)=>ch.checkpoint.blocks).filter((b)=>lastAlreadyInsertedBlockNumber === undefined || b.number > lastAlreadyInsertedBlockNumber);
|
|
60
79
|
await Promise.all([
|
|
61
80
|
// Update the pending chain validation status if provided
|
|
@@ -63,13 +82,24 @@ import groupBy from 'lodash.groupby';
|
|
|
63
82
|
// Add any logs emitted during the retrieved blocks
|
|
64
83
|
this.store.addLogs(newBlocks),
|
|
65
84
|
// Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
|
|
66
|
-
...newBlocks.map((block)=>this.addContractDataToDb(block))
|
|
85
|
+
...newBlocks.map((block)=>this.addContractDataToDb(block)),
|
|
86
|
+
// Promote the proposed checkpoint if requested
|
|
87
|
+
promoteProposed ? this.store.promoteProposedToCheckpointed(promoteProposed.l1, promoteProposed.attestations, promoteProposed.checkpoint.checkpoint.archive.root) : undefined
|
|
67
88
|
]);
|
|
89
|
+
await this.l2TipsCache?.refresh();
|
|
68
90
|
return {
|
|
69
91
|
prunedBlocks,
|
|
70
92
|
lastAlreadyInsertedBlockNumber
|
|
71
93
|
};
|
|
72
94
|
});
|
|
95
|
+
return result;
|
|
96
|
+
}
|
|
97
|
+
async setProposedCheckpoint(proposedCheckpoint) {
|
|
98
|
+
const result = await this.store.transactionAsync(async ()=>{
|
|
99
|
+
await this.store.setProposedCheckpoint(proposedCheckpoint);
|
|
100
|
+
await this.l2TipsCache?.refresh();
|
|
101
|
+
});
|
|
102
|
+
return result;
|
|
73
103
|
}
|
|
74
104
|
/**
|
|
75
105
|
* Checks for local proposed blocks that do not match the ones to be checkpointed and prunes them.
|
|
@@ -113,7 +143,7 @@ import groupBy from 'lodash.groupby';
|
|
|
113
143
|
this.log.verbose(`Block number ${blockNumber} already inserted and matches checkpoint`, blockInfos);
|
|
114
144
|
lastAlreadyInsertedBlockNumber = blockNumber;
|
|
115
145
|
} else {
|
|
116
|
-
this.log.
|
|
146
|
+
this.log.info(`Conflict detected at block ${blockNumber} between checkpointed and local block`, blockInfos);
|
|
117
147
|
const prunedBlocks = await this.removeBlocksAfter(BlockNumber(blockNumber - 1));
|
|
118
148
|
return {
|
|
119
149
|
prunedBlocks,
|
|
@@ -147,15 +177,20 @@ import groupBy from 'lodash.groupby';
|
|
|
147
177
|
* @param blockNumber - Remove all blocks with number greater than this.
|
|
148
178
|
* @returns The removed blocks.
|
|
149
179
|
* @throws Error if any block to be removed is checkpointed.
|
|
150
|
-
*/ removeUncheckpointedBlocksAfter(blockNumber) {
|
|
151
|
-
|
|
180
|
+
*/ async removeUncheckpointedBlocksAfter(blockNumber) {
|
|
181
|
+
const result = await this.store.transactionAsync(async ()=>{
|
|
152
182
|
// Verify we're only removing uncheckpointed blocks
|
|
153
183
|
const lastCheckpointedBlockNumber = await this.store.getCheckpointedL2BlockNumber();
|
|
154
184
|
if (blockNumber < lastCheckpointedBlockNumber) {
|
|
155
185
|
throw new Error(`Cannot remove blocks after ${blockNumber} because checkpointed blocks exist up to ${lastCheckpointedBlockNumber}`);
|
|
156
186
|
}
|
|
157
|
-
|
|
187
|
+
const result = await this.removeBlocksAfter(blockNumber);
|
|
188
|
+
// Clear the proposed checkpoint if it exists, since its blocks have been pruned
|
|
189
|
+
await this.store.deleteProposedCheckpoint();
|
|
190
|
+
await this.l2TipsCache?.refresh();
|
|
191
|
+
return result;
|
|
158
192
|
});
|
|
193
|
+
return result;
|
|
159
194
|
}
|
|
160
195
|
/**
|
|
161
196
|
* Removes all blocks strictly after the given block number along with any logs and contract data.
|
|
@@ -179,17 +214,38 @@ import groupBy from 'lodash.groupby';
|
|
|
179
214
|
* @param checkpointNumber - Remove all checkpoints strictly after this one.
|
|
180
215
|
* @returns True if the operation is successful.
|
|
181
216
|
*/ async removeCheckpointsAfter(checkpointNumber) {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
217
|
+
return await this.store.transactionAsync(async ()=>{
|
|
218
|
+
const { blocksRemoved = [] } = await this.store.removeCheckpointsAfter(checkpointNumber);
|
|
219
|
+
const opResults = await Promise.all([
|
|
220
|
+
// Prune rolls back to the last proven block, which is by definition valid
|
|
221
|
+
this.store.setPendingChainValidationStatus({
|
|
222
|
+
valid: true
|
|
223
|
+
}),
|
|
224
|
+
// Remove contract data for all blocks being removed
|
|
225
|
+
...blocksRemoved.map((block)=>this.removeContractDataFromDb(block)),
|
|
226
|
+
this.store.deleteLogs(blocksRemoved)
|
|
227
|
+
]);
|
|
228
|
+
await this.l2TipsCache?.refresh();
|
|
229
|
+
return opResults.every(Boolean);
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Updates the proven checkpoint number and refreshes the L2 tips cache.
|
|
234
|
+
* @param checkpointNumber - The checkpoint number to set as proven.
|
|
235
|
+
*/ async setProvenCheckpointNumber(checkpointNumber) {
|
|
236
|
+
await this.store.transactionAsync(async ()=>{
|
|
237
|
+
await this.store.setProvenCheckpointNumber(checkpointNumber);
|
|
238
|
+
await this.l2TipsCache?.refresh();
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Updates the finalized checkpoint number and refreshes the L2 tips cache.
|
|
243
|
+
* @param checkpointNumber - The checkpoint number to set as finalized.
|
|
244
|
+
*/ async setFinalizedCheckpointNumber(checkpointNumber) {
|
|
245
|
+
await this.store.transactionAsync(async ()=>{
|
|
246
|
+
await this.store.setFinalizedCheckpointNumber(checkpointNumber);
|
|
247
|
+
await this.l2TipsCache?.refresh();
|
|
248
|
+
});
|
|
193
249
|
}
|
|
194
250
|
/** Extracts and stores contract data from a single block. */ addContractDataToDb(block) {
|
|
195
251
|
return this.updateContractDataOnDb(block, 0);
|
|
@@ -204,31 +260,62 @@ import groupBy from 'lodash.groupby';
|
|
|
204
260
|
return (await Promise.all([
|
|
205
261
|
this.updatePublishedContractClasses(contractClassLogs, block.number, operation),
|
|
206
262
|
this.updateDeployedContractInstances(privateLogs, block.number, operation),
|
|
207
|
-
this.updateUpdatedContractInstances(publicLogs, block.header.globalVariables.timestamp, operation)
|
|
208
|
-
operation === 0 ? this.storeBroadcastedIndividualFunctions(contractClassLogs, block.number) : Promise.resolve(true)
|
|
263
|
+
this.updateUpdatedContractInstances(publicLogs, block.header.globalVariables.timestamp, operation)
|
|
209
264
|
])).every(Boolean);
|
|
210
265
|
}
|
|
211
266
|
/**
|
|
212
267
|
* Extracts and stores contract classes out of ContractClassPublished events emitted by the class registry contract.
|
|
213
268
|
*/ async updatePublishedContractClasses(allLogs, blockNum, operation) {
|
|
214
269
|
const contractClassPublishedEvents = allLogs.filter((log)=>ContractClassPublishedEvent.isContractClassPublishedEvent(log)).map((log)=>ContractClassPublishedEvent.fromLog(log));
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
contractClasses.
|
|
218
|
-
|
|
219
|
-
// TODO: Will probably want to create some worker threads to compute these bytecode commitments as they are expensive
|
|
220
|
-
const commitments = await Promise.all(contractClasses.map((c)=>computePublicBytecodeCommitment(c.packedBytecode)));
|
|
221
|
-
return await this.store.addContractClasses(contractClasses, commitments, blockNum);
|
|
222
|
-
} else if (operation == 1) {
|
|
270
|
+
if (operation == 1) {
|
|
271
|
+
const contractClasses = contractClassPublishedEvents.map((e)=>e.toContractClassPublic());
|
|
272
|
+
if (contractClasses.length > 0) {
|
|
273
|
+
contractClasses.forEach((c)=>this.log.verbose(`${Operation[operation]} contract class ${c.id.toString()}`));
|
|
223
274
|
return await this.store.deleteContractClasses(contractClasses, blockNum);
|
|
224
275
|
}
|
|
276
|
+
return true;
|
|
277
|
+
}
|
|
278
|
+
// Compute bytecode commitments and validate class IDs in a single pass.
|
|
279
|
+
const contractClasses = [];
|
|
280
|
+
for (const event of contractClassPublishedEvents){
|
|
281
|
+
const contractClass = await event.toContractClassPublicWithBytecodeCommitment();
|
|
282
|
+
const computedClassId = await computeContractClassId({
|
|
283
|
+
artifactHash: contractClass.artifactHash,
|
|
284
|
+
privateFunctionsRoot: contractClass.privateFunctionsRoot,
|
|
285
|
+
publicBytecodeCommitment: contractClass.publicBytecodeCommitment
|
|
286
|
+
});
|
|
287
|
+
if (!computedClassId.equals(contractClass.id)) {
|
|
288
|
+
this.log.warn(`Skipping contract class with mismatched id at block ${blockNum}. Claimed ${contractClass.id}, computed ${computedClassId}`, {
|
|
289
|
+
blockNum,
|
|
290
|
+
contractClassId: event.contractClassId.toString()
|
|
291
|
+
});
|
|
292
|
+
continue;
|
|
293
|
+
}
|
|
294
|
+
contractClasses.push(contractClass);
|
|
295
|
+
}
|
|
296
|
+
if (contractClasses.length > 0) {
|
|
297
|
+
contractClasses.forEach((c)=>this.log.verbose(`${Operation[operation]} contract class ${c.id.toString()}`));
|
|
298
|
+
return await this.store.addContractClasses(contractClasses, blockNum);
|
|
225
299
|
}
|
|
226
300
|
return true;
|
|
227
301
|
}
|
|
228
302
|
/**
|
|
229
303
|
* Extracts and stores contract instances out of ContractInstancePublished events emitted by the canonical deployer contract.
|
|
230
304
|
*/ async updateDeployedContractInstances(allLogs, blockNum, operation) {
|
|
231
|
-
const
|
|
305
|
+
const allInstances = allLogs.filter((log)=>ContractInstancePublishedEvent.isContractInstancePublishedEvent(log)).map((log)=>ContractInstancePublishedEvent.fromLog(log)).map((e)=>e.toContractInstance());
|
|
306
|
+
// Verify that each instance's address matches the one derived from its fields if we're adding
|
|
307
|
+
const contractInstances = operation === 1 ? allInstances : await filterAsync(allInstances, async (instance)=>{
|
|
308
|
+
const computedAddress = await computeContractAddressFromInstance(instance);
|
|
309
|
+
if (!computedAddress.equals(instance.address)) {
|
|
310
|
+
this.log.warn(`Found contract instance with mismatched address at block ${blockNum}. Claimed ${instance.address} but computed ${computedAddress}.`, {
|
|
311
|
+
instanceAddress: instance.address.toString(),
|
|
312
|
+
computedAddress: computedAddress.toString(),
|
|
313
|
+
blockNum
|
|
314
|
+
});
|
|
315
|
+
return false;
|
|
316
|
+
}
|
|
317
|
+
return true;
|
|
318
|
+
});
|
|
232
319
|
if (contractInstances.length > 0) {
|
|
233
320
|
contractInstances.forEach((c)=>this.log.verbose(`${Operation[operation]} contract instance at ${c.address.toString()}`));
|
|
234
321
|
if (operation == 0) {
|
|
@@ -253,50 +340,4 @@ import groupBy from 'lodash.groupby';
|
|
|
253
340
|
}
|
|
254
341
|
return true;
|
|
255
342
|
}
|
|
256
|
-
/**
|
|
257
|
-
* Stores the functions that were broadcasted individually.
|
|
258
|
-
*
|
|
259
|
-
* @dev Beware that there is not a delete variant of this, since they are added to contract classes
|
|
260
|
-
* and will be deleted as part of the class if needed.
|
|
261
|
-
*/ async storeBroadcastedIndividualFunctions(allLogs, _blockNum) {
|
|
262
|
-
// Filter out private and utility function broadcast events
|
|
263
|
-
const privateFnEvents = allLogs.filter((log)=>PrivateFunctionBroadcastedEvent.isPrivateFunctionBroadcastedEvent(log)).map((log)=>PrivateFunctionBroadcastedEvent.fromLog(log));
|
|
264
|
-
const utilityFnEvents = allLogs.filter((log)=>UtilityFunctionBroadcastedEvent.isUtilityFunctionBroadcastedEvent(log)).map((log)=>UtilityFunctionBroadcastedEvent.fromLog(log));
|
|
265
|
-
// Group all events by contract class id
|
|
266
|
-
for (const [classIdString, classEvents] of Object.entries(groupBy([
|
|
267
|
-
...privateFnEvents,
|
|
268
|
-
...utilityFnEvents
|
|
269
|
-
], (e)=>e.contractClassId.toString()))){
|
|
270
|
-
const contractClassId = Fr.fromHexString(classIdString);
|
|
271
|
-
const contractClass = await this.store.getContractClass(contractClassId);
|
|
272
|
-
if (!contractClass) {
|
|
273
|
-
this.log.warn(`Skipping broadcasted functions as contract class ${contractClassId.toString()} was not found`);
|
|
274
|
-
continue;
|
|
275
|
-
}
|
|
276
|
-
// Split private and utility functions, and filter out invalid ones
|
|
277
|
-
const allFns = classEvents.map((e)=>e.toFunctionWithMembershipProof());
|
|
278
|
-
const privateFns = allFns.filter((fn)=>'utilityFunctionsTreeRoot' in fn);
|
|
279
|
-
const utilityFns = allFns.filter((fn)=>'privateFunctionsArtifactTreeRoot' in fn);
|
|
280
|
-
const privateFunctionsWithValidity = await Promise.all(privateFns.map(async (fn)=>({
|
|
281
|
-
fn,
|
|
282
|
-
valid: await isValidPrivateFunctionMembershipProof(fn, contractClass)
|
|
283
|
-
})));
|
|
284
|
-
const validPrivateFns = privateFunctionsWithValidity.filter(({ valid })=>valid).map(({ fn })=>fn);
|
|
285
|
-
const utilityFunctionsWithValidity = await Promise.all(utilityFns.map(async (fn)=>({
|
|
286
|
-
fn,
|
|
287
|
-
valid: await isValidUtilityFunctionMembershipProof(fn, contractClass)
|
|
288
|
-
})));
|
|
289
|
-
const validUtilityFns = utilityFunctionsWithValidity.filter(({ valid })=>valid).map(({ fn })=>fn);
|
|
290
|
-
const validFnCount = validPrivateFns.length + validUtilityFns.length;
|
|
291
|
-
if (validFnCount !== allFns.length) {
|
|
292
|
-
this.log.warn(`Skipping ${allFns.length - validFnCount} invalid functions`);
|
|
293
|
-
}
|
|
294
|
-
// Store the functions in the contract class in a single operation
|
|
295
|
-
if (validFnCount > 0) {
|
|
296
|
-
this.log.verbose(`Storing ${validFnCount} functions for contract class ${contractClassId.toString()}`);
|
|
297
|
-
}
|
|
298
|
-
return await this.store.addFunctions(contractClassId, validPrivateFns, validUtilityFns);
|
|
299
|
-
}
|
|
300
|
-
return true;
|
|
301
|
-
}
|
|
302
343
|
}
|