Skip to content

Commit

Permalink
New rpc call "masternodelist info" (bitcoin#1513)
Browse files Browse the repository at this point in the history
* add masternodelist info call

* safe version conversion function

* change default sentinel version value

* fix issues
  • Loading branch information
gladcow authored and UdjinM6 committed Jul 10, 2017
1 parent 9268a33 commit 70eb83a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/masternode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ void CMasternodeBroadcast::Relay()

CMasternodePing::CMasternodePing(CTxIn& vinNew) :
fSentinelIsCurrent(false),
nSentinelVersion(0)
nSentinelVersion(DEFAULT_SENTINEL_VERSION)
{
LOCK(cs_main);
if (!chainActive.Tip() || chainActive.Height() < 12) return;
Expand Down
9 changes: 8 additions & 1 deletion src/masternode.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ static const int MASTERNODE_POSE_BAN_MAX_SCORE = 5;
// The Masternode Ping Class : Contains a different serialize method for sending pings from masternodes throughout the network
//

// sentinel version before sentinel ping implementation
#define DEFAULT_SENTINEL_VERSION 0x010001

class CMasternodePing
{
public:
Expand All @@ -45,7 +48,7 @@ class CMasternodePing
sigTime(0),
vchSig(),
fSentinelIsCurrent(false),
nSentinelVersion(0)
nSentinelVersion(DEFAULT_SENTINEL_VERSION)
{}

CMasternodePing(CTxIn& vinNew);
Expand All @@ -59,7 +62,11 @@ class CMasternodePing
READWRITE(sigTime);
READWRITE(vchSig);
if(ser_action.ForRead() && (s.size() == 0))
{
fSentinelIsCurrent = false;
nSentinelVersion = DEFAULT_SENTINEL_VERSION;
return;
}
READWRITE(fSentinelIsCurrent);
READWRITE(nSentinelVersion);
}
Expand Down
19 changes: 18 additions & 1 deletion src/rpc/masternode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ UniValue masternodelist(const UniValue& params, bool fHelp)
if (params.size() == 2) strFilter = params[1].get_str();

if (fHelp || (
strMode != "activeseconds" && strMode != "addr" && strMode != "full" &&
strMode != "activeseconds" && strMode != "addr" && strMode != "full" && strMode != "info" &&
strMode != "lastseen" && strMode != "lastpaidtime" && strMode != "lastpaidblock" &&
strMode != "protocol" && strMode != "payee" && strMode != "rank" && strMode != "status"))
{
Expand All @@ -469,6 +469,8 @@ UniValue masternodelist(const UniValue& params, bool fHelp)
" addr - Print ip address associated with a masternode (can be additionally filtered, partial match)\n"
" full - Print info in format 'status protocol payee lastseen activeseconds lastpaidtime lastpaidblock IP'\n"
" (can be additionally filtered, partial match)\n"
" info - Print info in format 'status protocol payee lastseen activeseconds sentinelversion sentinelstate IP'\n"
" (can be additionally filtered, partial match)\n"
" lastpaidblock - Print the last block height a node was paid on the network\n"
" lastpaidtime - Print the last time a node was paid on the network\n"
" lastseen - Print timestamp of when a masternode was last seen on the network\n"
Expand Down Expand Up @@ -520,6 +522,21 @@ UniValue masternodelist(const UniValue& params, bool fHelp)
if (strFilter !="" && strFull.find(strFilter) == std::string::npos &&
strOutpoint.find(strFilter) == std::string::npos) continue;
obj.push_back(Pair(strOutpoint, strFull));
} else if (strMode == "info") {
std::ostringstream streamInfo;
streamInfo << std::setw(18) <<
mn.GetStatus() << " " <<
mn.nProtocolVersion << " " <<
CBitcoinAddress(mn.pubKeyCollateralAddress.GetID()).ToString() << " " <<
(int64_t)mn.lastPing.sigTime << " " << std::setw(8) <<
(int64_t)(mn.lastPing.sigTime - mn.sigTime) << " " <<
SafeIntVersionToString(mn.lastPing.nSentinelVersion) << " " <<
(mn.lastPing.fSentinelIsCurrent ? "current" : "expired") << " " <<
mn.addr.ToString();
std::string strInfo = streamInfo.str();
if (strFilter !="" && strInfo.find(strFilter) == std::string::npos &&
strOutpoint.find(strFilter) == std::string::npos) continue;
obj.push_back(Pair(strOutpoint, strInfo));
} else if (strMode == "lastpaidblock") {
if (strFilter !="" && strOutpoint.find(strFilter) == std::string::npos) continue;
obj.push_back(Pair(strOutpoint, mn.GetLastPaidBlock()));
Expand Down
13 changes: 13 additions & 0 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -990,3 +990,16 @@ std::string IntVersionToString(uint32_t nVersion)
}
return boost::join(tokens, ".");
}

std::string SafeIntVersionToString(uint32_t nVersion)
{
try
{
return IntVersionToString(nVersion);
}
catch(const std::bad_cast&)
{
return "Invalid version";
}
}

10 changes: 10 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,14 @@ uint32_t StringVersionToInt(const std::string& strVersion);
std::string IntVersionToString(uint32_t nVersion);


/**
* @brief Copy of the IntVersionToString, that returns "Invalid version" string
* instead of throwing std::bad_cast
* @param nVersion 4-byte unsigned integer, most significant byte is always 0
* @return version string in "x.x.x" format (last 3 bytes as version parts)
* or "Invalid version" if can't cast the given value
*/
std::string SafeIntVersionToString(uint32_t nVersion);


#endif // BITCOIN_UTIL_H

0 comments on commit 70eb83a

Please sign in to comment.