Skip to content

Commit afac202

Browse files
committed
chore: minor simulator utils cleanup
1 parent de330ef commit afac202

File tree

4 files changed

+45
-91
lines changed

4 files changed

+45
-91
lines changed

yarn-project/pxe/src/pxe_service/pxe_service.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -343,27 +343,21 @@ export class PXEService implements PXE {
343343
}
344344

345345
/**
346-
* Simulate an unconstrained transaction on the given contract, without considering constraints set by ACIR.
346+
* Simulate an unconstrained function call on the given contract, without considering constraints set by ACIR.
347347
* The simulation parameters are fetched using ContractDataProvider and executed using AcirSimulator.
348348
* Returns the simulation result containing the outputs of the unconstrained function.
349349
*
350-
* @param execRequest - The transaction request object containing the target contract and function data.
351-
* @param scopes - The accounts whose notes we can access in this call. Currently optional and will default to all.
350+
* @param call - The function call to execute.
351+
* @param authWitnesses - Authentication witnesses required for the function call.
352+
* @param scopes - Optional array of account addresses whose notes can be accessed in this call. Defaults to all
353+
* accounts if not specified.
352354
* @returns The simulation result containing the outputs of the unconstrained function.
353355
*/
354-
async #simulateUnconstrained(execRequest: FunctionCall, authWitnesses?: AuthWitness[], scopes?: AztecAddress[]) {
355-
const { to: contractAddress, selector: functionSelector } = execRequest;
356-
356+
async #simulateUnconstrained(call: FunctionCall, authWitnesses?: AuthWitness[], scopes?: AztecAddress[]) {
357357
this.log.debug('Executing unconstrained simulator...');
358358
try {
359-
const result = await this.simulator.runUnconstrained(
360-
execRequest,
361-
contractAddress,
362-
functionSelector,
363-
authWitnesses ?? [],
364-
scopes,
365-
);
366-
this.log.verbose(`Unconstrained simulation for ${contractAddress}.${functionSelector} completed`);
359+
const result = await this.simulator.runUnconstrained(call, authWitnesses ?? [], scopes);
360+
this.log.verbose(`Unconstrained simulation for ${call.to}.${call.selector} completed`);
367361

368362
return result;
369363
} catch (err) {

yarn-project/simulator/src/private/simulator.ts

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import { Fr } from '@aztec/foundation/fields';
22
import { type Logger, createLogger } from '@aztec/foundation/log';
33
import type { AbiDecoded, FunctionCall } from '@aztec/stdlib/abi';
4-
import { FunctionSelector, FunctionType } from '@aztec/stdlib/abi';
4+
import { FunctionSelector, FunctionType, decodeFromAbi } from '@aztec/stdlib/abi';
55
import type { AuthWitness } from '@aztec/stdlib/auth-witness';
66
import { AztecAddress } from '@aztec/stdlib/aztec-address';
77
import { 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';
1011
import type { ExecutionDataProvider } from './execution_data_provider.js';
1112
import { ExecutionNoteCache } from './execution_note_cache.js';
1213
import { HashedValuesCache } from './hashed_values_cache.js';
1314
import { executePrivateFunction, verifyCurrentClassId } from './private_execution.js';
1415
import { PrivateExecutionOracle } from './private_execution_oracle.js';
1516
import type { SimulationProvider } from './providers/simulation_provider.js';
16-
import { executeUnconstrainedFunction } from './unconstrained_execution.js';
1717
import { 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
}

yarn-project/simulator/src/private/unconstrained_execution.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ describe('Unconstrained Execution test suite', () => {
9393
returnTypes: artifact.returnTypes,
9494
};
9595

96-
const result = await acirSimulator.runUnconstrained(execRequest, contractAddress, FunctionSelector.empty(), []);
96+
const result = await acirSimulator.runUnconstrained(execRequest, [], []);
9797

9898
expect(result).toEqual(9n);
9999
}, 30_000);

yarn-project/simulator/src/private/unconstrained_execution.ts

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)