Skip to content

Commit 0df67f1

Browse files
committed
Simplify hash loop code
1 parent 15facb4 commit 0df67f1

File tree

1 file changed

+9
-21
lines changed

1 file changed

+9
-21
lines changed

src/miner.cpp

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -365,33 +365,23 @@ void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int&
365365

366366
//
367367
// ScanHash scans nonces looking for a hash with at least some zero bits.
368-
// The nonce is usually preserved between calls, but periodically or if the
369-
// nonce is 0xffff0000 or above, the block is rebuilt and nNonce starts over at
370-
// zero.
368+
// The nonce is usually preserved between calls, but periodically the block is
369+
// rebuilt and nNonce starts over at zero.
371370
//
372-
bool static ScanHash(const CBlockHeader *pblock, uint32_t& nNonce, uint256 *phash)
371+
bool static ScanHash(CBlockHeader *pblock, uint256 *phash)
373372
{
374-
// Write the first 76 bytes of the block header to a double-SHA256 state.
375-
CHash256 hasher;
376-
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
377-
ss << *pblock;
378-
assert(ss.size() == 80);
379-
hasher.Write((unsigned char*)&ss[0], 76);
380-
381373
while (true) {
382-
nNonce++;
374+
pblock->nNonce++;
383375

384-
// Write the last 4 bytes of the block header (the nonce) to a copy of
385-
// the double-SHA256 state, and compute the result.
386-
CHash256(hasher).Write((unsigned char*)&nNonce, 4).Finalize((unsigned char*)phash);
376+
*phash = (CHashWriter(SER_GETHASH, 0) << *pblock).GetHash();
387377

388378
// Return the nonce if the hash has at least some zero bits,
389379
// caller will check if it has enough to reach the target
390380
if (((uint16_t*)phash)[15] == 0)
391381
return true;
392382

393-
// If nothing found after trying for a while, return -1
394-
if ((nNonce & 0xfff) == 0)
383+
// If nothing found after trying for a while, return false.
384+
if ((pblock->nNonce & 0xfff) == 0)
395385
return false;
396386
}
397387
}
@@ -478,15 +468,13 @@ void static BitcoinMiner(CWallet *pwallet)
478468
int64_t nStart = GetTime();
479469
arith_uint256 hashTarget = arith_uint256().SetCompact(pblock->nBits);
480470
uint256 hash;
481-
uint32_t nNonce = 0;
482471
while (true) {
483472
// Check if something found
484-
if (ScanHash(pblock, nNonce, &hash))
473+
if (ScanHash(pblock, &hash))
485474
{
486475
if (UintToArith256(hash) <= hashTarget)
487476
{
488477
// Found a solution
489-
pblock->nNonce = nNonce;
490478
assert(hash == pblock->GetHash());
491479

492480
SetThreadPriority(THREAD_PRIORITY_NORMAL);
@@ -508,7 +496,7 @@ void static BitcoinMiner(CWallet *pwallet)
508496
// Regtest mode doesn't require peers
509497
if (vNodes.empty() && Params().MiningRequiresPeers())
510498
break;
511-
if (nNonce >= 0xffff0000)
499+
if (pblock->nNonce >= 0xffff0000)
512500
break;
513501
if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 60)
514502
break;

0 commit comments

Comments
 (0)