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.
This commit is contained in:
Dave Collins 2016-10-28 02:38:57 -05:00
parent 05126f6034
commit a755305a2e
No known key found for this signature in database
GPG key ID: B8904D9D9C93D1F2
2 changed files with 13 additions and 8 deletions

View file

@ -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()

View file

@ -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.