Skip to content

Commit a439e98

Browse files
gladcowUdjinM6
authored andcommitted
Replace watchdogs with ping (#1491)
* Add hassentinelping to governanceinfo * sentinelping rpc call * additional fields in mnp * sentinel ping implementation * change sentinel state to byte in mnp * use adjusted time in sentinel ping * update nTimeLastWatchdogVote if sentinel ping is actual * remove unused fields * bump protocol to 70207 * Fix small issues - fix the error message text in CActivbeMasternodeUpdateSentinelPing; - add empty string before public: in CActiveMasternode class declaration; - rename field sentinelPing in CMasternodePing to sentinelIsActual and change $ - decrease sentinelVersion field size to uint16_t; * revert proto bump for MIN_... consts * revert changes in getgovernanceinfo * Update mn vote time for remote masternodes - call UpdateWatchdogVoteTime in CMasternodeMan::ProcessMessage - deserialize masternodeping from the previous version archive without exception - add ability to set time in UpdateWatchdogVoteTime - set nTimeLastWatchdogVote to masternode ping sigTime if sentinel is actual - bump CMasternodeMan::SERIALIZATION_VERSION_STRING * remove mn state checks and add correct rpc param convertion * fix var names * Helper class for version in string and integer form * String version in sentinel ping Version format is "x.x.x" * test for bacward compatibility in serialization * Change VersionInfo class to convert functions
1 parent f65017c commit a439e98

15 files changed

+202
-7
lines changed

src/activemasternode.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ bool CActiveMasternode::SendMasternodePing()
108108
}
109109

110110
CMasternodePing mnp(vin);
111+
mnp.nSentinelVersion = nSentinelVersion;
112+
mnp.fSentinelIsCurrent =
113+
(abs(GetAdjustedTime() - nSentinelPingTime) < MASTERNODE_WATCHDOG_MAX_SECONDS);
111114
if(!mnp.Sign(keyMasternode, pubKeyMasternode)) {
112115
LogPrintf("CActiveMasternode::SendMasternodePing -- ERROR: Couldn't sign Masternode Ping\n");
113116
return false;
@@ -127,6 +130,14 @@ bool CActiveMasternode::SendMasternodePing()
127130
return true;
128131
}
129132

133+
bool CActiveMasternode::UpdateSentinelPing(int version)
134+
{
135+
nSentinelVersion = version;
136+
nSentinelPingTime = GetAdjustedTime();
137+
138+
return true;
139+
}
140+
130141
void CActiveMasternode::ManageStateInitial()
131142
{
132143
LogPrint("masternode", "CActiveMasternode::ManageStateInitial -- status = %s, type = %s, pinger enabled = %d\n", GetStatus(), GetTypeString(), fPingerEnabled);

src/activemasternode.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ class CActiveMasternode
4040
/// Ping Masternode
4141
bool SendMasternodePing();
4242

43+
// sentinel ping data
44+
int64_t nSentinelPingTime;
45+
uint32_t nSentinelVersion;
46+
4347
public:
4448
// Keys for the active Masternode
4549
CPubKey pubKeyMasternode;
@@ -52,6 +56,7 @@ class CActiveMasternode
5256
int nState; // should be one of ACTIVE_MASTERNODE_XXXX
5357
std::string strNotCapableReason;
5458

59+
5560
CActiveMasternode()
5661
: eType(MASTERNODE_UNKNOWN),
5762
fPingerEnabled(false),
@@ -69,6 +74,8 @@ class CActiveMasternode
6974
std::string GetStatus() const;
7075
std::string GetTypeString() const;
7176

77+
bool UpdateSentinelPing(int version);
78+
7279
private:
7380
void ManageStateInitial();
7481
void ManageStateRemote();

src/masternode.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,9 @@ void CMasternodeBroadcast::Relay()
735735
RelayInv(inv);
736736
}
737737

