Skip to content

Commit

Permalink
Add calcfastmerkleroot RPC and test-framework support
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenroose committed Feb 13, 2019
1 parent 3f902b0 commit 01b03a9
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/rpc/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <chain.h>
#include <clientversion.h>
#include <consensus/merkle.h>
#include <core_io.h>
#include <crypto/ripemd160.h>
#include <key_io.h>
Expand Down Expand Up @@ -525,6 +526,22 @@ UniValue getpakinfo(const JSONRPCRequest& request)
return ret;
}

UniValue calcfastmerkleroot(const JSONRPCRequest& request)
{
std::vector<uint256> leaves;
for (const UniValue& leaf : request.params[0].get_array().getValues()) {
uint256 l;
l.SetHex(leaf.get_str());
leaves.push_back(l);
}

uint256 root = ComputeFastMerkleRoot(leaves);

UniValue ret(UniValue::VOBJ);
ret.setStr(root.GetHex());
return ret;
}


// END ELEMENTS CALLS
//
Expand All @@ -543,6 +560,7 @@ static const CRPCCommand commands[] =
// ELEMENTS:
{ "util", "getpakinfo", &getpakinfo, {}},
{ "util", "tweakfedpegscript", &tweakfedpegscript, {"claim_script"} },
{ "hidden", "calcfastmerkleroot", &calcfastmerkleroot, {"leaves"} },

/* Not shown in help */
{ "hidden", "setmocktime", &setmocktime, {"timestamp"}},
Expand Down
25 changes: 25 additions & 0 deletions test/functional/rpc_calcfastmerkleroot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env python3

from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal, calcfastmerkleroot
from test_framework import util

class CalcFastMerkleRoot(BitcoinTestFramework):
def set_test_params(self):
self.setup_clean_chain = True
self.num_nodes = 1

def run_test(self):
util.node_fastmerkle = self.nodes[0]

test_leaves = ["b66b041650db0f297b53f8d93c0e8706925bf3323f8c59c14a6fac37bfdcd06f", "99cb2fa68b2294ae133550a9f765fc755d71baa7b24389fed67d1ef3e5cb0255", "257e1b2fa49dd15724c67bac4df7911d44f6689860aa9f65a881ae0a2f40a303", "b67b0b9f093fa83d5e44b707ab962502b7ac58630e556951136196e65483bb80"]
test_roots = ["0000000000000000000000000000000000000000000000000000000000000000", "b66b041650db0f297b53f8d93c0e8706925bf3323f8c59c14a6fac37bfdcd06f", "f752938da0cb71c051aabdd5a86658e8d0b7ac00e1c2074202d8d2a79d8a6cf6", "245d364a28e9ad20d522c4a25ffc6a7369ab182f884e1c7dcd01aa3d32896bd3", "317d6498574b6ca75ee0368ec3faec75e096e245bdd5f36e8726fa693f775dfc"]

leaves = []
for i in range(4):
root = calcfastmerkleroot(leaves)
assert_equal(root, test_roots[i])
leaves.append(test_leaves[i])

if __name__ == '__main__':
CalcFastMerkleRoot().main()
7 changes: 7 additions & 0 deletions test/functional/test_framework/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@

logger = logging.getLogger("TestFramework.utils")

# This variable should be set to the node being used for CalcFastMerkleRoot calls
node_fastmerkle = None

def calcfastmerkleroot(leaves):
global node_fastmerkle
return node_fastmerkle.calcfastmerkleroot(leaves)

# Assert functions
##################

Expand Down
1 change: 1 addition & 0 deletions test/functional/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
'feature_block_v4.py',
'feature_pak.py',
'feature_blocksign.py',
'rpc_calcfastmerkleroot.py',
# Longest test should go first, to favor running tests in parallel
'feature_fee_estimation.py',
'wallet_hd.py',
Expand Down

0 comments on commit 01b03a9

Please sign in to comment.