agent.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum 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. // go-ethereum 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 go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package miner
  17. import (
  18. "sync"
  19. "github.com/ethereum/go-ethereum/common"
  20. "github.com/ethereum/go-ethereum/core/types"
  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 *types.Block
  28. quit chan struct{}
  29. quitCurrentOp chan struct{}
  30. returnCh chan<- *types.Block
  31. index int
  32. pow pow.PoW
  33. }
  34. func NewCpuAgent(index int, pow pow.PoW) *CpuAgent {
  35. miner := &CpuAgent{
  36. pow: pow,
  37. index: index,
  38. }
  39. return miner
  40. }
  41. func (self *CpuAgent) Work() chan<- *types.Block { return self.workCh }
  42. func (self *CpuAgent) Pow() pow.PoW { return self.pow }
  43. func (self *CpuAgent) SetReturnCh(ch chan<- *types.Block) { self.returnCh = ch }
  44. func (self *CpuAgent) Stop() {
  45. self.mu.Lock()
  46. defer self.mu.Unlock()
  47. close(self.quit)
  48. }
  49. func (self *CpuAgent) Start() {
  50. self.mu.Lock()
  51. defer self.mu.Unlock()
  52. self.quit = make(chan struct{})
  53. // creating current op ch makes sure we're not closing a nil ch
  54. // later on
  55. self.workCh = make(chan *types.Block, 1)
  56. go self.update()
  57. }
  58. func (self *CpuAgent) update() {
  59. out:
  60. for {
  61. select {
  62. case block := <-self.workCh:
  63. self.mu.Lock()
  64. if self.quitCurrentOp != nil {
  65. close(self.quitCurrentOp)
  66. }
  67. self.quitCurrentOp = make(chan struct{})
  68. go self.mine(block, self.quitCurrentOp)
  69. self.mu.Unlock()
  70. case <-self.quit:
  71. self.mu.Lock()
  72. if self.quitCurrentOp != nil {
  73. close(self.quitCurrentOp)
  74. self.quitCurrentOp = nil
  75. }
  76. self.mu.Unlock()
  77. break out
  78. }
  79. }
  80. done:
  81. // Empty work channel
  82. for {
  83. select {
  84. case <-self.workCh:
  85. default:
  86. close(self.workCh)
  87. break done
  88. }
  89. }
  90. }
  91. func (self *CpuAgent) mine(block *types.Block, 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(block, stop)
  95. if nonce != 0 {
  96. self.returnCh <- block.WithMiningResult(nonce, common.BytesToHash(mixDigest))
  97. } else {
  98. self.returnCh <- nil
  99. }
  100. }
  101. func (self *CpuAgent) GetHashRate() int64 {
  102. return self.pow.GetHashrate()
  103. }