From 6adfc07d1ea97599c7e06cf6ea84daa0c0ef57e6 Mon Sep 17 00:00:00 2001 From: David Mazary Date: Sat, 3 Oct 2020 21:12:09 -0400 Subject: [PATCH] Unmarshal hashes/second as float in GetMiningInfoResult --- btcjson/chainsvrresults.go | 4 +-- btcjson/chainsvrresults_test.go | 46 +++++++++++++++++++++++++++++++-- rpcserver.go | 4 +-- 3 files changed, 48 insertions(+), 6 deletions(-) diff --git a/btcjson/chainsvrresults.go b/btcjson/chainsvrresults.go index 689fbe67..00f8cd61 100644 --- a/btcjson/chainsvrresults.go +++ b/btcjson/chainsvrresults.go @@ -673,8 +673,8 @@ type GetMiningInfoResult struct { Errors string `json:"errors"` Generate bool `json:"generate"` GenProcLimit int32 `json:"genproclimit"` - HashesPerSec int64 `json:"hashespersec"` - NetworkHashPS int64 `json:"networkhashps"` + HashesPerSec float64 `json:"hashespersec"` + NetworkHashPS float64 `json:"networkhashps"` PooledTx uint64 `json:"pooledtx"` TestNet bool `json:"testnet"` } diff --git a/btcjson/chainsvrresults_test.go b/btcjson/chainsvrresults_test.go index 0bab922b..72dcd8d7 100644 --- a/btcjson/chainsvrresults_test.go +++ b/btcjson/chainsvrresults_test.go @@ -9,11 +9,10 @@ import ( "reflect" "testing" + "github.com/btcsuite/btcd/btcjson" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcutil" "github.com/davecgh/go-spew/spew" - - "github.com/btcsuite/btcd/btcjson" ) // TestChainSvrCustomResults ensures any results that have custom marshalling @@ -157,3 +156,46 @@ func TestGetTxOutSetInfoResult(t *testing.T) { } } } + +// TestChainSvrMiningInfoResults ensures GetMiningInfoResults are unmarshalled correctly +func TestChainSvrMiningInfoResults(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + result string + expected btcjson.GetMiningInfoResult + }{ + { + name: "mining info with integer networkhashps", + result: `{"networkhashps": 89790618491361}`, + expected: btcjson.GetMiningInfoResult{ + NetworkHashPS: 89790618491361, + }, + }, + { + name: "mining info with scientific notation networkhashps", + result: `{"networkhashps": 8.9790618491361e+13}`, + expected: btcjson.GetMiningInfoResult{ + NetworkHashPS: 89790618491361, + }, + }, + } + + t.Logf("Running %d tests", len(tests)) + for i, test := range tests { + var miningInfoResult btcjson.GetMiningInfoResult + err := json.Unmarshal([]byte(test.result), &miningInfoResult) + if err != nil { + t.Errorf("Test #%d (%s) unexpected error: %v", i, + test.name, err) + continue + } + if miningInfoResult != test.expected { + t.Errorf("Test #%d (%s) unexpected marhsalled data - "+ + "got %+v, want %+v", i, test.name, miningInfoResult, + test.expected) + continue + } + } +} diff --git a/rpcserver.go b/rpcserver.go index 9a52301e..f159f239 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -2366,8 +2366,8 @@ func handleGetMiningInfo(s *rpcServer, cmd interface{}, closeChan <-chan struct{ Difficulty: getDifficultyRatio(best.Bits, s.cfg.ChainParams), Generate: s.cfg.CPUMiner.IsMining(), GenProcLimit: s.cfg.CPUMiner.NumWorkers(), - HashesPerSec: int64(s.cfg.CPUMiner.HashesPerSecond()), - NetworkHashPS: networkHashesPerSec, + HashesPerSec: s.cfg.CPUMiner.HashesPerSecond(), + NetworkHashPS: float64(networkHashesPerSec), PooledTx: uint64(s.cfg.TxMemPool.Count()), TestNet: cfg.TestNet3, }