diff --git a/src/masternodes/rpc_masternodes.cpp b/src/masternodes/rpc_masternodes.cpp index 61fee71e13..c0198cc2e6 100644 --- a/src/masternodes/rpc_masternodes.cpp +++ b/src/masternodes/rpc_masternodes.cpp @@ -598,16 +598,15 @@ UniValue getmasternodeblocks(const JSONRPCRequest& request) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Masternode not found"); } - auto lastHeight = ::ChainActive().Tip()->nHeight + 1; - const auto creationHeight = masternode->creationHeight; - int depth{std::numeric_limits::max()}; if (!request.params[1].isNull()) { depth = request.params[1].get_int(); } - std::map> mintedBlocks; - auto currentHeight = ::ChainActive().Height(); + int lastHeight{}; + const auto creationHeight = masternode->creationHeight; + std::map> mintedBlocks; + const auto currentHeight = ::ChainActive().Height(); depth = std::min(depth, currentHeight); auto startBlock = currentHeight - depth; @@ -619,17 +618,14 @@ UniValue getmasternodeblocks(const JSONRPCRequest& request) { if (blockHeight <= creationHeight) { return true; } - if (blockHeight <= startBlock) { - return false; - } + auto tip = ::ChainActive()[blockHeight]; - if (tip && depth > 0) { + if (tip) { lastHeight = tip->nHeight; mintedBlocks.emplace(lastHeight, tip->GetBlockHash()); - depth--; } - return depth != 0; + return true; }; pcustomcsview->ForEachSubNode([&](const SubNodeBlockTimeKey &key, CLazySerialize){ @@ -642,7 +638,7 @@ UniValue getmasternodeblocks(const JSONRPCRequest& request) { auto tip = ::ChainActive()[std::min(lastHeight, Params().GetConsensus().DakotaCrescentHeight) - 1]; - for (; tip && tip->nHeight > creationHeight && depth > 0 && tip->nHeight > startBlock; tip = tip->pprev, --depth) { + for (; tip && tip->nHeight > creationHeight && tip->nHeight > startBlock; tip = tip->pprev) { auto id = pcustomcsview->GetMasternodeIdByOperator(tip->minterKey()); if (id && *id == mn_id) { mintedBlocks.emplace(tip->nHeight, tip->GetBlockHash()); @@ -651,6 +647,9 @@ UniValue getmasternodeblocks(const JSONRPCRequest& request) { UniValue ret(UniValue::VOBJ); for (const auto& [height, hash] : mintedBlocks) { + if (height <= currentHeight - depth) { + break; + } ret.pushKV(std::to_string(height), hash.ToString()); }