Skip to content

Commit

Permalink
fix: ye old format
Browse files Browse the repository at this point in the history
  • Loading branch information
Maddiaa0 committed Jan 19, 2024
1 parent 2dc2c70 commit 0b39a36
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 143 deletions.
2 changes: 1 addition & 1 deletion yarn-project/acir-simulator/src/avm/avm_context.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
90 changes: 44 additions & 46 deletions yarn-project/acir-simulator/src/avm/avm_execution_environment.ts
Original file line number Diff line number Diff line change
@@ -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,
[],
);
}

}
}
5 changes: 5 additions & 0 deletions yarn-project/acir-simulator/src/avm/avm_machine_state.ts
Original file line number Diff line number Diff line change
@@ -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;

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

}
72 changes: 36 additions & 36 deletions yarn-project/acir-simulator/src/avm/fixtures/index.ts
Original file line number Diff line number Diff line change
@@ -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,
[],
);
}
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,
[],
);
};
2 changes: 1 addition & 1 deletion yarn-project/acir-simulator/src/avm/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<AvmStateManager>;

beforeEach(() => {
stateManager = mock<AvmStateManager>();
})
});

it('Should execute a series of instructions', () => {
const calldata: Fr[] = [new Fr(1), new Fr(2)];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<AvmStateManager>;
Expand Down
77 changes: 39 additions & 38 deletions yarn-project/acir-simulator/src/avm/opcodes/storage.test.ts
Original file line number Diff line number Diff line change
@@ -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<AvmStateManager>;
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<AvmStateManager>();
describe('Storage Instructions', () => {
let stateManager: MockProxy<AvmStateManager>;
let machineState: AvmMachineState;
const contractAddress = AztecAddress.random();

const executionEnvironment = initExecutionEnvironment(contractAddress);
machineState = new AvmMachineState([], executionEnvironment);
});
beforeEach(() => {
stateManager = mock<AvmStateManager>();

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);
})
})
expect(stateManager.read).toBeCalledWith(contractAddress, a);

const actual = machineState.readMemory(1);
expect(actual).toEqual(expectedResult);
});
});
Loading

0 comments on commit 0b39a36

Please sign in to comment.