@aztec/pxe 4.0.0-rc.3 → 4.0.0-rc.5

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.
@@ -1,8 +1,53 @@
1
+ import { Fr } from '@aztec/foundation/curves/bn254';
1
2
  import { toArray } from '@aztec/foundation/iterable';
3
+ import { BufferReader, numToUInt8, serializeToBuffer } from '@aztec/foundation/serialize';
2
4
  import { FunctionCall, FunctionSelector, FunctionType, contractArtifactFromBuffer, contractArtifactToBuffer, encodeArguments, getFunctionDebugMetadata } from '@aztec/stdlib/abi';
3
5
  import { AztecAddress } from '@aztec/stdlib/aztec-address';
4
6
  import { SerializableContractInstance, getContractClassFromArtifact } from '@aztec/stdlib/contract';
5
7
  import { PrivateFunctionsTree } from './private_functions_tree.js';
8
+ const VERSION = 1;
9
+ /**
10
+ * All contract class data except the large packedBytecode.
11
+ * The expensive data from the ContractClass is precomputed and stored in this format to avoid redundant hashing.
12
+ * Since we have to store the artifacts anyway, the final ContractClass is reconstructed by combining this data
13
+ * with the packedBytecode obtained from the former. That way we can have quick class lookups without wasted storage.
14
+ */ export class SerializableContractClassData {
15
+ version = VERSION;
16
+ id;
17
+ artifactHash;
18
+ privateFunctionsRoot;
19
+ publicBytecodeCommitment;
20
+ privateFunctions;
21
+ constructor(data){
22
+ this.id = data.id;
23
+ this.artifactHash = data.artifactHash;
24
+ this.privateFunctionsRoot = data.privateFunctionsRoot;
25
+ this.publicBytecodeCommitment = data.publicBytecodeCommitment;
26
+ this.privateFunctions = data.privateFunctions;
27
+ }
28
+ toBuffer() {
29
+ return serializeToBuffer(numToUInt8(this.version), this.id, this.artifactHash, this.privateFunctionsRoot, this.publicBytecodeCommitment, this.privateFunctions.length, ...this.privateFunctions.map((fn)=>serializeToBuffer(fn.selector, fn.vkHash)));
30
+ }
31
+ static fromBuffer(bufferOrReader) {
32
+ const reader = BufferReader.asReader(bufferOrReader);
33
+ const version = reader.readUInt8();
34
+ if (version !== VERSION) {
35
+ throw new Error(`Unexpected contract class data version ${version}`);
36
+ }
37
+ return new SerializableContractClassData({
38
+ id: reader.readObject(Fr),
39
+ artifactHash: reader.readObject(Fr),
40
+ privateFunctionsRoot: reader.readObject(Fr),
41
+ publicBytecodeCommitment: reader.readObject(Fr),
42
+ privateFunctions: reader.readVector({
43
+ fromBuffer: (r)=>({
44
+ selector: r.readObject(FunctionSelector),
45
+ vkHash: r.readObject(Fr)
46
+ })
47
+ })
48
+ });
49
+ }
50
+ }
6
51
  /**
7
52
  * ContractStore serves as a data manager and retriever for Aztec.nr contracts.
8
53
  * It provides methods to obtain contract addresses, function ABI, bytecode, and membership witnesses
@@ -12,24 +57,46 @@ import { PrivateFunctionsTree } from './private_functions_tree.js';
12
57
  */ export class ContractStore {
13
58
  /** Map from contract class id to private function tree. */ // TODO: Update it to be LRU cache so that it doesn't keep all the data all the time.
14
59
  #privateFunctionTrees = new Map();
15
- /** Map from contract address to contract class id */ #contractClassIdMap = new Map();
60
+ /**
61
+ * In-memory cache of deserialized ContractArtifact objects, keyed by class id string.
62
+ * Avoids repeated LMDB reads + JSON.parse + Zod validation on every oracle call.
63
+ * Artifacts are large but immutable after registration — safe to cache for the lifetime of the store.
64
+ */ // TODO: Update it to be LRU cache so that it doesn't keep all the data all the time.
65
+ #contractArtifactCache = new Map();
66
+ /** Map from contract address to contract class id (avoids KV round-trip on hot path). */ #contractClassIdMap = new Map();
16
67
  #store;
17
68
  #contractArtifacts;
69
+ #contractClassData;
18
70
  #contractInstances;
19
71
  constructor(store){
20
72
  this.#store = store;
21
73
  this.#contractArtifacts = store.openMap('contract_artifacts');
74
+ this.#contractClassData = store.openMap('contract_classes');
22
75
  this.#contractInstances = store.openMap('contracts_instances');
23
76
  }
24
77
  // Setters
25
- async addContractArtifact(id, contract) {
26
- // Validation outside transactionAsync - these are not DB operations
78
+ /**
79
+ * Registers a new contract artifact and its corresponding class data.
80
+ * IMPORTANT: This method does not verify that the provided artifact matches the class data or that the class id matches the artifact.
81
+ * It is the caller's responsibility to ensure the consistency and correctness of the provided data.
82
+ * This is done to avoid redundant, expensive contract class computations
83
+ */ async addContractArtifact(contract, contractClassWithIdAndPreimage) {
84
+ const contractClass = contractClassWithIdAndPreimage ?? await getContractClassFromArtifact(contract);
85
+ const key = contractClass.id.toString();
86
+ if (this.#contractArtifactCache.has(key)) {
87
+ return contractClass.id;
88
+ }
27
89
  const privateFunctions = contract.functions.filter((functionArtifact)=>functionArtifact.functionType === FunctionType.PRIVATE);
28
- const privateSelectors = await Promise.all(privateFunctions.map(async (privateFunctionArtifact)=>(await FunctionSelector.fromNameAndParameters(privateFunctionArtifact.name, privateFunctionArtifact.parameters)).toString()));
90
+ const privateSelectors = await Promise.all(privateFunctions.map(async (fn)=>(await FunctionSelector.fromNameAndParameters(fn.name, fn.parameters)).toString()));
29
91
  if (privateSelectors.length !== new Set(privateSelectors).size) {
30
92
  throw new Error('Repeated function selectors of private functions');
31
93
  }
32
- await this.#store.transactionAsync(()=>this.#contractArtifacts.set(id.toString(), contractArtifactToBuffer(contract)));
94
+ this.#contractArtifactCache.set(key, contract);
95
+ await this.#store.transactionAsync(async ()=>{
96
+ await this.#contractArtifacts.set(key, contractArtifactToBuffer(contract));
97
+ await this.#contractClassData.set(key, new SerializableContractClassData(contractClass).toBuffer());
98
+ });
99
+ return contractClass.id;
33
100
  }
