challenge.js 2.13 KB
Newer Older
1
const fs = require("fs")
2
const { basedir, deployed, getBlockRlp, getTrieNodesForCall } = require("../scripts/lib")
George Hotz's avatar
George Hotz committed
3 4 5 6 7 8

async function main() {
  let [c, m, mm] = await deployed()

  const blockNumberN = parseInt(process.env.BLOCK)
  if (isNaN(blockNumberN)) {
9
    throw "usage: BLOCK=<number> npx hardhat run challenge.js"
George Hotz's avatar
George Hotz committed
10 11 12
  }
  console.log("challenging block number", blockNumberN)
  // sadly this doesn't work on hosthat
13
  const blockNp1 = await network.provider.send("eth_getBlockByNumber", ["0x"+(blockNumberN+1).toString(16), false])
George Hotz's avatar
George Hotz committed
14 15 16 17
  console.log(blockNp1)
  const blockNp1Rlp = getBlockRlp(blockNp1)

  console.log(c.address, m.address, mm.address)
18 19

  // TODO: move this to lib, it's shared with the test
20
  let startTrie = JSON.parse(fs.readFileSync(basedir+"/golden.json"))
21

22 23 24 25 26 27 28 29
  const assertionRootBinary = fs.readFileSync(basedir+"/0_"+blockNumberN.toString()+"/output")
  var assertionRoot = "0x"
  for (var i=0; i<32; i++) {
    hex = assertionRootBinary[i].toString(16);
    assertionRoot += ("0"+hex).slice(-2);
  }
  console.log("asserting root", assertionRoot)
  let finalTrie = JSON.parse(fs.readFileSync(basedir+"/0_"+blockNumberN.toString()+"/checkpoint_final.json"))
30 31 32 33 34

  let preimages = Object.assign({}, startTrie['preimages'], finalTrie['preimages']);
  const finalSystemState = finalTrie['root']

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

  // run "on chain"
  for (n of nodes) {
    await mm.AddTrieNode(n)
  }
42 43 44 45
// TODO: Setting the gas limit explicitly here shouldn't be necessary, for some
//    weird reason (to be investigated), it is for L2.
//  let ret = await c.initiateChallenge(...args)
  let ret = await c.initiateChallenge(...args, { gasLimit: 10_000_000 })
46
  let receipt = await ret.wait()
47
  // ChallengeCreated event
48 49
  let challengeId = receipt.events[0].args['challengeId'].toNumber()
  console.log("new challenge with id", challengeId)
George Hotz's avatar
George Hotz committed
50 51 52 53 54 55 56 57
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });