Skip to content

Commit 40b2ac6

Browse files
author
evan82
committed
Merge pull request #211 from UdjinM6/v0.11.2.x_pay_to_oldest_legit
V0.11.2.x pay to oldest legit MN
2 parents e2cf12b + f4f3f6b commit 40b2ac6

5 files changed

+45
-22
lines changed

src/masternode.cpp

+9-7
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ CMasternode::CMasternode()
133133
pubkey2 = CPubKey();
134134
sig = std::vector<unsigned char>();
135135
activeState = MASTERNODE_ENABLED;
136-
now = GetTime();
136+
sigTime = GetAdjustedTime();
137137
lastDseep = 0;
138138
lastTimeSeen = 0;
139139
cacheInputAge = 0;
@@ -153,7 +153,7 @@ CMasternode::CMasternode(const CMasternode& other)
153153
pubkey2 = other.pubkey2;
154154
sig = other.sig;
155155
activeState = other.activeState;
156-
now = other.now;
156+
sigTime = other.sigTime;
157157
lastDseep = other.lastDseep;
158158
lastTimeSeen = other.lastTimeSeen;
159159
cacheInputAge = other.cacheInputAge;
@@ -164,7 +164,7 @@ CMasternode::CMasternode(const CMasternode& other)
164164
nLastDsq = other.nLastDsq;
165165
}
166166