34
101
  async addContractInstance(contract) {
35
102
  this.#contractClassIdMap.set(contract.address.toString(), contract.currentContractClassId);
@@ -37,25 +104,17 @@ import { PrivateFunctionsTree } from './private_functions_tree.js';
37
104
  }
38
105
  // Private getters
39
106
  async #getContractClassId(contractAddress) {
40
- if (!this.#contractClassIdMap.has(contractAddress.toString())) {
107
+ const key = contractAddress.toString();
108
+ if (!this.#contractClassIdMap.has(key)) {
41
109
  const instance = await this.getContractInstance(contractAddress);
42
110
  if (!instance) {
43
111
  return;
44
112
  }
45
- this.#contractClassIdMap.set(contractAddress.toString(), instance.currentContractClassId);
113
+ this.#contractClassIdMap.set(key, instance.currentContractClassId);
46
114
  }
47
- return this.#contractClassIdMap.get(contractAddress.toString());
115
+ return this.#contractClassIdMap.get(key);
48
116
  }
49
- /**
50
- * Retrieve or create a ContractTree instance based on the provided class id.
51
- * If an existing tree with the same class id is found in the cache, it will be returned.
52
- * Otherwise, a new ContractTree instance will be created using the contract data from the database
53
- * and added to the cache before returning.
54
- *
55
- * @param classId - The class id of the contract for which the ContractTree is required.
56
- * @returns A ContractTree instance associated with the specified contract address.
57
- * @throws An Error if the contract is not found in the ContractDatabase.
58
- */ async #getPrivateFunctionTreeForClassId(classId) {
117
+ async #getPrivateFunctionTreeForClassId(classId) {
59
118
  if (!this.#privateFunctionTrees.has(classId.toString())) {
60
119
  const artifact = await this.getContractArtifact(classId);
61
120
  if (!artifact) {
@@ -66,9 +125,9 @@ import { PrivateFunctionsTree } from './private_functions_tree.js';
66
125
  }
67
126
  return this.#privateFunctionTrees.get(classId.toString());
68
127
  }
