diff --git a/rpcclient/mining.go b/rpcclient/mining.go index 15473e24be..717825eeaa 100644 --- a/rpcclient/mining.go +++ b/rpcclient/mining.go @@ -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 @@ -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() } @@ -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() } @@ -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() } diff --git a/rpcserver.go b/rpcserver.go index d184072942..6f60261a9c 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -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", } } @@ -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, } @@ -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) @@ -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 @@ -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. diff --git a/rpcserverhelp.go b/rpcserverhelp.go index 570b5b0500..16bbb62a2b 100644 --- a/rpcserverhelp.go +++ b/rpcserverhelp.go @@ -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)},