Skip to content

Commit

Permalink
refactor(avm): remove the state manager in favour of journal (AztecPr…
Browse files Browse the repository at this point in the history
  • Loading branch information
Maddiaa0 authored Jan 25, 2024
1 parent 3e86870 commit 40f9324
Show file tree
Hide file tree
Showing 18 changed files with 118 additions and 186 deletions.
14 changes: 7 additions & 7 deletions yarn-project/acir-simulator/src/avm/avm_context.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { AvmExecutionEnvironment } from './avm_execution_environment.js';
import { AvmMachineState } from './avm_machine_state.js';
import { AvmMessageCallResult } from './avm_message_call_result.js';
import { AvmStateManager } from './avm_state_manager.js';
import { AvmInterpreter } from './interpreter/index.js';
import { AvmJournal } from './journal/journal.js';
import { decodeBytecode } from './opcodes/decode_bytecode.js';
import { Instruction } from './opcodes/index.js';

Expand All @@ -14,12 +14,12 @@ import { Instruction } from './opcodes/index.js';
export class AvmContext {
/** Contains constant variables provided by the kernel */
private executionEnvironment: AvmExecutionEnvironment;
/** A wrapper that manages mutable state during execution - (caching, fetching) */
private stateManager: AvmStateManager;
/** Manages mutable state during execution - (caching, fetching) */
private journal: AvmJournal;

constructor(executionEnvironment: AvmExecutionEnvironment, stateManager: AvmStateManager) {
constructor(executionEnvironment: AvmExecutionEnvironment, journal: AvmJournal) {
this.executionEnvironment = executionEnvironment;
this.stateManager = stateManager;
this.journal = journal;
}

/**
Expand All @@ -32,13 +32,13 @@ export class AvmContext {
*/
public call(): AvmMessageCallResult {
// NOTE: the following is mocked as getPublicBytecode does not exist yet
// const bytecode = stateManager.journal.hostStorage.contractsDb.getBytecode(this.executionEnvironment.address);
// const bytecode = journal.journal.hostStorage.contractsDb.getBytecode(this.executionEnvironment.address);
const bytecode = Buffer.from('0x01000100020003');

const instructions: Instruction[] = decodeBytecode(bytecode);

const context = new AvmMachineState(this.executionEnvironment);
const interpreter = new AvmInterpreter(context, this.stateManager, instructions);
const interpreter = new AvmInterpreter(context, this.journal, instructions);

return interpreter.run();
}
Expand Down
65 changes: 0 additions & 65 deletions yarn-project/acir-simulator/src/avm/avm_state_manager.ts

This file was deleted.

6 changes: 3 additions & 3 deletions yarn-project/acir-simulator/src/avm/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import { Fr } from '@aztec/foundation/fields';
import { mock } from 'jest-mock-extended';

import { AvmMachineState } from './avm_machine_state.js';
import { AvmStateManager } from './avm_state_manager.js';
import { initExecutionEnvironment } from './fixtures/index.js';
import { AvmInterpreter } from './interpreter/interpreter.js';
import { AvmJournal } from './journal/journal.js';
import { decodeBytecode } from './opcodes/decode_bytecode.js';
import { encodeToBytecode } from './opcodes/encode_to_bytecode.js';
import { Opcode } from './opcodes/opcodes.js';

describe('avm', () => {
it('Should execute bytecode', () => {
const calldata: Fr[] = [new Fr(1), new Fr(2)];
const stateManager = mock<AvmStateManager>();
const journal = mock<AvmJournal>();

// Construct bytecode
const calldataCopyArgs = [0, 2, 0];
Expand All @@ -30,7 +30,7 @@ describe('avm', () => {

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

expect(avmReturnData.reverted).toBe(false);
Expand Down
3 changes: 0 additions & 3 deletions yarn-project/acir-simulator/src/avm/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ import { Fr } from '@aztec/foundation/fields';
import { MockProxy, mock } from 'jest-mock-extended';

import { AvmMachineState } from '../avm_machine_state.js';
import { AvmStateManager } from '../avm_state_manager.js';
import { initExecutionEnvironment } from '../fixtures/index.js';
import { AvmJournal } from '../journal/journal.js';
import { Add } from '../opcodes/arithmetic.js';
import { Jump, Return } from '../opcodes/control_flow.js';
import { Instruction } from '../opcodes/instruction.js';
import { CalldataCopy } from '../opcodes/memory.js';
import { AvmInterpreter, InvalidProgramCounterError } from './interpreter.js';

describe('interpreter', () => {
let stateManager: MockProxy<AvmStateManager>;
let journal: MockProxy<AvmJournal>;

beforeEach(() => {
stateManager = mock<AvmStateManager>();
journal = mock<AvmJournal>();
});

it('Should execute a series of instructions', () => {
Expand All @@ -28,7 +28,7 @@ describe('interpreter', () => {
];

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

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

const context = new AvmMachineState(initExecutionEnvironment({ calldata }));
const interpreter = new AvmInterpreter(context, stateManager, instructions);
const interpreter = new AvmInterpreter(context, journal, instructions);

const avmReturnData = interpreter.run();

Expand Down
10 changes: 5 additions & 5 deletions yarn-project/acir-simulator/src/avm/interpreter/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { strict as assert } from 'assert';

import { AvmMachineState } from '../avm_machine_state.js';
import { AvmMessageCallResult } from '../avm_message_call_result.js';
import { AvmStateManager } from '../avm_state_manager.js';
import { AvmJournal } from '../journal/index.js';
import { Instruction } from '../opcodes/index.js';

/**
Expand All @@ -15,11 +15,11 @@ import { Instruction } from '../opcodes/index.js';
export class AvmInterpreter {
private instructions: Instruction[] = [];
private machineState: AvmMachineState;
private stateManager: AvmStateManager;
private journal: AvmJournal;

constructor(machineState: AvmMachineState, stateManager: AvmStateManager, instructions: Instruction[]) {
constructor(machineState: AvmMachineState, stateManager: AvmJournal, instructions: Instruction[]) {
this.machineState = machineState;
this.stateManager = stateManager;
this.journal = stateManager;
this.instructions = instructions;
}

Expand All @@ -37,7 +37,7 @@ export class AvmInterpreter {
const instruction = this.instructions[this.machineState.pc];
assert(!!instruction); // This should never happen

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

if (this.machineState.pc >= this.instructions.length) {
throw new InvalidProgramCounterError(this.machineState.pc, /*max=*/ this.instructions.length);
Expand Down
18 changes: 9 additions & 9 deletions yarn-project/acir-simulator/src/avm/opcodes/arithmetic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ import { MockProxy, mock } from 'jest-mock-extended';

import { AvmMachineState } from '../avm_machine_state.js';
import { Field } from '../avm_memory_types.js';
import { AvmStateManager } from '../avm_state_manager.js';
import { initExecutionEnvironment } from '../fixtures/index.js';
import { AvmJournal } from '../journal/journal.js';
import { Add, Div, Mul, Sub } from './arithmetic.js';

describe('Arithmetic Instructions', () => {
let machineState: AvmMachineState;
let stateManager: MockProxy<AvmStateManager>;
let journal: MockProxy<AvmJournal>;

beforeEach(() => {
machineState = new AvmMachineState(initExecutionEnvironment());
stateManager = mock<AvmStateManager>();
journal = mock<AvmJournal>();
});

describe('Add', () => {
Expand All @@ -23,7 +23,7 @@ describe('Arithmetic Instructions', () => {
machineState.memory.set(0, a);
machineState.memory.set(1, b);

new Add(0, 1, 2).execute(machineState, stateManager);
new Add(0, 1, 2).execute(machineState, journal);

const expected = new Field(3n);
const actual = machineState.memory.get(2);
Expand All @@ -37,7 +37,7 @@ describe('Arithmetic Instructions', () => {
machineState.memory.set(0, a);
machineState.memory.set(1, b);

new Add(0, 1, 2).execute(machineState, stateManager);
new Add(0, 1, 2).execute(machineState, journal);

const expected = new Field(0n);
const actual = machineState.memory.get(2);
Expand All @@ -53,7 +53,7 @@ describe('Arithmetic Instructions', () => {
machineState.memory.set(0, a);
machineState.memory.set(1, b);

new Sub(0, 1, 2).execute(machineState, stateManager);
new Sub(0, 1, 2).execute(machineState, journal);

const expected = new Field(Field.MODULUS - 1n);
const actual = machineState.memory.get(2);
Expand All @@ -69,7 +69,7 @@ describe('Arithmetic Instructions', () => {
machineState.memory.set(0, a);
machineState.memory.set(1, b);

new Mul(0, 1, 2).execute(machineState, stateManager);
new Mul(0, 1, 2).execute(machineState, journal);

const expected = new Field(6n);
const actual = machineState.memory.get(2);
Expand All @@ -83,7 +83,7 @@ describe('Arithmetic Instructions', () => {
machineState.memory.set(0, a);
machineState.memory.set(1, b);

new Mul(0, 1, 2).execute(machineState, stateManager);
new Mul(0, 1, 2).execute(machineState, journal);

const expected = new Field(Field.MODULUS - 3n);
const actual = machineState.memory.get(2);
Expand All @@ -99,7 +99,7 @@ describe('Arithmetic Instructions', () => {
machineState.memory.set(0, a);
machineState.memory.set(1, b);

new Div(0, 1, 2).execute(machineState, stateManager);
new Div(0, 1, 2).execute(machineState, journal);

// Note
const actual = machineState.memory.get(2);
Expand Down
10 changes: 5 additions & 5 deletions yarn-project/acir-simulator/src/avm/opcodes/arithmetic.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AvmMachineState } from '../avm_machine_state.js';
import { AvmStateManager } from '../avm_state_manager.js';
import { AvmJournal } from '../journal/index.js';
import { Instruction } from './instruction.js';

export class Add extends Instruction {
Expand All @@ -10,7 +10,7 @@ export class Add extends Instruction {
super();
}

execute(machineState: AvmMachineState, _stateManager: AvmStateManager): void {
execute(machineState: AvmMachineState, _journal: AvmJournal): void {
const a = machineState.memory.get(this.aOffset);
const b = machineState.memory.get(this.bOffset);

Expand All @@ -29,7 +29,7 @@ export class Sub extends Instruction {
super();
}

execute(machineState: AvmMachineState, _stateManager: AvmStateManager): void {
execute(machineState: AvmMachineState, _journal: AvmJournal): void {
const a = machineState.memory.get(this.aOffset);
const b = machineState.memory.get(this.bOffset);

Expand All @@ -48,7 +48,7 @@ export class Mul extends Instruction {
super();
}

execute(machineState: AvmMachineState, _stateManager: AvmStateManager): void {
execute(machineState: AvmMachineState, _journal: AvmJournal): void {
const a = machineState.memory.get(this.aOffset);
const b = machineState.memory.get(this.bOffset);

Expand All @@ -68,7 +68,7 @@ export class Div extends Instruction {
super();
}

execute(machineState: AvmMachineState, _stateManager: AvmStateManager): void {
execute(machineState: AvmMachineState, _journal: AvmJournal): void {
const a = machineState.memory.get(this.aOffset);
const b = machineState.memory.get(this.bOffset);

Expand Down
Loading

0 comments on commit 40f9324

Please sign in to comment.