mips_test_memory.js 3.46 KB
Newer Older
George Hotz's avatar
George Hotz committed
1 2 3 4 5 6 7 8 9 10
const { expect } = require("chai");

async function write(mm, root, addr, data) {
  ret = await mm.WriteMemoryWithReceipt(root, addr, data)
  const receipt = await ret.wait()
  for (l of receipt.logs) {
    if (l.topics[0] == "0x86b89b5c9818dbbf520dd979a5f250d357508fe11b9511d4a43fd9bc6aa1be70") {
      root = l.data
    }
  }
George Hotz's avatar
George Hotz committed
11
  console.log("new hash", root)
George Hotz's avatar
George Hotz committed
12 13 14
  return root
}

15 16 17 18
function randint(n) {
  return Math.floor(Math.random() * n)
}

George Hotz's avatar
George Hotz committed
19
describe("MIPSMemory contract", function () {
George Hotz's avatar
George Hotz committed
20
  beforeEach(async function () {
George Hotz's avatar
George Hotz committed
21
    const MIPSMemory = await ethers.getContractFactory("MIPSMemory")
George Hotz's avatar
George Hotz committed
22
    mm = await MIPSMemory.deploy()
George Hotz's avatar
George Hotz committed
23
    await mm.AddTrieNode(new Uint8Array([0x80]))
George Hotz's avatar
George Hotz committed
24 25 26
  })
  it("write from new should work", async function() {
    let root = "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"
George Hotz's avatar
George Hotz committed
27

George Hotz's avatar
George Hotz committed
28
    root = await write(mm, root, 0, 1)
29 30 31 32
    root = await write(mm, root, 4, 2)

    expect(await mm.ReadMemory(root, 0)).to.equal(1)
    expect(await mm.ReadMemory(root, 4)).to.equal(2)
George Hotz's avatar
George Hotz committed
33 34
  })
  it("write three should work", async function() {
35 36
    await mm.AddTrieNode(new Uint8Array([0x80]))
    let root = "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"
George Hotz's avatar
George Hotz committed
37

George Hotz's avatar
George Hotz committed
38
    root = await write(mm, root, 0, 1)
39 40 41 42 43 44
    root = await write(mm, root, 4, 2)
    root = await write(mm, root, 0x40, 3)

    expect(await mm.ReadMemory(root, 0)).to.equal(1)
    expect(await mm.ReadMemory(root, 4)).to.equal(2)
    expect(await mm.ReadMemory(root, 0x40)).to.equal(3)
George Hotz's avatar
George Hotz committed
45
  })
George Hotz's avatar
George Hotz committed
46
  it("write other three should work", async function() {
George Hotz's avatar
George Hotz committed
47 48 49 50 51 52 53 54 55
    let root = "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"

    root = await write(mm, root, 0x7fffd00c, 1)
    root = await write(mm, root, 0x7fffd010, 2)
    root = await write(mm, root, 0x7fffcffc, 3)

    expect(await mm.ReadMemory(root, 0x7fffd00c)).to.equal(1)
    expect(await mm.ReadMemory(root, 0x7fffd010)).to.equal(2)
    expect(await mm.ReadMemory(root, 0x7fffcffc)).to.equal(3)
George Hotz's avatar
George Hotz committed
56
  })
George Hotz's avatar
George Hotz committed
57
  it("bug found fuzzing 1", async function() {
58
    let root = "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"
George Hotz's avatar
George Hotz committed
59 60 61
    root = await write(mm, root, 0, 0)
    root = await write(mm, root, 0, 1)
    root = await write(mm, root, 0, 2)
George Hotz's avatar
George Hotz committed
62
  })
63
  it("fuzzing should be okay", async function() {
George Hotz's avatar
George Hotz committed
64 65
    let root = "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"
    let kv = {}
66 67

    for (var i = 0; i < 100; i++) {
George Hotz's avatar
George Hotz committed
68
      const keys = Object.keys(kv)
69
      const choice = Math.random()
George Hotz's avatar
George Hotz committed
70
      if (choice < 0.3 || keys.length == 0) {
71
        // write new key
George Hotz's avatar
George Hotz committed
72
        const key = randint(0x100)*4
73
        const value = randint(0x100000000)
George Hotz's avatar
George Hotz committed
74
        console.log("writing", key, value)
75
        root = await write(mm, root, key, value)
George Hotz's avatar
George Hotz committed
76
        kv[key] = value
George Hotz's avatar
George Hotz committed
77 78 79 80 81 82 83
      } else if (choice < 0.5) {
        // write new high key
        const key = randint(0x100)*4 + 0x10000000
        const value = randint(0x100000000)
        console.log("writing", key, value)
        root = await write(mm, root, key, value)
        kv[key] = value
84 85 86 87
      } else if (choice > 0.7) {
        // read old key
        const idx = randint(keys.length)
        const key = keys[idx]
George Hotz's avatar
George Hotz committed
88 89
        console.log("reading", key)
        expect(await mm.ReadMemory(root, key)).to.equal(kv[key])
90 91 92 93 94
      } else {
        // rewrite old key
        const idx = randint(keys.length)
        const key = keys[idx]
        const value = randint(0x100000000)
George Hotz's avatar
George Hotz committed
95
        console.log("writing", key, value)
96
        root = await write(mm, root, key, value)
George Hotz's avatar
George Hotz committed
97
        kv[key] = value
98 99
      }
    }
100
  }).timeout(60000)
George Hotz's avatar
George Hotz committed
101
})