9 "github.com/bytom/consensus/difficulty"
10 "github.com/bytom/protocol/bc"
11 "github.com/bytom/protocol/bc/types"
12 "github.com/bytom/util"
16 maxNonce = ^uint64(0) // 2^32 - 1
20 func doWork(bh *types.BlockHeader, seed *bc.Hash) bool {
21 for i := uint64(0); i <= maxNonce; i++ {
23 headerHash := bh.Hash()
24 if difficulty.CheckProofOfWork(&headerHash, seed, bh.Bits) {
25 fmt.Printf("Mining: successful-----proof hash:%v\n", headerHash.String())
32 func getBlockHeaderByHeight(height uint64) {
34 BlockHeight uint64 `json:"block_height"`
38 BlockHeader *types.BlockHeader `json:"block_header"`
39 Reward uint64 `json:"reward"`
42 data, _ := util.ClientCall("/get-block-header-by-height", Req{BlockHeight: height})
43 rawData, err := json.Marshal(data)
50 if err = json.Unmarshal(rawData, resp); err != nil {
54 fmt.Println(resp.Reward)
58 data, _ := util.ClientCall("/getwork", nil)
59 rawData, err := json.Marshal(data)
64 resp := &api.GetWorkResp{}
65 if err = json.Unmarshal(rawData, resp); err != nil {
70 if doWork(resp.BlockHeader, resp.Seed) {
71 util.ClientCall("/submitwork", resp.BlockHeader)
74 getBlockHeaderByHeight(resp.BlockHeader.Height)