agent.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/common"
  21. "github.com/ethereum/go-ethereum/logger"
  22. "github.com/ethereum/go-ethereum/logger/glog"
  23. "github.com/ethereum/go-ethereum/pow"
  24. )
  25. type CpuAgent struct {
  26. mu sync.Mutex
  27. workCh chan *Work
  28. quit chan struct{}
  29. quitCurrentOp chan struct{}
  30. returnCh chan<- *Result
  31. index int
  32. pow pow.PoW
  33. isMining int32 // isMining indicates whether the agent is currently mining
  34. }
  35. func NewCpuAgent(index int, pow pow.PoW) *CpuAgent {
  36. miner := &CpuAgent{
  37. pow: pow,
  38. index: index,
  39. }
  40. return miner
  41. }
  42. func (self *CpuAgent) Work() chan<- *Work { return self.workCh }
  43. func (self *CpuAgent) Pow() pow.PoW { return self.pow }
  44. func (self *CpuAgent) SetReturnCh(ch chan<- *Result) { self.returnCh = ch }
  45. func (self *CpuAgent) Stop() {
  46. self.mu.Lock()
  47. defer self.mu.Unlock()
  48. close(self.quit)
  49. }
  50. func (self *CpuAgent) Start() {
  51. self.mu.Lock()
  52. defer self.mu.Unlock()
  53. if !atomic.CompareAndSwapInt32(&self.isMining, 0, 1) {
  54. return // agent already started
  55. }
  56. self.quit = make(chan struct{})
  57. // creating current op ch makes sure we're not closing a nil ch
  58. // later on
  59. self.workCh = make(chan *Work, 1)
  60. go self.update()
  61. }
  62. func (self *CpuAgent) update() {
  63. out:
  64. for {
  65. select {
  66. case work := <-self.workCh:
  67. self.mu.Lock()
  68. if self.quitCurrentOp != nil {
  69. close(self.quitCurrentOp)
  70. }
  71. self.quitCurrentOp = make(chan struct{})
  72. go self.mine(work, self.quitCurrentOp)
  73. self.mu.Unlock()
  74. case <-self.quit:
  75. self.mu.Lock()
  76. if self.quitCurrentOp != nil {
  77. close(self.quitCurrentOp)
  78. self.quitCurrentOp = nil
  79. }
  80. self.mu.Unlock()
  81. break out
  82. }
  83. }
  84. done:
  85. // Empty work channel
  86. for {
  87. select {
  88. case <-self.workCh:
  89. default:
  90. close(self.workCh)
  91. break done
  92. }
  93. }
  94. atomic.StoreInt32(&self.isMining, 0)
  95. }
  96. func (self *CpuAgent) mine(work *Work, stop <-chan struct{}) {
  97. glog.V(logger.Debug).Infof("(re)started agent[%d]. mining...\n", self.index)
  98. // Mine
  99. nonce, mixDigest := self.pow.Search(work.Block, stop)
  100. if nonce != 0 {
  101. block := work.Block.WithMiningResult(nonce, common.BytesToHash(mixDigest))
  102. self.returnCh <- &Result{work, block}
  103. } else {
  104. self.returnCh <- nil
  105. }
  106. }
  107. func (self *CpuAgent) GetHashRate() int64 {
  108. return self.pow.GetHashrate()
  109. }