@aztec/pxe 0.82.0 → 0.82.1

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 (35) hide show
  1. package/dest/config/index.js +1 -1
  2. package/dest/private_kernel/private_kernel_execution_prover.d.ts +0 -1
  3. package/dest/private_kernel/private_kernel_execution_prover.d.ts.map +1 -1
  4. package/dest/private_kernel/private_kernel_execution_prover.js +3 -12
  5. package/dest/private_kernel/private_kernel_oracle_impl.d.ts +1 -1
  6. package/dest/private_kernel/private_kernel_oracle_impl.d.ts.map +1 -1
  7. package/dest/private_kernel/private_kernel_oracle_impl.js +11 -1
  8. package/dest/pxe_oracle_interface/pxe_oracle_interface.d.ts.map +1 -1
  9. package/dest/pxe_oracle_interface/pxe_oracle_interface.js +6 -0
  10. package/dest/pxe_service/error_enriching.d.ts.map +1 -1
  11. package/dest/pxe_service/error_enriching.js +5 -8
  12. package/dest/pxe_service/pxe_service.d.ts.map +1 -1
  13. package/dest/pxe_service/pxe_service.js +8 -4
  14. package/dest/storage/contract_data_provider/contract_data_provider.d.ts +10 -44
  15. package/dest/storage/contract_data_provider/contract_data_provider.d.ts.map +1 -1
  16. package/dest/storage/contract_data_provider/contract_data_provider.js +92 -72
  17. package/dest/storage/contract_data_provider/index.d.ts +0 -1
  18. package/dest/storage/contract_data_provider/index.d.ts.map +1 -1
  19. package/dest/storage/contract_data_provider/index.js +0 -1
  20. package/dest/storage/contract_data_provider/private_functions_tree.d.ts +3 -42
  21. package/dest/storage/contract_data_provider/private_functions_tree.d.ts.map +1 -1
  22. package/dest/storage/contract_data_provider/private_functions_tree.js +9 -61
  23. package/dest/test/pxe_test_suite.d.ts.map +1 -1
  24. package/dest/test/pxe_test_suite.js +1 -1
  25. package/package.json +15 -15
  26. package/src/config/index.ts +1 -1
  27. package/src/private_kernel/private_kernel_execution_prover.ts +4 -20
  28. package/src/private_kernel/private_kernel_oracle_impl.ts +13 -1
  29. package/src/pxe_oracle_interface/pxe_oracle_interface.ts +6 -0
  30. package/src/pxe_service/error_enriching.ts +8 -14
  31. package/src/pxe_service/pxe_service.ts +10 -4
  32. package/src/storage/contract_data_provider/contract_data_provider.ts +110 -80
  33. package/src/storage/contract_data_provider/index.ts +0 -1
  34. package/src/storage/contract_data_provider/private_functions_tree.ts +11 -75
  35. package/src/test/pxe_test_suite.ts +3 -1
@@ -1,8 +1,7 @@
1
1
  import { toArray } from '@aztec/foundation/iterable';
2
- import { ContractClassNotFoundError, ContractNotFoundError } from '@aztec/simulator/client';
3
2
  import { FunctionSelector, FunctionType, contractArtifactFromBuffer, contractArtifactToBuffer, getFunctionDebugMetadata } from '@aztec/stdlib/abi';
4
3
  import { AztecAddress } from '@aztec/stdlib/aztec-address';
5
- import { SerializableContractInstance } from '@aztec/stdlib/contract';
4
+ import { SerializableContractInstance, getContractClassFromArtifact } from '@aztec/stdlib/contract';
6
5
  import { PrivateFunctionsTree } from './private_functions_tree.js';
7
6
  /**
8
7
  * ContractDataProvider serves as a data manager and retriever for Aztec.nr contracts.
@@ -11,7 +10,9 @@ import { PrivateFunctionsTree } from './private_functions_tree.js';
11
10
  * to efficiently serve the requested data. It interacts with the ContractDatabase and AztecNode to fetch
12
11
  * the required information and facilitate cryptographic proof generation.
13
12
  */ export class ContractDataProvider {
14
- /** Map from contract class id to private function tree. */ contractClassesCache = new Map();
13
+ /** 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
+ #privateFunctionTrees = new Map();
15
+ /** Map from contract address to contract class id */ #contractClassIdMap = new Map();
15
16
  #contractArtifacts;
16
17
  #contractInstances;
17
18
  constructor(store){
@@ -28,17 +29,19 @@ import { PrivateFunctionsTree } from './private_functions_tree.js';
28
29
  await this.#contractArtifacts.set(id.toString(), contractArtifactToBuffer(contract));
29
30
  }
30
31
  async addContractInstance(contract) {
32
+ this.#contractClassIdMap.set(contract.address.toString(), contract.currentContractClassId);
31
33
  await this.#contractInstances.set(contract.address.toString(), new SerializableContractInstance(contract).toBuffer());
32
34
  }
33
35
  // Private getters
34
- async #getContractInstance(address) {
35
- const contract = await this.#contractInstances.getAsync(address.toString());
36
- return contract && SerializableContractInstance.fromBuffer(contract).withAddress(address);
37
- }
38
- async #getContractArtifact(id) {
39
- const contract = await this.#contractArtifacts.getAsync(id.toString());
40
- // TODO(@spalladino): AztecAsyncMap lies and returns Uint8Arrays instead of Buffers, hence the extra Buffer.from.
41
- return contract && contractArtifactFromBuffer(Buffer.from(contract));
36
+ async #getContractClassId(contractAddress) {
37
+ if (!this.#contractClassIdMap.has(contractAddress.toString())) {
38
+ const instance = await this.getContractInstance(contractAddress);
39
+ if (!instance) {
40
+ return;
41
+ }
42
+ this.#contractClassIdMap.set(contractAddress.toString(), instance.currentContractClassId);
43
+ }
44
+ return this.#contractClassIdMap.get(contractAddress.toString());
42
45
  }
