-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
154 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 44 additions & 46 deletions
90
yarn-project/acir-simulator/src/avm/avm_execution_environment.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
[], | ||
); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
[], | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 39 additions & 38 deletions
77
yarn-project/acir-simulator/src/avm/opcodes/storage.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
Oops, something went wrong.