Skip to content

Commit

Permalink
modify difficultyRatio impl to mirror dcrd's
Browse files Browse the repository at this point in the history
  • Loading branch information
itswisdomagain committed May 7, 2021
1 parent b757da4 commit 7e108c1
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions internal/rpc/jsonrpc/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -1561,10 +1561,21 @@ func createVoutList(mtx *wire.MsgTx, chainParams *chaincfg.Params, filterAddrMap
// difficultyRatio returns the proof-of-work difficulty as a multiple of the
// minimum difficulty using the passed bits field from the header of a block.
func difficultyRatio(bits uint32, params *chaincfg.Params) float64 {
// The minimum difficulty is the max possible proof-of-work limit bits
// converted back to a number. Note this is not the same as the proof
// of work limit directly because the block difficulty is encoded in a
// block with the compact form which loses precision.
max := blockchain.CompactToBig(params.PowLimitBits)
target := blockchain.CompactToBig(bits)
ratio, _ := new(big.Rat).SetFrac(max, target).Float64()
return ratio

difficulty := new(big.Rat).SetFrac(max, target)
outString := difficulty.FloatString(8)
diff, err := strconv.ParseFloat(outString, 64)
if err != nil {
log.Errorf("Cannot get difficulty: %v", err)
return 0
}
return diff
}

// syncStatus handles a syncstatus request.
Expand Down

0 comments on commit 7e108c1

Please sign in to comment.