miner.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package miner
  2. import (
  3. "math/big"
  4. "sync/atomic"
  5. "github.com/ethereum/go-ethereum/common"
  6. "github.com/ethereum/go-ethereum/core"
  7. "github.com/ethereum/go-ethereum/core/state"
  8. "github.com/ethereum/go-ethereum/core/types"
  9. "github.com/ethereum/go-ethereum/eth/downloader"
  10. "github.com/ethereum/go-ethereum/event"
  11. "github.com/ethereum/go-ethereum/logger"
  12. "github.com/ethereum/go-ethereum/logger/glog"
  13. "github.com/ethereum/go-ethereum/pow"
  14. )
  15. type Miner struct {
  16. mux *event.TypeMux
  17. worker *worker
  18. MinAcceptedGasPrice *big.Int
  19. threads int
  20. coinbase common.Address
  21. mining int32
  22. eth core.Backend
  23. pow pow.PoW
  24. canStart int32 // can start indicates whether we can start the mining operation
  25. shouldStart int32 // should start indicates whether we should start after sync
  26. }
  27. func New(eth core.Backend, mux *event.TypeMux, pow pow.PoW) *Miner {
  28. miner := &Miner{eth: eth, mux: mux, pow: pow, worker: newWorker(common.Address{}, eth), canStart: 1}
  29. go miner.update()
  30. return miner
  31. }
  32. // update keeps track of the downloader events. Please be aware that this is a one shot type of update loop.
  33. // It's entered once and as soon as `Done` or `Failed` has been broadcasted the events are unregistered and
  34. // the loop is exited. This to prevent a major security vuln where external parties can DOS you with blocks
  35. // and halt your mining operation for as long as the DOS continues.
  36. func (self *Miner) update() {
  37. events := self.mux.Subscribe(downloader.StartEvent{}, downloader.DoneEvent{}, downloader.FailedEvent{})
  38. out:
  39. for ev := range events.Chan() {
  40. switch ev.(type) {
  41. case downloader.StartEvent:
  42. atomic.StoreInt32(&self.canStart, 0)
  43. if self.Mining() {
  44. self.Stop()
  45. atomic.StoreInt32(&self.shouldStart, 1)
  46. glog.V(logger.Info).Infoln("Mining operation aborted due to sync operation")
  47. }
  48. case downloader.DoneEvent, downloader.FailedEvent:
  49. shouldStart := atomic.LoadInt32(&self.shouldStart) == 1
  50. atomic.StoreInt32(&self.canStart, 1)
  51. atomic.StoreInt32(&self.shouldStart, 0)
  52. if shouldStart {
  53. self.Start(self.coinbase, self.threads)
  54. }
  55. // unsubscribe. we're only interested in this event once
  56. events.Unsubscribe()
  57. // stop immediately and ignore all further pending events
  58. break out
  59. }
  60. }
  61. }
  62. func (m *Miner) SetGasPrice(price *big.Int) {
  63. // FIXME block tests set a nil gas price. Quick dirty fix
  64. if price == nil {
  65. return
  66. }
  67. m.worker.setGasPrice(price)
  68. }
  69. func (self *Miner) Start(coinbase common.Address, threads int) {
  70. atomic.StoreInt32(&self.shouldStart, 1)
  71. self.threads = threads
  72. self.worker.coinbase = coinbase
  73. self.coinbase = coinbase
  74. if atomic.LoadInt32(&self.canStart) == 0 {
  75. glog.V(logger.Info).Infoln("Can not start mining operation due to network sync (starts when finished)")
  76. return
  77. }
  78. atomic.StoreInt32(&self.mining, 1)
  79. for i := 0; i < threads; i++ {
  80. self.worker.register(NewCpuAgent(i, self.pow))
  81. }
  82. glog.V(logger.Info).Infof("Starting mining operation (CPU=%d TOT=%d)\n", threads, len(self.worker.agents))
  83. self.worker.start()
  84. self.worker.commitNewWork()
  85. }
  86. func (self *Miner) Stop() {
  87. self.worker.stop()
  88. atomic.StoreInt32(&self.mining, 0)
  89. atomic.StoreInt32(&self.shouldStart, 0)
  90. }
  91. func (self *Miner) Register(agent Agent) {
  92. if self.Mining() {
  93. agent.Start()
  94. }
  95. self.worker.register(agent)
  96. }
  97. func (self *Miner) Mining() bool {
  98. return atomic.LoadInt32(&self.mining) > 0
  99. }
  100. func (self *Miner) HashRate() int64 {
  101. return self.pow.GetHashrate()
  102. }
  103. func (self *Miner) SetExtra(extra []byte) {
  104. self.worker.extra = extra
  105. }
  106. func (self *Miner) PendingState() *state.StateDB {
  107. return self.worker.pendingState()
  108. }
  109. func (self *Miner) PendingBlock() *types.Block {
  110. return self.worker.pendingBlock()
  111. }
  112. func (self *Miner) SetEtherbase(addr common.Address) {
  113. self.coinbase = addr
  114. self.worker.setEtherbase(addr)
  115. }