challenge_test.js 2 KB
Newer Older
George Hotz's avatar
George Hotz committed
1 2
const { expect } = require("chai")
const fs = require("fs")
3
const { deploy, getTrieNodesForCall } = require("../scripts/lib")
4

5
describe("Challenge contract", function () {
George Hotz's avatar
George Hotz committed
6 7 8 9 10
  if (!fs.existsSync("/tmp/cannon/golden.json")) {
    console.log("golden file doesn't exist, skipping test")
    return
  }

11
  beforeEach(async function () {
George Hotz's avatar
George Hotz committed
12
    [c, m, mm] = await deploy()
13 14 15 16
  })
  it("challenge contract deploys", async function() {
    console.log("Challenge deployed at", c.address)
  })
17 18 19 20 21 22
  it("initiate challenge", async function() {
    // TODO: is there a better way to get the "HardhatNetworkProvider"?
    const hardhat = network.provider._wrapped._wrapped._wrapped._wrapped._wrapped
    const blockchain = hardhat._node._blockchain

    // get data
George Hotz's avatar
George Hotz committed
23
    const blockNumberN = (await ethers.provider.getBlockNumber())-2
24 25 26
    const blockNp1 = blockchain._data._blocksByNumber.get(blockNumberN+1)
    const blockNp1Rlp = blockNp1.header.serialize()

27
    const assertionRoot = "0x9e0261efe4509912b8862f3d45a0cb8404b99b239247df9c55871bd3844cebbd"
George Hotz's avatar
George Hotz committed
28
    let startTrie = JSON.parse(fs.readFileSync("/tmp/cannon/golden.json"))
29 30
    let finalTrie = JSON.parse(fs.readFileSync("/tmp/cannon/0_13284469/checkpoint_final.json"))
    let preimages = Object.assign({}, startTrie['preimages'], finalTrie['preimages']);
George Hotz's avatar
George Hotz committed
31
    const finalSystemState = finalTrie['root']
32

George Hotz's avatar
George Hotz committed
33
    let args = [blockNumberN, blockNp1Rlp, assertionRoot, finalSystemState, finalTrie['step']]
34
    let cdat = c.interface.encodeFunctionData("InitiateChallenge", args)
35
    let nodes = await getTrieNodesForCall(c, c.address, cdat, preimages)
36

37 38 39
    // run "on chain"
    for (n of nodes) {
      await mm.AddTrieNode(n)
40
    }
41
    let ret = await c.InitiateChallenge(...args)
George Hotz's avatar
George Hotz committed
42 43 44 45
    let receipt = await ret.wait()
    // ChallengeCreate event
    let challengeId = receipt.events[0].args['challengeId'].toNumber()
    console.log("new challenge with id", challengeId)
George Hotz's avatar
George Hotz committed
46 47 48

    // the real issue here is from step 0->1 when we write the input hash
    // TODO: prove the challenger wrong?
George Hotz's avatar
George Hotz committed
49 50
  }).timeout(120000)
})