From: HAOYUatHZ Date: Thu, 3 May 2018 12:02:45 +0000 (+0800) Subject: update miner X-Git-Tag: v1.0.5~14^2~20^2~1 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=116062cf59ca5992f881a364610d9e0bb567f487;p=bytom%2Fbytom.git update miner --- diff --git a/cmd/miner/main.go b/cmd/miner/main.go index 21ccadc4..07a2dbbd 100644 --- a/cmd/miner/main.go +++ b/cmd/miner/main.go @@ -6,6 +6,7 @@ import ( "os" "github.com/bytom/api" + "github.com/bytom/consensus" "github.com/bytom/consensus/difficulty" "github.com/bytom/protocol/bc" "github.com/bytom/protocol/bc/types" @@ -14,19 +15,27 @@ import ( const ( maxNonce = ^uint64(0) // 2^64 - 1 - isCrazy = false + isCrazy = true + esHR = 1 //estimated Hashrate +) + +var ( + lastNonce = ^uint64(0) + lastHeight = uint64(0) ) // do proof of work func doWork(bh *types.BlockHeader, seed *bc.Hash) bool { - for i := uint64(0); i <= maxNonce; i++ { + for i := uint64(lastNonce + 1); i <= uint64(lastNonce+consensus.TargetSecondsPerBlock*esHR) && i <= maxNonce; i++ { bh.Nonce = i + log.Printf("nonce = %v\n", i) headerHash := bh.Hash() if difficulty.CheckProofOfWork(&headerHash, seed, bh.Bits) { log.Printf("Mining succeed! Proof hash: %v\n", headerHash.String()) return true } } + lastNonce = bh.Nonce return false } @@ -69,14 +78,17 @@ func main() { } log.Println("Mining at height:", resp.BlockHeader.Height) + if lastHeight != resp.BlockHeader.Height { + lastNonce = ^uint64(0) + } if doWork(resp.BlockHeader, resp.Seed) { util.ClientCall("/submit-work", &api.SubmitWorkReq{BlockHeader: resp.BlockHeader}) + getBlockHeaderByHeight(resp.BlockHeader.Height) } - getBlockHeaderByHeight(resp.BlockHeader.Height) - + lastHeight = resp.BlockHeader.Height if !isCrazy { return - } + } } }