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

[BUG][MN] Immediately detect forked masternodes #2060

Merged
merged 3 commits into from
Dec 14, 2020
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
2 changes: 1 addition & 1 deletion src/activemasternode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ bool CActiveMasternode::SendMasternodePing(std::string& errorMessage)
LogPrintf("CActiveMasternode::SendMasternodePing() - Relay Masternode Ping vin = %s\n", vin->ToString());

const uint256& nBlockHash = mnodeman.GetBlockHashToPing();
CMasternodePing mnp(*vin, nBlockHash);
CMasternodePing mnp(*vin, nBlockHash, GetAdjustedTime());
if (!mnp.Sign(privKeyMasternode, pubKeyMasternode)) {
errorMessage = "Couldn't sign Masternode Ping";
return false;
Expand Down
33 changes: 17 additions & 16 deletions src/masternode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ CMasternode::CMasternode() :
addr = CService();
pubKeyCollateralAddress = CPubKey();
pubKeyMasternode = CPubKey();
sigTime = GetAdjustedTime();
sigTime = 0;
lastPing = CMasternodePing();
protocolVersion = PROTOCOL_VERSION;
nScanningErrorCount = 0;
Expand Down Expand Up @@ -206,14 +206,16 @@ CMasternodeBroadcast::CMasternodeBroadcast() :
CMasternode()
{ }

CMasternodeBroadcast::CMasternodeBroadcast(CService newAddr, CTxIn newVin, CPubKey pubKeyCollateralAddressNew, CPubKey pubKeyMasternodeNew, int protocolVersionIn) :
CMasternodeBroadcast::CMasternodeBroadcast(CService newAddr, CTxIn newVin, CPubKey pubKeyCollateralAddressNew, CPubKey pubKeyMasternodeNew, int protocolVersionIn, const CMasternodePing& _lastPing) :
CMasternode()
{
vin = newVin;
addr = newAddr;
pubKeyCollateralAddress = pubKeyCollateralAddressNew;
pubKeyMasternode = pubKeyMasternodeNew;
protocolVersion = protocolVersionIn;
lastPing = _lastPing;
sigTime = lastPing.sigTime;
}

CMasternodeBroadcast::CMasternodeBroadcast(const CMasternode& mn) :
Expand Down Expand Up @@ -286,15 +288,15 @@ bool CMasternodeBroadcast::Create(const CTxIn& txin,

// Get block hash to ping (TODO: move outside of this function)
const uint256& nBlockHashToPing = mnodeman.GetBlockHashToPing();
CMasternodePing mnp(txin, nBlockHashToPing);
CMasternodePing mnp(txin, nBlockHashToPing, GetAdjustedTime());
if (!mnp.Sign(keyMasternodeNew, pubKeyMasternodeNew)) {
strErrorRet = strprintf("Failed to sign ping, masternode=%s", txin.prevout.hash.ToString());
LogPrint(BCLog::MASTERNODE,"CMasternodeBroadcast::Create -- %s\n", strErrorRet);
mnbRet = CMasternodeBroadcast();
return false;
}

mnbRet = CMasternodeBroadcast(service, txin, pubKeyCollateralAddressNew, pubKeyMasternodeNew, PROTOCOL_VERSION);
mnbRet = CMasternodeBroadcast(service, txin, pubKeyCollateralAddressNew, pubKeyMasternodeNew, PROTOCOL_VERSION, mnp);

if (!mnbRet.IsValidNetAddr()) {
strErrorRet = strprintf("Invalid IP address %s, masternode=%s", mnbRet.addr.ToStringIP (), txin.prevout.hash.ToString());
Expand All @@ -303,7 +305,6 @@ bool CMasternodeBroadcast::Create(const CTxIn& txin,
return false;
}

mnbRet.lastPing = mnp;
if (!mnbRet.Sign(keyCollateralAddressNew, pubKeyCollateralAddressNew)) {
strErrorRet = strprintf("Failed to sign broadcast, masternode=%s", txin.prevout.hash.ToString());
LogPrint(BCLog::MASTERNODE,"CMasternodeBroadcast::Create -- %s\n", strErrorRet);
Expand Down Expand Up @@ -563,14 +564,14 @@ CMasternodePing::CMasternodePing() :
CSignedMessage(),
vin(),
blockHash(),
sigTime(GetAdjustedTime())
sigTime(0)
{ }

CMasternodePing::CMasternodePing(const CTxIn& newVin, const uint256& nBlockHash) :
CMasternodePing::CMasternodePing(const CTxIn& newVin, const uint256& nBlockHash, uint64_t _sigTime) :
CSignedMessage(),
vin(newVin),
blockHash(nBlockHash),
sigTime(GetAdjustedTime())
sigTime(_sigTime)
{ }

uint256 CMasternodePing::GetHash() const
Expand Down Expand Up @@ -601,6 +602,14 @@ bool CMasternodePing::CheckAndUpdate(int& nDos, bool fRequireAvailable, bool fCh
return false;
}

// Check if the ping block hash exists and it's within 24 blocks from the tip
if (!mnodeman.IsWithinDepth(blockHash, 2 * MNPING_DEPTH)) {
LogPrint(BCLog::MNPING,"%s: Masternode %s block hash %s is too old or has an invalid block hash\n",
__func__, vin.prevout.hash.ToString(), blockHash.ToString());
nDos = 33;
return false;
}

// see if we have this Masternode
CMasternode* pmn = mnodeman.Find(vin.prevout);
const bool isMasternodeFound = (pmn != nullptr);
Expand Down Expand Up @@ -632,14 +641,6 @@ bool CMasternodePing::CheckAndUpdate(int& nDos, bool fRequireAvailable, bool fCh
return false;
}

// Check if the ping block hash exists and it's within 24 blocks from the tip
if (!mnodeman.IsWithinDepth(blockHash, 2 * MNPING_DEPTH)) {
LogPrint(BCLog::MNPING,"%s: Masternode %s block hash %s is too old or has an invalid block hash\n",
__func__, vin.prevout.hash.ToString(), blockHash.ToString());
nDos = 33;
return false;
}

// ping have passed the basic checks, can be updated now
mnodeman.mapSeenMasternodePing.emplace(GetHash(), *this);

Expand Down
4 changes: 2 additions & 2 deletions src/masternode.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class CMasternodePing : public CSignedMessage
int64_t sigTime; //mnb message times

CMasternodePing();
CMasternodePing(const CTxIn& newVin, const uint256& nBlockHash);
CMasternodePing(const CTxIn& newVin, const uint256& nBlockHash, uint64_t _sigTime);

ADD_SERIALIZE_METHODS;

Expand Down Expand Up @@ -268,7 +268,7 @@ class CMasternodeBroadcast : public CMasternode
{
public:
CMasternodeBroadcast();
CMasternodeBroadcast(CService newAddr, CTxIn newVin, CPubKey newPubkey, CPubKey newPubkey2, int protocolVersionIn);
CMasternodeBroadcast(CService newAddr, CTxIn newVin, CPubKey newPubkey, CPubKey newPubkey2, int protocolVersionIn, const CMasternodePing& _lastPing);
CMasternodeBroadcast(const CMasternode& mn);

bool CheckAndUpdate(int& nDoS);
Expand Down