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