Skip to content

Commit

Permalink
fix: make interpreter a singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
Maddiaa0 committed Jan 29, 2024
1 parent 85b3cbf commit 1795610
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 19 deletions.
16 changes: 7 additions & 9 deletions yarn-project/acir-simulator/src/avm/avm_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export class AvmContext {
/** Manages mutable state during execution - (caching, fetching) */
private journal: AvmJournal;

constructor(interpreter: AvmInterpreter, executionEnvironment: AvmExecutionEnvironment, journal: AvmJournal) {
this.interpreter = interpreter;
constructor(executionEnvironment: AvmExecutionEnvironment, journal: AvmJournal) {
this.interpreter = AvmInterpreter.getInstance();
this.executionEnvironment = executionEnvironment;
this.journal = journal;
}
Expand Down Expand Up @@ -61,15 +61,15 @@ export class AvmContext {
*/
public newWithForkedState(): AvmContext {
const forkedState = AvmJournal.branchParent(this.journal);
return new AvmContext(this.interpreter, this.executionEnvironment, forkedState);
return new AvmContext(this.executionEnvironment, forkedState);
}

/**
* Create a new forked avm context - for external calls
*/
public static newWithForkedState(interpreter: AvmInterpreter, executionEnvironment: AvmExecutionEnvironment, journal: AvmJournal): AvmContext {
public static newWithForkedState(executionEnvironment: AvmExecutionEnvironment, journal: AvmJournal): AvmContext {
const forkedState = AvmJournal.branchParent(journal);
return new AvmContext(interpreter, executionEnvironment, forkedState);
return new AvmContext(executionEnvironment, forkedState);
}

/**
Expand All @@ -86,13 +86,12 @@ export class AvmContext {
public static prepExternalCallContext(
address: AztecAddress,
calldata: Fr[],
interpreter: AvmInterpreter,
executionEnvironment: AvmExecutionEnvironment,
journal: AvmJournal,
): AvmContext {
const newExecutionEnvironment = executionEnvironment.newCall(address, calldata);
const forkedState = AvmJournal.branchParent(journal);
return new AvmContext(interpreter, newExecutionEnvironment, forkedState);
return new AvmContext(newExecutionEnvironment, forkedState);
}

/**
Expand All @@ -109,13 +108,12 @@ export class AvmContext {
public static prepExternalStaticCallContext(
address: AztecAddress,
calldata: Fr[],
interpreter: AvmInterpreter,
executionEnvironment: AvmExecutionEnvironment,
journal: AvmJournal,
): AvmContext {
const newExecutionEnvironment = executionEnvironment.newStaticCall(address, calldata);
const forkedState = AvmJournal.branchParent(journal);
return new AvmContext(interpreter, newExecutionEnvironment, forkedState);
return new AvmContext(newExecutionEnvironment, forkedState);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/acir-simulator/src/avm/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('avm', () => {

// Execute instructions
const context = new AvmMachineState(initExecutionEnvironment({ calldata }));
const interpreter = new AvmInterpreter();
const interpreter = AvmInterpreter.getInstance();
const avmReturnData = await interpreter.run(context, journal, instructions);

expect(avmReturnData.reverted).toBe(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('interpreter', () => {
];

const machineState = new AvmMachineState(initExecutionEnvironment({ calldata }));
const interpreter = new AvmInterpreter();
const interpreter = AvmInterpreter.getInstance();
const avmReturnData = await interpreter.run(machineState, journal, instructions);

expect(avmReturnData.reverted).toBe(false);
Expand All @@ -44,7 +44,7 @@ describe('interpreter', () => {
const instructions: Instruction[] = [new Jump(invalidJumpDestination)];

const machineState = new AvmMachineState(initExecutionEnvironment({ calldata }));
const interpreter = new AvmInterpreter();
const interpreter = AvmInterpreter.getInstance();

const avmReturnData = await interpreter.run(machineState, journal, instructions);

Expand Down
18 changes: 16 additions & 2 deletions yarn-project/acir-simulator/src/avm/interpreter/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,35 @@ import { Instruction } from '../opcodes/index.js';
* Executes an Avm context
*/
export class AvmInterpreter {
private static instance: AvmInterpreter;

public static getInstance(): AvmInterpreter {
if (!AvmInterpreter.instance) {
AvmInterpreter.instance = new AvmInterpreter();
}

return AvmInterpreter.instance;
}

/**
* Run the avm
* @returns bool - successful execution will return true
* - reverted execution will return false
* - any other panic will throw
*/
async run(machineState: AvmMachineState, journal: AvmJournal, instructions: Instruction[] = []): Promise<AvmMessageCallResult> {
async run(
machineState: AvmMachineState,
journal: AvmJournal,
instructions: Instruction[] = [],
): Promise<AvmMessageCallResult> {
assert(instructions.length > 0);

try {
while (!machineState.halted) {
const instruction = instructions[machineState.pc];
assert(!!instruction); // This should never happen

await instruction.execute(machineState, journal, this);
await instruction.execute(machineState, journal);

if (machineState.pc >= instructions.length) {
throw new InvalidProgramCounterError(machineState.pc, /*max=*/ instructions.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import { Fr } from '@aztec/foundation/fields';

import { AvmContext } from '../avm_context.js';
import { AvmMachineState } from '../avm_machine_state.js';
import { Field } from '../avm_memory_types.js';
import { AvmJournal } from '../journal/journal.js';
import { Instruction } from './instruction.js';
import { Field } from '../avm_memory_types.js';
import { AvmInterpreter } from '../interpreter/interpreter.js';

export class Call extends Instruction {
static type: string = 'CALL';
Expand All @@ -31,7 +30,6 @@ export class Call extends Instruction {
const avmContext = AvmContext.prepExternalCallContext(
new Fr(callAddress.toBigInt()),
calldata,
new AvmInterpreter(),
machineState.executionEnvironment,
journal,
);
Expand Down Expand Up @@ -78,8 +76,6 @@ export class StaticCall extends Instruction {
const avmContext = AvmContext.prepExternalStaticCallContext(
new Fr(callAddress.toBigInt()),
calldata,
// TODO: how can i remove this outside of the call context?
new AvmInterpreter(),
machineState.executionEnvironment,
journal,
);
Expand Down

0 comments on commit 1795610

Please sign in to comment.