cmd, eth: clean up miner startup API, drop noop config field

pull/17492/head
Péter Szilágyi 6 years ago
parent f34f361ca6
commit 92381ee009
No known key found for this signature in database
GPG Key ID: E9AE538CEDF8293D
  1. 20
      cmd/geth/main.go
  2. 6
      cmd/utils/flags.go
  3. 47
      eth/api.go
  4. 76
      eth/backend.go
  5. 1
      eth/config.go
  6. 5
      eth/gen_config.go

@ -336,26 +336,18 @@ func startNode(ctx *cli.Context, stack *node.Node) {
if err := stack.Service(&ethereum); err != nil { if err := stack.Service(&ethereum); err != nil {
utils.Fatalf("Ethereum service not running: %v", err) utils.Fatalf("Ethereum service not running: %v", err)
} }
// Use a reduced number of threads if requested
threads := ctx.GlobalInt(utils.MinerLegacyThreadsFlag.Name)
if ctx.GlobalIsSet(utils.MinerThreadsFlag.Name) {
threads = ctx.GlobalInt(utils.MinerThreadsFlag.Name)
}
if threads > 0 {
type threaded interface {
SetThreads(threads int)
}
if th, ok := ethereum.Engine().(threaded); ok {
th.SetThreads(threads)
}
}
// Set the gas price to the limits from the CLI and start mining // Set the gas price to the limits from the CLI and start mining
gasprice := utils.GlobalBig(ctx, utils.MinerLegacyGasPriceFlag.Name) gasprice := utils.GlobalBig(ctx, utils.MinerLegacyGasPriceFlag.Name)
if ctx.IsSet(utils.MinerGasPriceFlag.Name) { if ctx.IsSet(utils.MinerGasPriceFlag.Name) {
gasprice = utils.GlobalBig(ctx, utils.MinerGasPriceFlag.Name) gasprice = utils.GlobalBig(ctx, utils.MinerGasPriceFlag.Name)
} }
ethereum.TxPool().SetGasPrice(gasprice) ethereum.TxPool().SetGasPrice(gasprice)
if err := ethereum.StartMining(true); err != nil {
threads := ctx.GlobalInt(utils.MinerLegacyThreadsFlag.Name)
if ctx.GlobalIsSet(utils.MinerThreadsFlag.Name) {
threads = ctx.GlobalInt(utils.MinerThreadsFlag.Name)
}
if err := ethereum.StartMining(threads); err != nil {
utils.Fatalf("Failed to start mining: %v", err) utils.Fatalf("Failed to start mining: %v", err)
} }
} }

