9 "github.com/vapor/consensus"
10 "github.com/vapor/consensus/difficulty"
11 "github.com/vapor/protocol/bc"
12 "github.com/vapor/protocol/bc/types"
13 "github.com/vapor/util"
17 maxNonce = ^uint64(0) // 2^64 - 1
19 esHR = 1 //estimated Hashrate
23 lastNonce = ^uint64(0)
24 lastHeight = uint64(0)
28 func doWork(bh *types.BlockHeader, seed *bc.Hash) bool {
29 log.Println("Start from nonce:", lastNonce+1)
30 for i := uint64(lastNonce + 1); i <= uint64(lastNonce+consensus.TargetSecondsPerBlock*esHR) && i <= maxNonce; i++ {
32 // log.Printf("nonce = %v\n", i)
33 headerHash := bh.Hash()
34 if difficulty.CheckProofOfWork(&headerHash, seed, bh.Bits) {
35 log.Printf("Mining succeed! Proof hash: %v\n", headerHash.String())
39 log.Println("Stop at nonce:", bh.Nonce)
44 func getBlockHeaderByHeight(height uint64) {
46 BlockHeight uint64 `json:"block_height"`
50 BlockHeader *types.BlockHeader `json:"block_header"`
51 Reward uint64 `json:"reward"`
54 data, _ := util.ClientCall("/get-block-header", Req{BlockHeight: height})
55 rawData, err := json.Marshal(data)
61 if err = json.Unmarshal(rawData, resp); err != nil {
64 log.Println("Reward:", resp.Reward)
69 data, _ := util.ClientCall("/get-work", nil)
73 rawData, err := json.Marshal(data)
77 resp := &api.GetWorkResp{}
78 if err = json.Unmarshal(rawData, resp); err != nil {
82 log.Println("Mining at height:", resp.BlockHeader.Height)
83 if lastHeight != resp.BlockHeader.Height {
84 lastNonce = ^uint64(0)
86 if doWork(resp.BlockHeader, resp.Seed) {
87 util.ClientCall("/submit-work", &api.SubmitWorkReq{BlockHeader: resp.BlockHeader})
88 getBlockHeaderByHeight(resp.BlockHeader.Height)
91 lastHeight = resp.BlockHeader.Height