Skip to content

Commit befaaeb

Browse files
committed
Decouple ProcessMNBroadcast into its own method plus strip out Misbehaving call from its flow.
1 parent 2dc773b commit befaaeb

File tree

2 files changed

+71
-57
lines changed

2 files changed

+71
-57
lines changed

src/masternodeman.cpp

Lines changed: 67 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -639,55 +639,80 @@ std::vector<std::pair<int64_t, CMasternode>> CMasternodeMan::GetMasternodeRanks(
639639
return vecMasternodeScores;
640640
}
641641

642+
int CMasternodeMan::ProcessMNBroadcast(CNode* pfrom, CMasternodeBroadcast& mnb)
643+
{
644+
if (mapSeenMasternodeBroadcast.count(mnb.GetHash())) { //seen
645+
masternodeSync.AddedMasternodeList(mnb.GetHash());
646+
return 0;
647+
}
648+
mapSeenMasternodeBroadcast.emplace(mnb.GetHash(), mnb);
649+
650+
int nDoS = 0;
651+
if (!mnb.CheckAndUpdate(nDoS)) {
652+
return nDoS;
653+
}
654+
655+
// make sure the vout that was signed is related to the transaction that spawned the Masternode
656+
// - this is expensive, so it's only done once per Masternode
657+
if (!mnb.IsInputAssociatedWithPubkey()) {
658+
LogPrintf("CMasternodeMan::ProcessMessage() : mnb - Got mismatched pubkey and vin\n");
659+
return 33;
660+
}
661+
662+
// make sure it's still unspent
663+
// - this is checked later by .check() in many places and by ThreadCheckObfuScationPool()
664+
if (mnb.CheckInputsAndAdd(nDoS)) {
665+
// use this as a peer
666+
g_connman->AddNewAddress(CAddress(mnb.addr, NODE_NETWORK), pfrom->addr, 2 * 60 * 60);
667+
masternodeSync.AddedMasternodeList(mnb.GetHash());
668+
} else {
669+
LogPrint(BCLog::MASTERNODE,"mnb - Rejected Masternode entry %s\n", mnb.vin.prevout.hash.ToString());
670+
return nDoS;
671+
}
672+
// All good
673+
return 0;
674+
}
675+
676+
int CMasternodeMan::ProcessMNPing(CNode* pfrom, CMasternodePing& mnp)
677+
{
678+
if (mapSeenMasternodePing.count(mnp.GetHash())) return 0; //seen
679+
mapSeenMasternodePing.emplace(mnp.GetHash(), mnp);
680+
681+
int nDoS = 0;
682+
if (mnp.CheckAndUpdate(nDoS)) return 0;
683+
684+
if (nDoS > 0) {
685+
// if anything significant failed, mark that node
686+
return nDoS;
687+
} else {
688+
// if nothing significant failed, search existing Masternode list
689+
CMasternode* pmn = Find(mnp.vin);
690+
// if it's known, don't ask for the mnb, just return
691+
if (pmn != NULL) return 0;
692+
}
693+
694+
// something significant is broken or mn is unknown,
695+
// we might have to ask for a masternode entry once
696+
AskForMN(pfrom, mnp.vin);
697+
698+
// All good
699+
return 0;
700+
}
701+
642702
void CMasternodeMan::ProcessMessage(CNode* pfrom, std::string& strCommand, CDataStream& vRecv)
643703
{
644704
if (fLiteMode) return; //disable all Masternode related functionality
645705
if (!masternodeSync.IsBlockchainSynced()) return;
646706

647707
LOCK(cs_process_message);
648708

649-
if (strCommand == NetMsgType::MNBROADCAST) { //Masternode Broadcast
709+
if (strCommand == NetMsgType::MNBROADCAST) {
650710
CMasternodeBroadcast mnb;
651711
vRecv >> mnb;
652-
653-
if (mapSeenMasternodeBroadcast.count(mnb.GetHash())) { //seen
654-
masternodeSync.AddedMasternodeList(mnb.GetHash());
655-
return;
656-
}
657-
mapSeenMasternodeBroadcast.emplace(mnb.GetHash(), mnb);
658-
659-
int nDoS = 0;
660-
if (!mnb.CheckAndUpdate(nDoS)) {
661-
if (nDoS > 0) {
662-
LOCK(cs_main);
663-
Misbehaving(pfrom->GetId(), nDoS);
664-
}
665-
//failed
666-
return;
667-
}
668-
669-
// make sure the vout that was signed is related to the transaction that spawned the Masternode
670-
// - this is expensive, so it's only done once per Masternode
671-
if (!mnb.IsInputAssociatedWithPubkey()) {
672-
LogPrintf("CMasternodeMan::ProcessMessage() : mnb - Got mismatched pubkey and vin\n");
712+
int banScore = ProcessMNBroadcast(pfrom, mnb);
713+
if (banScore > 0) {
673714
LOCK(cs_main);
674-
Misbehaving(pfrom->GetId(), 33);
675-
return;
676-
}
677-
678-
// make sure it's still unspent
679-
// - this is checked later by .check() in many places and by ThreadCheckObfuScationPool()
680-
if (mnb.CheckInputsAndAdd(nDoS)) {
681-
// use this as a peer
682-
g_connman->AddNewAddress(CAddress(mnb.addr, NODE_NETWORK), pfrom->addr, 2 * 60 * 60);
683-
masternodeSync.AddedMasternodeList(mnb.GetHash());
684-
} else {
685-
LogPrint(BCLog::MASTERNODE,"mnb - Rejected Masternode entry %s\n", mnb.vin.prevout.hash.ToString());
686-
687-
if (nDoS > 0) {
688-
LOCK(cs_main);
689-
Misbehaving(pfrom->GetId(), nDoS);
690-
}
715+
Misbehaving(pfrom->GetId(), banScore);
691716
}
692717
}
693718

@@ -697,27 +722,12 @@ void CMasternodeMan::ProcessMessage(CNode* pfrom, std::string& strCommand, CData
697722

698723
LogPrint(BCLog::MNPING, "mnp - Masternode ping, vin: %s\n", mnp.vin.prevout.hash.ToString());
699724

700-
if (mapSeenMasternodePing.count(mnp.GetHash())) return; //seen
701-
mapSeenMasternodePing.emplace(mnp.GetHash(), mnp);
702-
703-
int nDoS = 0;
704-
if (mnp.CheckAndUpdate(nDoS)) return;
705-
706-
if (nDoS > 0) {
707-
// if anything significant failed, mark that node
725+
int banScore = ProcessMNPing(pfrom, mnp);
726+
if (banScore > 0) {
708727
LOCK(cs_main);
709-
Misbehaving(pfrom->GetId(), nDoS);
710-
} else {
711-
// if nothing significant failed, search existing Masternode list
712-
CMasternode* pmn = Find(mnp.vin);
713-
// if it's known, don't ask for the mnb, just return
714-
if (pmn != NULL) return;
728+
Misbehaving(pfrom->GetId(), banScore);
715729
}
716730

717-
// something significant is broken or mn is unknown,
718-
// we might have to ask for a masternode entry once
719-
AskForMN(pfrom, mnp.vin);
720-
721731
} else if (strCommand == NetMsgType::GETMNLIST) { //Get Masternode list or specific entry
722732
ProcessGetMNList(pfrom, strCommand, vRecv);
723733
}

src/masternodeman.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ class CMasternodeMan
7878
// Memory Only. Cache last block hashes. Used to verify mn pings and winners.
7979
CyclingVector<uint256> cvLastBlockHashes;
8080

81+
// Return the banning score (0 if no ban score increase is needed).
82+
int ProcessMNBroadcast(CNode* pfrom, CMasternodeBroadcast& mnb);
83+
int ProcessMNPing(CNode* pfrom, CMasternodePing& mnp);
84+
8185
public:
8286
// Keep track of all broadcasts I've seen
8387
std::map<uint256, CMasternodeBroadcast> mapSeenMasternodeBroadcast;

0 commit comments

Comments
 (0)