diff options
author | Péter Szilágyi <peterke@gmail.com> | 2018-08-23 18:02:36 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2018-08-23 19:09:45 +0800 |
commit | 92381ee00999398d0d0928d2c4be82b3334b912b (patch) | |
tree | a0bdcae53eace4fbf158c706b8c3cd33831edc8c /eth | |
parent | f34f361ca6635690f6dd81d6f3bddfff498e9fd6 (diff) | |
download | go-tangerine-92381ee00999398d0d0928d2c4be82b3334b912b.tar.gz go-tangerine-92381ee00999398d0d0928d2c4be82b3334b912b.tar.zst go-tangerine-92381ee00999398d0d0928d2c4be82b3334b912b.zip |
cmd, eth: clean up miner startup API, drop noop config field
Diffstat (limited to 'eth')
-rw-r--r-- | eth/api.go | 47 | ||||
-rw-r--r-- | eth/backend.go | 76 | ||||
-rw-r--r-- | eth/config.go | 1 | ||||
-rw-r--r-- | eth/gen_config.go | 5 |
4 files changed, 69 insertions, 60 deletions
diff --git a/eth/api.go b/eth/api.go index 4b0ba8edb..708f75a78 100644 --- a/eth/api.go +++ b/eth/api.go @@ -24,6 +24,7 @@ import ( "io" "math/big" "os" + "runtime" "strings" "time" @@ -34,7 +35,6 @@ import ( "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/internal/ethapi" - "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rpc" @@ -94,47 +94,22 @@ func NewPrivateMinerAPI(e *Ethereum) *PrivateMinerAPI { return &PrivateMinerAPI{e: e} } -// Start the miner with the given number of threads. If threads is nil the number -// of workers started is equal to the number of logical CPUs that are usable by -// this process. If mining is already running, this method adjust the number of -// threads allowed to use and updates the minimum price required by the transaction -// pool. +// Start starts the miner with the given number of threads. If threads is nil, +// the number of workers started is equal to the number of logical CPUs that are +// usable by this process. If mining is already running, this method adjust the +// number of threads allowed to use and updates the minimum price required by the +// transaction pool. func (api *PrivateMinerAPI) Start(threads *int) error { - // Set the number of threads if the seal engine supports it if threads == nil { - threads = new(int) - } else if *threads == 0 { - *threads = -1 // Disable the miner from within + return api.e.StartMining(runtime.NumCPU()) } - type threaded interface { - 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 + return api.e.StartMining(*threads) } -// Stop the miner -func (api *PrivateMinerAPI) Stop() bool { - type threaded interface { - SetThreads(threads int) - } - if th, ok := api.e.engine.(threaded); ok { - th.SetThreads(-1) - } +// Stop terminates the miner, both at the consensus engine level as well as at +// the block creation level. +func (api *PrivateMinerAPI) Stop() { api.e.StopMining() - return true } // SetExtra sets the extra data string that is included when this miner mines a block. diff --git a/eth/backend.go b/eth/backend.go index 648175acf..c530d3c7c 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -102,12 +102,18 @@ func (s *Ethereum) AddLesServer(ls LesServer) { // New creates a new Ethereum object (including the // initialisation of the common Ethereum object) func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) { + // Ensure configuration values are compatible and sane if config.SyncMode == downloader.LightSync { return nil, errors.New("can't run eth.Ethereum in light sync mode, use les.LightEthereum") } if !config.SyncMode.IsValid() { 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") if err != nil { return nil, err @@ -333,32 +339,66 @@ func (s *Ethereum) SetEtherbase(etherbase common.Address) { s.miner.SetEtherbase(etherbase) } -func (s *Ethereum) StartMining(local bool) error { - eb, err := s.Etherbase() - if err != nil { - log.Error("Cannot start mining without etherbase", "err", err) - return fmt.Errorf("etherbase missing: %v", err) +// StartMining starts the miner with the given number of CPU threads. If mining +// is already running, this method adjust the number of threads allowed to use +// and updates the minimum price required by the transaction pool. +func (s *Ethereum) StartMining(threads int) error { + // Update the thread count within the consensus engine + type threaded interface { + SetThreads(threads int) } - 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) + if th, ok := s.engine.(threaded); ok { + log.Info("Updated mining threads", "threads", threads) + if threads == 0 { + threads = -1 // Disable the miner from within } - clique.Authorize(eb, wallet.SignHash) + th.SetThreads(threads) } - if local { - // If local (CPU) mining is started, we can disable the transaction rejection - // mechanism introduced to speed sync times. CPU mining on mainnet is ludicrous - // so none will ever hit this path, whereas marking sync done on CPU mining - // will ensure that private networks work in single miner mode too. + // If the miner was not running, initialize it + if !s.IsMining() { + // Propagate the initial price point to the transaction pool + s.lock.RLock() + 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) + + go s.miner.Start(eb) } - go s.miner.Start(eb) 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) Miner() *miner.Miner { return s.miner } diff --git a/eth/config.go b/eth/config.go index 1398a5514..5b2eca585 100644 --- a/eth/config.go +++ b/eth/config.go @@ -97,7 +97,6 @@ type Config struct { // Mining-related options Etherbase common.Address `toml:",omitempty"` - MinerThreads int `toml:",omitempty"` MinerNotify []string `toml:",omitempty"` MinerExtraData []byte `toml:",omitempty"` MinerGasPrice *big.Int diff --git a/eth/gen_config.go b/eth/gen_config.go index 62556be7e..a72e35bcd 100644 --- a/eth/gen_config.go +++ b/eth/gen_config.go @@ -31,7 +31,6 @@ func (c Config) MarshalTOML() (interface{}, error) { TrieCache int TrieTimeout time.Duration Etherbase common.Address `toml:",omitempty"` - MinerThreads int `toml:",omitempty"` MinerNotify []string `toml:",omitempty"` MinerExtraData hexutil.Bytes `toml:",omitempty"` MinerGasPrice *big.Int @@ -55,7 +54,6 @@ func (c Config) MarshalTOML() (interface{}, error) { enc.TrieCache = c.TrieCache enc.TrieTimeout = c.TrieTimeout enc.Etherbase = c.Etherbase - enc.MinerThreads = c.MinerThreads enc.MinerNotify = c.MinerNotify enc.MinerExtraData = c.MinerExtraData enc.MinerGasPrice = c.MinerGasPrice @@ -134,9 +132,6 @@ func (c *Config) UnmarshalTOML(unmarshal func(interface{}) error) error { if dec.Etherbase != nil { c.Etherbase = *dec.Etherbase } - if dec.MinerThreads != nil { - c.MinerThreads = *dec.MinerThreads - } if dec.MinerNotify != nil { c.MinerNotify = dec.MinerNotify } |