OSDN Git Service

:white_check_mark: Check arbitrary len limit (#1283)
[bytom/bytom.git] / api / miner.go
1 package api
2
3 import (
4         "context"
5         "strconv"
6
7         chainjson "github.com/bytom/encoding/json"
8         "github.com/bytom/errors"
9         "github.com/bytom/protocol/bc"
10         "github.com/bytom/protocol/bc/types"
11 )
12
13 // BlockHeaderJSON struct provides support for get work in json format, when it also follows
14 // BlockHeader structure
15 type BlockHeaderJSON struct {
16         Version           uint64                 `json:"version"`             // The version of the block.
17         Height            uint64                 `json:"height"`              // The height of the block.
18         PreviousBlockHash bc.Hash                `json:"previous_block_hash"` // The hash of the previous block.
19         Timestamp         uint64                 `json:"timestamp"`           // The time of the block in seconds.
20         Nonce             uint64                 `json:"nonce"`               // Nonce used to generate the block.
21         Bits              uint64                 `json:"bits"`                // Difficulty target for the block.
22         BlockCommitment   *types.BlockCommitment `json:"block_commitment"`    // Block commitment
23 }
24
25 type CoinbaseArbitrary struct {
26         Arbitrary chainjson.HexBytes `json:"arbitrary"`
27 }
28
29 func (a *API) getCoinbaseArbitrary() Response {
30         arbitrary := a.wallet.AccountMgr.GetCoinbaseArbitrary()
31         resp := &CoinbaseArbitrary{
32                 Arbitrary: arbitrary,
33         }
34         return NewSuccessResponse(resp)
35 }
36
37 // setCoinbaseArbitrary add arbitary data to the reserved coinbase data.
38 // check function createCoinbaseTx in mining/mining.go for detail.
39 // arbitraryLenLimit is 107 and can be calculated by:
40 //      maxHeight := ^uint64(0)
41 //      reserved := append([]byte{0x00}, []byte(strconv.FormatUint(maxHeight, 10))...)
42 //      arbitraryLenLimit := consensus.CoinbaseArbitrarySizeLimit - len(reserved)
43 func (a *API) setCoinbaseArbitrary(ctx context.Context, req CoinbaseArbitrary) Response {
44         arbitraryLenLimit := 107
45         if len(req.Arbitrary) > arbitraryLenLimit {
46                 err := errors.New("Arbitrary exceeds limit: " + strconv.FormatUint(uint64(arbitraryLenLimit), 10))
47                 return NewErrorResponse(err)
48         }
49         a.wallet.AccountMgr.SetCoinbaseArbitrary(req.Arbitrary)
50         return a.getCoinbaseArbitrary()
51 }
52
53 // getWork gets work in compressed protobuf format
54 func (a *API) getWork() Response {
55         work, err := a.GetWork()
56         if err != nil {
57                 return NewErrorResponse(err)
58         }
59         return NewSuccessResponse(work)
60 }
61
62 // getWorkJSON gets work in json format
63 func (a *API) getWorkJSON() Response {
64         work, err := a.GetWorkJSON()
65         if err != nil {
66                 return NewErrorResponse(err)
67         }
68         return NewSuccessResponse(work)
69 }
70
71 // SubmitWorkJSONReq is req struct for submit-work API
72 type SubmitWorkReq struct {
73         BlockHeader *types.BlockHeader `json:"block_header"`
74 }
75
76 // submitWork submits work in compressed protobuf format
77 func (a *API) submitWork(ctx context.Context, req *SubmitWorkReq) Response {
78         if err := a.SubmitWork(req.BlockHeader); err != nil {
79                 return NewErrorResponse(err)
80         }
81         return NewSuccessResponse(true)
82 }
83
84 // SubmitWorkJSONReq is req struct for submit-work-json API
85 type SubmitWorkJSONReq struct {
86         BlockHeader *BlockHeaderJSON `json:"block_header"`
87 }
88
89 // submitWorkJSON submits work in json format
90 func (a *API) submitWorkJSON(ctx context.Context, req *SubmitWorkJSONReq) Response {
91         bh := &types.BlockHeader{
92                 Version:           req.BlockHeader.Version,
93                 Height:            req.BlockHeader.Height,
94                 PreviousBlockHash: req.BlockHeader.PreviousBlockHash,
95                 Timestamp:         req.BlockHeader.Timestamp,
96                 Nonce:             req.BlockHeader.Nonce,
97                 Bits:              req.BlockHeader.Bits,
98                 BlockCommitment:   *req.BlockHeader.BlockCommitment,
99         }
100
101         if err := a.SubmitWork(bh); err != nil {
102                 return NewErrorResponse(err)
103         }
104         return NewSuccessResponse(true)
105 }
106
107 // GetWorkResp is resp struct for get-work API
108 type GetWorkResp struct {
109         BlockHeader *types.BlockHeader `json:"block_header"`
110         Seed        *bc.Hash           `json:"seed"`
111 }
112
113 // GetWork gets work in compressed protobuf format
114 func (a *API) GetWork() (*GetWorkResp, error) {
115         bh, err := a.miningPool.GetWork()
116         if err != nil {
117                 return nil, err
118         }
119
120         seed, err := a.chain.CalcNextSeed(&bh.PreviousBlockHash)
121         if err != nil {
122                 return nil, err
123         }
124
125         return &GetWorkResp{
126                 BlockHeader: bh,
127                 Seed:        seed,
128         }, nil
129 }
130
131 // GetWorkJSONResp is resp struct for get-work-json API
132 type GetWorkJSONResp struct {
133         BlockHeader *BlockHeaderJSON `json:"block_header"`
134         Seed        *bc.Hash         `json:"seed"`
135 }
136
137 // GetWorkJSON gets work in json format
138 func (a *API) GetWorkJSON() (*GetWorkJSONResp, error) {
139         bh, err := a.miningPool.GetWork()
140         if err != nil {
141                 return nil, err
142         }
143
144         seed, err := a.chain.CalcNextSeed(&bh.PreviousBlockHash)
145         if err != nil {
146                 return nil, err
147         }
148
149         return &GetWorkJSONResp{
150                 BlockHeader: &BlockHeaderJSON{
151                         Version:           bh.Version,
152                         Height:            bh.Height,
153                         PreviousBlockHash: bh.PreviousBlockHash,
154                         Timestamp:         bh.Timestamp,
155                         Nonce:             bh.Nonce,
156                         Bits:              bh.Bits,
157                         BlockCommitment:   &bh.BlockCommitment,
158                 },
159                 Seed: seed,
160         }, nil
161 }
162
163 // SubmitWork tries to submit work to the chain
164 func (a *API) SubmitWork(bh *types.BlockHeader) error {
165         return a.miningPool.SubmitWork(bh)
166 }
167
168 func (a *API) setMining(in struct {
169         IsMining bool `json:"is_mining"`
170 }) Response {
171         if in.IsMining {
172                 if _, err := a.wallet.AccountMgr.GetMiningAddress(); err != nil {
173                         return NewErrorResponse(errors.New("Mining address does not exist"))
174                 }
175                 return a.startMining()
176         }
177         return a.stopMining()
178 }
179
180 func (a *API) startMining() Response {
181         a.cpuMiner.Start()
182         if !a.IsMining() {
183                 return NewErrorResponse(errors.New("Failed to start mining"))
184         }
185         return NewSuccessResponse("")
186 }
187
188 func (a *API) stopMining() Response {
189         a.cpuMiner.Stop()
190         if a.IsMining() {
191                 return NewErrorResponse(errors.New("Failed to stop mining"))
192         }
193         return NewSuccessResponse("")
194 }