Commit 2ed9dcae authored by George Hotz's avatar George Hotz

start challenge script

parent 6a92fc24
...@@ -53,6 +53,15 @@ receipt count 5 hash 0xa2947195971207f3654f635af06f2ab5d3a57af7a834ac88446afd3e8 ...@@ -53,6 +53,15 @@ receipt count 5 hash 0xa2947195971207f3654f635af06f2ab5d3a57af7a834ac88446afd3e8
process done with hash 0x5c45998dfbf9ce70bcbb80574ed7a622922d2c775e0a2331fe5a8b8dcc99f490 -> 0x9e0261efe4509912b8862f3d45a0cb8404b99b239247df9c55871bd3844cebbd process done with hash 0x5c45998dfbf9ce70bcbb80574ed7a622922d2c775e0a2331fe5a8b8dcc99f490 -> 0x9e0261efe4509912b8862f3d45a0cb8404b99b239247df9c55871bd3844cebbd
``` ```
## Workflow
```
# testing on cheapeth
npx hardhat node
npx hardhat run scripts/deploy.js
BLOCK=13652175 npx hardhat run scripts/challenge.js
```
## State Oracle API ## State Oracle API
......
...@@ -4,8 +4,22 @@ ...@@ -4,8 +4,22 @@
require("@nomiclabs/hardhat-ethers"); require("@nomiclabs/hardhat-ethers");
require("hardhat-gas-reporter"); require("hardhat-gas-reporter");
const fs = require("fs")
const private = fs.readFileSync(process.env.HOME+"/.privatekey").toString()
module.exports = { module.exports = {
defaultNetwork: "cheapeth",
networks: {
hosthat: {
url: "http://localhost:8545/",
accounts: ["0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"]
},
cheapeth: {
url: "https://rpc.cheapeth.org/rpc",
accounts: [private]
},
},
solidity: { solidity: {
version: "0.7.3", version: "0.7.3",
settings: { settings: {
......
const { deployed, getBlockRlp } = require("../scripts/lib")
async function main() {
let [c, m, mm] = await deployed()
const blockNumberN = parseInt(process.env.BLOCK)
if (isNaN(blockNumberN)) {
throw "usage: challenge.js <block number>"
}
console.log("challenging block number", blockNumberN)
// sadly this doesn't work on hosthat
const blockNp1 = await network.provider.send("eth_getBlockByNumber", ["0x"+(blockNumberN+1).toString(16), true])
console.log(blockNp1)
const blockNp1Rlp = getBlockRlp(blockNp1)
console.log(c.address, m.address, mm.address)
// TODO: finish this
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
const fs = require("fs") const fs = require("fs")
const { hasUncaughtExceptionCaptureCallback } = require("process") const rlp = require('rlp')
async function deploy() { async function deploy() {
const MIPS = await ethers.getContractFactory("MIPS") const MIPS = await ethers.getContractFactory("MIPS")
...@@ -16,6 +16,45 @@ async function deploy() { ...@@ -16,6 +16,45 @@ async function deploy() {
return [c,m,mm] return [c,m,mm]
} }
function getBlockRlp(block) {
let dat = [
block['parentHash'],
block['sha3Uncles'],
block['miner'],
block['stateRoot'],
block['transactionsRoot'],
block['receiptsRoot'],
block['logsBloom'],
block['difficulty'],
block['number'],
block['gasLimit'],
block['gasUsed'],
block['timestamp'],
block['extraData'],
block['mixHash'],
block['nonce'],
];
// post london
if (block['baseFeePerGas'] !== undefined) {
dat.push(block['baseFeePerGas'])
}
dat = dat.map(x => (x == "0x0") ? "0x" : x)
//console.log(dat)
let rdat = rlp.encode(dat)
if (ethers.utils.keccak256(rdat) != block['hash']) {
throw "block hash doesn't match"
}
return rdat
}
async function deployed() {
let addresses = JSON.parse(fs.readFileSync("/tmp/cannon/deployed.json"))
const c = await ethers.getContractAt("Challenge", addresses["Challenge"])
const m = await ethers.getContractAt("MIPS", addresses["MIPS"])
const mm = await ethers.getContractAt("MIPSMemory", addresses["MIPSMemory"])
return [c,m,mm]
}
async function getTrieNodesForCall(c, cdat, preimages) { async function getTrieNodesForCall(c, cdat, preimages) {
let nodes = [] let nodes = []
while (1) { while (1) {
...@@ -48,4 +87,4 @@ async function getTrieNodesForCall(c, cdat, preimages) { ...@@ -48,4 +87,4 @@ async function getTrieNodesForCall(c, cdat, preimages) {
return nodes return nodes
} }
module.exports = { deploy, getTrieNodesForCall } module.exports = { deploy, deployed, getTrieNodesForCall, getBlockRlp }
...@@ -20,7 +20,7 @@ describe("Challenge contract", function () { ...@@ -20,7 +20,7 @@ describe("Challenge contract", function () {
const blockchain = hardhat._node._blockchain const blockchain = hardhat._node._blockchain
// get data // get data
const blockNumberN = (await ethers.provider.getBlockNumber())-2; const blockNumberN = (await ethers.provider.getBlockNumber())-2
const blockNp1 = blockchain._data._blocksByNumber.get(blockNumberN+1) const blockNp1 = blockchain._data._blocksByNumber.get(blockNumberN+1)
const blockNp1Rlp = blockNp1.header.serialize() const blockNp1Rlp = blockNp1.header.serialize()
......
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