github.com/DxChainNetwork/dxc@v0.8.1-0.20220824085222-1162e304b6e7/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 "errors" 22 "math/big" 23 "strconv" 24 "sync" 25 "sync/atomic" 26 "time" 27 28 "github.com/DxChainNetwork/dxc/common" 29 "github.com/DxChainNetwork/dxc/consensus" 30 "github.com/DxChainNetwork/dxc/consensus/misc" 31 "github.com/DxChainNetwork/dxc/core" 32 "github.com/DxChainNetwork/dxc/core/state" 33 "github.com/DxChainNetwork/dxc/core/types" 34 "github.com/DxChainNetwork/dxc/event" 35 "github.com/DxChainNetwork/dxc/log" 36 "github.com/DxChainNetwork/dxc/params" 37 "github.com/DxChainNetwork/dxc/trie" 38 mapset "github.com/deckarep/golang-set" 39 ) 40 41 const ( 42 // resultQueueSize is the size of channel listening to sealing result. 43 resultQueueSize = 10 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 49 // chainHeadChanSize is the size of channel listening to ChainHeadEvent. 50 chainHeadChanSize = 10 51 52 // chainSideChanSize is the size of channel listening to ChainSideEvent. 53 chainSideChanSize = 10 54 55 // resubmitAdjustChanSize is the size of resubmitting interval adjustment channel. 56 resubmitAdjustChanSize = 10 57 58 // miningLogAtDepth is the number of confirmations before logging successful mining. 59 miningLogAtDepth = 7 60 61 // minRecommitInterval is the minimal time interval to recreate the mining block with 62 // any newly arrived transactions. 63 minRecommitInterval = 1 * time.Second 64 65 // maxRecommitInterval is the maximum time interval to recreate the mining block with 66 // any newly arrived transactions. 67 maxRecommitInterval = 15 * time.Second 68 69 // intervalAdjustRatio is the impact a single interval adjustment has on sealing work 70 // resubmitting interval. 71 intervalAdjustRatio = 0.1 72 73 // intervalAdjustBias is applied during the new resubmit interval calculation in favor of 74 // increasing upper limit or decreasing lower limit so that the limit can be reachable. 75 intervalAdjustBias = 200 * 1000.0 * 1000.0 76 77 // staleThreshold is the maximum depth of the acceptable stale block. 78 staleThreshold = 7 79 ) 80 81 // environment is the worker's current environment and holds all of the current state information. 82 type environment struct { 83 signer types.Signer 84 85 state *state.StateDB // apply state changes here 86 ancestors mapset.Set // ancestor set (used for checking uncle parent validity) 87 family mapset.Set // family set (used for checking uncle invalidity) 88 uncles mapset.Set // uncle set 89 tcount int // tx count in cycle 90 gasPool *core.GasPool // available gas used to pack transactions 91 92 header *types.Header 93 txs []*types.Transaction 94 receipts []*types.Receipt 95 96 extraValidator types.EvmExtraValidator 97 } 98 99 // task contains all information for consensus engine sealing and result submitting. 100 type task struct { 101 receipts []*types.Receipt 102 state *state.StateDB 103 block *types.Block 104 createdAt time.Time 105 } 106 107 const ( 108 commitInterruptNone int32 = iota 109 commitInterruptNewHead 110 commitInterruptResubmit 111 ) 112 113 // newWorkReq represents a request for new sealing work submitting with relative interrupt notifier. 114 type newWorkReq struct { 115 interrupt *int32 116 noempty bool 117 timestamp int64 118 } 119 120 // intervalAdjust represents a resubmitting interval adjustment. 121 type intervalAdjust struct { 122 ratio float64 123 inc bool 124 } 125 126 // worker is the main object which takes care of submitting new work to consensus engine 127 // and gathering the sealing result. 128 type worker struct { 129 config *Config 130 chainConfig *params.ChainConfig 131 engine consensus.Engine 132 eth Backend 133 chain *core.BlockChain 134 135 // Is the engine a PoSA engine? 136 posa consensus.PoSA 137 isPoSA bool 138 139 // Feeds 140 pendingLogsFeed event.Feed 141 142 // Subscriptions 143 mux *event.TypeMux 144 txsCh chan core.NewTxsEvent 145 txsSub event.Subscription 146 chainHeadCh chan core.ChainHeadEvent 147 chainHeadSub event.Subscription 148 chainSideCh chan core.ChainSideEvent 149 chainSideSub event.Subscription 150 151 // Channels 152 newWorkCh chan *newWorkReq 153 taskCh chan *task 154 resultCh chan *types.Block 155 startCh chan struct{} 156 exitCh chan struct{} 157 resubmitIntervalCh chan time.Duration 158 resubmitAdjustCh chan *intervalAdjust 159 160 current *environment // An environment for current running cycle. 161 localUncles map[common.Hash]*types.Block // A set of side blocks generated locally as the possible uncle blocks. 162 remoteUncles map[common.Hash]*types.Block // A set of side blocks as the possible uncle blocks. 163 unconfirmed *unconfirmedBlocks // A set of locally mined blocks pending canonicalness confirmations. 164 165 mu sync.RWMutex // The lock used to protect the coinbase and extra fields 166 coinbase common.Address 167 extra []byte 168 169 pendingMu sync.RWMutex 170 pendingTasks map[common.Hash]*task 171 172 snapshotMu sync.RWMutex // The lock used to protect the snapshots below 173 snapshotBlock *types.Block 174 snapshotReceipts types.Receipts 175 snapshotState *state.StateDB 176 177 // atomic status counters 178 running int32 // The indicator whether the consensus engine is running or not. 179 newTxs int32 // New arrival transaction count since last sealing work submitting. 180 181 // noempty is the flag used to control whether the feature of pre-seal empty 182 // block is enabled. The default value is false(pre-seal is enabled by default). 183 // But in some special scenario the consensus engine will seal blocks instantaneously, 184 // in this case this feature will add all empty blocks into canonical chain 185 // non-stop and no real transaction will be included. 186 noempty uint32 187 188 // External functions 189 isLocalBlock func(block *types.Block) bool // Function used to determine whether the specified block is mined by local miner. 190 191 // Test hooks 192 newTaskHook func(*task) // Method to call upon receiving a new sealing task. 193 skipSealHook func(*task) bool // Method to decide whether skipping the sealing. 194 fullTaskHook func() // Method to call before pushing the full sealing task. 195 resubmitHook func(time.Duration, time.Duration) // Method to call upon updating resubmitting interval. 196 } 197 198 func newWorker(config *Config, chainConfig *params.ChainConfig, engine consensus.Engine, eth Backend, mux *event.TypeMux, isLocalBlock func(*types.Block) bool, init bool) *worker { 199 posa, isPoSA := engine.(consensus.PoSA) 200 worker := &worker{ 201 config: config, 202 chainConfig: chainConfig, 203 engine: engine, 204 isPoSA: isPoSA, 205 posa: posa, 206 eth: eth, 207 mux: mux, 208 chain: eth.BlockChain(), 209 isLocalBlock: isLocalBlock, 210 localUncles: make(map[common.Hash]*types.Block), 211 remoteUncles: make(map[common.Hash]*types.Block), 212 unconfirmed: newUnconfirmedBlocks(eth.BlockChain(), miningLogAtDepth), 213 pendingTasks: make(map[common.Hash]*task), 214 txsCh: make(chan core.NewTxsEvent, txChanSize), 215 chainHeadCh: make(chan core.ChainHeadEvent, chainHeadChanSize), 216 chainSideCh: make(chan core.ChainSideEvent, chainSideChanSize), 217 newWorkCh: make(chan *newWorkReq), 218 taskCh: make(chan *task), 219 resultCh: make(chan *types.Block, resultQueueSize), 220 exitCh: make(chan struct{}), 221 startCh: make(chan struct{}, 1), 222 resubmitIntervalCh: make(chan time.Duration), 223 resubmitAdjustCh: make(chan *intervalAdjust, resubmitAdjustChanSize), 224 } 225 // Subscribe NewTxsEvent for tx pool 226 worker.txsSub = eth.TxPool().SubscribeNewTxsEvent(worker.txsCh) 227 // Subscribe events for blockchain 228 worker.chainHeadSub = eth.BlockChain().SubscribeChainHeadEvent(worker.chainHeadCh) 229 worker.chainSideSub = eth.BlockChain().SubscribeChainSideEvent(worker.chainSideCh) 230 231 // Sanitize recommit interval if the user-specified one is too short. 232 recommit := worker.config.Recommit 233 if recommit < minRecommitInterval { 234 log.Warn("Sanitizing miner recommit interval", "provided", recommit, "updated", minRecommitInterval) 235 recommit = minRecommitInterval 236 } 237 238 go worker.mainLoop() 239 go worker.newWorkLoop(recommit) 240 go worker.resultLoop() 241 go worker.taskLoop() 242 243 // Submit first work to initialize pending state. 244 if init { 245 worker.startCh <- struct{}{} 246 } 247 return worker 248 } 249 250 // setEtherbase sets the etherbase used to initialize the block coinbase field. 251 func (w *worker) setEtherbase(addr common.Address) { 252 w.mu.Lock() 253 defer w.mu.Unlock() 254 w.coinbase = addr 255 } 256 257 func (w *worker) setGasCeil(ceil uint64) { 258 w.mu.Lock() 259 defer w.mu.Unlock() 260 w.config.GasCeil = ceil 261 } 262 263 // setExtra sets the content used to initialize the block extra field. 264 func (w *worker) setExtra(extra []byte) { 265 w.mu.Lock() 266 defer w.mu.Unlock() 267 w.extra = extra 268 } 269 270 // setRecommitInterval updates the interval for miner sealing work recommitting. 271 func (w *worker) setRecommitInterval(interval time.Duration) { 272 w.resubmitIntervalCh <- interval 273 } 274 275 // disablePreseal disables pre-sealing mining feature 276 func (w *worker) disablePreseal() { 277 atomic.StoreUint32(&w.noempty, 1) 278 } 279 280 // enablePreseal enables pre-sealing mining feature 281 func (w *worker) enablePreseal() { 282 atomic.StoreUint32(&w.noempty, 0) 283 } 284 285 // pending returns the pending state and corresponding block. 286 func (w *worker) pending() (*types.Block, *state.StateDB) { 287 // return a snapshot to avoid contention on currentMu mutex 288 w.snapshotMu.RLock() 289 defer w.snapshotMu.RUnlock() 290 if w.snapshotState == nil { 291 return nil, nil 292 } 293 return w.snapshotBlock, w.snapshotState.Copy() 294 } 295 296 // pendingBlock returns pending block. 297 func (w *worker) pendingBlock() *types.Block { 298 // return a snapshot to avoid contention on currentMu mutex 299 w.snapshotMu.RLock() 300 defer w.snapshotMu.RUnlock() 301 return w.snapshotBlock 302 } 303 304 // pendingBlockAndReceipts returns pending block and corresponding receipts. 305 func (w *worker) pendingBlockAndReceipts() (*types.Block, types.Receipts) { 306 // return a snapshot to avoid contention on currentMu mutex 307 w.snapshotMu.RLock() 308 defer w.snapshotMu.RUnlock() 309 return w.snapshotBlock, w.snapshotReceipts 310 } 311 312 // start sets the running status as 1 and triggers new work submitting. 313 func (w *worker) start() { 314 atomic.StoreInt32(&w.running, 1) 315 w.startCh <- struct{}{} 316 } 317 318 // stop sets the running status as 0. 319 func (w *worker) stop() { 320 atomic.StoreInt32(&w.running, 0) 321 } 322 323 // isRunning returns an indicator whether worker is running or not. 324 func (w *worker) isRunning() bool { 325 return atomic.LoadInt32(&w.running) == 1 326 } 327 328 // close terminates all background threads maintained by the worker. 329 // Note the worker does not support being closed multiple times. 330 func (w *worker) close() { 331 if w.current != nil && w.current.state != nil { 332 w.current.state.StopPrefetcher() 333 } 334 atomic.StoreInt32(&w.running, 0) 335 close(w.exitCh) 336 } 337 338 // recalcRecommit recalculates the resubmitting interval upon feedback. 339 func recalcRecommit(minRecommit, prev time.Duration, target float64, inc bool) time.Duration { 340 var ( 341 prevF = float64(prev.Nanoseconds()) 342 next float64 343 ) 344 if inc { 345 next = prevF*(1-intervalAdjustRatio) + intervalAdjustRatio*(target+intervalAdjustBias) 346 max := float64(maxRecommitInterval.Nanoseconds()) 347 if next > max { 348 next = max 349 } 350 } else { 351 next = prevF*(1-intervalAdjustRatio) + intervalAdjustRatio*(target-intervalAdjustBias) 352 min := float64(minRecommit.Nanoseconds()) 353 if next < min { 354 next = min 355 } 356 } 357 return time.Duration(int64(next)) 358 } 359 360 // newWorkLoop is a standalone goroutine to submit new mining work upon received events. 361 func (w *worker) newWorkLoop(recommit time.Duration) { 362 var ( 363 interrupt *int32 364 minRecommit = recommit // minimal resubmit interval specified by user. 365 timestamp int64 // timestamp for each round of mining. 366 ) 367 368 timer := time.NewTimer(0) 369 defer timer.Stop() 370 <-timer.C // discard the initial tick 371 372 // commit aborts in-flight transaction execution with given signal and resubmits a new one. 373 commit := func(noempty bool, s int32) { 374 if interrupt != nil { 375 atomic.StoreInt32(interrupt, s) 376 } 377 interrupt = new(int32) 378 select { 379 case w.newWorkCh <- &newWorkReq{interrupt: interrupt, noempty: noempty, timestamp: timestamp}: 380 case <-w.exitCh: 381 return 382 } 383 timer.Reset(recommit) 384 atomic.StoreInt32(&w.newTxs, 0) 385 } 386 // clearPending cleans the stale pending tasks. 387 clearPending := func(number uint64) { 388 w.pendingMu.Lock() 389 for h, t := range w.pendingTasks { 390 if t.block.NumberU64()+staleThreshold <= number { 391 delete(w.pendingTasks, h) 392 } 393 } 394 w.pendingMu.Unlock() 395 } 396 397 for { 398 select { 399 case <-w.startCh: 400 clearPending(w.chain.CurrentBlock().NumberU64()) 401 timestamp = time.Now().Unix() 402 commit(false, commitInterruptNewHead) 403 404 case head := <-w.chainHeadCh: 405 clearPending(head.Block.NumberU64()) 406 timestamp = time.Now().Unix() 407 commit(false, commitInterruptNewHead) 408 409 case <-timer.C: 410 // If mining is running resubmit a new work cycle periodically to pull in 411 // higher priced transactions. Disable this overhead for pending blocks. 412 if w.isRunning() && (w.chainConfig.Clique == nil || w.chainConfig.Clique.Period > 0) { 413 // Short circuit if no new transaction arrives. 414 if atomic.LoadInt32(&w.newTxs) == 0 { 415 timer.Reset(recommit) 416 continue 417 } 418 commit(true, commitInterruptResubmit) 419 } 420 421 case interval := <-w.resubmitIntervalCh: 422 // Adjust resubmit interval explicitly by user. 423 if interval < minRecommitInterval { 424 log.Warn("Sanitizing miner recommit interval", "provided", interval, "updated", minRecommitInterval) 425 interval = minRecommitInterval 426 } 427 log.Info("Miner recommit interval update", "from", minRecommit, "to", interval) 428 minRecommit, recommit = interval, interval 429 430 if w.resubmitHook != nil { 431 w.resubmitHook(minRecommit, recommit) 432 } 433 434 case adjust := <-w.resubmitAdjustCh: 435 // Adjust resubmit interval by feedback. 436 if adjust.inc { 437 before := recommit 438 target := float64(recommit.Nanoseconds()) / adjust.ratio 439 recommit = recalcRecommit(minRecommit, recommit, target, true) 440 log.Trace("Increase miner recommit interval", "from", before, "to", recommit) 441 } else { 442 before := recommit 443 recommit = recalcRecommit(minRecommit, recommit, float64(minRecommit.Nanoseconds()), false) 444 log.Trace("Decrease miner recommit interval", "from", before, "to", recommit) 445 } 446 447 if w.resubmitHook != nil { 448 w.resubmitHook(minRecommit, recommit) 449 } 450 451 case <-w.exitCh: 452 return 453 } 454 } 455 } 456 457 // mainLoop is a standalone goroutine to regenerate the sealing task based on the received event. 458 func (w *worker) mainLoop() { 459 defer w.txsSub.Unsubscribe() 460 defer w.chainHeadSub.Unsubscribe() 461 defer w.chainSideSub.Unsubscribe() 462 463 for { 464 select { 465 case req := <-w.newWorkCh: 466 w.commitNewWork(req.interrupt, req.noempty, req.timestamp) 467 468 case ev := <-w.chainSideCh: 469 // Short circuit for duplicate side blocks 470 if _, exist := w.localUncles[ev.Block.Hash()]; exist { 471 continue 472 } 473 if _, exist := w.remoteUncles[ev.Block.Hash()]; exist { 474 continue 475 } 476 // Add side block to possible uncle block set depending on the author. 477 if w.isLocalBlock != nil && w.isLocalBlock(ev.Block) { 478 w.localUncles[ev.Block.Hash()] = ev.Block 479 } else { 480 w.remoteUncles[ev.Block.Hash()] = ev.Block 481 } 482 // If our mining block contains less than 2 uncle blocks, 483 // add the new uncle block if valid and regenerate a mining block. 484 if w.isRunning() && w.current != nil && w.current.uncles.Cardinality() < 2 { 485 start := time.Now() 486 if err := w.commitUncle(w.current, ev.Block.Header()); err == nil { 487 var uncles []*types.Header 488 w.current.uncles.Each(func(item interface{}) bool { 489 hash, ok := item.(common.Hash) 490 if !ok { 491 return false 492 } 493 uncle, exist := w.localUncles[hash] 494 if !exist { 495 uncle, exist = w.remoteUncles[hash] 496 } 497 if !exist { 498 return false 499 } 500 uncles = append(uncles, uncle.Header()) 501 return false 502 }) 503 w.commit(uncles, nil, true, start) 504 } 505 } 506 507 case ev := <-w.txsCh: 508 // Apply transactions to the pending state if we're not mining. 509 // 510 // Note all transactions received may not be continuous with transactions 511 // already included in the current mining block. These transactions will 512 // be automatically eliminated. 513 if !w.isRunning() && w.current != nil { 514 // If block is already full, abort 515 if gp := w.current.gasPool; gp != nil && gp.Gas() < params.TxGas { 516 continue 517 } 518 w.mu.RLock() 519 coinbase := w.coinbase 520 w.mu.RUnlock() 521 522 txs := make(map[common.Address]types.Transactions) 523 for _, tx := range ev.Txs { 524 acc, _ := types.Sender(w.current.signer, tx) 525 txs[acc] = append(txs[acc], tx) 526 } 527 txset := types.NewTransactionsByPriceAndNonce(w.current.signer, txs, w.current.header.BaseFee) 528 tcount := w.current.tcount 529 w.commitTransactions(txset, coinbase, nil) 530 // Only update the snapshot if any new transactons were added 531 // to the pending block 532 if tcount != w.current.tcount { 533 w.updateSnapshot() 534 } 535 } else { 536 // Special case, if the consensus engine is 0 period clique(dev mode), 537 // submit mining work here since all empty submission will be rejected 538 // by clique. Of course the advance sealing(empty submission) is disabled. 539 if w.chainConfig.Clique != nil && w.chainConfig.Clique.Period == 0 { 540 w.commitNewWork(nil, true, time.Now().Unix()) 541 } 542 } 543 atomic.AddInt32(&w.newTxs, int32(len(ev.Txs))) 544 545 // System stopped 546 case <-w.exitCh: 547 return 548 case <-w.txsSub.Err(): 549 return 550 case <-w.chainHeadSub.Err(): 551 return 552 case <-w.chainSideSub.Err(): 553 return 554 } 555 } 556 } 557 558 // taskLoop is a standalone goroutine to fetch sealing task from the generator and 559 // push them to consensus engine. 560 func (w *worker) taskLoop() { 561 var ( 562 stopCh chan struct{} 563 prev common.Hash 564 ) 565 566 // interrupt aborts the in-flight sealing task. 567 interrupt := func() { 568 if stopCh != nil { 569 close(stopCh) 570 stopCh = nil 571 } 572 } 573 for { 574 select { 575 case task := <-w.taskCh: 576 if w.newTaskHook != nil { 577 w.newTaskHook(task) 578 } 579 // Reject duplicate sealing work due to resubmitting. 580 sealHash := w.engine.SealHash(task.block.Header()) 581 if sealHash == prev { 582 continue 583 } 584 // Interrupt previous sealing operation 585 interrupt() 586 stopCh, prev = make(chan struct{}), sealHash 587 588 if w.skipSealHook != nil && w.skipSealHook(task) { 589 continue 590 } 591 w.pendingMu.Lock() 592 w.pendingTasks[sealHash] = task 593 w.pendingMu.Unlock() 594 595 if err := w.engine.Seal(w.chain, task.block, w.resultCh, stopCh); err != nil { 596 log.Warn("Block sealing failed", "err", err) 597 } 598 case <-w.exitCh: 599 interrupt() 600 return 601 } 602 } 603 } 604 605 // resultLoop is a standalone goroutine to handle sealing result submitting 606 // and flush relative data to the database. 607 func (w *worker) resultLoop() { 608 for { 609 select { 610 case block := <-w.resultCh: 611 // Short circuit when receiving empty result. 612 if block == nil { 613 continue 614 } 615 // Short circuit when receiving duplicate result caused by resubmitting. 616 if w.chain.HasBlock(block.Hash(), block.NumberU64()) { 617 continue 618 } 619 var ( 620 sealhash = w.engine.SealHash(block.Header()) 621 hash = block.Hash() 622 ) 623 w.pendingMu.RLock() 624 task, exist := w.pendingTasks[sealhash] 625 w.pendingMu.RUnlock() 626 if !exist { 627 log.Error("Block found but no relative pending task", "number", block.Number(), "sealhash", sealhash, "hash", hash) 628 continue 629 } 630 // Different block could share same sealhash, deep copy here to prevent write-write conflict. 631 var ( 632 receipts = make([]*types.Receipt, len(task.receipts)) 633 logs []*types.Log 634 ) 635 for i, receipt := range task.receipts { 636 // add block location fields 637 receipt.BlockHash = hash 638 receipt.BlockNumber = block.Number() 639 receipt.TransactionIndex = uint(i) 640 641 receipts[i] = new(types.Receipt) 642 *receipts[i] = *receipt 643 // Update the block hash in all logs since it is now available and not when the 644 // receipt/log of individual transactions were created. 645 for _, log := range receipt.Logs { 646 log.BlockHash = hash 647 } 648 logs = append(logs, receipt.Logs...) 649 } 650 // Commit block and state to database. 651 _, err := w.chain.WriteBlockWithState(block, receipts, logs, task.state, true) 652 if err != nil { 653 log.Error("Failed writing block to chain", "err", err) 654 continue 655 } 656 log.Info("Successfully sealed new block", "number", block.Number(), "sealhash", sealhash, "hash", hash, 657 "elapsed", common.PrettyDuration(time.Since(task.createdAt))) 658 659 // Broadcast the block and announce chain insertion event 660 w.mux.Post(core.NewMinedBlockEvent{Block: block}) 661 662 // Insert the block into the set of pending ones to resultLoop for confirmations 663 w.unconfirmed.Insert(block.NumberU64(), block.Hash()) 664 665 case <-w.exitCh: 666 return 667 } 668 } 669 } 670 671 // makeCurrent creates a new environment for the current cycle. 672 func (w *worker) makeCurrent(parent *types.Block, header *types.Header) error { 673 // Retrieve the parent state to execute on top and start a prefetcher for 674 // the miner to speed block sealing up a bit 675 state, err := w.chain.StateAt(parent.Root()) 676 if err != nil { 677 return err 678 } 679 state.StartPrefetcher("miner") 680 681 env := &environment{ 682 signer: types.MakeSigner(w.chainConfig, header.Number), 683 state: state, 684 ancestors: mapset.NewSet(), 685 family: mapset.NewSet(), 686 uncles: mapset.NewSet(), 687 header: header, 688 } 689 // when 08 is processed ancestors contain 07 (quick block) 690 for _, ancestor := range w.chain.GetBlocksFromHash(parent.Hash(), 7) { 691 for _, uncle := range ancestor.Uncles() { 692 env.family.Add(uncle.Hash()) 693 } 694 env.family.Add(ancestor.Hash()) 695 env.ancestors.Add(ancestor.Hash()) 696 } 697 // Keep track of transactions which return errors so they can be removed 698 env.tcount = 0 699 700 // Swap out the old work with the new one, terminating any leftover prefetcher 701 // processes in the meantime and starting a new one. 702 if w.current != nil && w.current.state != nil { 703 w.current.state.StopPrefetcher() 704 } 705 w.current = env 706 return nil 707 } 708 709 // commitUncle adds the given block to uncle block set, returns error if failed to add. 710 func (w *worker) commitUncle(env *environment, uncle *types.Header) error { 711 hash := uncle.Hash() 712 if env.uncles.Contains(hash) { 713 return errors.New("uncle not unique") 714 } 715 if env.header.ParentHash == uncle.ParentHash { 716 return errors.New("uncle is sibling") 717 } 718 if !env.ancestors.Contains(uncle.ParentHash) { 719 return errors.New("uncle's parent unknown") 720 } 721 if env.family.Contains(hash) { 722 return errors.New("uncle already included") 723 } 724 env.uncles.Add(uncle.Hash()) 725 return nil 726 } 727 728 // updateSnapshot updates pending snapshot block and state. 729 // Note this function assumes the current variable is thread safe. 730 func (w *worker) updateSnapshot() { 731 w.snapshotMu.Lock() 732 defer w.snapshotMu.Unlock() 733 734 var uncles []*types.Header 735 w.current.uncles.Each(func(item interface{}) bool { 736 hash, ok := item.(common.Hash) 737 if !ok { 738 return false 739 } 740 uncle, exist := w.localUncles[hash] 741 if !exist { 742 uncle, exist = w.remoteUncles[hash] 743 } 744 if !exist { 745 return false 746 } 747 uncles = append(uncles, uncle.Header()) 748 return false 749 }) 750 751 w.snapshotBlock = types.NewBlock( 752 w.current.header, 753 w.current.txs, 754 uncles, 755 w.current.receipts, 756 trie.NewStackTrie(nil), 757 ) 758 w.snapshotReceipts = copyReceipts(w.current.receipts) 759 w.snapshotState = w.current.state.Copy() 760 } 761 762 func (w *worker) commitTransaction(tx *types.Transaction, coinbase common.Address) ([]*types.Log, error) { 763 snap := w.current.state.Snapshot() 764 765 receipt, err := core.ApplyTransaction(w.chainConfig, w.chain, &coinbase, w.current.gasPool, w.current.state, w.current.header, tx, &w.current.header.GasUsed, *w.chain.GetVMConfig(), w.current.extraValidator) 766 if err != nil { 767 w.current.state.RevertToSnapshot(snap) 768 return nil, err 769 } 770 w.current.txs = append(w.current.txs, tx) 771 w.current.receipts = append(w.current.receipts, receipt) 772 773 return receipt.Logs, nil 774 } 775 776 func (w *worker) commitTransactions(txs *types.TransactionsByPriceAndNonce, coinbase common.Address, interrupt *int32) bool { 777 // Short circuit if current is nil 778 if w.current == nil { 779 return true 780 } 781 782 gasLimit := w.current.header.GasLimit 783 if w.current.gasPool == nil { 784 w.current.gasPool = new(core.GasPool).AddGas(gasLimit) 785 } 786 787 var coalescedLogs []*types.Log 788 789 for { 790 // In the following three cases, we will interrupt the execution of the transaction. 791 // (1) new head block event arrival, the interrupt signal is 1 792 // (2) worker start or restart, the interrupt signal is 1 793 // (3) worker recreate the mining block with any newly arrived transactions, the interrupt signal is 2. 794 // For the first two cases, the semi-finished work will be discarded. 795 // For the third case, the semi-finished work will be submitted to the consensus engine. 796 if interrupt != nil && atomic.LoadInt32(interrupt) != commitInterruptNone { 797 // Notify resubmit loop to increase resubmitting interval due to too frequent commits. 798 if atomic.LoadInt32(interrupt) == commitInterruptResubmit { 799 ratio := float64(gasLimit-w.current.gasPool.Gas()) / float64(gasLimit) 800 if ratio < 0.1 { 801 ratio = 0.1 802 } 803 w.resubmitAdjustCh <- &intervalAdjust{ 804 ratio: ratio, 805 inc: true, 806 } 807 } 808 return atomic.LoadInt32(interrupt) == commitInterruptNewHead 809 } 810 // If we don't have enough gas for any further transactions then we're done 811 if w.current.gasPool.Gas() < params.TxGas { 812 log.Trace("Not enough gas for further transactions", "have", w.current.gasPool, "want", params.TxGas) 813 break 814 } 815 // Retrieve the next transaction and abort if all done 816 tx := txs.Peek() 817 if tx == nil { 818 break 819 } 820 // Error may be ignored here. The error has already been checked 821 // during transaction acceptance is the transaction pool. 822 // 823 // We use the eip155 signer regardless of the current hf. 824 from, _ := types.Sender(w.current.signer, tx) 825 // Check whether the tx is replay protected. If we're not in the EIP155 hf 826 // phase, start ignoring the sender until we do. 827 if tx.Protected() && !w.chainConfig.IsEIP155(w.current.header.Number) { 828 log.Trace("Ignoring reply protected transaction", "hash", tx.Hash(), "eip155", w.chainConfig.EIP155Block) 829 830 txs.Pop() 831 continue 832 } 833 // consensus related validation 834 if w.isPoSA { 835 err := w.posa.ValidateTx(from, tx, w.current.header, w.current.state) 836 if err != nil { 837 log.Trace("Ignoring consensus invalid transaction", "hash", tx.Hash().String(), "from", from.String(), "to", tx.To(), "err", err) 838 txs.Pop() 839 continue 840 } 841 } 842 // Start executing the transaction 843 w.current.state.Prepare(tx.Hash(), w.current.tcount) 844 845 logs, err := w.commitTransaction(tx, coinbase) 846 switch { 847 case errors.Is(err, core.ErrGasLimitReached): 848 // Pop the current out-of-gas transaction without shifting in the next from the account 849 log.Trace("Gas limit exceeded for current block", "sender", from) 850 txs.Pop() 851 852 case errors.Is(err, core.ErrNonceTooLow): 853 // New head notification data race between the transaction pool and miner, shift 854 log.Trace("Skipping transaction with low nonce", "sender", from, "nonce", tx.Nonce()) 855 txs.Shift() 856 857 case errors.Is(err, core.ErrNonceTooHigh): 858 // Reorg notification data race between the transaction pool and miner, skip account = 859 log.Trace("Skipping account with hight nonce", "sender", from, "nonce", tx.Nonce()) 860 txs.Pop() 861 862 case errors.Is(err, nil): 863 // Everything ok, collect the logs and shift in the next transaction from the same account 864 coalescedLogs = append(coalescedLogs, logs...) 865 w.current.tcount++ 866 txs.Shift() 867 868 case errors.Is(err, core.ErrTxTypeNotSupported): 869 // Pop the unsupported transaction without shifting in the next from the account 870 log.Trace("Skipping unsupported transaction type", "sender", from, "type", tx.Type()) 871 txs.Pop() 872 873 default: 874 // Strange error, discard the transaction and get the next in line (note, the 875 // nonce-too-high clause will prevent us from executing in vain). 876 log.Debug("Transaction failed, account skipped", "hash", tx.Hash(), "err", err) 877 txs.Shift() 878 } 879 } 880 881 if !w.isRunning() && len(coalescedLogs) > 0 { 882 // We don't push the pendingLogsEvent while we are mining. The reason is that 883 // when we are mining, the worker will regenerate a mining block every 3 seconds. 884 // In order to avoid pushing the repeated pendingLog, we disable the pending log pushing. 885 886 // make a copy, the state caches the logs and these logs get "upgraded" from pending to mined 887 // logs by filling in the block hash when the block was mined by the local miner. This can 888 // cause a race condition if a log was "upgraded" before the PendingLogsEvent is processed. 889 cpy := make([]*types.Log, len(coalescedLogs)) 890 for i, l := range coalescedLogs { 891 cpy[i] = new(types.Log) 892 *cpy[i] = *l 893 } 894 w.pendingLogsFeed.Send(cpy) 895 } 896 // Notify resubmit loop to decrease resubmitting interval if current interval is larger 897 // than the user-specified one. 898 if interrupt != nil { 899 w.resubmitAdjustCh <- &intervalAdjust{inc: false} 900 } 901 return false 902 } 903 904 // commitNewWork generates several new sealing tasks based on the parent block. 905 func (w *worker) commitNewWork(interrupt *int32, noempty bool, timestamp int64) { 906 w.mu.RLock() 907 defer w.mu.RUnlock() 908 909 tstart := time.Now() 910 parent := w.chain.CurrentBlock() 911 912 if parent.Time() >= uint64(timestamp) { 913 timestamp = int64(parent.Time() + 1) 914 } 915 num := parent.Number() 916 header := &types.Header{ 917 ParentHash: parent.Hash(), 918 Number: num.Add(num, common.Big1), 919 GasLimit: core.CalcGasLimit(parent.GasLimit(), w.config.GasCeil), 920 Extra: w.extra, 921 Time: uint64(timestamp), 922 } 923 // Set baseFee and GasLimit if we are on an EIP-1559 chain 924 if w.chainConfig.IsLondon(header.Number) { 925 header.BaseFee = misc.CalcBaseFee(w.chainConfig, parent.Header()) 926 parentGasLimit := parent.GasLimit() 927 header.GasLimit = core.CalcGasLimit(parentGasLimit, w.config.GasCeil) 928 } 929 // Only set the coinbase if our consensus engine is running (avoid spurious block rewards) 930 if w.isRunning() { 931 if w.coinbase == (common.Address{}) { 932 log.Error("Refusing to mine without etherbase") 933 return 934 } 935 header.Coinbase = w.coinbase 936 } 937 if err := w.engine.Prepare(w.chain, header); err != nil { 938 log.Error("Failed to prepare header for mining", "err", err) 939 return 940 } 941 // If we are care about TheDAO hard-fork check whether to override the extra-data or not 942 if daoBlock := w.chainConfig.DAOForkBlock; daoBlock != nil { 943 // Check whether the block is among the fork extra-override range 944 limit := new(big.Int).Add(daoBlock, params.DAOForkExtraRange) 945 if header.Number.Cmp(daoBlock) >= 0 && header.Number.Cmp(limit) < 0 { 946 // Depending on whether we support or oppose the fork, override differently 947 if w.chainConfig.DAOForkSupport { 948 header.Extra = common.CopyBytes(params.DAOForkBlockExtra) 949 } else if bytes.Equal(header.Extra, params.DAOForkBlockExtra) { 950 header.Extra = []byte{} // If miner opposes, don't let it use the reserved extra-data 951 } 952 } 953 } 954 // Could potentially happen if starting to mine in an odd state. 955 err := w.makeCurrent(parent, header) 956 if err != nil { 957 log.Error("Failed to create mining context", "err", err) 958 return 959 } 960 // Create the current work task and check any fork transitions needed 961 env := w.current 962 if w.isPoSA { 963 if err := w.posa.PreHandle(w.chain, header, env.state); err != nil { 964 log.Error("Failed to apply system contract upgrade", "err", err) 965 return 966 } 967 env.extraValidator = w.posa.CreateEvmExtraValidator(header, env.state) 968 } 969 // Accumulate the uncles for the current block 970 uncles := make([]*types.Header, 0, 2) 971 commitUncles := func(blocks map[common.Hash]*types.Block) { 972 // Clean up stale uncle blocks first 973 for hash, uncle := range blocks { 974 if uncle.NumberU64()+staleThreshold <= header.Number.Uint64() { 975 delete(blocks, hash) 976 } 977 } 978 for hash, uncle := range blocks { 979 if len(uncles) == 2 { 980 break 981 } 982 if err := w.commitUncle(env, uncle.Header()); err != nil { 983 log.Trace("Possible uncle rejected", "hash", hash, "reason", err) 984 } else { 985 log.Debug("Committing new uncle to block", "hash", hash) 986 uncles = append(uncles, uncle.Header()) 987 } 988 } 989 } 990 // Prefer to locally generated uncle 991 commitUncles(w.localUncles) 992 commitUncles(w.remoteUncles) 993 994 // Create an empty block based on temporary copied state for 995 // sealing in advance without waiting block execution finished. 996 if !noempty && atomic.LoadUint32(&w.noempty) == 0 { 997 w.commit(uncles, nil, false, tstart) 998 } 999 1000 // Fill the block with all available pending transactions. 1001 pending, err := w.eth.TxPool().Pending(true) 1002 if err != nil { 1003 log.Error("Failed to fetch pending transactions", "err", err) 1004 return 1005 } 1006 // Short circuit if there is no available pending transactions. 1007 // But if we disable empty precommit already, ignore it. Since 1008 // empty block is necessary to keep the liveness of the network. 1009 if len(pending) == 0 && atomic.LoadUint32(&w.noempty) == 0 { 1010 w.updateSnapshot() 1011 return 1012 } 1013 // Split the pending transactions into locals and remotes 1014 localTxs, remoteTxs := make(map[common.Address]types.Transactions), pending 1015 for _, account := range w.eth.TxPool().Locals() { 1016 if txs := remoteTxs[account]; len(txs) > 0 { 1017 delete(remoteTxs, account) 1018 localTxs[account] = txs 1019 } 1020 } 1021 if len(localTxs) > 0 { 1022 txs := types.NewTransactionsByPriceAndNonce(w.current.signer, localTxs, header.BaseFee) 1023 if w.commitTransactions(txs, w.coinbase, interrupt) { 1024 return 1025 } 1026 } 1027 if len(remoteTxs) > 0 { 1028 txs := types.NewTransactionsByPriceAndNonce(w.current.signer, remoteTxs, header.BaseFee) 1029 if w.commitTransactions(txs, w.coinbase, interrupt) { 1030 return 1031 } 1032 } 1033 w.commit(uncles, w.fullTaskHook, true, tstart) 1034 } 1035 1036 // commit runs any post-transaction state modifications, assembles the final block 1037 // and commits new work if consensus engine is running. 1038 func (w *worker) commit(uncles []*types.Header, interval func(), update bool, start time.Time) error { 1039 // Deep copy receipts here to avoid interaction between different tasks. 1040 cpyReceipts := copyReceipts(w.current.receipts) 1041 // copy transactions to a new slice to avoid interaction between different tasks. 1042 txs := make([]*types.Transaction, len(w.current.txs)) 1043 copy(txs, w.current.txs) 1044 s := w.current.state.Copy() 1045 block, receipts, err := w.engine.FinalizeAndAssemble(w.chain, w.current.header, s, txs, uncles, cpyReceipts) 1046 if err != nil { 1047 return err 1048 } 1049 if w.isRunning() { 1050 if interval != nil { 1051 interval() 1052 } 1053 select { 1054 case w.taskCh <- &task{receipts: receipts, state: s, block: block, createdAt: time.Now()}: 1055 w.unconfirmed.Shift(block.NumberU64() - 1) 1056 log.Info("Commit new mining work", "number", block.Number(), "sealhash", w.engine.SealHash(block.Header()), 1057 "uncles", len(uncles), "txs", w.current.tcount, 1058 "gas", block.GasUsed(), "fees", totalFees(block, receipts), 1059 "elapsed", common.PrettyDuration(time.Since(start))) 1060 1061 case <-w.exitCh: 1062 log.Info("Worker has exited") 1063 } 1064 } 1065 if update { 1066 w.updateSnapshot() 1067 } 1068 return nil 1069 } 1070 1071 // copyReceipts makes a deep copy of the given receipts. 1072 func copyReceipts(receipts []*types.Receipt) []*types.Receipt { 1073 result := make([]*types.Receipt, len(receipts)) 1074 for i, l := range receipts { 1075 cpy := *l 1076 result[i] = &cpy 1077 } 1078 return result 1079 } 1080 1081 // postSideBlock fires a side chain event, only use it for testing. 1082 func (w *worker) postSideBlock(event core.ChainSideEvent) { 1083 select { 1084 case w.chainSideCh <- event: 1085 case <-w.exitCh: 1086 } 1087 } 1088 1089 // totalFees computes total consumed miner fees in ETH. Block transactions and receipts have to have the same order. 1090 func totalFees(block *types.Block, receipts []*types.Receipt) *big.Float { 1091 if len(block.Transactions()) != len(receipts) { 1092 // for debug 1093 log.Error("transactions len != receipts len", "blockHash", block.Hash().String(), "number", block.Number()) 1094 for i, tx := range block.Transactions() { 1095 js, _ := tx.MarshalJSON() 1096 log.Error("tx", strconv.Itoa(i), string(js)) 1097 } 1098 for i, receipt := range receipts { 1099 js, _ := receipt.MarshalJSON() 1100 log.Error("receipt", strconv.Itoa(i), string(js)) 1101 } 1102 } 1103 feesWei := new(big.Int) 1104 for i, tx := range block.Transactions() { 1105 minerFee, _ := tx.EffectiveGasTip(block.BaseFee()) 1106 feesWei.Add(feesWei, new(big.Int).Mul(new(big.Int).SetUint64(receipts[i].GasUsed), minerFee)) 1107 } 1108 return new(big.Float).Quo(new(big.Float).SetInt(feesWei), new(big.Float).SetInt(big.NewInt(params.Ether))) 1109 }