diff --git a/yarn-project/acir-simulator/src/avm/avm_context.ts b/yarn-project/acir-simulator/src/avm/avm_context.ts index 6a2e7d1f9f58..855d03338522 100644 --- a/yarn-project/acir-simulator/src/avm/avm_context.ts +++ b/yarn-project/acir-simulator/src/avm/avm_context.ts @@ -1,12 +1,12 @@ import { Fr } from '@aztec/foundation/fields'; +import { ExecutionEnvironment } 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 { decodeBytecode } from './opcodes/decode_bytecode.js'; import { Instruction } from './opcodes/index.js'; -import { ExecutionEnvironment } from './avm_execution_environment.js'; /** * Avm Executor manages the execution of the AVM diff --git a/yarn-project/acir-simulator/src/avm/avm_execution_environment.ts b/yarn-project/acir-simulator/src/avm/avm_execution_environment.ts index 5cc8741a8edb..871c9bb5a098 100644 --- a/yarn-project/acir-simulator/src/avm/avm_execution_environment.ts +++ b/yarn-project/acir-simulator/src/avm/avm_execution_environment.ts @@ -1,54 +1,52 @@ -import { AztecAddress } from "@aztec/foundation/aztec-address"; -import { EthAddress } from "@aztec/foundation/eth-address"; -import { Fr } from "@aztec/foundation/fields"; +import { AztecAddress } from '@aztec/foundation/aztec-address'; +import { EthAddress } from '@aztec/foundation/eth-address'; +import { Fr } from '@aztec/foundation/fields'; - /** - */ +/** - */ export class ExecutionEnvironment { constructor( - /** - */ - public readonly address: AztecAddress, - /** - */ - public readonly storageAddress: AztecAddress, - /** - */ - public readonly origin: AztecAddress, - /** - */ - public readonly sender: AztecAddress, - /** - */ - public readonly portal: EthAddress, - /** - */ - public readonly feePerL1Gas: Fr, - /** - */ - public readonly feePerL2Gas: Fr, - /** - */ - public readonly feePerDaGas: Fr, - /** - */ - public readonly contractCallDepth: Fr, - /** - */ - // globals: TODO: - /** - */ - public readonly isStaticCall: boolean, - /** - */ - public readonly isDelegateCall: boolean, - /** - */ - public readonly calldata: Fr[], - + /** - */ + public readonly address: AztecAddress, + /** - */ + public readonly storageAddress: AztecAddress, + /** - */ + public readonly origin: AztecAddress, + /** - */ + public readonly sender: AztecAddress, + /** - */ + public readonly portal: EthAddress, + /** - */ + public readonly feePerL1Gas: Fr, + /** - */ + public readonly feePerL2Gas: Fr, + /** - */ + public readonly feePerDaGas: Fr, + /** - */ + public readonly contractCallDepth: Fr, + /** - */ + // globals: TODO: + /** - */ + public readonly isStaticCall: boolean, + /** - */ + public readonly isDelegateCall: boolean, + /** - */ + public readonly calldata: Fr[], ) {} static empty(): ExecutionEnvironment { return new ExecutionEnvironment( - AztecAddress.zero(), - AztecAddress.zero(), - AztecAddress.zero(), - AztecAddress.zero(), - EthAddress.ZERO, - Fr.zero(), - Fr.zero(), - Fr.zero(), - Fr.zero(), - false, - false, - [], - ); + AztecAddress.zero(), + AztecAddress.zero(), + AztecAddress.zero(), + AztecAddress.zero(), + EthAddress.ZERO, + Fr.zero(), + Fr.zero(), + Fr.zero(), + Fr.zero(), + false, + false, + [], + ); } - -} \ No newline at end of file +} diff --git a/yarn-project/acir-simulator/src/avm/avm_machine_state.ts b/yarn-project/acir-simulator/src/avm/avm_machine_state.ts index 52411e863213..b5a3f2997a59 100644 --- a/yarn-project/acir-simulator/src/avm/avm_machine_state.ts +++ b/yarn-project/acir-simulator/src/avm/avm_machine_state.ts @@ -1,10 +1,15 @@ import { Fr } from '@aztec/foundation/fields'; + import { ExecutionEnvironment } from './avm_execution_environment.js'; /** * Store's data for an Avm execution frame */ export class AvmMachineState { + /** + * Execution environment contains hard coded information that is received from the kernel + * Items like, the block header and global variables fall within this category + */ public readonly executionEnvironment: ExecutionEnvironment; /** - */ diff --git a/yarn-project/acir-simulator/src/avm/avm_state_manager.ts b/yarn-project/acir-simulator/src/avm/avm_state_manager.ts index a06b9e586660..d75c25c72a1f 100644 --- a/yarn-project/acir-simulator/src/avm/avm_state_manager.ts +++ b/yarn-project/acir-simulator/src/avm/avm_state_manager.ts @@ -1,7 +1,7 @@ import { AztecAddress, BlockHeader } from '@aztec/circuits.js'; +import { Fr } from '@aztec/foundation/fields'; import { AvmJournal, HostStorage } from './journal/index.js'; -import { Fr } from '@aztec/foundation/fields'; /** * The Avm State Manager is the interpreter's interface to the node's state @@ -44,13 +44,22 @@ export class AvmStateManager { 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/fixtures/index.ts b/yarn-project/acir-simulator/src/avm/fixtures/index.ts index 4310ef7a4547..d0d821875700 100644 --- a/yarn-project/acir-simulator/src/avm/fixtures/index.ts +++ b/yarn-project/acir-simulator/src/avm/fixtures/index.ts @@ -1,40 +1,40 @@ // Place large AVM text fixtures in here +import { AztecAddress } from '@aztec/foundation/aztec-address'; +import { EthAddress } from '@aztec/foundation/eth-address'; +import { Fr } from '@aztec/foundation/fields'; -import { AztecAddress } from "@aztec/foundation/aztec-address"; -import { ExecutionEnvironment } from "../avm_execution_environment.js"; -import { EthAddress } from "@aztec/foundation/eth-address"; -import { Fr } from "@aztec/foundation/fields"; +import { ExecutionEnvironment } from '../avm_execution_environment.js'; -export const initExecutionEnvironmentEmpty = () : ExecutionEnvironment => { - return new ExecutionEnvironment( - AztecAddress.zero(), - AztecAddress.zero(), - AztecAddress.zero(), - AztecAddress.zero(), - EthAddress.ZERO, - Fr.zero(), - Fr.zero(), - Fr.zero(), - Fr.zero(), - false, - false, - [], - ); -} +export const initExecutionEnvironmentEmpty = (): ExecutionEnvironment => { + return new ExecutionEnvironment( + AztecAddress.zero(), + AztecAddress.zero(), + AztecAddress.zero(), + AztecAddress.zero(), + EthAddress.ZERO, + Fr.zero(), + Fr.zero(), + Fr.zero(), + Fr.zero(), + false, + false, + [], + ); +}; -export const initExecutionEnvironment = (contractAddress: AztecAddress) : ExecutionEnvironment => { - return new ExecutionEnvironment( - contractAddress, - contractAddress, - AztecAddress.zero(), - AztecAddress.zero(), - EthAddress.ZERO, - Fr.zero(), - Fr.zero(), - Fr.zero(), - Fr.zero(), - false, - false, - [], - ); -} \ No newline at end of file +export const initExecutionEnvironment = (contractAddress: AztecAddress): ExecutionEnvironment => { + return new ExecutionEnvironment( + contractAddress, + contractAddress, + AztecAddress.zero(), + AztecAddress.zero(), + EthAddress.ZERO, + Fr.zero(), + Fr.zero(), + Fr.zero(), + Fr.zero(), + false, + false, + [], + ); +}; diff --git a/yarn-project/acir-simulator/src/avm/index.test.ts b/yarn-project/acir-simulator/src/avm/index.test.ts index 37428df2368a..9ab9845b28bf 100644 --- a/yarn-project/acir-simulator/src/avm/index.test.ts +++ b/yarn-project/acir-simulator/src/avm/index.test.ts @@ -4,11 +4,11 @@ import { mock } from 'jest-mock-extended'; import { AvmMachineState } from './avm_machine_state.js'; import { AvmStateManager } from './avm_state_manager.js'; +import { initExecutionEnvironmentEmpty } from './fixtures/index.js'; import { AvmInterpreter } from './interpreter/interpreter.js'; import { decodeBytecode } from './opcodes/decode_bytecode.js'; import { encodeToBytecode } from './opcodes/encode_to_bytecode.js'; import { Opcode } from './opcodes/opcodes.js'; -import { initExecutionEnvironmentEmpty } from './fixtures/index.js'; describe('avm', () => { it('Should execute bytecode', () => { 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 245060af7456..e5bb9fb6dd25 100644 --- a/yarn-project/acir-simulator/src/avm/interpreter/interpreter.test.ts +++ b/yarn-project/acir-simulator/src/avm/interpreter/interpreter.test.ts @@ -4,19 +4,19 @@ import { MockProxy, mock } from 'jest-mock-extended'; import { AvmMachineState } from '../avm_machine_state.js'; import { AvmStateManager } from '../avm_state_manager.js'; +import { initExecutionEnvironmentEmpty } from '../fixtures/index.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 } from './interpreter.js'; -import { initExecutionEnvironmentEmpty } from '../fixtures/index.js'; describe('interpreter', () => { let stateManager: MockProxy; beforeEach(() => { stateManager = mock(); - }) + }); it('Should execute a series of instructions', () => { const calldata: Fr[] = [new Fr(1), new Fr(2)]; 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 0f31f7a10752..f3448c8740b3 100644 --- a/yarn-project/acir-simulator/src/avm/opcodes/arithmetic.test.ts +++ b/yarn-project/acir-simulator/src/avm/opcodes/arithmetic.test.ts @@ -4,8 +4,8 @@ import { MockProxy, mock } from 'jest-mock-extended'; import { AvmMachineState } from '../avm_machine_state.js'; import { AvmStateManager } from '../avm_state_manager.js'; -import { Add, Div, Mul, Sub } from './arithmetic.js'; import { initExecutionEnvironmentEmpty } from '../fixtures/index.js'; +import { Add, Div, Mul, Sub } from './arithmetic.js'; describe('Arithmetic Instructions', () => { let machineState: AvmMachineState; 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 fe5592527b9c..43f537b9f74a 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 @@ -4,12 +4,12 @@ import { MockProxy, mock } from 'jest-mock-extended'; import { AvmMachineState } from '../avm_machine_state.js'; import { AvmStateManager } from '../avm_state_manager.js'; +import { initExecutionEnvironmentEmpty } from '../fixtures/index.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'; import { InternalCall, InternalCallStackEmptyError, InternalReturn, Jump, JumpI } from './control_flow.js'; import { CalldataCopy, Cast, Mov, Set } from './memory.js'; -import { initExecutionEnvironmentEmpty } from '../fixtures/index.js'; describe('Control Flow Opcodes', () => { let stateManager: MockProxy; 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 cf659721f384..7f9f5fc07217 100644 --- a/yarn-project/acir-simulator/src/avm/opcodes/storage.test.ts +++ b/yarn-project/acir-simulator/src/avm/opcodes/storage.test.ts @@ -1,52 +1,53 @@ -import { MockProxy, mock } from "jest-mock-extended" -import { AvmStateManager } from "../avm_state_manager.js" -import { SLoad, SStore } from "./storage.js"; -import { AvmMachineState } from "../avm_machine_state.js"; -import { AztecAddress } from "@aztec/foundation/aztec-address"; -import { Fr } from "@aztec/foundation/fields"; -import { initExecutionEnvironment } from "../fixtures/index.js"; +import { AztecAddress } from '@aztec/foundation/aztec-address'; +import { Fr } from '@aztec/foundation/fields'; +import { MockProxy, mock } from 'jest-mock-extended'; -describe("Storage Instructions", () => { - let stateManager: MockProxy; - let machineState: AvmMachineState; - const contractAddress = AztecAddress.random(); +import { AvmMachineState } from '../avm_machine_state.js'; +import { AvmStateManager } from '../avm_state_manager.js'; +import { initExecutionEnvironment } from '../fixtures/index.js'; +import { SLoad, SStore } from './storage.js'; - beforeEach(() => { - stateManager = mock(); +describe('Storage Instructions', () => { + let stateManager: MockProxy; + let machineState: AvmMachineState; + const contractAddress = AztecAddress.random(); - const executionEnvironment = initExecutionEnvironment(contractAddress); - machineState = new AvmMachineState([], executionEnvironment); - }); + beforeEach(() => { + stateManager = mock(); - it("Sstore should Write into storage", () => { - const a = new Fr(1n); - const b = new Fr(2n); + const executionEnvironment = initExecutionEnvironment(contractAddress); + machineState = new AvmMachineState([], executionEnvironment); + }); - machineState.writeMemory(0, a); - machineState.writeMemory(1, b); + it('Sstore should Write into storage', () => { + const a = new Fr(1n); + const b = new Fr(2n); - new SStore(0, 1).execute(machineState, stateManager); + machineState.writeMemory(0, a); + machineState.writeMemory(1, b); - expect(stateManager.store).toBeCalledWith(contractAddress, a, b); - }) + new SStore(0, 1).execute(machineState, stateManager); - it("Sload should Read into storage", async () => { - // Mock response - const expectedResult = new Fr(1n); - stateManager.read.mockReturnValueOnce(Promise.resolve(expectedResult)); + expect(stateManager.store).toBeCalledWith(contractAddress, a, b); + }); - const a = new Fr(1n); - const b = new Fr(2n); + it('Sload should Read into storage', async () => { + // Mock response + const expectedResult = new Fr(1n); + stateManager.read.mockReturnValueOnce(Promise.resolve(expectedResult)); - machineState.writeMemory(0, a); - machineState.writeMemory(1, b); + const a = new Fr(1n); + const b = new Fr(2n); - await new SLoad(0, 1).execute(machineState, stateManager); + machineState.writeMemory(0, a); + machineState.writeMemory(1, b); - expect(stateManager.read).toBeCalledWith(contractAddress, a); + await new SLoad(0, 1).execute(machineState, stateManager); - const actual = machineState.readMemory(1); - expect(actual).toEqual(expectedResult); - }) -}) \ No newline at end of file + expect(stateManager.read).toBeCalledWith(contractAddress, 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 4e97117bbb7d..c773600413a7 100644 --- a/yarn-project/acir-simulator/src/avm/opcodes/storage.ts +++ b/yarn-project/acir-simulator/src/avm/opcodes/storage.ts @@ -1,12 +1,11 @@ -import { AvmMachineState } from "../avm_machine_state.js"; -import { AvmStateManager } from "../avm_state_manager.js"; -import { Instruction } from "./instruction.js"; +import { AvmMachineState } from '../avm_machine_state.js'; +import { AvmStateManager } from '../avm_state_manager.js'; +import { Instruction } from './instruction.js'; /** - */ export class SStore extends Instruction { - static type: string = "SSTORE"; - static numberOfOperands = 2; - + static type: string = 'SSTORE'; + static numberOfOperands = 2; constructor(private slotOffset: number, private dataOffset: number) { super(); @@ -15,8 +14,8 @@ export class SStore extends Instruction { execute(machineState: AvmMachineState, stateManager: AvmStateManager): void { const slot = machineState.readMemory(this.slotOffset); const data = machineState.readMemory(this.dataOffset); - - stateManager.store(machineState.executionEnvironment.storageAddress, slot, data) + + stateManager.store(machineState.executionEnvironment.storageAddress, slot, data); this.incrementPc(machineState); } @@ -24,9 +23,8 @@ export class SStore extends Instruction { /** - */ export class SLoad extends Instruction { - static type: string = "SLOAD"; - static numberOfOperands = 2; - + static type: string = 'SLOAD'; + static numberOfOperands = 2; constructor(private slotOffset: number, private destOffset: number) { super(); @@ -34,11 +32,11 @@ export class SLoad extends Instruction { async execute(machineState: AvmMachineState, stateManager: AvmStateManager): Promise { const slot = machineState.readMemory(this.slotOffset); - + const data = stateManager.read(machineState.executionEnvironment.storageAddress, slot); - machineState.writeMemory(this.destOffset, await data) + machineState.writeMemory(this.destOffset, await data); this.incrementPc(machineState); } -} \ No newline at end of file +}