miner.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // Package miner implements Ethereum block creation and mining.
  17. package miner
  18. import (
  19. "fmt"
  20. "math/big"
  21. "sync/atomic"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/core"
  24. "github.com/ethereum/go-ethereum/core/state"
  25. "github.com/ethereum/go-ethereum/core/types"
  26. "github.com/ethereum/go-ethereum/eth/downloader"
  27. "github.com/ethereum/go-ethereum/event"
  28. "github.com/ethereum/go-ethereum/logger"
  29. "github.com/ethereum/go-ethereum/logger/glog"
  30. "github.com/ethereum/go-ethereum/params"
  31. "github.com/ethereum/go-ethereum/pow"
  32. )
  33. type Miner struct {
  34. mux *event.TypeMux
  35. worker *worker
  36. MinAcceptedGasPrice *big.Int
  37. threads int
  38. coinbase common.Address
  39. mining int32
  40. eth core.Backend
  41. pow pow.PoW
  42. canStart int32 // can start indicates whether we can start the mining operation
  43. shouldStart int32 // should start indicates whether we should start after sync
  44. }
  45. func New(eth core.Backend, config *core.ChainConfig, mux *event.TypeMux, pow pow.PoW) *Miner {
  46. miner := &Miner{eth: eth, mux: mux, pow: pow, worker: newWorker(config, common.Address{}, eth), canStart: 1}
  47. go miner.update()
  48. return miner
  49. }
  50. // update keeps track of the downloader events. Please be aware that this is a one shot type of update loop.
  51. // It's entered once and as soon as `Done` or `Failed` has been broadcasted the events are unregistered and
  52. // the loop is exited. This to prevent a major security vuln where external parties can DOS you with blocks
  53. // and halt your mining operation for as long as the DOS continues.
  54. func (self *Miner) update() {
  55. events := self.mux.Subscribe(downloader.StartEvent{}, downloader.DoneEvent{}, downloader.FailedEvent{})
  56. out:
  57. for ev := range events.Chan() {
  58. switch ev.Data.(type) {
  59. case downloader.StartEvent:
  60. atomic.StoreInt32(&self.canStart, 0)
  61. if self.Mining() {
  62. self.Stop()
  63. atomic.StoreInt32(&self.shouldStart, 1)
  64. glog.V(logger.Info).Infoln("Mining operation aborted due to sync operation")
  65. }
  66. case downloader.DoneEvent, downloader.FailedEvent:
  67. shouldStart := atomic.LoadInt32(&self.shouldStart) == 1
  68. atomic.StoreInt32(&self.canStart, 1)
  69. atomic.StoreInt32(&self.shouldStart, 0)
  70. if shouldStart {
  71. self.Start(self.coinbase, self.threads)
  72. }
  73. // unsubscribe. we're only interested in this event once
  74. events.Unsubscribe()
  75. // stop immediately and ignore all further pending events
  76. break out
  77. }
  78. }
  79. }
  80. func (m *Miner) SetGasPrice(price *big.Int) {
  81. // FIXME block tests set a nil gas price. Quick dirty fix
  82. if price == nil {
  83. return
  84. }
  85. m.worker.setGasPrice(price)
  86. }
  87. func (self *Miner) Start(coinbase common.Address, threads int) {
  88. atomic.StoreInt32(&self.shouldStart, 1)
  89. self.threads = threads
  90. self.worker.coinbase = coinbase
  91. self.coinbase = coinbase
  92. if atomic.LoadInt32(&self.canStart) == 0 {
  93. glog.V(logger.Info).Infoln("Can not start mining operation due to network sync (starts when finished)")
  94. return
  95. }
  96. atomic.StoreInt32(&self.mining, 1)
  97. for i := 0; i < threads; i++ {
  98. self.worker.register(NewCpuAgent(i, self.pow))
  99. }
  100. glog.V(logger.Info).Infof("Starting mining operation (CPU=%d TOT=%d)\n", threads, len(self.worker.agents))
  101. self.worker.start()
  102. self.worker.commitNewWork()
  103. }
  104. func (self *Miner) Stop() {
  105. self.worker.stop()
  106. atomic.StoreInt32(&self.mining, 0)
  107. atomic.StoreInt32(&self.shouldStart, 0)
  108. }
  109. func (self *Miner) Register(agent Agent) {
  110. if self.Mining() {
  111. agent.Start()
  112. }
  113. self.worker.register(agent)
  114. }
  115. func (self *Miner) Unregister(agent Agent) {
  116. self.worker.unregister(agent)
  117. }
  118. func (self *Miner) Mining() bool {
  119. return atomic.LoadInt32(&self.mining) > 0
  120. }
  121. func (self *Miner) HashRate() (tot int64) {
  122. tot += self.pow.GetHashrate()
  123. // do we care this might race? is it worth we're rewriting some
  124. // aspects of the worker/locking up agents so we can get an accurate
  125. // hashrate?
  126. for agent := range self.worker.agents {
  127. tot += agent.GetHashRate()
  128. }
  129. return
  130. }
  131. func (self *Miner) SetExtra(extra []byte) error {
  132. if uint64(len(extra)) > params.MaximumExtraDataSize.Uint64() {
  133. return fmt.Errorf("Extra exceeds max length. %d > %v", len(extra), params.MaximumExtraDataSize)
  134. }
  135. self.worker.extra = extra
  136. return nil
  137. }
  138. // Pending returns the currently pending block and associated state.
  139. func (self *Miner) Pending() (*types.Block, *state.StateDB) {
  140. return self.worker.pending()
  141. }
  142. func (self *Miner) SetEtherbase(addr common.Address) {
  143. self.coinbase = addr
  144. self.worker.setEtherbase(addr)
  145. }