github.com/truechain/truechain-fpow@v1.8.11/miner/worker.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 "bytes" 21 "fmt" 22 "math/big" 23 "sync" 24 "sync/atomic" 25 "time" 26 27 "github.com/ethereum/go-ethereum/common" 28 "github.com/ethereum/go-ethereum/consensus" 29 "github.com/ethereum/go-ethereum/consensus/misc" 30 "github.com/ethereum/go-ethereum/core" 31 "github.com/ethereum/go-ethereum/core/state" 32 "github.com/ethereum/go-ethereum/core/types" 33 "github.com/ethereum/go-ethereum/core/vm" 34 "github.com/ethereum/go-ethereum/ethdb" 35 "github.com/ethereum/go-ethereum/event" 36 "github.com/ethereum/go-ethereum/log" 37 "github.com/ethereum/go-ethereum/params" 38 "gopkg.in/fatih/set.v0" 39 ) 40 41 const ( 42 resultQueueSize = 10 43 miningLogAtDepth = 5 44 45 // txChanSize is the size of channel listening to NewTxsEvent. 46 // The number is referenced from the size of tx pool. 47 txChanSize = 4096 48 // chainHeadChanSize is the size of channel listening to ChainHeadEvent. 49 chainHeadChanSize = 10 50 // chainSideChanSize is the size of channel listening to ChainSideEvent. 51 chainSideChanSize = 10 52 ) 53 54 // Agent can register themself with the worker 55 type Agent interface { 56 Work() chan<- *Work 57 SetReturnCh(chan<- *Result) 58 Stop() 59 Start() 60 GetHashRate() int64 61 } 62 63 // Work is the workers current environment and holds 64 // all of the current state information 65 type Work struct { 66 config *params.ChainConfig 67 signer types.Signer 68 69 state *state.StateDB // apply state changes here 70 ancestors *set.Set // ancestor set (used for checking uncle parent validity) 71 family *set.Set // family set (used for checking uncle invalidity) 72 uncles *set.Set // uncle set 73 tcount int // tx count in cycle 74 gasPool *core.GasPool // available gas used to pack transactions 75 76 Block *types.Block // the new block 77 78 header *types.Header 79 txs []*types.Transaction 80 receipts []*types.Receipt 81 82 createdAt time.Time 83 } 84 85 type Result struct { 86 Work *Work 87 Block *types.Block 88 } 89 90 // worker is the main object which takes care of applying messages to the new state 91 type worker struct { 92 config *params.ChainConfig 93 engine consensus.Engine 94 95 mu sync.Mutex 96 97 // update loop 98 mux *event.TypeMux 99 txsCh chan core.NewTxsEvent 100 txsSub event.Subscription 101 chainHeadCh chan core.ChainHeadEvent 102 chainHeadSub event.Subscription 103 chainSideCh chan core.ChainSideEvent 104 chainSideSub event.Subscription 105 wg sync.WaitGroup 106 107 agents map[Agent]struct{} 108 recv chan *Result 109 110 eth Backend 111 chain *core.BlockChain 112 proc core.Validator 113 chainDb ethdb.Database 114 115 coinbase common.Address 116 extra []byte 117 118 currentMu sync.Mutex 119 current *Work 120 121 snapshotMu sync.RWMutex 122 snapshotBlock *types.Block 123 snapshotState *state.StateDB 124 125 uncleMu sync.Mutex 126 possibleUncles map[common.Hash]*types.Block 127 128 unconfirmed *unconfirmedBlocks // set of locally mined blocks pending canonicalness confirmations 129 130 // atomic status counters 131 mining int32 132 atWork int32 133 } 134 135 func newWorker(config *params.ChainConfig, engine consensus.Engine, coinbase common.Address, eth Backend, mux *event.TypeMux) *worker { 136 worker := &worker{ 137 config: config, 138 engine: engine, 139 eth: eth, 140 mux: mux, 141 txsCh: make(chan core.NewTxsEvent, txChanSize), 142 chainHeadCh: make(chan core.ChainHeadEvent, chainHeadChanSize), 143 chainSideCh: make(chan core.ChainSideEvent, chainSideChanSize), 144 chainDb: eth.ChainDb(), 145 recv: make(chan *Result, resultQueueSize), 146 chain: eth.BlockChain(), 147 proc: eth.BlockChain().Validator(), 148 possibleUncles: make(map[common.Hash]*types.Block), 149 coinbase: coinbase, 150 agents: make(map[Agent]struct{}), 151 unconfirmed: newUnconfirmedBlocks(eth.BlockChain(), miningLogAtDepth), 152 } 153 // Subscribe NewTxsEvent for tx pool 154 worker.txsSub = eth.TxPool().SubscribeNewTxsEvent(worker.txsCh) 155 // Subscribe events for blockchain 156 worker.chainHeadSub = eth.BlockChain().SubscribeChainHeadEvent(worker.chainHeadCh) 157 worker.chainSideSub = eth.BlockChain().SubscribeChainSideEvent(worker.chainSideCh) 158 go worker.update() 159 160 go worker.wait() 161 worker.commitNewWork() 162 163 return worker 164 } 165 166 func (self *worker) setEtherbase(addr common.Address) { 167 self.mu.Lock() 168 defer self.mu.Unlock() 169 self.coinbase = addr 170 } 171 172 func (self *worker) setExtra(extra []byte) { 173 self.mu.Lock() 174 defer self.mu.Unlock() 175 self.extra = extra 176 } 177 178 func (self *worker) pending() (*types.Block, *state.StateDB) { 179 if atomic.LoadInt32(&self.mining) == 0 { 180 // return a snapshot to avoid contention on currentMu mutex 181 self.snapshotMu.RLock() 182 defer self.snapshotMu.RUnlock() 183 return self.snapshotBlock, self.snapshotState.Copy() 184 } 185 186 self.currentMu.Lock() 187 defer self.currentMu.Unlock() 188 return self.current.Block, self.current.state.Copy() 189 } 190 191 func (self *worker) pendingBlock() *types.Block { 192 if atomic.LoadInt32(&self.mining) == 0 { 193 // return a snapshot to avoid contention on currentMu mutex 194 self.snapshotMu.RLock() 195 defer self.snapshotMu.RUnlock() 196 return self.snapshotBlock 197 } 198 199 self.currentMu.Lock() 200 defer self.currentMu.Unlock() 201 return self.current.Block 202 } 203 204 func (self *worker) start() { 205 self.mu.Lock() 206 defer self.mu.Unlock() 207 208 atomic.StoreInt32(&self.mining, 1) 209 210 // spin up agents 211 for agent := range self.agents { 212 agent.Start() 213 } 214 } 215 216 func (self *worker) stop() { 217 self.wg.Wait() 218 219 self.mu.Lock() 220 defer self.mu.Unlock() 221 if atomic.LoadInt32(&self.mining) == 1 { 222 for agent := range self.agents { 223 agent.Stop() 224 } 225 } 226 atomic.StoreInt32(&self.mining, 0) 227 atomic.StoreInt32(&self.atWork, 0) 228 } 229 230 func (self *worker) register(agent Agent) { 231 self.mu.Lock() 232 defer self.mu.Unlock() 233 self.agents[agent] = struct{}{} 234 agent.SetReturnCh(self.recv) 235 } 236 237 func (self *worker) unregister(agent Agent) { 238 self.mu.Lock() 239 defer self.mu.Unlock() 240 delete(self.agents, agent) 241 agent.Stop() 242 } 243 244 func (self *worker) update() { 245 defer self.txsSub.Unsubscribe() 246 defer self.chainHeadSub.Unsubscribe() 247 defer self.chainSideSub.Unsubscribe() 248 249 for { 250 // A real event arrived, process interesting content 251 select { 252 // Handle ChainHeadEvent 253 case <-self.chainHeadCh: 254 self.commitNewWork() 255 256 // Handle ChainSideEvent 257 case ev := <-self.chainSideCh: 258 self.uncleMu.Lock() 259 self.possibleUncles[ev.Block.Hash()] = ev.Block 260 self.uncleMu.Unlock() 261 262 // Handle NewTxsEvent 263 case ev := <-self.txsCh: 264 // Apply transactions to the pending state if we're not mining. 265 // 266 // Note all transactions received may not be continuous with transactions 267 // already included in the current mining block. These transactions will 268 // be automatically eliminated. 269 if atomic.LoadInt32(&self.mining) == 0 { 270 self.currentMu.Lock() 271 txs := make(map[common.Address]types.Transactions) 272 for _, tx := range ev.Txs { 273 acc, _ := types.Sender(self.current.signer, tx) 274 txs[acc] = append(txs[acc], tx) 275 } 276 txset := types.NewTransactionsByPriceAndNonce(self.current.signer, txs) 277 self.current.commitTransactions(self.mux, txset, self.chain, self.coinbase) 278 self.updateSnapshot() 279 self.currentMu.Unlock() 280 } else { 281 // If we're mining, but nothing is being processed, wake on new transactions 282 if self.config.Clique != nil && self.config.Clique.Period == 0 { 283 self.commitNewWork() 284 } 285 } 286 287 // System stopped 288 case <-self.txsSub.Err(): 289 return 290 case <-self.chainHeadSub.Err(): 291 return 292 case <-self.chainSideSub.Err(): 293 return 294 } 295 } 296 } 297 298 func (self *worker) wait() { 299 for { 300 for result := range self.recv { 301 atomic.AddInt32(&self.atWork, -1) 302 303 if result == nil { 304 continue 305 } 306 block := result.Block 307 work := result.Work 308 309 // Update the block hash in all logs since it is now available and not when the 310 // receipt/log of individual transactions were created. 311 for _, r := range work.receipts { 312 for _, l := range r.Logs { 313 l.BlockHash = block.Hash() 314 } 315 } 316 for _, log := range work.state.Logs() { 317 log.BlockHash = block.Hash() 318 } 319 stat, err := self.chain.WriteBlockWithState(block, work.receipts, work.state) 320 if err != nil { 321 log.Error("Failed writing block to chain", "err", err) 322 continue 323 } 324 // Broadcast the block and announce chain insertion event 325 self.mux.Post(core.NewMinedBlockEvent{Block: block}) 326 var ( 327 events []interface{} 328 logs = work.state.Logs() 329 ) 330 events = append(events, core.ChainEvent{Block: block, Hash: block.Hash(), Logs: logs}) 331 if stat == core.CanonStatTy { 332 events = append(events, core.ChainHeadEvent{Block: block}) 333 } 334 self.chain.PostChainEvents(events, logs) 335 336 // Insert the block into the set of pending ones to wait for confirmations 337 self.unconfirmed.Insert(block.NumberU64(), block.Hash()) 338 } 339 } 340 } 341 342 // push sends a new work task to currently live miner agents. 343 func (self *worker) push(work *Work) { 344 if atomic.LoadInt32(&self.mining) != 1 { 345 return 346 } 347 for agent := range self.agents { 348 atomic.AddInt32(&self.atWork, 1) 349 if ch := agent.Work(); ch != nil { 350 ch <- work 351 } 352 } 353 } 354 355 // makeCurrent creates a new environment for the current cycle. 356 func (self *worker) makeCurrent(parent *types.Block, header *types.Header) error { 357 state, err := self.chain.StateAt(parent.Root()) 358 if err != nil { 359 return err 360 } 361 work := &Work{ 362 config: self.config, 363 signer: types.NewEIP155Signer(self.config.ChainID), 364 state: state, 365 ancestors: set.New(), 366 family: set.New(), 367 uncles: set.New(), 368 header: header, 369 createdAt: time.Now(), 370 } 371 372 // when 08 is processed ancestors contain 07 (quick block) 373 for _, ancestor := range self.chain.GetBlocksFromHash(parent.Hash(), 7) { 374 for _, uncle := range ancestor.Uncles() { 375 work.family.Add(uncle.Hash()) 376 } 377 work.family.Add(ancestor.Hash()) 378 work.ancestors.Add(ancestor.Hash()) 379 } 380 381 // Keep track of transactions which return errors so they can be removed 382 work.tcount = 0 383 self.current = work 384 return nil 385 } 386 387 func (self *worker) commitNewWork() { 388 self.mu.Lock() 389 defer self.mu.Unlock() 390 self.uncleMu.Lock() 391 defer self.uncleMu.Unlock() 392 self.currentMu.Lock() 393 defer self.currentMu.Unlock() 394 395 tstart := time.Now() 396 parent := self.chain.CurrentBlock() 397 398 tstamp := tstart.Unix() 399 if parent.Time().Cmp(new(big.Int).SetInt64(tstamp)) >= 0 { 400 tstamp = parent.Time().Int64() + 1 401 } 402 // this will ensure we're not going off too far in the future 403 if now := time.Now().Unix(); tstamp > now+1 { 404 wait := time.Duration(tstamp-now) * time.Second 405 log.Info("Mining too far in the future", "wait", common.PrettyDuration(wait)) 406 time.Sleep(wait) 407 } 408 409 num := parent.Number() 410 header := &types.Header{ 411 ParentHash: parent.Hash(), 412 Number: num.Add(num, common.Big1), 413 GasLimit: core.CalcGasLimit(parent), 414 Extra: self.extra, 415 Time: big.NewInt(tstamp), 416 } 417 // Only set the coinbase if we are mining (avoid spurious block rewards) 418 if atomic.LoadInt32(&self.mining) == 1 { 419 header.Coinbase = self.coinbase 420 } 421 if err := self.engine.Prepare(self.chain, header); err != nil { 422 log.Error("Failed to prepare header for mining", "err", err) 423 return 424 } 425 // If we are care about TheDAO hard-fork check whether to override the extra-data or not 426 if daoBlock := self.config.DAOForkBlock; daoBlock != nil { 427 // Check whether the block is among the fork extra-override range 428 limit := new(big.Int).Add(daoBlock, params.DAOForkExtraRange) 429 if header.Number.Cmp(daoBlock) >= 0 && header.Number.Cmp(limit) < 0 { 430 // Depending whether we support or oppose the fork, override differently 431 if self.config.DAOForkSupport { 432 header.Extra = common.CopyBytes(params.DAOForkBlockExtra) 433 } else if bytes.Equal(header.Extra, params.DAOForkBlockExtra) { 434 header.Extra = []byte{} // If miner opposes, don't let it use the reserved extra-data 435 } 436 } 437 } 438 // Could potentially happen if starting to mine in an odd state. 439 err := self.makeCurrent(parent, header) 440 if err != nil { 441 log.Error("Failed to create mining context", "err", err) 442 return 443 } 444 // Create the current work task and check any fork transitions needed 445 work := self.current 446 if self.config.DAOForkSupport && self.config.DAOForkBlock != nil && self.config.DAOForkBlock.Cmp(header.Number) == 0 { 447 misc.ApplyDAOHardFork(work.state) 448 } 449 pending, err := self.eth.TxPool().Pending() 450 if err != nil { 451 log.Error("Failed to fetch pending transactions", "err", err) 452 return 453 } 454 txs := types.NewTransactionsByPriceAndNonce(self.current.signer, pending) 455 work.commitTransactions(self.mux, txs, self.chain, self.coinbase) 456 457 // compute uncles for the new block. 458 var ( 459 uncles []*types.Header 460 badUncles []common.Hash 461 ) 462 for hash, uncle := range self.possibleUncles { 463 if len(uncles) == 2 { 464 break 465 } 466 if err := self.commitUncle(work, uncle.Header()); err != nil { 467 log.Trace("Bad uncle found and will be removed", "hash", hash) 468 log.Trace(fmt.Sprint(uncle)) 469 470 badUncles = append(badUncles, hash) 471 } else { 472 log.Debug("Committing new uncle to block", "hash", hash) 473 uncles = append(uncles, uncle.Header()) 474 } 475 } 476 for _, hash := range badUncles { 477 delete(self.possibleUncles, hash) 478 } 479 // Create the new block to seal with the consensus engine 480 if work.Block, err = self.engine.Finalize(self.chain, header, work.state, work.txs, uncles, work.receipts); err != nil { 481 log.Error("Failed to finalize block for sealing", "err", err) 482 return 483 } 484 // We only care about logging if we're actually mining. 485 if atomic.LoadInt32(&self.mining) == 1 { 486 log.Info("Commit new mining work", "number", work.Block.Number(), "txs", work.tcount, "uncles", len(uncles), "elapsed", common.PrettyDuration(time.Since(tstart))) 487 self.unconfirmed.Shift(work.Block.NumberU64() - 1) 488 } 489 self.push(work) 490 self.updateSnapshot() 491 } 492 493 func (self *worker) commitUncle(work *Work, uncle *types.Header) error { 494 hash := uncle.Hash() 495 if work.uncles.Has(hash) { 496 return fmt.Errorf("uncle not unique") 497 } 498 if !work.ancestors.Has(uncle.ParentHash) { 499 return fmt.Errorf("uncle's parent unknown (%x)", uncle.ParentHash[0:4]) 500 } 501 if work.family.Has(hash) { 502 return fmt.Errorf("uncle already in family (%x)", hash) 503 } 504 work.uncles.Add(uncle.Hash()) 505 return nil 506 } 507 508 func (self *worker) updateSnapshot() { 509 self.snapshotMu.Lock() 510 defer self.snapshotMu.Unlock() 511 512 self.snapshotBlock = types.NewBlock( 513 self.current.header, 514 self.current.txs, 515 nil, 516 self.current.receipts, 517 ) 518 self.snapshotState = self.current.state.Copy() 519 } 520 521 func (env *Work) commitTransactions(mux *event.TypeMux, txs *types.TransactionsByPriceAndNonce, bc *core.BlockChain, coinbase common.Address) { 522 if env.gasPool == nil { 523 env.gasPool = new(core.GasPool).AddGas(env.header.GasLimit) 524 } 525 526 var coalescedLogs []*types.Log 527 528 for { 529 // If we don't have enough gas for any further transactions then we're done 530 if env.gasPool.Gas() < params.TxGas { 531 log.Trace("Not enough gas for further transactions", "have", env.gasPool, "want", params.TxGas) 532 break 533 } 534 // Retrieve the next transaction and abort if all done 535 tx := txs.Peek() 536 if tx == nil { 537 break 538 } 539 // Error may be ignored here. The error has already been checked 540 // during transaction acceptance is the transaction pool. 541 // 542 // We use the eip155 signer regardless of the current hf. 543 from, _ := types.Sender(env.signer, tx) 544 // Check whether the tx is replay protected. If we're not in the EIP155 hf 545 // phase, start ignoring the sender until we do. 546 if tx.Protected() && !env.config.IsEIP155(env.header.Number) { 547 log.Trace("Ignoring reply protected transaction", "hash", tx.Hash(), "eip155", env.config.EIP155Block) 548 549 txs.Pop() 550 continue 551 } 552 // Start executing the transaction 553 env.state.Prepare(tx.Hash(), common.Hash{}, env.tcount) 554 555 err, logs := env.commitTransaction(tx, bc, coinbase, env.gasPool) 556 switch err { 557 case core.ErrGasLimitReached: 558 // Pop the current out-of-gas transaction without shifting in the next from the account 559 log.Trace("Gas limit exceeded for current block", "sender", from) 560 txs.Pop() 561 562 case core.ErrNonceTooLow: 563 // New head notification data race between the transaction pool and miner, shift 564 log.Trace("Skipping transaction with low nonce", "sender", from, "nonce", tx.Nonce()) 565 txs.Shift() 566 567 case core.ErrNonceTooHigh: 568 // Reorg notification data race between the transaction pool and miner, skip account = 569 log.Trace("Skipping account with hight nonce", "sender", from, "nonce", tx.Nonce()) 570 txs.Pop() 571 572 case nil: 573 // Everything ok, collect the logs and shift in the next transaction from the same account 574 coalescedLogs = append(coalescedLogs, logs...) 575 env.tcount++ 576 txs.Shift() 577 578 default: 579 // Strange error, discard the transaction and get the next in line (note, the 580 // nonce-too-high clause will prevent us from executing in vain). 581 log.Debug("Transaction failed, account skipped", "hash", tx.Hash(), "err", err) 582 txs.Shift() 583 } 584 } 585 586 if len(coalescedLogs) > 0 || env.tcount > 0 { 587 // make a copy, the state caches the logs and these logs get "upgraded" from pending to mined 588 // logs by filling in the block hash when the block was mined by the local miner. This can 589 // cause a race condition if a log was "upgraded" before the PendingLogsEvent is processed. 590 cpy := make([]*types.Log, len(coalescedLogs)) 591 for i, l := range coalescedLogs { 592 cpy[i] = new(types.Log) 593 *cpy[i] = *l 594 } 595 go func(logs []*types.Log, tcount int) { 596 if len(logs) > 0 { 597 mux.Post(core.PendingLogsEvent{Logs: logs}) 598 } 599 if tcount > 0 { 600 mux.Post(core.PendingStateEvent{}) 601 } 602 }(cpy, env.tcount) 603 } 604 } 605 606 func (env *Work) commitTransaction(tx *types.Transaction, bc *core.BlockChain, coinbase common.Address, gp *core.GasPool) (error, []*types.Log) { 607 snap := env.state.Snapshot() 608 609 receipt, _, err := core.ApplyTransaction(env.config, bc, &coinbase, gp, env.state, env.header, tx, &env.header.GasUsed, vm.Config{}) 610 if err != nil { 611 env.state.RevertToSnapshot(snap) 612 return err, nil 613 } 614 env.txs = append(env.txs, tx) 615 env.receipts = append(env.receipts, receipt) 616 617 return nil, receipt.Logs 618 }