Skip to content

Commit

Permalink
Merge branch 'fix/getmasternodeblocks-depth'
Browse files Browse the repository at this point in the history
  • Loading branch information
prasannavl committed Dec 24, 2021
2 parents 4801205 + 23a66e8 commit 211b393
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/masternodes/rpc_masternodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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) {
Expand All @@ -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<int64_t>){
Expand All @@ -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());
Expand Down
3 changes: 3 additions & 0 deletions test/functional/rpc_mn_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down

0 comments on commit 211b393

Please sign in to comment.