Skip to content

Commit

Permalink
Order getmasternodeblocks results (#1388)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bushstar authored Jul 19, 2022
1 parent 6d5d5a9 commit 8a92bcd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/masternodes/masternodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,15 @@ class CMasternodesView : public virtual CStorageView
void SetMasternodeLastBlockTime(const CKeyID & minter, const uint32_t &blockHeight, const int64_t &time);
std::optional<int64_t> GetMasternodeLastBlockTime(const CKeyID & minter, const uint32_t height);
void EraseMasternodeLastBlockTime(const uint256 &minter, const uint32_t& blockHeight);
void ForEachMinterNode(std::function<bool(MNBlockTimeKey const &, CLazySerialize<int64_t>)> callback, MNBlockTimeKey const & start = {});
void ForEachMinterNode(std::function<bool(MNBlockTimeKey const &, CLazySerialize<int64_t>)> callback,
MNBlockTimeKey const & start = {uint256{}, std::numeric_limits<uint32_t>::max()});

// Subnode block times
void SetSubNodesBlockTime(const CKeyID & minter, const uint32_t &blockHeight, const uint8_t id, const int64_t& time);
std::vector<int64_t> GetSubNodesBlockTime(const CKeyID & minter, const uint32_t height);
void EraseSubNodesLastBlockTime(const uint256& nodeId, const uint32_t& blockHeight);
void ForEachSubNode(std::function<bool(SubNodeBlockTimeKey const &, CLazySerialize<int64_t>)> callback, SubNodeBlockTimeKey const & start = {});
void ForEachSubNode(std::function<bool(SubNodeBlockTimeKey const &, CLazySerialize<int64_t>)> callback,
SubNodeBlockTimeKey const & start = {uint256{}, uint8_t{}, std::numeric_limits<uint32_t>::max()});

uint16_t GetTimelock(const uint256& nodeId, const CMasternode& node, const uint64_t height) const;

Expand Down
14 changes: 10 additions & 4 deletions src/masternodes/rpc_masternodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ UniValue getmasternodeblocks(const JSONRPCRequest& request) {

UniValue identifier = request.params[0].get_obj();
int idCount{0};
uint256 mn_id;
uint256 mn_id{};

if (!identifier["id"].isNull()) {
mn_id = ParseHashV(identifier["id"], "id");
Expand Down Expand Up @@ -652,7 +652,8 @@ UniValue getmasternodeblocks(const JSONRPCRequest& request) {
if (!request.params[1].isNull()) {
depth = request.params[1].get_int();
}
UniValue ret(UniValue::VOBJ);

std::map<uint32_t, uint256, std::greater<>> mintedBlocks;
auto currentHeight = ::ChainActive().Height();
depth = std::min(depth, currentHeight);
auto startBlock = currentHeight - depth;
Expand All @@ -671,7 +672,7 @@ UniValue getmasternodeblocks(const JSONRPCRequest& request) {
auto tip = ::ChainActive()[blockHeight];
if (tip && depth > 0) {
lastHeight = tip->nHeight;
ret.pushKV(std::to_string(lastHeight), tip->GetBlockHash().ToString());
mintedBlocks.emplace(lastHeight, tip->GetBlockHash());
depth--;
}

Expand All @@ -691,10 +692,15 @@ UniValue getmasternodeblocks(const JSONRPCRequest& request) {
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());
mintedBlocks.emplace(tip->nHeight, tip->GetBlockHash());
}
}

UniValue ret(UniValue::VOBJ);
for (const auto& [height, hash] : mintedBlocks) {
ret.pushKV(std::to_string(height), hash.ToString());
}

return GetRPCResultCache().Set(request, ret);
}

Expand Down

0 comments on commit 8a92bcd

Please sign in to comment.