mips_test_oracle.js 1.49 KB
Newer Older
1 2
const { keccak256 } = require("@ethersproject/keccak256");
const { expect } = require("chai");
3 4 5 6 7

const chai = require("chai");
const { solidity } = require("ethereum-waffle");
chai.use(solidity);

8 9
const { writeMemory } = require("../scripts/lib")

10 11 12 13 14 15 16 17 18 19 20
async function loadPreimageAndSelect(mm, data, offset) {
  // add in the preimage at offset 4
  const hash = keccak256(data)
  await mm.AddPreimage(data, offset)

  // write the oracle selection address
  let root = "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"
  root = await writeMemory(mm, root, 0x30001000, hash, true)
  return root
}

21 22 23 24 25 26 27
describe("MIPSMemory oracle", function () {
  beforeEach(async function () {
    const MIPSMemory = await ethers.getContractFactory("MIPSMemory")
    mm = await MIPSMemory.deploy()
    await mm.AddTrieNode(new Uint8Array([0x80]))
  })
  it("simple oracle", async function() {
28
    root = await loadPreimageAndSelect(mm, [0x11,0x22,0x33,0x44,0xaa,0xbb,0xcc,0xdd], 4)
29 30 31 32 33 34

    // length is 8
    expect(await mm.ReadMemory(root, 0x31000000)).to.equal(8)

    // offset 4 is 0xaabbccdd
    expect(await mm.ReadMemory(root, 0x31000008)).to.equal(0xaabbccdd)
35 36

    // offset 0 isn't loaded
George Hotz's avatar
George Hotz committed
37
    await expect(mm.ReadMemory(root, 0x31000004)).to.be.reverted;
38 39 40 41 42 43
  })

  it("misaligned oracle", async function() {
    root = await loadPreimageAndSelect(mm, [0x11,0x22,0x33,0x44,0xaa,0xbb,0xcc], 4)
    expect(await mm.ReadMemory(root, 0x31000000)).to.equal(7)
    expect(await mm.ReadMemory(root, 0x31000008)).to.equal(0xaabbcc00)
44 45
  })
})