Unmarshal hashes/second as float in GetMiningInfoResult

This commit is contained in:
David Mazary 2020-10-03 21:12:09 -04:00 committed by John C. Vernaleo
parent 6519c04a6f
commit 6adfc07d1e
3 changed files with 48 additions and 6 deletions

View file

@ -673,8 +673,8 @@ type GetMiningInfoResult struct {
Errors string `json:"errors"` Errors string `json:"errors"`
Generate bool `json:"generate"` Generate bool `json:"generate"`
GenProcLimit int32 `json:"genproclimit"` GenProcLimit int32 `json:"genproclimit"`
HashesPerSec int64 `json:"hashespersec"` HashesPerSec float64 `json:"hashespersec"`
NetworkHashPS int64 `json:"networkhashps"` NetworkHashPS float64 `json:"networkhashps"`
PooledTx uint64 `json:"pooledtx"` PooledTx uint64 `json:"pooledtx"`
TestNet bool `json:"testnet"` TestNet bool `json:"testnet"`
} }

View file

@ -9,11 +9,10 @@ import (
"reflect" "reflect"
"testing" "testing"
"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil"
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
"github.com/btcsuite/btcd/btcjson"
) )
// TestChainSvrCustomResults ensures any results that have custom marshalling // 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
}
}
}

View file

@ -2366,8 +2366,8 @@ func handleGetMiningInfo(s *rpcServer, cmd interface{}, closeChan <-chan struct{
Difficulty: getDifficultyRatio(best.Bits, s.cfg.ChainParams), Difficulty: getDifficultyRatio(best.Bits, s.cfg.ChainParams),
Generate: s.cfg.CPUMiner.IsMining(), Generate: s.cfg.CPUMiner.IsMining(),
GenProcLimit: s.cfg.CPUMiner.NumWorkers(), GenProcLimit: s.cfg.CPUMiner.NumWorkers(),
HashesPerSec: int64(s.cfg.CPUMiner.HashesPerSecond()), HashesPerSec: s.cfg.CPUMiner.HashesPerSecond(),
NetworkHashPS: networkHashesPerSec, NetworkHashPS: float64(networkHashesPerSec),
PooledTx: uint64(s.cfg.TxMemPool.Count()), PooledTx: uint64(s.cfg.TxMemPool.Count()),
TestNet: cfg.TestNet3, TestNet: cfg.TestNet3,
} }