Skip to content

Commit ec9ca7d

Browse files
mattbajorekjcvernaleo
authored andcommitted
rpcclient+rpcserver: Change getnetworkhashps return type to be a float to be in line with bitcoin core
1 parent 780cc08 commit ec9ca7d

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

rpcclient/mining.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -255,14 +255,14 @@ type FutureGetNetworkHashPS chan *Response
255255
// Receive waits for the Response promised by the future and returns the
256256
// estimated network hashes per second for the block heights provided by the
257257
// parameters.
258-
func (r FutureGetNetworkHashPS) Receive() (int64, error) {
258+
func (r FutureGetNetworkHashPS) Receive() (float64, error) {
259259
res, err := ReceiveFuture(r)
260260
if err != nil {
261261
return -1, err
262262
}
263263

264-
// Unmarshal result as an int64.
265-
var result int64
264+
// Unmarshal result as an float64.
265+
var result float64
266266
err = json.Unmarshal(res, &result)
267267
if err != nil {
268268
return 0, err
@@ -286,7 +286,7 @@ func (c *Client) GetNetworkHashPSAsync() FutureGetNetworkHashPS {
286286
//
287287
// See GetNetworkHashPS2 to override the number of blocks to use and
288288
// GetNetworkHashPS3 to override the height at which to calculate the estimate.
289-
func (c *Client) GetNetworkHashPS() (int64, error) {
289+
func (c *Client) GetNetworkHashPS() (float64, error) {
290290
return c.GetNetworkHashPSAsync().Receive()
291291
}
292292

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

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

rpcserver.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -2355,11 +2355,11 @@ func handleGetMiningInfo(s *rpcServer, cmd interface{}, closeChan <-chan struct{
23552355
if err != nil {
23562356
return nil, err
23572357
}
2358-
networkHashesPerSec, ok := networkHashesPerSecIface.(int64)
2358+
networkHashesPerSec, ok := networkHashesPerSecIface.(float64)
23592359
if !ok {
23602360
return nil, &btcjson.RPCError{
23612361
Code: btcjson.ErrRPCInternal.Code,
2362-
Message: "networkHashesPerSec is not an int64",
2362+
Message: "networkHashesPerSec is not a float64",
23632363
}
23642364
}
23652365

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

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

24002400
c := cmd.(*btcjson.GetNetworkHashPSCmd)
@@ -2409,7 +2409,7 @@ func handleGetNetworkHashPS(s *rpcServer, cmd interface{}, closeChan <-chan stru
24092409
endHeight = int32(*c.Height)
24102410
}
24112411
if endHeight > best.Height || endHeight == 0 {
2412-
return int64(0), nil
2412+
return float64(0), nil
24132413
}
24142414
if endHeight < 0 {
24152415
endHeight = best.Height
@@ -2476,13 +2476,13 @@ func handleGetNetworkHashPS(s *rpcServer, cmd interface{}, closeChan <-chan stru
24762476
// Calculate the difference in seconds between the min and max block
24772477
// timestamps and avoid division by zero in the case where there is no
24782478
// time difference.
2479-
timeDiff := int64(maxTimestamp.Sub(minTimestamp) / time.Second)
2479+
timeDiff := maxTimestamp.Sub(minTimestamp).Seconds()
24802480
if timeDiff == 0 {
2481-
return int64(0), nil
2481+
return timeDiff, nil
24822482
}
24832483

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

24882488
// handleGetNodeAddresses implements the getnodeaddresses command.

rpcserverhelp.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ var rpcResultTypes = map[string][]interface{}{
740740
"getmempoolinfo": {(*btcjson.GetMempoolInfoResult)(nil)},
741741
"getmininginfo": {(*btcjson.GetMiningInfoResult)(nil)},
742742
"getnettotals": {(*btcjson.GetNetTotalsResult)(nil)},
743-
"getnetworkhashps": {(*int64)(nil)},
743+
"getnetworkhashps": {(*float64)(nil)},
744744
"getnodeaddresses": {(*[]btcjson.GetNodeAddressesResult)(nil)},
745745
"getpeerinfo": {(*[]btcjson.GetPeerInfoResult)(nil)},
746746
"getrawmempool": {(*[]string)(nil), (*btcjson.GetRawMempoolVerboseResult)(nil)},

0 commit comments

Comments
 (0)