@@ -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)
104104func 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+
362402func (s * Ethereum ) IsMining () bool { return s .miner .Mining () }
363403func (s * Ethereum ) Miner () * miner.Miner { return s .miner }
364404
0 commit comments