@ -1130,12 +1130,6 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) {
if ctx.GlobalIsSet(CacheFlag.Name) || ctx.GlobalIsSet(CacheGCFlag.Name) { if ctx.GlobalIsSet(CacheFlag.Name) || ctx.GlobalIsSet(CacheGCFlag.Name) {
cfg.TrieCache = ctx.GlobalInt(CacheFlag.Name) * ctx.GlobalInt(CacheGCFlag.Name) / 100 cfg.TrieCache = ctx.GlobalInt(CacheFlag.Name) * ctx.GlobalInt(CacheGCFlag.Name) / 100
} }
if ctx.GlobalIsSet(MinerLegacyThreadsFlag.Name) {
cfg.MinerThreads = ctx.GlobalInt(MinerLegacyThreadsFlag.Name)
}
if ctx.GlobalIsSet(MinerThreadsFlag.Name) {
cfg.MinerThreads = ctx.GlobalInt(MinerThreadsFlag.Name)
}
if ctx.GlobalIsSet(MinerNotifyFlag.Name) { if ctx.GlobalIsSet(MinerNotifyFlag.Name) {
cfg.MinerNotify = strings.Split(ctx.GlobalString(MinerNotifyFlag.Name), ",") cfg.MinerNotify = strings.Split(ctx.GlobalString(MinerNotifyFlag.Name), ",")
} }

@ -24,6 +24,7 @@ import (
"io" "io"
"math/big" "math/big"
"os" "os"
"runtime"
"strings" "strings"
"time" "time"
@ -34,7 +35,6 @@ import (
"github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/internal/ethapi" "github.com/ethereum/go-ethereum/internal/ethapi"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/rpc"
@ -94,47 +94,22 @@ func NewPrivateMinerAPI(e *Ethereum) *PrivateMinerAPI {
return &PrivateMinerAPI{e: e} return &PrivateMinerAPI{e: e}
} }
// Start the miner with the given number of threads. If threads is nil the number // Start starts the miner with the given number of threads. If threads is nil,
// of workers started is equal to the number of logical CPUs that are usable by // the number of workers started is equal to the number of logical CPUs that are
// this process. If mining is already running, this method adjust the number of // usable by this process. If mining is already running, this method adjust the
// threads allowed to use and updates the minimum price required by the transaction // number of threads allowed to use and updates the minimum price required by the
// pool. // transaction pool.
func (api *PrivateMinerAPI) Start(threads *int) error { func (api *PrivateMinerAPI) Start(threads *int) error {
// Set the number of threads if the seal engine supports it
if threads == nil { if threads == nil {
threads = new(int) return api.e.StartMining(runtime.NumCPU())
} else if *threads == 0 {
*threads = -1 // Disable the miner from within
} }
type threaded interface { return api.e.StartMining(*threads)
SetThreads(threads int)
}
if th, ok := api.e.engine.(threaded); ok {
log.Info("Updated mining threads", "threads", *threads)
th.SetThreads(*threads)
}
// Start the miner and return
if !api.e.IsMining() {
// Propagate the initial price point to the transaction pool
api.e.lock.RLock()
price := api.e.gasPrice
api.e.lock.RUnlock()
api.e.txPool.SetGasPrice(price)
return api.e.StartMining(true)
}
return nil
} }
// Stop the miner // Stop terminates the miner, both at the consensus engine level as well as at
func (api *PrivateMinerAPI) Stop() bool { // the block creation level.
type threaded interface { func (api *PrivateMinerAPI) Stop() {
SetThreads(threads int)
}
if th, ok := api.e.engine.(threaded); ok {
th.SetThreads(-1)
}
api.e.StopMining() api.e.StopMining()
return true
} }
// SetExtra sets the extra data string that is included when this miner mines a block. // SetExtra sets the extra data string that is included when this miner mines a block.

@ -102,12 +102,18 @@ func (s *Ethereum) AddLesServer(ls LesServer) {
// New creates a new Ethereum object (including the // New creates a new Ethereum object (including the
// initialisation of the common Ethereum object) // initialisation of the common Ethereum object)
func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) { func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
// Ensure configuration values are compatible and sane
if config.SyncMode == downloader.LightSync { if config.SyncMode == downloader.LightSync {
return nil, errors.New("can't run eth.Ethereum in light sync mode, use les.LightEthereum") return nil, errors.New("can't run eth.Ethereum in light sync mode, use les.LightEthereum")
} }
if !config.SyncMode.IsValid() { if !config.SyncMode.IsValid() {
return nil, fmt.Errorf("invalid sync mode %d", config.SyncMode) return nil, fmt.Errorf("invalid sync mode %d", config.SyncMode)
} }
if config.MinerGasPrice == nil || config.MinerGasPrice.Cmp(common.Big0) <= 0 {
log.Warn("Sanitizing invalid miner gas price", "provided", config.MinerGasPrice, "updated", DefaultConfig.MinerGasPrice)
config.MinerGasPrice = new(big.Int).Set(DefaultConfig.MinerGasPrice)
}
// Assemble the Ethereum object
chainDb, err := CreateDB(ctx, config, "chaindata") chainDb, err := CreateDB(ctx, config, "chaindata")
if err != nil { if err != nil {
return nil, err return nil, err
@ -333,32 +339,66 @@ func (s *Ethereum) SetEtherbase(etherbase common.Address) {
s.miner.SetEtherbase(etherbase) s.miner.SetEtherbase(etherbase)
} }
func (s *Ethereum) StartMining(local bool) error { // StartMining starts the miner with the given number of CPU threads. If mining
eb, err := s.Etherbase() // is already running, this method adjust the number of threads allowed to use
if err != nil { // and updates the minimum price required by the transaction pool.
log.Error("Cannot start mining without etherbase", "err", err) func (s *Ethereum) StartMining(threads int) error {
return fmt.Errorf("etherbase missing: %v", err) // Update the thread count within the consensus engine
type threaded interface {
SetThreads(threads int)
} }
if clique, ok := s.engine.(*clique.Clique); ok { if th, ok := s.engine.(threaded); ok {
wallet, err := s.accountManager.Find(accounts.Account{Address: eb}) log.Info("Updated mining threads", "threads", threads)
if wallet == nil || err != nil { if threads == 0 {
log.Error("Etherbase account unavailable locally", "err", err) threads = -1 // Disable the miner from within
return fmt.Errorf("signer missing: %v", err)
} }
clique.Authorize(eb, wallet.SignHash) th.SetThreads(threads)
} }
if local { // If the miner was not running, initialize it
// If local (CPU) mining is started, we can disable the transaction rejection if !s.IsMining() {
// mechanism introduced to speed sync times. CPU mining on mainnet is ludicrous // Propagate the initial price point to the transaction pool
// so none will ever hit this path, whereas marking sync done on CPU mining s.lock.RLock()
// will ensure that private networks work in single miner mode too. price := s.gasPrice
s.lock.RUnlock()
s.txPool.SetGasPrice(price)
// Configure the local mining addess
eb, err := s.Etherbase()
if err != nil {
log.Error("Cannot start mining without etherbase", "err", err)
return fmt.Errorf("etherbase missing: %v", err)
}
if clique, ok := s.engine.(*clique.Clique); ok {
wallet, err := s.accountManager.Find(accounts.Account{Address: eb})
if wallet == nil || err != nil {
log.Error("Etherbase account unavailable locally", "err", err)
return fmt.Errorf("signer missing: %v", err)
}
clique.Authorize(eb, wallet.SignHash)
}
// If mining is started, we can disable the transaction rejection mechanism
// introduced to speed sync times.
atomic.StoreUint32(&s.protocolManager.acceptTxs, 1) atomic.StoreUint32(&s.protocolManager.acceptTxs, 1)
go s.miner.Start(eb)
} }
go s.miner.Start(eb)
return nil return nil
} }
func (s *Ethereum) StopMining() { s.miner.Stop() } // StopMining terminates the miner, both at the consensus engine level as well as
// at the block creation level.
func (s *Ethereum) StopMining() {
// Update the thread count within the consensus engine
type threaded interface {
SetThreads(threads int)
}
if th, ok := s.engine.(threaded); ok {
th.SetThreads(-1)
}
// Stop the block creating itself
s.miner.Stop()
}
func (s *Ethereum) IsMining() bool { return s.miner.Mining() } func (s *Ethereum) IsMining() bool { return s.miner.Mining() }
func (s *Ethereum) Miner() *miner.Miner { return s.miner } func (s *Ethereum) Miner() *miner.Miner { return s.miner }

@ -97,7 +97,6 @@ type Config struct {
// Mining-related options // Mining-related options
Etherbase common.Address `toml:",omitempty"` Etherbase common.Address `toml:",omitempty"`
MinerThreads int `toml:",omitempty"`
MinerNotify []string `toml:",omitempty"` MinerNotify []string `toml:",omitempty"`
MinerExtraData []byte `toml:",omitempty"` MinerExtraData []byte `toml:",omitempty"`
MinerGasPrice *big.Int MinerGasPrice *big.Int

@ -31,7 +31,6 @@ func (c Config) MarshalTOML() (interface{}, error) {
TrieCache int TrieCache int
TrieTimeout time.Duration TrieTimeout time.Duration
Etherbase common.Address `toml:",omitempty"` Etherbase common.Address `toml:",omitempty"`
MinerThreads int `toml:",omitempty"`
MinerNotify []string `toml:",omitempty"` MinerNotify []string `toml:",omitempty"`
MinerExtraData hexutil.Bytes `toml:",omitempty"` MinerExtraData hexutil.Bytes `toml:",omitempty"`
MinerGasPrice *big.Int MinerGasPrice *big.Int
@ -55,7 +54,6 @@ func (c Config) MarshalTOML() (interface{}, error) {
enc.TrieCache = c.TrieCache enc.TrieCache = c.TrieCache
enc.TrieTimeout = c.TrieTimeout enc.TrieTimeout = c.TrieTimeout
enc.Etherbase = c.Etherbase enc.Etherbase = c.Etherbase
enc.MinerThreads = c.MinerThreads
enc.MinerNotify = c.MinerNotify enc.MinerNotify = c.MinerNotify
enc.MinerExtraData = c.MinerExtraData enc.MinerExtraData = c.MinerExtraData
enc.MinerGasPrice = c.MinerGasPrice enc.MinerGasPrice = c.MinerGasPrice
@ -134,9 +132,6 @@ func (c *Config) UnmarshalTOML(unmarshal func(interface{}) error) error {
if dec.Etherbase != nil { if dec.Etherbase != nil {
c.Etherbase = *dec.Etherbase c.Etherbase = *dec.Etherbase
} }
if dec.MinerThreads != nil {
c.MinerThreads = *dec.MinerThreads
}
if dec.MinerNotify != nil { if dec.MinerNotify != nil {
c.MinerNotify = dec.MinerNotify c.MinerNotify = dec.MinerNotify
} }

Loading…
Cancel
Save