From b2017ebefaed40566d9728acc70820b3c5f6abc9 Mon Sep 17 00:00:00 2001 From: UdjinM6 Date: Sun, 1 Mar 2015 18:38:53 +0300 Subject: [PATCH] find oldest legit masternode to pay / naming "now" -> "sigTime" --- src/masternode.cpp | 16 +++++++++------- src/masternode.h | 8 ++++---- src/masternodeman.cpp | 17 ++++++++++------- src/masternodeman.h | 2 +- src/rpcdarksend.cpp | 6 +++--- 5 files changed, 27 insertions(+), 22 deletions(-) diff --git a/src/masternode.cpp b/src/masternode.cpp index bba657cbb5..5db27c74fa 100644 --- a/src/masternode.cpp +++ b/src/masternode.cpp @@ -133,7 +133,7 @@ CMasternode::CMasternode() pubkey2 = CPubKey(); sig = std::vector(); activeState = MASTERNODE_ENABLED; - now = GetTime(); + sigTime = GetAdjustedTime(); lastDseep = 0; lastTimeSeen = 0; cacheInputAge = 0; @@ -153,7 +153,7 @@ CMasternode::CMasternode(const CMasternode& other) pubkey2 = other.pubkey2; sig = other.sig; activeState = other.activeState; - now = other.now; + sigTime = other.sigTime; lastDseep = other.lastDseep; lastTimeSeen = other.lastTimeSeen; cacheInputAge = other.cacheInputAge; @@ -164,7 +164,7 @@ CMasternode::CMasternode(const CMasternode& other) nLastDsq = other.nLastDsq; } -CMasternode::CMasternode(CService newAddr, CTxIn newVin, CPubKey newPubkey, std::vector newSig, int64_t newNow, CPubKey newPubkey2, int protocolVersionIn) +CMasternode::CMasternode(CService newAddr, CTxIn newVin, CPubKey newPubkey, std::vector newSig, int64_t newSigTime, CPubKey newPubkey2, int protocolVersionIn) { LOCK(cs); vin = newVin; @@ -173,7 +173,7 @@ CMasternode::CMasternode(CService newAddr, CTxIn newVin, CPubKey newPubkey, std: pubkey2 = newPubkey2; sig = newSig; activeState = MASTERNODE_ENABLED; - now = newNow; + sigTime = newSigTime; lastDseep = 0; lastTimeSeen = 0; cacheInputAge = 0; @@ -373,6 +373,7 @@ bool CMasternodePayments::ProcessBlock(int nBlockHeight) { if(!enabled) return false; CMasternodePaymentWinner newWinner; + int nEnabled = mnodeman.CountEnabled(); std::vector vecLastPayments; BOOST_REVERSE_FOREACH(CMasternodePaymentWinner& winner, vWinning) @@ -383,8 +384,9 @@ bool CMasternodePayments::ProcessBlock(int nBlockHeight) vecLastPayments.push_back(winner.vin); } - CMasternode *pmn = mnodeman.FindNotInVec(vecLastPayments); - if(pmn != NULL) + // pay to the oldest MN that still had no payment but its input is old enough and it was active long enough + CMasternode *pmn = mnodeman.FindOldestNotInVec(vecLastPayments); + if(pmn != NULL && pmn->GetMasternodeInputAge() > nEnabled && pmn->lastTimeSeen - pmn->sigTime > nEnabled * 2.5 * 60) { newWinner.score = 0; newWinner.nBlockHeight = nBlockHeight; @@ -393,7 +395,7 @@ bool CMasternodePayments::ProcessBlock(int nBlockHeight) } //if we can't find new MN to get paid, pick first active MN counting back from the end of vecLastPayments list - if(newWinner.nBlockHeight == 0 && mnodeman.CountEnabled() > 0) + if(newWinner.nBlockHeight == 0 && nEnabled > 0) { BOOST_REVERSE_FOREACH(CTxIn& vinLP, vecLastPayments) { diff --git a/src/masternode.h b/src/masternode.h index 5d6771ca72..cb5b7ed596 100644 --- a/src/masternode.h +++ b/src/masternode.h @@ -69,7 +69,7 @@ public: CPubKey pubkey2; std::vector sig; int activeState; - int64_t now; //dsee message times + int64_t sigTime; //dsee message times int64_t lastDseep; int64_t lastTimeSeen; int cacheInputAge; @@ -81,7 +81,7 @@ public: CMasternode(); CMasternode(const CMasternode& other); - CMasternode(CService newAddr, CTxIn newVin, CPubKey newPubkey, std::vector newSig, int64_t newNow, CPubKey newPubkey2, int protocolVersionIn); + CMasternode(CService newAddr, CTxIn newVin, CPubKey newPubkey, std::vector newSig, int64_t newSigTime, CPubKey newPubkey2, int protocolVersionIn); void swap(CMasternode& first, CMasternode& second) // nothrow { @@ -96,7 +96,7 @@ public: swap(first.pubkey2, second.pubkey2); swap(first.sig, second.sig); swap(first.activeState, second.activeState); - swap(first.now, second.now); + swap(first.sigTime, second.sigTime); swap(first.lastDseep, second.lastDseep); swap(first.lastTimeSeen, second.lastTimeSeen); swap(first.cacheInputAge, second.cacheInputAge); @@ -138,7 +138,7 @@ public: READWRITE(pubkey2); READWRITE(sig); READWRITE(activeState); - READWRITE(now); + READWRITE(sigTime); READWRITE(lastDseep); READWRITE(lastTimeSeen); READWRITE(cacheInputAge); diff --git a/src/masternodeman.cpp b/src/masternodeman.cpp index b7228d23b2..2b6993ae35 100644 --- a/src/masternodeman.cpp +++ b/src/masternodeman.cpp @@ -258,10 +258,12 @@ CMasternode *CMasternodeMan::Find(const CTxIn &vin) return NULL; } -CMasternode *CMasternodeMan::FindNotInVec(const std::vector &vVins) +CMasternode* CMasternodeMan::FindOldestNotInVec(const std::vector &vVins) { LOCK(cs); + CMasternode *pOldestMasternode = NULL; + BOOST_FOREACH(CMasternode &mn, vMasternodes) { mn.Check(); @@ -277,10 +279,11 @@ CMasternode *CMasternodeMan::FindNotInVec(const std::vector &vVins) if(found) continue; - return &mn; + if(pOldestMasternode == NULL || pOldestMasternode->GetMasternodeInputAge() < mn.GetMasternodeInputAge()) + pOldestMasternode = &mn; } - return NULL; + return pOldestMasternode; } CMasternode *CMasternodeMan::FindRandom() @@ -435,10 +438,10 @@ void CMasternodeMan::ProcessMessage(CNode* pfrom, std::string& strCommand, CData if(count == -1 && pmn->pubkey == pubkey && !pmn->UpdatedWithin(MASTERNODE_MIN_DSEE_SECONDS)){ pmn->UpdateLastSeen(); - if(pmn->now < sigTime){ //take the newest entry + if(pmn->sigTime < sigTime){ //take the newest entry LogPrintf("dsee - Got updated entry for %s\n", addr.ToString().c_str()); pmn->pubkey2 = pubkey2; - pmn->now = sigTime; + pmn->sigTime = sigTime; pmn->sig = vchSig; pmn->protocolVersion = protocolVersion; pmn->addr = addr; @@ -614,9 +617,9 @@ void CMasternodeMan::ProcessMessage(CNode* pfrom, std::string& strCommand, CData { if(fDebug) LogPrintf("dseg - Sending masternode entry - %s \n", mn.addr.ToString().c_str()); if(vin == CTxIn()){ - pfrom->PushMessage("dsee", mn.vin, mn.addr, mn.sig, mn.now, mn.pubkey, mn.pubkey2, count, i, mn.lastTimeSeen, mn.protocolVersion); + pfrom->PushMessage("dsee", mn.vin, mn.addr, mn.sig, mn.sigTime, mn.pubkey, mn.pubkey2, count, i, mn.lastTimeSeen, mn.protocolVersion); } else if (vin == mn.vin) { - pfrom->PushMessage("dsee", mn.vin, mn.addr, mn.sig, mn.now, mn.pubkey, mn.pubkey2, count, i, mn.lastTimeSeen, mn.protocolVersion); + pfrom->PushMessage("dsee", mn.vin, mn.addr, mn.sig, mn.sigTime, mn.pubkey, mn.pubkey2, count, i, mn.lastTimeSeen, mn.protocolVersion); LogPrintf("dseg - Sent 1 masternode entries to %s\n", pfrom->addr.ToString().c_str()); return; } diff --git a/src/masternodeman.h b/src/masternodeman.h index 95f0d72365..f1f1a9cb3a 100644 --- a/src/masternodeman.h +++ b/src/masternodeman.h @@ -96,7 +96,7 @@ public: CMasternode* Find(const CTxIn& vin); //Find an entry thta do not match every entry provided vector - CMasternode* FindNotInVec(const std::vector &vVins); + CMasternode* FindOldestNotInVec(const std::vector &vVins); // Find a random entry CMasternode* FindRandom(); diff --git a/src/rpcdarksend.cpp b/src/rpcdarksend.cpp index a13298ce49..a170c8ac4e 100644 --- a/src/rpcdarksend.cpp +++ b/src/rpcdarksend.cpp @@ -468,7 +468,7 @@ Value masternode(const Array& params, bool fHelp) obj.push_back(Pair("vin", winner->vin.prevout.hash.ToString().c_str())); obj.push_back(Pair("pubkey", address2.ToString().c_str())); obj.push_back(Pair("lastseen", (int64_t)winner->lastTimeSeen)); - obj.push_back(Pair("activeseconds", (int64_t)(winner->lastTimeSeen - winner->now))); + obj.push_back(Pair("activeseconds", (int64_t)(winner->lastTimeSeen - winner->sigTime))); return obj; } @@ -627,7 +627,7 @@ Value masternodelist(const Array& params, bool fHelp) } else if (strMode == "activeseconds") { if(strFilter !="" && mn.addr.ToString().find(strFilter) == string::npos) continue; - obj.push_back(Pair(strAddr, (int64_t)(mn.lastTimeSeen - mn.now))); + obj.push_back(Pair(strAddr, (int64_t)(mn.lastTimeSeen - mn.sigTime))); } else if (strMode == "rank") { if(strFilter !="" && mn.addr.ToString().find(strFilter) == string::npos) continue; obj.push_back(Pair(strAddr, (int)(mnodeman.GetMasternodeRank(mn.vin, chainActive.Tip()->nHeight)))); @@ -644,7 +644,7 @@ Value masternodelist(const Array& params, bool fHelp) address2.ToString() << " | " << mn.vin.prevout.hash.ToString() << " | " << mn.lastTimeSeen << " | " << - (mn.lastTimeSeen - mn.now); + (mn.lastTimeSeen - mn.sigTime); std::string output = stringStream.str(); stringStream << " " << strAddr; if(strFilter !="" && stringStream.str().find(strFilter) == string::npos) continue;