Commit 5c5305de authored by George Hotz's avatar George Hotz

refactor to revertWithHex, and nil -> null

parent 3e56fb71
...@@ -19,17 +19,8 @@ contract MIPSMemory { ...@@ -19,17 +19,8 @@ contract MIPSMemory {
mapping(bytes32 => Preimage) public preimage; mapping(bytes32 => Preimage) public preimage;
// TODO: can share code with getTrieNode
function MissingPreimageRevert(bytes32 outhash, uint offset) internal pure { function MissingPreimageRevert(bytes32 outhash, uint offset) internal pure {
bytes memory node = Lib_BytesUtils.toNibbles(abi.encodePacked(outhash, offset)); Lib_BytesUtils.revertWithHex(abi.encodePacked(outhash, offset));
for (uint i = 0; i < node.length; i++) {
if (node[i] < bytes1(uint8(10))) {
node[i] = bytes1(uint8(node[i]) + uint8(0x30));
} else {
node[i] = bytes1(uint8(node[i]) + uint8(0x61-10));
}
}
revert(string(node));
} }
function GetPreimageLength(bytes32 outhash) public view returns (uint32) { function GetPreimageLength(bytes32 outhash) public view returns (uint32) {
......
...@@ -253,6 +253,23 @@ library Lib_BytesUtils { ...@@ -253,6 +253,23 @@ library Lib_BytesUtils {
return tempAddress; return tempAddress;
} }
function revertWithHex(
bytes memory _bytes
)
internal
pure
{
bytes memory node = Lib_BytesUtils.toNibbles(_bytes);
for (uint i = 0; i < node.length; i++) {
if (node[i] < bytes1(uint8(10))) {
node[i] = bytes1(uint8(node[i]) + uint8(0x30));
} else {
node[i] = bytes1(uint8(node[i]) + uint8(0x61-10));
}
}
revert(string(node));
}
function toNibbles( function toNibbles(
bytes memory _bytes bytes memory _bytes
) )
......
...@@ -103,15 +103,7 @@ library Lib_MerkleTrie { ...@@ -103,15 +103,7 @@ library Lib_MerkleTrie {
function getTrieNode(bytes32 nodeId) private view returns (TrieNode memory) { function getTrieNode(bytes32 nodeId) private view returns (TrieNode memory) {
bytes memory encoded = GetTrie()[nodeId]; bytes memory encoded = GetTrie()[nodeId];
if (encoded.length == 0) { if (encoded.length == 0) {
bytes memory node = Lib_BytesUtils.toNibbles(abi.encodePacked(nodeId)); Lib_BytesUtils.revertWithHex(abi.encodePacked(nodeId));
for (uint i = 0; i < node.length; i++) {
if (node[i] < bytes1(uint8(10))) {
node[i] = bytes1(uint8(node[i]) + uint8(0x30));
} else {
node[i] = bytes1(uint8(node[i]) + uint8(0x61-10));
}
}
revert(string(node));
} }
require(keccak256(encoded) == nodeId, "bad hash in trie lookup"); require(keccak256(encoded) == nodeId, "bad hash in trie lookup");
return getRawNode(encoded); return getRawNode(encoded);
......
...@@ -56,7 +56,7 @@ describe("Exec with trie dynamic", function () { ...@@ -56,7 +56,7 @@ describe("Exec with trie dynamic", function () {
it("add should work", async function () { it("add should work", async function () {
let root = trieAdd['root'] let root = trieAdd['root']
for (let i = 0; i < 12; i++) { for (let i = 0; i < 12; i++) {
root = await dynamicExecWithTrie(c, m, mm, root, trieAdd['preimages'], nil) root = await dynamicExecWithTrie(c, m, mm, root, trieAdd['preimages'], null)
console.log(i, root) console.log(i, root)
} }
}) })
......
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