-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add unit tests for storage opcodes
- Loading branch information
Showing
7 changed files
with
114 additions
and
14 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
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 +1,40 @@ | ||
// Place large AVM text fixtures in here | ||
|
||
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"; | ||
|
||
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, | ||
[], | ||
); | ||
} |
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
7 changes: 4 additions & 3 deletions
7
yarn-project/acir-simulator/src/avm/opcodes/arithmetic.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
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
52 changes: 52 additions & 0 deletions
52
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
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"; | ||
|
||
|
||
describe("Storage Instructions", () => { | ||
let stateManager: MockProxy<AvmStateManager>; | ||
let machineState: AvmMachineState; | ||
const contractAddress = AztecAddress.random(); | ||
|
||
beforeEach(() => { | ||
stateManager = mock<AvmStateManager>(); | ||
|
||
const executionEnvironment = initExecutionEnvironment(contractAddress); | ||
machineState = new AvmMachineState([], executionEnvironment); | ||
}); | ||
|
||
it("Sstore should Write into storage", () => { | ||
const a = new Fr(1n); | ||
const b = new Fr(2n); | ||
|
||
machineState.writeMemory(0, a); | ||
machineState.writeMemory(1, b); | ||
|
||
new SStore(0, 1).execute(machineState, stateManager); | ||
|
||
expect(stateManager.store).toBeCalledWith(contractAddress, a, b); | ||
}) | ||
|
||
it("Sload should Read into storage", async () => { | ||
// Mock response | ||
const expectedResult = new Fr(1n); | ||
stateManager.read.mockReturnValueOnce(Promise.resolve(expectedResult)); | ||
|
||
const a = new Fr(1n); | ||
const b = new Fr(2n); | ||
|
||
machineState.writeMemory(0, a); | ||
machineState.writeMemory(1, b); | ||
|
||
await new SLoad(0, 1).execute(machineState, stateManager); | ||
|
||
expect(stateManager.read).toBeCalledWith(contractAddress, a); | ||
|
||
const actual = machineState.readMemory(1); | ||
expect(actual).toEqual(expectedResult); | ||
}) | ||
}) |