worker.go 34 KB

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