Commit f622649e authored by George Hotz's avatar George Hotz

fix lib merkletrie

parent 455a4e5e
...@@ -6,6 +6,8 @@ import { Lib_BytesUtils } from "./Lib_BytesUtils.sol"; ...@@ -6,6 +6,8 @@ import { Lib_BytesUtils } from "./Lib_BytesUtils.sol";
import { Lib_RLPReader } from "./Lib_RLPReader.sol"; import { Lib_RLPReader } from "./Lib_RLPReader.sol";
import { Lib_RLPWriter } from "./Lib_RLPWriter.sol"; import { Lib_RLPWriter } from "./Lib_RLPWriter.sol";
import "hardhat/console.sol";
/** /**
* @title Lib_MerkleTrie * @title Lib_MerkleTrie
*/ */
...@@ -192,7 +194,7 @@ library Lib_MerkleTrie { ...@@ -192,7 +194,7 @@ library Lib_MerkleTrie {
bool _isFinalNode bool _isFinalNode
) )
{ {
// TODO: is 9 okay max length? // TODO: this is max length
_proof = new TrieNode[](9); _proof = new TrieNode[](9);
uint256 pathLength = 0; uint256 pathLength = 0;
...@@ -206,6 +208,9 @@ library Lib_MerkleTrie { ...@@ -206,6 +208,9 @@ library Lib_MerkleTrie {
// Proof is top-down, so we start at the first element (root). // Proof is top-down, so we start at the first element (root).
for (uint256 i = 0; i < _proof.length; i++) { for (uint256 i = 0; i < _proof.length; i++) {
if (currentNodeID == bytes32(RLP_NULL)) {
break;
}
if (currentNodeLength >= 32) { if (currentNodeLength >= 32) {
currentNode = getTrieNode(trie, currentNodeID); currentNode = getTrieNode(trie, currentNodeID);
} else { } else {
...@@ -319,7 +324,6 @@ library Lib_MerkleTrie { ...@@ -319,7 +324,6 @@ library Lib_MerkleTrie {
bytes memory _value bytes memory _value
) )
private private
pure
returns ( returns (
TrieNode[] memory _newPath TrieNode[] memory _newPath
) )
...@@ -329,6 +333,7 @@ library Lib_MerkleTrie { ...@@ -329,6 +333,7 @@ library Lib_MerkleTrie {
// Most of our logic depends on the status of the last node in the path. // Most of our logic depends on the status of the last node in the path.
TrieNode memory lastNode = _path[_pathLength - 1]; TrieNode memory lastNode = _path[_pathLength - 1];
NodeType lastNodeType = _getNodeType(lastNode); NodeType lastNodeType = _getNodeType(lastNode);
console.log(uint256(lastNodeType));
// Create an array for newly created nodes. // Create an array for newly created nodes.
// We need up to three new nodes, depending on the contents of the last node. // We need up to three new nodes, depending on the contents of the last node.
...@@ -441,7 +446,7 @@ library Lib_MerkleTrie { ...@@ -441,7 +446,7 @@ library Lib_MerkleTrie {
// We're dealing with an unnecessary extension node. // We're dealing with an unnecessary extension node.
// We're going to delete the node entirely. // We're going to delete the node entirely.
// Simply insert its current value into the branch index. // Simply insert its current value into the branch index.
newBranch = _editBranchIndex(newBranch, branchKey, _getNodeValue(lastNode)); newBranch = _editBranchIndexRLP(newBranch, branchKey, lastNode.decoded[lastNode.decoded.length-1]);
} }
} }
...@@ -906,6 +911,21 @@ library Lib_MerkleTrie { ...@@ -906,6 +911,21 @@ library Lib_MerkleTrie {
return _makeNode(_branch.decoded); return _makeNode(_branch.decoded);
} }
function _editBranchIndexRLP(
TrieNode memory _branch,
uint8 _index,
Lib_RLPReader.RLPItem memory encoded
)
private
pure
returns (
TrieNode memory _updatedNode
)
{
_branch.decoded[_index] = encoded;
return _makeNode(_branch.decoded);
}
/** /**
* @notice Utility; adds a prefix to a key. * @notice Utility; adds a prefix to a key.
* @param _key Key to prefix. * @param _key Key to prefix.
......
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