chain_manager.go 18 KB

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