Skip to content

Commit

Permalink
[RPC] Add DMN support to listmasternodes
Browse files Browse the repository at this point in the history
  • Loading branch information
random-zebra committed May 4, 2021
1 parent 716e7ed commit f218357
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/evo/deterministicmns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,15 @@ void CDeterministicMN::ToJson(UniValue& obj) const
obj.pushKV("collateralHash", collateralOutpoint.hash.ToString());
obj.pushKV("collateralIndex", (int)collateralOutpoint.n);

std::string collateralAddressStr = "";
Coin coin;
if (GetUTXOCoin(collateralOutpoint, coin)) {
CTxDestination dest;
if (ExtractDestination(coin.out.scriptPubKey, dest)) {
obj.pushKV("collateralAddress", EncodeDestination(dest));
collateralAddressStr = EncodeDestination(dest);
}
}

obj.pushKV("collateralAddress", collateralAddressStr);
obj.pushKV("operatorReward", (double)nOperatorReward / 100);
obj.pushKV("dmnstate", stateObj);
}
Expand Down
38 changes: 35 additions & 3 deletions src/rpc/masternode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,25 @@ UniValue getcachedblockhashes(const JSONRPCRequest& request)
return ret;
}

UniValue listmasternodes(const JSONRPCRequest& request)
static inline bool filter(const std::string& str, const std::string& strFilter)
{
std::string strFilter = "";
return str.find(strFilter) != std::string::npos;
}

if (request.params.size() == 1) strFilter = request.params[0].get_str();
static inline bool filterMasternode(const UniValue& dmno, const std::string& strFilter, bool fEnabled)
{
return strFilter.empty() || (filter("ENABLED", strFilter) && fEnabled)
|| (filter("POSE_BANNED", strFilter) && !fEnabled)
|| (filter(dmno["proTxHash"].get_str(), strFilter))
|| (filter(dmno["collateralHash"].get_str(), strFilter))
|| (filter(dmno["collateralAddress"].get_str(), strFilter))
|| (filter(dmno["dmnstate"]["ownerAddress"].get_str(), strFilter))
|| (filter(dmno["dmnstate"]["operatorAddress"].get_str(), strFilter))
|| (filter(dmno["dmnstate"]["votingAddress"].get_str(), strFilter));
}

UniValue listmasternodes(const JSONRPCRequest& request)
{
if (request.fHelp || (request.params.size() > 1))
throw std::runtime_error(
"listmasternodes ( \"filter\" )\n"
Expand All @@ -114,6 +127,7 @@ UniValue listmasternodes(const JSONRPCRequest& request)
"\nArguments:\n"
"1. \"filter\" (string, optional) Filter search text. Partial match by txhash, status, or addr.\n"

// !TODO: update for DMNs
"\nResult:\n"
"[\n"
" {\n"
Expand All @@ -134,7 +148,25 @@ UniValue listmasternodes(const JSONRPCRequest& request)
"\nExamples:\n" +
HelpExampleCli("listmasternodes", "") + HelpExampleRpc("listmasternodes", ""));


const std::string& strFilter = request.params.size() > 0 ? request.params[0].get_str() : "";
UniValue ret(UniValue::VARR);

auto mnList = deterministicMNManager->GetListAtChainTip();
mnList.ForEachMN(false, [&](const CDeterministicMNCPtr& dmn) {
UniValue obj(UniValue::VOBJ);
dmn->ToJson(obj);
bool fEnabled = dmn->pdmnState->nPoSeBanHeight == -1;
if (filterMasternode(obj, strFilter, fEnabled)) {
// Added for backward compatibility. can be removed later.
obj.pushKV("txhash", obj["proTxHash"].get_str());

obj.pushKV("status", fEnabled ? "ENABLED" : "POSE_BANNED");
ret.push_back(obj);
}
});

// Legacy masternodes (!TODO: remove when transition to dmn is complete)
const CBlockIndex* chainTip = GetChainTip();
if (!chainTip) return "[]";
int nHeight = chainTip->nHeight;
Expand Down

0 comments on commit f218357

Please sign in to comment.