-// GetDifficultyResp is resp struct for getDifficulty API
-type GetDifficultyResp struct {
- BlockHash *bc.Hash `json:"hash"`
- BlockHeight uint64 `json:"height"`
- Bits uint64 `json:"bits"`
- Difficulty string `json:"difficulty"`
-}
-
-func (a *API) getDifficulty(ins BlockReq) Response {
- block, err := a.getBlockHelper(ins)
- if err != nil {
- return NewErrorResponse(err)
- }
-
- blockHash := block.Hash()
- resp := &GetDifficultyResp{
- BlockHash: &blockHash,
- BlockHeight: block.Height,
- Bits: block.Bits,
- Difficulty: difficulty.CalcWork(block.Bits).String(),
- }
- return NewSuccessResponse(resp)
-}
-
-// getHashRateResp is resp struct for getHashRate API
-type getHashRateResp struct {
- BlockHash *bc.Hash `json:"hash"`
- BlockHeight uint64 `json:"height"`
- HashRate uint64 `json:"hash_rate"`
-}
-
-func (a *API) getHashRate(ins BlockReq) Response {
- if len(ins.BlockHash) != 32 && len(ins.BlockHash) != 0 {
- err := errors.New("Block hash format error.")
- return NewErrorResponse(err)
- }
- if ins.BlockHeight == 0 {
- ins.BlockHeight = a.chain.BestBlockHeight()
- }
-
- block, err := a.getBlockHelper(ins)
- if err != nil {
- return NewErrorResponse(err)
- }
-
- preBlock, err := a.chain.GetBlockByHash(&block.PreviousBlockHash)
- if err != nil {
- return NewErrorResponse(err)
- }
-
- diffTime := block.Timestamp - preBlock.Timestamp
- if preBlock.Timestamp >= block.Timestamp {
- diffTime = 1
- }
- hashCount := difficulty.CalcWork(block.Bits)
- hashRate := new(big.Int).Div(hashCount, big.NewInt(int64(diffTime)))
-
- blockHash := block.Hash()
- resp := &getHashRateResp{
- BlockHash: &blockHash,
- BlockHeight: block.Height,
- HashRate: hashRate.Uint64(),
- }
- return NewSuccessResponse(resp)
-}
-