bool CMasternodeBroadcast::CheckAndUpdate(int& nDos) { // make sure signature isn't in the future (past is OK) if (sigTime > GetAdjustedTime() + 60 * 60) { LogPrintf("mnb - Signature rejected, too far into the future %s\n", vin.ToString()); nDos = 1; return false; } std::string vchPubKey(pubkey.begin(), pubkey.end()); std::string vchPubKey2(pubkey2.begin(), pubkey2.end()); std::string strMessage = addr.ToString() + boost::lexical_cast<std::string>(sigTime) + vchPubKey + vchPubKey2 + boost::lexical_cast<std::string>(protocolVersion); if(protocolVersion < masternodePayments.GetMinMasternodePaymentsProto()) { LogPrintf("mnb - ignoring outdated Masternode %s protocol version %d\n", vin.ToString(), protocolVersion); return false; } CScript pubkeyScript; pubkeyScript = GetScriptForDestination(pubkey.GetID()); if(pubkeyScript.size() != 25) { LogPrintf("mnb - pubkey the wrong size\n"); nDos = 100; return false; } CScript pubkeyScript2; pubkeyScript2 = GetScriptForDestination(pubkey2.GetID()); if(pubkeyScript2.size() != 25) { LogPrintf("mnb - pubkey2 the wrong size\n"); nDos = 100; return false; } if(!vin.scriptSig.empty()) { LogPrintf("mnb - Ignore Not Empty ScriptSig %s\n",vin.ToString()); return false; } std::string errorMessage = ""; if(!darkSendSigner.VerifyMessage(pubkey, sig, strMessage, errorMessage)){ LogPrintf("mnb - Got bad Masternode address signature\n"); nDos = 100; return false; } if(Params().NetworkID() == CBaseChainParams::MAIN) { if(addr.GetPort() != 9999) return false; } else if(addr.GetPort() == 9999) return false; //search existing Masternode list, this is where we update existing Masternodes with new mnb broadcasts CMasternode* pmn = mnodeman.Find(vin); // no such masternode or it's not enabled already, nothing to update if(pmn == NULL || (pmn != NULL && !pmn->IsEnabled())) return true; // mn.pubkey = pubkey, IsVinAssociatedWithPubkey is validated once below, // after that they just need to match if(pmn->pubkey == pubkey && !pmn->IsBroadcastedWithin(MASTERNODE_MIN_MNB_SECONDS)) { //take the newest entry LogPrintf("mnb - Got updated entry for %s\n", addr.ToString()); if(pmn->UpdateFromNewBroadcast((*this))){ pmn->Check(); if(pmn->IsEnabled()) Relay(); } masternodeSync.AddedMasternodeList(GetHash()); } return true; }
bool CMasternodeBroadcast::CheckAndUpdate(int& nDos) { nDos = 0; // make sure signature isn't in the future (past is OK) if (sigTime > GetAdjustedTime() + 60 * 60) { LogPrintf("mnb - Signature rejected, too far into the future %s\n", vin.ToString()); nDos = 1; return false; } if(protocolVersion < masternodePayments.GetMinMasternodePaymentsProto()) { LogPrintf("mnb - ignoring outdated Masternode %s protocol version %d\n", vin.ToString(), protocolVersion); return false; } CScript pubkeyScript; pubkeyScript = GetScriptForDestination(pubkey.GetID()); if(pubkeyScript.size() != 25) { LogPrintf("mnb - pubkey the wrong size\n"); nDos = 100; return false; } CScript pubkeyScript2; pubkeyScript2 = GetScriptForDestination(pubkey2.GetID()); if(pubkeyScript2.size() != 25) { LogPrintf("mnb - pubkey2 the wrong size\n"); nDos = 100; return false; } if(!vin.scriptSig.empty()) { LogPrintf("mnb - Ignore Not Empty ScriptSig %s\n",vin.ToString()); return false; } std::string strMessage; std::string errorMessage = ""; if(protocolVersion < 70201) { std::string vchPubKey(pubkey.begin(), pubkey.end()); std::string vchPubKey2(pubkey2.begin(), pubkey2.end()); strMessage = addr.ToString(false) + boost::lexical_cast<std::string>(sigTime) + vchPubKey + vchPubKey2 + boost::lexical_cast<std::string>(protocolVersion); LogPrint("masternode", "mnb - sanitized strMessage: %s, pubkey address: %s, sig: %s\n", SanitizeString(strMessage), CBitcoinAddress(pubkey.GetID()).ToString(), EncodeBase64(&sig[0], sig.size())); if(!darkSendSigner.VerifyMessage(pubkey, sig, strMessage, errorMessage)){ if (addr.ToString() != addr.ToString(false)) { // maybe it's wrong format, try again with the old one strMessage = addr.ToString() + boost::lexical_cast<std::string>(sigTime) + vchPubKey + vchPubKey2 + boost::lexical_cast<std::string>(protocolVersion); LogPrint("masternode", "mnb - sanitized strMessage: %s, pubkey address: %s, sig: %s\n", SanitizeString(strMessage), CBitcoinAddress(pubkey.GetID()).ToString(), EncodeBase64(&sig[0], sig.size())); if(!darkSendSigner.VerifyMessage(pubkey, sig, strMessage, errorMessage)){ // didn't work either LogPrintf("mnb - Got bad Masternode address signature, sanitized error: %s\n", SanitizeString(errorMessage)); // there is a bug in old MN signatures, ignore such MN but do not ban the peer we got this from return false; } } else { // nope, sig is actually wrong LogPrintf("mnb - Got bad Masternode address signature, sanitized error: %s\n", SanitizeString(errorMessage)); // there is a bug in old MN signatures, ignore such MN but do not ban the peer we got this from return false; } } } else { strMessage = addr.ToString(false) + boost::lexical_cast<std::string>(sigTime) + pubkey.GetID().ToString() + pubkey2.GetID().ToString() + boost::lexical_cast<std::string>(protocolVersion); LogPrint("masternode", "mnb - strMessage: %s, pubkey address: %s, sig: %s\n", strMessage, CBitcoinAddress(pubkey.GetID()).ToString(), EncodeBase64(&sig[0], sig.size())); if(!darkSendSigner.VerifyMessage(pubkey, sig, strMessage, errorMessage)){ LogPrintf("mnb - Got bad Masternode address signature, error: %s\n", errorMessage); nDos = 100; return false; } } if(Params().NetworkID() == CBaseChainParams::MAIN) { if(addr.GetPort() != 9887) return false; } else if(addr.GetPort() == 9887) return false; //search existing Masternode list, this is where we update existing Masternodes with new mnb broadcasts CMasternode* pmn = mnodeman.Find(vin); // no such masternode, nothing to update if(pmn == NULL) return true ; else { // this broadcast older than we have, it's bad. if(pmn->sigTime > sigTime) { LogPrintf("mnb - Bad sigTime %d for Masternode %20s %105s (existing broadcast is at %d)\n", sigTime, addr.ToString(), vin.ToString(), pmn->sigTime); return false; } // masternode is not enabled yet/already, nothing to update if(!pmn->IsEnabled()) return true; } // mn.pubkey = pubkey, IsVinAssociatedWithPubkey is validated once below, // after that they just need to match if(pmn->pubkey == pubkey && !pmn->IsBroadcastedWithin(MASTERNODE_MIN_MNB_SECONDS)) { //take the newest entry LogPrintf("mnb - Got updated entry for %s\n", addr.ToString()); if(pmn->UpdateFromNewBroadcast((*this))){ pmn->Check(); if(pmn->IsEnabled()) Relay(); } masternodeSync.AddedMasternodeList(GetHash()); } return true; }