From 6a24178fdee3cc06d5179d68af7ceaeb67b45b36 Mon Sep 17 00:00:00 2001 From: Peter Bushnell Date: Mon, 1 Aug 2022 11:37:54 +0100 Subject: [PATCH 1/2] Fix depth argument in getmasternodeblocks --- src/masternodes/rpc_masternodes.cpp | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/masternodes/rpc_masternodes.cpp b/src/masternodes/rpc_masternodes.cpp index 61fee71e131..351a506746d 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()); } From edeff2811f17bdddb739acc7855b9a22c914be34 Mon Sep 17 00:00:00 2001 From: Peter Bushnell Date: Tue, 2 Aug 2022 08:21:17 +0100 Subject: [PATCH 2/2] Fix off-by-one error in getmasternodeblocks --- 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 351a506746d..c0198cc2e65 100644 --- a/src/masternodes/rpc_masternodes.cpp +++ b/src/masternodes/rpc_masternodes.cpp @@ -647,7 +647,7 @@ UniValue getmasternodeblocks(const JSONRPCRequest& request) { UniValue ret(UniValue::VOBJ); for (const auto& [height, hash] : mintedBlocks) { - if (height < currentHeight - depth) { + if (height <= currentHeight - depth) { break; } ret.pushKV(std::to_string(height), hash.ToString());