ComputeBlockNonce find nonce make HashBlock() match the MiningBlock Difficulty from the startNonce if interrupted or stopped highest difficulty nonce will be sent to the NonceCh HACK(auxten): make calculation parallel.
( block MiningBlock, startNonce Uint256, difficulty int, )
| 64 | // if interrupted or stopped highest difficulty nonce will be sent to the NonceCh |
| 65 | // HACK(auxten): make calculation parallel. |
| 66 | func (miner *CPUMiner) ComputeBlockNonce( |
| 67 | block MiningBlock, |
| 68 | startNonce Uint256, |
| 69 | difficulty int, |
| 70 | ) (err error) { |
| 71 | |
| 72 | var ( |
| 73 | bestNonce NonceInfo |
| 74 | ) |
| 75 | for i := startNonce; ; i.Inc() { |
| 76 | select { |
| 77 | case <-block.Stop: |
| 78 | log.Info("stop block nonce job") |
| 79 | block.NonceChan <- bestNonce |
| 80 | return errors.New("mining job stopped") |
| 81 | case <-miner.quit: |
| 82 | log.Info("stop block nonce worker") |
| 83 | block.NonceChan <- bestNonce |
| 84 | return errors.New("miner interrupted") |
| 85 | default: |
| 86 | currentHash := HashBlock(block.Data, i) |
| 87 | currentDifficulty := currentHash.Difficulty() |
| 88 | if currentDifficulty >= difficulty { |
| 89 | bestNonce.Difficulty = currentDifficulty |
| 90 | bestNonce.Nonce = i |
| 91 | bestNonce.Hash.SetBytes(currentHash[:]) |
| 92 | block.NonceChan <- bestNonce |
| 93 | return |
| 94 | } |
| 95 | if currentDifficulty > bestNonce.Difficulty { |
| 96 | bestNonce.Difficulty = currentDifficulty |
| 97 | bestNonce.Nonce = i |
| 98 | bestNonce.Hash.SetBytes(currentHash[:]) |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | } |