Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions yarn-project/simulator/src/public/avm/avm_simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,9 @@ import { AvmExecutionEnvironment } from './avm_execution_environment.js';
import type { Gas } from './avm_gas.js';
import { AvmMachineState } from './avm_machine_state.js';
import type { AvmSimulatorInterface } from './avm_simulator_interface.js';
import {
AvmExecutionError,
AvmRevertReason,
InvalidProgramCounterError,
revertReasonFromExceptionalHalt,
revertReasonFromExplicitRevert,
} from './errors.js';
import { AvmExecutionError, AvmRevertReason, InvalidProgramCounterError } from './errors.js';
import type { Instruction } from './opcodes/instruction.js';
import { revertReasonFromExceptionalHalt, revertReasonFromExplicitRevert } from './revert_reason.js';
import {
INSTRUCTION_SET,
type InstructionSet,
Expand Down
54 changes: 1 addition & 53 deletions yarn-project/simulator/src/public/avm/errors.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type { Fr, Point } from '@aztec/foundation/fields';
import type { Point } from '@aztec/foundation/fields';
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
import type { FailingFunction, NoirCallStack } from '@aztec/stdlib/errors';

import { ExecutionError } from '../../common/errors.js';
import type { AvmContext } from './avm_context.js';

/**
* Avm-specific errors should derive from this
Expand Down Expand Up @@ -179,54 +178,3 @@ export class AvmRevertReason extends ExecutionError {
super(message, failingFunction, noirCallStack, options);
}
}

async function createRevertReason(message: string, revertData: Fr[], context: AvmContext): Promise<AvmRevertReason> {
// We drop the returnPc information.
const internalCallStack = context.machineState.internalCallStack.map(entry => entry.callPc);

// If we are reverting due to the same error that we have been tracking, we use the nested error as the cause.
let nestedError = undefined;
const revertDataEquals = (a: Fr[], b: Fr[]) => a.length === b.length && a.every((v, i) => v.equals(b[i]));
if (
context.machineState.collectedRevertInfo &&
revertDataEquals(context.machineState.collectedRevertInfo.revertDataRepresentative, revertData)
) {
nestedError = context.machineState.collectedRevertInfo.recursiveRevertReason;
message = context.machineState.collectedRevertInfo.recursiveRevertReason.message;
}

const fnName = await context.persistableState.getPublicFunctionDebugName(context.environment);

return new AvmRevertReason(
message,
/*failingFunction=*/ {
contractAddress: context.environment.address,
functionName: fnName,
},
/*noirCallStack=*/ [...internalCallStack, context.machineState.pc].map(pc => `0.${pc}`),
/*options=*/ { cause: nestedError },
);
}

/**
* Create a "revert reason" error for an exceptional halt.
*
* @param haltingError - the lower-level error causing the exceptional halt
* @param context - the context of the AVM execution used to extract the failingFunction and noirCallStack
*/
export async function revertReasonFromExceptionalHalt(
haltingError: AvmExecutionError,
context: AvmContext,
): Promise<AvmRevertReason> {
return await createRevertReason(haltingError.message, [], context);
}

/**
* Create a "revert reason" error for an explicit revert (a root cause).
*
* @param revertData - output data of the explicit REVERT instruction
* @param context - the context of the AVM execution used to extract the failingFunction and noirCallStack
*/
export async function revertReasonFromExplicitRevert(revertData: Fr[], context: AvmContext): Promise<AvmRevertReason> {
return await createRevertReason('Assertion failed: ', revertData, context);
}
55 changes: 55 additions & 0 deletions yarn-project/simulator/src/public/avm/revert_reason.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import type { Fr } from '@aztec/foundation/fields';

import type { AvmContext } from './avm_context.js';
import { type AvmExecutionError, AvmRevertReason } from './errors.js';

async function createRevertReason(message: string, revertData: Fr[], context: AvmContext): Promise<AvmRevertReason> {
// We drop the returnPc information.
const internalCallStack = context.machineState.internalCallStack.map(entry => entry.callPc);

// If we are reverting due to the same error that we have been tracking, we use the nested error as the cause.
let nestedError = undefined;
const revertDataEquals = (a: Fr[], b: Fr[]) => a.length === b.length && a.every((v, i) => v.equals(b[i]));
if (
context.machineState.collectedRevertInfo &&
revertDataEquals(context.machineState.collectedRevertInfo.revertDataRepresentative, revertData)
) {
nestedError = context.machineState.collectedRevertInfo.recursiveRevertReason;
message = context.machineState.collectedRevertInfo.recursiveRevertReason.message;
}

const fnName = await context.persistableState.getPublicFunctionDebugName(context.environment);

return new AvmRevertReason(
message,
/*failingFunction=*/ {
contractAddress: context.environment.address,
functionName: fnName,
},
/*noirCallStack=*/ [...internalCallStack, context.machineState.pc].map(pc => `0.${pc}`),
/*options=*/ { cause: nestedError },
);
}

/**
* Create a "revert reason" error for an exceptional halt.
*
* @param haltingError - the lower-level error causing the exceptional halt
* @param context - the context of the AVM execution used to extract the failingFunction and noirCallStack
*/
export async function revertReasonFromExceptionalHalt(
haltingError: AvmExecutionError,
context: AvmContext,
): Promise<AvmRevertReason> {
return await createRevertReason(haltingError.message, [], context);
}

/**
* Create a "revert reason" error for an explicit revert (a root cause).
*
* @param revertData - output data of the explicit REVERT instruction
* @param context - the context of the AVM execution used to extract the failingFunction and noirCallStack
*/
export async function revertReasonFromExplicitRevert(revertData: Fr[], context: AvmContext): Promise<AvmRevertReason> {
return await createRevertReason('Assertion failed: ', revertData, context);
}