worker.go 31 KB

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