69
- async #getContractArtifactByAddress(contractAddress) {
70
- const contractClassId = await this.#getContractClassId(contractAddress);
71
- return contractClassId && this.getContractArtifact(contractClassId);
128
+ async #getArtifactByAddress(contractAddress) {
129
+ const classId = await this.#getContractClassId(contractAddress);
130
+ return classId && this.getContractArtifact(classId);
72
131
  }
73
132
  // Public getters
74
133
  getContractsAddresses() {
@@ -77,22 +136,43 @@ import { PrivateFunctionsTree } from './private_functions_tree.js';
77
136
  return keys.map(AztecAddress.fromString);
78
137
  });
79
138
  }
80
- /** Returns a contract instance for a given address. Throws if not found. */ getContractInstance(contractAddress) {
139
+ /** Returns a contract instance for a given address. */ getContractInstance(contractAddress) {
81
140
  return this.#store.transactionAsync(async ()=>{
82
141
  const contract = await this.#contractInstances.getAsync(contractAddress.toString());
83
142
  return contract && SerializableContractInstance.fromBuffer(contract).withAddress(contractAddress);
84
143
  });
85
144
  }
86
- getContractArtifact(contractClassId) {
87
- return this.#store.transactionAsync(async ()=>{
88
- const contract = await this.#contractArtifacts.getAsync(contractClassId.toString());
89
- // TODO(@spalladino): AztecAsyncMap lies and returns Uint8Arrays instead of Buffers, hence the extra Buffer.from.
90
- return contract && contractArtifactFromBuffer(Buffer.from(contract));
145
+ /** Returns the raw contract artifact for a given class id. */ async getContractArtifact(contractClassId) {
146
+ const key = contractClassId.toString();
147
+ const cached = this.#contractArtifactCache.get(key);
148
+ if (cached) {
149
+ return cached;
150
+ }
151
+ const artifact = await this.#store.transactionAsync(async ()=>{
152
+ const buf = await this.#contractArtifacts.getAsync(key);
153
+ return buf && contractArtifactFromBuffer(buf);
91
154
  });
155
+ if (artifact) {
156
+ this.#contractArtifactCache.set(key, artifact);
157
+ }
158
+ return artifact;
92
159
  }
93
- /** Returns a contract class for a given class id. Throws if not found. */ async getContractClass(contractClassId) {
160
+ /** Returns a contract class for a given class id. */ async getContractClassWithPreimage(contractClassId) {
161
+ const key = contractClassId.toString();
162
+ const buf = await this.#contractClassData.getAsync(key);
163
+ if (!buf) {
164
+ return undefined;
165
+ }
166
+ const classData = SerializableContractClassData.fromBuffer(buf);
94
167
  const artifact = await this.getContractArtifact(contractClassId);
95
- return artifact && getContractClassFromArtifact(artifact);
168
+ if (!artifact) {
169
+ return undefined;
170
+ }
171
+ const packedBytecode = artifact.functions.find((f)=>f.name === 'public_dispatch')?.bytecode ?? Buffer.alloc(0);
172
+ return {
173
+ ...classData,
174
+ packedBytecode
175
+ };
96
176
  }
97
177
  async getContract(address) {
98
178
  const instance = await this.getContractInstance(address);
@@ -110,17 +190,18 @@ import { PrivateFunctionsTree } from './private_functions_tree.js';
110
190
  }
111
191
  /**
112
192
  * Retrieves the artifact of a specified function within a given contract.
113
- * The function is identified by its selector, which is a unique code generated from the function's signature.
114
- * Throws an error if the contract address or function selector are invalid or not found.
115
193
  *
116
194
  * @param contractAddress - The AztecAddress representing the contract containing the function.
117
195
  * @param selector - The function selector.
118
196
  * @returns The corresponding function's artifact as an object.
119
197
  */ async getFunctionArtifact(contractAddress, selector) {
120
- const artifact = await this.#getContractArtifactByAddress(contractAddress);
121
- const fnArtifact = artifact && await this.#findFunctionArtifactBySelector(artifact, selector);
122
- return fnArtifact && {
123
- ...fnArtifact,
198
+ const artifact = await this.#getArtifactByAddress(contractAddress);
199
+ if (!artifact) {
200
+ return undefined;
201
+ }
202
+ const fn = await this.#findFunctionArtifactBySelector(artifact, selector);
203
+ return fn && {
204
+ ...fn,
124
205
  contractName: artifact.name
125
206
  };
126
207
  }
@@ -136,40 +217,38 @@ import { PrivateFunctionsTree } from './private_functions_tree.js';
136
217
  };
137
218
  }
