kaspad/server/rpc/handle_get_mining_info.go
stasatdaglabs 40342eb45a [NOD-275] Split rpcserver.go to separate files (#417)
* [NOD-275] Moved getBlockTemplate and related functionality to a separate file.

* [NOD-275] Started moving handlers to separate files.

* [NOD-275] Fixed merge errors.

* [NOD-275] Moved all handlers out of rpcserver.go.

* [NOD-275] Moved non-shared functions out of rpcserver.go.

* [NOD-275] Moved handleGetAllManualNodesInfo to a separate file.

* [NOD-275] Moved handlers out of rpcwebsocket.go to separate files.

* [NOD-275] Fixed import error.

* [NOD-275] Renamed all handler files to include underscores.

* [NOD-275] Moved common rpc helper functions to common.go.
2019-09-22 16:41:37 +03:00

58 lines
1.9 KiB
Go

package rpc
import (
"github.com/daglabs/btcd/btcjson"
"github.com/daglabs/btcd/config"
)
// handleGetMiningInfo implements the getMiningInfo command. We only return the
// fields that are not related to wallet functionality.
func handleGetMiningInfo(s *Server, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
if config.MainConfig().SubnetworkID != nil {
return nil, &btcjson.RPCError{
Code: btcjson.ErrRPCInvalidRequest.Code,
Message: "`getMiningInfo` is not supported on partial nodes.",
}
}
// Create a default getNetworkHashPs command to use defaults and make
// use of the existing getNetworkHashPs handler.
gnhpsCmd := btcjson.NewGetNetworkHashPSCmd(nil, nil)
networkHashesPerSecIface, err := handleGetNetworkHashPS(s, gnhpsCmd,
closeChan)
if err != nil {
return nil, err
}
networkHashesPerSec, ok := networkHashesPerSecIface.(int64)
if !ok {
return nil, &btcjson.RPCError{
Code: btcjson.ErrRPCInternal.Code,
Message: "networkHashesPerSec is not an int64",
}
}
selectedTipHash := s.cfg.DAG.SelectedTipHash()
selectedBlock, err := s.cfg.DAG.BlockByHash(selectedTipHash)
if err != nil {
return nil, &btcjson.RPCError{
Code: btcjson.ErrRPCInternal.Code,
Message: "could not find block for selected tip",
}
}
result := btcjson.GetMiningInfoResult{
Blocks: int64(s.cfg.DAG.BlockCount()),
CurrentBlockSize: uint64(selectedBlock.MsgBlock().SerializeSize()),
CurrentBlockTx: uint64(len(selectedBlock.MsgBlock().Transactions)),
Difficulty: getDifficultyRatio(s.cfg.DAG.CurrentBits(), s.cfg.DAGParams),
Generate: s.cfg.CPUMiner.IsMining(),
GenProcLimit: s.cfg.CPUMiner.NumWorkers(),
HashesPerSec: int64(s.cfg.CPUMiner.HashesPerSecond()),
NetworkHashPS: networkHashesPerSec,
PooledTx: uint64(s.cfg.TxMemPool.Count()),
TestNet: config.MainConfig().TestNet3,
DevNet: config.MainConfig().DevNet,
}
return &result, nil
}