chain_manager.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. package core
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "math/big"
  7. "runtime"
  8. "sync"
  9. "sync/atomic"
  10. "time"
  11. "github.com/ethereum/go-ethereum/common"
  12. "github.com/ethereum/go-ethereum/compression/rle"
  13. "github.com/ethereum/go-ethereum/core/state"
  14. "github.com/ethereum/go-ethereum/core/types"
  15. "github.com/ethereum/go-ethereum/ethdb"
  16. "github.com/ethereum/go-ethereum/event"
  17. "github.com/ethereum/go-ethereum/logger"
  18. "github.com/ethereum/go-ethereum/logger/glog"
  19. "github.com/ethereum/go-ethereum/params"
  20. "github.com/ethereum/go-ethereum/pow"
  21. "github.com/ethereum/go-ethereum/rlp"
  22. "github.com/hashicorp/golang-lru"
  23. "github.com/syndtr/goleveldb/leveldb"
  24. )
  25. var (
  26. chainlogger = logger.NewLogger("CHAIN")
  27. jsonlogger = logger.NewJsonLogger()
  28. blockHashPre = []byte("block-hash-")
  29. blockNumPre = []byte("block-num-")
  30. blockInsertTimer = metrics.GetOrRegisterTimer("core/BlockInsertions", metrics.DefaultRegistry)
  31. )
  32. const (
  33. blockCacheLimit = 256
  34. maxFutureBlocks = 256
  35. maxTimeFutureBlocks = 30
  36. )
  37. // CalcDifficulty is the difficulty adjustment algorithm. It returns
  38. // the difficulty that a new block b should have when created at time
  39. // given the parent block's time and difficulty.
  40. func CalcDifficulty(time int64, parentTime int64, parentDiff *big.Int) *big.Int {
  41. diff := new(big.Int)
  42. adjust := new(big.Int).Div(parentDiff, params.DifficultyBoundDivisor)
  43. if big.NewInt(time-parentTime).Cmp(params.DurationLimit) < 0 {
  44. diff.Add(parentDiff, adjust)
  45. } else {
  46. diff.Sub(parentDiff, adjust)
  47. }
  48. if diff.Cmp(params.MinimumDifficulty) < 0 {
  49. return params.MinimumDifficulty
  50. }
  51. return diff
  52. }
  53. // CalcTD computes the total difficulty of block.
  54. func CalcTD(block, parent *types.Block) *big.Int {
  55. if parent == nil {
  56. return block.Difficulty()
  57. }
  58. d := block.Difficulty()
  59. d.Add(d, parent.Td)
  60. return d
  61. }
  62. // CalcGasLimit computes the gas limit of the next block after parent.
  63. // The result may be modified by the caller.
  64. func CalcGasLimit(parent *types.Block) *big.Int {
  65. decay := new(big.Int).Div(parent.GasLimit(), params.GasLimitBoundDivisor)
  66. contrib := new(big.Int).Mul(parent.GasUsed(), big.NewInt(3))
  67. contrib = contrib.Div(contrib, big.NewInt(2))
  68. contrib = contrib.Div(contrib, params.GasLimitBoundDivisor)
  69. gl := new(big.Int).Sub(parent.GasLimit(), decay)
  70. gl = gl.Add(gl, contrib)
  71. gl = gl.Add(gl, big.NewInt(1))
  72. gl.Set(common.BigMax(gl, params.MinGasLimit))
  73. if gl.Cmp(params.GenesisGasLimit) < 0 {
  74. gl.Add(parent.GasLimit(), decay)
  75. gl.Set(common.BigMin(gl, params.GenesisGasLimit))
  76. }
  77. return gl
  78. }
  79. type ChainManager struct {
  80. //eth EthManager
  81. blockDb common.Database
  82. stateDb common.Database
  83. processor types.BlockProcessor
  84. eventMux *event.TypeMux
  85. genesisBlock *types.Block
  86. // Last known total difficulty
  87. mu sync.RWMutex
  88. chainmu sync.RWMutex
  89. tsmu sync.RWMutex
  90. td *big.Int
  91. currentBlock *types.Block
  92. lastBlockHash common.Hash
  93. currentGasLimit *big.Int
  94. transState *state.StateDB
  95. txState *state.ManagedState
  96. cache *lru.Cache // cache is the LRU caching
  97. futureBlocks *BlockCache // future blocks are blocks added for later processing
  98. pendingBlocks *BlockCache // pending blocks contain blocks not yet written to the db
  99. quit chan struct{}
  100. // procInterrupt must be atomically called
  101. procInterrupt int32 // interrupt signaler for block processing
  102. wg sync.WaitGroup
  103. pow pow.PoW
  104. }
  105. func NewChainManager(genesis *types.Block, blockDb, stateDb common.Database, pow pow.PoW, mux *event.TypeMux) (*ChainManager, error) {
  106. cache, _ := lru.New(blockCacheLimit)
  107. bc := &ChainManager{
  108. blockDb: blockDb,
  109. stateDb: stateDb,
  110. genesisBlock: GenesisBlock(42, stateDb),
  111. eventMux: mux,
  112. quit: make(chan struct{}),
  113. cache: cache,
  114. pow: pow,
  115. }
  116. // Check the genesis block given to the chain manager. If the genesis block mismatches block number 0
  117. // throw an error. If no block or the same block's found continue.
  118. if g := bc.GetBlockByNumber(0); g != nil && g.Hash() != genesis.Hash() {
  119. return nil, fmt.Errorf("Genesis mismatch. Maybe different nonce (%d vs %d)? %x / %x", g.Nonce(), genesis.Nonce(), g.Hash().Bytes()[:4], genesis.Hash().Bytes()[:4])
  120. }
  121. bc.genesisBlock = genesis
  122. bc.setLastState()
  123. // Check the current state of the block hashes and make sure that we do not have any of the bad blocks in our chain
  124. for hash, _ := range BadHashes {
  125. if block := bc.GetBlock(hash); block != nil {
  126. glog.V(logger.Error).Infof("Found bad hash. Reorganising chain to state %x\n", block.ParentHash().Bytes()[:4])
  127. block = bc.GetBlock(block.ParentHash())
  128. if block == nil {
  129. glog.Fatal("Unable to complete. Parent block not found. Corrupted DB?")
  130. }
  131. bc.SetHead(block)
  132. glog.V(logger.Error).Infoln("Chain reorg was successfull. Resuming normal operation")
  133. }
  134. }
  135. bc.transState = bc.State().Copy()
  136. // Take ownership of this particular state
  137. bc.txState = state.ManageState(bc.State().Copy())
  138. bc.futureBlocks = NewBlockCache(maxFutureBlocks)
  139. bc.makeCache()
  140. go bc.update()
  141. return bc, nil
  142. }
  143. func (bc *ChainManager) SetHead(head *types.Block) {
  144. bc.mu.Lock()
  145. defer bc.mu.Unlock()
  146. for block := bc.currentBlock; block != nil && block.Hash() != head.Hash(); block = bc.GetBlock(block.ParentHash()) {
  147. bc.removeBlock(block)
  148. }
  149. bc.cache, _ = lru.New(blockCacheLimit)
  150. bc.currentBlock = head
  151. bc.makeCache()
  152. statedb := state.New(head.Root(), bc.stateDb)
  153. bc.txState = state.ManageState(statedb)
  154. bc.transState = statedb.Copy()
  155. bc.setTotalDifficulty(head.Td)
  156. bc.insert(head)
  157. bc.setLastState()
  158. }
  159. func (self *ChainManager) Td() *big.Int {
  160. self.mu.RLock()
  161. defer self.mu.RUnlock()
  162. return new(big.Int).Set(self.td)
  163. }
  164. func (self *ChainManager) GasLimit() *big.Int {
  165. self.mu.RLock()
  166. defer self.mu.RUnlock()
  167. return self.currentBlock.GasLimit()
  168. }
  169. func (self *ChainManager) LastBlockHash() common.Hash {
  170. self.mu.RLock()
  171. defer self.mu.RUnlock()
  172. return self.lastBlockHash
  173. }
  174. func (self *ChainManager) CurrentBlock() *types.Block {
  175. self.mu.RLock()
  176. defer self.mu.RUnlock()
  177. return self.currentBlock
  178. }
  179. func (self *ChainManager) Status() (td *big.Int, currentBlock common.Hash, genesisBlock common.Hash) {
  180. self.mu.RLock()
  181. defer self.mu.RUnlock()
  182. return new(big.Int).Set(self.td), self.currentBlock.Hash(), self.genesisBlock.Hash()
  183. }
  184. func (self *ChainManager) SetProcessor(proc types.BlockProcessor) {
  185. self.processor = proc
  186. }
  187. func (self *ChainManager) State() *state.StateDB {
  188. return state.New(self.CurrentBlock().Root(), self.stateDb)
  189. }
  190. func (self *ChainManager) TransState() *state.StateDB {
  191. self.tsmu.RLock()
  192. defer self.tsmu.RUnlock()
  193. return self.transState
  194. }
  195. func (self *ChainManager) setTransState(statedb *state.StateDB) {
  196. self.transState = statedb
  197. }
  198. func (bc *ChainManager) setLastState() {
  199. data, _ := bc.blockDb.Get([]byte("LastBlock"))
  200. if len(data) != 0 {
  201. block := bc.GetBlock(common.BytesToHash(data))
  202. if block != nil {
  203. bc.currentBlock = block
  204. bc.lastBlockHash = block.Hash()
  205. } else {
  206. glog.Fatalf("Fatal. LastBlock not found. Please run removedb and resync")
  207. }
  208. } else {
  209. bc.Reset()
  210. }
  211. bc.td = bc.currentBlock.Td
  212. bc.currentGasLimit = CalcGasLimit(bc.currentBlock)
  213. if glog.V(logger.Info) {
  214. glog.Infof("Last block (#%v) %x TD=%v\n", bc.currentBlock.Number(), bc.currentBlock.Hash(), bc.td)
  215. }
  216. }
  217. func (bc *ChainManager) makeCache() {
  218. bc.cache, _ = lru.New(blockCacheLimit)
  219. // load in last `blockCacheLimit` - 1 blocks. Last block is the current.
  220. ancestors := bc.GetAncestors(bc.currentBlock, blockCacheLimit-1)
  221. ancestors = append(ancestors, bc.currentBlock)
  222. for _, block := range ancestors {
  223. bc.cache.Add(block.Hash(), block)
  224. }
  225. }
  226. func (bc *ChainManager) Reset() {
  227. bc.mu.Lock()
  228. defer bc.mu.Unlock()
  229. for block := bc.currentBlock; block != nil; block = bc.GetBlock(block.ParentHash()) {
  230. bc.removeBlock(block)
  231. }
  232. bc.cache, _ = lru.New(blockCacheLimit)
  233. // Prepare the genesis block
  234. bc.write(bc.genesisBlock)
  235. bc.insert(bc.genesisBlock)
  236. bc.currentBlock = bc.genesisBlock
  237. bc.makeCache()
  238. bc.setTotalDifficulty(common.Big("0"))
  239. }
  240. func (bc *ChainManager) removeBlock(block *types.Block) {
  241. bc.blockDb.Delete(append(blockHashPre, block.Hash().Bytes()...))
  242. }
  243. func (bc *ChainManager) ResetWithGenesisBlock(gb *types.Block) {
  244. bc.mu.Lock()
  245. defer bc.mu.Unlock()
  246. for block := bc.currentBlock; block != nil; block = bc.GetBlock(block.ParentHash()) {
  247. bc.removeBlock(block)
  248. }
  249. // Prepare the genesis block
  250. gb.Td = gb.Difficulty()
  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. if err := self.ExportN(w, uint64(0), self.currentBlock.NumberU64()); err != nil {
  261. return err
  262. }
  263. return nil
  264. }
  265. // ExportN writes a subset of the active chain to the given writer.
  266. func (self *ChainManager) ExportN(w io.Writer, first uint64, last uint64) error {
  267. self.mu.RLock()
  268. defer self.mu.RUnlock()
  269. if first > last {
  270. return fmt.Errorf("export failed: first (%d) is greater than last (%d)", first, last)
  271. }
  272. glog.V(logger.Info).Infof("exporting %d blocks...\n", last-first+1)
  273. for nr := first; nr <= last; nr++ {
  274. block := self.GetBlockByNumber(nr)
  275. if block == nil {
  276. return fmt.Errorf("export failed on #%d: not found", nr)
  277. }
  278. if err := block.EncodeRLP(w); err != nil {
  279. return err
  280. }
  281. }
  282. return nil
  283. }
  284. // insert injects a block into the current chain block chain. Note, this function
  285. // assumes that the `mu` mutex is held!
  286. func (bc *ChainManager) insert(block *types.Block) {
  287. key := append(blockNumPre, block.Number().Bytes()...)
  288. err := bc.blockDb.Put(key, block.Hash().Bytes())
  289. if err != nil {
  290. glog.Fatal("db write fail:", err)
  291. }
  292. err = bc.blockDb.Put([]byte("LastBlock"), block.Hash().Bytes())
  293. if err != nil {
  294. glog.Fatal("db write fail:", err)
  295. }
  296. bc.currentBlock = block
  297. bc.lastBlockHash = block.Hash()
  298. }
  299. func (bc *ChainManager) write(block *types.Block) {
  300. tstart := time.Now()
  301. go func() {
  302. enc, _ := rlp.EncodeToBytes((*types.StorageBlock)(block))
  303. key := append(blockHashPre, block.Hash().Bytes()...)
  304. err := bc.blockDb.Put(key, enc)
  305. if err != nil {
  306. glog.Fatal("db write fail:", err)
  307. }
  308. }()
  309. if glog.V(logger.Debug) {
  310. glog.Infof("wrote block #%v %s. Took %v\n", block.Number(), common.PP(block.Hash().Bytes()), time.Since(tstart))
  311. }
  312. }
  313. // Accessors
  314. func (bc *ChainManager) Genesis() *types.Block {
  315. return bc.genesisBlock
  316. }
  317. // Block fetching methods
  318. func (bc *ChainManager) HasBlock(hash common.Hash) bool {
  319. if bc.cache.Contains(hash) {
  320. return true
  321. }
  322. if bc.pendingBlocks != nil {
  323. if block := bc.pendingBlocks.Get(hash); block != nil {
  324. return true
  325. }
  326. }
  327. data, _ := bc.blockDb.Get(append(blockHashPre, hash[:]...))
  328. return len(data) != 0
  329. }
  330. func (self *ChainManager) GetBlockHashesFromHash(hash common.Hash, max uint64) (chain []common.Hash) {
  331. block := self.GetBlock(hash)
  332. if block == nil {
  333. return
  334. }
  335. // XXX Could be optimised by using a different database which only holds hashes (i.e., linked list)
  336. for i := uint64(0); i < max; i++ {
  337. block = self.GetBlock(block.ParentHash())
  338. if block == nil {
  339. break
  340. }
  341. chain = append(chain, block.Hash())
  342. if block.Number().Cmp(common.Big0) <= 0 {
  343. break
  344. }
  345. }
  346. return
  347. }
  348. func (self *ChainManager) GetBlock(hash common.Hash) *types.Block {
  349. if block, ok := self.cache.Get(hash); ok {
  350. return block.(*types.Block)
  351. }
  352. if self.pendingBlocks != nil {
  353. if block := self.pendingBlocks.Get(hash); block != nil {
  354. return block
  355. }
  356. }
  357. data, _ := self.blockDb.Get(append(blockHashPre, hash[:]...))
  358. if len(data) == 0 {
  359. return nil
  360. }
  361. var block types.StorageBlock
  362. if err := rlp.Decode(bytes.NewReader(data), &block); err != nil {
  363. glog.V(logger.Error).Infof("invalid block RLP for hash %x: %v", hash, err)
  364. return nil
  365. }
  366. // Add the block to the cache
  367. self.cache.Add(hash, (*types.Block)(&block))
  368. return (*types.Block)(&block)
  369. }
  370. func (self *ChainManager) GetBlockByNumber(num uint64) *types.Block {
  371. self.mu.RLock()
  372. defer self.mu.RUnlock()
  373. return self.getBlockByNumber(num)
  374. }
  375. // GetBlocksFromHash returns the block corresponding to hash and up to n-1 ancestors.
  376. func (self *ChainManager) GetBlocksFromHash(hash common.Hash, n int) (blocks []*types.Block) {
  377. for i := 0; i < n; i++ {
  378. block := self.GetBlock(hash)
  379. if block == nil {
  380. break
  381. }
  382. blocks = append(blocks, block)
  383. hash = block.ParentHash()
  384. }
  385. return
  386. }
  387. // non blocking version
  388. func (self *ChainManager) getBlockByNumber(num uint64) *types.Block {
  389. key, _ := self.blockDb.Get(append(blockNumPre, big.NewInt(int64(num)).Bytes()...))
  390. if len(key) == 0 {
  391. return nil
  392. }
  393. return self.GetBlock(common.BytesToHash(key))
  394. }
  395. func (self *ChainManager) GetUnclesInChain(block *types.Block, length int) (uncles []*types.Header) {
  396. for i := 0; block != nil && i < length; i++ {
  397. uncles = append(uncles, block.Uncles()...)
  398. block = self.GetBlock(block.ParentHash())
  399. }
  400. return
  401. }
  402. // setTotalDifficulty updates the TD of the chain manager. Note, this function
  403. // assumes that the `mu` mutex is held!
  404. func (bc *ChainManager) setTotalDifficulty(td *big.Int) {
  405. bc.td = new(big.Int).Set(td)
  406. }
  407. func (bc *ChainManager) Stop() {
  408. close(bc.quit)
  409. atomic.StoreInt32(&bc.procInterrupt, 1)
  410. bc.wg.Wait()
  411. glog.V(logger.Info).Infoln("Chain manager stopped")
  412. }
  413. type queueEvent struct {
  414. queue []interface{}
  415. canonicalCount int
  416. sideCount int
  417. splitCount int
  418. }
  419. func (self *ChainManager) procFutureBlocks() {
  420. var blocks []*types.Block
  421. self.futureBlocks.Each(func(i int, block *types.Block) {
  422. blocks = append(blocks, block)
  423. })
  424. if len(blocks) > 0 {
  425. types.BlockBy(types.Number).Sort(blocks)
  426. self.InsertChain(blocks)
  427. }
  428. }
  429. func (self *ChainManager) enqueueForWrite(block *types.Block) {
  430. self.pendingBlocks.Push(block)
  431. }
  432. func (self *ChainManager) flushQueuedBlocks() {
  433. db, batchWrite := self.blockDb.(*ethdb.LDBDatabase)
  434. batch := new(leveldb.Batch)
  435. self.pendingBlocks.Each(func(i int, block *types.Block) {
  436. enc, _ := rlp.EncodeToBytes((*types.StorageBlock)(block))
  437. key := append(blockHashPre, block.Hash().Bytes()...)
  438. if batchWrite {
  439. batch.Put(key, rle.Compress(enc))
  440. } else {
  441. self.blockDb.Put(key, enc)
  442. }
  443. })
  444. if batchWrite {
  445. db.LDB().Write(batch, nil)
  446. }
  447. }
  448. // InsertChain will attempt to insert the given chain in to the canonical chain or, otherwise, create a fork. It an error is returned
  449. // it will return the index number of the failing block as well an error describing what went wrong (for possible errors see core/errors.go).
  450. func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
  451. self.wg.Add(1)
  452. defer self.wg.Done()
  453. self.chainmu.Lock()
  454. defer self.chainmu.Unlock()
  455. self.pendingBlocks = NewBlockCache(len(chain))
  456. // A queued approach to delivering events. This is generally
  457. // faster than direct delivery and requires much less mutex
  458. // acquiring.
  459. var (
  460. queue = make([]interface{}, len(chain))
  461. queueEvent = queueEvent{queue: queue}
  462. stats struct{ queued, processed, ignored int }
  463. tstart = time.Now()
  464. nonceDone = make(chan nonceResult, len(chain))
  465. nonceQuit = make(chan struct{})
  466. nonceChecked = make([]bool, len(chain))
  467. )
  468. // Start the parallel nonce verifier.
  469. go verifyNonces(self.pow, chain, nonceQuit, nonceDone)
  470. defer close(nonceQuit)
  471. defer self.flushQueuedBlocks()
  472. defer func() {
  473. }()
  474. txcount := 0
  475. for i, block := range chain {
  476. if atomic.LoadInt32(&self.procInterrupt) == 1 {
  477. glog.V(logger.Debug).Infoln("Premature abort during chain processing")
  478. break
  479. }
  480. bstart := time.Now()
  481. // Wait for block i's nonce to be verified before processing
  482. // its state transition.
  483. for !nonceChecked[i] {
  484. r := <-nonceDone
  485. nonceChecked[r.i] = true
  486. if !r.valid {
  487. block := chain[r.i]
  488. return r.i, &BlockNonceErr{Hash: block.Hash(), Number: block.Number(), Nonce: block.Nonce()}
  489. }
  490. }
  491. if BadHashes[block.Hash()] {
  492. err := fmt.Errorf("Found known bad hash in chain %x", block.Hash())
  493. blockErr(block, err)
  494. return i, err
  495. }
  496. // Setting block.Td regardless of error (known for example) prevents errors down the line
  497. // in the protocol handler
  498. block.Td = new(big.Int).Set(CalcTD(block, self.GetBlock(block.ParentHash())))
  499. // Call in to the block processor and check for errors. It's likely that if one block fails
  500. // all others will fail too (unless a known block is returned).
  501. logs, err := self.processor.Process(block)
  502. if err != nil {
  503. if IsKnownBlockErr(err) {
  504. stats.ignored++
  505. continue
  506. }
  507. if err == BlockFutureErr {
  508. // Allow up to MaxFuture second in the future blocks. If this limit
  509. // is exceeded the chain is discarded and processed at a later time
  510. // if given.
  511. if max := time.Now().Unix() + maxTimeFutureBlocks; block.Time() > max {
  512. return i, fmt.Errorf("%v: BlockFutureErr, %v > %v", BlockFutureErr, block.Time(), max)
  513. }
  514. self.futureBlocks.Push(block)
  515. stats.queued++
  516. continue
  517. }
  518. if IsParentErr(err) && self.futureBlocks.Has(block.ParentHash()) {
  519. self.futureBlocks.Push(block)
  520. stats.queued++
  521. continue
  522. }
  523. blockErr(block, err)
  524. return i, err
  525. }
  526. txcount += len(block.Transactions())
  527. cblock := self.currentBlock
  528. // Compare the TD of the last known block in the canonical chain to make sure it's greater.
  529. // At this point it's possible that a different chain (fork) becomes the new canonical chain.
  530. if block.Td.Cmp(self.Td()) > 0 {
  531. // chain fork
  532. if block.ParentHash() != cblock.Hash() {
  533. // during split we merge two different chains and create the new canonical chain
  534. err := self.merge(cblock, block)
  535. if err != nil {
  536. return i, err
  537. }
  538. queue[i] = ChainSplitEvent{block, logs}
  539. queueEvent.splitCount++
  540. }
  541. self.mu.Lock()
  542. self.setTotalDifficulty(block.Td)
  543. self.insert(block)
  544. self.mu.Unlock()
  545. jsonlogger.LogJson(&logger.EthChainNewHead{
  546. BlockHash: block.Hash().Hex(),
  547. BlockNumber: block.Number(),
  548. ChainHeadHash: cblock.Hash().Hex(),
  549. BlockPrevHash: block.ParentHash().Hex(),
  550. })
  551. self.setTransState(state.New(block.Root(), self.stateDb))
  552. self.txState.SetState(state.New(block.Root(), self.stateDb))
  553. queue[i] = ChainEvent{block, block.Hash(), logs}
  554. queueEvent.canonicalCount++
  555. if glog.V(logger.Debug) {
  556. glog.Infof("[%v] inserted block #%d (%d TXs %d UNCs) (%x...). Took %v\n", time.Now().UnixNano(), block.Number(), len(block.Transactions()), len(block.Uncles()), block.Hash().Bytes()[0:4], time.Since(bstart))
  557. }
  558. } else {
  559. if glog.V(logger.Detail) {
  560. glog.Infof("inserted forked block #%d (TD=%v) (%d TXs %d UNCs) (%x...). Took %v\n", block.Number(), block.Difficulty(), len(block.Transactions()), len(block.Uncles()), block.Hash().Bytes()[0:4], time.Since(bstart))
  561. }
  562. queue[i] = ChainSideEvent{block, logs}
  563. queueEvent.sideCount++
  564. }
  565. // not in the canonical chain.
  566. self.enqueueForWrite(block)
  567. // Delete from future blocks
  568. self.futureBlocks.Delete(block.Hash())
  569. stats.processed++
  570. blockInsertTimer.UpdateSince(bstart)
  571. }
  572. if (stats.queued > 0 || stats.processed > 0 || stats.ignored > 0) && bool(glog.V(logger.Info)) {
  573. tend := time.Since(tstart)
  574. start, end := chain[0], chain[len(chain)-1]
  575. glog.Infof("imported %d block(s) (%d queued %d ignored) including %d txs in %v. #%v [%x / %x]\n", stats.processed, stats.queued, stats.ignored, txcount, tend, end.Number(), start.Hash().Bytes()[:4], end.Hash().Bytes()[:4])
  576. }
  577. go self.eventMux.Post(queueEvent)
  578. return 0, nil
  579. }
  580. // diff takes two blocks, an old chain and a new chain and will reconstruct the blocks and inserts them
  581. // to be part of the new canonical chain.
  582. func (self *ChainManager) diff(oldBlock, newBlock *types.Block) (types.Blocks, error) {
  583. var (
  584. newChain types.Blocks
  585. commonBlock *types.Block
  586. oldStart = oldBlock
  587. newStart = newBlock
  588. )
  589. // first reduce whoever is higher bound
  590. if oldBlock.NumberU64() > newBlock.NumberU64() {
  591. // reduce old chain
  592. for oldBlock = oldBlock; oldBlock != nil && oldBlock.NumberU64() != newBlock.NumberU64(); oldBlock = self.GetBlock(oldBlock.ParentHash()) {
  593. }
  594. } else {
  595. // reduce new chain and append new chain blocks for inserting later on
  596. for newBlock = newBlock; newBlock != nil && newBlock.NumberU64() != oldBlock.NumberU64(); newBlock = self.GetBlock(newBlock.ParentHash()) {
  597. newChain = append(newChain, newBlock)
  598. }
  599. }
  600. if oldBlock == nil {
  601. return nil, fmt.Errorf("Invalid old chain")
  602. }
  603. if newBlock == nil {
  604. return nil, fmt.Errorf("Invalid new chain")
  605. }
  606. numSplit := newBlock.Number()
  607. for {
  608. if oldBlock.Hash() == newBlock.Hash() {
  609. commonBlock = oldBlock
  610. break
  611. }
  612. newChain = append(newChain, newBlock)
  613. oldBlock, newBlock = self.GetBlock(oldBlock.ParentHash()), self.GetBlock(newBlock.ParentHash())
  614. if oldBlock == nil {
  615. return nil, fmt.Errorf("Invalid old chain")
  616. }
  617. if newBlock == nil {
  618. return nil, fmt.Errorf("Invalid new chain")
  619. }
  620. }
  621. if glog.V(logger.Info) {
  622. commonHash := commonBlock.Hash()
  623. glog.Infof("Fork detected @ %x. Reorganising chain from #%v %x to %x", commonHash[:4], numSplit, oldStart.Hash().Bytes()[:4], newStart.Hash().Bytes()[:4])
  624. }
  625. return newChain, nil
  626. }
  627. // merge merges two different chain to the new canonical chain
  628. func (self *ChainManager) merge(oldBlock, newBlock *types.Block) error {
  629. newChain, err := self.diff(oldBlock, newBlock)
  630. if err != nil {
  631. return fmt.Errorf("chain reorg failed: %v", err)
  632. }
  633. // insert blocks. Order does not matter. Last block will be written in ImportChain itself which creates the new head properly
  634. self.mu.Lock()
  635. for _, block := range newChain {
  636. self.insert(block)
  637. }
  638. self.mu.Unlock()
  639. return nil
  640. }
  641. func (self *ChainManager) update() {
  642. events := self.eventMux.Subscribe(queueEvent{})
  643. futureTimer := time.Tick(5 * time.Second)
  644. out:
  645. for {
  646. select {
  647. case ev := <-events.Chan():
  648. switch ev := ev.(type) {
  649. case queueEvent:
  650. for _, event := range ev.queue {
  651. switch event := event.(type) {
  652. case ChainEvent:
  653. // We need some control over the mining operation. Acquiring locks and waiting for the miner to create new block takes too long
  654. // and in most cases isn't even necessary.
  655. if self.lastBlockHash == event.Hash {
  656. self.currentGasLimit = CalcGasLimit(event.Block)
  657. self.eventMux.Post(ChainHeadEvent{event.Block})
  658. }
  659. }
  660. self.eventMux.Post(event)
  661. }
  662. }
  663. case <-futureTimer:
  664. self.procFutureBlocks()
  665. case <-self.quit:
  666. break out
  667. }
  668. }
  669. }
  670. func blockErr(block *types.Block, err error) {
  671. h := block.Header()
  672. glog.V(logger.Error).Infof("Bad block #%v (%x)\n", h.Number, h.Hash().Bytes())
  673. glog.V(logger.Error).Infoln(err)
  674. glog.V(logger.Debug).Infoln(verifyNonces)
  675. }
  676. type nonceResult struct {
  677. i int
  678. valid bool
  679. }
  680. // block verifies nonces of the given blocks in parallel and returns
  681. // an error if one of the blocks nonce verifications failed.
  682. func verifyNonces(pow pow.PoW, blocks []*types.Block, quit <-chan struct{}, done chan<- nonceResult) {
  683. // Spawn a few workers. They listen for blocks on the in channel
  684. // and send results on done. The workers will exit in the
  685. // background when in is closed.
  686. var (
  687. in = make(chan int)
  688. nworkers = runtime.GOMAXPROCS(0)
  689. )
  690. defer close(in)
  691. if len(blocks) < nworkers {
  692. nworkers = len(blocks)
  693. }
  694. for i := 0; i < nworkers; i++ {
  695. go func() {
  696. for i := range in {
  697. done <- nonceResult{i: i, valid: pow.Verify(blocks[i])}
  698. }
  699. }()
  700. }
  701. // Feed block indices to the workers.
  702. for i := range blocks {
  703. select {
  704. case in <- i:
  705. continue
  706. case <-quit:
  707. return
  708. }
  709. }
  710. }