Skip to content

Commit

Permalink
rpcclient+rpcserver: Change getnetworkhashps return type to be a floa…
Browse files Browse the repository at this point in the history
…t to be in line with bitcoin core
  • Loading branch information
mattbajorek committed Dec 6, 2021
1 parent 780cc08 commit 456fdc7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
12 changes: 6 additions & 6 deletions rpcclient/mining.go
Expand Up @@ -255,14 +255,14 @@ type FutureGetNetworkHashPS chan *Response
// Receive waits for the Response promised by the future and returns the
// estimated network hashes per second for the block heights provided by the
// parameters.
func (r FutureGetNetworkHashPS) Receive() (int64, error) {
func (r FutureGetNetworkHashPS) Receive() (float64, error) {
res, err := ReceiveFuture(r)
if err != nil {
return -1, err
}

// Unmarshal result as an int64.
var result int64
// Unmarshal result as an float64.
var result float64
err = json.Unmarshal(res, &result)
if err != nil {
return 0, err
Expand All @@ -286,7 +286,7 @@ func (c *Client) GetNetworkHashPSAsync() FutureGetNetworkHashPS {
//
// See GetNetworkHashPS2 to override the number of blocks to use and
// GetNetworkHashPS3 to override the height at which to calculate the estimate.
func (c *Client) GetNetworkHashPS() (int64, error) {
func (c *Client) GetNetworkHashPS() (float64, error) {
return c.GetNetworkHashPSAsync().Receive()
}

Expand All @@ -307,7 +307,7 @@ func (c *Client) GetNetworkHashPS2Async(blocks int) FutureGetNetworkHashPS {
//
// See GetNetworkHashPS to use defaults and GetNetworkHashPS3 to override the
// height at which to calculate the estimate.
func (c *Client) GetNetworkHashPS2(blocks int) (int64, error) {
func (c *Client) GetNetworkHashPS2(blocks int) (float64, error) {
return c.GetNetworkHashPS2Async(blocks).Receive()
}

Expand All @@ -327,7 +327,7 @@ func (c *Client) GetNetworkHashPS3Async(blocks, height int) FutureGetNetworkHash
// of blocks since the last difficulty change will be used.
//
// See GetNetworkHashPS and GetNetworkHashPS2 to use defaults.
func (c *Client) GetNetworkHashPS3(blocks, height int) (int64, error) {
func (c *Client) GetNetworkHashPS3(blocks, height int) (float64, error) {
return c.GetNetworkHashPS3Async(blocks, height).Receive()
}

Expand Down
20 changes: 10 additions & 10 deletions rpcserver.go
Expand Up @@ -2355,11 +2355,11 @@ func handleGetMiningInfo(s *rpcServer, cmd interface{}, closeChan <-chan struct{
if err != nil {
return nil, err
}
networkHashesPerSec, ok := networkHashesPerSecIface.(int64)
networkHashesPerSec, ok := networkHashesPerSecIface.(float64)
if !ok {
return nil, &btcjson.RPCError{
Code: btcjson.ErrRPCInternal.Code,
Message: "networkHashesPerSec is not an int64",
Message: "networkHashesPerSec is not a float64",
}
}

Expand All @@ -2373,7 +2373,7 @@ func handleGetMiningInfo(s *rpcServer, cmd interface{}, closeChan <-chan struct{
Generate: s.cfg.CPUMiner.IsMining(),
GenProcLimit: s.cfg.CPUMiner.NumWorkers(),
HashesPerSec: s.cfg.CPUMiner.HashesPerSecond(),
NetworkHashPS: float64(networkHashesPerSec),
NetworkHashPS: networkHashesPerSec,
PooledTx: uint64(s.cfg.TxMemPool.Count()),
TestNet: cfg.TestNet3,
}
Expand All @@ -2393,8 +2393,8 @@ func handleGetNetTotals(s *rpcServer, cmd interface{}, closeChan <-chan struct{}

// handleGetNetworkHashPS implements the getnetworkhashps command.
func handleGetNetworkHashPS(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
// Note: All valid error return paths should return an int64.
// Literal zeros are inferred as int, and won't coerce to int64
// Note: All valid error return paths should return a float64.
// Literal zeros are inferred as int, and won't coerce to float64
// because the return value is an interface{}.

c := cmd.(*btcjson.GetNetworkHashPSCmd)
Expand All @@ -2409,7 +2409,7 @@ func handleGetNetworkHashPS(s *rpcServer, cmd interface{}, closeChan <-chan stru
endHeight = int32(*c.Height)
}
if endHeight > best.Height || endHeight == 0 {
return int64(0), nil
return float64(0), nil
}
if endHeight < 0 {
endHeight = best.Height
Expand Down Expand Up @@ -2476,13 +2476,13 @@ func handleGetNetworkHashPS(s *rpcServer, cmd interface{}, closeChan <-chan stru
// Calculate the difference in seconds between the min and max block
// timestamps and avoid division by zero in the case where there is no
// time difference.
timeDiff := int64(maxTimestamp.Sub(minTimestamp) / time.Second)
timeDiff := maxTimestamp.Sub(minTimestamp).Seconds()
if timeDiff == 0 {
return int64(0), nil
return timeDiff, nil
}

hashesPerSec := new(big.Int).Div(totalWork, big.NewInt(timeDiff))
return hashesPerSec.Int64(), nil
hashesPerSec, _ := new(big.Float).Quo(new(big.Float).SetInt(totalWork), new(big.Float).SetFloat64(timeDiff)).Float64()
return hashesPerSec, nil
}

// handleGetNodeAddresses implements the getnodeaddresses command.
Expand Down
2 changes: 1 addition & 1 deletion rpcserverhelp.go
Expand Up @@ -740,7 +740,7 @@ var rpcResultTypes = map[string][]interface{}{
"getmempoolinfo": {(*btcjson.GetMempoolInfoResult)(nil)},
"getmininginfo": {(*btcjson.GetMiningInfoResult)(nil)},
"getnettotals": {(*btcjson.GetNetTotalsResult)(nil)},
"getnetworkhashps": {(*int64)(nil)},
"getnetworkhashps": {(*float64)(nil)},
"getnodeaddresses": {(*[]btcjson.GetNodeAddressesResult)(nil)},
"getpeerinfo": {(*[]btcjson.GetPeerInfoResult)(nil)},
"getrawmempool": {(*[]string)(nil), (*btcjson.GetRawMempoolVerboseResult)(nil)},
Expand Down

0 comments on commit 456fdc7

Please sign in to comment.