diff --git a/yarn-project/acir-simulator/src/avm/avm_context.ts b/yarn-project/acir-simulator/src/avm/avm_context.ts index ec3fd17a87a..699f9365b15 100644 --- a/yarn-project/acir-simulator/src/avm/avm_context.ts +++ b/yarn-project/acir-simulator/src/avm/avm_context.ts @@ -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'; @@ -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; } /** @@ -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(); } diff --git a/yarn-project/acir-simulator/src/avm/avm_state_manager.ts b/yarn-project/acir-simulator/src/avm/avm_state_manager.ts deleted file mode 100644 index d75c25c72a1..00000000000 --- a/yarn-project/acir-simulator/src/avm/avm_state_manager.ts +++ /dev/null @@ -1,65 +0,0 @@ -import { AztecAddress, BlockHeader } from '@aztec/circuits.js'; -import { Fr } from '@aztec/foundation/fields'; - -import { AvmJournal, HostStorage } from './journal/index.js'; - -/** - * The Avm State Manager is the interpreter's interface to the node's state - * It creates revertible views into the node state and manages the current call's journal - */ -export class AvmStateManager { - /** - */ - public readonly blockHeader: BlockHeader; - - /** - * Journal keeps track of pending state changes - */ - public readonly journal: AvmJournal; - - constructor(blockHeader: BlockHeader, journal: AvmJournal) { - this.blockHeader = blockHeader; - this.journal = journal; - } - - /** - * Create a base state root manager - * - this should be created by the highest level item where the state - * can be reverted - * @param blockHeader - - * @param hostStorage - An immutable view into the node db - * @returns Avm State Manager - */ - public static rootStateManager(blockHeader: BlockHeader, hostStorage: HostStorage): AvmStateManager { - const journal = AvmJournal.rootJournal(hostStorage); - return new AvmStateManager(blockHeader, journal); - } - - /** - * Avm State - * @param parent - Avm state manager with a forked journal - * @returns - */ - public static forkStateManager(parent: AvmStateManager): AvmStateManager { - const journal = AvmJournal.branchParent(parent.journal); - return new AvmStateManager(parent.blockHeader, journal); - } - - /** - * Passes storage call to the journal - * @param contractAddress - - * @param slot - - * @param value - - */ - public store(contractAddress: AztecAddress, slot: Fr, value: Fr): void { - this.journal.writeStorage(contractAddress, slot, value); - } - - /** - * Passes storage read from the journal - * @param contractAddress - - * @param slot - - */ - public read(contractAddress: AztecAddress, slot: Fr): Promise { - return this.journal.readStorage(contractAddress, slot); - } -} diff --git a/yarn-project/acir-simulator/src/avm/index.test.ts b/yarn-project/acir-simulator/src/avm/index.test.ts index bb1b15bffe0..282afcc901e 100644 --- a/yarn-project/acir-simulator/src/avm/index.test.ts +++ b/yarn-project/acir-simulator/src/avm/index.test.ts @@ -3,9 +3,9 @@ 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'; @@ -13,7 +13,7 @@ import { Opcode } from './opcodes/opcodes.js'; describe('avm', () => { it('Should execute bytecode', () => { const calldata: Fr[] = [new Fr(1), new Fr(2)]; - const stateManager = mock(); + const journal = mock(); // Construct bytecode const calldataCopyArgs = [0, 2, 0]; @@ -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); diff --git a/yarn-project/acir-simulator/src/avm/index.ts b/yarn-project/acir-simulator/src/avm/index.ts deleted file mode 100644 index 6a102a6cd57..00000000000 --- a/yarn-project/acir-simulator/src/avm/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -export * from './avm_machine_state.js'; -export * from './avm_context.js'; -export * from './avm_state_manager.js'; diff --git a/yarn-project/acir-simulator/src/avm/interpreter/interpreter.test.ts b/yarn-project/acir-simulator/src/avm/interpreter/interpreter.test.ts index cd6b93d592c..39b8092dabf 100644 --- a/yarn-project/acir-simulator/src/avm/interpreter/interpreter.test.ts +++ b/yarn-project/acir-simulator/src/avm/interpreter/interpreter.test.ts @@ -3,8 +3,8 @@ 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'; @@ -12,10 +12,10 @@ import { CalldataCopy } from '../opcodes/memory.js'; import { AvmInterpreter, InvalidProgramCounterError } from './interpreter.js'; describe('interpreter', () => { - let stateManager: MockProxy; + let journal: MockProxy; beforeEach(() => { - stateManager = mock(); + journal = mock(); }); it('Should execute a series of instructions', () => { @@ -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); @@ -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(); diff --git a/yarn-project/acir-simulator/src/avm/interpreter/interpreter.ts b/yarn-project/acir-simulator/src/avm/interpreter/interpreter.ts index f3970ade43f..aaeca455eac 100644 --- a/yarn-project/acir-simulator/src/avm/interpreter/interpreter.ts +++ b/yarn-project/acir-simulator/src/avm/interpreter/interpreter.ts @@ -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'; /** @@ -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; } @@ -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); diff --git a/yarn-project/acir-simulator/src/avm/opcodes/arithmetic.test.ts b/yarn-project/acir-simulator/src/avm/opcodes/arithmetic.test.ts index c1bd72b6512..3a7fcec81bd 100644 --- a/yarn-project/acir-simulator/src/avm/opcodes/arithmetic.test.ts +++ b/yarn-project/acir-simulator/src/avm/opcodes/arithmetic.test.ts @@ -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; + let journal: MockProxy; beforeEach(() => { machineState = new AvmMachineState(initExecutionEnvironment()); - stateManager = mock(); + journal = mock(); }); describe('Add', () => { @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); diff --git a/yarn-project/acir-simulator/src/avm/opcodes/arithmetic.ts b/yarn-project/acir-simulator/src/avm/opcodes/arithmetic.ts index 94df9713c83..2ce9d5c2ed6 100644 --- a/yarn-project/acir-simulator/src/avm/opcodes/arithmetic.ts +++ b/yarn-project/acir-simulator/src/avm/opcodes/arithmetic.ts @@ -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'; /** -*/ @@ -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); @@ -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); @@ -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); @@ -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); diff --git a/yarn-project/acir-simulator/src/avm/opcodes/bitwise.test.ts b/yarn-project/acir-simulator/src/avm/opcodes/bitwise.test.ts index 56856458a77..34dcc304f2f 100644 --- a/yarn-project/acir-simulator/src/avm/opcodes/bitwise.test.ts +++ b/yarn-project/acir-simulator/src/avm/opcodes/bitwise.test.ts @@ -1,10 +1,10 @@ import { Fr } from '@aztec/foundation/fields'; -import { mock } from 'jest-mock-extended'; +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 { And, /*Not,*/ @@ -16,11 +16,11 @@ import { describe('Bitwise instructions', () => { let machineState: AvmMachineState; - let stateManager = mock(); + let journal: MockProxy; beforeEach(() => { machineState = new AvmMachineState(initExecutionEnvironment()); - stateManager = mock(); + journal = mock(); }); it('Should AND correctly over Fr type', () => { @@ -30,7 +30,7 @@ describe('Bitwise instructions', () => { machineState.writeMemory(0, a); machineState.writeMemory(1, b); - new And(0, 1, 2).execute(machineState, stateManager); + new And(0, 1, 2).execute(machineState, journal); const expected = new Fr(0b11100100010001000100n); const actual = machineState.readMemory(2); @@ -44,7 +44,7 @@ describe('Bitwise instructions', () => { machineState.writeMemory(0, a); machineState.writeMemory(1, b); - new Or(0, 1, 2).execute(machineState, stateManager); + new Or(0, 1, 2).execute(machineState, journal); const expected = new Fr(0b11111110111011101111n); const actual = machineState.readMemory(2); @@ -58,7 +58,7 @@ describe('Bitwise instructions', () => { machineState.writeMemory(0, a); machineState.writeMemory(1, b); - new Xor(0, 1, 2).execute(machineState, stateManager); + new Xor(0, 1, 2).execute(machineState, journal); const expected = new Fr(0b00011010101010101011n); const actual = machineState.readMemory(2); @@ -73,7 +73,7 @@ describe('Bitwise instructions', () => { machineState.writeMemory(0, a); machineState.writeMemory(1, b); - new Shr(0, 1, 2).execute(machineState, stateManager); + new Shr(0, 1, 2).execute(machineState, journal); const expected = a; const actual = machineState.readMemory(2); @@ -87,7 +87,7 @@ describe('Bitwise instructions', () => { machineState.writeMemory(0, a); machineState.writeMemory(1, b); - new Shr(0, 1, 2).execute(machineState, stateManager); + new Shr(0, 1, 2).execute(machineState, journal); const expected = new Fr(0b00111111100100111001n); const actual = machineState.readMemory(2); @@ -101,7 +101,7 @@ describe('Bitwise instructions', () => { machineState.writeMemory(0, a); machineState.writeMemory(1, b); - new Shr(0, 1, 2).execute(machineState, stateManager); + new Shr(0, 1, 2).execute(machineState, journal); const expected = new Fr(0b01n); const actual = machineState.readMemory(2); @@ -117,7 +117,7 @@ describe('Bitwise instructions', () => { machineState.writeMemory(0, a); machineState.writeMemory(1, b); - new Shl(0, 1, 2).execute(machineState, stateManager); + new Shl(0, 1, 2).execute(machineState, journal); const expected = a; const actual = machineState.readMemory(2); @@ -131,7 +131,7 @@ describe('Bitwise instructions', () => { machineState.writeMemory(0, a); machineState.writeMemory(1, b); - new Shl(0, 1, 2).execute(machineState, stateManager); + new Shl(0, 1, 2).execute(machineState, journal); const expected = new Fr(0b1111111001001110010000n); const actual = machineState.readMemory(2); diff --git a/yarn-project/acir-simulator/src/avm/opcodes/bitwise.ts b/yarn-project/acir-simulator/src/avm/opcodes/bitwise.ts index ff7802aac61..98494770672 100644 --- a/yarn-project/acir-simulator/src/avm/opcodes/bitwise.ts +++ b/yarn-project/acir-simulator/src/avm/opcodes/bitwise.ts @@ -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'; /** - */ @@ -13,7 +13,7 @@ export class And 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); @@ -33,7 +33,7 @@ export class Or 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); @@ -53,7 +53,7 @@ export class Xor 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); @@ -73,7 +73,7 @@ export class Not extends Instruction { super(); } - execute(machineState: AvmMachineState, _stateManager: AvmStateManager): void { + execute(machineState: AvmMachineState, _journal: AvmJournal): void { const a: Fr = machineState.readMemory(this.aOffset); // TODO: hack -> Bitwise operations should not occur over field elements @@ -96,7 +96,7 @@ export class Shl 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); @@ -116,7 +116,7 @@ export class Shr 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); diff --git a/yarn-project/acir-simulator/src/avm/opcodes/comparators.ts b/yarn-project/acir-simulator/src/avm/opcodes/comparators.ts index 685a5438c42..f34c9216c89 100644 --- a/yarn-project/acir-simulator/src/avm/opcodes/comparators.ts +++ b/yarn-project/acir-simulator/src/avm/opcodes/comparators.ts @@ -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'; /** -*/ @@ -13,7 +13,7 @@ export class Eq 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); @@ -32,7 +32,7 @@ export class Lt 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); @@ -52,7 +52,7 @@ export class Lte 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); diff --git a/yarn-project/acir-simulator/src/avm/opcodes/control_flow.test.ts b/yarn-project/acir-simulator/src/avm/opcodes/control_flow.test.ts index 976c56089ad..8d1e5e0fdae 100644 --- a/yarn-project/acir-simulator/src/avm/opcodes/control_flow.test.ts +++ b/yarn-project/acir-simulator/src/avm/opcodes/control_flow.test.ts @@ -3,8 +3,8 @@ 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, Mul, Sub } from './arithmetic.js'; import { And, Not, Or, Shl, Shr, Xor } from './bitwise.js'; import { Eq, Lt, Lte } from './comparators.js'; @@ -12,11 +12,11 @@ import { InternalCall, InternalCallStackEmptyError, InternalReturn, Jump, JumpI import { CMov, CalldataCopy, Cast, Mov, Set } from './memory.js'; describe('Control Flow Opcodes', () => { - let stateManager: MockProxy; + let journal: MockProxy; let machineState: AvmMachineState; beforeEach(() => { - stateManager = mock(); + journal = mock(); machineState = new AvmMachineState(initExecutionEnvironment()); }); @@ -26,7 +26,7 @@ describe('Control Flow Opcodes', () => { expect(machineState.pc).toBe(0); const instruction = new Jump(jumpLocation); - instruction.execute(machineState, stateManager); + instruction.execute(machineState, journal); expect(machineState.pc).toBe(jumpLocation); }); @@ -40,12 +40,12 @@ describe('Control Flow Opcodes', () => { machineState.writeMemory(1, new Fr(2n)); const instruction = new JumpI(jumpLocation, 0); - instruction.execute(machineState, stateManager); + instruction.execute(machineState, journal); expect(machineState.pc).toBe(jumpLocation); // Truthy can be greater than 1 const instruction1 = new JumpI(jumpLocation1, 1); - instruction1.execute(machineState, stateManager); + instruction1.execute(machineState, journal); expect(machineState.pc).toBe(jumpLocation1); }); @@ -57,7 +57,7 @@ describe('Control Flow Opcodes', () => { machineState.writeMemory(0, new Fr(0n)); const instruction = new JumpI(jumpLocation, 0); - instruction.execute(machineState, stateManager); + instruction.execute(machineState, journal); expect(machineState.pc).toBe(1); }); @@ -69,10 +69,10 @@ describe('Control Flow Opcodes', () => { const instruction = new InternalCall(jumpLocation); const returnInstruction = new InternalReturn(); - instruction.execute(machineState, stateManager); + instruction.execute(machineState, journal); expect(machineState.pc).toBe(jumpLocation); - returnInstruction.execute(machineState, stateManager); + returnInstruction.execute(machineState, journal); expect(machineState.pc).toBe(1); }); @@ -106,14 +106,14 @@ describe('Control Flow Opcodes', () => { ]; for (let i = 0; i < instructions.length; i++) { - instructions[i].execute(machineState, stateManager); + instructions[i].execute(machineState, journal); expect(machineState.pc).toBe(expectedPcs[i]); } }); it('Should error if Internal Return is called without a corresponding Internal Call', () => { const returnInstruction = new InternalReturn(); - expect(() => returnInstruction.execute(machineState, stateManager)).toThrow(InternalCallStackEmptyError); + expect(() => returnInstruction.execute(machineState, journal)).toThrow(InternalCallStackEmptyError); }); it('Should increment PC on All other Instructions', () => { @@ -141,7 +141,7 @@ describe('Control Flow Opcodes', () => { // Use a fresh machine state each run const innerMachineState = new AvmMachineState(initExecutionEnvironment()); expect(machineState.pc).toBe(0); - instruction.execute(innerMachineState, stateManager); + instruction.execute(innerMachineState, journal); expect(innerMachineState.pc).toBe(1); } }); diff --git a/yarn-project/acir-simulator/src/avm/opcodes/control_flow.ts b/yarn-project/acir-simulator/src/avm/opcodes/control_flow.ts index b988979076e..efbecc03345 100644 --- a/yarn-project/acir-simulator/src/avm/opcodes/control_flow.ts +++ b/yarn-project/acir-simulator/src/avm/opcodes/control_flow.ts @@ -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'; /** - */ @@ -11,7 +11,7 @@ export class Return extends Instruction { super(); } - execute(machineState: AvmMachineState, _stateManager: AvmStateManager): void { + execute(machineState: AvmMachineState, _journal: AvmJournal): void { const returnData = machineState.readMemoryChunk(this.returnOffset, this.returnOffset + this.copySize); machineState.setReturnData(returnData); @@ -28,7 +28,7 @@ export class Jump extends Instruction { super(); } - execute(machineState: AvmMachineState, _stateManager: AvmStateManager): void { + execute(machineState: AvmMachineState, _journal: AvmJournal): void { machineState.pc = this.jumpOffset; } } @@ -42,7 +42,7 @@ export class JumpI extends Instruction { super(); } - execute(machineState: AvmMachineState, _stateManager: AvmStateManager): void { + execute(machineState: AvmMachineState, _journal: AvmJournal): void { const condition = machineState.readMemory(this.condOffset); if (condition.toBigInt() == 0n) { @@ -62,7 +62,7 @@ export class InternalCall extends Instruction { super(); } - execute(machineState: AvmMachineState, _stateManager: AvmStateManager): void { + execute(machineState: AvmMachineState, _journal: AvmJournal): void { machineState.internalCallStack.push(machineState.pc + 1); machineState.pc = this.jumpOffset; } @@ -77,7 +77,7 @@ export class InternalReturn extends Instruction { super(); } - execute(machineState: AvmMachineState, _stateManager: AvmStateManager): void { + execute(machineState: AvmMachineState, _journal: AvmJournal): void { const jumpOffset = machineState.internalCallStack.pop(); if (jumpOffset === undefined) { throw new InternalCallStackEmptyError(); diff --git a/yarn-project/acir-simulator/src/avm/opcodes/instruction.ts b/yarn-project/acir-simulator/src/avm/opcodes/instruction.ts index c77a1a9ddd3..d66a405f1f3 100644 --- a/yarn-project/acir-simulator/src/avm/opcodes/instruction.ts +++ b/yarn-project/acir-simulator/src/avm/opcodes/instruction.ts @@ -1,5 +1,5 @@ import { AvmMachineState } from '../avm_machine_state.js'; -import { AvmStateManager } from '../avm_state_manager.js'; +import { AvmJournal } from '../journal/index.js'; export const AVM_OPERAND_BYTE_LENGTH = 4; export const AVM_OPCODE_BYTE_LENGTH = 1; @@ -8,7 +8,7 @@ export const AVM_OPCODE_BYTE_LENGTH = 1; * Opcode base class */ export abstract class Instruction { - abstract execute(machineState: AvmMachineState, stateManager: AvmStateManager): void; + abstract execute(machineState: AvmMachineState, stateManager: AvmJournal): void; incrementPc(machineState: AvmMachineState): void { machineState.pc++; diff --git a/yarn-project/acir-simulator/src/avm/opcodes/memory.test.ts b/yarn-project/acir-simulator/src/avm/opcodes/memory.test.ts index a6c5543ec0b..e83d5cf881b 100644 --- a/yarn-project/acir-simulator/src/avm/opcodes/memory.test.ts +++ b/yarn-project/acir-simulator/src/avm/opcodes/memory.test.ts @@ -3,23 +3,23 @@ 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 { AvmJournal } from '../journal/journal.js'; import { CMov, CalldataCopy, Cast, Mov, Set } from './memory.js'; describe('Memory instructions', () => { let machineState: AvmMachineState; - let stateManager = mock(); + let journal = mock(); beforeEach(() => { machineState = new AvmMachineState(initExecutionEnvironment()); - stateManager = mock(); + journal = mock(); }); it('Should SET memory correctly', () => { const value = 123456n; - new Set(value, 1).execute(machineState, stateManager); + new Set(value, 1).execute(machineState, journal); const expected = new Fr(value); const actual = machineState.readMemory(1); @@ -33,7 +33,7 @@ describe('Memory instructions', () => { machineState.writeMemory(0, value); - new Cast(/*aOffset=*/ 0, /*dstOffset=*/ 1).execute(machineState, stateManager); + new Cast(/*aOffset=*/ 0, /*dstOffset=*/ 1).execute(machineState, journal); const actual = machineState.readMemory(1); expect(actual).toEqual(value); @@ -44,7 +44,7 @@ describe('Memory instructions', () => { machineState.writeMemory(0, value); - new Cast(/*aOffset=*/ 0, /*dstOffset=*/ 0).execute(machineState, stateManager); + new Cast(/*aOffset=*/ 0, /*dstOffset=*/ 0).execute(machineState, journal); const actual = machineState.readMemory(0); expect(actual).toEqual(value); @@ -57,7 +57,7 @@ describe('Memory instructions', () => { machineState.writeMemory(0, value); - new Mov(/*aOffset=*/ 0, /*dstOffset=*/ 1).execute(machineState, stateManager); + new Mov(/*aOffset=*/ 0, /*dstOffset=*/ 1).execute(machineState, journal); const actual = machineState.readMemory(1); expect(actual).toEqual(value); @@ -68,7 +68,7 @@ describe('Memory instructions', () => { machineState.writeMemory(0, value); - new Mov(/*aOffset=*/ 0, /*dstOffset=*/ 0).execute(machineState, stateManager); + new Mov(/*aOffset=*/ 0, /*dstOffset=*/ 0).execute(machineState, journal); const actual = machineState.readMemory(0); expect(actual).toEqual(value); @@ -85,7 +85,7 @@ describe('Memory instructions', () => { machineState.writeMemory(1, valueB); machineState.writeMemory(2, valueCondition); - new CMov(/*aOffset=*/ 0, /*bOffset=*/ 1, /*condOffset=*/ 2, /*dstOffset=*/ 3).execute(machineState, stateManager); + new CMov(/*aOffset=*/ 0, /*bOffset=*/ 1, /*condOffset=*/ 2, /*dstOffset=*/ 3).execute(machineState, journal); const actual = machineState.readMemory(3); expect(actual).toEqual(valueA); @@ -100,7 +100,7 @@ describe('Memory instructions', () => { machineState.writeMemory(1, valueB); machineState.writeMemory(2, valueCondition); - new CMov(/*aOffset=*/ 0, /*bOffset=*/ 1, /*condOffset=*/ 2, /*dstOffset=*/ 3).execute(machineState, stateManager); + new CMov(/*aOffset=*/ 0, /*bOffset=*/ 1, /*condOffset=*/ 2, /*dstOffset=*/ 3).execute(machineState, journal); const actual = machineState.readMemory(3); expect(actual).toEqual(valueB); @@ -115,7 +115,7 @@ describe('Memory instructions', () => { machineState.writeMemory(1, valueB); machineState.writeMemory(2, valueCondition); - new CMov(/*aOffset=*/ 0, /*bOffset=*/ 1, /*condOffset=*/ 2, /*dstOffset=*/ 2).execute(machineState, stateManager); + new CMov(/*aOffset=*/ 0, /*bOffset=*/ 1, /*condOffset=*/ 2, /*dstOffset=*/ 2).execute(machineState, journal); const actual = machineState.readMemory(2); expect(actual).toEqual(valueA); @@ -130,7 +130,7 @@ describe('Memory instructions', () => { machineState.writeMemory(1, valueB); machineState.writeMemory(2, valueCondition); - new CMov(/*aOffset=*/ 0, /*bOffset=*/ 1, /*condOffset=*/ 2, /*dstOffset=*/ 2).execute(machineState, stateManager); + new CMov(/*aOffset=*/ 0, /*bOffset=*/ 1, /*condOffset=*/ 2, /*dstOffset=*/ 2).execute(machineState, journal); const actual = machineState.readMemory(2); expect(actual).toEqual(valueB); @@ -145,7 +145,7 @@ describe('Memory instructions', () => { machineState = new AvmMachineState(initExecutionEnvironment({ calldata })); machineState.writeMemory(0, previousValue); - new CalldataCopy(/*cdOffset=*/ 2, /*copySize=*/ 0, /*dstOffset=*/ 0).execute(machineState, stateManager); + new CalldataCopy(/*cdOffset=*/ 2, /*copySize=*/ 0, /*dstOffset=*/ 0).execute(machineState, journal); const actual = machineState.readMemory(0); expect(actual).toEqual(previousValue); @@ -158,7 +158,7 @@ describe('Memory instructions', () => { machineState = new AvmMachineState(initExecutionEnvironment({ calldata })); machineState.writeMemory(0, previousValue); - new CalldataCopy(/*cdOffset=*/ 0, /*copySize=*/ 3, /*dstOffset=*/ 0).execute(machineState, stateManager); + new CalldataCopy(/*cdOffset=*/ 0, /*copySize=*/ 3, /*dstOffset=*/ 0).execute(machineState, journal); const actual = machineState.readMemoryChunk(/*offset=*/ 0, /*size=*/ 3); expect(actual).toEqual(calldata); @@ -171,7 +171,7 @@ describe('Memory instructions', () => { machineState = new AvmMachineState(initExecutionEnvironment({ calldata })); machineState.writeMemory(0, previousValue); - new CalldataCopy(/*cdOffset=*/ 1, /*copySize=*/ 2, /*dstOffset=*/ 0).execute(machineState, stateManager); + new CalldataCopy(/*cdOffset=*/ 1, /*copySize=*/ 2, /*dstOffset=*/ 0).execute(machineState, journal); const expected = calldata.slice(1); const actual = machineState.readMemoryChunk(/*offset=*/ 0, /*size=*/ 2); diff --git a/yarn-project/acir-simulator/src/avm/opcodes/memory.ts b/yarn-project/acir-simulator/src/avm/opcodes/memory.ts index 8bfe50e4a5e..4e566d5c5ad 100644 --- a/yarn-project/acir-simulator/src/avm/opcodes/memory.ts +++ b/yarn-project/acir-simulator/src/avm/opcodes/memory.ts @@ -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'; /** - */ @@ -13,7 +13,7 @@ export class Set extends Instruction { super(); } - execute(machineState: AvmMachineState, _stateManager: AvmStateManager): void { + execute(machineState: AvmMachineState, _journal: AvmJournal): void { machineState.writeMemory(this.destOffset, new Fr(this.value)); this.incrementPc(machineState); @@ -30,7 +30,7 @@ export class Cast extends Instruction { super(); } - execute(machineState: AvmMachineState, _stateManager: AvmStateManager): void { + execute(machineState: AvmMachineState, _journal: AvmJournal): void { const a = machineState.readMemory(this.aOffset); machineState.writeMemory(this.destOffset, a); @@ -48,7 +48,7 @@ export class Mov extends Instruction { super(); } - execute(machineState: AvmMachineState, _stateManager: AvmStateManager): void { + execute(machineState: AvmMachineState, _journal: AvmJournal): void { const a = machineState.readMemory(this.aOffset); machineState.writeMemory(this.destOffset, a); @@ -71,7 +71,7 @@ export class CMov 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); const cond = machineState.readMemory(this.condOffset); @@ -91,7 +91,7 @@ export class CalldataCopy extends Instruction { super(); } - execute(machineState: AvmMachineState, _stateManager: AvmStateManager): void { + execute(machineState: AvmMachineState, _journal: AvmJournal): void { const calldata = machineState.executionEnvironment.calldata.slice(this.cdOffset, this.cdOffset + this.copySize); machineState.writeMemoryChunk(this.destOffset, calldata); diff --git a/yarn-project/acir-simulator/src/avm/opcodes/storage.test.ts b/yarn-project/acir-simulator/src/avm/opcodes/storage.test.ts index f44d6fb5f5f..7a11a75e86c 100644 --- a/yarn-project/acir-simulator/src/avm/opcodes/storage.test.ts +++ b/yarn-project/acir-simulator/src/avm/opcodes/storage.test.ts @@ -4,17 +4,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 { SLoad, SStore } from './storage.js'; describe('Storage Instructions', () => { - let stateManager: MockProxy; + let journal: MockProxy; let machineState: AvmMachineState; const address = AztecAddress.random(); beforeEach(() => { - stateManager = mock(); + journal = mock(); const executionEnvironment = initExecutionEnvironment({ address, storageAddress: address }); machineState = new AvmMachineState(executionEnvironment); @@ -27,15 +27,15 @@ describe('Storage Instructions', () => { machineState.writeMemory(0, a); machineState.writeMemory(1, b); - new SStore(0, 1).execute(machineState, stateManager); + new SStore(0, 1).execute(machineState, journal); - expect(stateManager.store).toBeCalledWith(address, a, b); + expect(journal.writeStorage).toBeCalledWith(address, a, b); }); it('Sload should Read into storage', async () => { // Mock response const expectedResult = new Fr(1n); - stateManager.read.mockReturnValueOnce(Promise.resolve(expectedResult)); + journal.readStorage.mockReturnValueOnce(Promise.resolve(expectedResult)); const a = new Fr(1n); const b = new Fr(2n); @@ -43,9 +43,9 @@ describe('Storage Instructions', () => { machineState.writeMemory(0, a); machineState.writeMemory(1, b); - await new SLoad(0, 1).execute(machineState, stateManager); + await new SLoad(0, 1).execute(machineState, journal); - expect(stateManager.read).toBeCalledWith(address, a); + expect(journal.readStorage).toBeCalledWith(address, a); const actual = machineState.readMemory(1); expect(actual).toEqual(expectedResult); diff --git a/yarn-project/acir-simulator/src/avm/opcodes/storage.ts b/yarn-project/acir-simulator/src/avm/opcodes/storage.ts index c773600413a..e03ae48a851 100644 --- a/yarn-project/acir-simulator/src/avm/opcodes/storage.ts +++ b/yarn-project/acir-simulator/src/avm/opcodes/storage.ts @@ -1,5 +1,5 @@ import { AvmMachineState } from '../avm_machine_state.js'; -import { AvmStateManager } from '../avm_state_manager.js'; +import { AvmJournal } from '../journal/journal.js'; import { Instruction } from './instruction.js'; /** - */ @@ -11,11 +11,11 @@ export class SStore extends Instruction { super(); } - execute(machineState: AvmMachineState, stateManager: AvmStateManager): void { + execute(machineState: AvmMachineState, journal: AvmJournal): void { const slot = machineState.readMemory(this.slotOffset); const data = machineState.readMemory(this.dataOffset); - stateManager.store(machineState.executionEnvironment.storageAddress, slot, data); + journal.writeStorage(machineState.executionEnvironment.storageAddress, slot, data); this.incrementPc(machineState); } @@ -30,10 +30,10 @@ export class SLoad extends Instruction { super(); } - async execute(machineState: AvmMachineState, stateManager: AvmStateManager): Promise { + async execute(machineState: AvmMachineState, journal: AvmJournal): Promise { const slot = machineState.readMemory(this.slotOffset); - const data = stateManager.read(machineState.executionEnvironment.storageAddress, slot); + const data = journal.readStorage(machineState.executionEnvironment.storageAddress, slot); machineState.writeMemory(this.destOffset, await data);