@aztec/archiver 4.0.0-nightly.20260114 → 4.0.0-nightly.20260116

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.
Files changed (169) hide show
  1. package/dest/archiver.d.ts +134 -0
  2. package/dest/archiver.d.ts.map +1 -0
  3. package/dest/archiver.js +768 -0
  4. package/dest/{archiver/config.d.ts → config.d.ts} +9 -1
  5. package/dest/config.d.ts.map +1 -0
  6. package/dest/{archiver/config.js → config.js} +9 -0
  7. package/dest/{archiver/errors.d.ts → errors.d.ts} +1 -1
  8. package/dest/errors.d.ts.map +1 -0
  9. package/dest/factory.d.ts +5 -6
  10. package/dest/factory.d.ts.map +1 -1
  11. package/dest/factory.js +82 -5
  12. package/dest/index.d.ts +10 -4
  13. package/dest/index.d.ts.map +1 -1
  14. package/dest/index.js +8 -3
  15. package/dest/interfaces.d.ts +9 -0
  16. package/dest/interfaces.d.ts.map +1 -0
  17. package/dest/interfaces.js +3 -0
  18. package/dest/{archiver/l1 → l1}/bin/retrieve-calldata.d.ts +1 -1
  19. package/dest/l1/bin/retrieve-calldata.d.ts.map +1 -0
  20. package/dest/{archiver/l1 → l1}/calldata_retriever.d.ts +2 -2
  21. package/dest/l1/calldata_retriever.d.ts.map +1 -0
  22. package/dest/{archiver/l1 → l1}/data_retrieval.d.ts +2 -2
  23. package/dest/l1/data_retrieval.d.ts.map +1 -0
  24. package/dest/{archiver/l1 → l1}/debug_tx.d.ts +1 -1
  25. package/dest/l1/debug_tx.d.ts.map +1 -0
  26. package/dest/{archiver/l1 → l1}/spire_proposer.d.ts +1 -1
  27. package/dest/l1/spire_proposer.d.ts.map +1 -0
  28. package/dest/{archiver/l1 → l1}/trace_tx.d.ts +1 -1
  29. package/dest/l1/trace_tx.d.ts.map +1 -0
  30. package/dest/l1/types.d.ts +12 -0
  31. package/dest/l1/types.d.ts.map +1 -0
  32. package/dest/{archiver/l1 → l1}/validate_trace.d.ts +1 -1
  33. package/dest/l1/validate_trace.d.ts.map +1 -0
  34. package/dest/modules/data_source_base.d.ts +83 -0
  35. package/dest/modules/data_source_base.d.ts.map +1 -0
  36. package/dest/{archiver/archive_source_base.js → modules/data_source_base.js} +109 -10
  37. package/dest/modules/data_store_updater.d.ts +46 -0
  38. package/dest/modules/data_store_updater.d.ts.map +1 -0
  39. package/dest/modules/data_store_updater.js +216 -0
  40. package/dest/modules/instrumentation.d.ts +37 -0
  41. package/dest/modules/instrumentation.d.ts.map +1 -0
  42. package/dest/modules/l1_synchronizer.d.ts +73 -0
  43. package/dest/modules/l1_synchronizer.d.ts.map +1 -0
  44. package/dest/{archiver/archiver.js → modules/l1_synchronizer.js} +62 -542
  45. package/dest/{archiver → modules}/validation.d.ts +1 -1
  46. package/dest/modules/validation.d.ts.map +1 -0
  47. package/dest/{archiver/kv_archiver_store → store}/block_store.d.ts +1 -1
  48. package/dest/store/block_store.d.ts.map +1 -0
  49. package/dest/store/contract_class_store.d.ts +18 -0
  50. package/dest/store/contract_class_store.d.ts.map +1 -0
  51. package/dest/store/contract_instance_store.d.ts +24 -0
  52. package/dest/store/contract_instance_store.d.ts.map +1 -0
  53. package/dest/{archiver/kv_archiver_store → store}/kv_archiver_store.d.ts +2 -2
  54. package/dest/store/kv_archiver_store.d.ts.map +1 -0
  55. package/dest/{archiver/kv_archiver_store → store}/log_store.d.ts +1 -1
  56. package/dest/store/log_store.d.ts.map +1 -0
  57. package/dest/{archiver/kv_archiver_store → store}/message_store.d.ts +1 -1
  58. package/dest/store/message_store.d.ts.map +1 -0
  59. package/dest/{archiver/structs → structs}/data_retrieval.d.ts +1 -1
  60. package/dest/structs/data_retrieval.d.ts.map +1 -0
  61. package/dest/structs/inbox_message.d.ts +15 -0
  62. package/dest/structs/inbox_message.d.ts.map +1 -0
  63. package/dest/{archiver/structs → structs}/published.d.ts +1 -1
  64. package/dest/structs/published.d.ts.map +1 -0
  65. package/dest/{archiver/test → test}/fake_l1_state.d.ts +1 -1
  66. package/dest/test/fake_l1_state.d.ts.map +1 -0
  67. package/dest/test/index.d.ts +2 -1
  68. package/dest/test/index.d.ts.map +1 -1
  69. package/dest/test/index.js +1 -0
  70. package/dest/test/mock_structs.d.ts +76 -2
  71. package/dest/test/mock_structs.d.ts.map +1 -1
  72. package/dest/test/mock_structs.js +133 -2
  73. package/package.json +15 -17
  74. package/src/archiver.ts +523 -0
  75. package/src/{archiver/config.ts → config.ts} +11 -0
  76. package/src/factory.ts +118 -6
  77. package/src/index.ts +10 -3
  78. package/src/interfaces.ts +9 -0
  79. package/src/{archiver/l1 → l1}/calldata_retriever.ts +1 -1
  80. package/src/{archiver/l1 → l1}/data_retrieval.ts +1 -1
  81. package/src/{archiver/archive_source_base.ts → modules/data_source_base.ts} +130 -30
  82. package/src/modules/data_store_updater.ts +318 -0
  83. package/src/{archiver/archiver.ts → modules/l1_synchronizer.ts} +74 -714
  84. package/src/test/index.ts +1 -0
  85. package/src/test/mock_structs.ts +247 -2
  86. package/dest/archiver/archive_source_base.d.ts +0 -75
  87. package/dest/archiver/archive_source_base.d.ts.map +0 -1
  88. package/dest/archiver/archiver.d.ts +0 -168
  89. package/dest/archiver/archiver.d.ts.map +0 -1
  90. package/dest/archiver/archiver_store_updates.d.ts +0 -38
  91. package/dest/archiver/archiver_store_updates.d.ts.map +0 -1
  92. package/dest/archiver/archiver_store_updates.js +0 -212
  93. package/dest/archiver/config.d.ts.map +0 -1
  94. package/dest/archiver/errors.d.ts.map +0 -1
  95. package/dest/archiver/index.d.ts +0 -8
  96. package/dest/archiver/index.d.ts.map +0 -1
  97. package/dest/archiver/index.js +0 -6
  98. package/dest/archiver/instrumentation.d.ts +0 -37
  99. package/dest/archiver/instrumentation.d.ts.map +0 -1
  100. package/dest/archiver/kv_archiver_store/block_store.d.ts.map +0 -1
  101. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts +0 -18
  102. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts.map +0 -1
  103. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts +0 -24
  104. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +0 -1
  105. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +0 -1
  106. package/dest/archiver/kv_archiver_store/log_store.d.ts.map +0 -1
  107. package/dest/archiver/kv_archiver_store/message_store.d.ts.map +0 -1
  108. package/dest/archiver/l1/bin/retrieve-calldata.d.ts.map +0 -1
  109. package/dest/archiver/l1/calldata_retriever.d.ts.map +0 -1
  110. package/dest/archiver/l1/data_retrieval.d.ts.map +0 -1
  111. package/dest/archiver/l1/debug_tx.d.ts.map +0 -1
  112. package/dest/archiver/l1/spire_proposer.d.ts.map +0 -1
  113. package/dest/archiver/l1/trace_tx.d.ts.map +0 -1
  114. package/dest/archiver/l1/types.d.ts +0 -12
  115. package/dest/archiver/l1/types.d.ts.map +0 -1
  116. package/dest/archiver/l1/validate_trace.d.ts.map +0 -1
  117. package/dest/archiver/structs/data_retrieval.d.ts.map +0 -1
  118. package/dest/archiver/structs/inbox_message.d.ts +0 -15
  119. package/dest/archiver/structs/inbox_message.d.ts.map +0 -1
  120. package/dest/archiver/structs/published.d.ts.map +0 -1
  121. package/dest/archiver/test/fake_l1_state.d.ts.map +0 -1
  122. package/dest/archiver/validation.d.ts.map +0 -1
  123. package/dest/rpc/index.d.ts +0 -9
  124. package/dest/rpc/index.d.ts.map +0 -1
  125. package/dest/rpc/index.js +0 -15
  126. package/src/archiver/archiver_store_updates.ts +0 -321
  127. package/src/archiver/index.ts +0 -7
  128. package/src/rpc/index.ts +0 -16
  129. /package/dest/{archiver/errors.js → errors.js} +0 -0
  130. /package/dest/{archiver/l1 → l1}/bin/retrieve-calldata.js +0 -0
  131. /package/dest/{archiver/l1 → l1}/calldata_retriever.js +0 -0
  132. /package/dest/{archiver/l1 → l1}/data_retrieval.js +0 -0
  133. /package/dest/{archiver/l1 → l1}/debug_tx.js +0 -0
  134. /package/dest/{archiver/l1 → l1}/spire_proposer.js +0 -0
  135. /package/dest/{archiver/l1 → l1}/trace_tx.js +0 -0
  136. /package/dest/{archiver/l1 → l1}/types.js +0 -0
  137. /package/dest/{archiver/l1 → l1}/validate_trace.js +0 -0
  138. /package/dest/{archiver → modules}/instrumentation.js +0 -0
  139. /package/dest/{archiver → modules}/validation.js +0 -0
  140. /package/dest/{archiver/kv_archiver_store → store}/block_store.js +0 -0
  141. /package/dest/{archiver/kv_archiver_store → store}/contract_class_store.js +0 -0
  142. /package/dest/{archiver/kv_archiver_store → store}/contract_instance_store.js +0 -0
  143. /package/dest/{archiver/kv_archiver_store → store}/kv_archiver_store.js +0 -0
  144. /package/dest/{archiver/kv_archiver_store → store}/log_store.js +0 -0
  145. /package/dest/{archiver/kv_archiver_store → store}/message_store.js +0 -0
  146. /package/dest/{archiver/structs → structs}/data_retrieval.js +0 -0
  147. /package/dest/{archiver/structs → structs}/inbox_message.js +0 -0
  148. /package/dest/{archiver/structs → structs}/published.js +0 -0
  149. /package/dest/{archiver/test → test}/fake_l1_state.js +0 -0
  150. /package/src/{archiver/errors.ts → errors.ts} +0 -0
  151. /package/src/{archiver/l1 → l1}/README.md +0 -0
  152. /package/src/{archiver/l1 → l1}/bin/retrieve-calldata.ts +0 -0
  153. /package/src/{archiver/l1 → l1}/debug_tx.ts +0 -0
  154. /package/src/{archiver/l1 → l1}/spire_proposer.ts +0 -0
  155. /package/src/{archiver/l1 → l1}/trace_tx.ts +0 -0
  156. /package/src/{archiver/l1 → l1}/types.ts +0 -0
  157. /package/src/{archiver/l1 → l1}/validate_trace.ts +0 -0
  158. /package/src/{archiver → modules}/instrumentation.ts +0 -0
  159. /package/src/{archiver → modules}/validation.ts +0 -0
  160. /package/src/{archiver/kv_archiver_store → store}/block_store.ts +0 -0
  161. /package/src/{archiver/kv_archiver_store → store}/contract_class_store.ts +0 -0
  162. /package/src/{archiver/kv_archiver_store → store}/contract_instance_store.ts +0 -0
  163. /package/src/{archiver/kv_archiver_store → store}/kv_archiver_store.ts +0 -0
  164. /package/src/{archiver/kv_archiver_store → store}/log_store.ts +0 -0
  165. /package/src/{archiver/kv_archiver_store → store}/message_store.ts +0 -0
  166. /package/src/{archiver/structs → structs}/data_retrieval.ts +0 -0
  167. /package/src/{archiver/structs → structs}/inbox_message.ts +0 -0
  168. /package/src/{archiver/structs → structs}/published.ts +0 -0
  169. /package/src/{archiver/test → test}/fake_l1_state.ts +0 -0
