chain_manager.go 23 KB

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