// // Deterministically calculate a given "score" for a Masternode depending on how close it's hash is to // the proof of work for that block. The further away they are the better, the furthest will win the election // and get paid this block // uint256 CMasternode::CalculateScore(int mod, int64_t nBlockHeight) { if(chainActive.Tip() == NULL) return 0; uint256 hash = 0; uint256 aux = vin.prevout.hash + vin.prevout.n; if(!GetBlockHash(hash, nBlockHeight)) { LogPrintf("CalculateScore ERROR - nHeight %d - Returned 0\n", nBlockHeight); return 0; } CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION); ss << hash; uint256 hash2 = ss.GetHash(); CHashWriter ss2(SER_GETHASH, PROTOCOL_VERSION); ss2 << hash; ss2 << aux; uint256 hash3 = ss2.GetHash(); uint256 r = (hash3 > hash2 ? hash3 - hash2 : hash2 - hash3); return r; }
string CBlockIndex::ToString() const { return strprintf("CBlockIndex(nprev=%08x, pnext=%08x, nFile=%d, nBlockPos=%-6d nHeight=%d, merkle=%s, hashBlock=%s)", pprev, pnext, nFile, nBlockPos, nHeight, hashMerkleRoot.ToString().substr(0,10).c_str(), GetBlockHash().ToString().substr(0,20).c_str()); }
string CDiskBlockIndex::ToString() const { string str = "CDiskBlockIndex("; str += CBlockIndex::ToString(); str += strprintf("\n hashBlock=%s, hashPrev=%s, hashNext=%s)", GetBlockHash().ToString().c_str(), hashPrev.ToString().substr(0,20).c_str(), hashNext.ToString().substr(0,20).c_str()); return str; }
// // Deterministically calculate a given "score" for a alphanode depending on how close it's hash is to // the proof of work for that block. The further away they are the better, the furthest will win the election // and get paid this block // uint256 CAlphanode::CalculateScore(int mod, int64_t nBlockHeight) { if(pindexBest == NULL) return 0; uint256 hash = 0; uint256 aux = vin.prevout.hash + vin.prevout.n; if(!GetBlockHash(hash, nBlockHeight)) return 0; uint256 hash2 = Hash(BEGIN(hash), END(hash)); uint256 hash3 = Hash(BEGIN(hash), END(hash), BEGIN(aux), END(aux)); uint256 r = (hash3 > hash2 ? hash3 - hash2 : hash2 - hash3); return r; }
// Check kernel hash target and coinstake signature bool CheckProofOfStake(const CBlock& block, const CBlockIndex* prevBlock, const COutPoint& outpointStakePointer, uint256& hashProofOfStake) { const CTransaction tx = block.vtx[1]; if (!tx.IsCoinStake()) return error("CheckProofOfStake() : called on non-coinstake %s", tx.GetHash().ToString().c_str()); // Get the stake modifier auto pindexModifier = prevBlock->GetAncestor(prevBlock->nHeight - Params().KernelModifierOffset()); if (!pindexModifier) return error("CheckProofOfStake() : could not find modifier index for stake"); uint256 nStakeModifier = pindexModifier->GetBlockHash(); // Get the correct amount for the collateral CAmount nAmountCollateral = 0; if (outpointStakePointer.n == 1) nAmountCollateral = MASTERNODE_COLLATERAL; else if (outpointStakePointer.n == 2) nAmountCollateral = SYSTEMNODE_COLLATERAL; else return error("%s: Stake pointer is neither pos 1 or 2", __func__); // Reconstruct the kernel that created the stake auto pairOut = std::make_pair(outpointStakePointer.hash, outpointStakePointer.n); Kernel kernel(pairOut, nAmountCollateral, nStakeModifier, prevBlock->GetBlockTime(), block.nTime); bool fNegative; bool fOverflow; arith_uint256 bnTarget; bnTarget.SetCompact(block.nBits, &fNegative, &fOverflow); // Check range if (fNegative || bnTarget == 0 || fOverflow) return error("CheckProofOfStake() : nBits below minimum stake"); LogPrintf("%s : %s\n", __func__, kernel.ToString()); hashProofOfStake = kernel.GetStakeHash(); return kernel.IsValidProof(ArithToUint256(bnTarget)); }
bool CBlockIndex::CheckIndex() const { return CheckProofOfWork(GetBlockHash(), nBits); }