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.
| 279 | // zero. |
| 280 | // |
| 281 | bool static ScanHash(const CBlockHeader *pblock, uint32_t& nNonce, uint256 *phash) |
| 282 | { |
| 283 | // Write the first 76 bytes of the block header to a double-SHA256 state. |
| 284 | CHash256 hasher; |
| 285 | CDataStream ss(SER_NETWORK, PROTOCOL_VERSION); |
| 286 | ss << *pblock; |
| 287 | assert(ss.size() == 80); |
| 288 | hasher.Write((unsigned char*)&ss[0], 76); |
| 289 | |
| 290 | while (true) { |
| 291 | nNonce++; |
| 292 | |
| 293 | // Write the last 4 bytes of the block header (the nonce) to a copy of |
| 294 | // the double-SHA256 state, and compute the result. |
| 295 | CHash256(hasher).Write((unsigned char*)&nNonce, 4).Finalize((unsigned char*)phash); |
| 296 | |
| 297 | // Return the nonce if the hash has at least some zero bits, |
| 298 | // caller will check if it has enough to reach the target |
| 299 | if (((uint16_t*)phash)[15] == 0) |
| 300 | return true; |
| 301 | |
| 302 | // If nothing found after trying for a while, return -1 |
| 303 | if ((nNonce & 0xfff) == 0) |
| 304 | return false; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey) |
| 309 | { |
no test coverage detected