From e0c0ec53ffbf2c310745ff4fd83e421573b87471 Mon Sep 17 00:00:00 2001 From: HAOYUatHZ Date: Tue, 5 Jun 2018 11:05:28 +0800 Subject: [PATCH] submit-work-json works --- api/miner.go | 48 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/api/miner.go b/api/miner.go index b9891615..fc7fce51 100644 --- a/api/miner.go +++ b/api/miner.go @@ -8,6 +8,16 @@ import ( "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 { @@ -36,8 +46,28 @@ func (a *API) submitWork(ctx context.Context, req *SubmitWorkReq) Response { return NewSuccessResponse(true) } -func (a *API) submitWorkJSON(ctx context.Context, req *SubmitWorkReq) Response { - if err := a.SubmitWork(req.BlockHeader); err != nil { +// 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) @@ -67,17 +97,7 @@ func (a *API) GetWork() (*GetWorkResp, error) { }, nil } -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 -} - -// GetWorkResp is resp struct for API get-work-json +// GetWorkJSONResp is resp struct for API get-work-json type GetWorkJSONResp struct { BlockHeader *BlockHeaderJSON `json:"block_header"` Seed *bc.Hash `json:"seed"` @@ -114,7 +134,7 @@ func (a *API) GetWorkJSON() (*GetWorkJSONResp, error) { }, nil } -// SubmitWork submit work +// SubmitWork submit work to the miningpool func (a *API) SubmitWork(bh *types.BlockHeader) error { return a.miningPool.SubmitWork(bh) } -- 2.11.0