Commit 0daac0f6 authored by George Hotz's avatar George Hotz

CallWithTrieNodes in progress

parent 20112775
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
pragma solidity ^0.7.3; pragma solidity ^0.7.3;
pragma experimental ABIEncoderV2;
import "./lib/Lib_RLPReader.sol"; import "./lib/Lib_RLPReader.sol";
import "hardhat/console.sol"; import "hardhat/console.sol";
...@@ -10,6 +11,7 @@ interface IMIPS { ...@@ -10,6 +11,7 @@ interface IMIPS {
} }
interface IMIPSMemory { interface IMIPSMemory {
function AddTrieNode(bytes calldata anything) external;
function ReadMemory(bytes32 stateHash, uint32 addr) external view returns (uint32); function ReadMemory(bytes32 stateHash, uint32 addr) external view returns (uint32);
function ReadBytes32(bytes32 stateHash, uint32 addr) external view returns (bytes32); function ReadBytes32(bytes32 stateHash, uint32 addr) external view returns (bytes32);
function WriteMemory(bytes32 stateHash, uint32 addr, uint32 val) external returns (bytes32); function WriteMemory(bytes32 stateHash, uint32 addr, uint32 val) external returns (bytes32);
...@@ -74,6 +76,22 @@ contract Challenge { ...@@ -74,6 +76,22 @@ contract Challenge {
return challengeId; return challengeId;
} }
// helper function to determine what nodes we need
function CallWithTrieNodes(bytes calldata dat, bytes[] calldata nodes) public {
for (uint i = 0; i < nodes.length; i++) {
mem.AddTrieNode(nodes[i]);
}
(bool success, bytes memory revertData) = address(this).call(dat);
// TODO: better way to revert?
if (!success) {
uint256 revertDataLength = revertData.length;
assembly {
let revertDataStart := add(revertData, 32)
revert(revertDataStart, revertDataLength)
}
}
}
function InitiateChallenge(uint blockNumberN, bytes calldata blockHeaderNp1, function InitiateChallenge(uint blockNumberN, bytes calldata blockHeaderNp1,
bytes32 assertionRoot, bytes32 finalSystemState, uint256 stepCount) external returns (uint256) { bytes32 assertionRoot, bytes32 finalSystemState, uint256 stepCount) external returns (uint256) {
bytes32 computedBlockHash = keccak256(blockHeaderNp1); bytes32 computedBlockHash = keccak256(blockHeaderNp1);
......
...@@ -111,13 +111,7 @@ library Lib_MerkleTrie { ...@@ -111,13 +111,7 @@ library Lib_MerkleTrie {
node[i] = bytes1(uint8(node[i]) + uint8(0x61-10)); node[i] = bytes1(uint8(node[i]) + uint8(0x61-10));
} }
} }
// Error(string) revert(string(node));
bytes memory revertData = abi.encodeWithSelector(0x08c379a0, string(node));
uint256 revertDataLength = revertData.length;
assembly {
let revertDataStart := add(revertData, 32)
revert(revertDataStart, revertDataLength)
}
} }
require(keccak256(encoded) == nodeId, "bad hash in trie lookup"); require(keccak256(encoded) == nodeId, "bad hash in trie lookup");
return getRawNode(encoded); return getRawNode(encoded);
......
...@@ -26,23 +26,23 @@ describe("Challenge contract", function () { ...@@ -26,23 +26,23 @@ describe("Challenge contract", function () {
const assertionRoot = "0x9e0261efe4509912b8862f3d45a0cb8404b99b239247df9c55871bd3844cebbd" const assertionRoot = "0x9e0261efe4509912b8862f3d45a0cb8404b99b239247df9c55871bd3844cebbd"
let startTrie = JSON.parse(fs.readFileSync("/tmp/cannon/golden.json")) let startTrie = JSON.parse(fs.readFileSync("/tmp/cannon/golden.json"))
let finalTrie = JSON.parse(fs.readFileSync("/tmp/cannon/0_13284469/checkpoint_85059435.json")) let finalTrie = JSON.parse(fs.readFileSync("/tmp/cannon/0_13284469/checkpoint_final.json"))
let preimages = Object.assign({}, startTrie['preimages'], finalTrie['preimages']);
const finalSystemState = finalTrie['root'] const finalSystemState = finalTrie['root']
let cdat = c.interface.encodeFunctionData("InitiateChallenge", [blockNumberN, blockNp1Rlp, assertionRoot, finalSystemState, 1])
while (1) { while (1) {
try { try {
// TODO: make this eth call? // TODO: make this eth call?
// needs something like InitiateChallengeWithTrieNodes // needs something like InitiateChallengeWithTrieNodesj
await c.InitiateChallenge(blockNumberN, blockNp1Rlp, assertionRoot, finalSystemState, 1) await c.CallWithTrieNodes(cdat, [])
break break
} catch(e) { } catch(e) {
const missing = e.toString().split("'")[1] const missing = e.toString().split("'")[1]
if (missing.length == 64) { if (missing.length == 64) {
console.log("requested node", missing) console.log("requested node", missing)
let node = startTrie['preimages']["0x"+missing] let node = preimages["0x"+missing]
if (node === undefined) {
node = finalTrie['preimages']["0x"+missing]
}
expect(node).to.not.be.an('undefined') expect(node).to.not.be.an('undefined')
const bin = Uint8Array.from(Buffer.from(node, 'base64').toString('binary'), c => c.charCodeAt(0)) const bin = Uint8Array.from(Buffer.from(node, 'base64').toString('binary'), c => c.charCodeAt(0))
await mm.AddTrieNode(bin) await mm.AddTrieNode(bin)
......
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