Skip to content

Commit 67d6d0b

Browse files
authored
Merge pull request #17492 from karalabe/eth-miner-threads-defaults
cmd, eth: clean up miner startup API, drop noop config field
2 parents 1df1187 + 1e63a01 commit 67d6d0b

8 files changed

Lines changed: 489 additions & 80 deletions

File tree

‎cmd/geth/main.go‎

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -336,26 +336,18 @@ func startNode(ctx *cli.Context, stack *node.Node) {
336336
if err := stack.Service(&ethereum); err != nil {
337337
utils.Fatalf("Ethereum service not running: %v", err)
338338
}
339-
// Use a reduced number of threads if requested
340-
threads := ctx.GlobalInt(utils.MinerLegacyThreadsFlag.Name)
341-
if ctx.GlobalIsSet(utils.MinerThreadsFlag.Name) {
342-
threads = ctx.GlobalInt(utils.MinerThreadsFlag.Name)
343-
}
344-
if threads > 0 {
345-
type threaded interface {
346-
SetThreads(threads int)
347-
}
348-
if th, ok := ethereum.Engine().(threaded); ok {
349-
th.SetThreads(threads)
350-
}
351-
}
352339
// Set the gas price to the limits from the CLI and start mining
353340
gasprice := utils.GlobalBig(ctx, utils.MinerLegacyGasPriceFlag.Name)
354341
if ctx.IsSet(utils.MinerGasPriceFlag.Name) {
355342
gasprice = utils.GlobalBig(ctx, utils.MinerGasPriceFlag.Name)
356343
}
357344
ethereum.TxPool().SetGasPrice(gasprice)
358-
if err := ethereum.StartMining(true); err != nil {
345+
346+
threads := ctx.GlobalInt(utils.MinerLegacyThreadsFlag.Name)
347+
if ctx.GlobalIsSet(utils.MinerThreadsFlag.Name) {
348+
threads = ctx.GlobalInt(utils.MinerThreadsFlag.Name)
349+
}
350+
if err := ethereum.StartMining(threads); err != nil {
359351
utils.Fatalf("Failed to start mining: %v", err)
360352
}
361353
}

‎cmd/utils/flags.go‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,12 +1130,6 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) {
11301130
if ctx.GlobalIsSet(CacheFlag.Name) || ctx.GlobalIsSet(CacheGCFlag.Name) {
11311131
cfg.TrieCache = ctx.GlobalInt(CacheFlag.Name) * ctx.GlobalInt(CacheGCFlag.Name) / 100
11321132
}
1133-
if ctx.GlobalIsSet(MinerLegacyThreadsFlag.Name) {
1134-
cfg.MinerThreads = ctx.GlobalInt(MinerLegacyThreadsFlag.Name)
1135-
}
1136-
if ctx.GlobalIsSet(MinerThreadsFlag.Name) {
1137-
cfg.MinerThreads = ctx.GlobalInt(MinerThreadsFlag.Name)
1138-
}
11391133
if ctx.GlobalIsSet(MinerNotifyFlag.Name) {
11401134
cfg.MinerNotify = strings.Split(ctx.GlobalString(MinerNotifyFlag.Name), ",")
11411135
}

‎eth/api.go‎

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"io"
2525
"math/big"
2626
"os"
27+
"runtime"
2728
"strings"
2829
"time"
2930

@@ -34,7 +35,6 @@ import (
3435
"github.com/ethereum/go-ethereum/core/state"
3536
"github.com/ethereum/go-ethereum/core/types"
3637
"github.com/ethereum/go-ethereum/internal/ethapi"
37-
"github.com/ethereum/go-ethereum/log"
3838
"github.com/ethereum/go-ethereum/params"
3939
"github.com/ethereum/go-ethereum/rlp"
4040
"github.com/ethereum/go-ethereum/rpc"
@@ -94,47 +94,22 @@ func NewPrivateMinerAPI(e *Ethereum) *PrivateMinerAPI {
9494
return &PrivateMinerAPI{e: e}
9595
}
9696

97-
// Start the miner with the given number of threads. If threads is nil the number
98-
// of workers started is equal to the number of logical CPUs that are usable by
99-
// this process. If mining is already running, this method adjust the number of
100-
// threads allowed to use and updates the minimum price required by the transaction
101-
// pool.
97+
// Start starts the miner with the given number of threads. If threads is nil,
98+
// the number of workers started is equal to the number of logical CPUs that are
99+
// usable by this process. If mining is already running, this method adjust the
100+
// number of threads allowed to use and updates the minimum price required by the
101+
// transaction pool.
102102
func (api *PrivateMinerAPI) Start(threads *int) error {
103-
// Set the number of threads if the seal engine supports it
104103
if threads == nil {
105-
threads = new(int)
106-
} else if *threads == 0 {
107-
*threads = -1 // Disable the miner from within
104+
return api.e.StartMining(runtime.NumCPU())
108105
}
109-
type threaded interface {
110-
SetThreads(threads int)
111-
}
112-
if th, ok := api.e.engine.(threaded); ok {
113-
log.Info("Updated mining threads", "threads", *threads)
114-
th.SetThreads(*threads)
115-
}
116-
// Start the miner and return
117-
if !api.e.IsMining() {
118-
// Propagate the initial price point to the transaction pool
119-
api.e.lock.RLock()
120-
price := api.e.gasPrice
121-
api.e.lock.RUnlock()
122-
api.e.txPool.SetGasPrice(price)
123-
return api.e.StartMining(true)
124-
}
125-
return nil
106+
return api.e.StartMining(*threads)
126107
}
127108

128-
// Stop the miner
129-
func (api *PrivateMinerAPI) Stop() bool {
130-
type threaded interface {
131-
SetThreads(threads int)
132-
}
133-
if th, ok := api.e.engine.(threaded); ok {
134-
th.SetThreads(-1)
135-
}
109+
// Stop terminates the miner, both at the consensus engine level as well as at
110+
// the block creation level.
111+
func (api *PrivateMinerAPI) Stop() {
136112
api.e.StopMining()
137-
return true
138113
}
139114

140115
// SetExtra sets the extra data string that is included when this miner mines a block.

‎eth/backend.go‎

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,18 @@ func (s *Ethereum) AddLesServer(ls LesServer) {
102102
// New creates a new Ethereum object (including the
103103
// initialisation of the common Ethereum object)
104104
func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
105+
// Ensure configuration values are compatible and sane
105106
if config.SyncMode == downloader.LightSync {
106107
return nil, errors.New("can't run eth.Ethereum in light sync mode, use les.LightEthereum")
107108
}
108109
if !config.SyncMode.IsValid() {
109110
return nil, fmt.Errorf("invalid sync mode %d", config.SyncMode)
110111
}
112+
if config.MinerGasPrice == nil || config.MinerGasPrice.Cmp(common.Big0) <= 0 {
113+
log.Warn("Sanitizing invalid miner gas price", "provided", config.MinerGasPrice, "updated", DefaultConfig.MinerGasPrice)
114+
config.MinerGasPrice = new(big.Int).Set(DefaultConfig.MinerGasPrice)
115+
}
116+
// Assemble the Ethereum object
111117
chainDb, err := CreateDB(ctx, config, "chaindata")
112118
if err != nil {
113119
return nil, err
@@ -333,32 +339,66 @@ func (s *Ethereum) SetEtherbase(etherbase common.Address) {
333339
s.miner.SetEtherbase(etherbase)
334340
}
335341

336-
func (s *Ethereum) StartMining(local bool) error {
337-
eb, err := s.Etherbase()
338-
if err != nil {
339-
log.Error("Cannot start mining without etherbase", "err", err)
340-
return fmt.Errorf("etherbase missing: %v", err)
342+
// StartMining starts the miner with the given number of CPU threads. If mining
343+
// is already running, this method adjust the number of threads allowed to use
344+
// and updates the minimum price required by the transaction pool.
345+
func (s *Ethereum) StartMining(threads int) error {
346+
// Update the thread count within the consensus engine
347+
type threaded interface {
348+
SetThreads(threads int)
341349
}
342-
if clique, ok := s.engine.(*clique.Clique); ok {
343-
wallet, err := s.accountManager.Find(accounts.Account{Address: eb})
344-
if wallet == nil || err != nil {
345-
log.Error("Etherbase account unavailable locally", "err", err)
346-
return fmt.Errorf("signer missing: %v", err)
350+
if th, ok := s.engine.(threaded); ok {
351+
log.Info("Updated mining threads", "threads", threads)
352+
if threads == 0 {
353+
threads = -1 // Disable the miner from within
347354
}
348-
clique.Authorize(eb, wallet.SignHash)
355+
th.SetThreads(threads)
349356
}
350-
if local {
351-
// If local (CPU) mining is started, we can disable the transaction rejection
352-
// mechanism introduced to speed sync times. CPU mining on mainnet is ludicrous
353-
// so none will ever hit this path, whereas marking sync done on CPU mining
354-
// will ensure that private networks work in single miner mode too.
357+
// If the miner was not running, initialize it
358+
if !s.IsMining() {
359+
// Propagate the initial price point to the transaction pool
360+
s.lock.RLock()
361+
price := s.gasPrice
362+
s.lock.RUnlock()
363+
s.txPool.SetGasPrice(price)
364+
365+
// Configure the local mining addess
366+
eb, err := s.Etherbase()
367+
if err != nil {
368+
log.Error("Cannot start mining without etherbase", "err", err)
369+
return fmt.Errorf("etherbase missing: %v", err)
370+
}
371+
if clique, ok := s.engine.(*clique.Clique); ok {
372+
wallet, err := s.accountManager.Find(accounts.Account{Address: eb})
373+
if wallet == nil || err != nil {
374+
log.Error("Etherbase account unavailable locally", "err", err)
375+
return fmt.Errorf("signer missing: %v", err)
376+
}
377+
clique.Authorize(eb, wallet.SignHash)
378+
}
379+
// If mining is started, we can disable the transaction rejection mechanism
380+
// introduced to speed sync times.
355381
atomic.StoreUint32(&s.protocolManager.acceptTxs, 1)
382+
383+
go s.miner.Start(eb)
356384
}
357-
go s.miner.Start(eb)
358385
return nil
359386
}
360387

361-
func (s *Ethereum) StopMining() { s.miner.Stop() }
388+
// StopMining terminates the miner, both at the consensus engine level as well as
389+
// at the block creation level.
390+
func (s *Ethereum) StopMining() {
391+
// Update the thread count within the consensus engine
392+
type threaded interface {
393+
SetThreads(threads int)
394+
}
395+
if th, ok := s.engine.(threaded); ok {
396+
th.SetThreads(-1)
397+
}
398+
// Stop the block creating itself
399+
s.miner.Stop()
400+
}
401+
362402
func (s *Ethereum) IsMining() bool { return s.miner.Mining() }
363403
func (s *Ethereum) Miner() *miner.Miner { return s.miner }
364404

‎eth/config.go‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ type Config struct {
9797

9898
// Mining-related options
9999
Etherbase common.Address `toml:",omitempty"`
100-
MinerThreads int `toml:",omitempty"`
101100
MinerNotify []string `toml:",omitempty"`
102101
MinerExtraData []byte `toml:",omitempty"`
103102
MinerGasPrice *big.Int

‎eth/gen_config.go‎

Lines changed: 0 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)