From 01b03a92062e4da93e29d761b4f809d56579fb9a Mon Sep 17 00:00:00 2001 From: Steven Roose Date: Tue, 29 Jan 2019 12:30:21 +0000 Subject: [PATCH] Add calcfastmerkleroot RPC and test-framework support --- src/rpc/misc.cpp | 18 ++++++++++++++++ test/functional/rpc_calcfastmerkleroot.py | 25 +++++++++++++++++++++++ test/functional/test_framework/util.py | 7 +++++++ test/functional/test_runner.py | 1 + 4 files changed, 51 insertions(+) create mode 100755 test/functional/rpc_calcfastmerkleroot.py diff --git a/src/rpc/misc.cpp b/src/rpc/misc.cpp index cbcb04a913..5bf4c4fce4 100644 --- a/src/rpc/misc.cpp +++ b/src/rpc/misc.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -525,6 +526,22 @@ UniValue getpakinfo(const JSONRPCRequest& request) return ret; } +UniValue calcfastmerkleroot(const JSONRPCRequest& request) +{ + std::vector 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 // @@ -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"}}, diff --git a/test/functional/rpc_calcfastmerkleroot.py b/test/functional/rpc_calcfastmerkleroot.py new file mode 100755 index 0000000000..0e5c9f9fec --- /dev/null +++ b/test/functional/rpc_calcfastmerkleroot.py @@ -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() diff --git a/test/functional/test_framework/util.py b/test/functional/test_framework/util.py index 23b2f5bc2c..f87df55d13 100644 --- a/test/functional/test_framework/util.py +++ b/test/functional/test_framework/util.py @@ -22,6 +22,13 @@ from .authproxy import AuthServiceProxy, JSONRPCException 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 ################## diff --git a/test/functional/test_runner.py b/test/functional/test_runner.py index 117e5c3884..9dea52901a 100755 --- a/test/functional/test_runner.py +++ b/test/functional/test_runner.py @@ -65,6 +65,7 @@ BASE_SCRIPTS = [ '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',