6 "github.com/bytom/errors"
7 "github.com/bytom/protocol/bc"
8 "github.com/bytom/protocol/bc/types"
11 type BlockHeaderJSON struct {
12 Version uint64 `json:"version"` // The version of the block.
13 Height uint64 `json:"height"` // The height of the block.
14 PreviousBlockHash bc.Hash `json:"previous_block_hash"` // The hash of the previous block.
15 Timestamp uint64 `json:"timestamp"` // The time of the block in seconds.
16 Nonce uint64 `json:"nonce"` // Nonce used to generate the block.
17 Bits uint64 `json:"bits"` // Difficulty target for the block.
18 BlockCommitment *types.BlockCommitment `json:"block_commitment"` //Block commitment
21 func (a *API) getWork() Response {
22 work, err := a.GetWork()
24 return NewErrorResponse(err)
26 return NewSuccessResponse(work)
29 func (a *API) getWorkJSON() Response {
30 work, err := a.GetWorkJSON()
32 return NewErrorResponse(err)
34 return NewSuccessResponse(work)
37 // SubmitWorkReq used to submitWork req
38 type SubmitWorkReq struct {
39 BlockHeader *types.BlockHeader `json:"block_header"`
42 func (a *API) submitWork(ctx context.Context, req *SubmitWorkReq) Response {
43 if err := a.SubmitWork(req.BlockHeader); err != nil {
44 return NewErrorResponse(err)
46 return NewSuccessResponse(true)
49 // SubmitWorkJSONReq used to submitWork req
50 type SubmitWorkJSONReq struct {
51 BlockHeader *BlockHeaderJSON `json:"block_header"`
54 func (a *API) submitWorkJSON(ctx context.Context, req *SubmitWorkJSONReq) Response {
55 blockCommitment := types.BlockCommitment{
56 TransactionsMerkleRoot: req.BlockHeader.BlockCommitment.TransactionsMerkleRoot,
57 TransactionStatusHash: req.BlockHeader.BlockCommitment.TransactionStatusHash,
60 bh := &types.BlockHeader{
61 Version: req.BlockHeader.Version,
62 Height: req.BlockHeader.Height,
63 PreviousBlockHash: req.BlockHeader.PreviousBlockHash,
64 Timestamp: req.BlockHeader.Timestamp,
65 Nonce: req.BlockHeader.Nonce,
66 Bits: req.BlockHeader.Bits,
67 BlockCommitment: blockCommitment,
70 if err := a.SubmitWork(bh); err != nil {
71 return NewErrorResponse(err)
73 return NewSuccessResponse(true)
76 // GetWorkResp is resp struct for API
77 type GetWorkResp struct {
78 BlockHeader *types.BlockHeader `json:"block_header"`
79 Seed *bc.Hash `json:"seed"`
83 func (a *API) GetWork() (*GetWorkResp, error) {
84 bh, err := a.miningPool.GetWork()
89 seed, err := a.chain.CalcNextSeed(&bh.PreviousBlockHash)
100 // GetWorkJSONResp is resp struct for API get-work-json
101 type GetWorkJSONResp struct {
102 BlockHeader *BlockHeaderJSON `json:"block_header"`
103 Seed *bc.Hash `json:"seed"`
106 // GetWorkJSON get work in json
107 func (a *API) GetWorkJSON() (*GetWorkJSONResp, error) {
108 bh, err := a.miningPool.GetWork()
113 seed, err := a.chain.CalcNextSeed(&bh.PreviousBlockHash)
118 blockCommitment := &types.BlockCommitment{
119 TransactionsMerkleRoot: bh.BlockCommitment.TransactionsMerkleRoot,
120 TransactionStatusHash: bh.BlockCommitment.TransactionStatusHash,
123 return &GetWorkJSONResp{
124 BlockHeader: &BlockHeaderJSON{
127 PreviousBlockHash: bh.PreviousBlockHash,
128 Timestamp: bh.Timestamp,
131 BlockCommitment: blockCommitment,
137 // SubmitWork submit work to the miningpool
138 func (a *API) SubmitWork(bh *types.BlockHeader) error {
139 return a.miningPool.SubmitWork(bh)
142 func (a *API) setMining(in struct {
143 IsMining bool `json:"is_mining"`
146 return a.startMining()
148 return a.stopMining()
151 func (a *API) startMining() Response {
154 return NewErrorResponse(errors.New("Failed to start mining"))
156 return NewSuccessResponse("")
159 func (a *API) stopMining() Response {
162 return NewErrorResponse(errors.New("Failed to stop mining"))
164 return NewSuccessResponse("")