167-
CMasternode::CMasternode(CService newAddr, CTxIn newVin, CPubKey newPubkey, std::vector<unsigned char> newSig, int64_t newNow, CPubKey newPubkey2, int protocolVersionIn)
167+
CMasternode::CMasternode(CService newAddr, CTxIn newVin, CPubKey newPubkey, std::vector<unsigned char> newSig, int64_t newSigTime, CPubKey newPubkey2, int protocolVersionIn)
168168
{
169169
LOCK(cs);
170170
vin = newVin;
@@ -173,7 +173,7 @@ CMasternode::CMasternode(CService newAddr, CTxIn newVin, CPubKey newPubkey, std:
173173
pubkey2 = newPubkey2;
174174
sig = newSig;
175175
activeState = MASTERNODE_ENABLED;
176-
now = newNow;
176+
sigTime = newSigTime;
177177
lastDseep = 0;
178178
lastTimeSeen = 0;
179179
cacheInputAge = 0;
@@ -373,6 +373,7 @@ bool CMasternodePayments::ProcessBlock(int nBlockHeight)
373373
{
374374
if(!enabled) return false;
375375
CMasternodePaymentWinner newWinner;
376+
int nEnabled = mnodeman.CountEnabled();
376377

377378
std::vector<CTxIn> vecLastPayments;
378379
BOOST_REVERSE_FOREACH(CMasternodePaymentWinner& winner, vWinning)
@@ -383,8 +384,9 @@ bool CMasternodePayments::ProcessBlock(int nBlockHeight)
383384
vecLastPayments.push_back(winner.vin);
384385
}
385386

386-
CMasternode *pmn = mnodeman.FindNotInVec(vecLastPayments);
387-
if(pmn != NULL)
387+
// pay to the oldest MN that still had no payment but its input is old enough and it was active long enough
388+
CMasternode *pmn = mnodeman.FindOldestNotInVec(vecLastPayments);
389+
if(pmn != NULL && pmn->GetMasternodeInputAge() > nEnabled && pmn->lastTimeSeen - pmn->sigTime > nEnabled * 2.5 * 60)
388390
{
389391
newWinner.score = 0;
390392
newWinner.nBlockHeight = nBlockHeight;
@@ -393,7 +395,7 @@ bool CMasternodePayments::ProcessBlock(int nBlockHeight)
393395
}
394396

395397
//if we can't find new MN to get paid, pick first active MN counting back from the end of vecLastPayments list
396-
if(newWinner.nBlockHeight == 0 && mnodeman.CountEnabled() > 0)
398+
if(newWinner.nBlockHeight == 0 && nEnabled > 0)
397399
{
398400
BOOST_REVERSE_FOREACH(CTxIn& vinLP, vecLastPayments)
399401
{

src/masternode.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class CMasternode
6969
CPubKey pubkey2;
7070
std::vector<unsigned char> sig;
7171
int activeState;
72-
int64_t now; //dsee message times
72+
int64_t sigTime; //dsee message times
7373
int64_t lastDseep;
7474
int64_t lastTimeSeen;
7575
int cacheInputAge;
@@ -81,7 +81,7 @@ class CMasternode
8181

8282
CMasternode();
8383
CMasternode(const CMasternode& other);
84-
CMasternode(CService newAddr, CTxIn newVin, CPubKey newPubkey, std::vector<unsigned char> newSig, int64_t newNow, CPubKey newPubkey2, int protocolVersionIn);
84+
CMasternode(CService newAddr, CTxIn newVin, CPubKey newPubkey, std::vector<unsigned char> newSig, int64_t newSigTime, CPubKey newPubkey2, int protocolVersionIn);
8585

8686
void swap(CMasternode& first, CMasternode& second) // nothrow
8787
{
@@ -96,7 +96,7 @@ class CMasternode
9696
swap(first.pubkey2, second.pubkey2);
9797
swap(first.sig, second.sig);
9898
swap(first.activeState, second.activeState);
99-
swap(first.now, second.now);
99+
swap(first.sigTime, second.sigTime);
100100
swap(first.lastDseep, second.lastDseep);
101101
swap(first.lastTimeSeen, second.lastTimeSeen);
102102
swap(first.cacheInputAge, second.cacheInputAge);
@@ -138,7 +138,7 @@ class CMasternode
138138
READWRITE(pubkey2);
139139
READWRITE(sig);
140140
READWRITE(activeState);
141-
READWRITE(now);
141+
READWRITE(sigTime);
142142
READWRITE(lastDseep);
143143
READWRITE(lastTimeSeen);
144144
READWRITE(cacheInputAge);

src/masternodeman.cpp

+28-7
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,12 @@ CMasternode *CMasternodeMan::Find(const CTxIn &vin)
268268
return NULL;
269269
}
270270

271-
CMasternode *CMasternodeMan::FindNotInVec(const std::vector<CTxIn> &vVins)
271+
CMasternode* CMasternodeMan::FindOldestNotInVec(const std::vector<CTxIn> &vVins)
272272
{
273273
LOCK(cs);
274274

275+
CMasternode *pOldestMasternode = NULL;
276+
275277
BOOST_FOREACH(CMasternode &mn, vMasternodes)
276278
{
277279
mn.Check();
@@ -287,10 +289,11 @@ CMasternode *CMasternodeMan::FindNotInVec(const std::vector<CTxIn> &vVins)
287289

288290
if(found) continue;
289291

290-
return &mn;
292+
if(pOldestMasternode == NULL || pOldestMasternode->GetMasternodeInputAge() < mn.GetMasternodeInputAge())
293+
pOldestMasternode = &mn;
291294
}
292295

293-
return NULL;
296+
return pOldestMasternode;
294297
}
295298

296299
CMasternode *CMasternodeMan::FindRandom()
@@ -445,10 +448,10 @@ void CMasternodeMan::ProcessMessage(CNode* pfrom, std::string& strCommand, CData
445448
if(count == -1 && pmn->pubkey == pubkey && !pmn->UpdatedWithin(MASTERNODE_MIN_DSEE_SECONDS)){
446449
pmn->UpdateLastSeen();
447450

448-
if(pmn->now < sigTime){ //take the newest entry
451+
if(pmn->sigTime < sigTime){ //take the newest entry
449452
LogPrintf("dsee - Got updated entry for %s\n", addr.ToString().c_str());
450453
pmn->pubkey2 = pubkey2;
451-
pmn->now = sigTime;
454+
pmn->sigTime = sigTime;
452455
pmn->sig = vchSig;
453456
pmn->protocolVersion = protocolVersion;
454457
pmn->addr = addr;
@@ -488,6 +491,24 @@ void CMasternodeMan::ProcessMessage(CNode* pfrom, std::string& strCommand, CData
488491
return;
489492
}
490493

494+
// verify that sig time is legit in past
495+
// should be at least not earlier than block when 1000 DRK tx got MASTERNODE_MIN_CONFIRMATIONS
496+
uint256 hashBlock = 0;
497+
GetTransaction(vin.prevout.hash, tx, hashBlock, true);
498+
map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashBlock);
499+
if (mi != mapBlockIndex.end() && (*mi).second)
500+
{
501+
CBlockIndex* pMNIndex = (*mi).second; // block for 1000 DRK tx -> 1 confirmation
502+
CBlockIndex* pConfIndex = chainActive[pMNIndex->nHeight + MASTERNODE_MIN_CONFIRMATIONS - 1]; // block where tx got MASTERNODE_MIN_CONFIRMATIONS
503+
if(pConfIndex->GetBlockTime() > sigTime)
504+
{
505+
LogPrintf("dsee - Bad sigTime %d for masternode %20s %105s (%i conf block is at %d)\n",
506+
sigTime, addr.ToString(), vin.ToString(), MASTERNODE_MIN_CONFIRMATIONS, pConfIndex->GetBlockTime());
507+
return;
508+
}
509+
}
510+
511+
491512
// use this as a peer
492513
addrman.Add(CAddress(addr), pfrom->addr, 2*60*60);
493514

@@ -624,9 +645,9 @@ void CMasternodeMan::ProcessMessage(CNode* pfrom, std::string& strCommand, CData
624645
{
625646
if(fDebug) LogPrintf("dseg - Sending masternode entry - %s \n", mn.addr.ToString().c_str());
626647
if(vin == CTxIn()){
627-
pfrom->PushMessage("dsee", mn.vin, mn.addr, mn.sig, mn.now, mn.pubkey, mn.pubkey2, count, i, mn.lastTimeSeen, mn.protocolVersion);
648+
pfrom->PushMessage("dsee", mn.vin, mn.addr, mn.sig, mn.sigTime, mn.pubkey, mn.pubkey2, count, i, mn.lastTimeSeen, mn.protocolVersion);
628649
} else if (vin == mn.vin) {
629-
pfrom->PushMessage("dsee", mn.vin, mn.addr, mn.sig, mn.now, mn.pubkey, mn.pubkey2, count, i, mn.lastTimeSeen, mn.protocolVersion);
650+
pfrom->PushMessage("dsee", mn.vin, mn.addr, mn.sig, mn.sigTime, mn.pubkey, mn.pubkey2, count, i, mn.lastTimeSeen, mn.protocolVersion);
630651
LogPrintf("dseg - Sent 1 masternode entries to %s\n", pfrom->addr.ToString().c_str());
631652
return;
632653
}

src/masternodeman.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class CMasternodeMan
9696
CMasternode* Find(const CTxIn& vin);
9797

9898
//Find an entry thta do not match every entry provided vector
99-
CMasternode* FindNotInVec(const std::vector<CTxIn> &vVins);
99+
CMasternode* FindOldestNotInVec(const std::vector<CTxIn> &vVins);
100100

101101
// Find a random entry
102102
CMasternode* FindRandom();

src/rpcdarksend.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ Value masternode(const Array& params, bool fHelp)
468468
obj.push_back(Pair("vin", winner->vin.prevout.hash.ToString().c_str()));
469469
obj.push_back(Pair("pubkey", address2.ToString().c_str()));
470470
obj.push_back(Pair("lastseen", (int64_t)winner->lastTimeSeen));
471-
obj.push_back(Pair("activeseconds", (int64_t)(winner->lastTimeSeen - winner->now)));
471+
obj.push_back(Pair("activeseconds", (int64_t)(winner->lastTimeSeen - winner->sigTime)));
472472
return obj;
473473
}
474474

@@ -627,7 +627,7 @@ Value masternodelist(const Array& params, bool fHelp)
627627
} else if (strMode == "activeseconds") {
628628
if(strFilter !="" && mn.addr.ToString().find(strFilter) == string::npos) continue;
629629

630-
obj.push_back(Pair(strAddr, (int64_t)(mn.lastTimeSeen - mn.now)));
630+
obj.push_back(Pair(strAddr, (int64_t)(mn.lastTimeSeen - mn.sigTime)));
631631
} else if (strMode == "rank") {
632632
if(strFilter !="" && mn.addr.ToString().find(strFilter) == string::npos) continue;
633633
obj.push_back(Pair(strAddr, (int)(mnodeman.GetMasternodeRank(mn.vin, chainActive.Tip()->nHeight))));
@@ -644,7 +644,7 @@ Value masternodelist(const Array& params, bool fHelp)
644644
address2.ToString() << " | " <<
645645
mn.vin.prevout.hash.ToString() << " | " <<
646646
mn.lastTimeSeen << " | " <<
647-
(mn.lastTimeSeen - mn.now);
647+
(mn.lastTimeSeen - mn.sigTime);
648648
std::string output = stringStream.str();
649649
stringStream << " " << strAddr;
650650
if(strFilter !="" && stringStream.str().find(strFilter) == string::npos) continue;

0 commit comments

Comments
 (0)