feature: branchRoot

buddies-main-deployment
James Prestwich 4 years ago
parent 67ec81184f
commit a715cb3d55
No known key found for this signature in database
GPG Key ID: 519E010A79028CCC
  1. 24
      solidity/contracts/Merkle.sol
  2. 6
      solidity/contracts/Replica.sol

@ -13,18 +13,28 @@ library MerkleLib {
uint256 count;
}
function branchRoot(
bytes32 item,
bytes32[32] memory branch,
uint256 index,
bytes32[TREE_DEPTH] storage zero_hashes
) internal view returns (bytes32 node) {
uint256 idx = index;
node = item;
for (uint256 i = 0; i < TREE_DEPTH; i++) {
if ((idx & 1) == 1)
node = sha256(abi.encodePacked(branch[i], node));
else node = sha256(abi.encodePacked(node, zero_hashes[i]));
idx /= 2;
}
}
function root(Tree storage _tree, bytes32[TREE_DEPTH] storage zero_hashes)
internal
view
returns (bytes32 node)
{
uint256 size = _tree.count;
for (uint256 i = 0; i < TREE_DEPTH; i++) {
if ((size & 1) == 1)
node = sha256(abi.encodePacked(_tree.branch[i], node));
else node = sha256(abi.encodePacked(node, zero_hashes[i]));
size /= 2;
}
return branchRoot(bytes32(0), _tree.branch, _tree.count, zero_hashes);
}
function insert(Tree storage _tree, bytes32 node) internal {

@ -11,6 +11,8 @@ contract Replica is Common {
bytes32 pending;
uint256 confirmAt;
uint256 lastProcessed;
event DoubleUpdate();
constructor(
@ -18,11 +20,13 @@ contract Replica is Common {
uint32 _ownSLIP44,
address _updater,
uint256 _optimisticSeconds,
bytes32 _start
bytes32 _start,
uint256 _lastProcessed
) Common(_originSLIP44, _updater) {
ownSLIP44 = _ownSLIP44;
optimisticSeconds = _optimisticSeconds;
current = _start;
lastProcessed = _lastProcessed;
}
function fail() internal override {

Loading…
Cancel
Save