Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(avm): make interpreter a function not a class #4272

Merged
merged 22 commits into from
Jan 30, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: environment getters
  • Loading branch information
Maddiaa0 committed Jan 29, 2024
commit 6efc0c93fbfbf7a110dfc700e59a81d6d1e77aab
22 changes: 22 additions & 0 deletions yarn-project/acir-simulator/src/avm/fixtures/index.ts
Original file line number Diff line number Diff line change
@@ -57,3 +57,25 @@ export function initExecutionEnvironment(overrides?: AvmExecutionEnvironmentOver
overrides?.calldata ?? [],
);
}

/**
* An interface that allows to override the default values of the GlobalVariables
*/
export interface GlobalVariablesOverrides {
chainId?: Fr;
version?: Fr;
blockNumber?: Fr;
timestamp?: Fr;
}

/**
* Create an empty instance of the Execution Environment where all values are zero, unless overriden in the overrides object
*/
export function initGlobalVariables(overrides?: GlobalVariablesOverrides): GlobalVariables {
return new GlobalVariables(
overrides?.chainId ?? Fr.zero(),
overrides?.version ?? Fr.zero(),
overrides?.blockNumber ?? Fr.zero(),
overrides?.timestamp ?? Fr.zero(),
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { Fr } from '@aztec/foundation/fields';

import { MockProxy, mock } from 'jest-mock-extended';

import { AvmMachineState } from '../avm_machine_state.js';
import { initExecutionEnvironment, initGlobalVariables } from '../fixtures/index.js';
import { AvmJournal } from '../journal/journal.js';
import {
Address,
BlockNumber,
ChainId,
FeePerDAGas,
FeePerL1Gas,
FeePerL2Gas,
Origin,
Portal,
Sender,
StorageAddress,
Timestamp,
Version,
} from './environment_getters.js';

describe('Environment getters instructions', () => {
let machineState: AvmMachineState;
let journal: MockProxy<AvmJournal>;

beforeEach(async () => {
journal = mock<AvmJournal>();
});

type EnvInstruction = Portal | FeePerL1Gas | FeePerL2Gas | FeePerDAGas | Origin | Sender | StorageAddress | Address;
const envGetterTest = async (key: string, value: Fr, instruction: EnvInstruction) => {
machineState = new AvmMachineState(initExecutionEnvironment({ [key]: value }));

await instruction.execute(machineState, journal);
const actual = machineState.readMemory(0);
expect(actual).toEqual(value);
};

it('Should read address correctly', async () => {
const address = new Fr(123456n);
await envGetterTest('address', address, new Address(0));
});

it('Should read storage address correctly', async () => {
const address = new Fr(123456n);
await envGetterTest('storageAddress', address, new StorageAddress(0));
});

it('Should read Portal correctly', async () => {
const portal = new Fr(123456n);
await envGetterTest('portal', portal, new Portal(0));
});

it('Should read FeePerL1Gas correctly', async () => {
const feePerL1Gas = new Fr(123456n);
await envGetterTest('feePerL1Gas', feePerL1Gas, new FeePerL1Gas(0));
});

it('Should read FeePerL2Gas correctly', async () => {
const feePerL2Gas = new Fr(123456n);
await envGetterTest('feePerL2Gas', feePerL2Gas, new FeePerL2Gas(0));
});

it('Should read FeePerDAGas correctly', async () => {
const feePerDaGas = new Fr(123456n);
await envGetterTest('feePerDaGas', feePerDaGas, new FeePerDAGas(0));
});

it('Should read Origin correctly', async () => {
const origin = new Fr(123456n);
await envGetterTest('origin', origin, new Origin(0));
});

it('Should read Sender correctly', async () => {
const sender = new Fr(123456n);
await envGetterTest('sender', sender, new Sender(0));
});

describe('Global Variables', () => {
type GlobalsInstruction = ChainId | Version | BlockNumber | Timestamp;
const readGlobalVariableTest = async (key: string, value: Fr, instruction: GlobalsInstruction) => {
const globals = initGlobalVariables({ [key]: value });
machineState = new AvmMachineState(initExecutionEnvironment({ globals }));

await instruction.execute(machineState, journal);
const actual = machineState.readMemory(0);
expect(actual).toEqual(value);
};

it('Should read chainId', async () => {
const chainId = new Fr(123456n);
await readGlobalVariableTest('chainId', chainId, new ChainId(0));
});

it('Should read version', async () => {
const version = new Fr(123456n);
await readGlobalVariableTest('version', version, new Version(0));
});

it('Should read block number', async () => {
const blockNumber = new Fr(123456n);
await readGlobalVariableTest('blockNumber', blockNumber, new BlockNumber(0));
});

it('Should read timestamp', async () => {
const timestamp = new Fr(123456n);
await readGlobalVariableTest('timestamp', timestamp, new Timestamp(0));
});
});
});
Loading