From a755305a2e130163839fe8e7a4df440f877661ee Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Fri, 28 Oct 2016 02:38:57 -0500 Subject: [PATCH] rpcserver: Move error check for generate RPC. This moves the error check for an attempt to call the generate RPC when on a network that there is virtually no chance of mining a block with the CPU into the RPC server where it more naturally belongs. The caller of the CPU should be the one to determine if it wants to allow mining or not. While here, use a more accurate RPC error code of ErrDifficulty instead of ErrInternal. This change is a step towards being able to separate the CPU mining code into its own subpackage. --- cpuminer.go | 8 -------- rpcserver.go | 13 +++++++++++++ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/cpuminer.go b/cpuminer.go index a45e70ee..1e473349 100644 --- a/cpuminer.go +++ b/cpuminer.go @@ -548,14 +548,6 @@ func (m *CPUMiner) NumWorkers() int32 { func (m *CPUMiner) GenerateNBlocks(n uint32) ([]*chainhash.Hash, error) { m.Lock() - // Respond with an error if there's virtually 0 chance of CPU-mining a block. - if !m.cfg.ChainParams.GenerateSupported { - m.Unlock() - return nil, errors.New("No support for `generate` on the current " + - "network, " + m.cfg.ChainParams.Net.String() + - ", as it's unlikely to be possible to CPU-mine a block.") - } - // Respond with an error if server is already mining. if m.started || m.discreteMining { m.Unlock() diff --git a/rpcserver.go b/rpcserver.go index 74f8b8e4..5c5547aa 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -856,6 +856,19 @@ func handleGenerate(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (i } } + // Respond with an error if there's virtually 0 chance of mining a block + // with the CPU. + params := s.server.chainParams + if !s.server.chainParams.GenerateSupported { + return nil, &btcjson.RPCError{ + Code: btcjson.ErrRPCDifficulty, + Message: fmt.Sprintf("No support for `generate` on "+ + "the current network, %s, as it's unlikely to "+ + "be possible to main a block with the CPU.", + params.Net), + } + } + c := cmd.(*btcjson.GenerateCmd) // Respond with an error if the client is requesting 0 blocks to be generated.