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