Skip to content

Commit

Permalink
feat(avm): link up storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Maddiaa0 committed Jan 19, 2024
1 parent b40f42c commit 9e08d0d
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 7 deletions.
10 changes: 6 additions & 4 deletions yarn-project/acir-simulator/src/avm/avm_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

/**
Expand All @@ -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);
Expand Down
54 changes: 54 additions & 0 deletions yarn-project/acir-simulator/src/avm/avm_execution_environment.ts
Original file line number Diff line number Diff line change
@@ -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,
[],
);
}

}
8 changes: 7 additions & 1 deletion yarn-project/acir-simulator/src/avm/avm_machine_state.ts
Original file line number Diff line number Diff line change
@@ -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[];
Expand Down Expand Up @@ -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 = [];
Expand All @@ -42,6 +46,8 @@ export class AvmMachineState {
this.callStack = [];

this.halted = false;

this.executionEnvironment = executionEnvironment;
}

/**
Expand Down
13 changes: 12 additions & 1 deletion yarn-project/acir-simulator/src/avm/avm_state_manager.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<Fr> {
return this.journal.readStorage(contractAddress, slot);
}

}
44 changes: 44 additions & 0 deletions yarn-project/acir-simulator/src/avm/opcodes/storage.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
const slot = machineState.readMemory(this.slotOffset);

const data = stateManager.read(machineState.executionEnvironment.storageAddress, slot);

machineState.writeMemory(this.destOffset, await data)

this.incrementPc(machineState);
}
}
2 changes: 1 addition & 1 deletion yellow-paper/docs/public-vm/avm.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ ExecutionEnvironment {
storageAddress: AztecAddress,
origin: AztecAddress,
sender: AztecAddress,
portal: AztecAddress,
portal: EthAddress,
feePerL1Gas: field,
feePerL2Gas: field,
feePerDaGas: field,
Expand Down

0 comments on commit 9e08d0d

Please sign in to comment.