From 6879367b98fc8704917c567acbf4ff03e3be4b2b Mon Sep 17 00:00:00 2001 From: Diego del Corral Tercero Date: Fri, 3 Dec 2021 13:20:13 +0100 Subject: [PATCH 1/3] Fix getmasternodeblocks depth filtering --- src/masternodes/rpc_masternodes.cpp | 7 ++++--- test/functional/rpc_mn_basic.py | 3 +++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/masternodes/rpc_masternodes.cpp b/src/masternodes/rpc_masternodes.cpp index 618813021b3..b1c5008e1f1 100644 --- a/src/masternodes/rpc_masternodes.cpp +++ b/src/masternodes/rpc_masternodes.cpp @@ -318,7 +318,7 @@ UniValue remforcedrewardaddress(const JSONRPCRequest& request) // Temporarily disabled for 2.2 throw JSONRPCError(RPC_INVALID_REQUEST, "reward address change is disabled for Fort Canning"); - + auto pwallet = GetWallet(request); RPCHelpMan{"remforcedrewardaddress", @@ -814,10 +814,11 @@ UniValue getmasternodeblocks(const JSONRPCRequest& request) { if (blockHeight <= creationHeight) { return true; } - - if (auto tip = ::ChainActive()[blockHeight]) { + auto tip = ::ChainActive()[blockHeight]; + if (tip && depth > 0) { lastHeight = tip->height; ret.pushKV(std::to_string(tip->height), tip->GetBlockHash().ToString()); + depth--; } return true; diff --git a/test/functional/rpc_mn_basic.py b/test/functional/rpc_mn_basic.py index b5e86d726de..23ec606d10e 100755 --- a/test/functional/rpc_mn_basic.py +++ b/test/functional/rpc_mn_basic.py @@ -205,6 +205,9 @@ def run_test(self): # test getmasternodeblocks self.nodes[0].generate(1) node0_keys = self.nodes[0].get_genesis_keys() + blocks = self.nodes[0].getmasternodeblocks({'operatorAddress': node0_keys.operatorAuthAddress}, 2) + assert_equal(len(blocks), 2) + blocks = self.nodes[0].getmasternodeblocks({'operatorAddress': node0_keys.operatorAuthAddress}) assert_equal(list(blocks.keys())[0], '162') From fc021e123b59af4492e71b0f4b179f2602d3e9c7 Mon Sep 17 00:00:00 2001 From: Diego del Corral Tercero Date: Fri, 3 Dec 2021 13:32:15 +0100 Subject: [PATCH 2/3] Check depth on lambda return --- src/masternodes/rpc_masternodes.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/masternodes/rpc_masternodes.cpp b/src/masternodes/rpc_masternodes.cpp index b1c5008e1f1..5b474b29006 100644 --- a/src/masternodes/rpc_masternodes.cpp +++ b/src/masternodes/rpc_masternodes.cpp @@ -821,7 +821,7 @@ UniValue getmasternodeblocks(const JSONRPCRequest& request) { depth--; } - return true; + return depth != 0; }; pcustomcsview->ForEachSubNode([&](const SubNodeBlockTimeKey &key, CLazySerialize){ From a3c3a8ec14252adfc70c3d3e8a4fb3ffd606835e Mon Sep 17 00:00:00 2001 From: Diego del Corral Tercero Date: Mon, 6 Dec 2021 15:52:19 +0100 Subject: [PATCH 3/3] Correct startBlock implementation for depth in getmasternodeblocks --- src/masternodes/rpc_masternodes.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/masternodes/rpc_masternodes.cpp b/src/masternodes/rpc_masternodes.cpp index 5b474b29006..ea7737fd3f4 100644 --- a/src/masternodes/rpc_masternodes.cpp +++ b/src/masternodes/rpc_masternodes.cpp @@ -803,8 +803,10 @@ UniValue getmasternodeblocks(const JSONRPCRequest& request) { if (!request.params[1].isNull()) { depth = request.params[1].get_int(); } - UniValue ret(UniValue::VOBJ); + auto currentHeight = ::ChainActive().Height(); + depth = std::min(depth, currentHeight); + auto startBlock = currentHeight - depth; auto masternodeBlocks = [&](const uint256& masternodeID, uint32_t blockHeight) { if (masternodeID != mn_id) { @@ -814,6 +816,9 @@ UniValue getmasternodeblocks(const JSONRPCRequest& request) { if (blockHeight <= creationHeight) { return true; } + if (blockHeight <= startBlock) { + return false; + } auto tip = ::ChainActive()[blockHeight]; if (tip && depth > 0) { lastHeight = tip->height; @@ -834,7 +839,7 @@ UniValue getmasternodeblocks(const JSONRPCRequest& request) { auto tip = ::ChainActive()[std::min(lastHeight, uint64_t(Params().GetConsensus().DakotaCrescentHeight)) - 1]; - for (; tip && tip->height > creationHeight && depth > 0; tip = tip->pprev, --depth) { + for (; tip && tip->height > creationHeight && depth > 0 && tip->height > startBlock; tip = tip->pprev, --depth) { auto id = pcustomcsview->GetMasternodeIdByOperator(tip->minterKey()); if (id && *id == mn_id) { ret.pushKV(std::to_string(tip->height), tip->GetBlockHash().ToString());