"github.com/bytom/protocol/bc/types"
)
+type BlockHeaderJSON struct {
+ Version uint64 `json:"version"` // The version of the block.
+ Height uint64 `json:"height"` // The height of the block.
+ PreviousBlockHash bc.Hash `json:"previous_block_hash"` // The hash of the previous block.
+ 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
+}
+
func (a *API) getWork() Response {
work, err := a.GetWork()
if err != nil {
return NewSuccessResponse(work)
}
+func (a *API) getWorkJSON() Response {
+ work, err := a.GetWorkJSON()
+ if err != nil {
+ return NewErrorResponse(err)
+ }
+ return NewSuccessResponse(work)
+}
+
// SubmitWorkReq used to submitWork req
type SubmitWorkReq struct {
BlockHeader *types.BlockHeader `json:"block_header"`
return NewSuccessResponse(true)
}
+// SubmitWorkJSONReq used to submitWork req
+type SubmitWorkJSONReq struct {
+ BlockHeader *BlockHeaderJSON `json:"block_header"`
+}
+
+func (a *API) submitWorkJSON(ctx context.Context, req *SubmitWorkJSONReq) Response {
+ blockCommitment := types.BlockCommitment{
+ TransactionsMerkleRoot: req.BlockHeader.BlockCommitment.TransactionsMerkleRoot,
+ TransactionStatusHash: req.BlockHeader.BlockCommitment.TransactionStatusHash,
+ }
+
+ bh := &types.BlockHeader{
+ Version: req.BlockHeader.Version,
+ Height: req.BlockHeader.Height,
+ PreviousBlockHash: req.BlockHeader.PreviousBlockHash,
+ Timestamp: req.BlockHeader.Timestamp,
+ Nonce: req.BlockHeader.Nonce,
+ Bits: req.BlockHeader.Bits,
+ BlockCommitment: blockCommitment,
+ }
+
+ if err := a.SubmitWork(bh); err != nil {
+ return NewErrorResponse(err)
+ }
+ return NewSuccessResponse(true)
+}
+
// GetWorkResp is resp struct for API
type GetWorkResp struct {
BlockHeader *types.BlockHeader `json:"block_header"`
}, nil
}
-// SubmitWork submit work
+// GetWorkJSONResp is resp struct for API get-work-json
+type GetWorkJSONResp struct {
+ BlockHeader *BlockHeaderJSON `json:"block_header"`
+ Seed *bc.Hash `json:"seed"`
+}
+
+// GetWorkJSON get work in json
+func (a *API) GetWorkJSON() (*GetWorkJSONResp, error) {
+ bh, err := a.miningPool.GetWork()
+ if err != nil {
+ return nil, err
+ }
+
+ seed, err := a.chain.CalcNextSeed(&bh.PreviousBlockHash)
+ if err != nil {
+ return nil, err
+ }
+
+ blockCommitment := &types.BlockCommitment{
+ TransactionsMerkleRoot: bh.BlockCommitment.TransactionsMerkleRoot,
+ TransactionStatusHash: bh.BlockCommitment.TransactionStatusHash,
+ }
+
+ return &GetWorkJSONResp{
+ BlockHeader: &BlockHeaderJSON{
+ Version: bh.Version,
+ Height: bh.Height,
+ PreviousBlockHash: bh.PreviousBlockHash,
+ Timestamp: bh.Timestamp,
+ Nonce: bh.Nonce,
+ Bits: bh.Bits,
+ BlockCommitment: blockCommitment,
+ },
+ Seed: seed,
+ }, nil
+}
+
+// SubmitWork submit work to the miningpool
func (a *API) SubmitWork(bh *types.BlockHeader) error {
return a.miningPool.SubmitWork(bh)
}