738-
CMasternodePing::CMasternodePing(CTxIn& vinNew)
738+
CMasternodePing::CMasternodePing(CTxIn& vinNew) :
739+
fSentinelIsCurrent(false),
740+
nSentinelVersion(0)
739741
{
740742
LOCK(cs_main);
741743
if (!chainActive.Tip() || chainActive.Height() < 12) return;
@@ -751,6 +753,7 @@ bool CMasternodePing::Sign(CKey& keyMasternode, CPubKey& pubKeyMasternode)
751753
std::string strError;
752754
std::string strMasterNodeSignMessage;
753755

756+
// TODO: add sentinel data
754757
sigTime = GetAdjustedTime();
755758
std::string strMessage = vin.ToString() + blockHash.ToString() + boost::lexical_cast<std::string>(sigTime);
756759

@@ -769,6 +772,7 @@ bool CMasternodePing::Sign(CKey& keyMasternode, CPubKey& pubKeyMasternode)
769772

770773
bool CMasternodePing::CheckSignature(CPubKey& pubKeyMasternode, int &nDos)
771774
{
775+
// TODO: add sentinel data
772776
std::string strMessage = vin.ToString() + blockHash.ToString() + boost::lexical_cast<std::string>(sigTime);
773777
std::string strError = "";
774778
nDos = 0;
@@ -909,10 +913,10 @@ void CMasternode::RemoveGovernanceObject(uint256 nGovernanceObjectHash)
909913
mapGovernanceObjectsVotedOn.erase(it);
910914
}
911915

912-
void CMasternode::UpdateWatchdogVoteTime()
916+
void CMasternode::UpdateWatchdogVoteTime(uint64_t nVoteTime)
913917
{
914918
LOCK(cs);
915-
nTimeLastWatchdogVote = GetTime();
919+
nTimeLastWatchdogVote = (nVoteTime == 0) ? GetTime() : nVoteTime;
916920
}
917921

918922
/**

src/masternode.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ static const int MASTERNODE_WATCHDOG_MAX_SECONDS = 120 * 60;
2323
static const int MASTERNODE_NEW_START_REQUIRED_SECONDS = 180 * 60;
2424

2525
static const int MASTERNODE_POSE_BAN_MAX_SCORE = 5;
26+
2627
//
2728
// The Masternode Ping Class : Contains a different serialize method for sending pings from masternodes throughout the network
2829
//
@@ -34,13 +35,17 @@ class CMasternodePing
3435
uint256 blockHash;
3536
int64_t sigTime; //mnb message times
3637
std::vector<unsigned char> vchSig;
38+
bool fSentinelIsCurrent; // true if last sentinel ping was actual
39+
uint32_t nSentinelVersion; // MSB is always 0, other 3 bits corresponds to x.x.x version scheme
3740
//removed stop
3841

3942
CMasternodePing() :
4043
vin(),
4144
blockHash(),
4245
sigTime(0),
43-
vchSig()
46+
vchSig(),
47+
fSentinelIsCurrent(false),
48+
nSentinelVersion(0)
4449
{}
4550

4651
CMasternodePing(CTxIn& vinNew);
@@ -53,6 +58,10 @@ class CMasternodePing
5358
READWRITE(blockHash);
5459
READWRITE(sigTime);
5560
READWRITE(vchSig);
61+
if(ser_action.ForRead() && (s.size() == 0))
62+
return;
63+
READWRITE(fSentinelIsCurrent);
64+
READWRITE(nSentinelVersion);
5665
}
5766

5867
void swap(CMasternodePing& first, CMasternodePing& second) // nothrow
@@ -66,6 +75,8 @@ class CMasternodePing
6675
swap(first.blockHash, second.blockHash);
6776
swap(first.sigTime, second.sigTime);
6877
swap(first.vchSig, second.vchSig);
78+
swap(first.fSentinelIsCurrent, second.fSentinelIsCurrent);
79+
swap(first.nSentinelVersion, second.nSentinelVersion);
6980
}
7081

7182
uint256 GetHash() const
@@ -318,7 +329,7 @@ class CMasternode
318329

319330
void RemoveGovernanceObject(uint256 nGovernanceObjectHash);
320331

321-
void UpdateWatchdogVoteTime();
332+
void UpdateWatchdogVoteTime(uint64_t nVoteTime = 0);
322333

323334
CMasternode& operator=(CMasternode from)
324335
{

src/masternodeman.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
/** Masternode manager */
1717
CMasternodeMan mnodeman;
1818

19-
const std::string CMasternodeMan::SERIALIZATION_VERSION_STRING = "CMasternodeMan-Version-4";
19+
const std::string CMasternodeMan::SERIALIZATION_VERSION_STRING = "CMasternodeMan-Version-5";
2020

2121
struct CompareLastPaidBlock
2222
{
@@ -849,6 +849,12 @@ void CMasternodeMan::ProcessMessage(CNode* pfrom, std::string& strCommand, CData
849849
// see if we have this Masternode
850850
CMasternode* pmn = mnodeman.Find(mnp.vin);
851851

852+
// if masternode uses sentinel ping instead of watchdog
853+
// we shoud update nTimeLastWatchdogVote here if sentinel
854+
// ping flag is actual
855+
if(pmn && mnp.fSentinelIsCurrent)
856+
pmn->UpdateWatchdogVoteTime(mnp.sigTime);
857+
852858
// too late, new MNANNOUNCE is required
853859
if(pmn && pmn->IsNewStartRequired()) return;
854860

@@ -1642,6 +1648,11 @@ void CMasternodeMan::SetMasternodeLastPing(const CTxIn& vin, const CMasternodePi
16421648
return;
16431649
}
16441650
pMN->lastPing = mnp;
1651+
// if masternode uses sentinel ping instead of watchdog
1652+
// we shoud update nTimeLastWatchdogVote here if sentinel
1653+
// ping flag is actual
1654+
if(mnp.fSentinelIsCurrent)
1655+
pMN->UpdateWatchdogVoteTime(mnp.sigTime);
16451656
mapSeenMasternodePing.insert(std::make_pair(mnp.GetHash(), mnp));
16461657

16471658
CMasternodeBroadcast mnb(*pMN);

src/rpc/governance.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -972,3 +972,4 @@ UniValue getsuperblockbudget(const UniValue& params, bool fHelp)
972972

973973
return strBudget;
974974
}
975+

src/rpc/masternode.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,3 +795,23 @@ UniValue masternodebroadcast(const UniValue& params, bool fHelp)
795795

796796
return NullUniValue;
797797
}
798+
799+
UniValue sentinelping(const UniValue& params, bool fHelp)
800+
{
801+
if (fHelp || params.size() != 1) {
802+
throw std::runtime_error(
803+
"sentinelping version\n"
804+
"\nSentinel ping.\n"
805+
"\nArguments:\n"
806+
"1. version (string, required) Sentinel version in the form \"x.x.x\"\n"
807+
"\nResult:\n"
808+
"state (boolean) Ping result\n"
809+
"\nExamples:\n"
810+
+ HelpExampleCli("sentinelping", "1.0.2")
811+
+ HelpExampleRpc("sentinelping", "1.0.2")
812+
);
813+
}
814+
815+
activeMasternode.UpdateSentinelPing(StringVersionToInt(params[0].get_str()));
816+
return true;
817+
}

src/rpc/server.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ static const CRPCCommand vRPCCommands[] =
353353
{ "dash", "mnsync", &mnsync, true },
354354
{ "dash", "spork", &spork, true },
355355
{ "dash", "getpoolinfo", &getpoolinfo, true },
356+
{ "dash", "sentinelping", &sentinelping, true },
356357
#ifdef ENABLE_WALLET
357358
{ "dash", "privatesend", &privatesend, false },
358359

src/rpc/server.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ extern UniValue getchaintips(const UniValue& params, bool fHelp);
296296
extern UniValue invalidateblock(const UniValue& params, bool fHelp);
297297
extern UniValue reconsiderblock(const UniValue& params, bool fHelp);
298298
extern UniValue getspentinfo(const UniValue& params, bool fHelp);
299+
extern UniValue sentinelping(const UniValue& params, bool fHelp);
299300

300301
bool StartRPC();
301302
void InterruptRPC();

src/test/rpc_tests.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,4 +307,11 @@ BOOST_AUTO_TEST_CASE(rpc_ban)
307307
BOOST_CHECK_EQUAL(adr.get_str(), "2001:4d48:ac57:400:cacf:e9ff:fe1d:9c63/128");
308308
}
309309

310+
BOOST_AUTO_TEST_CASE(rpc_sentinel_ping)
311+
{
312+
BOOST_CHECK_NO_THROW(CallRPC("sentinelping 1.0.2"));
313+
BOOST_CHECK_THROW(CallRPC("sentinelping"), runtime_error);
314+
BOOST_CHECK_THROW(CallRPC("sentinelping 2"), bad_cast);
315+
}
316+
310317
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)