@@ -0,0 +1,318 @@
1
+ import type { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
2
+ import { Fr } from '@aztec/foundation/curves/bn254';
3
+ import { createLogger } from '@aztec/foundation/log';
4
+ import {
5
+ ContractClassPublishedEvent,
6
+ PrivateFunctionBroadcastedEvent,
7
+ UtilityFunctionBroadcastedEvent,
8
+ } from '@aztec/protocol-contracts/class-registry';
9
+ import {
10
+ ContractInstancePublishedEvent,
11
+ ContractInstanceUpdatedEvent,
12
+ } from '@aztec/protocol-contracts/instance-registry';
13
+ import type { L2BlockNew, ValidateCheckpointResult } from '@aztec/stdlib/block';
14
+ import type { PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
15
+ import {
16
+ type ExecutablePrivateFunctionWithMembershipProof,
17
+ type UtilityFunctionWithMembershipProof,
18
+ computePublicBytecodeCommitment,
19
+ isValidPrivateFunctionMembershipProof,
20
+ isValidUtilityFunctionMembershipProof,
21
+ } from '@aztec/stdlib/contract';
22
+ import type { ContractClassLog, PrivateLog, PublicLog } from '@aztec/stdlib/logs';
23
+ import type { UInt64 } from '@aztec/stdlib/types';
24
+
25
+ import groupBy from 'lodash.groupby';
26
+
27
+ import type { KVArchiverDataStore } from '../store/kv_archiver_store.js';
28
+
29
+ /** Operation type for contract data updates. */
30
+ enum Operation {
31
+ Store,
32
+ Delete,
33
+ }
34
+
35
+ /** Archiver helper module to handle updates to the data store. */
36
+ export class ArchiverDataStoreUpdater {
37
+ private readonly log = createLogger('archiver:store_updater');
38
+
39
+ constructor(private store: KVArchiverDataStore) {}
40
+
41
+ /**
42
+ * Adds blocks to the store with contract class/instance extraction from logs.
43
+ * Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events,
44
+ * and individually broadcasted functions from the block logs.
45
+ *
46
+ * @param blocks - The L2 blocks to add.
47
+ * @param pendingChainValidationStatus - Optional validation status to set.
48
+ * @returns True if the operation is successful.
49
+ */
50
+ public addBlocksWithContractData(
51
+ blocks: L2BlockNew[],
52
+ pendingChainValidationStatus?: ValidateCheckpointResult,
53
+ ): Promise<boolean> {
54
+ return this.store.transactionAsync(async () => {
55
+ await this.store.addBlocks(blocks);
56
+
57
+ const opResults = await Promise.all([
58
+ // Update the pending chain validation status if provided
59
+ pendingChainValidationStatus && this.store.setPendingChainValidationStatus(pendingChainValidationStatus),
60
+ // Add any logs emitted during the retrieved blocks
61
+ this.store.addLogs(blocks),
62
+ // Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
63
+ ...blocks.map(block => this.addBlockDataToDB(block)),
64
+ ]);
65
+
66
+ return opResults.every(Boolean);
67
+ });
68
+ }
69
+
70
+ /**
71
+ * Adds checkpoints to the store with contract class/instance extraction from logs.
72
+ * Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events,
73
+ * and individually broadcasted functions from the checkpoint block logs.
74
+ *
75
+ * @param checkpoints - The published checkpoints to add.
76
+ * @param pendingChainValidationStatus - Optional validation status to set.
77
+ * @returns True if the operation is successful.
78
+ */
79
+ public addCheckpointsWithContractData(
80
+ checkpoints: PublishedCheckpoint[],
81
+ pendingChainValidationStatus?: ValidateCheckpointResult,
82
+ ): Promise<boolean> {
83
+ return this.store.transactionAsync(async () => {
84
+ await this.store.addCheckpoints(checkpoints);
85
+ const allBlocks = checkpoints.flatMap((ch: PublishedCheckpoint) => ch.checkpoint.blocks);
86
+
87
+ const opResults = await Promise.all([
88
+ // Update the pending chain validation status if provided
89
+ pendingChainValidationStatus && this.store.setPendingChainValidationStatus(pendingChainValidationStatus),
90
+ // Add any logs emitted during the retrieved blocks
91
+ this.store.addLogs(allBlocks),
92
+ // Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
93
+ ...allBlocks.map(block => this.addBlockDataToDB(block)),
94
+ ]);
95
+
96
+ return opResults.every(Boolean);
97
+ });
98
+ }
99
+
100
+ /**
101
+ * Unwinds checkpoints from the store with reverse contract extraction.
102
+ * Deletes ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated data
103
+ * that was stored for the unwound checkpoints.
104
+ *
105
+ * @param from - The checkpoint number to unwind from (must be the current tip).
106
+ * @param checkpointsToUnwind - The number of checkpoints to unwind.
107
+ * @returns True if the operation is successful.
108
+ */
109
+ public async unwindCheckpointsWithContractData(
110
+ from: CheckpointNumber,
111
+ checkpointsToUnwind: number,
112
+ ): Promise<boolean> {
113
+ if (checkpointsToUnwind <= 0) {
114
+ throw new Error(`Cannot unwind ${checkpointsToUnwind} blocks`);
115
+ }
116
+
117
+ const last = await this.store.getSynchedCheckpointNumber();
118
+ if (from != last) {
119
+ throw new Error(`Cannot unwind checkpoints from checkpoint ${from} when the last checkpoint is ${last}`);
120
+ }
121
+
122
+ const blocks = [];
123
+ const lastCheckpointNumber = from + checkpointsToUnwind - 1;
124
+ for (let checkpointNumber = from; checkpointNumber <= lastCheckpointNumber; checkpointNumber++) {
125
+ const blocksForCheckpoint = await this.store.getBlocksForCheckpoint(checkpointNumber);
126
+ if (!blocksForCheckpoint) {
127
+ continue;
128
+ }
129
+ blocks.push(...blocksForCheckpoint);
130
+ }
131
+
132
+ const opResults = await Promise.all([
133
+ // Prune rolls back to the last proven block, which is by definition valid
134
+ this.store.setPendingChainValidationStatus({ valid: true }),
135
+ // Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
136
+ ...blocks.map(async block => {
137
+ const contractClassLogs = block.body.txEffects.flatMap(txEffect => txEffect.contractClassLogs);
138
+ // ContractInstancePublished event logs are broadcast in privateLogs.
139
+ const privateLogs = block.body.txEffects.flatMap(txEffect => txEffect.privateLogs);
140
+ const publicLogs = block.body.txEffects.flatMap(txEffect => txEffect.publicLogs);
141
+
142
+ return (
143
+ await Promise.all([
144
+ this.updatePublishedContractClasses(contractClassLogs, block.number, Operation.Delete),
145
+ this.updateDeployedContractInstances(privateLogs, block.number, Operation.Delete),
146
+ this.updateUpdatedContractInstances(publicLogs, block.header.globalVariables.timestamp, Operation.Delete),
147
+ ])
148
+ ).every(Boolean);
149
+ }),
150
+
151
+ this.store.deleteLogs(blocks),
152
+ this.store.unwindCheckpoints(from, checkpointsToUnwind),
153
+ ]);
154
+
155
+ return opResults.every(Boolean);
156
+ }
157
+
158
+ /**
159
+ * Extracts and stores contract data from a single block.
160
+ */
161
+ private async addBlockDataToDB(block: L2BlockNew): Promise<boolean> {
162
+ const contractClassLogs = block.body.txEffects.flatMap(txEffect => txEffect.contractClassLogs);
163
+ // ContractInstancePublished event logs are broadcast in privateLogs.
164
+ const privateLogs = block.body.txEffects.flatMap(txEffect => txEffect.privateLogs);
165
+ const publicLogs = block.body.txEffects.flatMap(txEffect => txEffect.publicLogs);
166
+
167
+ return (
168
+ await Promise.all([
169
+ this.updatePublishedContractClasses(contractClassLogs, block.number, Operation.Store),
170
+ this.updateDeployedContractInstances(privateLogs, block.number, Operation.Store),
171
+ this.updateUpdatedContractInstances(publicLogs, block.header.globalVariables.timestamp, Operation.Store),
172
+ this.storeBroadcastedIndividualFunctions(contractClassLogs, block.number),
173
+ ])
174
+ ).every(Boolean);
175
+ }
176
+
177
+ /**
178
+ * Extracts and stores contract classes out of ContractClassPublished events emitted by the class registry contract.
179
+ */
180
+ private async updatePublishedContractClasses(
181
+ allLogs: ContractClassLog[],
182
+ blockNum: BlockNumber,
183
+ operation: Operation,
184
+ ): Promise<boolean> {
185
+ const contractClassPublishedEvents = allLogs
186
+ .filter(log => ContractClassPublishedEvent.isContractClassPublishedEvent(log))
187
+ .map(log => ContractClassPublishedEvent.fromLog(log));
188
+
189
+ const contractClasses = await Promise.all(contractClassPublishedEvents.map(e => e.toContractClassPublic()));
190
+ if (contractClasses.length > 0) {
191
+ contractClasses.forEach(c => this.log.verbose(`${Operation[operation]} contract class ${c.id.toString()}`));
192
+ if (operation == Operation.Store) {
193
+ // TODO: Will probably want to create some worker threads to compute these bytecode commitments as they are expensive
194
+ const commitments = await Promise.all(
195
+ contractClasses.map(c => computePublicBytecodeCommitment(c.packedBytecode)),
196
+ );
197
+ return await this.store.addContractClasses(contractClasses, commitments, blockNum);
198
+ } else if (operation == Operation.Delete) {
199
+ return await this.store.deleteContractClasses(contractClasses, blockNum);
200
+ }
201
+ }
202
+ return true;
203
+ }
204
+
205
+ /**
206
+ * Extracts and stores contract instances out of ContractInstancePublished events emitted by the canonical deployer contract.
207
+ */
208
+ private async updateDeployedContractInstances(
209
+ allLogs: PrivateLog[],
210
+ blockNum: BlockNumber,
211
+ operation: Operation,
212
+ ): Promise<boolean> {
213
+ const contractInstances = allLogs
214
+ .filter(log => ContractInstancePublishedEvent.isContractInstancePublishedEvent(log))
215
+ .map(log => ContractInstancePublishedEvent.fromLog(log))
216
+ .map(e => e.toContractInstance());
217
+ if (contractInstances.length > 0) {
218
+ contractInstances.forEach(c =>
219
+ this.log.verbose(`${Operation[operation]} contract instance at ${c.address.toString()}`),
220
+ );
221
+ if (operation == Operation.Store) {
222
+ return await this.store.addContractInstances(contractInstances, blockNum);
223
+ } else if (operation == Operation.Delete) {
224
+ return await this.store.deleteContractInstances(contractInstances, blockNum);
225
+ }
226
+ }
227
+ return true;
228
+ }
229
+
230
+ /**
231
+ * Extracts and stores contract instance updates out of ContractInstanceUpdated events.
232
+ */
233
+ private async updateUpdatedContractInstances(
234
+ allLogs: PublicLog[],
235
+ timestamp: UInt64,
236
+ operation: Operation,
237
+ ): Promise<boolean> {
238
+ const contractUpdates = allLogs
239
+ .filter(log => ContractInstanceUpdatedEvent.isContractInstanceUpdatedEvent(log))
240
+ .map(log => ContractInstanceUpdatedEvent.fromLog(log))
241
+ .map(e => e.toContractInstanceUpdate());
242
+
243
+ if (contractUpdates.length > 0) {
244
+ contractUpdates.forEach(c =>
245
+ this.log.verbose(`${Operation[operation]} contract instance update at ${c.address.toString()}`),
246
+ );
247
+ if (operation == Operation.Store) {
248
+ return await this.store.addContractInstanceUpdates(contractUpdates, timestamp);
249
+ } else if (operation == Operation.Delete) {
250
+ return await this.store.deleteContractInstanceUpdates(contractUpdates, timestamp);
251
+ }
252
+ }
253
+ return true;
254
+ }
255
+
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
+ */
262
+ private async storeBroadcastedIndividualFunctions(
263
+ allLogs: ContractClassLog[],
264
+ _blockNum: BlockNumber,
265
+ ): Promise<boolean> {
266
+ // Filter out private and utility function broadcast events
267
+ const privateFnEvents = allLogs
268
+ .filter(log => PrivateFunctionBroadcastedEvent.isPrivateFunctionBroadcastedEvent(log))
269
+ .map(log => PrivateFunctionBroadcastedEvent.fromLog(log));
270
+ const utilityFnEvents = allLogs
271
+ .filter(log => UtilityFunctionBroadcastedEvent.isUtilityFunctionBroadcastedEvent(log))
272
+ .map(log => UtilityFunctionBroadcastedEvent.fromLog(log));
273
+
274
+ // Group all events by contract class id
275
+ for (const [classIdString, classEvents] of Object.entries(
276
+ groupBy([...privateFnEvents, ...utilityFnEvents], e => e.contractClassId.toString()),
277
+ )) {
278
+ const contractClassId = Fr.fromHexString(classIdString);
279
+ const contractClass = await this.store.getContractClass(contractClassId);
280
+ if (!contractClass) {
281
+ this.log.warn(`Skipping broadcasted functions as contract class ${contractClassId.toString()} was not found`);
282
+ continue;
283
+ }
284
+
285
+ // Split private and utility functions, and filter out invalid ones
286
+ const allFns = classEvents.map(e => e.toFunctionWithMembershipProof());
287
+ const privateFns = allFns.filter(
288
+ (fn): fn is ExecutablePrivateFunctionWithMembershipProof => 'utilityFunctionsTreeRoot' in fn,
289
+ );
290
+ const utilityFns = allFns.filter(
291
+ (fn): fn is UtilityFunctionWithMembershipProof => 'privateFunctionsArtifactTreeRoot' in fn,
292
+ );
293
+
294
+ const privateFunctionsWithValidity = await Promise.all(
295
+ privateFns.map(async fn => ({ fn, valid: await isValidPrivateFunctionMembershipProof(fn, contractClass) })),
296
+ );
297
+ const validPrivateFns = privateFunctionsWithValidity.filter(({ valid }) => valid).map(({ fn }) => fn);
298
+ const utilityFunctionsWithValidity = await Promise.all(
299
+ utilityFns.map(async fn => ({
300
+ fn,
301
+ valid: await isValidUtilityFunctionMembershipProof(fn, contractClass),
302
+ })),
303
+ );
304
+ const validUtilityFns = utilityFunctionsWithValidity.filter(({ valid }) => valid).map(({ fn }) => fn);
305
+ const validFnCount = validPrivateFns.length + validUtilityFns.length;
306
+ if (validFnCount !== allFns.length) {
307
+ this.log.warn(`Skipping ${allFns.length - validFnCount} invalid functions`);
308
+ }
309
+
310
+ // Store the functions in the contract class in a single operation
311
+ if (validFnCount > 0) {
312
+ this.log.verbose(`Storing ${validFnCount} functions for contract class ${contractClassId.toString()}`);
313
+ }
314
+ return await this.store.addFunctions(contractClassId, validPrivateFns, validUtilityFns);
315
+ }
316
+ return true;
317
+ }
318
+ }