From 9e08d0d1136a1442a2e8a7b73a40fe3f2989e982 Mon Sep 17 00:00:00 2001 From: Maddiaa0 <47148561+Maddiaa0@users.noreply.github.com> Date: Thu, 18 Jan 2024 21:03:11 +0000 Subject: [PATCH] feat(avm): link up storage --- .../acir-simulator/src/avm/avm_context.ts | 10 ++-- .../src/avm/avm_execution_environment.ts | 54 +++++++++++++++++++ .../src/avm/avm_machine_state.ts | 8 ++- .../src/avm/avm_state_manager.ts | 13 ++++- .../acir-simulator/src/avm/opcodes/storage.ts | 44 +++++++++++++++ yellow-paper/docs/public-vm/avm.md | 2 +- 6 files changed, 124 insertions(+), 7 deletions(-) create mode 100644 yarn-project/acir-simulator/src/avm/avm_execution_environment.ts create mode 100644 yarn-project/acir-simulator/src/avm/opcodes/storage.ts diff --git a/yarn-project/acir-simulator/src/avm/avm_context.ts b/yarn-project/acir-simulator/src/avm/avm_context.ts index 79e763f79f45..b094b705e29b 100644 --- a/yarn-project/acir-simulator/src/avm/avm_context.ts +++ b/yarn-project/acir-simulator/src/avm/avm_context.ts @@ -6,6 +6,7 @@ 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 @@ -14,9 +15,11 @@ import { Instruction } from './opcodes/index.js'; */ export class AvmContext { private stateManager: AvmStateManager; + private executionEnvironment: ExecutionEnvironment; - constructor(stateManager: AvmStateManager) { + constructor(executionEnvironment: ExecutionEnvironment, stateManager: AvmStateManager) { this.stateManager = stateManager; + this.executionEnvironment = executionEnvironment; } /** @@ -26,12 +29,11 @@ export class AvmContext { * - We interpret the bytecode * - We run the interpreter * - * @param contractAddress - * @param calldata - */ - public call(contractAddress: Fr, calldata: Fr[]): AvmMessageCallResult { + public call(calldata: Fr[]): AvmMessageCallResult { // NOTE: the following is mocked as getPublicBytecode does not exist yet - // const bytecode = stateManager.journal.hostStorage.contractsDb.getBytecode(contractAddress); + // const bytecode = stateManager.journal.hostStorage.contractsDb.getBytecode(this.executionEnvironment.address); const bytecode = Buffer.from('0x01000100020003'); const instructions: Instruction[] = decodeBytecode(bytecode); diff --git a/yarn-project/acir-simulator/src/avm/avm_execution_environment.ts b/yarn-project/acir-simulator/src/avm/avm_execution_environment.ts new file mode 100644 index 000000000000..5cc8741a8edb --- /dev/null +++ b/yarn-project/acir-simulator/src/avm/avm_execution_environment.ts @@ -0,0 +1,54 @@ +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[], + + ) {} + + 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, + [], + ); + } + +} \ 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 53e8599b7c37..52411e863213 100644 --- a/yarn-project/acir-simulator/src/avm/avm_machine_state.ts +++ b/yarn-project/acir-simulator/src/avm/avm_machine_state.ts @@ -1,9 +1,12 @@ import { Fr } from '@aztec/foundation/fields'; +import { ExecutionEnvironment } from './avm_execution_environment.js'; /** * Store's data for an Avm execution frame */ export class AvmMachineState { + public readonly executionEnvironment: ExecutionEnvironment; + /** - */ public readonly calldata: Fr[]; private returnData: Fr[]; @@ -31,8 +34,9 @@ export class AvmMachineState { /** * Create a new avm context * @param calldata - + * @param executionEnvironment - Machine context that is passed to the avm */ - constructor(calldata: Fr[]) { + constructor(calldata: Fr[], executionEnvironment: ExecutionEnvironment) { this.calldata = calldata; this.returnData = []; this.memory = []; @@ -42,6 +46,8 @@ export class AvmMachineState { this.callStack = []; this.halted = false; + + this.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 b5396accc7a9..a06b9e586660 100644 --- a/yarn-project/acir-simulator/src/avm/avm_state_manager.ts +++ b/yarn-project/acir-simulator/src/avm/avm_state_manager.ts @@ -1,6 +1,7 @@ -import { BlockHeader } from '@aztec/circuits.js'; +import { AztecAddress, BlockHeader } from '@aztec/circuits.js'; 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 @@ -42,4 +43,14 @@ export class AvmStateManager { const journal = AvmJournal.branchParent(parent.journal); return new AvmStateManager(parent.blockHeader, journal); } + + + public store(contractAddress: AztecAddress, slot: Fr, value: Fr): void { + this.journal.writeStorage(contractAddress, slot, value); + } + + public read(contractAddress: AztecAddress, slot: Fr): Promise { + return this.journal.readStorage(contractAddress, slot); + } + } diff --git a/yarn-project/acir-simulator/src/avm/opcodes/storage.ts b/yarn-project/acir-simulator/src/avm/opcodes/storage.ts new file mode 100644 index 000000000000..4e97117bbb7d --- /dev/null +++ b/yarn-project/acir-simulator/src/avm/opcodes/storage.ts @@ -0,0 +1,44 @@ +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; + + + constructor(private slotOffset: number, private dataOffset: number) { + super(); + } + + 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) + + this.incrementPc(machineState); + } +} + +/** - */ +export class SLoad extends Instruction { + static type: string = "SLOAD"; + static numberOfOperands = 2; + + + constructor(private slotOffset: number, private destOffset: number) { + super(); + } + + 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) + + this.incrementPc(machineState); + } +} \ No newline at end of file diff --git a/yellow-paper/docs/public-vm/avm.md b/yellow-paper/docs/public-vm/avm.md index b3417afcb298..60f06218ab6f 100644 --- a/yellow-paper/docs/public-vm/avm.md +++ b/yellow-paper/docs/public-vm/avm.md @@ -70,7 +70,7 @@ ExecutionEnvironment { storageAddress: AztecAddress, origin: AztecAddress, sender: AztecAddress, - portal: AztecAddress, + portal: EthAddress, feePerL1Gas: field, feePerL2Gas: field, feePerDaGas: field,