github.com/daragao/go-ethereum@v1.8.14-0.20180809141559-45eaef243198/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 "sync/atomic" 22 23 "github.com/ethereum/go-ethereum/consensus" 24 "github.com/ethereum/go-ethereum/log" 25 ) 26 27 type CpuAgent struct { 28 mu sync.Mutex 29 30 taskCh chan *Package 31 returnCh chan<- *Package 32 stop chan struct{} 33 quitCurrentOp chan struct{} 34 35 chain consensus.ChainReader 36 engine consensus.Engine 37 38 started int32 // started indicates whether the agent is currently started 39 } 40 41 func NewCpuAgent(chain consensus.ChainReader, engine consensus.Engine) *CpuAgent { 42 agent := &CpuAgent{ 43 chain: chain, 44 engine: engine, 45 stop: make(chan struct{}, 1), 46 taskCh: make(chan *Package, 1), 47 } 48 return agent 49 } 50 51 func (self *CpuAgent) AssignTask(p *Package) { 52 if atomic.LoadInt32(&self.started) == 1 { 53 self.taskCh <- p 54 } 55 } 56 func (self *CpuAgent) DeliverTo(ch chan<- *Package) { self.returnCh = ch } 57 58 func (self *CpuAgent) Start() { 59 if !atomic.CompareAndSwapInt32(&self.started, 0, 1) { 60 return // agent already started 61 } 62 go self.update() 63 } 64 65 func (self *CpuAgent) Stop() { 66 if !atomic.CompareAndSwapInt32(&self.started, 1, 0) { 67 return // agent already stopped 68 } 69 self.stop <- struct{}{} 70 done: 71 // Empty work channel 72 for { 73 select { 74 case <-self.taskCh: 75 default: 76 break done 77 } 78 } 79 } 80 81 func (self *CpuAgent) update() { 82 out: 83 for { 84 select { 85 case p := <-self.taskCh: 86 self.mu.Lock() 87 if self.quitCurrentOp != nil { 88 close(self.quitCurrentOp) 89 } 90 self.quitCurrentOp = make(chan struct{}) 91 go self.mine(p, self.quitCurrentOp) 92 self.mu.Unlock() 93 case <-self.stop: 94 self.mu.Lock() 95 if self.quitCurrentOp != nil { 96 close(self.quitCurrentOp) 97 self.quitCurrentOp = nil 98 } 99 self.mu.Unlock() 100 break out 101 } 102 } 103 } 104 105 func (self *CpuAgent) mine(p *Package, stop <-chan struct{}) { 106 var err error 107 if p.Block, err = self.engine.Seal(self.chain, p.Block, stop); p.Block != nil { 108 log.Info("Successfully sealed new block", "number", p.Block.Number(), "hash", p.Block.Hash()) 109 self.returnCh <- p 110 } else { 111 if err != nil { 112 log.Warn("Block sealing failed", "err", err) 113 } 114 self.returnCh <- nil 115 } 116 }