agent.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2015 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
  17. import (
  18. "sync"
  19. "sync/atomic"
  20. "github.com/ethereum/go-ethereum/consensus"
  21. "github.com/ethereum/go-ethereum/log"
  22. )
  23. type CpuAgent struct {
  24. mu sync.Mutex
  25. workCh chan *Work
  26. stop chan struct{}
  27. quitCurrentOp chan struct{}
  28. returnCh chan<- *Result
  29. chain consensus.ChainReader
  30. engine consensus.Engine
  31. isMining int32 // isMining indicates whether the agent is currently mining
  32. }
  33. func NewCpuAgent(chain consensus.ChainReader, engine consensus.Engine) *CpuAgent {
  34. miner := &CpuAgent{
  35. chain: chain,
  36. engine: engine,
  37. stop: make(chan struct{}, 1),
  38. workCh: make(chan *Work, 1),
  39. }
  40. return miner
  41. }
  42. func (self *CpuAgent) Work() chan<- *Work { return self.workCh }
  43. func (self *CpuAgent) SetReturnCh(ch chan<- *Result) { self.returnCh = ch }
  44. func (self *CpuAgent) Stop() {
  45. if !atomic.CompareAndSwapInt32(&self.isMining, 1, 0) {
  46. return // agent already stopped
  47. }
  48. self.stop <- struct{}{}
  49. done:
  50. // Empty work channel
  51. for {
  52. select {
  53. case <-self.workCh:
  54. default:
  55. break done
  56. }
  57. }
  58. }
  59. func (self *CpuAgent) Start() {
  60. if !atomic.CompareAndSwapInt32(&self.isMining, 0, 1) {
  61. return // agent already started
  62. }
  63. go self.update()
  64. }
  65. func (self *CpuAgent) update() {
  66. out:
  67. for {
  68. select {
  69. case work := <-self.workCh:
  70. self.mu.Lock()
  71. if self.quitCurrentOp != nil {
  72. close(self.quitCurrentOp)
  73. }
  74. self.quitCurrentOp = make(chan struct{})
  75. go self.mine(work, self.quitCurrentOp)
  76. self.mu.Unlock()
  77. case <-self.stop:
  78. self.mu.Lock()
  79. if self.quitCurrentOp != nil {
  80. close(self.quitCurrentOp)
  81. self.quitCurrentOp = nil
  82. }
  83. self.mu.Unlock()
  84. break out
  85. }
  86. }
  87. }
  88. func (self *CpuAgent) mine(work *Work, stop <-chan struct{}) {
  89. if result, err := self.engine.Seal(self.chain, work.Block, stop); result != nil {
  90. log.Info("Successfully sealed new block", "number", result.Number(), "hash", result.Hash())
  91. self.returnCh <- &Result{work, result}
  92. } else {
  93. if err != nil {
  94. log.Warn("Block sealing failed", "err", err)
  95. }
  96. self.returnCh <- nil
  97. }
  98. }
  99. func (self *CpuAgent) GetHashRate() int64 {
  100. if pow, ok := self.engine.(consensus.PoW); ok {
  101. return int64(pow.Hashrate())
  102. }
  103. return 0
  104. }