diff options
author | Pieter Wuille <pieter.wuille@gmail.com> | 2015-03-31 19:15:42 -0700 |
---|---|---|
committer | Pieter Wuille <pieter.wuille@gmail.com> | 2015-04-01 10:25:51 -0700 |
commit | 0df67f1f7ab4adfe9f0b3ba6276e737b37826464 (patch) | |
tree | 378af98b2dda4e0affddd33dbef90abe5bc53370 /src/miner.cpp | |
parent | 15facb4aca75122b6ae0dcc6f6e112127e6a0e59 (diff) |
Simplify hash loop code
Diffstat (limited to 'src/miner.cpp')
-rw-r--r-- | src/miner.cpp | 30 |
1 files changed, 9 insertions, 21 deletions
diff --git a/src/miner.cpp b/src/miner.cpp index cf08b78229..44661fd13e 100644 --- a/src/miner.cpp +++ b/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. -// The nonce is usually preserved between calls, but periodically or if the -// nonce is 0xffff0000 or above, the block is rebuilt and nNonce starts over at -// zero. +// The nonce is usually preserved between calls, but periodically the block is +// rebuilt and nNonce starts over at 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) { - nNonce++; + pblock->nNonce++; - // Write the last 4 bytes of the block header (the nonce) to a copy of - // the double-SHA256 state, and compute the result. - CHash256(hasher).Write((unsigned char*)&nNonce, 4).Finalize((unsigned char*)phash); + *phash = (CHashWriter(SER_GETHASH, 0) << *pblock).GetHash(); // Return the nonce if the hash has at least some zero bits, // caller will check if it has enough to reach the target if (((uint16_t*)phash)[15] == 0) return true; - // If nothing found after trying for a while, return -1 - if ((nNonce & 0xfff) == 0) + // If nothing found after trying for a while, return false. + if ((pblock->nNonce & 0xfff) == 0) return false; } } @@ -478,15 +468,13 @@ void static BitcoinMiner(CWallet *pwallet) int64_t nStart = GetTime(); arith_uint256 hashTarget = arith_uint256().SetCompact(pblock->nBits); uint256 hash; - uint32_t nNonce = 0; while (true) { // Check if something found - if (ScanHash(pblock, nNonce, &hash)) + if (ScanHash(pblock, &hash)) { if (UintToArith256(hash) <= hashTarget) { // Found a solution - pblock->nNonce = nNonce; assert(hash == pblock->GetHash()); SetThreadPriority(THREAD_PRIORITY_NORMAL); @@ -508,7 +496,7 @@ void static BitcoinMiner(CWallet *pwallet) // Regtest mode doesn't require peers if (vNodes.empty() && Params().MiningRequiresPeers()) break; - if (nNonce >= 0xffff0000) + if (pblock->nNonce >= 0xffff0000) break; if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 60) break; |