11import { Fr } from '@aztec/foundation/fields' ;
22import { type Logger , createLogger } from '@aztec/foundation/log' ;
33import type { AbiDecoded , FunctionCall } from '@aztec/stdlib/abi' ;
4- import { FunctionSelector , FunctionType } from '@aztec/stdlib/abi' ;
4+ import { FunctionSelector , FunctionType , decodeFromAbi } from '@aztec/stdlib/abi' ;
55import type { AuthWitness } from '@aztec/stdlib/auth-witness' ;
66import { AztecAddress } from '@aztec/stdlib/aztec-address' ;
77import { CallContext , HashedValues , PrivateExecutionResult , TxExecutionRequest , collectNested } from '@aztec/stdlib/tx' ;
88
9- import { createSimulationError } from '../common/errors.js' ;
9+ import { ExecutionError , createSimulationError , resolveAssertionMessageFromError } from '../common/errors.js' ;
10+ import { Oracle , extractCallStack , toACVMWitness , witnessMapToFields } from './acvm/index.js' ;
1011import type { ExecutionDataProvider } from './execution_data_provider.js' ;
1112import { ExecutionNoteCache } from './execution_note_cache.js' ;
1213import { HashedValuesCache } from './hashed_values_cache.js' ;
1314import { executePrivateFunction , verifyCurrentClassId } from './private_execution.js' ;
1415import { PrivateExecutionOracle } from './private_execution_oracle.js' ;
1516import type { SimulationProvider } from './providers/simulation_provider.js' ;
16- import { executeUnconstrainedFunction } from './unconstrained_execution.js' ;
1717import { UnconstrainedExecutionOracle } from './unconstrained_execution_oracle.js' ;
1818
1919/**
@@ -117,27 +117,26 @@ export class AcirSimulator {
117117
118118 /**
119119 * Runs an unconstrained function.
120- * @param request - The transaction request.
121- * @param entryPointArtifact - The artifact of the entry point function.
122- * @param contractAddress - The address of the contract.
123- * @param scopes - The accounts whose notes we can access in this call. Currently optional and will default to all.
120+ * @param call - The function call to execute.
121+ * @param authwits - Authentication witnesses required for the function call.
122+ * @param scopes - Optional array of account addresses whose notes can be accessed in this call. Defaults to all
123+ * accounts if not specified.
124+ * @returns A decoded ABI value containing the function's return data.
124125 */
125126 public async runUnconstrained (
126- request : FunctionCall ,
127- contractAddress : AztecAddress ,
128- selector : FunctionSelector ,
127+ call : FunctionCall ,
129128 authwits : AuthWitness [ ] ,
130129 scopes ?: AztecAddress [ ] ,
131130 ) : Promise < AbiDecoded > {
132- await verifyCurrentClassId ( contractAddress , this . executionDataProvider ) ;
133- const entryPointArtifact = await this . executionDataProvider . getFunctionArtifact ( contractAddress , selector ) ;
131+ await verifyCurrentClassId ( call . to , this . executionDataProvider ) ;
132+ const entryPointArtifact = await this . executionDataProvider . getFunctionArtifact ( call . to , call . selector ) ;
134133
135134 if ( entryPointArtifact . functionType !== FunctionType . UNCONSTRAINED ) {
136135 throw new Error ( `Cannot run ${ entryPointArtifact . functionType } function as unconstrained` ) ;
137136 }
138137
139- const context = new UnconstrainedExecutionOracle (
140- contractAddress ,
138+ const oracle = new UnconstrainedExecutionOracle (
139+ call . to ,
141140 authwits ,
142141 [ ] ,
143142 this . executionDataProvider ,
@@ -146,14 +145,29 @@ export class AcirSimulator {
146145 ) ;
147146
148147 try {
149- return await executeUnconstrainedFunction (
150- this . simulationProvider ,
151- context ,
152- entryPointArtifact ,
153- contractAddress ,
154- request . selector ,
155- request . args ,
156- ) ;
148+ this . log . verbose ( `Executing unconstrained function ${ entryPointArtifact . name } ` , {
149+ contract : call . to ,
150+ selector : call . selector ,
151+ } ) ;
152+
153+ const initialWitness = toACVMWitness ( 0 , call . args ) ;
154+ const acirExecutionResult = await this . simulationProvider
155+ . executeUserCircuit ( initialWitness , entryPointArtifact , new Oracle ( oracle ) )
156+ . catch ( ( err : Error ) => {
157+ err . message = resolveAssertionMessageFromError ( err , entryPointArtifact ) ;
158+ throw new ExecutionError (
159+ err . message ,
160+ {
161+ contractAddress : call . to ,
162+ functionSelector : call . selector ,
163+ } ,
164+ extractCallStack ( err , entryPointArtifact . debug ) ,
165+ { cause : err } ,
166+ ) ;
167+ } ) ;
168+
169+ const returnWitness = witnessMapToFields ( acirExecutionResult . returnWitness ) ;
170+ return decodeFromAbi ( entryPointArtifact . returnTypes , returnWitness ) ;
157171 } catch ( err ) {
158172 throw createSimulationError ( err instanceof Error ? err : new Error ( 'Unknown error during private execution' ) ) ;
159173 }
0 commit comments