7 chainjson "github.com/vapor/encoding/json"
8 "github.com/vapor/errors"
9 "github.com/vapor/event"
10 "github.com/vapor/protocol/bc/types"
13 type CoinbaseArbitrary struct {
14 Arbitrary chainjson.HexBytes `json:"arbitrary"`
17 func (a *API) getCoinbaseArbitrary() Response {
18 arbitrary := a.wallet.AccountMgr.GetCoinbaseArbitrary()
19 resp := &CoinbaseArbitrary{
22 return NewSuccessResponse(resp)
25 // setCoinbaseArbitrary add arbitary data to the reserved coinbase data.
26 // check function createCoinbaseTx in mining/mining.go for detail.
27 // arbitraryLenLimit is 107 and can be calculated by:
28 // maxHeight := ^uint64(0)
29 // reserved := append([]byte{0x00}, []byte(strconv.FormatUint(maxHeight, 10))...)
30 // arbitraryLenLimit := consensus.CoinbaseArbitrarySizeLimit - len(reserved)
31 func (a *API) setCoinbaseArbitrary(ctx context.Context, req CoinbaseArbitrary) Response {
32 arbitraryLenLimit := 107
33 if len(req.Arbitrary) > arbitraryLenLimit {
34 err := errors.New("Arbitrary exceeds limit: " + strconv.FormatUint(uint64(arbitraryLenLimit), 10))
35 return NewErrorResponse(err)
37 a.wallet.AccountMgr.SetCoinbaseArbitrary(req.Arbitrary)
38 return a.getCoinbaseArbitrary()
41 // SubmitBlockReq is req struct for submit-block API
42 type SubmitBlockReq struct {
43 Block *types.Block `json:"raw_block"`
46 // submitBlock trys to submit a raw block to the chain
47 func (a *API) submitBlock(ctx context.Context, req *SubmitBlockReq) Response {
48 isOrphan, err := a.chain.ProcessBlock(req.Block)
50 return NewErrorResponse(err)
54 return NewErrorResponse(errors.New("block submitted is orphan"))
57 if err = a.eventDispatcher.Post(event.NewProposedBlockEvent{Block: *req.Block}); err != nil {
58 return NewErrorResponse(err)
61 return NewSuccessResponse(true)
64 func (a *API) setMining(in struct {
65 IsMining bool `json:"is_mining"`
68 if _, err := a.wallet.AccountMgr.GetMiningAddress(); err != nil {
69 return NewErrorResponse(errors.New("Mining address does not exist"))
71 return a.startMining()
76 func (a *API) startMining() Response {
77 a.blockProposer.Start()
79 return NewErrorResponse(errors.New("Failed to start mining"))
81 return NewSuccessResponse("")
84 func (a *API) stopMining() Response {
85 a.blockProposer.Stop()
87 return NewErrorResponse(errors.New("Failed to stop mining"))
89 return NewSuccessResponse("")