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 55204f88b47..f8aa30fed67 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 9f45eb15ac5..6dddce70aac 100644 --- a/yarn-project/acir-simulator/src/avm/opcodes/arithmetic.test.ts +++ b/yarn-project/acir-simulator/src/avm/opcodes/arithmetic.test.ts @@ -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; + let journal: MockProxy; beforeEach(() => { machineState = new AvmMachineState(initExecutionEnvironment()); - stateManager = mock(); + journal = mock(); }); describe('Add', () => { @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); diff --git a/yarn-project/acir-simulator/src/avm/opcodes/arithmetic.ts b/yarn-project/acir-simulator/src/avm/opcodes/arithmetic.ts index 6bfc61ff584..d6b5bc2bccb 100644 --- a/yarn-project/acir-simulator/src/avm/opcodes/arithmetic.ts +++ b/yarn-project/acir-simulator/src/avm/opcodes/arithmetic.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'; export class Add extends Instruction { @@ -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); @@ -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); @@ -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); @@ -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); 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 4221f338972..4c791379c7e 100644 --- a/yarn-project/acir-simulator/src/avm/opcodes/bitwise.test.ts +++ b/yarn-project/acir-simulator/src/avm/opcodes/bitwise.test.ts @@ -1,25 +1,25 @@ -import { mock } from 'jest-mock-extended'; +import { MockProxy, mock } from 'jest-mock-extended'; import { AvmMachineState } from '../avm_machine_state.js'; import { TypeTag, Uint16, Uint32 } 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 { And, Not, Or, Shl, Shr, Xor } from './bitwise.js'; 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 integral types', () => { machineState.memory.set(0, new Uint32(0b11111110010011100100n)); machineState.memory.set(1, new Uint32(0b11100100111001001111n)); - new And(0, 1, 2, TypeTag.UINT32).execute(machineState, stateManager); + new And(0, 1, 2, TypeTag.UINT32).execute(machineState, journal); const actual = machineState.memory.get(2); expect(actual).toEqual(new Uint32(0b11100100010001000100n)); @@ -32,7 +32,7 @@ describe('Bitwise instructions', () => { machineState.memory.set(0, a); machineState.memory.set(1, b); - new Or(0, 1, 2, TypeTag.UINT32).execute(machineState, stateManager); + new Or(0, 1, 2, TypeTag.UINT32).execute(machineState, journal); const expected = new Uint32(0b11111110111011101111n); const actual = machineState.memory.get(2); @@ -46,7 +46,7 @@ describe('Bitwise instructions', () => { machineState.memory.set(0, a); machineState.memory.set(1, b); - new Xor(0, 1, 2, TypeTag.UINT32).execute(machineState, stateManager); + new Xor(0, 1, 2, TypeTag.UINT32).execute(machineState, journal); const expected = new Uint32(0b00011010101010101011n); const actual = machineState.memory.get(2); @@ -61,7 +61,7 @@ describe('Bitwise instructions', () => { machineState.memory.set(0, a); machineState.memory.set(1, b); - new Shr(0, 1, 2, TypeTag.UINT32).execute(machineState, stateManager); + new Shr(0, 1, 2, TypeTag.UINT32).execute(machineState, journal); const expected = a; const actual = machineState.memory.get(2); @@ -75,7 +75,7 @@ describe('Bitwise instructions', () => { machineState.memory.set(0, a); machineState.memory.set(1, b); - new Shr(0, 1, 2, TypeTag.UINT32).execute(machineState, stateManager); + new Shr(0, 1, 2, TypeTag.UINT32).execute(machineState, journal); const expected = new Uint32(0b00111111100100111001n); const actual = machineState.memory.get(2); @@ -89,7 +89,7 @@ describe('Bitwise instructions', () => { machineState.memory.set(0, a); machineState.memory.set(1, b); - new Shr(0, 1, 2, TypeTag.UINT32).execute(machineState, stateManager); + new Shr(0, 1, 2, TypeTag.UINT32).execute(machineState, journal); const expected = new Uint32(0b01n); const actual = machineState.memory.get(2); @@ -105,7 +105,7 @@ describe('Bitwise instructions', () => { machineState.memory.set(0, a); machineState.memory.set(1, b); - new Shl(0, 1, 2, TypeTag.UINT32).execute(machineState, stateManager); + new Shl(0, 1, 2, TypeTag.UINT32).execute(machineState, journal); const expected = a; const actual = machineState.memory.get(2); @@ -119,7 +119,7 @@ describe('Bitwise instructions', () => { machineState.memory.set(0, a); machineState.memory.set(1, b); - new Shl(0, 1, 2, TypeTag.UINT32).execute(machineState, stateManager); + new Shl(0, 1, 2, TypeTag.UINT32).execute(machineState, journal); const expected = new Uint32(0b1111111001001110010000n); const actual = machineState.memory.get(2); @@ -133,7 +133,7 @@ describe('Bitwise instructions', () => { machineState.memory.set(0, a); machineState.memory.set(1, b); - new Shl(0, 1, 2, TypeTag.UINT16).execute(machineState, stateManager); + new Shl(0, 1, 2, TypeTag.UINT16).execute(machineState, journal); const expected = new Uint16(0n); const actual = machineState.memory.get(2); @@ -147,7 +147,7 @@ describe('Bitwise instructions', () => { machineState.memory.set(0, a); machineState.memory.set(1, b); - new Shl(0, 1, 2, TypeTag.UINT16).execute(machineState, stateManager); + new Shl(0, 1, 2, TypeTag.UINT16).execute(machineState, journal); const expected = new Uint16(0b1001001110011100n); const actual = machineState.memory.get(2); @@ -160,7 +160,7 @@ describe('Bitwise instructions', () => { machineState.memory.set(0, a); - new Not(0, 1, TypeTag.UINT16).execute(machineState, stateManager); + new Not(0, 1, TypeTag.UINT16).execute(machineState, journal); const expected = new Uint16(0b1001101100011011n); // high bits! const actual = machineState.memory.get(1); diff --git a/yarn-project/acir-simulator/src/avm/opcodes/bitwise.ts b/yarn-project/acir-simulator/src/avm/opcodes/bitwise.ts index e788abcc1f5..7d7f881bbf9 100644 --- a/yarn-project/acir-simulator/src/avm/opcodes/bitwise.ts +++ b/yarn-project/acir-simulator/src/avm/opcodes/bitwise.ts @@ -1,6 +1,6 @@ import { AvmMachineState } from '../avm_machine_state.js'; import { IntegralValue, TypeTag } from '../avm_memory_types.js'; -import { AvmStateManager } from '../avm_state_manager.js'; +import { AvmJournal } from '../journal/index.js'; import { Instruction } from './instruction.js'; export class And extends Instruction { @@ -11,7 +11,7 @@ export class And extends Instruction { super(); } - execute(machineState: AvmMachineState, _stateManager: AvmStateManager): void { + execute(machineState: AvmMachineState, _journal: AvmJournal): void { Instruction.checkTags(machineState, this.inTag, this.aOffset, this.bOffset); const a = machineState.memory.getAs(this.aOffset); @@ -32,7 +32,7 @@ export class Or extends Instruction { super(); } - execute(machineState: AvmMachineState, _stateManager: AvmStateManager): void { + execute(machineState: AvmMachineState, _journal: AvmJournal): void { Instruction.checkTags(machineState, this.inTag, this.aOffset, this.bOffset); const a = machineState.memory.getAs(this.aOffset); @@ -53,7 +53,7 @@ export class Xor extends Instruction { super(); } - execute(machineState: AvmMachineState, _stateManager: AvmStateManager): void { + execute(machineState: AvmMachineState, _journal: AvmJournal): void { Instruction.checkTags(machineState, this.inTag, this.aOffset, this.bOffset); const a = machineState.memory.getAs(this.aOffset); @@ -74,7 +74,7 @@ export class Not extends Instruction { super(); } - execute(machineState: AvmMachineState, _stateManager: AvmStateManager): void { + execute(machineState: AvmMachineState, _journal: AvmJournal): void { Instruction.checkTags(machineState, this.inTag, this.aOffset); const a = machineState.memory.getAs(this.aOffset); @@ -94,7 +94,7 @@ export class Shl extends Instruction { super(); } - execute(machineState: AvmMachineState, _stateManager: AvmStateManager): void { + execute(machineState: AvmMachineState, _journal: AvmJournal): void { Instruction.checkTags(machineState, this.inTag, this.aOffset, this.bOffset); const a = machineState.memory.getAs(this.aOffset); @@ -115,7 +115,7 @@ export class Shr extends Instruction { super(); } - execute(machineState: AvmMachineState, _stateManager: AvmStateManager): void { + execute(machineState: AvmMachineState, _journal: AvmJournal): void { Instruction.checkTags(machineState, this.inTag, this.aOffset, this.bOffset); const a = machineState.memory.getAs(this.aOffset); diff --git a/yarn-project/acir-simulator/src/avm/opcodes/comparators.ts b/yarn-project/acir-simulator/src/avm/opcodes/comparators.ts index b4f65a66f7b..d9a4c48d867 100644 --- a/yarn-project/acir-simulator/src/avm/opcodes/comparators.ts +++ b/yarn-project/acir-simulator/src/avm/opcodes/comparators.ts @@ -1,6 +1,6 @@ import { AvmMachineState } from '../avm_machine_state.js'; import { Field } from '../avm_memory_types.js'; -import { AvmStateManager } from '../avm_state_manager.js'; +import { AvmJournal } from '../journal/index.js'; import { Instruction } from './instruction.js'; export class Eq extends Instruction { @@ -11,7 +11,7 @@ export class Eq 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); @@ -30,7 +30,7 @@ export class Lt 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); @@ -49,7 +49,7 @@ export class Lte 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); 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 f703242cc5a..75a46347f61 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 @@ -2,8 +2,8 @@ import { MockProxy, mock } from 'jest-mock-extended'; import { AvmMachineState } from '../avm_machine_state.js'; import { TypeTag, Uint16 } 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, 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 { InstructionExecutionError } from './instruction.js'; 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.memory.set(1, new Uint16(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.memory.set(0, new Uint16(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(InstructionExecutionError); + expect(() => returnInstruction.execute(machineState, journal)).toThrow(InstructionExecutionError); }); it('Should increment PC on All other Instructions', () => { @@ -144,7 +144,7 @@ describe('Control Flow Opcodes', () => { innerMachineState.memory.set(1, new Uint16(8n)); innerMachineState.memory.set(2, new Uint16(12n)); 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 7967a417c8a..95c9e12dcdb 100644 --- a/yarn-project/acir-simulator/src/avm/opcodes/control_flow.ts +++ b/yarn-project/acir-simulator/src/avm/opcodes/control_flow.ts @@ -2,7 +2,7 @@ import { Fr } from '@aztec/foundation/fields'; import { AvmMachineState } from '../avm_machine_state.js'; import { IntegralValue } from '../avm_memory_types.js'; -import { AvmStateManager } from '../avm_state_manager.js'; +import { AvmJournal } from '../journal/journal.js'; import { Instruction, InstructionExecutionError } from './instruction.js'; export class Return extends Instruction { @@ -13,7 +13,7 @@ export class Return extends Instruction { super(); } - execute(machineState: AvmMachineState, _stateManager: AvmStateManager): void { + execute(machineState: AvmMachineState, _journal: AvmJournal): void { const returnData = machineState.memory .getSlice(this.returnOffset, this.copySize) .map(fvt => new Fr(fvt.toBigInt())); @@ -32,7 +32,7 @@ export class Jump extends Instruction { super(); } - execute(machineState: AvmMachineState, _stateManager: AvmStateManager): void { + execute(machineState: AvmMachineState, _journal: AvmJournal): void { machineState.pc = this.jumpOffset; } } @@ -45,7 +45,7 @@ export class JumpI extends Instruction { super(); } - execute(machineState: AvmMachineState, _stateManager: AvmStateManager): void { + execute(machineState: AvmMachineState, _journal: AvmJournal): void { const condition = machineState.memory.getAs(this.condOffset); // TODO: reconsider this casting @@ -65,7 +65,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; } @@ -79,7 +79,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 InstructionExecutionError('Internal call empty!'); diff --git a/yarn-project/acir-simulator/src/avm/opcodes/instruction.ts b/yarn-project/acir-simulator/src/avm/opcodes/instruction.ts index 766e5ee158e..03e362018ec 100644 --- a/yarn-project/acir-simulator/src/avm/opcodes/instruction.ts +++ b/yarn-project/acir-simulator/src/avm/opcodes/instruction.ts @@ -1,6 +1,6 @@ import { AvmMachineState } from '../avm_machine_state.js'; import { TypeTag } from '../avm_memory_types.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; @@ -9,7 +9,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, journal: 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 5c3edb9e379..8b1b7d101f8 100644 --- a/yarn-project/acir-simulator/src/avm/opcodes/memory.test.ts +++ b/yarn-project/acir-simulator/src/avm/opcodes/memory.test.ts @@ -4,22 +4,22 @@ import { mock } from 'jest-mock-extended'; import { AvmMachineState } from '../avm_machine_state.js'; import { Field, TypeTag, Uint8, Uint16, Uint32, Uint64, Uint128 } 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 { 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(); }); describe('SET', () => { it('should correctly set value and tag (uninitialized)', () => { - new Set(/*value=*/ 1234n, /*offset=*/ 1, TypeTag.UINT16).execute(machineState, stateManager); + new Set(/*value=*/ 1234n, /*offset=*/ 1, TypeTag.UINT16).execute(machineState, journal); const actual = machineState.memory.get(1); const tag = machineState.memory.getTag(1); @@ -31,7 +31,7 @@ describe('Memory instructions', () => { it('should correctly set value and tag (overwriting)', () => { machineState.memory.set(1, new Field(27)); - new Set(/*value=*/ 1234n, /*offset=*/ 1, TypeTag.UINT32).execute(machineState, stateManager); + new Set(/*value=*/ 1234n, /*offset=*/ 1, TypeTag.UINT32).execute(machineState, journal); const actual = machineState.memory.get(1); const tag = machineState.memory.getTag(1); @@ -55,7 +55,7 @@ describe('Memory instructions', () => { new Cast(/*aOffset=*/ 2, /*dstOffset=*/ 12, TypeTag.UINT64), new Cast(/*aOffset=*/ 3, /*dstOffset=*/ 13, TypeTag.UINT128), new Cast(/*aOffset=*/ 4, /*dstOffset=*/ 14, TypeTag.UINT128), - ].forEach(i => i.execute(machineState, stateManager)); + ].forEach(i => i.execute(machineState, journal)); const actual = machineState.memory.getSlice(/*offset=*/ 10, /*size=*/ 5); expect(actual).toEqual([ @@ -82,7 +82,7 @@ describe('Memory instructions', () => { new Cast(/*aOffset=*/ 2, /*dstOffset=*/ 12, TypeTag.UINT16), new Cast(/*aOffset=*/ 3, /*dstOffset=*/ 13, TypeTag.UINT32), new Cast(/*aOffset=*/ 4, /*dstOffset=*/ 14, TypeTag.UINT64), - ].forEach(i => i.execute(machineState, stateManager)); + ].forEach(i => i.execute(machineState, journal)); const actual = machineState.memory.getSlice(/*offset=*/ 10, /*size=*/ 5); expect(actual).toEqual([ @@ -109,7 +109,7 @@ describe('Memory instructions', () => { new Cast(/*aOffset=*/ 2, /*dstOffset=*/ 12, TypeTag.FIELD), new Cast(/*aOffset=*/ 3, /*dstOffset=*/ 13, TypeTag.FIELD), new Cast(/*aOffset=*/ 4, /*dstOffset=*/ 14, TypeTag.FIELD), - ].forEach(i => i.execute(machineState, stateManager)); + ].forEach(i => i.execute(machineState, journal)); const actual = machineState.memory.getSlice(/*offset=*/ 10, /*size=*/ 5); expect(actual).toEqual([ @@ -136,7 +136,7 @@ describe('Memory instructions', () => { new Cast(/*aOffset=*/ 2, /*dstOffset=*/ 12, TypeTag.UINT32), new Cast(/*aOffset=*/ 3, /*dstOffset=*/ 13, TypeTag.UINT64), new Cast(/*aOffset=*/ 4, /*dstOffset=*/ 14, TypeTag.UINT128), - ].forEach(i => i.execute(machineState, stateManager)); + ].forEach(i => i.execute(machineState, journal)); const actual = machineState.memory.getSlice(/*offset=*/ 10, /*size=*/ 5); expect(actual).toEqual([ @@ -153,7 +153,7 @@ describe('Memory instructions', () => { it('Should cast between field elements', () => { machineState.memory.set(0, new Field(12345678n)); - new Cast(/*aOffset=*/ 0, /*dstOffset=*/ 1, TypeTag.FIELD).execute(machineState, stateManager); + new Cast(/*aOffset=*/ 0, /*dstOffset=*/ 1, TypeTag.FIELD).execute(machineState, journal); const actual = machineState.memory.get(1); expect(actual).toEqual(new Field(12345678n)); @@ -165,7 +165,7 @@ describe('Memory instructions', () => { describe('MOV', () => { it('Should move integrals on different memory cells', () => { machineState.memory.set(1, new Uint16(27)); - new Mov(/*offsetA=*/ 1, /*offsetA=*/ 2).execute(machineState, stateManager); + new Mov(/*offsetA=*/ 1, /*offsetA=*/ 2).execute(machineState, journal); const actual = machineState.memory.get(2); const tag = machineState.memory.getTag(2); @@ -176,7 +176,7 @@ describe('Memory instructions', () => { it('Should move field elements on different memory cells', () => { machineState.memory.set(1, new Field(27)); - new Mov(/*offsetA=*/ 1, /*offsetA=*/ 2).execute(machineState, stateManager); + new Mov(/*offsetA=*/ 1, /*offsetA=*/ 2).execute(machineState, journal); const actual = machineState.memory.get(2); const tag = machineState.memory.getTag(2); @@ -192,7 +192,7 @@ describe('Memory instructions', () => { machineState.memory.set(1, new Uint16(456)); // B machineState.memory.set(2, new Uint8(2)); // Condition - 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.memory.get(3); const tag = machineState.memory.getTag(3); @@ -205,7 +205,7 @@ describe('Memory instructions', () => { machineState.memory.set(1, new Uint16(456)); // B machineState.memory.set(2, new Uint8(0)); // Condition - 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.memory.get(3); const tag = machineState.memory.getTag(3); @@ -218,7 +218,7 @@ describe('Memory instructions', () => { machineState.memory.set(1, new Uint16(456)); // B machineState.memory.set(2, new Field(1)); // Condition - 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.memory.get(3); const tag = machineState.memory.getTag(3); @@ -231,7 +231,7 @@ describe('Memory instructions', () => { machineState.memory.set(1, new Uint16(456)); // B machineState.memory.set(2, new Field(0)); // Condition - 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.memory.get(3); const tag = machineState.memory.getTag(3); @@ -246,7 +246,7 @@ describe('Memory instructions', () => { machineState = new AvmMachineState(initExecutionEnvironment({ calldata })); machineState.memory.set(0, new Uint16(12)); // Some previous data to be overwritten - new CalldataCopy(/*cdOffset=*/ 0, /*copySize=*/ 0, /*dstOffset=*/ 0).execute(machineState, stateManager); + new CalldataCopy(/*cdOffset=*/ 0, /*copySize=*/ 0, /*dstOffset=*/ 0).execute(machineState, journal); const actual = machineState.memory.get(0); expect(actual).toEqual(new Uint16(12)); @@ -257,7 +257,7 @@ describe('Memory instructions', () => { machineState = new AvmMachineState(initExecutionEnvironment({ calldata })); machineState.memory.set(0, new Uint16(12)); // Some previous data to be overwritten - 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.memory.getSlice(/*offset=*/ 0, /*size=*/ 3); expect(actual).toEqual([new Field(1), new Field(2), new Field(3)]); @@ -268,7 +268,7 @@ describe('Memory instructions', () => { machineState = new AvmMachineState(initExecutionEnvironment({ calldata })); machineState.memory.set(0, new Uint16(12)); // Some previous data to be overwritten - new CalldataCopy(/*cdOffset=*/ 1, /*copySize=*/ 2, /*dstOffset=*/ 0).execute(machineState, stateManager); + new CalldataCopy(/*cdOffset=*/ 1, /*copySize=*/ 2, /*dstOffset=*/ 0).execute(machineState, journal); const actual = machineState.memory.getSlice(/*offset=*/ 0, /*size=*/ 2); expect(actual).toEqual([new Field(2), new Field(3)]); diff --git a/yarn-project/acir-simulator/src/avm/opcodes/memory.ts b/yarn-project/acir-simulator/src/avm/opcodes/memory.ts index 5645246b962..16aa2327db4 100644 --- a/yarn-project/acir-simulator/src/avm/opcodes/memory.ts +++ b/yarn-project/acir-simulator/src/avm/opcodes/memory.ts @@ -1,6 +1,6 @@ import { AvmMachineState } from '../avm_machine_state.js'; import { Field, TaggedMemory, TypeTag } from '../avm_memory_types.js'; -import { AvmStateManager } from '../avm_state_manager.js'; +import { AvmJournal } from '../journal/index.js'; import { Instruction } from './instruction.js'; export class Set extends Instruction { @@ -11,7 +11,7 @@ export class Set extends Instruction { super(); } - execute(machineState: AvmMachineState, _stateManager: AvmStateManager): void { + execute(machineState: AvmMachineState, _journal: AvmJournal): void { const res = TaggedMemory.integralFromTag(this.value, this.dstTag); machineState.memory.set(this.dstOffset, res); @@ -28,7 +28,7 @@ export class Cast extends Instruction { super(); } - execute(machineState: AvmMachineState, _stateManager: AvmStateManager): void { + execute(machineState: AvmMachineState, _journal: AvmJournal): void { const a = machineState.memory.get(this.aOffset); // TODO: consider not using toBigInt() @@ -49,7 +49,7 @@ export class Mov extends Instruction { super(); } - execute(machineState: AvmMachineState, _stateManager: AvmStateManager): void { + execute(machineState: AvmMachineState, _journal: AvmJournal): void { const a = machineState.memory.get(this.aOffset); machineState.memory.set(this.dstOffset, a); @@ -66,7 +66,7 @@ export class CMov 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); const cond = machineState.memory.get(this.condOffset); @@ -86,7 +86,7 @@ export class CalldataCopy extends Instruction { super(); } - execute(machineState: AvmMachineState, _stateManager: AvmStateManager): void { + execute(machineState: AvmMachineState, _journal: AvmJournal): void { const transformedData = machineState.executionEnvironment.calldata .slice(this.cdOffset, this.cdOffset + this.copySize) .map(f => new Field(f)); 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 265bf670e20..d5fcb0da8be 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.memory.set(0, a); machineState.memory.set(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.memory.set(0, a); machineState.memory.set(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.memory.get(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 3f79594cb31..c8631e515d7 100644 --- a/yarn-project/acir-simulator/src/avm/opcodes/storage.ts +++ b/yarn-project/acir-simulator/src/avm/opcodes/storage.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/journal.js'; import { Instruction } from './instruction.js'; /** - */ @@ -13,11 +13,11 @@ export class SStore extends Instruction { super(); } - execute(machineState: AvmMachineState, stateManager: AvmStateManager): void { + execute(machineState: AvmMachineState, journal: AvmJournal): void { const slot = machineState.memory.get(this.slotOffset); const data = machineState.memory.get(this.dataOffset); - stateManager.store( + journal.writeStorage( machineState.executionEnvironment.storageAddress, new Fr(slot.toBigInt()), new Fr(data.toBigInt()), @@ -36,10 +36,10 @@ export class SLoad extends Instruction { super(); } - async execute(machineState: AvmMachineState, stateManager: AvmStateManager): Promise { + async execute(machineState: AvmMachineState, journal: AvmJournal): Promise { const slot = machineState.memory.get(this.slotOffset); - const data = stateManager.read(machineState.executionEnvironment.storageAddress, new Fr(slot.toBigInt())); + const data = journal.readStorage(machineState.executionEnvironment.storageAddress, new Fr(slot.toBigInt())); machineState.memory.set(this.destOffset, await data);