Browse Source

Simplify hash loop code

0.13
Pieter Wuille 10 years ago
parent
commit
0df67f1f7a
  1. 30
      src/miner.cpp

30
src/miner.cpp

@ -365,33 +365,23 @@ void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int&
// //
// ScanHash scans nonces looking for a hash with at least some zero bits. // ScanHash scans nonces looking for a hash with at least some zero bits.
// The nonce is usually preserved between calls, but periodically or if the // The nonce is usually preserved between calls, but periodically the block is
// nonce is 0xffff0000 or above, the block is rebuilt and nNonce starts over at // rebuilt and nNonce starts over at zero.
// zero.
// //
bool static ScanHash(const CBlockHeader *pblock, uint32_t& nNonce, uint256 *phash) bool static ScanHash(CBlockHeader *pblock, uint256 *phash)
{ {
// Write the first 76 bytes of the block header to a double-SHA256 state.
CHash256 hasher;
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
ss << *pblock;
assert(ss.size() == 80);
hasher.Write((unsigned char*)&ss[0], 76);
while (true) { while (true) {
nNonce++; pblock->nNonce++;
// Write the last 4 bytes of the block header (the nonce) to a copy of *phash = (CHashWriter(SER_GETHASH, 0) << *pblock).GetHash();
// the double-SHA256 state, and compute the result.
CHash256(hasher).Write((unsigned char*)&nNonce, 4).Finalize((unsigned char*)phash);
// Return the nonce if the hash has at least some zero bits, // Return the nonce if the hash has at least some zero bits,
// caller will check if it has enough to reach the target // caller will check if it has enough to reach the target
if (((uint16_t*)phash)[15] == 0) if (((uint16_t*)phash)[15] == 0)
return true; return true;
// If nothing found after trying for a while, return -1 // If nothing found after trying for a while, return false.
if ((nNonce & 0xfff) == 0) if ((pblock->nNonce & 0xfff) == 0)
return false; return false;
} }
} }
@ -478,15 +468,13 @@ void static BitcoinMiner(CWallet *pwallet)
int64_t nStart = GetTime(); int64_t nStart = GetTime();
arith_uint256 hashTarget = arith_uint256().SetCompact(pblock->nBits); arith_uint256 hashTarget = arith_uint256().SetCompact(pblock->nBits);
uint256 hash; uint256 hash;
uint32_t nNonce = 0;
while (true) { while (true) {
// Check if something found // Check if something found
if (ScanHash(pblock, nNonce, &hash)) if (ScanHash(pblock, &hash))
{ {
if (UintToArith256(hash) <= hashTarget) if (UintToArith256(hash) <= hashTarget)
{ {
// Found a solution // Found a solution
pblock->nNonce = nNonce;
assert(hash == pblock->GetHash()); assert(hash == pblock->GetHash());
SetThreadPriority(THREAD_PRIORITY_NORMAL); SetThreadPriority(THREAD_PRIORITY_NORMAL);
@ -508,7 +496,7 @@ void static BitcoinMiner(CWallet *pwallet)
// Regtest mode doesn't require peers // Regtest mode doesn't require peers
if (vNodes.empty() && Params().MiningRequiresPeers()) if (vNodes.empty() && Params().MiningRequiresPeers())
break; break;
if (nNonce >= 0xffff0000) if (pblock->nNonce >= 0xffff0000)
break; break;
if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 60) if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 60)
break; break;

Loading…
Cancel
Save