github.com/FusionFoundation/efsn/v4@v4.2.0/miner/miner.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 miner implements Ethereum block creation and mining. 18 package miner 19 20 import ( 21 "fmt" 22 "math/big" 23 "sync" 24 "time" 25 26 "github.com/FusionFoundation/efsn/v4/common" 27 "github.com/FusionFoundation/efsn/v4/common/hexutil" 28 "github.com/FusionFoundation/efsn/v4/consensus" 29 "github.com/FusionFoundation/efsn/v4/core" 30 "github.com/FusionFoundation/efsn/v4/core/state" 31 "github.com/FusionFoundation/efsn/v4/core/types" 32 "github.com/FusionFoundation/efsn/v4/eth/downloader" 33 "github.com/FusionFoundation/efsn/v4/event" 34 "github.com/FusionFoundation/efsn/v4/log" 35 "github.com/FusionFoundation/efsn/v4/params" 36 ) 37 38 // Backend wraps all methods required for mining. 39 type Backend interface { 40 BlockChain() *core.BlockChain 41 TxPool() *core.TxPool 42 } 43 44 // Config is the configuration parameters of mining. 45 type Config struct { 46 Etherbase common.Address `toml:",omitempty"` // Public address for block mining rewards (default = first account) 47 Notify []string `toml:",omitempty"` // HTTP URL list to be notified of new work packages (only useful in ethash). 48 ExtraData hexutil.Bytes `toml:",omitempty"` // Block extra data set by the miner 49 GasFloor uint64 // Target gas floor for mined blocks. 50 GasCeil uint64 // Target gas ceiling for mined blocks. 51 GasPrice *big.Int // Minimum gas price for mining a transaction 52 Recommit time.Duration // The time interval for miner to re-create mining work. 53 Noverify bool // Disable remote mining solution verification(only useful in ethash). 54 } 55 56 // Miner creates blocks and searches for proof-of-work values. 57 type Miner struct { 58 mux *event.TypeMux 59 worker *worker 60 coinbase common.Address 61 eth Backend 62 engine consensus.Engine 63 exitCh chan struct{} 64 startCh chan common.Address 65 stopCh chan struct{} 66 67 wg sync.WaitGroup 68 } 69 70 func New(eth Backend, config *Config, chainConfig *params.ChainConfig, mux *event.TypeMux, engine consensus.Engine, isLocalBlock func(block *types.Block) bool) *Miner { 71 miner := &Miner{ 72 eth: eth, 73 mux: mux, 74 engine: engine, 75 exitCh: make(chan struct{}), 76 startCh: make(chan common.Address), 77 stopCh: make(chan struct{}), 78 worker: newWorker(config, chainConfig, engine, eth, mux, isLocalBlock), 79 } 80 miner.wg.Add(1) 81 go miner.update() 82 return miner 83 } 84 85 // update keeps track of the downloader events. Please be aware that this is a one shot type of update loop. 86 // It's entered once and as soon as `Done` or `Failed` has been broadcasted the events are unregistered and 87 // the loop is exited. This to prevent a major security vuln where external parties can DOS you with blocks 88 // and halt your mining operation for as long as the DOS continues. 89 func (miner *Miner) update() { 90 defer miner.wg.Done() 91 92 events := miner.mux.Subscribe(downloader.StartEvent{}, downloader.DoneEvent{}, downloader.FailedEvent{}) 93 defer func() { 94 if !events.Closed() { 95 events.Unsubscribe() 96 } 97 }() 98 99 shouldStart := false 100 canStart := true 101 dlEventCh := events.Chan() 102 for { 103 select { 104 case ev := <-dlEventCh: 105 if ev == nil { 106 // Unsubscription done, stop listening 107 dlEventCh = nil 108 continue 109 } 110 switch ev.Data.(type) { 111 case downloader.StartEvent: 112 wasMining := miner.Mining() 113 miner.worker.stop() 114 canStart = false 115 if wasMining { 116 // Resume mining after sync was finished 117 shouldStart = true 118 log.Info("Mining aborted due to sync") 119 } 120 case downloader.FailedEvent: 121 canStart = true 122 if shouldStart { 123 miner.SetEtherbase(miner.coinbase) 124 miner.worker.start() 125 } 126 case downloader.DoneEvent: 127 canStart = true 128 if shouldStart { 129 miner.SetEtherbase(miner.coinbase) 130 miner.worker.start() 131 } 132 // Stop reacting to downloader events 133 events.Unsubscribe() 134 } 135 case addr := <-miner.startCh: 136 miner.SetEtherbase(addr) 137 if canStart { 138 miner.worker.start() 139 } 140 shouldStart = true 141 case <-miner.stopCh: 142 shouldStart = false 143 miner.worker.stop() 144 case <-miner.exitCh: 145 miner.worker.close() 146 return 147 } 148 } 149 } 150 151 func (miner *Miner) Start(coinbase common.Address) { 152 miner.startCh <- coinbase 153 } 154 155 func (miner *Miner) Stop() { 156 miner.stopCh <- struct{}{} 157 } 158 159 func (miner *Miner) Close() { 160 close(miner.exitCh) 161 miner.wg.Wait() 162 } 163 164 func (miner *Miner) Mining() bool { 165 return miner.worker.isRunning() 166 } 167 168 func (miner *Miner) Hashrate() uint64 { 169 if pow, ok := miner.engine.(consensus.PoW); ok { 170 return uint64(pow.Hashrate()) 171 } 172 return 0 173 } 174 175 func (miner *Miner) SetExtra(extra []byte) error { 176 if uint64(len(extra)) > params.MaximumExtraDataSize { 177 return fmt.Errorf("extra exceeds max length. %d > %v", len(extra), params.MaximumExtraDataSize) 178 } 179 miner.worker.setExtra(extra) 180 return nil 181 } 182 183 // SetRecommitInterval sets the interval for sealing work resubmitting. 184 func (miner *Miner) SetRecommitInterval(interval time.Duration) { 185 miner.worker.setRecommitInterval(interval) 186 } 187 188 // Pending returns the currently pending block and associated state. 189 func (miner *Miner) Pending() (*types.Block, *state.StateDB) { 190 return miner.worker.pending() 191 } 192 193 // PendingBlock returns the currently pending block. 194 // 195 // Note, to access both the pending block and the pending state 196 // simultaneously, please use Pending(), as the pending state can 197 // change between multiple method calls 198 func (miner *Miner) PendingBlock() *types.Block { 199 return miner.worker.pendingBlock() 200 } 201 202 // PendingBlockAndReceipts returns the currently pending block and corresponding receipts. 203 func (miner *Miner) PendingBlockAndReceipts() (*types.Block, types.Receipts) { 204 return miner.worker.pendingBlockAndReceipts() 205 } 206 207 func (miner *Miner) SetEtherbase(addr common.Address) { 208 miner.coinbase = addr 209 miner.worker.setEtherbase(addr) 210 } 211 212 // SubscribePendingLogs starts delivering logs from pending transactions 213 // to the given channel. 214 func (miner *Miner) SubscribePendingLogs(ch chan<- []*types.Log) event.Subscription { 215 return miner.worker.pendingLogsFeed.Subscribe(ch) 216 }