chain_manager.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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.genesisBlock = genesis
  244. bc.insert(bc.genesisBlock)
  245. bc.currentBlock = bc.genesisBlock
  246. bc.setTotalDifficulty(genesis.Difficulty())
  247. }
  248. // Export writes the active chain to the given writer.
  249. func (self *ChainManager) Export(w io.Writer) error {
  250. if err := self.ExportN(w, uint64(0), self.currentBlock.NumberU64()); err != nil {
  251. return err
  252. }
  253. return nil
  254. }
  255. // ExportN writes a subset of the active chain to the given writer.
  256. func (self *ChainManager) ExportN(w io.Writer, first uint64, last uint64) error {
  257. self.mu.RLock()
  258. defer self.mu.RUnlock()
  259. if first > last {
  260. return fmt.Errorf("export failed: first (%d) is greater than last (%d)", first, last)
  261. }
  262. glog.V(logger.Info).Infof("exporting %d blocks...\n", last-first+1)
  263. for nr := first; nr <= last; nr++ {
  264. block := self.GetBlockByNumber(nr)
  265. if block == nil {
  266. return fmt.Errorf("export failed on #%d: not found", nr)
  267. }
  268. if err := block.EncodeRLP(w); err != nil {
  269. return err
  270. }
  271. }
  272. return nil
  273. }
  274. // insert injects a block into the current chain block chain. Note, this function
  275. // assumes that the `mu` mutex is held!
  276. func (bc *ChainManager) insert(block *types.Block) {
  277. // Add the block to the canonical chain number scheme and mark as the head
  278. if err := WriteCanonicalHash(bc.chainDb, block.Hash(), block.NumberU64()); err != nil {
  279. glog.Fatalf("failed to insert block number: %v", err)
  280. }
  281. if err := WriteHeadBlockHash(bc.chainDb, block.Hash()); err != nil {
  282. glog.Fatalf("failed to insert block number: %v", err)
  283. }
  284. // Add a new restore point if we reached some limit
  285. bc.checkpoint++
  286. if bc.checkpoint > checkpointLimit {
  287. if err := bc.chainDb.Put([]byte("checkpoint"), block.Hash().Bytes()); err != nil {
  288. glog.Fatalf("failed to create checkpoint: %v", err)
  289. }
  290. bc.checkpoint = 0
  291. }
  292. // Update the internal internal state with the head block
  293. bc.currentBlock = block
  294. }
  295. // Accessors
  296. func (bc *ChainManager) Genesis() *types.Block {
  297. return bc.genesisBlock
  298. }
  299. // HasHeader checks if a block header is present in the database or not, caching
  300. // it if present.
  301. func (bc *ChainManager) HasHeader(hash common.Hash) bool {
  302. return bc.GetHeader(hash) != nil
  303. }
  304. // GetHeader retrieves a block header from the database by hash, caching it if
  305. // found.
  306. func (self *ChainManager) GetHeader(hash common.Hash) *types.Header {
  307. // Short circuit if the header's already in the cache, retrieve otherwise
  308. if header, ok := self.headerCache.Get(hash); ok {
  309. return header.(*types.Header)
  310. }
  311. header := GetHeader(self.chainDb, hash)
  312. if header == nil {
  313. return nil
  314. }
  315. // Cache the found header for next time and return
  316. self.headerCache.Add(header.Hash(), header)
  317. return header
  318. }
  319. // GetHeaderByNumber retrieves a block header from the database by number,
  320. // caching it (associated with its hash) if found.
  321. func (self *ChainManager) GetHeaderByNumber(number uint64) *types.Header {
  322. hash := GetCanonicalHash(self.chainDb, number)
  323. if hash == (common.Hash{}) {
  324. return nil
  325. }
  326. return self.GetHeader(hash)
  327. }
  328. // GetBody retrieves a block body (transactions and uncles) from the database by
  329. // hash, caching it if found.
  330. func (self *ChainManager) GetBody(hash common.Hash) *types.Body {
  331. // Short circuit if the body's already in the cache, retrieve otherwise
  332. if cached, ok := self.bodyCache.Get(hash); ok {
  333. body := cached.(*types.Body)
  334. return body
  335. }
  336. body := GetBody(self.chainDb, hash)
  337. if body == nil {
  338. return nil
  339. }
  340. // Cache the found body for next time and return
  341. self.bodyCache.Add(hash, body)
  342. return body
  343. }
  344. // GetBodyRLP retrieves a block body in RLP encoding from the database by hash,
  345. // caching it if found.
  346. func (self *ChainManager) GetBodyRLP(hash common.Hash) rlp.RawValue {
  347. // Short circuit if the body's already in the cache, retrieve otherwise
  348. if cached, ok := self.bodyRLPCache.Get(hash); ok {
  349. return cached.(rlp.RawValue)
  350. }
  351. body := GetBodyRLP(self.chainDb, hash)
  352. if len(body) == 0 {
  353. return nil
  354. }
  355. // Cache the found body for next time and return
  356. self.bodyRLPCache.Add(hash, body)
  357. return body
  358. }
  359. // GetTd retrieves a block's total difficulty in the canonical chain from the
  360. // database by hash, caching it if found.
  361. func (self *ChainManager) GetTd(hash common.Hash) *big.Int {
  362. // Short circuit if the td's already in the cache, retrieve otherwise
  363. if cached, ok := self.tdCache.Get(hash); ok {
  364. return cached.(*big.Int)
  365. }
  366. td := GetTd(self.chainDb, hash)
  367. if td == nil {
  368. return nil
  369. }
  370. // Cache the found body for next time and return
  371. self.tdCache.Add(hash, td)
  372. return td
  373. }
  374. // HasBlock checks if a block is fully present in the database or not, caching
  375. // it if present.
  376. func (bc *ChainManager) HasBlock(hash common.Hash) bool {
  377. return bc.GetBlock(hash) != nil
  378. }
  379. // GetBlock retrieves a block from the database by hash, caching it if found.
  380. func (self *ChainManager) GetBlock(hash common.Hash) *types.Block {
  381. // Short circuit if the block's already in the cache, retrieve otherwise
  382. if block, ok := self.blockCache.Get(hash); ok {
  383. return block.(*types.Block)
  384. }
  385. block := GetBlock(self.chainDb, hash)
  386. if block == nil {
  387. return nil
  388. }
  389. // Cache the found block for next time and return
  390. self.blockCache.Add(block.Hash(), block)
  391. return block
  392. }
  393. // GetBlockByNumber retrieves a block from the database by number, caching it
  394. // (associated with its hash) if found.
  395. func (self *ChainManager) GetBlockByNumber(number uint64) *types.Block {
  396. hash := GetCanonicalHash(self.chainDb, number)
  397. if hash == (common.Hash{}) {
  398. return nil
  399. }
  400. return self.GetBlock(hash)
  401. }
  402. // GetBlockHashesFromHash retrieves a number of block hashes starting at a given
  403. // hash, fetching towards the genesis block.
  404. func (self *ChainManager) GetBlockHashesFromHash(hash common.Hash, max uint64) []common.Hash {
  405. // Get the origin header from which to fetch
  406. header := self.GetHeader(hash)
  407. if header == nil {
  408. return nil
  409. }
  410. // Iterate the headers until enough is collected or the genesis reached
  411. chain := make([]common.Hash, 0, max)
  412. for i := uint64(0); i < max; i++ {
  413. if header = self.GetHeader(header.ParentHash); header == nil {
  414. break
  415. }
  416. chain = append(chain, header.Hash())
  417. if header.Number.Cmp(common.Big0) == 0 {
  418. break
  419. }
  420. }
  421. return chain
  422. }
  423. // [deprecated by eth/62]
  424. // GetBlocksFromHash returns the block corresponding to hash and up to n-1 ancestors.
  425. func (self *ChainManager) GetBlocksFromHash(hash common.Hash, n int) (blocks []*types.Block) {
  426. for i := 0; i < n; i++ {
  427. block := self.GetBlock(hash)
  428. if block == nil {
  429. break
  430. }
  431. blocks = append(blocks, block)
  432. hash = block.ParentHash()
  433. }
  434. return
  435. }
  436. func (self *ChainManager) GetUnclesInChain(block *types.Block, length int) (uncles []*types.Header) {
  437. for i := 0; block != nil && i < length; i++ {
  438. uncles = append(uncles, block.Uncles()...)
  439. block = self.GetBlock(block.ParentHash())
  440. }
  441. return
  442. }
  443. // setTotalDifficulty updates the TD of the chain manager. Note, this function
  444. // assumes that the `mu` mutex is held!
  445. func (bc *ChainManager) setTotalDifficulty(td *big.Int) {
  446. bc.td = new(big.Int).Set(td)
  447. }
  448. func (bc *ChainManager) Stop() {
  449. if !atomic.CompareAndSwapInt32(&bc.running, 0, 1) {
  450. return
  451. }
  452. close(bc.quit)
  453. atomic.StoreInt32(&bc.procInterrupt, 1)
  454. bc.wg.Wait()
  455. glog.V(logger.Info).Infoln("Chain manager stopped")
  456. }
  457. type queueEvent struct {
  458. queue []interface{}
  459. canonicalCount int
  460. sideCount int
  461. splitCount int
  462. }
  463. func (self *ChainManager) procFutureBlocks() {
  464. blocks := make([]*types.Block, self.futureBlocks.Len())
  465. for i, hash := range self.futureBlocks.Keys() {
  466. block, _ := self.futureBlocks.Get(hash)
  467. blocks[i] = block.(*types.Block)
  468. }
  469. if len(blocks) > 0 {
  470. types.BlockBy(types.Number).Sort(blocks)
  471. self.InsertChain(blocks)
  472. }
  473. }
  474. type writeStatus byte
  475. const (
  476. NonStatTy writeStatus = iota
  477. CanonStatTy
  478. SplitStatTy
  479. SideStatTy
  480. )
  481. // WriteBlock writes the block to the chain.
  482. func (self *ChainManager) WriteBlock(block *types.Block) (status writeStatus, err error) {
  483. self.wg.Add(1)
  484. defer self.wg.Done()
  485. // Calculate the total difficulty of the block
  486. ptd := self.GetTd(block.ParentHash())
  487. if ptd == nil {
  488. return NonStatTy, ParentError(block.ParentHash())
  489. }
  490. td := new(big.Int).Add(block.Difficulty(), ptd)
  491. self.mu.RLock()
  492. cblock := self.currentBlock
  493. self.mu.RUnlock()
  494. // Compare the TD of the last known block in the canonical chain to make sure it's greater.
  495. // At this point it's possible that a different chain (fork) becomes the new canonical chain.
  496. if td.Cmp(self.Td()) > 0 {
  497. // chain fork
  498. if block.ParentHash() != cblock.Hash() {
  499. // during split we merge two different chains and create the new canonical chain
  500. err := self.reorg(cblock, block)
  501. if err != nil {
  502. return NonStatTy, err
  503. }
  504. }
  505. status = CanonStatTy
  506. self.mu.Lock()
  507. self.setTotalDifficulty(td)
  508. self.insert(block)
  509. self.mu.Unlock()
  510. } else {
  511. status = SideStatTy
  512. }
  513. if err := WriteTd(self.chainDb, block.Hash(), td); err != nil {
  514. glog.Fatalf("failed to write block total difficulty: %v", err)
  515. }
  516. if err := WriteBlock(self.chainDb, block); err != nil {
  517. glog.Fatalf("filed to write block contents: %v", err)
  518. }
  519. // Delete from future blocks
  520. self.futureBlocks.Remove(block.Hash())
  521. return
  522. }
  523. // InsertChain will attempt to insert the given chain in to the canonical chain or, otherwise, create a fork. It an error is returned
  524. // 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).
  525. func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
  526. self.wg.Add(1)
  527. defer self.wg.Done()
  528. self.chainmu.Lock()
  529. defer self.chainmu.Unlock()
  530. // A queued approach to delivering events. This is generally
  531. // faster than direct delivery and requires much less mutex
  532. // acquiring.
  533. var (
  534. queue = make([]interface{}, len(chain))
  535. queueEvent = queueEvent{queue: queue}
  536. stats struct{ queued, processed, ignored int }
  537. tstart = time.Now()
  538. nonceChecked = make([]bool, len(chain))
  539. )
  540. // Start the parallel nonce verifier.
  541. nonceAbort, nonceResults := verifyNoncesFromBlocks(self.pow, chain)
  542. defer close(nonceAbort)
  543. txcount := 0
  544. for i, block := range chain {
  545. if atomic.LoadInt32(&self.procInterrupt) == 1 {
  546. glog.V(logger.Debug).Infoln("Premature abort during chain processing")
  547. break
  548. }
  549. bstart := time.Now()
  550. // Wait for block i's nonce to be verified before processing
  551. // its state transition.
  552. for !nonceChecked[i] {
  553. r := <-nonceResults
  554. nonceChecked[r.index] = true
  555. if !r.valid {
  556. block := chain[r.index]
  557. return r.index, &BlockNonceErr{Hash: block.Hash(), Number: block.Number(), Nonce: block.Nonce()}
  558. }
  559. }
  560. if BadHashes[block.Hash()] {
  561. err := BadHashError(block.Hash())
  562. blockErr(block, err)
  563. return i, err
  564. }
  565. // Call in to the block processor and check for errors. It's likely that if one block fails
  566. // all others will fail too (unless a known block is returned).
  567. logs, receipts, err := self.processor.Process(block)
  568. if err != nil {
  569. if IsKnownBlockErr(err) {
  570. stats.ignored++
  571. continue
  572. }
  573. if err == BlockFutureErr {
  574. // Allow up to MaxFuture second in the future blocks. If this limit
  575. // is exceeded the chain is discarded and processed at a later time
  576. // if given.
  577. max := big.NewInt(time.Now().Unix() + maxTimeFutureBlocks)
  578. if block.Time().Cmp(max) == 1 {
  579. return i, fmt.Errorf("%v: BlockFutureErr, %v > %v", BlockFutureErr, block.Time(), max)
  580. }
  581. self.futureBlocks.Add(block.Hash(), block)
  582. stats.queued++
  583. continue
  584. }
  585. if IsParentErr(err) && self.futureBlocks.Contains(block.ParentHash()) {
  586. self.futureBlocks.Add(block.Hash(), block)
  587. stats.queued++
  588. continue
  589. }
  590. blockErr(block, err)
  591. go ReportBlock(block, err)
  592. return i, err
  593. }
  594. if err := PutBlockReceipts(self.chainDb, block, receipts); err != nil {
  595. glog.V(logger.Warn).Infoln("error writing block receipts:", err)
  596. }
  597. txcount += len(block.Transactions())
  598. // write the block to the chain and get the status
  599. status, err := self.WriteBlock(block)
  600. if err != nil {
  601. return i, err
  602. }
  603. switch status {
  604. case CanonStatTy:
  605. if glog.V(logger.Debug) {
  606. 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))
  607. }
  608. queue[i] = ChainEvent{block, block.Hash(), logs}
  609. queueEvent.canonicalCount++
  610. // This puts transactions in a extra db for rpc
  611. PutTransactions(self.chainDb, block, block.Transactions())
  612. // store the receipts
  613. PutReceipts(self.chainDb, receipts)
  614. case SideStatTy:
  615. if glog.V(logger.Detail) {
  616. 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))
  617. }
  618. queue[i] = ChainSideEvent{block, logs}
  619. queueEvent.sideCount++
  620. case SplitStatTy:
  621. queue[i] = ChainSplitEvent{block, logs}
  622. queueEvent.splitCount++
  623. }
  624. stats.processed++
  625. }
  626. if (stats.queued > 0 || stats.processed > 0 || stats.ignored > 0) && bool(glog.V(logger.Info)) {
  627. tend := time.Since(tstart)
  628. start, end := chain[0], chain[len(chain)-1]
  629. 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])
  630. }
  631. go self.eventMux.Post(queueEvent)
  632. return 0, nil
  633. }
  634. // reorgs takes two blocks, an old chain and a new chain and will reconstruct the blocks and inserts them
  635. // to be part of the new canonical chain and accumulates potential missing transactions and post an
  636. // event about them
  637. func (self *ChainManager) reorg(oldBlock, newBlock *types.Block) error {
  638. self.mu.Lock()
  639. defer self.mu.Unlock()
  640. var (
  641. newChain types.Blocks
  642. commonBlock *types.Block
  643. oldStart = oldBlock
  644. newStart = newBlock
  645. deletedTxs types.Transactions
  646. )
  647. // first reduce whoever is higher bound
  648. if oldBlock.NumberU64() > newBlock.NumberU64() {
  649. // reduce old chain
  650. for oldBlock = oldBlock; oldBlock != nil && oldBlock.NumberU64() != newBlock.NumberU64(); oldBlock = self.GetBlock(oldBlock.ParentHash()) {
  651. deletedTxs = append(deletedTxs, oldBlock.Transactions()...)
  652. }
  653. } else {
  654. // reduce new chain and append new chain blocks for inserting later on
  655. for newBlock = newBlock; newBlock != nil && newBlock.NumberU64() != oldBlock.NumberU64(); newBlock = self.GetBlock(newBlock.ParentHash()) {
  656. newChain = append(newChain, newBlock)
  657. }
  658. }
  659. if oldBlock == nil {
  660. return fmt.Errorf("Invalid old chain")
  661. }
  662. if newBlock == nil {
  663. return fmt.Errorf("Invalid new chain")
  664. }
  665. numSplit := newBlock.Number()
  666. for {
  667. if oldBlock.Hash() == newBlock.Hash() {
  668. commonBlock = oldBlock
  669. break
  670. }
  671. newChain = append(newChain, newBlock)
  672. deletedTxs = append(deletedTxs, oldBlock.Transactions()...)
  673. oldBlock, newBlock = self.GetBlock(oldBlock.ParentHash()), self.GetBlock(newBlock.ParentHash())
  674. if oldBlock == nil {
  675. return fmt.Errorf("Invalid old chain")
  676. }
  677. if newBlock == nil {
  678. return fmt.Errorf("Invalid new chain")
  679. }
  680. }
  681. if glog.V(logger.Debug) {
  682. commonHash := commonBlock.Hash()
  683. glog.Infof("Chain split detected @ %x. Reorganising chain from #%v %x to %x", commonHash[:4], numSplit, oldStart.Hash().Bytes()[:4], newStart.Hash().Bytes()[:4])
  684. }
  685. var addedTxs types.Transactions
  686. // insert blocks. Order does not matter. Last block will be written in ImportChain itself which creates the new head properly
  687. for _, block := range newChain {
  688. // insert the block in the canonical way, re-writing history
  689. self.insert(block)
  690. // write canonical receipts and transactions
  691. PutTransactions(self.chainDb, block, block.Transactions())
  692. PutReceipts(self.chainDb, GetBlockReceipts(self.chainDb, block.Hash()))
  693. addedTxs = append(addedTxs, block.Transactions()...)
  694. }
  695. // calculate the difference between deleted and added transactions
  696. diff := types.TxDifference(deletedTxs, addedTxs)
  697. // When transactions get deleted from the database that means the
  698. // receipts that were created in the fork must also be deleted
  699. for _, tx := range diff {
  700. DeleteReceipt(self.chainDb, tx.Hash())
  701. DeleteTransaction(self.chainDb, tx.Hash())
  702. }
  703. self.eventMux.Post(RemovedTransactionEvent{diff})
  704. return nil
  705. }
  706. func (self *ChainManager) update() {
  707. events := self.eventMux.Subscribe(queueEvent{})
  708. futureTimer := time.Tick(5 * time.Second)
  709. out:
  710. for {
  711. select {
  712. case ev := <-events.Chan():
  713. switch ev := ev.(type) {
  714. case queueEvent:
  715. for _, event := range ev.queue {
  716. switch event := event.(type) {
  717. case ChainEvent:
  718. // We need some control over the mining operation. Acquiring locks and waiting for the miner to create new block takes too long
  719. // and in most cases isn't even necessary.
  720. if self.currentBlock.Hash() == event.Hash {
  721. self.currentGasLimit = CalcGasLimit(event.Block)
  722. self.eventMux.Post(ChainHeadEvent{event.Block})
  723. }
  724. }
  725. self.eventMux.Post(event)
  726. }
  727. }
  728. case <-futureTimer:
  729. self.procFutureBlocks()
  730. case <-self.quit:
  731. break out
  732. }
  733. }
  734. }
  735. func blockErr(block *types.Block, err error) {
  736. h := block.Header()
  737. glog.V(logger.Error).Infof("Bad block #%v (%x)\n", h.Number, h.Hash().Bytes())
  738. glog.V(logger.Error).Infoln(err)
  739. glog.V(logger.Debug).Infoln(verifyNonces)
  740. }