mirror of
https://github.com/btcsuite/btcd.git
synced 2024-11-19 09:50:08 +01:00
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:
parent
05126f6034
commit
a755305a2e
@ -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()
|
||||
|
13
rpcserver.go
13
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.
|
||||
|
Loading…
Reference in New Issue
Block a user