chain_manager.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. // Copyright 2014 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 core implements the Ethereum consensus protocol.
  17. package core
  18. import (
  19. "errors"
  20. "fmt"
  21. "io"
  22. "math/big"
  23. "sync"
  24. "sync/atomic"
  25. "time"
  26. "github.com/ethereum/go-ethereum/common"
  27. "github.com/ethereum/go-ethereum/core/state"
  28. "github.com/ethereum/go-ethereum/core/types"
  29. "github.com/ethereum/go-ethereum/ethdb"
  30. "github.com/ethereum/go-ethereum/event"
  31. "github.com/ethereum/go-ethereum/logger"
  32. "github.com/ethereum/go-ethereum/logger/glog"
  33. "github.com/ethereum/go-ethereum/metrics"
  34. "github.com/ethereum/go-ethereum/pow"
  35. "github.com/ethereum/go-ethereum/rlp"
  36. "github.com/hashicorp/golang-lru"
  37. )
  38. var (
  39. chainlogger = logger.NewLogger("CHAIN")
  40. jsonlogger = logger.NewJsonLogger()
  41. blockInsertTimer = metrics.NewTimer("chain/inserts")
  42. ErrNoGenesis = errors.New("Genesis not found in chain")
  43. )
  44. const (
  45. headerCacheLimit = 512
  46. bodyCacheLimit = 256
  47. tdCacheLimit = 1024
  48. blockCacheLimit = 256
  49. maxFutureBlocks = 256
  50. maxTimeFutureBlocks = 30
  51. checkpointLimit = 200
  52. )
  53. type ChainManager struct {
  54. //eth EthManager
  55. chainDb ethdb.Database
  56. processor types.BlockProcessor
  57. eventMux *event.TypeMux
  58. genesisBlock *types.Block
  59. // Last known total difficulty
  60. mu sync.RWMutex
  61. chainmu sync.RWMutex
  62. tsmu sync.RWMutex
  63. checkpoint int // checkpoint counts towards the new checkpoint
  64. td *big.Int
  65. currentBlock *types.Block
  66. currentGasLimit *big.Int
  67. headerCache *lru.Cache // Cache for the most recent block headers
  68. bodyCache *lru.Cache // Cache for the most recent block bodies
  69. bodyRLPCache *lru.Cache // Cache for the most recent block bodies in RLP encoded format
  70. tdCache *lru.Cache // Cache for the most recent block total difficulties
  71. blockCache *lru.Cache // Cache for the most recent entire blocks
  72. futureBlocks *lru.Cache // future blocks are blocks added for later processing
  73. quit chan struct{}
  74. running int32 // running must be called automically
  75. // procInterrupt must be atomically called
  76. procInterrupt int32 // interrupt signaler for block processing
  77. wg sync.WaitGroup
  78. pow pow.PoW
  79. }
  80. func NewChainManager(chainDb ethdb.Database, pow pow.PoW, mux *event.TypeMux) (*ChainManager, error) {
  81. headerCache, _ := lru.New(headerCacheLimit)
  82. bodyCache, _ := lru.New(bodyCacheLimit)
  83. bodyRLPCache, _ := lru.New(bodyCacheLimit)
  84. tdCache, _ := lru.New(tdCacheLimit)
  85. blockCache, _ := lru.New(blockCacheLimit)
  86. futureBlocks, _ := lru.New(maxFutureBlocks)
  87. bc := &ChainManager{
  88. chainDb: chainDb,
  89. eventMux: mux,
  90. quit: make(chan struct{}),
  91. headerCache: headerCache,
  92. bodyCache: bodyCache,
  93. bodyRLPCache: bodyRLPCache,
  94. tdCache: tdCache,
  95. blockCache: blockCache,
  96. futureBlocks: futureBlocks,
  97. pow: pow,
  98. }
  99. bc.genesisBlock = bc.GetBlockByNumber(0)
  100. if bc.genesisBlock == nil {
  101. reader, err := NewDefaultGenesisReader()
  102. if err != nil {
  103. return nil, err
  104. }
  105. bc.genesisBlock, err = WriteGenesisBlock(chainDb, reader)
  106. if err != nil {
  107. return nil, err
  108. }
  109. glog.V(logger.Info).Infoln("WARNING: Wrote default ethereum genesis block")
  110. }
  111. if err := bc.setLastState(); err != nil {
  112. return nil, err
  113. }
  114. // Check the current state of the block hashes and make sure that we do not have any of the bad blocks in our chain
  115. for hash, _ := range BadHashes {
  116. if block := bc.GetBlock(hash); block != nil {
  117. glog.V(logger.Error).Infof("Found bad hash. Reorganising chain to state %x\n", block.ParentHash().Bytes()[:4])
  118. block = bc.GetBlock(block.ParentHash())
  119. if block == nil {
  120. glog.Fatal("Unable to complete. Parent block not found. Corrupted DB?")
  121. }
  122. bc.SetHead(block)
  123. glog.V(logger.Error).Infoln("Chain reorg was successfull. Resuming normal operation")
  124. }
  125. }
  126. // Take ownership of this particular state
  127. go bc.update()
  128. return bc, nil
  129. }
  130. func (bc *ChainManager) SetHead(head *types.Block) {
  131. bc.mu.Lock()
  132. defer bc.mu.Unlock()
  133. for block := bc.currentBlock; block != nil && block.Hash() != head.Hash(); block = bc.GetBlock(block.ParentHash()) {
  134. DeleteBlock(bc.chainDb, block.Hash())
  135. }
  136. bc.headerCache.Purge()
  137. bc.bodyCache.Purge()
  138. bc.bodyRLPCache.Purge()
  139. bc.blockCache.Purge()
  140. bc.futureBlocks.Purge()
  141. bc.currentBlock = head
  142. bc.setTotalDifficulty(bc.GetTd(head.Hash()))
  143. bc.insert(head)
  144. bc.setLastState()
  145. }
  146. func (self *ChainManager) Td() *big.Int {
  147. self.mu.RLock()
  148. defer self.mu.RUnlock()
  149. return new(big.Int).Set(self.td)
  150. }
  151. func (self *ChainManager) GasLimit() *big.Int {
  152. self.mu.RLock()
  153. defer self.mu.RUnlock()
  154. return self.currentBlock.GasLimit()
  155. }
  156. func (self *ChainManager) LastBlockHash() common.Hash {
  157. self.mu.RLock()
  158. defer self.mu.RUnlock()
  159. return self.currentBlock.Hash()
  160. }
  161. func (self *ChainManager) CurrentBlock() *types.Block {
  162. self.mu.RLock()
  163. defer self.mu.RUnlock()
  164. return self.currentBlock
  165. }
  166. func (self *ChainManager) Status() (td *big.Int, currentBlock common.Hash, genesisBlock common.Hash) {
  167. self.mu.RLock()
  168. defer self.mu.RUnlock()
  169. return new(big.Int).Set(self.td), self.currentBlock.Hash(), self.genesisBlock.Hash()
  170. }
  171. func (self *ChainManager) SetProcessor(proc types.BlockProcessor) {
  172. self.processor = proc
  173. }
  174. func (self *ChainManager) State() *state.StateDB {
  175. return state.New(self.CurrentBlock().Root(), self.chainDb)
  176. }
  177. func (bc *ChainManager) recover() bool {
  178. data, _ := bc.chainDb.Get([]byte("checkpoint"))
  179. if len(data) != 0 {
  180. block := bc.GetBlock(common.BytesToHash(data))
  181. if block != nil {
  182. if err := WriteCanonicalHash(bc.chainDb, block.Hash(), block.NumberU64()); err != nil {
  183. glog.Fatalf("failed to write database head number: %v", err)
  184. }
  185. if err := WriteHeadBlockHash(bc.chainDb, block.Hash()); err != nil {
  186. glog.Fatalf("failed to write database head hash: %v", err)
  187. }
  188. bc.currentBlock = block
  189. return true
  190. }
  191. }
  192. return false
  193. }
  194. func (bc *ChainManager) setLastState() error {
  195. head := GetHeadBlockHash(bc.chainDb)
  196. if head != (common.Hash{}) {
  197. block := bc.GetBlock(head)
  198. if block != nil {
  199. bc.currentBlock = block
  200. } else {
  201. glog.Infof("LastBlock (%x) not found. Recovering...\n", head)
  202. if bc.recover() {
  203. glog.Infof("Recover successful")
  204. } else {
  205. glog.Fatalf("Recover failed. Please report")
  206. }
  207. }
  208. } else {
  209. bc.Reset()
  210. }
  211. bc.td = bc.GetTd(bc.currentBlock.Hash())
  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. return nil
  217. }
  218. // Reset purges the entire blockchain, restoring it to its genesis state.
  219. func (bc *ChainManager) Reset() {
  220. bc.ResetWithGenesisBlock(bc.genesisBlock)
  221. }
  222. // ResetWithGenesisBlock purges the entire blockchain, restoring it to the
  223. // specified genesis state.
  224. func (bc *ChainManager) ResetWithGenesisBlock(genesis *types.Block) {
  225. bc.mu.Lock()
  226. defer bc.mu.Unlock()
  227. // Dump the entire block chain and purge the caches
  228. for block := bc.currentBlock; block != nil; block = bc.GetBlock(block.ParentHash()) {
  229. DeleteBlock(bc.chainDb, block.Hash())
  230. }
  231. bc.headerCache.Purge()
  232. bc.bodyCache.Purge()
  233. bc.bodyRLPCache.Purge()
  234. bc.blockCache.Purge()
  235. bc.futureBlocks.Purge()
  236. // Prepare the genesis block and reinitialize the chain
  237. if err := WriteTd(bc.chainDb, genesis.Hash(), genesis.Difficulty()); err != nil {
  238. glog.Fatalf("failed to write genesis block TD: %v", err)
  239. }
  240. if err := WriteBlock(bc.chainDb, genesis); err != nil {
  241. glog.Fatalf("failed to write genesis block: %v", err)
  242. }
  243. bc.insert(bc.genesisBlock)
  244. bc.currentBlock = bc.genesisBlock
  245. bc.setTotalDifficulty(genesis.Difficulty())
  246. }
  247. // Export writes the active chain to the given writer.
  248. func (self *ChainManager) Export(w io.Writer) error {
  249. if err := self.ExportN(w, uint64(0), self.currentBlock.NumberU64()); err != nil {
  250. return err
  251. }
  252. return nil
  253. }
  254. // ExportN writes a subset of the active chain to the given writer.
  255. func (self *ChainManager) ExportN(w io.Writer, first uint64, last uint64) error {
  256. self.mu.RLock()
  257. defer self.mu.RUnlock()
  258. if first > last {
  259. return fmt.Errorf("export failed: first (%d) is greater than last (%d)", first, last)
  260. }
  261. glog.V(logger.Info).Infof("exporting %d blocks...\n", last-first+1)
  262. for nr := first; nr <= last; nr++ {
  263. block := self.GetBlockByNumber(nr)
  264. if block == nil {
  265. return fmt.Errorf("export failed on #%d: not found", nr)
  266. }
  267. if err := block.EncodeRLP(w); err != nil {
  268. return err
  269. }
  270. }
  271. return nil
  272. }
  273. // insert injects a block into the current chain block chain. Note, this function
  274. // assumes that the `mu` mutex is held!
  275. func (bc *ChainManager) insert(block *types.Block) {
  276. // Add the block to the canonical chain number scheme and mark as the head
  277. if err := WriteCanonicalHash(bc.chainDb, block.Hash(), block.NumberU64()); err != nil {
  278. glog.Fatalf("failed to insert block number: %v", err)
  279. }
  280. if err := WriteHeadBlockHash(bc.chainDb, block.Hash()); err != nil {
  281. glog.Fatalf("failed to insert block number: %v", err)
  282. }
  283. // Add a new restore point if we reached some limit
  284. bc.checkpoint++
  285. if bc.checkpoint > checkpointLimit {
  286. if err := bc.chainDb.Put([]byte("checkpoint"), block.Hash().Bytes()); err != nil {
  287. glog.Fatalf("failed to create checkpoint: %v", err)
  288. }
  289. bc.checkpoint = 0
  290. }
  291. // Update the internal internal state with the head block
  292. bc.currentBlock = block
  293. }
  294. // Accessors
  295. func (bc *ChainManager) Genesis() *types.Block {
  296. return bc.genesisBlock
  297. }
  298. // HasHeader checks if a block header is present in the database or not, caching
  299. // it if present.
  300. func (bc *ChainManager) HasHeader(hash common.Hash) bool {
  301. return bc.GetHeader(hash) != nil
  302. }
  303. // GetHeader retrieves a block header from the database by hash, caching it if
  304. // found.
  305. func (self *ChainManager) GetHeader(hash common.Hash) *types.Header {
  306. // Short circuit if the header's already in the cache, retrieve otherwise
  307. if header, ok := self.headerCache.Get(hash); ok {
  308. return header.(*types.Header)
  309. }
  310. header := GetHeader(self.chainDb, hash)
  311. if header == nil {
  312. return nil
  313. }
  314. // Cache the found header for next time and return
  315. self.headerCache.Add(header.Hash(), header)
  316. return header
  317. }
  318. // GetHeaderByNumber retrieves a block header from the database by number,
  319. // caching it (associated with its hash) if found.
  320. func (self *ChainManager) GetHeaderByNumber(number uint64) *types.Header {
  321. hash := GetCanonicalHash(self.chainDb, number)
  322. if hash == (common.Hash{}) {
  323. return nil
  324. }
  325. return self.GetHeader(hash)
  326. }
  327. // GetBody retrieves a block body (transactions and uncles) from the database by
  328. // hash, caching it if found.
  329. func (self *ChainManager) GetBody(hash common.Hash) *types.Body {
  330. // Short circuit if the body's already in the cache, retrieve otherwise
  331. if cached, ok := self.bodyCache.Get(hash); ok {
  332. body := cached.(*types.Body)
  333. return body
  334. }
  335. body := GetBody(self.chainDb, hash)
  336. if body == nil {
  337. return nil
  338. }
  339. // Cache the found body for next time and return
  340. self.bodyCache.Add(hash, body)
  341. return body
  342. }
  343. // GetBodyRLP retrieves a block body in RLP encoding from the database by hash,
  344. // caching it if found.
  345. func (self *ChainManager) GetBodyRLP(hash common.Hash) rlp.RawValue {
  346. // Short circuit if the body's already in the cache, retrieve otherwise
  347. if cached, ok := self.bodyRLPCache.Get(hash); ok {
  348. return cached.(rlp.RawValue)
  349. }
  350. body := GetBodyRLP(self.chainDb, hash)
  351. if len(body) == 0 {
  352. return nil
  353. }
  354. // Cache the found body for next time and return
  355. self.bodyRLPCache.Add(hash, body)
  356. return body
  357. }
  358. // GetTd retrieves a block's total difficulty in the canonical chain from the
  359. // database by hash, caching it if found.
  360. func (self *ChainManager) GetTd(hash common.Hash) *big.Int {
  361. // Short circuit if the td's already in the cache, retrieve otherwise
  362. if cached, ok := self.tdCache.Get(hash); ok {
  363. return cached.(*big.Int)
  364. }
  365. td := GetTd(self.chainDb, hash)
  366. if td == nil {
  367. return nil
  368. }
  369. // Cache the found body for next time and return
  370. self.tdCache.Add(hash, td)
  371. return td
  372. }
  373. // HasBlock checks if a block is fully present in the database or not, caching
  374. // it if present.
  375. func (bc *ChainManager) HasBlock(hash common.Hash) bool {
  376. return bc.GetBlock(hash) != nil
  377. }
  378. // GetBlock retrieves a block from the database by hash, caching it if found.
  379. func (self *ChainManager) GetBlock(hash common.Hash) *types.Block {
  380. // Short circuit if the block's already in the cache, retrieve otherwise
  381. if block, ok := self.blockCache.Get(hash); ok {
  382. return block.(*types.Block)
  383. }
  384. block := GetBlock(self.chainDb, hash)
  385. if block == nil {
  386. return nil
  387. }
  388. // Cache the found block for next time and return
  389. self.blockCache.Add(block.Hash(), block)
  390. return block
  391. }
  392. // GetBlockByNumber retrieves a block from the database by number, caching it
  393. // (associated with its hash) if found.
  394. func (self *ChainManager) GetBlockByNumber(number uint64) *types.Block {
  395. hash := GetCanonicalHash(self.chainDb, number)
  396. if hash == (common.Hash{}) {
  397. return nil
  398. }
  399. return self.GetBlock(hash)
  400. }
  401. // GetBlockHashesFromHash retrieves a number of block hashes starting at a given
  402. // hash, fetching towards the genesis block.
  403. func (self *ChainManager) GetBlockHashesFromHash(hash common.Hash, max uint64) []common.Hash {
  404. // Get the origin header from which to fetch
  405. header := self.GetHeader(hash)
  406. if header == nil {
  407. return nil
  408. }
  409. // Iterate the headers until enough is collected or the genesis reached
  410. chain := make([]common.Hash, 0, max)
  411. for i := uint64(0); i < max; i++ {
  412. if header = self.GetHeader(header.ParentHash); header == nil {
  413. break
  414. }
  415. chain = append(chain, header.Hash())
  416. if header.Number.Cmp(common.Big0) == 0 {
  417. break
  418. }
  419. }
  420. return chain
  421. }
  422. // [deprecated by eth/62]
  423. // GetBlocksFromHash returns the block corresponding to hash and up to n-1 ancestors.
  424. func (self *ChainManager) GetBlocksFromHash(hash common.Hash, n int) (blocks []*types.Block) {
  425. for i := 0; i < n; i++ {
  426. block := self.GetBlock(hash)
  427. if block == nil {
  428. break
  429. }
  430. blocks = append(blocks, block)
  431. hash = block.ParentHash()
  432. }
  433. return
  434. }
  435. func (self *ChainManager) GetUnclesInChain(block *types.Block, length int) (uncles []*types.Header) {
  436. for i := 0; block != nil && i < length; i++ {
  437. uncles = append(uncles, block.Uncles()...)
  438. block = self.GetBlock(block.ParentHash())
  439. }
  440. return
  441. }
  442. // setTotalDifficulty updates the TD of the chain manager. Note, this function
  443. // assumes that the `mu` mutex is held!
  444. func (bc *ChainManager) setTotalDifficulty(td *big.Int) {
  445. bc.td = new(big.Int).Set(td)
  446. }
  447. func (bc *ChainManager) Stop() {
  448. if !atomic.CompareAndSwapInt32(&bc.running, 0, 1) {
  449. return
  450. }
  451. close(bc.quit)
  452. atomic.StoreInt32(&bc.procInterrupt, 1)
  453. bc.wg.Wait()
  454. glog.V(logger.Info).Infoln("Chain manager stopped")
  455. }
  456. type queueEvent struct {
  457. queue []interface{}
  458. canonicalCount int
  459. sideCount int
  460. splitCount int
  461. }
  462. func (self *ChainManager) procFutureBlocks() {
  463. blocks := make([]*types.Block, self.futureBlocks.Len())
  464. for i, hash := range self.futureBlocks.Keys() {
  465. block, _ := self.futureBlocks.Get(hash)
  466. blocks[i] = block.(*types.Block)
  467. }
  468. if len(blocks) > 0 {
  469. types.BlockBy(types.Number).Sort(blocks)
  470. self.InsertChain(blocks)
  471. }
  472. }
  473. type writeStatus byte
  474. const (
  475. NonStatTy writeStatus = iota
  476. CanonStatTy
  477. SplitStatTy
  478. SideStatTy
  479. )
  480. // WriteBlock writes the block to the chain.
  481. func (self *ChainManager) WriteBlock(block *types.Block) (status writeStatus, err error) {
  482. self.wg.Add(1)
  483. defer self.wg.Done()
  484. // Calculate the total difficulty of the block
  485. ptd := self.GetTd(block.ParentHash())
  486. if ptd == nil {
  487. return NonStatTy, ParentError(block.ParentHash())
  488. }
  489. td := new(big.Int).Add(block.Difficulty(), ptd)
  490. self.mu.RLock()
  491. cblock := self.currentBlock
  492. self.mu.RUnlock()
  493. // Compare the TD of the last known block in the canonical chain to make sure it's greater.
  494. // At this point it's possible that a different chain (fork) becomes the new canonical chain.
  495. if td.Cmp(self.Td()) > 0 {
  496. // chain fork
  497. if block.ParentHash() != cblock.Hash() {
  498. // during split we merge two different chains and create the new canonical chain
  499. err := self.reorg(cblock, block)
  500. if err != nil {
  501. return NonStatTy, err
  502. }
  503. }
  504. status = CanonStatTy
  505. self.mu.Lock()
  506. self.setTotalDifficulty(td)
  507. self.insert(block)
  508. self.mu.Unlock()
  509. } else {
  510. status = SideStatTy
  511. }
  512. if err := WriteTd(self.chainDb, block.Hash(), td); err != nil {
  513. glog.Fatalf("failed to write block total difficulty: %v", err)
  514. }
  515. if err := WriteBlock(self.chainDb, block); err != nil {
  516. glog.Fatalf("filed to write block contents: %v", err)
  517. }
  518. // Delete from future blocks
  519. self.futureBlocks.Remove(block.Hash())
  520. return
  521. }
  522. // InsertChain will attempt to insert the given chain in to the canonical chain or, otherwise, create a fork. It an error is returned
  523. // 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).
  524. func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
  525. self.wg.Add(1)
  526. defer self.wg.Done()
  527. self.chainmu.Lock()
  528. defer self.chainmu.Unlock()
  529. // A queued approach to delivering events. This is generally
  530. // faster than direct delivery and requires much less mutex
  531. // acquiring.
  532. var (
  533. queue = make([]interface{}, len(chain))
  534. queueEvent = queueEvent{queue: queue}
  535. stats struct{ queued, processed, ignored int }
  536. tstart = time.Now()
  537. nonceChecked = make([]bool, len(chain))
  538. )
  539. // Start the parallel nonce verifier.
  540. nonceAbort, nonceResults := verifyNoncesFromBlocks(self.pow, chain)
  541. defer close(nonceAbort)
  542. txcount := 0
  543. for i, block := range chain {
  544. if atomic.LoadInt32(&self.procInterrupt) == 1 {
  545. glog.V(logger.Debug).Infoln("Premature abort during chain processing")
  546. break
  547. }
  548. bstart := time.Now()
  549. // Wait for block i's nonce to be verified before processing
  550. // its state transition.
  551. for !nonceChecked[i] {
  552. r := <-nonceResults
  553. nonceChecked[r.index] = true
  554. if !r.valid {
  555. block := chain[r.index]
  556. return r.index, &BlockNonceErr{Hash: block.Hash(), Number: block.Number(), Nonce: block.Nonce()}
  557. }
  558. }
  559. if BadHashes[block.Hash()] {
  560. err := fmt.Errorf("Found known bad hash in chain %x", block.Hash())
  561. blockErr(block, err)
  562. return i, err
  563. }
  564. // Call in to the block processor and check for errors. It's likely that if one block fails
  565. // all others will fail too (unless a known block is returned).
  566. logs, receipts, err := self.processor.Process(block)
  567. if err != nil {
  568. if IsKnownBlockErr(err) {
  569. stats.ignored++
  570. continue
  571. }
  572. if err == BlockFutureErr {
  573. // Allow up to MaxFuture second in the future blocks. If this limit
  574. // is exceeded the chain is discarded and processed at a later time
  575. // if given.
  576. max := big.NewInt(time.Now().Unix() + maxTimeFutureBlocks)
  577. if block.Time().Cmp(max) == 1 {
  578. return i, fmt.Errorf("%v: BlockFutureErr, %v > %v", BlockFutureErr, block.Time(), max)
  579. }
  580. self.futureBlocks.Add(block.Hash(), block)
  581. stats.queued++
  582. continue
  583. }
  584. if IsParentErr(err) && self.futureBlocks.Contains(block.ParentHash()) {
  585. self.futureBlocks.Add(block.Hash(), block)
  586. stats.queued++
  587. continue
  588. }
  589. blockErr(block, err)
  590. go ReportBlock(block, err)
  591. return i, err
  592. }
  593. if err := PutBlockReceipts(self.chainDb, block, receipts); err != nil {
  594. glog.V(logger.Warn).Infoln("error writing block receipts:", err)
  595. }
  596. txcount += len(block.Transactions())
  597. // write the block to the chain and get the status
  598. status, err := self.WriteBlock(block)
  599. if err != nil {
  600. return i, err
  601. }
  602. switch status {
  603. case CanonStatTy:
  604. if glog.V(logger.Debug) {
  605. glog.Infof("[%v] inserted block #%d (%d TXs %v G %d UNCs) (%x...). Took %v\n", time.Now().UnixNano(), block.Number(), len(block.Transactions()), block.GasUsed(), len(block.Uncles()), block.Hash().Bytes()[0:4], time.Since(bstart))
  606. }
  607. queue[i] = ChainEvent{block, block.Hash(), logs}
  608. queueEvent.canonicalCount++
  609. // This puts transactions in a extra db for rpc
  610. PutTransactions(self.chainDb, block, block.Transactions())
  611. // store the receipts
  612. PutReceipts(self.chainDb, receipts)
  613. case SideStatTy:
  614. if glog.V(logger.Detail) {
  615. 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))
  616. }
  617. queue[i] = ChainSideEvent{block, logs}
  618. queueEvent.sideCount++
  619. case SplitStatTy:
  620. queue[i] = ChainSplitEvent{block, logs}
  621. queueEvent.splitCount++
  622. }
  623. stats.processed++
  624. }
  625. if (stats.queued > 0 || stats.processed > 0 || stats.ignored > 0) && bool(glog.V(logger.Info)) {
  626. tend := time.Since(tstart)
  627. start, end := chain[0], chain[len(chain)-1]
  628. 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])
  629. }
  630. go self.eventMux.Post(queueEvent)
  631. return 0, nil
  632. }
  633. // reorgs takes two blocks, an old chain and a new chain and will reconstruct the blocks and inserts them
  634. // to be part of the new canonical chain and accumulates potential missing transactions and post an
  635. // event about them
  636. func (self *ChainManager) reorg(oldBlock, newBlock *types.Block) error {
  637. self.mu.Lock()
  638. defer self.mu.Unlock()
  639. var (
  640. newChain types.Blocks
  641. commonBlock *types.Block
  642. oldStart = oldBlock
  643. newStart = newBlock
  644. deletedTxs types.Transactions
  645. )
  646. // first reduce whoever is higher bound
  647. if oldBlock.NumberU64() > newBlock.NumberU64() {
  648. // reduce old chain
  649. for oldBlock = oldBlock; oldBlock != nil && oldBlock.NumberU64() != newBlock.NumberU64(); oldBlock = self.GetBlock(oldBlock.ParentHash()) {
  650. deletedTxs = append(deletedTxs, oldBlock.Transactions()...)
  651. }
  652. } else {
  653. // reduce new chain and append new chain blocks for inserting later on
  654. for newBlock = newBlock; newBlock != nil && newBlock.NumberU64() != oldBlock.NumberU64(); newBlock = self.GetBlock(newBlock.ParentHash()) {
  655. newChain = append(newChain, newBlock)
  656. }
  657. }
  658. if oldBlock == nil {
  659. return fmt.Errorf("Invalid old chain")
  660. }
  661. if newBlock == nil {
  662. return fmt.Errorf("Invalid new chain")
  663. }
  664. numSplit := newBlock.Number()
  665. for {
  666. if oldBlock.Hash() == newBlock.Hash() {
  667. commonBlock = oldBlock
  668. break
  669. }
  670. newChain = append(newChain, newBlock)
  671. deletedTxs = append(deletedTxs, oldBlock.Transactions()...)
  672. oldBlock, newBlock = self.GetBlock(oldBlock.ParentHash()), self.GetBlock(newBlock.ParentHash())
  673. if oldBlock == nil {
  674. return fmt.Errorf("Invalid old chain")
  675. }
  676. if newBlock == nil {
  677. return fmt.Errorf("Invalid new chain")
  678. }
  679. }
  680. if glog.V(logger.Debug) {
  681. commonHash := commonBlock.Hash()
  682. glog.Infof("Chain split detected @ %x. Reorganising chain from #%v %x to %x", commonHash[:4], numSplit, oldStart.Hash().Bytes()[:4], newStart.Hash().Bytes()[:4])
  683. }
  684. var addedTxs types.Transactions
  685. // insert blocks. Order does not matter. Last block will be written in ImportChain itself which creates the new head properly
  686. for _, block := range newChain {
  687. // insert the block in the canonical way, re-writing history
  688. self.insert(block)
  689. // write canonical receipts and transactions
  690. PutTransactions(self.chainDb, block, block.Transactions())
  691. PutReceipts(self.chainDb, GetBlockReceipts(self.chainDb, block.Hash()))
  692. addedTxs = append(addedTxs, block.Transactions()...)
  693. }
  694. // calculate the difference between deleted and added transactions
  695. diff := types.TxDifference(deletedTxs, addedTxs)
  696. // When transactions get deleted from the database that means the
  697. // receipts that were created in the fork must also be deleted
  698. for _, tx := range diff {
  699. DeleteReceipt(self.chainDb, tx.Hash())
  700. DeleteTransaction(self.chainDb, tx.Hash())
  701. }
  702. self.eventMux.Post(RemovedTransactionEvent{diff})
  703. return nil
  704. }
  705. func (self *ChainManager) update() {
  706. events := self.eventMux.Subscribe(queueEvent{})
  707. futureTimer := time.Tick(5 * time.Second)
  708. out:
  709. for {
  710. select {
  711. case ev := <-events.Chan():
  712. switch ev := ev.(type) {
  713. case queueEvent:
  714. for _, event := range ev.queue {
  715. switch event := event.(type) {
  716. case ChainEvent:
  717. // We need some control over the mining operation. Acquiring locks and waiting for the miner to create new block takes too long
  718. // and in most cases isn't even necessary.
  719. if self.currentBlock.Hash() == event.Hash {
  720. self.currentGasLimit = CalcGasLimit(event.Block)
  721. self.eventMux.Post(ChainHeadEvent{event.Block})
  722. }
  723. }
  724. self.eventMux.Post(event)
  725. }
  726. }
  727. case <-futureTimer:
  728. self.procFutureBlocks()
  729. case <-self.quit:
  730. break out
  731. }
  732. }
  733. }
  734. func blockErr(block *types.Block, err error) {
  735. if glog.V(logger.Error) {
  736. glog.Errorf("Bad block #%v (%s)\n", block.Number(), block.Hash().Hex())
  737. glog.Errorf(" %v", err)
  738. }
  739. }