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

Order getmasternodeblocks results #1388

Merged
merged 1 commit into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
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
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