43
46
  /**
44
47
  * Retrieve or create a ContractTree instance based on the provided class id.
@@ -49,29 +52,20 @@ import { PrivateFunctionsTree } from './private_functions_tree.js';
49
52
  * @param classId - The class id of the contract for which the ContractTree is required.
50
53
  * @returns A ContractTree instance associated with the specified contract address.
51
54
  * @throws An Error if the contract is not found in the ContractDatabase.
52
- */ async getTreeForClassId(classId) {
53
- if (!this.contractClassesCache.has(classId.toString())) {
54
- const artifact = await this.#getContractArtifact(classId);
55
+ */ async #getPrivateFunctionTreeForClassId(classId) {
56
+ if (!this.#privateFunctionTrees.has(classId.toString())) {
57
+ const artifact = await this.getContractArtifact(classId);
55
58
  if (!artifact) {
56
- throw new ContractClassNotFoundError(classId.toString());
59
+ return;
57
60
  }
58
61
  const tree = await PrivateFunctionsTree.create(artifact);
59
- this.contractClassesCache.set(classId.toString(), tree);
62
+ this.#privateFunctionTrees.set(classId.toString(), tree);
60
63
  }
61
- return this.contractClassesCache.get(classId.toString());
64
+ return this.#privateFunctionTrees.get(classId.toString());
62
65
  }
63
- /**
64
- * Retrieve or create a ContractTree instance based on the provided AztecAddress.
65
- * If an existing tree with the same contract address is found in the cache, it will be returned.
66
- * Otherwise, a new ContractTree instance will be created using the contract data from the database
67
- * and added to the cache before returning.
68
- *
69
- * @param contractAddress - The AztecAddress of the contract for which the ContractTree is required.
70
- * @returns A ContractTree instance associated with the specified contract address.
71
- * @throws An Error if the contract is not found in the ContractDatabase.
72
- */ async getTreeForAddress(contractAddress) {
73
- const instance = await this.getContractInstance(contractAddress);
74
- return this.getTreeForClassId(instance.currentContractClassId);
66
+ async #getContractArtifactByAddress(contractAddress) {
67
+ const contractClassId = await this.#getContractClassId(contractAddress);
68
+ return contractClassId && this.getContractArtifact(contractClassId);
75
69
  }
76
70
  // Public getters
77
71
  async getContractsAddresses() {
@@ -79,25 +73,26 @@ import { PrivateFunctionsTree } from './private_functions_tree.js';
79
73
  return keys.map(AztecAddress.fromString);
80
74
  }
81
75
  /** Returns a contract instance for a given address. Throws if not found. */ async getContractInstance(contractAddress) {
82
- const instance = await this.#getContractInstance(contractAddress);
83
- if (!instance) {
84
- throw new ContractNotFoundError(contractAddress.toString());
85
- }
86
- return instance;
76
+ const contract = await this.#contractInstances.getAsync(contractAddress.toString());
77
+ return contract && SerializableContractInstance.fromBuffer(contract).withAddress(contractAddress);
87
78
  }
88
79
  async getContractArtifact(contractClassId) {
89
- const tree = await this.getTreeForClassId(contractClassId);
90
- return tree.getArtifact();
80
+ const contract = await this.#contractArtifacts.getAsync(contractClassId.toString());
81
+ // TODO(@spalladino): AztecAsyncMap lies and returns Uint8Arrays instead of Buffers, hence the extra Buffer.from.
82
+ return contract && contractArtifactFromBuffer(Buffer.from(contract));
91
83
  }
92
84
  /** Returns a contract class for a given class id. Throws if not found. */ async getContractClass(contractClassId) {
93
- const tree = await this.getTreeForClassId(contractClassId);
94
- return tree.getContractClass();
85
+ const artifact = await this.getContractArtifact(contractClassId);
86
+ return artifact && getContractClassFromArtifact(artifact);
95
87
  }
96
88
  async getContract(address) {
97
89
  const instance = await this.getContractInstance(address);
98
- const artifact = instance && await this.getContractArtifact(instance?.currentContractClassId);
99
- if (!instance || !artifact) {
100
- return undefined;
90
+ if (!instance) {
91
+ return;
92
+ }
93
+ const artifact = await this.getContractArtifact(instance.currentContractClassId);
94
+ if (!artifact) {
95
+ return;
101
96
  }
102
97
  return {
103
98
  ...instance,
@@ -113,12 +108,19 @@ import { PrivateFunctionsTree } from './private_functions_tree.js';
113
108
  * @param selector - The function selector.
114
109
  * @returns The corresponding function's artifact as an object.
115
110
  */ async getFunctionArtifact(contractAddress, selector) {
116
- const tree = await this.getTreeForAddress(contractAddress);
117
- const contractArtifact = tree.getArtifact();
118
- const functionArtifact = await tree.getFunctionArtifact(selector);
119
- return {
120
- ...functionArtifact,
121
- contractName: contractArtifact.name
111
+ const artifact = await this.#getContractArtifactByAddress(contractAddress);
112
+ const fnArtifact = artifact && await this.#findFunctionArtifactBySelector(artifact, selector);
113
+ return fnArtifact && {
114
+ ...fnArtifact,
115
+ contractName: artifact.name
116
+ };
117
+ }
118
+ async getPublicFunctionArtifact(contractAddress) {
119
+ const artifact = await this.#getContractArtifactByAddress(contractAddress);
120
+ const fnArtifact = artifact && artifact.functions.find((fn)=>fn.functionType === FunctionType.PUBLIC);
121
+ return fnArtifact && {
122
+ ...fnArtifact,
123
+ contractName: artifact.name
122
124
  };
123
125
  }
124
126
  /**
@@ -130,8 +132,12 @@ import { PrivateFunctionsTree } from './private_functions_tree.js';
130
132
  * @param functionName - The name of the function.
131
133
  * @returns The corresponding function's artifact as an object
132
134
  */ async getFunctionArtifactByName(contractAddress, functionName) {
133
- const tree = await this.getTreeForAddress(contractAddress);
134
- return tree.getArtifact().functions.find((f)=>f.name === functionName);
135
+ const artifact = await this.#getContractArtifactByAddress(contractAddress);
136
+ return artifact?.functions.find((fn)=>fn.name === functionName);
137
+ }
138
+ async getFunctionAbi(contractAddress, selector) {
139
+ const artifact = await this.#getContractArtifactByAddress(contractAddress);
140
+ return artifact && await this.#findFunctionAbiBySelector(artifact, selector);
135
141
  }
136
142
  /**
137
143
  * Retrieves the debug metadata of a specified function within a given contract.
@@ -143,22 +149,14 @@ import { PrivateFunctionsTree } from './private_functions_tree.js';
143
149
  * @param selector - The function selector.
144
150
  * @returns The corresponding function's artifact as an object.
145
151
  */ async getFunctionDebugMetadata(contractAddress, selector) {
146
- const tree = await this.getTreeForAddress(contractAddress);
147
- const artifact = await tree.getFunctionArtifact(selector);
148
- return getFunctionDebugMetadata(tree.getArtifact(), artifact);
152
+ const artifact = await this.#getContractArtifactByAddress(contractAddress);
153
+ const fnArtifact = artifact && await this.#findFunctionArtifactBySelector(artifact, selector);
154
+ return fnArtifact && getFunctionDebugMetadata(artifact, fnArtifact);
149
155
  }
150
- /**
151
- * Retrieve the bytecode of a specific function in a contract at the given address.
152
- * The returned bytecode is required for executing and verifying the function's behavior
153
- * in the Aztec network. Throws an error if the contract or function cannot be found.
154
- *
155
- * @param contractAddress - The contract's address.
156
- * @param selector - The function selector.
157
- * @returns A Promise that resolves to a Buffer containing the bytecode of the specified function.
158
- * @throws Error if the contract address is unknown or not found.
159
- */ async getBytecode(contractAddress, selector) {
160
- const tree = await this.getTreeForAddress(contractAddress);
161
- return tree.getBytecode(selector);
156
+ async getPublicFunctionDebugMetadata(contractAddress) {
157
+ const artifact = await this.#getContractArtifactByAddress(contractAddress);
158
+ const fnArtifact = artifact && artifact.functions.find((fn)=>fn.functionType === FunctionType.PUBLIC);
159
+ return fnArtifact && getFunctionDebugMetadata(artifact, fnArtifact);
162
160
  }
163
161
  /**
164
162
  * Retrieve the function membership witness for the given contract class and function selector.
@@ -169,20 +167,42 @@ import { PrivateFunctionsTree } from './private_functions_tree.js';
169
167
  * @param selector - The function selector.
170
168
  * @returns A promise that resolves with the MembershipWitness instance for the specified contract's function.
171
169
  */ async getFunctionMembershipWitness(contractClassId, selector) {
172
- const tree = await this.getTreeForClassId(contractClassId);
173
- return tree.getFunctionMembershipWitness(selector);
170
+ const tree = await this.#getPrivateFunctionTreeForClassId(contractClassId);
171
+ return tree?.getFunctionMembershipWitness(selector);
174
172
  }
175
173
  async getDebugContractName(contractAddress) {
176
- const tree = await this.getTreeForAddress(contractAddress);
177
- return tree.getArtifact().name;
174
+ const artifact = await this.#getContractArtifactByAddress(contractAddress);
175
+ return artifact?.name;
178
176
  }
179
177
  async getDebugFunctionName(contractAddress, selector) {
180
- const tree = await this.getTreeForAddress(contractAddress);
181
- const { name: contractName } = tree.getArtifact();
182
- const { name: functionName } = await tree.getFunctionArtifact(selector);
183
- return `${contractName}:${functionName}`;
178
+ const artifact = await this.#getContractArtifactByAddress(contractAddress);
179
+ const fnArtifact = artifact && await this.#findFunctionAbiBySelector(artifact, selector);
180
+ return `${artifact?.name ?? contractAddress}:${fnArtifact?.name ?? selector}`;
184
181
  }
185
182
  async getSize() {
186
183
  return (await toArray(this.#contractInstances.valuesAsync())).concat(await toArray(this.#contractArtifacts.valuesAsync())).reduce((sum, value)=>sum + value.length, 0);
187
184
  }
185
+ async #findFunctionArtifactBySelector(artifact, selector) {
186
+ const functions = artifact.functions;
187
+ for(let i = 0; i < functions.length; i++){
188
+ const fn = functions[i];
189
+ const fnSelector = await FunctionSelector.fromNameAndParameters(fn.name, fn.parameters);
190
+ if (fnSelector.equals(selector)) {
191
+ return fn;
192
+ }
193
+ }
194
+ }
195
+ async #findFunctionAbiBySelector(artifact, selector) {
196
+ const functions = [
197
+ ...artifact.functions,
198
+ ...artifact.nonDispatchPublicFunctions ?? []
199
+ ];
200
+ for(let i = 0; i < functions.length; i++){
201
+ const fn = functions[i];
202
+ const fnSelector = await FunctionSelector.fromNameAndParameters(fn.name, fn.parameters);
203
+ if (fnSelector.equals(selector)) {
204
+ return fn;
205
+ }
206
+ }
207
+ }
188
208
  }
@@ -1,3 +1,2 @@
1
1
  export { ContractDataProvider } from './contract_data_provider.js';
2
- export { PrivateFunctionsTree } from './private_functions_tree.js';
3
2
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/storage/contract_data_provider/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AACnE,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/storage/contract_data_provider/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC"}
@@ -1,2 +1 @@
1
1
  export { ContractDataProvider } from './contract_data_provider.js';
2
- export { PrivateFunctionsTree } from './private_functions_tree.js';
@@ -1,10 +1,6 @@
1
- /// <reference types="node" resolution-mode="require"/>
2
- /// <reference types="node" resolution-mode="require"/>
3
1
  import { FUNCTION_TREE_HEIGHT } from '@aztec/constants';
4
- import { Fr } from '@aztec/foundation/fields';
5
- import { MembershipWitness, type MerkleTree } from '@aztec/foundation/trees';
6
- import { type ContractArtifact, type FunctionArtifact, FunctionSelector } from '@aztec/stdlib/abi';
7
- import { type ContractClassWithId } from '@aztec/stdlib/contract';
2
+ import { MembershipWitness } from '@aztec/foundation/trees';
3
+ import { type ContractArtifact, FunctionSelector } from '@aztec/stdlib/abi';
8
4
  /**
9
5
  * Represents a Merkle tree of functions for a particular Contract Class.
10
6
  * It manages the construction of the function tree, computes its root, and generates membership witnesses
@@ -12,45 +8,10 @@ import { type ContractClassWithId } from '@aztec/stdlib/contract';
12
8
  * It is used in combination with the AztecNode to compute various data for executing private transactions.
13
9
  */
14
10
  export declare class PrivateFunctionsTree {
15
- private readonly artifact;
16
- private contractClass;
11
+ private readonly privateFunctions;
17
12
  private tree?;
18
13
  private constructor();
19
14
  static create(artifact: ContractArtifact): Promise<PrivateFunctionsTree>;
20
- /**
21
- * Retrieve the artifact of a given function.
22
- * The function is identified by its selector, which represents a unique identifier for the function's signature.
23
- * Throws an error if the function with the provided selector is not found in the contract.
24
- *
25
- * @param selector - The function selector.
26
- * @returns The artifact object containing relevant information about the targeted function.
27
- */
28
- getFunctionArtifact(selector: FunctionSelector): Promise<FunctionArtifact>;
29
- /**
30
- * Retrieve the bytecode of a function in the contract by its function selector.
31
- * The function selector is a unique identifier for each function in a contract.
32
- * Throws an error if the function with the given selector is not found in the contract.
33
- *
34
- * @param selector - The selector of a function to get bytecode for.
35
- * @returns The bytecode of the function as a string.
36
- */
37
- getBytecode(selector: FunctionSelector): Promise<Buffer>;
38
- /**
39
- * Calculate and return the root of the function tree for the current contract.
40
- * This root is a cryptographic commitment to the set of constrained functions within the contract,
41
- * which is used in the Aztec node's proof system. The root will be cached after the first call.
42
- *
43
- * @returns A promise that resolves to the Fr (finite field element) representation of the function tree root.
44
- */
45
- getFunctionTreeRoot(): Promise<MerkleTree>;
46
- /** Returns the contract class object. */
47
- getContractClass(): ContractClassWithId;
48
- /** Returns the contract artifact. */
49
- getArtifact(): ContractArtifact;
50
- /**
51
- * Returns the contract class identifier for the given artifact.
52
- */
53
- getContractClassId(): Fr;
54
15
  /**
55
16
  * Retrieve the membership witness of a function within a contract's function tree.
56
17
  * A membership witness represents the position and authentication path of a target function
@@ -1 +1 @@
1
- {"version":3,"file":"private_functions_tree.d.ts","sourceRoot":"","sources":["../../../src/storage/contract_data_provider/private_functions_tree.ts"],"names":[],"mappings":";;AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AACxD,OAAO,EAAE,EAAE,EAAE,MAAM,0BAA0B,CAAC;AAE9C,OAAO,EAAE,iBAAiB,EAAE,KAAK,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAC7E,OAAO,EAAE,KAAK,gBAAgB,EAAE,KAAK,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AACnG,OAAO,EACL,KAAK,mBAAmB,EAIzB,MAAM,wBAAwB,CAAC;AAEhC;;;;;GAKG;AACH,qBAAa,oBAAoB;IAGX,OAAO,CAAC,QAAQ,CAAC,QAAQ;IAAoB,OAAO,CAAC,aAAa;IAFtF,OAAO,CAAC,IAAI,CAAC,CAAa;IAE1B,OAAO;WAEM,MAAM,CAAC,QAAQ,EAAE,gBAAgB;IAK9C;;;;;;;OAOG;IACU,mBAAmB,CAAC,QAAQ,EAAE,gBAAgB,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAkBvF;;;;;;;OAOG;IACU,WAAW,CAAC,QAAQ,EAAE,gBAAgB;IAKnD;;;;;;OAMG;IACI,mBAAmB;IAI1B,yCAAyC;IAClC,gBAAgB;IAIvB,qCAAqC;IAC9B,WAAW;IAIlB;;OAEG;IACI,kBAAkB;IAIzB;;;;;;;;OAQG;IACU,4BAA4B,CACvC,QAAQ,EAAE,gBAAgB,GACzB,OAAO,CAAC,iBAAiB,CAAC,OAAO,oBAAoB,CAAC,CAAC;YAiB5C,OAAO;CAOtB"}
1
+ {"version":3,"file":"private_functions_tree.d.ts","sourceRoot":"","sources":["../../../src/storage/contract_data_provider/private_functions_tree.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AAGxD,OAAO,EAAE,iBAAiB,EAAmB,MAAM,yBAAyB,CAAC;AAC7E,OAAO,EAAE,KAAK,gBAAgB,EAAE,gBAAgB,EAAgB,MAAM,mBAAmB,CAAC;AAQ1F;;;;;GAKG;AACH,qBAAa,oBAAoB;IAGX,OAAO,CAAC,QAAQ,CAAC,gBAAgB;IAFrD,OAAO,CAAC,IAAI,CAAC,CAAa;IAE1B,OAAO;WAEM,MAAM,CAAC,QAAQ,EAAE,gBAAgB;IAS9C;;;;;;;;OAQG;IACU,4BAA4B,CACvC,QAAQ,EAAE,gBAAgB,GACzB,OAAO,CAAC,iBAAiB,CAAC,OAAO,oBAAoB,CAAC,CAAC;YAiB5C,OAAO;CAMtB"}
@@ -2,73 +2,22 @@ import { FUNCTION_TREE_HEIGHT } from '@aztec/constants';
2
2
  import { Fr } from '@aztec/foundation/fields';
3
3
  import { assertLength } from '@aztec/foundation/serialize';
4
4
  import { MembershipWitness } from '@aztec/foundation/trees';
5
- import { FunctionSelector } from '@aztec/stdlib/abi';
6
- import { computePrivateFunctionLeaf, computePrivateFunctionsTree, getContractClassFromArtifact } from '@aztec/stdlib/contract';
5
+ import { FunctionType } from '@aztec/stdlib/abi';
6
+ import { computePrivateFunctionLeaf, computePrivateFunctionsTree, getContractClassPrivateFunctionFromArtifact } from '@aztec/stdlib/contract';
7
7
  /**
8
8
  * Represents a Merkle tree of functions for a particular Contract Class.
9
9
  * It manages the construction of the function tree, computes its root, and generates membership witnesses
10
10
  * for constrained functions. This class also enables lookup of specific function artifact using selectors.
11
11
  * It is used in combination with the AztecNode to compute various data for executing private transactions.
12
12
  */ export class PrivateFunctionsTree {
13
- artifact;
14
- contractClass;
13
+ privateFunctions;
15
14
  tree;
16
- constructor(artifact, contractClass){
17
- this.artifact = artifact;
18
- this.contractClass = contractClass;
15
+ constructor(privateFunctions){
16
+ this.privateFunctions = privateFunctions;
19
17
  }
20
18
  static async create(artifact) {
21
- const contractClass = await getContractClassFromArtifact(artifact);
22
- return new PrivateFunctionsTree(artifact, contractClass);
23
- }
24
- /**
25
- * Retrieve the artifact of a given function.
26
- * The function is identified by its selector, which represents a unique identifier for the function's signature.
27
- * Throws an error if the function with the provided selector is not found in the contract.
28
- *
29
- * @param selector - The function selector.
30
- * @returns The artifact object containing relevant information about the targeted function.
31
- */ async getFunctionArtifact(selector) {
32
- const functionsAndSelectors = await Promise.all(this.artifact.functions.map(async (f)=>({
33
- f,
34
- selector: await FunctionSelector.fromNameAndParameters(f.name, f.parameters)
35
- })));
36
- const artifact = functionsAndSelectors.find((f)=>selector.equals(f.selector))?.f;
37
- if (!artifact) {
38
- throw new Error(`Unknown function. Selector ${selector.toString()} not found in the artifact ${this.artifact.name} with class ${this.getContractClassId().toString()}.`);
39
- }
40
- return artifact;
41
- }
42
- /**
43
- * Retrieve the bytecode of a function in the contract by its function selector.
44
- * The function selector is a unique identifier for each function in a contract.
45
- * Throws an error if the function with the given selector is not found in the contract.
46
- *
47
- * @param selector - The selector of a function to get bytecode for.
48
- * @returns The bytecode of the function as a string.
49
- */ async getBytecode(selector) {
50
- const artifact = await this.getFunctionArtifact(selector);
51
- return artifact.bytecode;
52
- }
53
- /**
54
- * Calculate and return the root of the function tree for the current contract.
55
- * This root is a cryptographic commitment to the set of constrained functions within the contract,
56
- * which is used in the Aztec node's proof system. The root will be cached after the first call.
57
- *
58
- * @returns A promise that resolves to the Fr (finite field element) representation of the function tree root.
59
- */ getFunctionTreeRoot() {
60
- return this.getTree();
61
- }
62
- /** Returns the contract class object. */ getContractClass() {
63
- return this.contractClass;
64
- }
65
- /** Returns the contract artifact. */ getArtifact() {
66
- return this.artifact;
67
- }
68
- /**
69
- * Returns the contract class identifier for the given artifact.
70
- */ getContractClassId() {
71
- return this.getContractClass().id;
19
+ const privateFunctions = await Promise.all(artifact.functions.filter((fn)=>fn.functionType === FunctionType.PRIVATE).map(getContractClassPrivateFunctionFromArtifact));
20
+ return new PrivateFunctionsTree(privateFunctions);
72
21
  }
73
22
  /**
74
23
  * Retrieve the membership witness of a function within a contract's function tree.
@@ -79,7 +28,7 @@ import { computePrivateFunctionLeaf, computePrivateFunctionsTree, getContractCla
79
28
  * @param selector - The function selector.
80
29
  * @returns A MembershipWitness instance representing the position and authentication path of the function in the function tree.
81
30
  */ async getFunctionMembershipWitness(selector) {
82
- const fn = this.getContractClass().privateFunctions.find((f)=>f.selector.equals(selector));
31
+ const fn = this.privateFunctions.find((f)=>f.selector.equals(selector));
83
32
  if (!fn) {
84
33
  throw new Error(`Private function with selector ${selector.toString()} not found in contract class.`);
85
34
  }
@@ -91,8 +40,7 @@ import { computePrivateFunctionLeaf, computePrivateFunctionsTree, getContractCla
91
40
  }
92
41
  async getTree() {
93
42
  if (!this.tree) {
94
- const fns = this.getContractClass().privateFunctions;
95
- this.tree = await computePrivateFunctionsTree(fns);
43
+ this.tree = await computePrivateFunctionsTree(this.privateFunctions);
96
44
  }
97
45
  return this.tree;
98
46
  }
@@ -1 +1 @@
1
- {"version":3,"file":"pxe_test_suite.d.ts","sourceRoot":"","sources":["../../src/test/pxe_test_suite.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,iCAAiC,CAAC;AAS3D,eAAO,MAAM,YAAY,aAAc,MAAM,YAAY,MAAM,QAAQ,GAAG,CAAC,SAiG1E,CAAC"}
1
+ {"version":3,"file":"pxe_test_suite.d.ts","sourceRoot":"","sources":["../../src/test/pxe_test_suite.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,iCAAiC,CAAC;AAS3D,eAAO,MAAM,YAAY,aAAc,MAAM,YAAY,MAAM,QAAQ,GAAG,CAAC,SAmG1E,CAAC"}
@@ -69,7 +69,7 @@ export const pxeTestSuite = (testName, pxeSetup)=>{
69
69
  const instance = await randomContractInstanceWithAddress();
70
70
  await expect(pxe.registerContract({
71
71
  instance
72
- })).rejects.toThrow(/DB has no contract class with id/i);
72
+ })).rejects.toThrow(/Artifact not found when registering an instance/);
73
73
  });
74
74
  it('refuses to register a contract with an artifact with mismatching class id', async ()=>{
75
75
  const artifact = randomContractArtifact();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aztec/pxe",
3
- "version": "0.82.0",
3
+ "version": "0.82.1",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  "./server": "./dest/entrypoints/server/index.js",
@@ -59,19 +59,19 @@
59
59
  ]
60
60
  },
61
61
  "dependencies": {
62
- "@aztec/bb-prover": "0.82.0",
63
- "@aztec/bb.js": "0.82.0",
64
- "@aztec/builder": "0.82.0",
65
- "@aztec/constants": "0.82.0",
66
- "@aztec/ethereum": "0.82.0",
67
- "@aztec/foundation": "0.82.0",
68
- "@aztec/key-store": "0.82.0",
69
- "@aztec/kv-store": "0.82.0",
70
- "@aztec/noir-protocol-circuits-types": "0.82.0",
71
- "@aztec/noir-types": "0.82.0",
72
- "@aztec/protocol-contracts": "0.82.0",
73
- "@aztec/simulator": "0.82.0",
74
- "@aztec/stdlib": "0.82.0",
62
+ "@aztec/bb-prover": "0.82.1",
63
+ "@aztec/bb.js": "0.82.1",
64
+ "@aztec/builder": "0.82.1",
65
+ "@aztec/constants": "0.82.1",
66
+ "@aztec/ethereum": "0.82.1",
67
+ "@aztec/foundation": "0.82.1",
68
+ "@aztec/key-store": "0.82.1",
69
+ "@aztec/kv-store": "0.82.1",
70
+ "@aztec/noir-protocol-circuits-types": "0.82.1",
71
+ "@aztec/noir-types": "0.82.1",
72
+ "@aztec/protocol-contracts": "0.82.1",
73
+ "@aztec/simulator": "0.82.1",
74
+ "@aztec/stdlib": "0.82.1",
75
75
  "@msgpack/msgpack": "^3.0.0-beta2",
76
76
  "koa": "^2.14.2",
77
77
  "koa-router": "^12.0.0",
@@ -81,7 +81,7 @@
81
81
  "viem": "2.23.7"
82
82
  },
83
83
  "devDependencies": {
84
- "@aztec/noir-contracts.js": "0.82.0",
84
+ "@aztec/noir-contracts.js": "0.82.1",
85
85
  "@jest/globals": "^29.5.0",
86
86
  "@types/jest": "^29.5.0",
87
87
  "@types/lodash.omit": "^4.5.7",
@@ -71,7 +71,7 @@ export const pxeConfigMappings: ConfigMappingsType<PXEServiceConfig> = {
71
71
  proverEnabled: {
72
72
  env: 'PXE_PROVER_ENABLED',
73
73
  description: 'Enable real proofs',
74
- ...booleanConfigHelper(),
74
+ ...booleanConfigHelper(true),
75
75
  },
76
76
  };
77
77
 
@@ -29,10 +29,8 @@ import {
29
29
  type PrivateCallExecutionResult,
30
30
  type PrivateExecutionResult,
31
31
  TxRequest,
32
- collectEnqueuedPublicFunctionCalls,
33
32
  collectNoteHashLeafIndexMap,
34
33
  collectNoteHashNullifierCounterMap,
35
- collectPublicTeardownFunctionCall,
36
34
  getFinalMinRevertibleSideEffectCounter,
37
35
  } from '@aztec/stdlib/tx';
38
36
  import { VerificationKeyAsFields } from '@aztec/stdlib/vks';
@@ -93,7 +91,7 @@ export class PrivateKernelExecutionProver {
93
91
 
94
92
  const timer = new Timer();
95
93
 
96
- const isPrivateOnlyTx = this.isPrivateOnly(executionResult);
94
+ const isPrivateOnlyTx = executionResult.publicFunctionCalldata.length === 0;
97
95
 
98
96
  const executionStack = [executionResult.entrypoint];
99
97
  let firstIteration = true;
@@ -104,10 +102,9 @@ export class PrivateKernelExecutionProver {
104
102
 
105
103
  const noteHashLeafIndexMap = collectNoteHashLeafIndexMap(executionResult);
106
104
  const noteHashNullifierCounterMap = collectNoteHashNullifierCounterMap(executionResult);
107
- const enqueuedPublicFunctions = collectEnqueuedPublicFunctionCalls(executionResult);
108
- const hasPublicCalls =
109
- enqueuedPublicFunctions.length > 0 || !collectPublicTeardownFunctionCall(executionResult).isEmpty();
110
- const validationRequestsSplitCounter = hasPublicCalls ? getFinalMinRevertibleSideEffectCounter(executionResult) : 0;
105
+ const validationRequestsSplitCounter = isPrivateOnlyTx
106
+ ? 0
107
+ : getFinalMinRevertibleSideEffectCounter(executionResult);
111
108
 
112
109
  while (executionStack.length) {
113
110
  if (!firstIteration) {
@@ -339,17 +336,4 @@ export class PrivateKernelExecutionProver {
339
336
  }),
340
337
  });
341
338
  }
342
-
343
- private isPrivateOnly(executionResult: PrivateExecutionResult): boolean {
344
- const isPrivateOnlyRecursive = (callResult: PrivateCallExecutionResult): boolean => {
345
- const makesPublicCalls =
346
- callResult.enqueuedPublicFunctionCalls.some(enqueuedCall => !enqueuedCall.isEmpty()) ||
347
- !callResult.publicTeardownFunctionCall.isEmpty();
348
- return (
349
- !makesPublicCalls &&
350
- callResult.nestedExecutions.every(nestedExecution => isPrivateOnlyRecursive(nestedExecution))
351
- );
352
- };
353
- return isPrivateOnlyRecursive(executionResult.entrypoint);
354
- }
355
339
  }
@@ -37,6 +37,9 @@ export class PrivateKernelOracleImpl implements PrivateKernelOracle {
37
37
 
38
38
  public async getContractAddressPreimage(address: AztecAddress) {
39
39
  const instance = await this.contractDataProvider.getContractInstance(address);
40
+ if (!instance) {
41
+ throw new Error(`Contract instance not found when getting address preimage. Contract address: ${address}.`);
42
+ }
40
43
  return {
41
44
  saltedInitializationHash: await computeSaltedInitializationHash(instance),
42
45
  ...instance,
@@ -45,11 +48,20 @@ export class PrivateKernelOracleImpl implements PrivateKernelOracle {
45
48
 
46
49
  public async getContractClassIdPreimage(contractClassId: Fr) {
47
50
  const contractClass = await this.contractDataProvider.getContractClass(contractClassId);
51
+ if (!contractClass) {
52
+ throw new Error(`Contract class not found when getting class id preimage. Class id: ${contractClassId}.`);
53
+ }
48
54
  return computeContractClassIdPreimage(contractClass);
49
55
  }
50
56
 
51
57
  public async getFunctionMembershipWitness(contractClassId: Fr, selector: FunctionSelector) {
52
- return await this.contractDataProvider.getFunctionMembershipWitness(contractClassId, selector);
58
+ const membershipWitness = await this.contractDataProvider.getFunctionMembershipWitness(contractClassId, selector);
59
+ if (!membershipWitness) {
60
+ throw new Error(
61
+ `Membership witness not found for contract class id ${contractClassId} and selector ${selector}.`,
62
+ );
63
+ }
64
+ return membershipWitness;
53
65
  }
54
66
 
55
67
  public getVkMembershipWitness(vk: VerificationKeyAsFields) {
@@ -106,6 +106,9 @@ export class PXEOracleInterface implements ExecutionDataProvider {
106
106
  selector: FunctionSelector,
107
107
  ): Promise<FunctionArtifactWithContractName> {
108
108
  const artifact = await this.contractDataProvider.getFunctionArtifact(contractAddress, selector);
109
+ if (!artifact) {
110
+ throw new Error(`Function artifact not found for contract ${contractAddress} and selector ${selector}.`);
111
+ }
109
112
  const debug = await this.contractDataProvider.getFunctionDebugMetadata(contractAddress, selector);
110
113
  return {
111
114
  ...artifact,
@@ -118,6 +121,9 @@ export class PXEOracleInterface implements ExecutionDataProvider {
118
121
  functionName: string,
119
122
  ): Promise<FunctionArtifactWithContractName | undefined> {
120
123
  const instance = await this.contractDataProvider.getContractInstance(contractAddress);
124
+ if (!instance) {
125
+ return;
126
+ }
121
127
  const artifact = await this.contractDataProvider.getContractArtifact(instance.currentContractClassId);
122
128
  return artifact && getFunctionArtifact(artifact, functionName);
123
129
  }