10 //"github.com/blockchain/rpc/chainhash"
11 "github.com/blockchain/rpc/wire"
14 func solveBlock(header *wire.BlockHeader, targetDifficulty *big.Int) bool {
15 // sbResult is used by the solver goroutines to send results.
16 type sbResult struct {
21 // solver accepts a block header and a nonce range to test. It is
22 // intended to be run as a goroutine.
23 quit := make(chan bool)
24 results := make(chan sbResult)
25 solver := func(hdr wire.BlockHeader, startNonce, stopNonce uint32) {
26 // We need to modify the nonce field of the header, so make sure
27 // we work with a copy of the original header.
28 for i := startNonce; i >= startNonce && i <= stopNonce; i++ {
34 hash := hdr.BlockHash()
35 if wire.HashToBig(&hash).Cmp(targetDifficulty) <= 0 {
36 results <- sbResult{true, i}
41 results <- sbResult{false, 0}
44 startNonce := uint32(1)
45 stopNonce := uint32(math.MaxUint32)
46 numCores := uint32(runtime.NumCPU())
47 noncesPerCore := (stopNonce - startNonce) / numCores
48 for i := uint32(0); i < numCores; i++ {
49 rangeStart := startNonce + (noncesPerCore * i)
50 rangeStop := startNonce + (noncesPerCore * (i + 1)) - 1
54 go solver(*header, rangeStart, rangeStop)
56 for i := uint32(0); i < numCores; i++ {
60 header.Nonce = result.nonce
68 func checkProofOfWork(header *wire.BlockHeader, targetDifficulty *big.Int) bool {
69 hash := hdr.BlockHash()
70 if wire.HashToBig(&hash).Cmp(targetDifficulty) <= 0 {