agent.go 3.0 KB

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