Skip to content

Commit

Permalink
Masternode System Bug Fixes
Browse files Browse the repository at this point in the history
- Fixed a race condition with masternode node selection upon new blocks
- Using nTime for lastpaid instead of the current time for high consensus
  • Loading branch information
Evan Duffield committed Jun 15, 2015
1 parent b2b2e12 commit c489574
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/activemasternode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ bool CActiveMasternode::Register(std::string strService, std::string strKeyMaste
}

bool CActiveMasternode::Register(CTxIn vin, CService service, CKey keyCollateralAddress, CPubKey pubKeyCollateralAddress, CKey keyMasternode, CPubKey pubKeyMasternode, CScript donationAddress, int donationPercentage, std::string &retErrorMessage) {

CMasternode* pmn = mnodeman.Find(vin);
if(pmn == NULL)
{
Expand All @@ -239,6 +238,7 @@ bool CActiveMasternode::Register(CTxIn vin, CService service, CKey keyCollateral
}

CMasternode mn(mnb);
mn.UpdateLastSeen();
mnodeman.Add(mn);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3240,7 +3240,7 @@ bool ProcessNewBlock(CValidationState &state, CNode* pfrom, CBlock* pblock, CDis
//UPDATE MASTERNODE LAST PAID TIME
CMasternode* pmn = mnodeman.Find(payee);
if(pmn != NULL) {
pmn->nLastPaid = GetAdjustedTime();
pmn->nLastPaid = chainActive.Tip()->nTime;
}
LogPrintf("%s : Update Masternode Last Paid Time - %d\n", __func__, chainActive.Tip()->nHeight);
}
Expand Down
6 changes: 4 additions & 2 deletions src/masternode-payments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ bool CMasternodePayments::GetBlockPayee(int nBlockHeight, CScript& payee)
return false;
}

bool CMasternodePayments::IsScheduled(CMasternode& mn)
bool CMasternodePayments::IsScheduled(CMasternode& mn, int nNotBlockHeight)
{
CBlockIndex* pindexPrev = chainActive.Tip();
if(pindexPrev == NULL) return false;
Expand All @@ -240,6 +240,7 @@ bool CMasternodePayments::IsScheduled(CMasternode& mn)

CScript payee;
for(int64_t h = pindexPrev->nHeight; h <= pindexPrev->nHeight+10; h++){
if(h == nNotBlockHeight) continue;
if(mapMasternodeBlocks.count(h)){
if(mapMasternodeBlocks[h].GetPayee(payee)){
if(mnpayee == payee || mn.donationAddress == payee) {
Expand Down Expand Up @@ -450,14 +451,15 @@ bool CMasternodePayments::ProcessBlock(int nBlockHeight)
} else {
CScript payeeSource;
uint256 hash;

if(!GetBlockHash(hash, nBlockHeight-100)) return false;
unsigned int nHash;
memcpy(&nHash, &hash, 2);

LogPrintf(" ProcessBlock Start nHeight %d. \n", nBlockHeight);

// 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.GetNextMasternodeInQueueForPayment();
CMasternode *pmn = mnodeman.GetNextMasternodeInQueueForPayment(nBlockHeight);
if(pmn != NULL)
{
LogPrintf(" Found by FindOldestNotInVec \n");
Expand Down
2 changes: 1 addition & 1 deletion src/masternode-payments.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ class CMasternodePayments

bool GetBlockPayee(int nBlockHeight, CScript& payee);
bool IsTransactionValid(const CTransaction& txNew, int nBlockHeight);
bool IsScheduled(CMasternode& mn);
bool IsScheduled(CMasternode& mn, int nNotBlockHeight);

void ProcessMessageMasternodePayments(CNode* pfrom, std::string& strCommand, CDataStream& vRecv);
std::string GetRequiredPaymentsString(int nBlockHeight);
Expand Down
9 changes: 3 additions & 6 deletions src/masternodeman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ bool CMasternodeMan::Add(CMasternode &mn)
return false;

CMasternode *pmn = Find(mn.vin);

if (pmn == NULL)
{
if(fDebug) LogPrintf("CMasternodeMan: Adding new Masternode %s - %i now\n", mn.addr.ToString().c_str(), size() + 1);
Expand Down Expand Up @@ -365,7 +364,7 @@ CMasternode *CMasternodeMan::Find(const CPubKey &pubKeyMasternode)
return NULL;
}

CMasternode* CMasternodeMan::GetNextMasternodeInQueueForPayment()
CMasternode* CMasternodeMan::GetNextMasternodeInQueueForPayment(int nBlockHeight)
{
LOCK(cs);

Expand All @@ -377,7 +376,7 @@ CMasternode* CMasternodeMan::GetNextMasternodeInQueueForPayment()
if(!mn.IsEnabled()) continue;

//it's in the list -- so let's skip it
if(masternodePayments.IsScheduled(mn)) continue;
if(masternodePayments.IsScheduled(mn, nBlockHeight)) continue;

//make sure it has as many confirmations as there are masternodes
if(mn.GetMasternodeInputAge() < CountEnabled()) continue;
Expand Down Expand Up @@ -440,13 +439,11 @@ int CMasternodeMan::GetMasternodeRank(const CTxIn& vin, int64_t nBlockHeight, in

// scan for winner
BOOST_FOREACH(CMasternode& mn, vMasternodes) {

if(mn.protocolVersion < minProtocol) continue;
if(fOnlyActive) {
mn.Check();
if(!mn.IsEnabled()) continue;
}

uint256 n = mn.CalculateScore(1, nBlockHeight);
unsigned int n2 = 0;
memcpy(&n2, &n, sizeof(n2));
Expand All @@ -459,7 +456,7 @@ int CMasternodeMan::GetMasternodeRank(const CTxIn& vin, int64_t nBlockHeight, in
int rank = 0;
BOOST_FOREACH (PAIRTYPE(unsigned int, CTxIn)& s, vecMasternodeScores){
rank++;
if(s.second == vin) {
if(s.second.prevout == vin.prevout) {
return rank;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/masternodeman.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class CMasternodeMan
CMasternode* Find(const CPubKey& pubKeyMasternode);

/// Find an entry in the masternode list that is next to be paid
CMasternode* GetNextMasternodeInQueueForPayment();
CMasternode* GetNextMasternodeInQueueForPayment(int nBlockHeight);

/// Find a random entry
CMasternode* FindRandom();
Expand Down

0 comments on commit c489574

Please sign in to comment.