import (
"context"
+ "strconv"
- "github.com/bytom/errors"
- "github.com/bytom/protocol/bc"
- "github.com/bytom/protocol/bc/types"
+ chainjson "github.com/bytom/bytom/encoding/json"
+ "github.com/bytom/bytom/errors"
+ "github.com/bytom/bytom/event"
+ "github.com/bytom/bytom/protocol/bc"
+ "github.com/bytom/bytom/protocol/bc/types"
)
// BlockHeaderJSON struct provides support for get work in json format, when it also follows
Timestamp uint64 `json:"timestamp"` // The time of the block in seconds.
Nonce uint64 `json:"nonce"` // Nonce used to generate the block.
Bits uint64 `json:"bits"` // Difficulty target for the block.
- BlockCommitment *types.BlockCommitment `json:"block_commitment"` //Block commitment
+ BlockCommitment *types.BlockCommitment `json:"block_commitment"` // Block commitment
+}
+
+type CoinbaseArbitrary struct {
+ Arbitrary chainjson.HexBytes `json:"arbitrary"`
+}
+
+func (a *API) getCoinbaseArbitrary() Response {
+ arbitrary := a.wallet.AccountMgr.GetCoinbaseArbitrary()
+ resp := &CoinbaseArbitrary{
+ Arbitrary: arbitrary,
+ }
+ return NewSuccessResponse(resp)
+}
+
+// setCoinbaseArbitrary add arbitary data to the reserved coinbase data.
+// check function createCoinbaseTx in mining/mining.go for detail.
+// arbitraryLenLimit is 107 and can be calculated by:
+// maxHeight := ^uint64(0)
+// reserved := append([]byte{0x00}, []byte(strconv.FormatUint(maxHeight, 10))...)
+// arbitraryLenLimit := consensus.CoinbaseArbitrarySizeLimit - len(reserved)
+func (a *API) setCoinbaseArbitrary(ctx context.Context, req CoinbaseArbitrary) Response {
+ arbitraryLenLimit := 107
+ if len(req.Arbitrary) > arbitraryLenLimit {
+ err := errors.New("Arbitrary exceeds limit: " + strconv.FormatUint(uint64(arbitraryLenLimit), 10))
+ return NewErrorResponse(err)
+ }
+ a.wallet.AccountMgr.SetCoinbaseArbitrary(req.Arbitrary)
+ return a.getCoinbaseArbitrary()
}
// getWork gets work in compressed protobuf format
return NewSuccessResponse(work)
}
-// SubmitWorkJSONReq is req struct for submit-work API
+// SubmitBlockReq is req struct for submit-block API
+type SubmitBlockReq struct {
+ Block *types.Block `json:"raw_block"`
+}
+
+// submitBlock trys to submit a raw block to the chain
+func (a *API) submitBlock(ctx context.Context, req *SubmitBlockReq) Response {
+ isOrphan, err := a.chain.ProcessBlock(req.Block)
+ if err != nil {
+ return NewErrorResponse(err)
+ }
+
+ if isOrphan {
+ return NewErrorResponse(errors.New("block submitted is orphan"))
+ }
+
+ if err = a.eventDispatcher.Post(event.NewMinedBlockEvent{Block: *req.Block}); err != nil {
+ return NewErrorResponse(err)
+ }
+
+ return NewSuccessResponse(true)
+}
+
+// SubmitWorkReq is req struct for submit-work API
type SubmitWorkReq struct {
BlockHeader *types.BlockHeader `json:"block_header"`
}
IsMining bool `json:"is_mining"`
}) Response {
if in.IsMining {
+ if _, err := a.wallet.AccountMgr.GetMiningAddress(); err != nil {
+ return NewErrorResponse(errors.New("Mining address does not exist"))
+ }
return a.startMining()
}
return a.stopMining()