miner.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. func (self *Miner) update() {
  33. events := self.mux.Subscribe(downloader.StartEvent{}, downloader.DoneEvent{}, downloader.FailedEvent{})
  34. for ev := range events.Chan() {
  35. switch ev.(type) {
  36. case downloader.StartEvent:
  37. atomic.StoreInt32(&self.canStart, 0)
  38. if self.Mining() {
  39. self.Stop()
  40. glog.V(logger.Info).Infoln("Mining operation aborted due to sync operation")
  41. }
  42. case downloader.DoneEvent, downloader.FailedEvent:
  43. shouldStart := atomic.LoadInt32(&self.shouldStart) == 1
  44. atomic.StoreInt32(&self.canStart, 1)
  45. atomic.StoreInt32(&self.shouldStart, 0)
  46. if shouldStart {
  47. self.Start(self.coinbase, self.threads)
  48. }
  49. }
  50. }
  51. }
  52. func (m *Miner) SetGasPrice(price *big.Int) {
  53. // FIXME block tests set a nil gas price. Quick dirty fix
  54. if price == nil {
  55. return
  56. }
  57. m.worker.gasPrice = price
  58. }
  59. func (self *Miner) Start(coinbase common.Address, threads int) {
  60. atomic.StoreInt32(&self.shouldStart, 1)
  61. self.threads = threads
  62. self.worker.coinbase = coinbase
  63. if atomic.LoadInt32(&self.canStart) == 0 {
  64. glog.V(logger.Info).Infoln("Can not start mining operation due to network sync (starts when finished)")
  65. return
  66. }
  67. atomic.StoreInt32(&self.mining, 1)
  68. for i := 0; i < threads; i++ {
  69. self.worker.register(NewCpuAgent(i, self.pow))
  70. }
  71. glog.V(logger.Info).Infof("Starting mining operation (CPU=%d TOT=%d)\n", threads, len(self.worker.agents))
  72. self.worker.start()
  73. self.worker.commitNewWork()
  74. }
  75. func (self *Miner) Stop() {
  76. self.worker.stop()
  77. atomic.StoreInt32(&self.mining, 0)
  78. atomic.StoreInt32(&self.shouldStart, 0)
  79. }
  80. func (self *Miner) Register(agent Agent) {
  81. if atomic.LoadInt32(&self.mining) == 0 {
  82. agent.Start()
  83. }
  84. self.worker.register(agent)
  85. }
  86. func (self *Miner) Mining() bool {
  87. return atomic.LoadInt32(&self.mining) > 0
  88. }
  89. func (self *Miner) HashRate() int64 {
  90. return self.pow.GetHashrate()
  91. }
  92. func (self *Miner) SetExtra(extra []byte) {
  93. self.worker.extra = extra
  94. }
  95. func (self *Miner) PendingState() *state.StateDB {
  96. return self.worker.pendingState()
  97. }
  98. func (self *Miner) PendingBlock() *types.Block {
  99. return self.worker.pendingBlock()
  100. }