GetPubKeyNonce will make his best effort to find a difficult enough nonce.
(
publicKey *PublicKey,
difficulty int,
timeThreshold time.Duration,
quit chan struct{})
| 186 | // GetPubKeyNonce will make his best effort to find a difficult enough |
| 187 | // nonce. |
| 188 | func GetPubKeyNonce( |
| 189 | publicKey *PublicKey, |
| 190 | difficulty int, |
| 191 | timeThreshold time.Duration, |
| 192 | quit chan struct{}) (nonce mine.NonceInfo) { |
| 193 | |
| 194 | miner := mine.NewCPUMiner(quit) |
| 195 | nonceCh := make(chan mine.NonceInfo) |
| 196 | // if miner finished his work before timeThreshold |
| 197 | // make sure writing to the Stop chan non-blocking. |
| 198 | stop := make(chan struct{}, 1) |
| 199 | block := mine.MiningBlock{ |
| 200 | Data: publicKey.Serialize(), |
| 201 | NonceChan: nonceCh, |
| 202 | Stop: stop, |
| 203 | } |
| 204 | |
| 205 | go miner.ComputeBlockNonce(block, mine.Uint256{}, difficulty) |
| 206 | |
| 207 | time.Sleep(timeThreshold) |
| 208 | // stop miner |
| 209 | block.Stop <- struct{}{} |
| 210 | |
| 211 | return <-block.NonceChan |
| 212 | } |