From 9725f3172038788ae686bfa2f3fbb066aed0d96d Mon Sep 17 00:00:00 2001 From: HAOYUatHZ Date: Mon, 4 Jun 2018 13:10:01 +0800 Subject: [PATCH] BlockCommitment for get-work-json --- api/miner.go | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/api/miner.go b/api/miner.go index d183459c..b9891615 100644 --- a/api/miner.go +++ b/api/miner.go @@ -17,7 +17,7 @@ func (a *API) getWork() Response { } func (a *API) getWorkJSON() Response { - work, err := a.GetWork() + work, err := a.GetWorkJSON() if err != nil { return NewErrorResponse(err) } @@ -67,6 +67,53 @@ 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 +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 func (a *API) SubmitWork(bh *types.BlockHeader) error { return a.miningPool.SubmitWork(bh) -- 2.11.0