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