github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/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/intfoundation/intchain/consensus"
    25  	tdmTypes "github.com/intfoundation/intchain/consensus/ipbft/types"
    26  	"github.com/intfoundation/intchain/core/types"
    27  	"github.com/intfoundation/intchain/log"
    28  )
    29  
    30  type CpuAgent struct {
    31  	mu sync.Mutex
    32  
    33  	workCh        chan *Work
    34  	stop          chan struct{}
    35  	quitCurrentOp chan struct{}
    36  	returnCh      chan<- *Result
    37  
    38  	chain  consensus.ChainReader
    39  	engine consensus.Engine
    40  
    41  	isMining int32 // isMining indicates whether the agent is currently mining
    42  
    43  	logger log.Logger
    44  }
    45  
    46  func NewCpuAgent(chain consensus.ChainReader, engine consensus.Engine, logger log.Logger) *CpuAgent {
    47  	miner := &CpuAgent{
    48  		chain:  chain,
    49  		engine: engine,
    50  		stop:   make(chan struct{}, 1),
    51  		workCh: make(chan *Work, 1),
    52  		logger: logger,
    53  	}
    54  	return miner
    55  }
    56  
    57  func (self *CpuAgent) Work() chan<- *Work            { return self.workCh }
    58  func (self *CpuAgent) SetReturnCh(ch chan<- *Result) { self.returnCh = ch }
    59  
    60  func (self *CpuAgent) Stop() {
    61  	if !atomic.CompareAndSwapInt32(&self.isMining, 1, 0) {
    62  		return // agent already stopped
    63  	}
    64  	self.stop <- struct{}{}
    65  done:
    66  	// Empty work channel
    67  	for {
    68  		select {
    69  		case <-self.workCh:
    70  		default:
    71  			break done
    72  		}
    73  	}
    74  }
    75  
    76  func (self *CpuAgent) Start() {
    77  	if !atomic.CompareAndSwapInt32(&self.isMining, 0, 1) {
    78  		return // agent already started
    79  	}
    80  	go self.update()
    81  }
    82  
    83  func (self *CpuAgent) update() {
    84  out:
    85  	for {
    86  		select {
    87  		case work := <-self.workCh:
    88  			self.mu.Lock()
    89  			if self.quitCurrentOp != nil {
    90  				close(self.quitCurrentOp)
    91  			}
    92  			self.quitCurrentOp = make(chan struct{})
    93  			go self.mine(work, self.quitCurrentOp)
    94  			self.mu.Unlock()
    95  		case <-self.stop:
    96  			self.mu.Lock()
    97  			if self.quitCurrentOp != nil {
    98  				close(self.quitCurrentOp)
    99  				self.quitCurrentOp = nil
   100  			}
   101  			self.mu.Unlock()
   102  			break out
   103  		}
   104  	}
   105  }
   106  
   107  func (self *CpuAgent) mine(work *Work, stop <-chan struct{}) {
   108  	if result, err := self.engine.Seal(self.chain, work.Block, stop); result != nil {
   109  		switch result := result.(type) {
   110  		case *types.Block:
   111  			self.logger.Info("Successfully sealed new block", "number", result.Number(), "hash", result.Hash())
   112  			self.returnCh <- &Result{Work: work, Block: result}
   113  		case *tdmTypes.IntermediateBlockResult:
   114  			self.logger.Info("v Successfully sealed new block", "number", result.Block.Number(), "hash", result.Block.Hash())
   115  			self.returnCh <- &Result{Intermediate: result}
   116  		}
   117  
   118  	} else {
   119  		if err != nil {
   120  			self.logger.Warn("Block sealing failed", "err", err)
   121  		}
   122  		self.logger.Warn("Block sealing aborted")
   123  		self.returnCh <- nil
   124  	}
   125  }