github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/producer/producer.go (about)

     1  // Copyright 2014 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 producer implements Hubble block creation and producing.
    18  package producer
    19  
    20  import (
    21  	"fmt"
    22  	"sync/atomic"
    23  
    24  	"github.com/vntchain/go-vnt/accounts"
    25  	"github.com/vntchain/go-vnt/common"
    26  	"github.com/vntchain/go-vnt/consensus"
    27  	"github.com/vntchain/go-vnt/core"
    28  	"github.com/vntchain/go-vnt/core/state"
    29  	"github.com/vntchain/go-vnt/core/types"
    30  	"github.com/vntchain/go-vnt/event"
    31  	"github.com/vntchain/go-vnt/log"
    32  	"github.com/vntchain/go-vnt/params"
    33  	"github.com/vntchain/go-vnt/vnt/downloader"
    34  	"github.com/vntchain/go-vnt/vntdb"
    35  )
    36  
    37  // Backend wraps all methods required for producing.
    38  type Backend interface {
    39  	AccountManager() *accounts.Manager
    40  	BlockChain() *core.BlockChain
    41  	TxPool() *core.TxPool
    42  	ChainDb() vntdb.Database
    43  }
    44  
    45  // Producer creates blocks and searches for proof-of-work values.
    46  type Producer struct {
    47  	mux *event.TypeMux
    48  
    49  	worker    *worker
    50  	coinbase  common.Address
    51  	producing int32
    52  	vnt       Backend
    53  	engine    consensus.Engine
    54  
    55  	canStart    int32 // can start indicates whether we can start the block producing operation
    56  	shouldStart int32 // should start indicates whether we should start after sync
    57  }
    58  
    59  func New(vnt Backend, config *params.ChainConfig, mux *event.TypeMux, engine consensus.Engine) *Producer {
    60  	producer := &Producer{
    61  		vnt:      vnt,
    62  		mux:      mux,
    63  		engine:   engine,
    64  		worker:   newWorker(config, engine, common.Address{}, vnt, mux),
    65  		canStart: 1,
    66  	}
    67  	go producer.update()
    68  
    69  	return producer
    70  }
    71  
    72  // update keeps track of the downloader events. Please be aware that this is a one shot type of update loop.
    73  // It's entered once and as soon as `Done` or `Failed` has been broadcasted the events are unregistered and
    74  // the loop is exited. This to prevent a major security vuln where external parties can DOS you with blocks
    75  // and halt your producing operation for as long as the DOS continues.
    76  func (self *Producer) update() {
    77  	events := self.mux.Subscribe(downloader.StartEvent{}, downloader.DoneEvent{}, downloader.FailedEvent{})
    78  out:
    79  	for ev := range events.Chan() {
    80  		switch ev.Data.(type) {
    81  		case downloader.StartEvent:
    82  			atomic.StoreInt32(&self.canStart, 0)
    83  			if self.Producing() {
    84  				self.Stop()
    85  				atomic.StoreInt32(&self.shouldStart, 1)
    86  				log.Info("Producing aborted due to sync")
    87  			}
    88  		case downloader.DoneEvent, downloader.FailedEvent:
    89  			shouldStart := atomic.LoadInt32(&self.shouldStart) == 1
    90  
    91  			atomic.StoreInt32(&self.canStart, 1)
    92  			atomic.StoreInt32(&self.shouldStart, 0)
    93  			if shouldStart {
    94  				self.Start(self.coinbase)
    95  			}
    96  			// unsubscribe. we're only interested in this event once
    97  			events.Unsubscribe()
    98  			// stop immediately and ignore all further pending events
    99  			break out
   100  		}
   101  	}
   102  }
   103  
   104  func (self *Producer) Start(coinbase common.Address) {
   105  	atomic.StoreInt32(&self.shouldStart, 1)
   106  	self.SetCoinbase(coinbase)
   107  
   108  	if atomic.LoadInt32(&self.canStart) == 0 {
   109  		log.Info("Network syncing, will start producer afterwards")
   110  		return
   111  	}
   112  	atomic.StoreInt32(&self.producing, 1)
   113  
   114  	log.Info("Starting block producing operation")
   115  	self.worker.start()
   116  	self.worker.commitNewWork()
   117  }
   118  
   119  func (self *Producer) Stop() {
   120  	self.worker.stop()
   121  	atomic.StoreInt32(&self.producing, 0)
   122  	atomic.StoreInt32(&self.shouldStart, 0)
   123  }
   124  
   125  func (self *Producer) Producing() bool {
   126  	return atomic.LoadInt32(&self.producing) > 0
   127  }
   128  
   129  func (self *Producer) SetExtra(extra []byte) error {
   130  	if uint64(len(extra)) > params.MaximumExtraDataSize {
   131  		return fmt.Errorf("Extra exceeds max length. %d > %v", len(extra), params.MaximumExtraDataSize)
   132  	}
   133  	self.worker.setExtra(extra)
   134  	return nil
   135  }
   136  
   137  // Pending returns the currently pending block and associated state.
   138  func (self *Producer) Pending() (*types.Block, *state.StateDB) {
   139  	return self.worker.pending()
   140  }
   141  
   142  // PendingBlock returns the currently pending block.
   143  //
   144  // Note, to access both the pending block and the pending state
   145  // simultaneously, please use Pending(), as the pending state can
   146  // change between multiple method calls
   147  func (self *Producer) PendingBlock() *types.Block {
   148  	return self.worker.pendingBlock()
   149  }
   150  
   151  func (self *Producer) SetCoinbase(addr common.Address) {
   152  	self.coinbase = addr
   153  	self.worker.setCoinbase(addr)
   154  }