Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix depth argument in getmasternodeblocks #1402

Merged
merged 3 commits into from
Aug 2, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions src/masternodes/rpc_masternodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>::max()};
if (!request.params[1].isNull()) {
depth = request.params[1].get_int();
}

std::map<uint32_t, uint256, std::greater<>> mintedBlocks;
auto currentHeight = ::ChainActive().Height();
int lastHeight{};
const auto creationHeight = masternode->creationHeight;
std::map<int, uint256, std::greater<>> mintedBlocks;
const auto currentHeight = ::ChainActive().Height();
depth = std::min(depth, currentHeight);
auto startBlock = currentHeight - depth;

Expand All @@ -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<int64_t>){
Expand All @@ -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());
Expand All @@ -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());
}

Expand Down