challenge_test.js 2.14 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 6
// This test needs preimages to run correctly.
// It is skipped when running `make test_contracts`, but can be run with `make test_challenge`.
7
describe("Challenge contract", function () {
George Hotz's avatar
George Hotz committed
8 9 10 11 12
  if (!fs.existsSync("/tmp/cannon/golden.json")) {
    console.log("golden file doesn't exist, skipping test")
    return
  }

13
  beforeEach(async function () {
George Hotz's avatar
George Hotz committed
14
    [c, m, mm] = await deploy()
15 16 17 18
  })
  it("challenge contract deploys", async function() {
    console.log("Challenge deployed at", c.address)
  })
19 20 21 22 23 24
  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
25
    const blockNumberN = (await ethers.provider.getBlockNumber())-2
26 27 28
    const blockNp1 = blockchain._data._blocksByNumber.get(blockNumberN+1)
    const blockNp1Rlp = blockNp1.header.serialize()

29
    const assertionRoot = "0x9e0261efe4509912b8862f3d45a0cb8404b99b239247df9c55871bd3844cebbd"
George Hotz's avatar
George Hotz committed
30
    let startTrie = JSON.parse(fs.readFileSync("/tmp/cannon/golden.json"))
31 32
    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
33
    const finalSystemState = finalTrie['root']
34

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

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

    // 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
51 52
  }).timeout(120000)
})