chain_manager.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. package core
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "math/big"
  7. "sync"
  8. "time"
  9. "github.com/ethereum/go-ethereum/common"
  10. "github.com/ethereum/go-ethereum/core/state"
  11. "github.com/ethereum/go-ethereum/core/types"
  12. "github.com/ethereum/go-ethereum/event"
  13. "github.com/ethereum/go-ethereum/logger"
  14. "github.com/ethereum/go-ethereum/logger/glog"
  15. "github.com/ethereum/go-ethereum/params"
  16. "github.com/ethereum/go-ethereum/rlp"
  17. )
  18. var (
  19. chainlogger = logger.NewLogger("CHAIN")
  20. jsonlogger = logger.NewJsonLogger()
  21. blockHashPre = []byte("block-hash-")
  22. blockNumPre = []byte("block-num-")
  23. )
  24. const (
  25. blockCacheLimit = 10000
  26. maxFutureBlocks = 256
  27. )
  28. func CalcDifficulty(block, parent *types.Header) *big.Int {
  29. diff := new(big.Int)
  30. adjust := new(big.Int).Div(parent.Difficulty, params.DifficultyBoundDivisor)
  31. if big.NewInt(int64(block.Time)-int64(parent.Time)).Cmp(params.DurationLimit) < 0 {
  32. diff.Add(parent.Difficulty, adjust)
  33. } else {
  34. diff.Sub(parent.Difficulty, adjust)
  35. }
  36. if diff.Cmp(params.MinimumDifficulty) < 0 {
  37. return params.MinimumDifficulty
  38. }
  39. return diff
  40. }
  41. func CalculateTD(block, parent *types.Block) *big.Int {
  42. td := new(big.Int).Add(parent.Td, block.Header().Difficulty)
  43. return td
  44. }
  45. func CalcGasLimit(parent *types.Block) *big.Int {
  46. // ((1024-1) * parent.gasLimit + (gasUsed * 6 / 5)) / 1024
  47. previous := new(big.Int).Mul(big.NewInt(1024-1), parent.GasLimit())
  48. current := new(big.Rat).Mul(new(big.Rat).SetInt(parent.GasUsed()), big.NewRat(6, 5))
  49. curInt := new(big.Int).Div(current.Num(), current.Denom())
  50. result := new(big.Int).Add(previous, curInt)
  51. result.Div(result, big.NewInt(1024))
  52. return common.BigMax(params.GenesisGasLimit, result)
  53. }
  54. type ChainManager struct {
  55. //eth EthManager
  56. blockDb common.Database
  57. stateDb common.Database
  58. processor types.BlockProcessor
  59. eventMux *event.TypeMux
  60. genesisBlock *types.Block
  61. // Last known total difficulty
  62. mu sync.RWMutex
  63. tsmu sync.RWMutex
  64. td *big.Int
  65. currentBlock *types.Block
  66. lastBlockHash common.Hash
  67. currentGasLimit *big.Int
  68. transState *state.StateDB
  69. txState *state.ManagedState
  70. cache *BlockCache
  71. futureBlocks *BlockCache
  72. quit chan struct{}
  73. }
  74. func NewChainManager(blockDb, stateDb common.Database, mux *event.TypeMux) *ChainManager {
  75. bc := &ChainManager{
  76. blockDb: blockDb,
  77. stateDb: stateDb,
  78. genesisBlock: GenesisBlock(stateDb),
  79. eventMux: mux,
  80. quit: make(chan struct{}),
  81. cache: NewBlockCache(blockCacheLimit),
  82. }
  83. bc.setLastState()
  84. // Check the current state of the block hashes and make sure that we do not have any of the bad blocks in our chain
  85. for _, hash := range badHashes {
  86. if block := bc.GetBlock(hash); block != nil {
  87. glog.V(logger.Error).Infof("Found bad hash. Reorganising chain to state %x\n", block.ParentHash().Bytes()[:4])
  88. block = bc.GetBlock(block.ParentHash())
  89. if block == nil {
  90. glog.Fatal("Unable to complete. Parent block not found. Corrupted DB?")
  91. }
  92. bc.SetHead(block)
  93. glog.V(logger.Error).Infoln("Chain reorg was successfull. Resuming normal operation")
  94. }
  95. }
  96. bc.transState = bc.State().Copy()
  97. // Take ownership of this particular state
  98. bc.txState = state.ManageState(bc.State().Copy())
  99. bc.futureBlocks = NewBlockCache(maxFutureBlocks)
  100. bc.makeCache()
  101. go bc.update()
  102. return bc
  103. }
  104. func (bc *ChainManager) SetHead(head *types.Block) {
  105. bc.mu.Lock()
  106. defer bc.mu.Unlock()
  107. for block := bc.currentBlock; block != nil && block.Hash() != head.Hash(); block = bc.GetBlock(block.Header().ParentHash) {
  108. bc.removeBlock(block)
  109. }
  110. bc.cache = NewBlockCache(blockCacheLimit)
  111. bc.currentBlock = head
  112. bc.makeCache()
  113. statedb := state.New(head.Root(), bc.stateDb)
  114. bc.txState = state.ManageState(statedb)
  115. bc.transState = statedb.Copy()
  116. bc.setTotalDifficulty(head.Td)
  117. bc.insert(head)
  118. bc.setLastState()
  119. }
  120. func (self *ChainManager) Td() *big.Int {
  121. self.mu.RLock()
  122. defer self.mu.RUnlock()
  123. return self.td
  124. }
  125. func (self *ChainManager) GasLimit() *big.Int {
  126. return self.currentGasLimit
  127. }
  128. func (self *ChainManager) LastBlockHash() common.Hash {
  129. self.mu.RLock()
  130. defer self.mu.RUnlock()
  131. return self.lastBlockHash
  132. }
  133. func (self *ChainManager) CurrentBlock() *types.Block {
  134. self.mu.RLock()
  135. defer self.mu.RUnlock()
  136. return self.currentBlock
  137. }
  138. func (self *ChainManager) Status() (td *big.Int, currentBlock common.Hash, genesisBlock common.Hash) {
  139. self.mu.RLock()
  140. defer self.mu.RUnlock()
  141. return self.td, self.currentBlock.Hash(), self.genesisBlock.Hash()
  142. }
  143. func (self *ChainManager) SetProcessor(proc types.BlockProcessor) {
  144. self.processor = proc
  145. }
  146. func (self *ChainManager) State() *state.StateDB {
  147. return state.New(self.CurrentBlock().Root(), self.stateDb)
  148. }
  149. func (self *ChainManager) TransState() *state.StateDB {
  150. self.tsmu.RLock()
  151. defer self.tsmu.RUnlock()
  152. return self.transState
  153. }
  154. func (self *ChainManager) TxState() *state.ManagedState {
  155. self.tsmu.RLock()
  156. defer self.tsmu.RUnlock()
  157. return self.txState
  158. }
  159. func (self *ChainManager) setTxState(statedb *state.StateDB) {
  160. self.tsmu.Lock()
  161. defer self.tsmu.Unlock()
  162. self.txState = state.ManageState(statedb)
  163. }
  164. func (self *ChainManager) setTransState(statedb *state.StateDB) {
  165. self.transState = statedb
  166. }
  167. func (bc *ChainManager) setLastState() {
  168. data, _ := bc.blockDb.Get([]byte("LastBlock"))
  169. if len(data) != 0 {
  170. block := bc.GetBlock(common.BytesToHash(data))
  171. bc.currentBlock = block
  172. bc.lastBlockHash = block.Hash()
  173. // Set the last know difficulty (might be 0x0 as initial value, Genesis)
  174. bc.td = common.BigD(bc.blockDb.LastKnownTD())
  175. } else {
  176. bc.Reset()
  177. }
  178. bc.currentGasLimit = CalcGasLimit(bc.currentBlock)
  179. if glog.V(logger.Info) {
  180. glog.Infof("Last block (#%v) %x TD=%v\n", bc.currentBlock.Number(), bc.currentBlock.Hash(), bc.td)
  181. }
  182. }
  183. func (bc *ChainManager) makeCache() {
  184. if bc.cache == nil {
  185. bc.cache = NewBlockCache(blockCacheLimit)
  186. }
  187. // load in last `blockCacheLimit` - 1 blocks. Last block is the current.
  188. ancestors := bc.GetAncestors(bc.currentBlock, blockCacheLimit-1)
  189. ancestors = append(ancestors, bc.currentBlock)
  190. for _, block := range ancestors {
  191. bc.cache.Push(block)
  192. }
  193. }
  194. // Block creation & chain handling
  195. func (bc *ChainManager) NewBlock(coinbase common.Address) *types.Block {
  196. bc.mu.RLock()
  197. defer bc.mu.RUnlock()
  198. var (
  199. root common.Hash
  200. parentHash common.Hash
  201. )
  202. if bc.currentBlock != nil {
  203. root = bc.currentBlock.Header().Root
  204. parentHash = bc.lastBlockHash
  205. }
  206. block := types.NewBlock(
  207. parentHash,
  208. coinbase,
  209. root,
  210. common.BigPow(2, 32),
  211. 0,
  212. nil)
  213. block.SetUncles(nil)
  214. block.SetTransactions(nil)
  215. block.SetReceipts(nil)
  216. parent := bc.currentBlock
  217. if parent != nil {
  218. header := block.Header()
  219. header.Difficulty = CalcDifficulty(block.Header(), parent.Header())
  220. header.Number = new(big.Int).Add(parent.Header().Number, common.Big1)
  221. header.GasLimit = CalcGasLimit(parent)
  222. }
  223. return block
  224. }
  225. func (bc *ChainManager) Reset() {
  226. bc.mu.Lock()
  227. defer bc.mu.Unlock()
  228. for block := bc.currentBlock; block != nil; block = bc.GetBlock(block.Header().ParentHash) {
  229. bc.removeBlock(block)
  230. }
  231. if bc.cache == nil {
  232. bc.cache = NewBlockCache(blockCacheLimit)
  233. }
  234. // Prepare the genesis block
  235. bc.write(bc.genesisBlock)
  236. bc.insert(bc.genesisBlock)
  237. bc.currentBlock = bc.genesisBlock
  238. bc.makeCache()
  239. bc.setTotalDifficulty(common.Big("0"))
  240. }
  241. func (bc *ChainManager) removeBlock(block *types.Block) {
  242. bc.blockDb.Delete(append(blockHashPre, block.Hash().Bytes()...))
  243. }
  244. func (bc *ChainManager) ResetWithGenesisBlock(gb *types.Block) {
  245. bc.mu.Lock()
  246. defer bc.mu.Unlock()
  247. for block := bc.currentBlock; block != nil; block = bc.GetBlock(block.Header().ParentHash) {
  248. bc.removeBlock(block)
  249. }
  250. // Prepare the genesis block
  251. bc.genesisBlock = gb
  252. bc.write(bc.genesisBlock)
  253. bc.insert(bc.genesisBlock)
  254. bc.currentBlock = bc.genesisBlock
  255. bc.makeCache()
  256. bc.td = gb.Difficulty()
  257. }
  258. // Export writes the active chain to the given writer.
  259. func (self *ChainManager) Export(w io.Writer) error {
  260. self.mu.RLock()
  261. defer self.mu.RUnlock()
  262. glog.V(logger.Info).Infof("exporting %v blocks...\n", self.currentBlock.Header().Number)
  263. last := self.currentBlock.NumberU64()
  264. for nr := uint64(0); nr <= last; nr++ {
  265. block := self.GetBlockByNumber(nr)
  266. if block == nil {
  267. return fmt.Errorf("export failed on #%d: not found", nr)
  268. }
  269. if err := block.EncodeRLP(w); err != nil {
  270. return err
  271. }
  272. }
  273. return nil
  274. }
  275. func (bc *ChainManager) insert(block *types.Block) {
  276. key := append(blockNumPre, block.Number().Bytes()...)
  277. bc.blockDb.Put(key, block.Hash().Bytes())
  278. bc.blockDb.Put([]byte("LastBlock"), block.Hash().Bytes())
  279. bc.currentBlock = block
  280. bc.lastBlockHash = block.Hash()
  281. }
  282. func (bc *ChainManager) write(block *types.Block) {
  283. enc, _ := rlp.EncodeToBytes((*types.StorageBlock)(block))
  284. key := append(blockHashPre, block.Hash().Bytes()...)
  285. bc.blockDb.Put(key, enc)
  286. // Push block to cache
  287. bc.cache.Push(block)
  288. }
  289. // Accessors
  290. func (bc *ChainManager) Genesis() *types.Block {
  291. return bc.genesisBlock
  292. }
  293. // Block fetching methods
  294. func (bc *ChainManager) HasBlock(hash common.Hash) bool {
  295. data, _ := bc.blockDb.Get(append(blockHashPre, hash[:]...))
  296. return len(data) != 0
  297. }
  298. func (self *ChainManager) GetBlockHashesFromHash(hash common.Hash, max uint64) (chain []common.Hash) {
  299. block := self.GetBlock(hash)
  300. if block == nil {
  301. return
  302. }
  303. // XXX Could be optimised by using a different database which only holds hashes (i.e., linked list)
  304. for i := uint64(0); i < max; i++ {
  305. block = self.GetBlock(block.ParentHash())
  306. if block == nil {
  307. break
  308. }
  309. chain = append(chain, block.Hash())
  310. if block.Number().Cmp(common.Big0) <= 0 {
  311. break
  312. }
  313. }
  314. return
  315. }
  316. func (self *ChainManager) GetBlock(hash common.Hash) *types.Block {
  317. if block := self.cache.Get(hash); block != nil {
  318. return block
  319. }
  320. data, _ := self.blockDb.Get(append(blockHashPre, hash[:]...))
  321. if len(data) == 0 {
  322. return nil
  323. }
  324. var block types.StorageBlock
  325. if err := rlp.Decode(bytes.NewReader(data), &block); err != nil {
  326. glog.V(logger.Error).Infof("invalid block RLP for hash %x: %v", hash, err)
  327. return nil
  328. }
  329. return (*types.Block)(&block)
  330. }
  331. func (self *ChainManager) GetBlockByNumber(num uint64) *types.Block {
  332. self.mu.RLock()
  333. defer self.mu.RUnlock()
  334. return self.getBlockByNumber(num)
  335. }
  336. // non blocking version
  337. func (self *ChainManager) getBlockByNumber(num uint64) *types.Block {
  338. key, _ := self.blockDb.Get(append(blockNumPre, big.NewInt(int64(num)).Bytes()...))
  339. if len(key) == 0 {
  340. return nil
  341. }
  342. return self.GetBlock(common.BytesToHash(key))
  343. }
  344. func (self *ChainManager) GetUnclesInChain(block *types.Block, length int) (uncles []*types.Header) {
  345. for i := 0; block != nil && i < length; i++ {
  346. uncles = append(uncles, block.Uncles()...)
  347. block = self.GetBlock(block.ParentHash())
  348. }
  349. return
  350. }
  351. func (self *ChainManager) GetAncestors(block *types.Block, length int) (blocks []*types.Block) {
  352. for i := 0; i < length; i++ {
  353. block = self.GetBlock(block.ParentHash())
  354. if block == nil {
  355. break
  356. }
  357. blocks = append(blocks, block)
  358. }
  359. return
  360. }
  361. func (bc *ChainManager) setTotalDifficulty(td *big.Int) {
  362. bc.blockDb.Put([]byte("LTD"), td.Bytes())
  363. bc.td = td
  364. }
  365. func (self *ChainManager) CalcTotalDiff(block *types.Block) (*big.Int, error) {
  366. parent := self.GetBlock(block.Header().ParentHash)
  367. if parent == nil {
  368. return nil, fmt.Errorf("Unable to calculate total diff without known parent %x", block.Header().ParentHash)
  369. }
  370. parentTd := parent.Td
  371. uncleDiff := new(big.Int)
  372. for _, uncle := range block.Uncles() {
  373. uncleDiff = uncleDiff.Add(uncleDiff, uncle.Difficulty)
  374. }
  375. td := new(big.Int)
  376. td = td.Add(parentTd, uncleDiff)
  377. td = td.Add(td, block.Header().Difficulty)
  378. return td, nil
  379. }
  380. func (bc *ChainManager) Stop() {
  381. close(bc.quit)
  382. }
  383. type queueEvent struct {
  384. queue []interface{}
  385. canonicalCount int
  386. sideCount int
  387. splitCount int
  388. }
  389. func (self *ChainManager) procFutureBlocks() {
  390. blocks := make([]*types.Block, len(self.futureBlocks.blocks))
  391. self.futureBlocks.Each(func(i int, block *types.Block) {
  392. blocks[i] = block
  393. })
  394. types.BlockBy(types.Number).Sort(blocks)
  395. self.InsertChain(blocks)
  396. }
  397. func (self *ChainManager) InsertChain(chain types.Blocks) error {
  398. // A queued approach to delivering events. This is generally faster than direct delivery and requires much less mutex acquiring.
  399. var (
  400. queue = make([]interface{}, len(chain))
  401. queueEvent = queueEvent{queue: queue}
  402. stats struct{ queued, processed int }
  403. tstart = time.Now()
  404. )
  405. for i, block := range chain {
  406. if block == nil {
  407. continue
  408. }
  409. // Call in to the block processor and check for errors. It's likely that if one block fails
  410. // all others will fail too (unless a known block is returned).
  411. logs, err := self.processor.Process(block)
  412. if err != nil {
  413. if IsKnownBlockErr(err) {
  414. continue
  415. }
  416. block.Td = new(big.Int)
  417. // Do not penelise on future block. We'll need a block queue eventually that will queue
  418. // future block for future use
  419. if err == BlockFutureErr {
  420. block.SetQueued(true)
  421. self.futureBlocks.Push(block)
  422. stats.queued++
  423. continue
  424. }
  425. if IsParentErr(err) && self.futureBlocks.Has(block.ParentHash()) {
  426. block.SetQueued(true)
  427. self.futureBlocks.Push(block)
  428. stats.queued++
  429. continue
  430. }
  431. h := block.Header()
  432. glog.V(logger.Error).Infof("INVALID block #%v (%x)\n", h.Number, h.Hash().Bytes())
  433. glog.V(logger.Error).Infoln(err)
  434. glog.V(logger.Debug).Infoln(block)
  435. return err
  436. }
  437. block.Td = new(big.Int).Set(CalculateTD(block, self.GetBlock(block.ParentHash())))
  438. self.mu.Lock()
  439. {
  440. cblock := self.currentBlock
  441. // Write block to database. Eventually we'll have to improve on this and throw away blocks that are
  442. // not in the canonical chain.
  443. self.write(block)
  444. // Compare the TD of the last known block in the canonical chain to make sure it's greater.
  445. // At this point it's possible that a different chain (fork) becomes the new canonical chain.
  446. if block.Td.Cmp(self.td) > 0 {
  447. // Check for chain forks. If H(block.num - 1) != block.parent, we're on a fork and need to do some merging
  448. if previous := self.getBlockByNumber(block.NumberU64() - 1); previous.Hash() != block.ParentHash() {
  449. chash := cblock.Hash()
  450. hash := block.Hash()
  451. if glog.V(logger.Info) {
  452. glog.Infof("Split detected. New head #%v (%x) TD=%v, was #%v (%x) TD=%v\n", block.Header().Number, hash[:4], block.Td, cblock.Header().Number, chash[:4], self.td)
  453. }
  454. // during split we merge two different chains and create the new canonical chain
  455. self.merge(previous, block)
  456. queue[i] = ChainSplitEvent{block, logs}
  457. queueEvent.splitCount++
  458. }
  459. self.setTotalDifficulty(block.Td)
  460. self.insert(block)
  461. jsonlogger.LogJson(&logger.EthChainNewHead{
  462. BlockHash: block.Hash().Hex(),
  463. BlockNumber: block.Number(),
  464. ChainHeadHash: cblock.Hash().Hex(),
  465. BlockPrevHash: block.ParentHash().Hex(),
  466. })
  467. self.setTransState(state.New(block.Root(), self.stateDb))
  468. self.txState.SetState(state.New(block.Root(), self.stateDb))
  469. queue[i] = ChainEvent{block, logs}
  470. queueEvent.canonicalCount++
  471. if glog.V(logger.Debug) {
  472. glog.Infof("inserted block #%d (%d TXs %d UNCs) (%x...)\n", block.Number(), len(block.Transactions()), len(block.Uncles()), block.Hash().Bytes()[0:4])
  473. }
  474. } else {
  475. if glog.V(logger.Detail) {
  476. glog.Infof("inserted forked block #%d (%d TXs %d UNCs) (%x...)\n", block.Number(), len(block.Transactions()), len(block.Uncles()), block.Hash().Bytes()[0:4])
  477. }
  478. queue[i] = ChainSideEvent{block, logs}
  479. queueEvent.sideCount++
  480. }
  481. self.futureBlocks.Delete(block.Hash())
  482. }
  483. self.mu.Unlock()
  484. stats.processed++
  485. }
  486. if (stats.queued > 0 || stats.processed > 0) && bool(glog.V(logger.Info)) {
  487. tend := time.Since(tstart)
  488. start, end := chain[0], chain[len(chain)-1]
  489. glog.Infof("imported %d block(s) %d queued in %v. #%v [%x / %x]\n", stats.processed, stats.queued, tend, end.Number(), start.Hash().Bytes()[:4], end.Hash().Bytes()[:4])
  490. }
  491. go self.eventMux.Post(queueEvent)
  492. return nil
  493. }
  494. // diff takes two blocks, an old chain and a new chain and will reconstruct the blocks and inserts them
  495. // to be part of the new canonical chain.
  496. func (self *ChainManager) diff(oldBlock, newBlock *types.Block) types.Blocks {
  497. glog.V(logger.Debug).Infof("Applying diff to %x & %x\n", oldBlock.Hash().Bytes()[:4], newBlock.Hash().Bytes()[:4])
  498. var newChain types.Blocks
  499. // first find common number
  500. for newBlock = newBlock; newBlock.NumberU64() != oldBlock.NumberU64(); newBlock = self.GetBlock(newBlock.ParentHash()) {
  501. newChain = append(newChain, newBlock)
  502. }
  503. glog.V(logger.Debug).Infoln("Found common number", newBlock.Number())
  504. for {
  505. if oldBlock.Hash() == newBlock.Hash() {
  506. break
  507. }
  508. newChain = append(newChain, newBlock)
  509. oldBlock, newBlock = self.GetBlock(oldBlock.ParentHash()), self.GetBlock(newBlock.ParentHash())
  510. }
  511. return newChain
  512. }
  513. // merge merges two different chain to the new canonical chain
  514. func (self *ChainManager) merge(oldBlock, newBlock *types.Block) {
  515. newChain := self.diff(oldBlock, newBlock)
  516. // insert blocks
  517. for _, block := range newChain {
  518. self.insert(block)
  519. }
  520. }
  521. func (self *ChainManager) update() {
  522. events := self.eventMux.Subscribe(queueEvent{})
  523. futureTimer := time.NewTicker(5 * time.Second)
  524. out:
  525. for {
  526. select {
  527. case ev := <-events.Chan():
  528. switch ev := ev.(type) {
  529. case queueEvent:
  530. for i, event := range ev.queue {
  531. switch event := event.(type) {
  532. case ChainEvent:
  533. // We need some control over the mining operation. Acquiring locks and waiting for the miner to create new block takes too long
  534. // and in most cases isn't even necessary.
  535. if i+1 == ev.canonicalCount {
  536. self.currentGasLimit = CalcGasLimit(event.Block)
  537. self.eventMux.Post(ChainHeadEvent{event.Block})
  538. }
  539. case ChainSplitEvent:
  540. // On chain splits we need to reset the transaction state. We can't be sure whether the actual
  541. // state of the accounts are still valid.
  542. if i == ev.splitCount {
  543. self.setTxState(state.New(event.Block.Root(), self.stateDb))
  544. }
  545. }
  546. self.eventMux.Post(event)
  547. }
  548. }
  549. case <-futureTimer.C:
  550. self.procFutureBlocks()
  551. case <-self.quit:
  552. break out
  553. }
  554. }
  555. }