Skip to content

Commit

Permalink
node votes fix
Browse files Browse the repository at this point in the history
  • Loading branch information
SkaZun committed Nov 9, 2018
1 parent ef29c8b commit d0a5a63
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 94 deletions.
2 changes: 1 addition & 1 deletion configure.ac
Expand Up @@ -3,7 +3,7 @@ AC_PREREQ([2.60])
define(_CLIENT_VERSION_MAJOR, 2)
define(_CLIENT_VERSION_MINOR, 0)
define(_CLIENT_VERSION_REVISION, 3)
define(_CLIENT_VERSION_BUILD, 1)
define(_CLIENT_VERSION_BUILD, 2)
define(_CLIENT_VERSION_IS_RELEASE, true)
define(_COPYRIGHT_YEAR, 2018)
AC_INIT([ESportBettingCoin Core],[_CLIENT_VERSION_MAJOR._CLIENT_VERSION_MINOR._CLIENT_VERSION_REVISION],[http://esbproject.online/],[esbcoin])
Expand Down
2 changes: 1 addition & 1 deletion src/clientversion.cpp
Expand Up @@ -88,7 +88,7 @@ static std::string FormatVersion(int nVersion)

std::string FormatFullVersion()
{
return "2.0.3.1";
return "2.0.3.2";
// return CLIENT_BUILD;
}

Expand Down
2 changes: 1 addition & 1 deletion src/clientversion.h
Expand Up @@ -17,7 +17,7 @@
#define CLIENT_VERSION_MAJOR 2
#define CLIENT_VERSION_MINOR 0
#define CLIENT_VERSION_REVISION 3
#define CLIENT_VERSION_BUILD 1
#define CLIENT_VERSION_BUILD 2

//! Set to true for release, false for prerelease or test build
#define CLIENT_VERSION_IS_RELEASE true
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Expand Up @@ -3544,7 +3544,7 @@ bool ProcessNewBlock(CValidationState& state, CNode* pfrom, CBlock* pblock, CDis
if (!fLiteMode) {
if (masternodeSync.RequestedMasternodeAssets > MASTERNODE_SYNC_LIST) {
obfuScationPool.NewBlock();
masternodePayments.ProcessBlock(GetHeight() + 10);
masternodePayments.ProcessBlock(GetHeight());
}
}

Expand Down
67 changes: 40 additions & 27 deletions src/masternode-payments.cpp
Expand Up @@ -14,6 +14,7 @@
#include "sync.h"
#include "util.h"
#include "utilmoneystr.h"
#include <algorithm>
#include <boost/filesystem.hpp>
#include <boost/lexical_cast.hpp>

Expand All @@ -27,18 +28,18 @@ CCriticalSection cs_mapMasternodePayeeVotes;
bool IsBlockValueValid(const CBlock& block, CAmount nExpectedValue, CAmount nMinted)
{
CBlockIndex* pindexPrev = chainActive.Tip();
if (pindexPrev == NULL) return true;
if (!pindexPrev) return true;

int nHeight = 0;
if (pindexPrev->GetBlockHash() == block.hashPrevBlock) {
nHeight = pindexPrev->nHeight + 1;
} else { //out of order
BlockMap::iterator mi = mapBlockIndex.find(block.hashPrevBlock);
if (mi != mapBlockIndex.end() && (*mi).second)
nHeight = (*mi).second->nHeight + 1;
if (mi != mapBlockIndex.end() && mi->second)
nHeight = mi->second->nHeight + 1;
}

if (nHeight == 0) {
if (!nHeight) {
LogPrintf("IsBlockValueValid() : WARNING: Couldn't find previous block\n");
}

Expand Down Expand Up @@ -117,7 +118,7 @@ CAmount CMasternodePayments::FillBlockPayee(CMutableTransaction& txNew, CAmount

LogPrintf("Masternode payment of %s to %s\n", FormatMoney(masternodePayment).c_str(), address2.ToString().c_str());
}

return mn_payments_total;

}
Expand Down Expand Up @@ -173,7 +174,7 @@ void CMasternodePayments::ProcessMessageMasternodePayments(CNode* pfrom, std::st
auto winner_mn = mnodeman.Find(winner.payee);

if (!winner_mn) {
LogPrintf(/*"mnpayments", */"mnw - unknown payee %s\n", payee_addr.ToString().c_str());
LogPrintf("mnpayments", "mnw - unknown payee %s\n", payee_addr.ToString().c_str());
return;
}

Expand All @@ -191,7 +192,7 @@ void CMasternodePayments::ProcessMessageMasternodePayments(CNode* pfrom, std::st
return;
}

int nFirstBlock = nHeight - mnodeman.CountEnabled(winner.payeeLevel) / 100 * 125;
int nFirstBlock = nHeight - int(mnodeman.CountEnabled(winner.payeeLevel) * 1.25); // / 100 * 125;
if (winner.nBlockHeight < nFirstBlock || winner.nBlockHeight > nHeight + 20) {
LogPrint("mnpayments", "mnw - winner out of range - FirstBlock %d Height %d bestHeight %d\n", nFirstBlock, winner.nBlockHeight, nHeight);
return;
Expand All @@ -213,6 +214,7 @@ void CMasternodePayments::ProcessMessageMasternodePayments(CNode* pfrom, std::st
LogPrint("mnpayments", "mnw - winning vote - Addr %s Height %d bestHeight %d - %s\n", payee_addr.ToString().c_str(), winner.nBlockHeight, nHeight, winner.vinMasternode.prevout.ToStringShort());

if (masternodePayments.AddWinningMasternode(winner)) {
LogPrintf("add winner %s\n", winner.ToString());
winner.Relay();
masternodeSync.AddedMasternodeWinner(winner.GetHash());
}
Expand Down Expand Up @@ -253,7 +255,7 @@ bool CMasternodePayments::GetBlockPayee(int nBlockHeight, unsigned mnlevel, CScr

// Is this masternode scheduled to get paid soon?
// -- Only look ahead up to 8 blocks to allow for propagation of the latest 2 winners
bool CMasternodePayments::IsScheduled(CMasternode& mn, int nNotBlockHeight) const
bool CMasternodePayments::IsScheduled(CMasternode& mn, int nSameLevelMNCount, int nNotBlockHeight) const
{
LOCK(cs_mapMasternodeBlocks);

Expand All @@ -274,7 +276,8 @@ bool CMasternodePayments::IsScheduled(CMasternode& mn, int nNotBlockHeight) cons

CScript mnpayee = GetScriptForDestination(mn.pubKeyCollateralAddress.GetID());

for(int64_t h = nHeight; h <= nHeight + 8; ++h) {
for(int64_t h_upper_bound = nHeight + 10, h = h_upper_bound - std::min(10, nSameLevelMNCount - 1); h < h_upper_bound; ++h) {
// for(int64_t h = nHeight; h <= nHeight + 8; ++h) {

if(h == nNotBlockHeight)
continue;
Expand All @@ -292,15 +295,15 @@ bool CMasternodePayments::IsScheduled(CMasternode& mn, int nNotBlockHeight) cons
if(mnpayee == payee)
return true;
}

return false;
}

bool CMasternodePayments::CanVote(const COutPoint& outMasternode, int nBlockHeight, unsigned mnlevel)
{
LOCK(cs_mapMasternodePayeeVotes);

uint256 key = outMasternode.hash + outMasternode.n + mnlevel;
uint256 key = ((outMasternode.hash + outMasternode.n) << 4) + mnlevel;
// uint256 key = outMasternode.hash + outMasternode.n + mnlevel;

auto ins_res = mapMasternodesLastVote.emplace(key, nBlockHeight);

Expand All @@ -309,7 +312,7 @@ bool CMasternodePayments::CanVote(const COutPoint& outMasternode, int nBlockHeig
auto& last_vote = ins_res.first->second;

// if(last_vote <= nBlockHeight)
if(last_vote == nBlockHeight)
if(last_vote >= nBlockHeight)
return false;

last_vote = nBlockHeight;
Expand Down Expand Up @@ -477,7 +480,7 @@ void CMasternodePayments::CleanPaymentList()
}

//keep up to five cycles for historical sake
int nLimit = std::max(mnodeman.size() / 100 * 125, 1000);
int nLimit = std::max(int(mnodeman.size() * 1.25), 1000); /*/ 100 * 125*/

std::map<uint256, CMasternodePaymentWinner>::iterator it = mapMasternodePayeeVotes.begin();
while (it != mapMasternodePayeeVotes.end()) {
Expand Down Expand Up @@ -538,24 +541,28 @@ bool CMasternodePayments::ProcessBlock(int nBlockHeight)
if(!fMasterNode)
return false;

auto nWinnerBlockHeight = nBlockHeight + 10;

//reference node - hybrid mode

if(nBlockHeight <= nLastBlockHeight)
//if(nBlockHeight <= nLastBlockHeight)
if(nWinnerBlockHeight <= nLastBlockHeight)
return false;

int n = mnodeman.GetMasternodeRank(activeMasternode.vin, nBlockHeight - 100, ActiveProtocol());
int n = mnodeman.GetMasternodeRank(activeMasternode.vin, nWinnerBlockHeight - 100, ActiveProtocol());

if(n == -1) {
/* for testing
if (n == -1) {
LogPrint("mnpayments", "CMasternodePayments::ProcessBlock - Unknown Masternode\n");
return false;
}
if(n > MNPAYMENTS_SIGNATURES_TOTAL) {
if (n > MNPAYMENTS_SIGNATURES_TOTAL) {
LogPrint("mnpayments", "CMasternodePayments::ProcessBlock - Masternode not in the top %d (%d)\n", MNPAYMENTS_SIGNATURES_TOTAL, n);
return false;
}

LogPrintf("CMasternodePayments::ProcessBlock() Start nHeight %d - vin %s. \n", nBlockHeight, activeMasternode.vin.prevout.hash.ToString());
*/
LogPrintf("CMasternodePayments::ProcessBlock() Start nHeight %d - vin %s. \n", nWinnerBlockHeight, activeMasternode.vin.prevout.hash.ToString());
// pay to the oldest MN that still had no payment but its input is old enough and it was active long enough

std::string errorMessage;
Expand All @@ -571,11 +578,9 @@ bool CMasternodePayments::ProcessBlock(int nBlockHeight)

for(unsigned mnlevel = CMasternode::LevelValue::MIN; mnlevel <= CMasternode::LevelValue::MAX; ++mnlevel) {

CMasternodePaymentWinner newWinner{activeMasternode.vin};

unsigned nCount = 0;

auto pmn = mnodeman.GetNextMasternodeInQueueForPayment(nBlockHeight, mnlevel, true, nCount);
auto pmn = mnodeman.GetNextMasternodeInQueueForPayment(nWinnerBlockHeight, mnlevel, true, nCount);

if(!pmn) {
LogPrintf("CMasternodePayments::ProcessBlock() Failed to find masternode level %d to pay \n", mnlevel);
Expand All @@ -584,7 +589,8 @@ bool CMasternodePayments::ProcessBlock(int nBlockHeight)

auto payee = GetScriptForDestination(pmn->pubKeyCollateralAddress.GetID());

newWinner.nBlockHeight = nBlockHeight;
CMasternodePaymentWinner newWinner{activeMasternode.vin};
newWinner.nBlockHeight = nWinnerBlockHeight;
newWinner.AddPayee(payee, mnlevel);

CTxDestination address1;
Expand All @@ -606,14 +612,18 @@ bool CMasternodePayments::ProcessBlock(int nBlockHeight)
winners.emplace_back(newWinner);
}

// for testing
if (n > MNPAYMENTS_SIGNATURES_TOTAL || n == -1)
return false;

if(winners.empty())
return false;

for(auto& winner : winners) {
winner.Relay();
}

nLastBlockHeight = nBlockHeight;
nLastBlockHeight = nWinnerBlockHeight;

return true;
}
Expand Down Expand Up @@ -658,18 +668,21 @@ void CMasternodePayments::Sync(CNode* node, int nCountNeeded)
}

auto mn_counts = mnodeman.CountEnabledByLevels();
unsigned max_mn_count = 0u;

for(auto& count : mn_counts)
count.second = std::min(nCountNeeded, count.second / 100 * 125);
max_mn_count = std::max(max_mn_count, unsigned(count.second * 1.25));
//count.second = std::min(nCountNeeded, int(count.second * 1.25)); /*/ 100 * 125*/

if(max_mn_count > nCountNeeded) max_mn_count = nCountNeeded;

int nInvCount = 0;

for(const auto& vote : mapMasternodePayeeVotes) {

const auto& winner = vote.second;

bool push = winner.nBlockHeight >= nHeight - mn_counts[winner.payeeLevel]
&& winner.nBlockHeight <= nHeight + 20;
bool push = winner.nBlockHeight >= nHeight - max_mn_count && winner.nBlockHeight <= nHeight + 20;

if(!push)
continue;
Expand Down
4 changes: 2 additions & 2 deletions src/masternode-payments.h
Expand Up @@ -219,7 +219,7 @@ class CMasternodePayments
public:
std::map<uint256, CMasternodePaymentWinner> mapMasternodePayeeVotes;
std::map<int, CMasternodeBlockPayees> mapMasternodeBlocks;
std::map<uint256, int> mapMasternodesLastVote; //prevout.hash + prevout.n + mnlevel, nBlockHeight
std::map<uint256, int> mapMasternodesLastVote;

CMasternodePayments()
{
Expand All @@ -242,7 +242,7 @@ class CMasternodePayments

bool GetBlockPayee(int nBlockHeight, unsigned mnlevel, CScript& payee);
bool IsTransactionValid(const CTransaction& txNew, int nBlockHeight);
bool IsScheduled(CMasternode& mn, int nNotBlockHeight) const;
bool IsScheduled(CMasternode& mn, int nSameLevelMNCount, int nNotBlockHeight) const;
bool CanVote(const COutPoint& outMasternode, int nBlockHeight, unsigned mnlevel);

int GetMinMasternodePaymentsProto();
Expand Down
10 changes: 5 additions & 5 deletions src/masternode.cpp
Expand Up @@ -257,7 +257,7 @@ int64_t CMasternode::GetLastPaid()
CBlockIndex* pindexPrev = chainActive.Tip();

if (!pindexPrev)
return false;
return 0;

CScript mnpayee;
mnpayee = GetScriptForDestination(pubKeyCollateralAddress.GetID());
Expand All @@ -267,12 +267,12 @@ int64_t CMasternode::GetLastPaid()
ss << sigTime;
uint256 hash = ss.GetHash();

// use a deterministic offset to break a tie -- 2.5 minutes
int64_t nOffset = hash.GetCompact(false) % 150;
// use a deterministic offset to break a tie -- 1.5 minutes
int64_t nOffset = hash.GetCompact(false) % 90;

const CBlockIndex* BlockReading = pindexPrev;

int nMnCount = mnodeman.CountEnabled(Level()) / 100 * 125;
int nMnCount = int(mnodeman.CountEnabled(Level()) / 1.25);
int n = 0;
for (unsigned int i = 1; BlockReading && BlockReading->nHeight > 0; i++) {
if (n >= nMnCount) {
Expand All @@ -286,7 +286,7 @@ int64_t CMasternode::GetLastPaid()
to converge on the same payees quickly, then keep the same schedule.
*/
if (masternodePayments.mapMasternodeBlocks[BlockReading->nHeight].HasPayeeWithVotes(mnpayee, 2)) {
return BlockReading->nTime + nOffset;
return BlockReading->nTime - nOffset;
}
}

Expand Down

0 comments on commit d0a5a63

Please sign in to comment.