138
219
  async getPublicFunctionArtifact(contractAddress) {
139
- const artifact = await this.#getContractArtifactByAddress(contractAddress);
140
- const fnArtifact = artifact && artifact.functions.find((fn)=>fn.functionType === FunctionType.PUBLIC);
141
- return fnArtifact && {
142
- ...fnArtifact,
220
+ const artifact = await this.#getArtifactByAddress(contractAddress);
221
+ const fn = artifact && artifact.functions.find((f)=>f.functionType === FunctionType.PUBLIC);
222
+ return fn && {
223
+ ...fn,
143
224
  contractName: artifact.name
144
225
  };
145
226
  }
146
227
  async getFunctionAbi(contractAddress, selector) {
147
- const artifact = await this.#getContractArtifactByAddress(contractAddress);
228
+ const artifact = await this.#getArtifactByAddress(contractAddress);
148
229
  return artifact && await this.#findFunctionAbiBySelector(artifact, selector);
149
230
  }
150
231
  /**
151
232
  * Retrieves the debug metadata of a specified function within a given contract.
152
- * The function is identified by its selector, which is a unique code generated from the function's signature.
153
- * Returns undefined if the debug metadata for the given function is not found.
154
- * Throws if the contract has not been added to the database.
155
233
  *
156
234
  * @param contractAddress - The AztecAddress representing the contract containing the function.
157
235
  * @param selector - The function selector.
158
- * @returns The corresponding function's artifact as an object.
236
+ * @returns The corresponding function's debug metadata, or undefined.
159
237
  */ async getFunctionDebugMetadata(contractAddress, selector) {
160
- const artifact = await this.#getContractArtifactByAddress(contractAddress);
161
- const fnArtifact = artifact && await this.#findFunctionArtifactBySelector(artifact, selector);
162
- return fnArtifact && getFunctionDebugMetadata(artifact, fnArtifact);
238
+ const artifact = await this.#getArtifactByAddress(contractAddress);
239
+ if (!artifact) {
240
+ return undefined;
241
+ }
242
+ const fn = await this.#findFunctionArtifactBySelector(artifact, selector);
243
+ return fn && getFunctionDebugMetadata(artifact, fn);
163
244
  }
164
245
  async getPublicFunctionDebugMetadata(contractAddress) {
165
- const artifact = await this.#getContractArtifactByAddress(contractAddress);
166
- const fnArtifact = artifact && artifact.functions.find((fn)=>fn.functionType === FunctionType.PUBLIC);
167
- return fnArtifact && getFunctionDebugMetadata(artifact, fnArtifact);
246
+ const artifact = await this.#getArtifactByAddress(contractAddress);
247
+ const fn = artifact && artifact.functions.find((f)=>f.functionType === FunctionType.PUBLIC);
248
+ return fn && getFunctionDebugMetadata(artifact, fn);
168
249
  }
169
250
  /**
170
251
  * Retrieve the function membership witness for the given contract class and function selector.
171
- * The function membership witness represents a proof that the function belongs to the specified contract.
172
- * Throws an error if the contract address or function selector is unknown.
173
252
  *
174
253
  * @param contractClassId - The id of the class.
175
254
  * @param selector - The function selector.
@@ -179,18 +258,16 @@ import { PrivateFunctionsTree } from './private_functions_tree.js';
179
258
  return tree?.getFunctionMembershipWitness(selector);
180
259
  }
181
260
  async getDebugContractName(contractAddress) {
182
- const artifact = await this.#getContractArtifactByAddress(contractAddress);
261
+ const artifact = await this.#getArtifactByAddress(contractAddress);
183
262
  return artifact?.name;
184
263
  }
185
264
  async getDebugFunctionName(contractAddress, selector) {
186
- const artifact = await this.#getContractArtifactByAddress(contractAddress);
187
- const fnArtifact = artifact && await this.#findFunctionAbiBySelector(artifact, selector);
188
- return `${artifact?.name ?? contractAddress}:${fnArtifact?.name ?? selector}`;
265
+ const artifact = await this.#getArtifactByAddress(contractAddress);
266
+ const fn = artifact && await this.#findFunctionAbiBySelector(artifact, selector);
267
+ return `${artifact?.name ?? contractAddress}:${fn?.name ?? selector}`;
189
268
  }
190
269
  async #findFunctionArtifactBySelector(artifact, selector) {
191
- const functions = artifact.functions;
192
- for(let i = 0; i < functions.length; i++){
193
- const fn = functions[i];
270
+ for (const fn of artifact.functions){
194
271
  const fnSelector = await FunctionSelector.fromNameAndParameters(fn.name, fn.parameters);
195
272
  if (fnSelector.equals(selector)) {
196
273
  return fn;
@@ -198,12 +275,10 @@ import { PrivateFunctionsTree } from './private_functions_tree.js';
198
275
  }
199
276
  }
200
277
  async #findFunctionAbiBySelector(artifact, selector) {
201
- const functions = [
278
+ for (const fn of [
202
279
  ...artifact.functions,
203
280
  ...artifact.nonDispatchPublicFunctions ?? []
204
- ];
205
- for(let i = 0; i < functions.length; i++){
206
- const fn = functions[i];
281
+ ]){
207
282
  const fnSelector = await FunctionSelector.fromNameAndParameters(fn.name, fn.parameters);
208
283
  if (fnSelector.equals(selector)) {
209
284
  return fn;
@@ -219,10 +294,11 @@ import { PrivateFunctionsTree } from './private_functions_tree.js';
219
294
  if (!functionDao) {
220
295
  throw new Error(`Unknown function ${functionName} in contract ${contract.name}.`);
221
296
  }
297
+ const selector = await FunctionSelector.fromNameAndParameters(functionDao.name, functionDao.parameters);
222
298
  return FunctionCall.from({
223
299
  name: functionDao.name,
224
300
  to,
225
- selector: await FunctionSelector.fromNameAndParameters(functionDao.name, functionDao.parameters),
301
+ selector,
226
302
  type: functionDao.functionType,
227
303
  hideMsgSender: false,
228
304
  isStatic: functionDao.isStatic,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aztec/pxe",
3
- "version": "4.0.0-rc.3",
3
+ "version": "4.0.0-rc.5",
4
4
  "type": "module",
5
5
  "typedocOptions": {
6
6
  "entryPoints": [
@@ -70,19 +70,19 @@
70
70
  ]
71
71
  },
72
72
  "dependencies": {
73
- "@aztec/bb-prover": "4.0.0-rc.3",
74
- "@aztec/bb.js": "4.0.0-rc.3",
75
- "@aztec/builder": "4.0.0-rc.3",
76
- "@aztec/constants": "4.0.0-rc.3",
77
- "@aztec/ethereum": "4.0.0-rc.3",
78
- "@aztec/foundation": "4.0.0-rc.3",
79
- "@aztec/key-store": "4.0.0-rc.3",
80
- "@aztec/kv-store": "4.0.0-rc.3",
81
- "@aztec/noir-protocol-circuits-types": "4.0.0-rc.3",
82
- "@aztec/noir-types": "4.0.0-rc.3",
83
- "@aztec/protocol-contracts": "4.0.0-rc.3",
84
- "@aztec/simulator": "4.0.0-rc.3",
85
- "@aztec/stdlib": "4.0.0-rc.3",
73
+ "@aztec/bb-prover": "4.0.0-rc.5",
74
+ "@aztec/bb.js": "4.0.0-rc.5",
75
+ "@aztec/builder": "4.0.0-rc.5",
76
+ "@aztec/constants": "4.0.0-rc.5",
77
+ "@aztec/ethereum": "4.0.0-rc.5",
78
+ "@aztec/foundation": "4.0.0-rc.5",
79
+ "@aztec/key-store": "4.0.0-rc.5",
80
+ "@aztec/kv-store": "4.0.0-rc.5",
81
+ "@aztec/noir-protocol-circuits-types": "4.0.0-rc.5",
82
+ "@aztec/noir-types": "4.0.0-rc.5",
83
+ "@aztec/protocol-contracts": "4.0.0-rc.5",
84
+ "@aztec/simulator": "4.0.0-rc.5",
85
+ "@aztec/stdlib": "4.0.0-rc.5",
86
86
  "koa": "^2.16.1",
87
87
  "koa-router": "^13.1.1",
88
88
  "lodash.omit": "^4.5.0",
@@ -91,8 +91,8 @@
91
91
  "viem": "npm:@aztec/viem@2.38.2"
92
92
  },
93
93
  "devDependencies": {
94
- "@aztec/merkle-tree": "4.0.0-rc.3",
95
- "@aztec/noir-test-contracts.js": "4.0.0-rc.3",
94
+ "@aztec/merkle-tree": "4.0.0-rc.5",
95
+ "@aztec/noir-test-contracts.js": "4.0.0-rc.5",
96
96
  "@jest/globals": "^30.0.0",
97
97
  "@types/jest": "^30.0.0",
98
98
  "@types/lodash.omit": "^4.5.7",
@@ -4,8 +4,6 @@ import {
4
4
  AVM_SENDL2TOL1MSG_BASE_L2_GAS,
5
5
  DA_GAS_PER_FIELD,
6
6
  FIXED_AVM_STARTUP_L2_GAS,
7
- FIXED_DA_GAS,
8
- FIXED_L2_GAS,
9
7
  L2_GAS_PER_CONTRACT_CLASS_LOG,
10
8
  L2_GAS_PER_L2_TO_L1_MSG,
11
9
  L2_GAS_PER_NOTE_HASH,
@@ -19,6 +17,9 @@ import {
19
17
  MAX_NULLIFIERS_PER_TX,
20
18
  MAX_NULLIFIER_READ_REQUESTS_PER_TX,
21
19
  MAX_PRIVATE_LOGS_PER_TX,
20
+ PRIVATE_TX_L2_GAS_OVERHEAD,
21
+ PUBLIC_TX_L2_GAS_OVERHEAD,
22
+ TX_DA_GAS_OVERHEAD,
22
23
  } from '@aztec/constants';
23
24
  import { arrayNonEmptyLength, padArrayEnd } from '@aztec/foundation/collection';
24
25
  import { Fr } from '@aztec/foundation/curves/bn254';
@@ -653,7 +654,12 @@ export async function generateSimulatedProvingResult(
653
654
 
654
655
  const publicInputs = new PrivateKernelTailCircuitPublicInputs(
655
656
  constantData,
656
- /*gasUsed=*/ gasUsed.add(Gas.from({ l2Gas: FIXED_L2_GAS, daGas: FIXED_DA_GAS })),
657
+ /*gasUsed=*/ gasUsed.add(
658
+ Gas.from({
659
+ l2Gas: isPrivateOnlyTx ? PRIVATE_TX_L2_GAS_OVERHEAD : PUBLIC_TX_L2_GAS_OVERHEAD,
660
+ daGas: TX_DA_GAS_OVERHEAD,
661
+ }),
662
+ ),
657
663
  /*feePayer=*/ AztecAddress.zero(),
658
664
  /*expirationTimestamp=*/ 0n,
659
665
  hasPublicCalls ? inputsForPublic : undefined,
@@ -8,11 +8,7 @@ import { ProtocolContractAddress } from '@aztec/protocol-contracts';
8
8
  import type { FunctionSelector } from '@aztec/stdlib/abi';
9
9
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
10
10
  import { BlockHash } from '@aztec/stdlib/block';
11
- import {
12
- type ContractInstanceWithAddress,
13
- computeContractClassIdPreimage,
14
- computeSaltedInitializationHash,
15
- } from '@aztec/stdlib/contract';
11
+ import { type ContractInstanceWithAddress, computeSaltedInitializationHash } from '@aztec/stdlib/contract';
16
12
  import { DelayedPublicMutableValues, DelayedPublicMutableValuesWithHash } from '@aztec/stdlib/delayed-public-mutable';
17
13
  import { computePublicDataTreeLeafSlot } from '@aztec/stdlib/hash';
18
14
  import type { AztecNode } from '@aztec/stdlib/interfaces/client';
@@ -49,11 +45,15 @@ export class PrivateKernelOracle {
49
45
 
50
46
  /** Retrieves the preimage of a contract class id from the contract classes db. */
51
47
  public async getContractClassIdPreimage(contractClassId: Fr) {
52
- const contractClass = await this.contractStore.getContractClass(contractClassId);
48
+ const contractClass = await this.contractStore.getContractClassWithPreimage(contractClassId);
53
49
  if (!contractClass) {
54
50
  throw new Error(`Contract class not found when getting class id preimage. Class id: ${contractClassId}.`);
55
51
  }
56
- return computeContractClassIdPreimage(contractClass);
52
+ return {
53
+ artifactHash: contractClass.artifactHash,
54
+ privateFunctionsRoot: contractClass.privateFunctionsRoot,
55
+ publicBytecodeCommitment: contractClass.publicBytecodeCommitment,
56
+ };
57
57
  }
58
58
 
59
59
  /** Returns a membership witness with the sibling path and leaf index in our private functions tree. */
package/src/pxe.ts CHANGED
@@ -344,9 +344,8 @@ export class PXE {
344
344
  async #registerProtocolContracts() {
345
345
  const registered: Record<string, string> = {};
346
346
  for (const name of protocolContractNames) {
347
- const { address, contractClass, instance, artifact } =
348
- await this.protocolContractsProvider.getProtocolContractArtifact(name);
349
- await this.contractStore.addContractArtifact(contractClass.id, artifact);
347
+ const { address, instance, artifact } = await this.protocolContractsProvider.getProtocolContractArtifact(name);
348
+ await this.contractStore.addContractArtifact(artifact);
350
349
  await this.contractStore.addContractInstance(instance);
351
350
  registered[name] = address.toString();
352
351
  }
@@ -601,8 +600,7 @@ export class PXE {
601
600
  * @param artifact - The build artifact for the contract class.
602
601
  */
603
602
  public async registerContractClass(artifact: ContractArtifact): Promise<void> {
604
- const { id: contractClassId } = await getContractClassFromArtifact(artifact);
605
- await this.contractStore.addContractArtifact(contractClassId, artifact);
603
+ const contractClassId = await this.contractStore.addContractArtifact(artifact);
606
604
  this.log.info(`Added contract class ${artifact.name} with id ${contractClassId}`);
607
605
  }
608
606
 
@@ -621,17 +619,17 @@ export class PXE {
621
619
  if (artifact) {
622
620
  // If the user provides an artifact, validate it against the expected class id and register it
623
621
  const contractClass = await getContractClassFromArtifact(artifact);
624
- const contractClassId = contractClass.id;
625
- if (!contractClassId.equals(instance.currentContractClassId)) {
622
+ if (!contractClass.id.equals(instance.currentContractClassId)) {
626
623
  throw new Error(
627
- `Artifact does not match expected class id (computed ${contractClassId} but instance refers to ${instance.currentContractClassId})`,
624
+ `Artifact does not match expected class id (computed ${contractClass.id} but instance refers to ${instance.currentContractClassId})`,
628
625
  );
629
626
  }
630
627
  const computedAddress = await computeContractAddressFromInstance(instance);
631
628
  if (!computedAddress.equals(instance.address)) {
632
629
  throw new Error('Added a contract in which the address does not match the contract instance.');
633
630
  }
634
- await this.contractStore.addContractArtifact(contractClass.id, artifact);
631
+
632
+ await this.contractStore.addContractArtifact(artifact, contractClass);
635
633
 
636
634
  const publicFunctionSignatures = artifact.functions
637
635
  .filter(fn => fn.functionType === FunctionType.PUBLIC)
@@ -680,15 +678,16 @@ export class PXE {
680
678
  throw new Error('Could not update contract to a class different from the current one.');
681
679
  }
682
680
 
683
- await this.contractStore.addContractArtifact(contractClass.id, artifact);
684
-
685
681
  const publicFunctionSignatures = artifact.functions
686
682
  .filter(fn => fn.functionType === FunctionType.PUBLIC)
687
683
  .map(fn => decodeFunctionSignature(fn.name, fn.parameters));
688
684
  await this.node.registerContractFunctionSignatures(publicFunctionSignatures);
689
685
 
690
686
  currentInstance.currentContractClassId = contractClass.id;
691
- await this.contractStore.addContractInstance(currentInstance);
687
+ await Promise.all([
688
+ this.contractStore.addContractArtifact(artifact, contractClass),
689
+ this.contractStore.addContractInstance(currentInstance),
690
+ ]);
692
691
  this.log.info(`Updated contract ${artifact.name} at ${contractAddress.toString()} to class ${contractClass.id}`);
693
692
  });
694
693
  }