Skip to content

Commit f19f112

Browse files
author
dbanks12
committed
fix: dependency cycles in public simulator - errors/revertReason
1 parent 76cab3f commit f19f112

File tree

3 files changed

+58
-60
lines changed

3 files changed

+58
-60
lines changed

yarn-project/simulator/src/public/avm/avm_simulator.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,9 @@ import { AvmExecutionEnvironment } from './avm_execution_environment.js';
1414
import type { Gas } from './avm_gas.js';
1515
import { AvmMachineState } from './avm_machine_state.js';
1616
import type { AvmSimulatorInterface } from './avm_simulator_interface.js';
17-
import {
18-
AvmExecutionError,
19-
AvmRevertReason,
20-
InvalidProgramCounterError,
21-
revertReasonFromExceptionalHalt,
22-
revertReasonFromExplicitRevert,
23-
} from './errors.js';
17+
import { AvmExecutionError, AvmRevertReason, InvalidProgramCounterError } from './errors.js';
2418
import type { Instruction } from './opcodes/instruction.js';
19+
import { revertReasonFromExceptionalHalt, revertReasonFromExplicitRevert } from './revert_reason.js';
2520
import {
2621
INSTRUCTION_SET,
2722
type InstructionSet,

yarn-project/simulator/src/public/avm/errors.ts

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import type { Fr, Point } from '@aztec/foundation/fields';
1+
import type { Point } from '@aztec/foundation/fields';
22
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
33
import type { FailingFunction, NoirCallStack } from '@aztec/stdlib/errors';
44

55
import { ExecutionError } from '../../common/errors.js';
6-
import type { AvmContext } from './avm_context.js';
76

87
/**
98
* Avm-specific errors should derive from this
@@ -179,54 +178,3 @@ export class AvmRevertReason extends ExecutionError {
179178
super(message, failingFunction, noirCallStack, options);
180179
}
181180
}
182-
183-
async function createRevertReason(message: string, revertData: Fr[], context: AvmContext): Promise<AvmRevertReason> {
184-
// We drop the returnPc information.
185-
const internalCallStack = context.machineState.internalCallStack.map(entry => entry.callPc);
186-
187-
// If we are reverting due to the same error that we have been tracking, we use the nested error as the cause.
188-
let nestedError = undefined;
189-
const revertDataEquals = (a: Fr[], b: Fr[]) => a.length === b.length && a.every((v, i) => v.equals(b[i]));
190-
if (
191-
context.machineState.collectedRevertInfo &&
192-
revertDataEquals(context.machineState.collectedRevertInfo.revertDataRepresentative, revertData)
193-
) {
194-
nestedError = context.machineState.collectedRevertInfo.recursiveRevertReason;
195-
message = context.machineState.collectedRevertInfo.recursiveRevertReason.message;
196-
}
197-
198-
const fnName = await context.persistableState.getPublicFunctionDebugName(context.environment);
199-
200-
return new AvmRevertReason(
201-
message,
202-
/*failingFunction=*/ {
203-
contractAddress: context.environment.address,
204-
functionName: fnName,
205-
},
206-
/*noirCallStack=*/ [...internalCallStack, context.machineState.pc].map(pc => `0.${pc}`),
207-
/*options=*/ { cause: nestedError },
208-
);
209-
}
210-
211-
/**
212-
* Create a "revert reason" error for an exceptional halt.
213-
*
214-
* @param haltingError - the lower-level error causing the exceptional halt
215-
* @param context - the context of the AVM execution used to extract the failingFunction and noirCallStack
216-
*/
217-
export async function revertReasonFromExceptionalHalt(
218-
haltingError: AvmExecutionError,
219-
context: AvmContext,
220-
): Promise<AvmRevertReason> {
221-
return await createRevertReason(haltingError.message, [], context);
222-
}
223-
224-
/**
225-
* Create a "revert reason" error for an explicit revert (a root cause).
226-
*
227-
* @param revertData - output data of the explicit REVERT instruction
228-
* @param context - the context of the AVM execution used to extract the failingFunction and noirCallStack
229-
*/
230-
export async function revertReasonFromExplicitRevert(revertData: Fr[], context: AvmContext): Promise<AvmRevertReason> {
231-
return await createRevertReason('Assertion failed: ', revertData, context);
232-
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import type { Fr } from '@aztec/foundation/fields';
2+
3+
import type { AvmContext } from './avm_context.js';
4+
import { type AvmExecutionError, AvmRevertReason } from './errors.js';
5+
6+
async function createRevertReason(message: string, revertData: Fr[], context: AvmContext): Promise<AvmRevertReason> {
7+
// We drop the returnPc information.
8+
const internalCallStack = context.machineState.internalCallStack.map(entry => entry.callPc);
9+
10+
// If we are reverting due to the same error that we have been tracking, we use the nested error as the cause.
11+
let nestedError = undefined;
12+
const revertDataEquals = (a: Fr[], b: Fr[]) => a.length === b.length && a.every((v, i) => v.equals(b[i]));
13+
if (
14+
context.machineState.collectedRevertInfo &&
15+
revertDataEquals(context.machineState.collectedRevertInfo.revertDataRepresentative, revertData)
16+
) {
17+
nestedError = context.machineState.collectedRevertInfo.recursiveRevertReason;
18+
message = context.machineState.collectedRevertInfo.recursiveRevertReason.message;
19+
}
20+
21+
const fnName = await context.persistableState.getPublicFunctionDebugName(context.environment);
22+
23+
return new AvmRevertReason(
24+
message,
25+
/*failingFunction=*/ {
26+
contractAddress: context.environment.address,
27+
functionName: fnName,
28+
},
29+
/*noirCallStack=*/ [...internalCallStack, context.machineState.pc].map(pc => `0.${pc}`),
30+
/*options=*/ { cause: nestedError },
31+
);
32+
}
33+
34+
/**
35+
* Create a "revert reason" error for an exceptional halt.
36+
*
37+
* @param haltingError - the lower-level error causing the exceptional halt
38+
* @param context - the context of the AVM execution used to extract the failingFunction and noirCallStack
39+
*/
40+
export async function revertReasonFromExceptionalHalt(
41+
haltingError: AvmExecutionError,
42+
context: AvmContext,
43+
): Promise<AvmRevertReason> {
44+
return await createRevertReason(haltingError.message, [], context);
45+
}
46+
47+
/**
48+
* Create a "revert reason" error for an explicit revert (a root cause).
49+
*
50+
* @param revertData - output data of the explicit REVERT instruction
51+
* @param context - the context of the AVM execution used to extract the failingFunction and noirCallStack
52+
*/
53+
export async function revertReasonFromExplicitRevert(revertData: Fr[], context: AvmContext): Promise<AvmRevertReason> {
54+
return await createRevertReason('Assertion failed: ', revertData, context);
55+
}

0 commit comments

Comments
 (0)