github.com/kisexp/xdchain@v0.0.0-20211206025815-490d6b732aa7/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 "sync" 24 "sync/atomic" 25 "time" 26 27 mapset "github.com/deckarep/golang-set" 28 "github.com/kisexp/xdchain/common" 29 "github.com/kisexp/xdchain/consensus" 30 "github.com/kisexp/xdchain/consensus/misc" 31 "github.com/kisexp/xdchain/core" 32 "github.com/kisexp/xdchain/core/mps" 33 "github.com/kisexp/xdchain/core/rawdb" 34 "github.com/kisexp/xdchain/core/state" 35 "github.com/kisexp/xdchain/core/types" 36 "github.com/kisexp/xdchain/event" 37 "github.com/kisexp/xdchain/log" 38 "github.com/kisexp/xdchain/params" 39 "github.com/kisexp/xdchain/trie" 40 ) 41 42 const ( 43 // resultQueueSize is the size of channel listening to sealing result. 44 resultQueueSize = 10 45 46 // txChanSize is the size of channel listening to NewTxsEvent. 47 // The number is referenced from the size of tx pool. 48 txChanSize = 4096 49 50 // chainHeadChanSize is the size of channel listening to ChainHeadEvent. 51 chainHeadChanSize = 10 52 53 // chainSideChanSize is the size of channel listening to ChainSideEvent. 54 chainSideChanSize = 10 55 56 // resubmitAdjustChanSize is the size of resubmitting interval adjustment channel. 57 resubmitAdjustChanSize = 10 58 59 // miningLogAtDepth is the number of confirmations before logging successful mining. 60 miningLogAtDepth = 7 61 62 // minRecommitInterval is the minimal time interval to recreate the mining block with 63 // any newly arrived transactions. 64 minRecommitInterval = 1 * time.Second 65 66 // maxRecommitInterval is the maximum time interval to recreate the mining block with 67 // any newly arrived transactions. 68 maxRecommitInterval = 15 * time.Second 69 70 // intervalAdjustRatio is the impact a single interval adjustment has on sealing work 71 // resubmitting interval. 72 intervalAdjustRatio = 0.1 73 74 // intervalAdjustBias is applied during the new resubmit interval calculation in favor of 75 // increasing upper limit or decreasing lower limit so that the limit can be reachable. 76 intervalAdjustBias = 200 * 1000.0 * 1000.0 77 78 // staleThreshold is the maximum depth of the acceptable stale block. 79 staleThreshold = 7 80 ) 81 82 // environment is the worker's current environment and holds all of the current state information. 83 type environment struct { 84 signer types.Signer 85 86 state *state.StateDB // apply state changes here 87 ancestors mapset.Set // ancestor set (used for checking uncle parent validity) 88 family mapset.Set // family set (used for checking uncle invalidity) 89 uncles mapset.Set // uncle set 90 tcount int // tx count in cycle 91 gasPool *core.GasPool // available gas used to pack transactions 92 93 header *types.Header 94 txs []*types.Transaction 95 receipts []*types.Receipt 96 97 // Quorum 98 privateReceipts []*types.Receipt 99 privateStateRepo mps.PrivateStateRepository 100 // End Quorum 101 } 102 103 // task contains all information for consensus engine sealing and result submitting. 104 type task struct { 105 receipts []*types.Receipt 106 state *state.StateDB 107 block *types.Block 108 createdAt time.Time 109 110 // Quorum 111 privateReceipts []*types.Receipt 112 privateStateRepo mps.PrivateStateRepository 113 // End Quorum 114 } 115 116 const ( 117 commitInterruptNone int32 = iota 118 commitInterruptNewHead 119 commitInterruptResubmit 120 ) 121 122 // newWorkReq represents a request for new sealing work submitting with relative interrupt notifier. 123 type newWorkReq struct { 124 interrupt *int32 125 noempty bool 126 timestamp int64 127 } 128 129 // intervalAdjust represents a resubmitting interval adjustment. 130 type intervalAdjust struct { 131 ratio float64 132 inc bool 133 } 134 135 // worker is the main object which takes care of submitting new work to consensus engine 136 // and gathering the sealing result. 137 type worker struct { 138 config *Config 139 chainConfig *params.ChainConfig 140 engine consensus.Engine 141 eth Backend 142 chain *core.BlockChain 143 144 // Feeds 145 pendingLogsFeed event.Feed 146 147 // Subscriptions 148 mux *event.TypeMux 149 txsCh chan core.NewTxsEvent 150 txsSub event.Subscription 151 chainHeadCh chan core.ChainHeadEvent 152 chainHeadSub event.Subscription 153 chainSideCh chan core.ChainSideEvent 154 chainSideSub event.Subscription 155 156 // Channels 157 newWorkCh chan *newWorkReq 158 taskCh chan *task 159 resultCh chan *types.Block 160 startCh chan struct{} 161 exitCh chan struct{} 162 resubmitIntervalCh chan time.Duration 163 resubmitAdjustCh chan *intervalAdjust 164 165 current *environment // An environment for current running cycle. 166 localUncles map[common.Hash]*types.Block // A set of side blocks generated locally as the possible uncle blocks. 167 remoteUncles map[common.Hash]*types.Block // A set of side blocks as the possible uncle blocks. 168 unconfirmed *unconfirmedBlocks // A set of locally mined blocks pending canonicalness confirmations. 169 170 mu sync.RWMutex // The lock used to protect the coinbase and extra fields 171 coinbase common.Address 172 extra []byte 173 174 pendingMu sync.RWMutex 175 pendingTasks map[common.Hash]*task 176 177 snapshotMu sync.RWMutex // The lock used to protect the block snapshot and state snapshot 178 snapshotBlock *types.Block 179 snapshotState *state.StateDB 180 181 // atomic status counters 182 running int32 // The indicator whether the consensus engine is running or not. 183 newTxs int32 // New arrival transaction count since last sealing work submitting. 184 185 // noempty is the flag used to control whether the feature of pre-seal empty 186 // block is enabled. The default value is false(pre-seal is enabled by default). 187 // But in some special scenario the consensus engine will seal blocks instantaneously, 188 // in this case this feature will add all empty blocks into canonical chain 189 // non-stop and no real transaction will be included. 190 noempty uint32 191 192 // External functions 193 isLocalBlock func(block *types.Block) bool // Function used to determine whether the specified block is mined by local miner. 194 195 // Test hooks 196 newTaskHook func(*task) // Method to call upon receiving a new sealing task. 197 skipSealHook func(*task) bool // Method to decide whether skipping the sealing. 198 fullTaskHook func() // Method to call before pushing the full sealing task. 199 resubmitHook func(time.Duration, time.Duration) // Method to call upon updating resubmitting interval. 200 } 201 202 func newWorker(config *Config, chainConfig *params.ChainConfig, engine consensus.Engine, eth Backend, mux *event.TypeMux, isLocalBlock func(*types.Block) bool, init bool) *worker { 203 worker := &worker{ 204 config: config, 205 chainConfig: chainConfig, 206 engine: engine, 207 eth: eth, 208 mux: mux, 209 chain: eth.BlockChain(), 210 isLocalBlock: isLocalBlock, 211 localUncles: make(map[common.Hash]*types.Block), 212 remoteUncles: make(map[common.Hash]*types.Block), 213 unconfirmed: newUnconfirmedBlocks(eth.BlockChain(), miningLogAtDepth), 214 pendingTasks: make(map[common.Hash]*task), 215 txsCh: make(chan core.NewTxsEvent, txChanSize), 216 chainHeadCh: make(chan core.ChainHeadEvent, chainHeadChanSize), 217 chainSideCh: make(chan core.ChainSideEvent, chainSideChanSize), 218 newWorkCh: make(chan *newWorkReq), 219 taskCh: make(chan *task), 220 resultCh: make(chan *types.Block, resultQueueSize), 221 exitCh: make(chan struct{}), 222 startCh: make(chan struct{}, 1), 223 resubmitIntervalCh: make(chan time.Duration), 224 resubmitAdjustCh: make(chan *intervalAdjust, resubmitAdjustChanSize), 225 } 226 if _, ok := engine.(consensus.Istanbul); ok || !chainConfig.IsQuorum || chainConfig.Clique != nil { 227 // Subscribe NewTxsEvent for tx pool 228 worker.txsSub = eth.TxPool().SubscribeNewTxsEvent(worker.txsCh) 229 // Subscribe events for blockchain 230 worker.chainHeadSub = eth.BlockChain().SubscribeChainHeadEvent(worker.chainHeadCh) 231 worker.chainSideSub = eth.BlockChain().SubscribeChainSideEvent(worker.chainSideCh) 232 // Sanitize recommit interval if the user-specified one is too short. 233 //如果用户指定的时间间隔太短,请清理重新提交时间间隔。 234 recommit := worker.config.Recommit 235 if recommit < minRecommitInterval { 236 log.Warn("Sanitizing miner recommit interval", "provided", recommit, "updated", minRecommitInterval) 237 recommit = minRecommitInterval 238 } 239 240 go worker.mainLoop() 241 go worker.newWorkLoop(recommit) 242 go worker.resultLoop() 243 go worker.taskLoop() 244 245 // Submit first work to initialize pending state. 246 if init { 247 worker.startCh <- struct{}{} 248 } 249 } 250 return worker 251 } 252 253 // setEtherbase sets the etherbase used to initialize the block coinbase field. 254 func (w *worker) setEtherbase(addr common.Address) { 255 w.mu.Lock() 256 defer w.mu.Unlock() 257 w.coinbase = addr 258 } 259 260 // setExtra sets the content used to initialize the block extra field. 261 func (w *worker) setExtra(extra []byte) { 262 w.mu.Lock() 263 defer w.mu.Unlock() 264 w.extra = extra 265 } 266 267 // setRecommitInterval updates the interval for miner sealing work recommitting. 268 func (w *worker) setRecommitInterval(interval time.Duration) { 269 w.resubmitIntervalCh <- interval 270 } 271 272 // disablePreseal disables pre-sealing mining feature 273 func (w *worker) disablePreseal() { 274 atomic.StoreUint32(&w.noempty, 1) 275 } 276 277 // enablePreseal enables pre-sealing mining feature 278 func (w *worker) enablePreseal() { 279 atomic.StoreUint32(&w.noempty, 0) 280 } 281 282 // pending returns the pending state and corresponding block. 283 func (w *worker) pending(psi types.PrivateStateIdentifier) (*types.Block, *state.StateDB, *state.StateDB) { 284 // return a snapshot to avoid contention on currentMu mutex 285 w.snapshotMu.RLock() 286 defer w.snapshotMu.RUnlock() 287 if w.snapshotState == nil { 288 return nil, nil, nil 289 } 290 privateState, err := w.current.privateStateRepo.StatePSI(psi) 291 if err != nil { 292 log.Error("Unable to retrieve private state", "psi", psi, "err", err) 293 return nil, nil, nil 294 } 295 return w.snapshotBlock, w.snapshotState.Copy(), privateState.Copy() 296 } 297 298 // pendingBlock returns pending block. 299 func (w *worker) pendingBlock() *types.Block { 300 // return a snapshot to avoid contention on currentMu mutex 301 w.snapshotMu.RLock() 302 defer w.snapshotMu.RUnlock() 303 return w.snapshotBlock 304 } 305 306 // start sets the running status as 1 and triggers new work submitting. 307 func (w *worker) start() { 308 atomic.StoreInt32(&w.running, 1) 309 if istanbul, ok := w.engine.(consensus.Istanbul); ok { 310 istanbul.Start(w.chain, w.chain.CurrentBlock, w.chain.HasBadBlock) 311 } 312 w.startCh <- struct{}{} 313 } 314 315 // stop sets the running status as 0. 316 func (w *worker) stop() { 317 if istanbul, ok := w.engine.(consensus.Istanbul); ok { 318 istanbul.Stop() 319 } 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 atomic.StoreInt32(&w.running, 0) 332 close(w.exitCh) 333 } 334 335 // recalcRecommit recalculates the resubmitting interval upon feedback. 336 func recalcRecommit(minRecommit, prev time.Duration, target float64, inc bool) time.Duration { 337 var ( 338 prevF = float64(prev.Nanoseconds()) 339 next float64 340 ) 341 if inc { 342 next = prevF*(1-intervalAdjustRatio) + intervalAdjustRatio*(target+intervalAdjustBias) 343 max := float64(maxRecommitInterval.Nanoseconds()) 344 if next > max { 345 next = max 346 } 347 } else { 348 next = prevF*(1-intervalAdjustRatio) + intervalAdjustRatio*(target-intervalAdjustBias) 349 min := float64(minRecommit.Nanoseconds()) 350 if next < min { 351 next = min 352 } 353 } 354 return time.Duration(int64(next)) 355 } 356 357 // newWorkLoop is a standalone goroutine to submit new mining work upon received events. 358 func (w *worker) newWorkLoop(recommit time.Duration) { 359 var ( 360 interrupt *int32 361 minRecommit = recommit // minimal resubmit interval specified by user. 362 timestamp int64 // timestamp for each round of mining. 363 ) 364 365 timer := time.NewTimer(0) 366 defer timer.Stop() 367 <-timer.C // discard the initial tick 368 369 // commit aborts in-flight transaction execution with given signal and resubmits a new one. 370 commit := func(noempty bool, s int32) { 371 if interrupt != nil { 372 atomic.StoreInt32(interrupt, s) 373 } 374 interrupt = new(int32) 375 w.newWorkCh <- &newWorkReq{interrupt: interrupt, noempty: noempty, timestamp: timestamp} 376 timer.Reset(recommit) 377 atomic.StoreInt32(&w.newTxs, 0) 378 } 379 // clearPending cleans the stale pending tasks. 380 clearPending := func(number uint64) { 381 w.pendingMu.Lock() 382 for h, t := range w.pendingTasks { 383 if t.block.NumberU64()+staleThreshold <= number { 384 delete(w.pendingTasks, h) 385 } 386 } 387 w.pendingMu.Unlock() 388 } 389 390 for { 391 select { 392 case <-w.startCh: 393 clearPending(w.chain.CurrentBlock().NumberU64()) 394 timestamp = time.Now().Unix() 395 commit(false, commitInterruptNewHead) 396 397 case head := <-w.chainHeadCh: 398 if h, ok := w.engine.(consensus.Handler); ok { 399 h.NewChainHead() 400 } 401 clearPending(head.Block.NumberU64()) 402 timestamp = time.Now().Unix() 403 commit(false, commitInterruptNewHead) 404 405 case <-timer.C: 406 // If mining is running resubmit a new work cycle periodically to pull in 407 // higher priced transactions. Disable this overhead for pending blocks. 408 if w.isRunning() && (w.chainConfig.Clique == nil || w.chainConfig.Clique.Period > 0) { 409 // Short circuit if no new transaction arrives. 410 if atomic.LoadInt32(&w.newTxs) == 0 { 411 timer.Reset(recommit) 412 continue 413 } 414 commit(true, commitInterruptResubmit) 415 } 416 417 case interval := <-w.resubmitIntervalCh: 418 // Adjust resubmit interval explicitly by user. 419 if interval < minRecommitInterval { 420 log.Warn("Sanitizing miner recommit interval", "provided", interval, "updated", minRecommitInterval) 421 interval = minRecommitInterval 422 } 423 log.Info("Miner recommit interval update", "from", minRecommit, "to", interval) 424 minRecommit, recommit = interval, interval 425 426 if w.resubmitHook != nil { 427 w.resubmitHook(minRecommit, recommit) 428 } 429 430 case adjust := <-w.resubmitAdjustCh: 431 // Adjust resubmit interval by feedback. 432 if adjust.inc { 433 before := recommit 434 target := float64(recommit.Nanoseconds()) / adjust.ratio 435 recommit = recalcRecommit(minRecommit, recommit, target, true) 436 log.Trace("Increase miner recommit interval", "from", before, "to", recommit) 437 } else { 438 before := recommit 439 recommit = recalcRecommit(minRecommit, recommit, float64(minRecommit.Nanoseconds()), false) 440 log.Trace("Decrease miner recommit interval", "from", before, "to", recommit) 441 } 442 443 if w.resubmitHook != nil { 444 w.resubmitHook(minRecommit, recommit) 445 } 446 447 case <-w.exitCh: 448 return 449 } 450 } 451 } 452 453 // mainLoop is a standalone goroutine to regenerate the sealing task based on the received event. 454 func (w *worker) mainLoop() { 455 defer w.txsSub.Unsubscribe() 456 defer w.chainHeadSub.Unsubscribe() 457 defer w.chainSideSub.Unsubscribe() 458 459 for { 460 select { 461 case req := <-w.newWorkCh: 462 w.commitNewWork(req.interrupt, req.noempty, req.timestamp) 463 464 case ev := <-w.chainSideCh: 465 // Short circuit for duplicate side blocks 466 if _, exist := w.localUncles[ev.Block.Hash()]; exist { 467 continue 468 } 469 if _, exist := w.remoteUncles[ev.Block.Hash()]; exist { 470 continue 471 } 472 // Add side block to possible uncle block set depending on the author. 473 if w.isLocalBlock != nil && w.isLocalBlock(ev.Block) { 474 w.localUncles[ev.Block.Hash()] = ev.Block 475 } else { 476 w.remoteUncles[ev.Block.Hash()] = ev.Block 477 } 478 // If our mining block contains less than 2 uncle blocks, 479 // add the new uncle block if valid and regenerate a mining block. 480 // 如果我们的采矿区块少于2个区块, 481 // 添加新块(如果有效)并重新生成挖掘块。 482 if w.isRunning() && w.current != nil && w.current.uncles.Cardinality() < 2 { 483 start := time.Now() 484 if err := w.commitUncle(w.current, ev.Block.Header()); err == nil { 485 var uncles []*types.Header 486 w.current.uncles.Each(func(item interface{}) bool { 487 hash, ok := item.(common.Hash) 488 if !ok { 489 return false 490 } 491 uncle, exist := w.localUncles[hash] 492 if !exist { 493 uncle, exist = w.remoteUncles[hash] 494 } 495 if !exist { 496 return false 497 } 498 uncles = append(uncles, uncle.Header()) 499 return false 500 }) 501 w.commit(uncles, nil, true, start) 502 } 503 } 504 505 case ev := <-w.txsCh: 506 // Apply transactions to the pending state if we're not mining. 507 // 508 // Note all transactions received may not be continuous with transactions 509 // already included in the current mining block. These transactions will 510 // be automatically eliminated. 511 if !w.isRunning() && w.current != nil { 512 // If block is already full, abort 513 if gp := w.current.gasPool; gp != nil && gp.Gas() < params.TxGas { 514 continue 515 } 516 w.mu.RLock() 517 coinbase := w.coinbase 518 w.mu.RUnlock() 519 520 txs := make(map[common.Address]types.Transactions) 521 for _, tx := range ev.Txs { 522 acc, _ := types.Sender(w.current.signer, tx) 523 txs[acc] = append(txs[acc], tx) 524 } 525 txset := types.NewTransactionsByPriceAndNonce(w.current.signer, txs) 526 tcount := w.current.tcount 527 w.commitTransactions(txset, coinbase, nil) 528 // Only update the snapshot if any new transactons were added 529 // to the pending block 530 if tcount != w.current.tcount { 531 w.updateSnapshot() 532 } 533 } else { 534 // Special case, if the consensus engine is 0 period clique(dev mode), 535 // submit mining work here since all empty submission will be rejected 536 // by clique. Of course the advance sealing(empty submission) is disabled. 537 if w.chainConfig.Clique != nil && w.chainConfig.Clique.Period == 0 { 538 w.commitNewWork(nil, true, time.Now().Unix()) 539 } 540 } 541 atomic.AddInt32(&w.newTxs, int32(len(ev.Txs))) 542 543 // System stopped 544 case <-w.exitCh: 545 return 546 case <-w.txsSub.Err(): 547 return 548 case <-w.chainHeadSub.Err(): 549 return 550 case <-w.chainSideSub.Err(): 551 return 552 } 553 } 554 } 555 556 // taskLoop is a standalone goroutine to fetch sealing task from the generator and 557 // push them to consensus engine. 558 func (w *worker) taskLoop() { 559 var ( 560 stopCh chan struct{} 561 prev common.Hash 562 ) 563 564 // interrupt aborts the in-flight sealing task. 565 interrupt := func() { 566 if stopCh != nil { 567 close(stopCh) 568 stopCh = nil 569 } 570 } 571 for { 572 select { 573 case task := <-w.taskCh: 574 if w.newTaskHook != nil { 575 w.newTaskHook(task) 576 } 577 // Reject duplicate sealing work due to resubmitting. 578 sealHash := w.engine.SealHash(task.block.Header()) 579 if sealHash == prev { 580 continue 581 } 582 // Interrupt previous sealing operation 583 interrupt() 584 stopCh, prev = make(chan struct{}), sealHash 585 586 if w.skipSealHook != nil && w.skipSealHook(task) { 587 continue 588 } 589 w.pendingMu.Lock() 590 w.pendingTasks[sealHash] = task 591 w.pendingMu.Unlock() 592 593 if err := w.engine.Seal(w.chain, task.block, w.resultCh, stopCh); err != nil { 594 log.Warn("Block sealing failed", "err", err) 595 } 596 case <-w.exitCh: 597 interrupt() 598 return 599 } 600 } 601 } 602 603 // resultLoop is a standalone goroutine to handle sealing result submitting 604 // and flush relative data to the database. 605 //resultLoop是一个独立的goroutine,用于处理密封结果提交 606 //并将相关数据刷新到数据库。 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 pubReceipts = make([]*types.Receipt, len(task.receipts)) 633 prvReceipts = make([]*types.Receipt, len(task.privateReceipts)) 634 logs []*types.Log 635 ) 636 offset := len(task.receipts) 637 for i, receipt := range task.receipts { 638 // add block location fields 639 receipt.BlockHash = hash 640 receipt.BlockNumber = block.Number() 641 receipt.TransactionIndex = uint(i) 642 643 pubReceipts[i] = new(types.Receipt) 644 *pubReceipts[i] = *receipt 645 // Update the block hash in all logs since it is now available and not when the 646 // receipt/log of individual transactions were created. 647 for _, log := range receipt.Logs { 648 log.BlockHash = hash 649 } 650 logs = append(logs, receipt.Logs...) 651 652 // If this was a public privacy marker transaction then there will be an associated private receipt to handle. 653 if receipt.PSReceipts != nil { 654 tx := block.Transaction(receipt.TxHash) 655 if tx.IsPrivacyMarker() { 656 for _, markerReceipt := range receipt.PSReceipts { 657 markerReceipt.BlockHash = hash 658 markerReceipt.BlockNumber = block.Number() 659 markerReceipt.TransactionIndex = uint(i) 660 661 // Update the block hash in all logs since it is now available and not when the 662 // receipt/log of individual transactions were created. 663 for _, log := range markerReceipt.Logs { 664 log.BlockHash = hash 665 } 666 // Note that we don't append logs here, else will get duplicates. 667 } 668 } 669 } 670 } 671 672 for i, receipt := range task.privateReceipts { 673 // add block location fields 674 receipt.BlockHash = hash 675 receipt.BlockNumber = block.Number() 676 receipt.TransactionIndex = uint(i + offset) 677 678 prvReceipts[i] = new(types.Receipt) 679 *prvReceipts[i] = *receipt 680 // Update the block hash in all logs since it is now available and not when the 681 // receipt/log of individual transactions were created. 682 for _, log := range receipt.Logs { 683 log.BlockHash = hash 684 } 685 logs = append(logs, receipt.Logs...) 686 687 for _, psReceipt := range receipt.PSReceipts { 688 // if block location fields are already populated then this is a privacy marker receipt and it is 689 // already handled - skip processing it again 690 if psReceipt.BlockNumber != nil { 691 continue 692 } 693 // add block location fields 694 psReceipt.BlockHash = hash 695 psReceipt.BlockNumber = block.Number() 696 psReceipt.TransactionIndex = uint(i + offset) 697 698 // Update the block hash in all logs since it is now available and not when the 699 // receipt/log of individual transactions were created. 700 for _, log := range psReceipt.Logs { 701 log.BlockHash = hash 702 } 703 logs = append(logs, psReceipt.Logs...) 704 } 705 } 706 707 allReceipts := task.privateStateRepo.MergeReceipts(pubReceipts, prvReceipts) 708 709 // Commit block and state to database. 710 _, err := w.chain.WriteBlockWithState(block, allReceipts, logs, task.state, task.privateStateRepo, true) 711 if err != nil { 712 log.Error("Failed writing block to chain", "err", err) 713 continue 714 } 715 if err := rawdb.WritePrivateBlockBloom(w.eth.ChainDb(), block.NumberU64(), prvReceipts); err != nil { 716 log.Error("Failed writing private block bloom", "err", err) 717 continue 718 } 719 log.Info("Successfully sealed new block", "number", block.Number(), "sealhash", sealhash, "hash", hash, 720 "elapsed", common.PrettyDuration(time.Since(task.createdAt))) 721 722 // Broadcast the block and announce chain insertion event 723 //广播区块并宣布链插入事件 724 w.mux.Post(core.NewMinedBlockEvent{Block: block}) 725 726 // Insert the block into the set of pending ones to resultLoop for confirmations 727 //将块插入到resultLoop的挂起块集中进行确认 728 w.unconfirmed.Insert(block.NumberU64(), block.Hash()) 729 730 case <-w.exitCh: 731 return 732 } 733 } 734 } 735 736 // makeCurrent creates a new environment for the current cycle. 737 func (w *worker) makeCurrent(parent *types.Block, header *types.Header) error { 738 publicState, privateStateRepo, err := w.chain.StateAt(parent.Root()) 739 if err != nil { 740 return err 741 } 742 env := &environment{ 743 signer: types.MakeSigner(w.chainConfig, header.Number), 744 state: publicState, 745 ancestors: mapset.NewSet(), 746 family: mapset.NewSet(), 747 uncles: mapset.NewSet(), 748 header: header, 749 privateStateRepo: privateStateRepo, 750 } 751 752 // when 08 is processed ancestors contain 07 (quick block) 753 for _, ancestor := range w.chain.GetBlocksFromHash(parent.Hash(), 7) { 754 for _, uncle := range ancestor.Uncles() { 755 env.family.Add(uncle.Hash()) 756 } 757 env.family.Add(ancestor.Hash()) 758 env.ancestors.Add(ancestor.Hash()) 759 } 760 761 // Keep track of transactions which return errors so they can be removed 762 env.tcount = 0 763 w.current = env 764 return nil 765 } 766 767 // commitUncle adds the given block to uncle block set, returns error if failed to add. 768 func (w *worker) commitUncle(env *environment, uncle *types.Header) error { 769 hash := uncle.Hash() 770 if env.uncles.Contains(hash) { 771 return errors.New("uncle not unique") 772 } 773 if env.header.ParentHash == uncle.ParentHash { 774 return errors.New("uncle is sibling") 775 } 776 if !env.ancestors.Contains(uncle.ParentHash) { 777 return errors.New("uncle's parent unknown") 778 } 779 if env.family.Contains(hash) { 780 return errors.New("uncle already included") 781 } 782 env.uncles.Add(uncle.Hash()) 783 return nil 784 } 785 786 // updateSnapshot updates pending snapshot block and state. 787 // Note this function assumes the current variable is thread safe. 788 func (w *worker) updateSnapshot() { 789 w.snapshotMu.Lock() 790 defer w.snapshotMu.Unlock() 791 792 var uncles []*types.Header 793 w.current.uncles.Each(func(item interface{}) bool { 794 hash, ok := item.(common.Hash) 795 if !ok { 796 return false 797 } 798 uncle, exist := w.localUncles[hash] 799 if !exist { 800 uncle, exist = w.remoteUncles[hash] 801 } 802 if !exist { 803 return false 804 } 805 uncles = append(uncles, uncle.Header()) 806 return false 807 }) 808 809 w.snapshotBlock = types.NewBlock( 810 w.current.header, 811 w.current.txs, 812 uncles, 813 w.current.receipts, 814 new(trie.Trie), 815 ) 816 817 w.snapshotState = w.current.state.Copy() 818 } 819 820 func (w *worker) commitTransaction(tx *types.Transaction, coinbase common.Address) ([]*types.Log, error) { 821 workerEnv := w.current 822 publicStateDB := workerEnv.state 823 snap := publicStateDB.Snapshot() 824 privateStateRepo := workerEnv.privateStateRepo 825 txnStart := time.Now() 826 827 mpsReceipt, privateStateSnaphots, err := w.handleMPS(tx, coinbase, false) 828 if err != nil { 829 w.revertToPrivateStateSnapshots(privateStateSnaphots) 830 return nil, err 831 } 832 privateStateDB, err := privateStateRepo.DefaultState() 833 if err != nil { 834 w.revertToPrivateStateSnapshots(privateStateSnaphots) 835 return nil, err 836 } 837 privateStateDB.Prepare(tx.Hash(), common.Hash{}, workerEnv.tcount) 838 publicStateDB.Prepare(tx.Hash(), common.Hash{}, workerEnv.tcount) 839 privateStateSnaphots[privateStateRepo.DefaultStateMetadata().ID] = privateStateDB.Snapshot() 840 receipt, privateReceipt, err := core.ApplyTransaction(w.chainConfig, w.chain, &coinbase, workerEnv.gasPool, publicStateDB, privateStateDB, workerEnv.header, tx, &workerEnv.header.GasUsed, *w.chain.GetVMConfig(), privateStateRepo.IsMPS(), privateStateRepo) 841 if err != nil { 842 publicStateDB.RevertToSnapshot(snap) 843 w.revertToPrivateStateSnapshots(privateStateSnaphots) 844 return nil, err 845 } 846 workerEnv.txs = append(workerEnv.txs, tx) 847 workerEnv.receipts = append(workerEnv.receipts, receipt) 848 log.EmitCheckpoint(log.TxCompleted, "tx", tx.Hash().Hex(), "time", time.Since(txnStart)) 849 850 logs := receipt.Logs 851 852 // Quorum 853 if privateReceipt != nil { 854 newPrivateReceipt, privateLogs := core.HandlePrivateReceipt(receipt, privateReceipt, mpsReceipt, tx, privateStateDB, privateStateRepo, w.chain) 855 workerEnv.privateReceipts = append(workerEnv.privateReceipts, newPrivateReceipt) 856 logs = append(logs, privateLogs...) 857 } 858 // End Quorum 859 860 return logs, nil 861 } 862 863 func (w *worker) commitTransactions(txs *types.TransactionsByPriceAndNonce, coinbase common.Address, interrupt *int32) bool { 864 // Short circuit if current is nil 865 if w.current == nil { 866 return true 867 } 868 869 if w.current.gasPool == nil { 870 w.current.gasPool = new(core.GasPool).AddGas(w.current.header.GasLimit) 871 } 872 873 var coalescedLogs []*types.Log 874 875 loopStartTime := time.Now() // Quorum 876 for { 877 // In the following three cases, we will interrupt the execution of the transaction. 878 // (1) new head block event arrival, the interrupt signal is 1 879 // (2) worker start or restart, the interrupt signal is 1 880 // (3) worker recreate the mining block with any newly arrived transactions, the interrupt signal is 2. 881 // For the first two cases, the semi-finished work will be discarded. 882 // For the third case, the semi-finished work will be submitted to the consensus engine. 883 if interrupt != nil && atomic.LoadInt32(interrupt) != commitInterruptNone { 884 log.Info("Aborting transaction processing due to 'commitInterruptNewHead',", "elapsed time", time.Since(loopStartTime)) // Quorum 885 // Notify resubmit loop to increase resubmitting interval due to too frequent commits. 886 if atomic.LoadInt32(interrupt) == commitInterruptResubmit { 887 ratio := float64(w.current.header.GasLimit-w.current.gasPool.Gas()) / float64(w.current.header.GasLimit) 888 if ratio < 0.1 { 889 ratio = 0.1 890 } 891 w.resubmitAdjustCh <- &intervalAdjust{ 892 ratio: ratio, 893 inc: true, 894 } 895 } 896 return atomic.LoadInt32(interrupt) == commitInterruptNewHead 897 } 898 // If we don't have enough gas for any further transactions then we're done 899 if w.current.gasPool.Gas() < params.TxGas { 900 log.Trace("Not enough gas for further transactions", "have", w.current.gasPool, "want", params.TxGas) 901 break 902 } 903 // Retrieve the next transaction and abort if all done 904 tx := txs.Peek() 905 if tx == nil { 906 break 907 } 908 // Error may be ignored here. The error has already been checked 909 // during transaction acceptance is the transaction pool. 910 // 911 // We use the eip155 signer regardless of the current hf. 912 from, _ := types.Sender(w.current.signer, tx) 913 // Check whether the tx is replay protected. If we're not in the EIP155 hf 914 // phase, start ignoring the sender until we do. 915 if tx.Protected() && !w.chainConfig.IsEIP155(w.current.header.Number) && !tx.IsPrivate() { 916 log.Trace("Ignoring reply protected transaction", "hash", tx.Hash(), "eip155", w.chainConfig.EIP155Block) 917 918 txs.Pop() 919 continue 920 } 921 // Start executing the transaction 922 logs, err := w.commitTransaction(tx, coinbase) 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 w.current.tcount++ 943 txs.Shift() 944 945 default: 946 // Strange error, discard the transaction and get the next in line (note, the 947 // nonce-too-high clause will prevent us from executing in vain). 948 log.Debug("Transaction failed, account skipped", "hash", tx.Hash(), "err", err) 949 txs.Shift() 950 } 951 } 952 953 if !w.isRunning() && len(coalescedLogs) > 0 { 954 // We don't push the pendingLogsEvent while we are mining. The reason is that 955 // when we are mining, the worker will regenerate a mining block every 3 seconds. 956 // In order to avoid pushing the repeated pendingLog, we disable the pending log pushing. 957 958 // make a copy, the state caches the logs and these logs get "upgraded" from pending to mined 959 // logs by filling in the block hash when the block was mined by the local miner. This can 960 // cause a race condition if a log was "upgraded" before the PendingLogsEvent is processed. 961 cpy := make([]*types.Log, len(coalescedLogs)) 962 for i, l := range coalescedLogs { 963 cpy[i] = new(types.Log) 964 *cpy[i] = *l 965 } 966 w.pendingLogsFeed.Send(cpy) 967 } 968 // Notify resubmit loop to decrease resubmitting interval if current interval is larger 969 // than the user-specified one. 970 if interrupt != nil { 971 w.resubmitAdjustCh <- &intervalAdjust{inc: false} 972 } 973 return false 974 } 975 976 // commitNewWork generates several new sealing tasks based on the parent block. 977 // commitNewWork基于父块生成几个新的密封任务。 978 func (w *worker) commitNewWork(interrupt *int32, noempty bool, timestamp int64) { 979 w.mu.RLock() 980 defer w.mu.RUnlock() 981 982 tstart := time.Now() 983 parent := w.chain.CurrentBlock() 984 985 if parent.Time() >= uint64(timestamp) { 986 timestamp = int64(parent.Time() + 1) 987 } 988 989 allowedFutureBlockTime := int64(w.config.AllowedFutureBlockTime) //Quorum - get AllowedFutureBlockTime to fix issue # 1004 990 991 // this will ensure we're not going off too far in the future 992 // 这将确保之后的区块不会超出最大时间 993 if now := time.Now().Unix(); timestamp > now+1+allowedFutureBlockTime { 994 wait := time.Duration(timestamp-now) * time.Second 995 log.Info("Mining too far in the future", "wait", common.PrettyDuration(wait)) 996 time.Sleep(wait) 997 } 998 999 num := parent.Number() 1000 header := &types.Header{ 1001 ParentHash: parent.Hash(), 1002 Number: num.Add(num, common.Big1), 1003 GasLimit: core.CalcGasLimit(parent, w.config.GasFloor, w.config.GasCeil), 1004 Extra: w.extra, 1005 Time: uint64(timestamp), 1006 } 1007 // Only set the coinbase if our consensus engine is running (avoid spurious block rewards) 1008 if w.isRunning() { 1009 if w.coinbase == (common.Address{}) { 1010 log.Error("Refusing to mine without etherbase") 1011 return 1012 } 1013 header.Coinbase = w.coinbase 1014 } 1015 if err := w.engine.Prepare(w.chain, header); err != nil { 1016 log.Error("Failed to prepare header for mining", "err", err) 1017 return 1018 } 1019 // If we are care about TheDAO hard-fork check whether to override the extra-data or not 1020 if daoBlock := w.chainConfig.DAOForkBlock; daoBlock != nil { 1021 // Check whether the block is among the fork extra-override range 1022 limit := new(big.Int).Add(daoBlock, params.DAOForkExtraRange) 1023 if header.Number.Cmp(daoBlock) >= 0 && header.Number.Cmp(limit) < 0 { 1024 // Depending whether we support or oppose the fork, override differently 1025 if w.chainConfig.DAOForkSupport { 1026 header.Extra = common.CopyBytes(params.DAOForkBlockExtra) 1027 } else if bytes.Equal(header.Extra, params.DAOForkBlockExtra) { 1028 header.Extra = []byte{} // If miner opposes, don't let it use the reserved extra-data 1029 } 1030 } 1031 } 1032 // Could potentially happen if starting to mine in an odd state. 1033 err := w.makeCurrent(parent, header) 1034 if err != nil { 1035 log.Error("Failed to create mining context", "err", err) 1036 return 1037 } 1038 // Create the current work task and check any fork transitions needed 1039 env := w.current 1040 if w.chainConfig.DAOForkSupport && w.chainConfig.DAOForkBlock != nil && w.chainConfig.DAOForkBlock.Cmp(header.Number) == 0 { 1041 misc.ApplyDAOHardFork(env.state) 1042 } 1043 // Accumulate the uncles for the current block 1044 uncles := make([]*types.Header, 0, 2) 1045 commitUncles := func(blocks map[common.Hash]*types.Block) { 1046 // Clean up stale uncle blocks first 1047 for hash, uncle := range blocks { 1048 if uncle.NumberU64()+staleThreshold <= header.Number.Uint64() { 1049 delete(blocks, hash) 1050 } 1051 } 1052 for hash, uncle := range blocks { 1053 if len(uncles) == 2 { 1054 break 1055 } 1056 if err := w.commitUncle(env, uncle.Header()); err != nil { 1057 log.Trace("Possible uncle rejected", "hash", hash, "reason", err) 1058 } else { 1059 log.Debug("Committing new uncle to block", "hash", hash) 1060 uncles = append(uncles, uncle.Header()) 1061 } 1062 } 1063 } 1064 // Prefer to locally generated uncle 1065 commitUncles(w.localUncles) 1066 commitUncles(w.remoteUncles) 1067 1068 // Create an empty block based on temporary copied state for 1069 // sealing in advance without waiting block execution finished. 1070 if !noempty && atomic.LoadUint32(&w.noempty) == 0 { 1071 w.commit(uncles, nil, false, tstart) 1072 } 1073 1074 // Fill the block with all available pending transactions. 1075 pending, err := w.eth.TxPool().Pending() 1076 if err != nil { 1077 log.Error("Failed to fetch pending transactions", "err", err) 1078 return 1079 } 1080 // Short circuit if there is no available pending transactions. 1081 // But if we disable empty precommit already, ignore it. Since 1082 // empty block is necessary to keep the liveness of the network. 1083 if len(pending) == 0 && atomic.LoadUint32(&w.noempty) == 0 { 1084 w.updateSnapshot() 1085 return 1086 } 1087 // Split the pending transactions into locals and remotes 1088 localTxs, remoteTxs := make(map[common.Address]types.Transactions), pending 1089 for _, account := range w.eth.TxPool().Locals() { 1090 if txs := remoteTxs[account]; len(txs) > 0 { 1091 delete(remoteTxs, account) 1092 localTxs[account] = txs 1093 } 1094 } 1095 if len(localTxs) > 0 { 1096 txs := types.NewTransactionsByPriceAndNonce(w.current.signer, localTxs) 1097 if w.commitTransactions(txs, w.coinbase, interrupt) { 1098 return 1099 } 1100 } 1101 if len(remoteTxs) > 0 { 1102 txs := types.NewTransactionsByPriceAndNonce(w.current.signer, remoteTxs) 1103 if w.commitTransactions(txs, w.coinbase, interrupt) { 1104 return 1105 } 1106 } 1107 w.commit(uncles, w.fullTaskHook, true, tstart) 1108 } 1109 1110 // commit runs any post-transaction state modifications, assembles the final block 1111 // and commits new work if consensus engine is running. 1112 //commit运行任何事务后状态修改,组装最后一个块 1113 //如果共识引擎正在运行,则提交新工作。 1114 func (w *worker) commit(uncles []*types.Header, interval func(), update bool, start time.Time) error { 1115 // Deep copy receipts here to avoid interaction between different tasks. 1116 receipts := copyReceipts(w.current.receipts) 1117 privateReceipts := copyReceipts(w.current.privateReceipts) // Quorum 1118 1119 s := w.current.state.Copy() 1120 psrCopy := w.current.privateStateRepo.Copy() 1121 block, err := w.engine.FinalizeAndAssemble(w.chain, w.current.header, s, w.current.txs, uncles, receipts) 1122 if err != nil { 1123 return err 1124 } 1125 if w.isRunning() { 1126 if interval != nil { 1127 interval() 1128 } 1129 select { 1130 case w.taskCh <- &task{receipts: receipts, privateReceipts: privateReceipts, state: s, privateStateRepo: psrCopy, block: block, createdAt: time.Now()}: 1131 w.unconfirmed.Shift(block.NumberU64() - 1) 1132 log.Info("Commit new mining work", "number", block.Number(), "sealhash", w.engine.SealHash(block.Header()), 1133 "uncles", len(uncles), "txs", w.current.tcount, 1134 "gas", block.GasUsed(), "fees", totalFees(block, receipts), 1135 "elapsed", common.PrettyDuration(time.Since(start))) 1136 1137 case <-w.exitCh: 1138 log.Info("Worker has exited") 1139 } 1140 } 1141 if update { 1142 w.updateSnapshot() 1143 } 1144 return nil 1145 } 1146 1147 // copyReceipts makes a deep copy of the given receipts. 1148 func copyReceipts(receipts []*types.Receipt) []*types.Receipt { 1149 result := make([]*types.Receipt, len(receipts)) 1150 for i, l := range receipts { 1151 cpy := *l 1152 result[i] = &cpy 1153 } 1154 return result 1155 } 1156 1157 // postSideBlock fires a side chain event, only use it for testing. 1158 func (w *worker) postSideBlock(event core.ChainSideEvent) { 1159 select { 1160 case w.chainSideCh <- event: 1161 case <-w.exitCh: 1162 } 1163 } 1164 1165 // totalFees computes total consumed fees in ETH. Block transactions and receipts have to have the same order. 1166 func totalFees(block *types.Block, receipts []*types.Receipt) *big.Float { 1167 feesWei := new(big.Int) 1168 for i, tx := range block.Transactions() { 1169 feesWei.Add(feesWei, new(big.Int).Mul(new(big.Int).SetUint64(receipts[i].GasUsed), tx.GasPrice())) 1170 } 1171 return new(big.Float).Quo(new(big.Float).SetInt(feesWei), new(big.Float).SetInt(big.NewInt(params.Ether))) 1172 } 1173 1174 // Quorum 1175 // 1176 // revertToPrivateStateSnapshots attempts to revert all private states to the provided snapshots 1177 func (w *worker) revertToPrivateStateSnapshots(snapshots map[types.PrivateStateIdentifier]int) { 1178 for psi, snapshot := range snapshots { 1179 privateState, err := w.current.privateStateRepo.StatePSI(psi) 1180 if err == nil { 1181 privateState.RevertToSnapshot(snapshot) 1182 } else { 1183 log.Warn("unable to revert to snapshot", "psi", psi, "snapshot", snapshot, "err", err) 1184 } 1185 } 1186 } 1187 1188 // Quorum 1189 // 1190 // handling MPS scenario for a private transaction. It also captures the snapshot 1191 // before applying the transaction 1192 // 1193 // handleMPS returns the auxiliary receipt and the non-nil snapshots. 1194 // 1195 // Caller must check for error and reverts private states. 1196 func (w *worker) handleMPS(tx *types.Transaction, coinbase common.Address, applyOnPartyOnly bool) (mpsReceipt *types.Receipt, privateStateSnaphots map[types.PrivateStateIdentifier]int, err error) { 1197 workerEnv := w.current 1198 privateStateRepo := workerEnv.privateStateRepo 1199 // make sure we don't return NIL map 1200 privateStateSnaphots = make(map[types.PrivateStateIdentifier]int) 1201 if tx.IsPrivate() && privateStateRepo.IsMPS() { 1202 publicStateDBFactory := func() *state.StateDB { 1203 db := workerEnv.state.Copy() 1204 db.Prepare(tx.Hash(), common.Hash{}, workerEnv.tcount) 1205 return db 1206 } 1207 privateStateDBFactory := func(psi types.PrivateStateIdentifier) (*state.StateDB, error) { 1208 db, err := privateStateRepo.StatePSI(psi) 1209 if err != nil { 1210 return nil, err 1211 } 1212 db.Prepare(tx.Hash(), common.Hash{}, workerEnv.tcount) 1213 privateStateSnaphots[psi] = db.Snapshot() 1214 return db, nil 1215 } 1216 mpsReceipt, err = core.ApplyTransactionOnMPS(w.chainConfig, w.chain, &coinbase, workerEnv.gasPool, publicStateDBFactory, privateStateDBFactory, workerEnv.header, tx, &workerEnv.header.GasUsed, *w.chain.GetVMConfig(), privateStateRepo, applyOnPartyOnly) 1217 } 1218 return 1219 }