OSDN Git Service

BlockCommitment for get-work-json
[bytom/bytom.git] / api / miner.go
1 package api
2
3 import (
4         "context"
5
6         "github.com/bytom/errors"
7         "github.com/bytom/protocol/bc"
8         "github.com/bytom/protocol/bc/types"
9 )
10
11 func (a *API) getWork() Response {
12         work, err := a.GetWork()
13         if err != nil {
14                 return NewErrorResponse(err)
15         }
16         return NewSuccessResponse(work)
17 }
18
19 func (a *API) getWorkJSON() Response {
20         work, err := a.GetWorkJSON()
21         if err != nil {
22                 return NewErrorResponse(err)
23         }
24         return NewSuccessResponse(work)
25 }
26
27 // SubmitWorkReq used to submitWork req
28 type SubmitWorkReq struct {
29         BlockHeader *types.BlockHeader `json:"block_header"`
30 }
31
32 func (a *API) submitWork(ctx context.Context, req *SubmitWorkReq) Response {
33         if err := a.SubmitWork(req.BlockHeader); err != nil {
34                 return NewErrorResponse(err)
35         }
36         return NewSuccessResponse(true)
37 }
38
39 func (a *API) submitWorkJSON(ctx context.Context, req *SubmitWorkReq) Response {
40         if err := a.SubmitWork(req.BlockHeader); err != nil {
41                 return NewErrorResponse(err)
42         }
43         return NewSuccessResponse(true)
44 }
45
46 // GetWorkResp is resp struct for API
47 type GetWorkResp struct {
48         BlockHeader *types.BlockHeader `json:"block_header"`
49         Seed        *bc.Hash           `json:"seed"`
50 }
51
52 // GetWork get work
53 func (a *API) GetWork() (*GetWorkResp, error) {
54         bh, err := a.miningPool.GetWork()
55         if err != nil {
56                 return nil, err
57         }
58
59         seed, err := a.chain.CalcNextSeed(&bh.PreviousBlockHash)
60         if err != nil {
61                 return nil, err
62         }
63
64         return &GetWorkResp{
65                 BlockHeader: bh,
66                 Seed:        seed,
67         }, nil
68 }
69
70 type BlockHeaderJSON struct {
71         Version           uint64                                `json:"version"`  // The version of the block.
72         Height            uint64                                `json:"height"`  // The height of the block.
73         PreviousBlockHash bc.Hash                               `json:"previous_block_hash"` // The hash of the previous block.
74         Timestamp         uint64                                `json:"timestamp"` // The time of the block in seconds.
75         Nonce             uint64                                `json:"nonce"` // Nonce used to generate the block.
76         Bits              uint64                                `json:"bits"` // Difficulty target for the block.
77         BlockCommitment   *types.BlockCommitment `json:"block_commitment"` //Block commitment
78 }
79
80 // GetWorkResp is resp struct for API get-work-json
81 type GetWorkJSONResp struct {
82         BlockHeader *BlockHeaderJSON    `json:"block_header"`
83         Seed        *bc.Hash            `json:"seed"`
84 }
85
86 // GetWorkJSON get work in json
87 func (a *API) GetWorkJSON() (*GetWorkJSONResp, error) {
88         bh, err := a.miningPool.GetWork()
89         if err != nil {
90                 return nil, err
91         }
92
93         seed, err := a.chain.CalcNextSeed(&bh.PreviousBlockHash)
94         if err != nil {
95                 return nil, err
96         }
97
98         blockCommitment := &types.BlockCommitment{
99                                                         TransactionsMerkleRoot: bh.BlockCommitment.TransactionsMerkleRoot,
100                                                         TransactionStatusHash:  bh.BlockCommitment.TransactionStatusHash,
101                                                 }
102
103         return &GetWorkJSONResp{
104                 BlockHeader: &BlockHeaderJSON{
105                         Version:                        bh.Version,
106                         Height:                         bh.Height,
107                         PreviousBlockHash:      bh.PreviousBlockHash,
108                         Timestamp:              bh.Timestamp,
109                         Nonce:                  bh.Nonce,
110                         Bits:                   bh.Bits,
111                         BlockCommitment:        blockCommitment,
112                 },
113                 Seed:        seed,
114         }, nil
115 }
116
117 // SubmitWork submit work
118 func (a *API) SubmitWork(bh *types.BlockHeader) error {
119         return a.miningPool.SubmitWork(bh)
120 }
121
122 func (a *API) setMining(in struct {
123         IsMining bool `json:"is_mining"`
124 }) Response {
125         if in.IsMining {
126                 return a.startMining()
127         }
128         return a.stopMining()
129 }
130
131 func (a *API) startMining() Response {
132         a.cpuMiner.Start()
133         if !a.IsMining() {
134                 return NewErrorResponse(errors.New("Failed to start mining"))
135         }
136         return NewSuccessResponse("")
137 }
138
139 func (a *API) stopMining() Response {
140         a.cpuMiner.Stop()
141         if a.IsMining() {
142                 return NewErrorResponse(errors.New("Failed to stop mining"))
143         }
144         return NewSuccessResponse("")
145 }