Fix GetBlockHash - Currently it's doing the job already done by chainActive and it's off by 1 block

This commit is contained in:
UdjinM6 2016-08-06 17:31:51 +03:00
parent 5a8c0c9b9d
commit a73c31f3f0
7 changed files with 22 additions and 52 deletions

View File

@ -2339,6 +2339,16 @@ int32_t ComputeBlockVersion(const CBlockIndex* pindexPrev, const Consensus::Para
return nVersion;
}
bool GetBlockHash(uint256& hashRet, int nBlockHeight)
{
LOCK(cs_main);
if(chainActive.Tip() == NULL) return false;
if(nBlockHeight < -1 || nBlockHeight > chainActive.Height()) return false;
if(nBlockHeight == -1) nBlockHeight = chainActive.Height();
hashRet = chainActive[nBlockHeight]->GetBlockHash();
return true;
}
/**
* Threshold condition checker that triggers when unknown versionbits are seen on the network.
*/

View File

@ -565,6 +565,12 @@ int GetSpendHeight(const CCoinsViewCache& inputs);
*/
int32_t ComputeBlockVersion(const CBlockIndex* pindexPrev, const Consensus::Params& params);
/**
* Return true if hash can be found in chainActive at nBlockHeight height.
* Fills hashRet with found hash, if no nBlockHeight is specified - chainActive.Height() is used.
*/
bool GetBlockHash(uint256& hashRet, int nBlockHeight = -1);
/** Reject codes greater or equal to this can be returned by AcceptToMemPool
* for transactions, to signal internal conditions. They cannot and should not
* be sent over the P2P network.

View File

@ -333,9 +333,7 @@ bool CMasternodePayments::IsScheduled(CMasternode& mn, int nNotBlockHeight)
bool CMasternodePayments::AddWinningMasternode(CMasternodePaymentWinner& winnerIn)
{
uint256 blockHash = uint256();
if(!GetBlockHash(blockHash, winnerIn.nBlockHeight-100)) {
return false;
}
if(!GetBlockHash(blockHash, winnerIn.nBlockHeight - 101)) return false;
{
LOCK2(cs_mapMasternodePayeeVotes, cs_mapMasternodeBlocks);
@ -489,7 +487,7 @@ bool CMasternodePaymentWinner::IsValid(CNode* pnode, int nValidationHeight, std:
return false;
}
int nRank = mnodeman.GetMasternodeRank(vinMasternode, nBlockHeight - 100, MIN_MNW_PEER_PROTO_VERSION);
int nRank = mnodeman.GetMasternodeRank(vinMasternode, nBlockHeight - 101, MIN_MNW_PEER_PROTO_VERSION);
if(nRank > MNPAYMENTS_SIGNATURES_TOTAL) {
// It's common to have masternodes mistakenly think they are in the top 10
@ -513,7 +511,7 @@ bool CMasternodePayments::ProcessBlock(int nBlockHeight)
if(!fMasterNode) return false;
int n = mnodeman.GetMasternodeRank(activeMasternode.vin, nBlockHeight-100, MIN_MNW_PEER_PROTO_VERSION);
int n = mnodeman.GetMasternodeRank(activeMasternode.vin, nBlockHeight - 101, MIN_MNW_PEER_PROTO_VERSION);
if(n == -1)
{

View File

@ -17,47 +17,6 @@
// keep track of the scanning errors I've seen
map<uint256, int> mapSeenMasternodeScanningErrors;
// cache block hashes as we calculate them
std::map<int64_t, uint256> mapCacheBlockHashes;
//Get the last hash that matches the modulus given. Processed in reverse order
bool GetBlockHash(uint256& hash, int nBlockHeight)
{
LOCK(cs_main);
if (chainActive.Tip() == NULL) return false;
if(nBlockHeight == 0)
nBlockHeight = chainActive.Tip()->nHeight;
if(mapCacheBlockHashes.count(nBlockHeight)){
hash = mapCacheBlockHashes[nBlockHeight];
return true;
}
const CBlockIndex *BlockLastSolved = chainActive.Tip();
const CBlockIndex *BlockReading = chainActive.Tip();
if (BlockLastSolved == NULL || BlockLastSolved->nHeight == 0 || chainActive.Tip()->nHeight+1 < nBlockHeight) return false;
int nBlocksAgo = 0;
if(nBlockHeight > 0) nBlocksAgo = (chainActive.Tip()->nHeight+1)-nBlockHeight;
assert(nBlocksAgo >= 0);
int n = 0;
for (unsigned int i = 1; BlockReading && BlockReading->nHeight > 0; i++) {
if(n >= nBlocksAgo){
hash = BlockReading->GetBlockHash();
mapCacheBlockHashes[nBlockHeight] = hash;
return true;
}
n++;
if (BlockReading->pprev == NULL) { assert(BlockReading); break; }
BlockReading = BlockReading->pprev;
}
return false;
}
CMasternode::CMasternode()
{
@ -163,7 +122,7 @@ uint256 CMasternode::CalculateScore(int mod, int64_t nBlockHeight)
uint256 aux = ArithToUint256(UintToArith256(vin.prevout.hash) + vin.prevout.n);
if(!GetBlockHash(hash, nBlockHeight)) {
LogPrintf("CalculateScore ERROR - nHeight %d - Returned 0\n", nBlockHeight);
LogPrintf("CMasternode::CalculateScore -- ERROR: GetBlockHash() failed at nBlockHeight %d\n", nBlockHeight);
return uint256();
}

View File

@ -25,9 +25,6 @@ using namespace std;
class CMasternode;
class CMasternodeBroadcast;
class CMasternodePing;
extern map<int64_t, uint256> mapCacheBlockHashes;
bool GetBlockHash(uint256& hash, int nBlockHeight);
//
// The Masternode Ping Class : Contains a different serialize method for sending pings from masternodes throughout the network

View File

@ -333,7 +333,7 @@ CMasternode* CMasternodeMan::GetNextMasternodeInQueueForPayment(int nBlockHeight
CMasternode* pmn = Find(s.second);
if(!pmn) break;
arith_uint256 n = UintToArith256(pmn->CalculateScore(1, nBlockHeight-100));
arith_uint256 n = UintToArith256(pmn->CalculateScore(1, nBlockHeight - 101));
if(n > nHigh){
nHigh = n;
pBestMasternode = pmn;

View File

@ -491,7 +491,7 @@ UniValue masternode(const UniValue& params, bool fHelp)
// arith_uint256 nHigh = 0;
// CMasternode *pBestMasternode = NULL;
// BOOST_FOREACH(CMasternode& mn, vMasternodes) {
// arith_uint256 n = UintToArith256(mn.CalculateScore(1, i - 100));
// arith_uint256 n = UintToArith256(mn.CalculateScore(1, i - 101));
// if(n > nHigh){
// nHigh = n;
// pBestMasternode = &mn;