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

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

  const challengeId = parseInt(process.env.ID)
8 9
  const blockNumberN = parseInt(process.env.BLOCK)
  const isChallenger = process.env.CHALLENGER == "1"
George Hotz's avatar
George Hotz committed
10 11

  let step = (await c.getStepNumber(challengeId)).toNumber()
12
  console.log("searching step", step, "at block", blockNumberN)
13

14 15 16 17
  if (!(await c.isSearching(challengeId))) {
    console.log("search is done")
    return
  }
George Hotz's avatar
George Hotz committed
18

19 20 21 22 23 24 25 26 27
  // see if it's proposed or not
  const proposed = await c.getProposedState(challengeId)
  const isProposing = proposed == "0x0000000000000000000000000000000000000000000000000000000000000000"
  if (isProposing != isChallenger) {
    console.log("bad challenger state")
    return
  }
  console.log("isProposing", isProposing)
  let thisTrie = getTrieAtStep(blockNumberN, step)
George Hotz's avatar
George Hotz committed
28 29 30
  const root = thisTrie['root']
  console.log("new root", root)

31 32 33 34 35
  let ret
  if (isProposing) {
    ret = await c.ProposeState(challengeId, root)
  } else {
    ret = await c.RespondState(challengeId, root)
36
  }
37 38
  let receipt = await ret.wait()
  console.log("done", receipt.blockNumber)
George Hotz's avatar
George Hotz committed
39 40 41 42 43 44 45 46
}

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