diff --git a/src/masternodes/rpc_masternodes.cpp b/src/masternodes/rpc_masternodes.cpp index d1c63ef814..1b9e26004a 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", @@ -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,13 +816,17 @@ UniValue getmasternodeblocks(const JSONRPCRequest& request) { if (blockHeight <= creationHeight) { return true; } - - if (auto tip = ::ChainActive()[blockHeight]) { + if (blockHeight <= startBlock) { + return false; + } + auto tip = ::ChainActive()[blockHeight]; + if (tip && depth > 0) { lastHeight = tip->nHeight; ret.pushKV(std::to_string(lastHeight), tip->GetBlockHash().ToString()); + depth--; } - return true; + return depth != 0; }; pcustomcsview->ForEachSubNode([&](const SubNodeBlockTimeKey &key, CLazySerialize){ @@ -833,7 +839,7 @@ UniValue getmasternodeblocks(const JSONRPCRequest& request) { auto tip = ::ChainActive()[std::min(lastHeight, Params().GetConsensus().DakotaCrescentHeight) - 1]; - for (; tip && tip->nHeight > creationHeight && depth > 0; tip = tip->pprev, --depth) { + for (; tip && tip->nHeight > creationHeight && depth > 0 && tip->nHeight > startBlock; tip = tip->pprev, --depth) { auto id = pcustomcsview->GetMasternodeIdByOperator(tip->minterKey()); if (id && *id == mn_id) { ret.pushKV(std::to_string(tip->nHeight), tip->GetBlockHash().ToString()); diff --git a/test/functional/rpc_mn_basic.py b/test/functional/rpc_mn_basic.py index 509f133580..326f56ee18 100644 --- a/test/functional/rpc_mn_basic.py +++ b/test/functional/rpc_mn_basic.py @@ -207,6 +207,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')