Skip to content

Commit

Permalink
refactor: remove the state manager in favour of journal
Browse files Browse the repository at this point in the history
  • Loading branch information
Maddiaa0 committed Jan 24, 2024
1 parent 26a2315 commit f0a9fae
Show file tree
Hide file tree
Showing 18 changed files with 111 additions and 179 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 @@ -3,17 +3,17 @@ 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, 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 @@ -24,7 +24,7 @@ describe('Arithmetic Instructions', () => {
machineState.writeMemory(0, a);
machineState.writeMemory(1, b);

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

const expected = new Fr(3n);
const actual = machineState.readMemory(2);
Expand All @@ -38,7 +38,7 @@ describe('Arithmetic Instructions', () => {
machineState.writeMemory(0, a);
machineState.writeMemory(1, b);

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

const expected = new Fr(0n);
const actual = machineState.readMemory(3);
Expand All @@ -54,7 +54,7 @@ describe('Arithmetic Instructions', () => {
machineState.writeMemory(0, a);
machineState.writeMemory(1, b);

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

const expected = new Fr(Fr.MODULUS - 1n);
const actual = machineState.readMemory(2);
Expand All @@ -70,7 +70,7 @@ describe('Arithmetic Instructions', () => {
machineState.writeMemory(0, a);
machineState.writeMemory(1, b);

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

const expected = new Fr(6n);
const actual = machineState.readMemory(2);
Expand All @@ -84,7 +84,7 @@ describe('Arithmetic Instructions', () => {
machineState.writeMemory(0, a);
machineState.writeMemory(1, b);

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

const expected = new Fr(Fr.MODULUS - 3n);
const actual = machineState.readMemory(2);
Expand All @@ -100,7 +100,7 @@ describe('Arithmetic Instructions', () => {
machineState.writeMemory(0, a);
machineState.writeMemory(1, b);

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

// Note
const actual = machineState.readMemory(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,7 +1,7 @@
import { Fr } from '@aztec/foundation/fields';

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';

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

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

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

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

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

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

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

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

Expand Down
Loading

0 comments on commit f0a9fae

Please sign in to comment.