Commit 34b1fedf authored by George Hotz's avatar George Hotz

factor out getTrieNodesForCall

parent 0daac0f6
const fs = require("fs")
const { hasUncaughtExceptionCaptureCallback } = require("process")
async function deploy() {
const MIPS = await ethers.getContractFactory("MIPS")
......@@ -15,4 +16,37 @@ async function deploy() {
return [c,m,mm]
}
module.exports = { deploy }
async function getTrieNodesForCall(c, cdat, preimages) {
let nodes = []
while (1) {
try {
// TODO: make this eth call?
// needs something like InitiateChallengeWithTrieNodesj
let calldata = c.interface.encodeFunctionData("CallWithTrieNodes", [cdat, nodes])
ret = await ethers.provider.call({
to:c.address,
data:calldata
});
console.log(ret)
break
} catch(e) {
const missing = e.toString().split("'")[1]
if (missing.length == 64) {
console.log("requested node", missing)
let node = preimages["0x"+missing]
if (node === undefined) {
throw("node not found")
}
const bin = Uint8Array.from(Buffer.from(node, 'base64').toString('binary'), c => c.charCodeAt(0))
nodes.push(bin)
continue
} else {
console.log(e)
break
}
}
}
return nodes
}
module.exports = { deploy, getTrieNodesForCall }
const { expect } = require("chai")
const fs = require("fs")
const { deploy } = require("../scripts/lib")
const { deploy, getTrieNodesForCall } = require("../scripts/lib")
describe("Challenge contract", function () {
if (!fs.existsSync("/tmp/cannon/golden.json")) {
......@@ -20,7 +20,7 @@ describe("Challenge contract", function () {
const blockchain = hardhat._node._blockchain
// get data
const blockNumberN = (await ethers.provider.getBlockNumber())-1;
const blockNumberN = (await ethers.provider.getBlockNumber())-2;
const blockNp1 = blockchain._data._blocksByNumber.get(blockNumberN+1)
const blockNp1Rlp = blockNp1.header.serialize()
......@@ -30,31 +30,14 @@ describe("Challenge contract", function () {
let preimages = Object.assign({}, startTrie['preimages'], finalTrie['preimages']);
const finalSystemState = finalTrie['root']
let cdat = c.interface.encodeFunctionData("InitiateChallenge", [blockNumberN, blockNp1Rlp, assertionRoot, finalSystemState, 1])
let args = [blockNumberN, blockNp1Rlp, assertionRoot, finalSystemState, 1]
let cdat = c.interface.encodeFunctionData("InitiateChallenge", args)
let nodes = await getTrieNodesForCall(c, cdat, preimages)
while (1) {
try {
// TODO: make this eth call?
// needs something like InitiateChallengeWithTrieNodesj
await c.CallWithTrieNodes(cdat, [])
break
} catch(e) {
const missing = e.toString().split("'")[1]
if (missing.length == 64) {
console.log("requested node", missing)
let node = preimages["0x"+missing]
expect(node).to.not.be.an('undefined')
const bin = Uint8Array.from(Buffer.from(node, 'base64').toString('binary'), c => c.charCodeAt(0))
await mm.AddTrieNode(bin)
continue
} else {
console.log(e)
break
}
}
// run "on chain"
for (n of nodes) {
await mm.AddTrieNode(n)
}
//const blockHeaderNp1 = getBlockRlp(await ethers.provider.getBlock(blockNumberN+1));
//console.log(blockNumberN, blockHeaderNp1);
await c.InitiateChallenge(...args)
})
})
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment