blockchain.go 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  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. crand "crypto/rand"
  20. "errors"
  21. "fmt"
  22. "io"
  23. "math"
  24. "math/big"
  25. mrand "math/rand"
  26. "runtime"
  27. "sync"
  28. "sync/atomic"
  29. "time"
  30. "github.com/ethereum/go-ethereum/common"
  31. "github.com/ethereum/go-ethereum/core/state"
  32. "github.com/ethereum/go-ethereum/core/types"
  33. "github.com/ethereum/go-ethereum/core/vm"
  34. "github.com/ethereum/go-ethereum/crypto"
  35. "github.com/ethereum/go-ethereum/ethdb"
  36. "github.com/ethereum/go-ethereum/event"
  37. "github.com/ethereum/go-ethereum/logger"
  38. "github.com/ethereum/go-ethereum/logger/glog"
  39. "github.com/ethereum/go-ethereum/metrics"
  40. "github.com/ethereum/go-ethereum/pow"
  41. "github.com/ethereum/go-ethereum/rlp"
  42. "github.com/ethereum/go-ethereum/trie"
  43. "github.com/hashicorp/golang-lru"
  44. )
  45. var (
  46. chainlogger = logger.NewLogger("CHAIN")
  47. jsonlogger = logger.NewJsonLogger()
  48. blockInsertTimer = metrics.NewTimer("chain/inserts")
  49. ErrNoGenesis = errors.New("Genesis not found in chain")
  50. )
  51. const (
  52. headerCacheLimit = 512
  53. bodyCacheLimit = 256
  54. tdCacheLimit = 1024
  55. blockCacheLimit = 256
  56. maxFutureBlocks = 256
  57. maxTimeFutureBlocks = 30
  58. // must be bumped when consensus algorithm is changed, this forces the upgradedb
  59. // command to be run (forces the blocks to be imported again using the new algorithm)
  60. BlockChainVersion = 3
  61. )
  62. // BlockChain represents the canonical chain given a database with a genesis
  63. // block. The Blockchain manages chain imports, reverts, chain reorganisations.
  64. //
  65. // Importing blocks in to the block chain happens according to the set of rules
  66. // defined by the two stage Validator. Processing of blocks is done using the
  67. // Processor which processes the included transaction. The validation of the state
  68. // is done in the second part of the Validator. Failing results in aborting of
  69. // the import.
  70. //
  71. // The BlockChain also helps in returning blocks from **any** chain included
  72. // in the database as well as blocks that represents the canonical chain. It's
  73. // important to note that GetBlock can return any block and does not need to be
  74. // included in the canonical one where as GetBlockByNumber always represents the
  75. // canonical chain.
  76. type BlockChain struct {
  77. chainDb ethdb.Database
  78. eventMux *event.TypeMux
  79. genesisBlock *types.Block
  80. // Last known total difficulty
  81. mu sync.RWMutex
  82. chainmu sync.RWMutex
  83. tsmu sync.RWMutex
  84. procmu sync.RWMutex
  85. checkpoint int // checkpoint counts towards the new checkpoint
  86. currentHeader *types.Header // Current head of the header chain (may be above the block chain!)
  87. currentBlock *types.Block // Current head of the block chain
  88. currentFastBlock *types.Block // Current head of the fast-sync chain (may be above the block chain!)
  89. headerCache *lru.Cache // Cache for the most recent block headers
  90. bodyCache *lru.Cache // Cache for the most recent block bodies
  91. bodyRLPCache *lru.Cache // Cache for the most recent block bodies in RLP encoded format
  92. tdCache *lru.Cache // Cache for the most recent block total difficulties
  93. blockCache *lru.Cache // Cache for the most recent entire blocks
  94. futureBlocks *lru.Cache // future blocks are blocks added for later processing
  95. quit chan struct{}
  96. running int32 // running must be called automically
  97. // procInterrupt must be atomically called
  98. procInterrupt int32 // interrupt signaler for block processing
  99. wg sync.WaitGroup
  100. pow pow.PoW
  101. rand *mrand.Rand
  102. processor Processor
  103. validator Validator
  104. }
  105. // NewBlockChain returns a fully initialised block chain using information
  106. // available in the database. It initialiser the default Ethereum Validator and
  107. // Processor.
  108. func NewBlockChain(chainDb ethdb.Database, pow pow.PoW, mux *event.TypeMux) (*BlockChain, error) {
  109. headerCache, _ := lru.New(headerCacheLimit)
  110. bodyCache, _ := lru.New(bodyCacheLimit)
  111. bodyRLPCache, _ := lru.New(bodyCacheLimit)
  112. tdCache, _ := lru.New(tdCacheLimit)
  113. blockCache, _ := lru.New(blockCacheLimit)
  114. futureBlocks, _ := lru.New(maxFutureBlocks)
  115. bc := &BlockChain{
  116. chainDb: chainDb,
  117. eventMux: mux,
  118. quit: make(chan struct{}),
  119. headerCache: headerCache,
  120. bodyCache: bodyCache,
  121. bodyRLPCache: bodyRLPCache,
  122. tdCache: tdCache,
  123. blockCache: blockCache,
  124. futureBlocks: futureBlocks,
  125. pow: pow,
  126. }
  127. // Seed a fast but crypto originating random generator
  128. seed, err := crand.Int(crand.Reader, big.NewInt(math.MaxInt64))
  129. if err != nil {
  130. return nil, err
  131. }
  132. bc.rand = mrand.New(mrand.NewSource(seed.Int64()))
  133. bc.SetValidator(NewBlockValidator(bc, pow))
  134. bc.SetProcessor(NewStateProcessor(bc))
  135. bc.genesisBlock = bc.GetBlockByNumber(0)
  136. if bc.genesisBlock == nil {
  137. bc.genesisBlock, err = WriteDefaultGenesisBlock(chainDb)
  138. if err != nil {
  139. return nil, err
  140. }
  141. glog.V(logger.Info).Infoln("WARNING: Wrote default ethereum genesis block")
  142. }
  143. if err := bc.loadLastState(); err != nil {
  144. return nil, err
  145. }
  146. // Check the current state of the block hashes and make sure that we do not have any of the bad blocks in our chain
  147. for hash, _ := range BadHashes {
  148. if header := bc.GetHeader(hash); header != nil {
  149. glog.V(logger.Error).Infof("Found bad hash, rewinding chain to block #%d [%x…]", header.Number, header.ParentHash[:4])
  150. bc.SetHead(header.Number.Uint64() - 1)
  151. glog.V(logger.Error).Infoln("Chain rewind was successful, resuming normal operation")
  152. }
  153. }
  154. // Take ownership of this particular state
  155. go bc.update()
  156. return bc, nil
  157. }
  158. // loadLastState loads the last known chain state from the database. This method
  159. // assumes that the chain manager mutex is held.
  160. func (self *BlockChain) loadLastState() error {
  161. // Restore the last known head block
  162. head := GetHeadBlockHash(self.chainDb)
  163. if head == (common.Hash{}) {
  164. // Corrupt or empty database, init from scratch
  165. self.Reset()
  166. } else {
  167. if block := self.GetBlock(head); block != nil {
  168. // Block found, set as the current head
  169. self.currentBlock = block
  170. } else {
  171. // Corrupt or empty database, init from scratch
  172. self.Reset()
  173. }
  174. }
  175. // Restore the last known head header
  176. self.currentHeader = self.currentBlock.Header()
  177. if head := GetHeadHeaderHash(self.chainDb); head != (common.Hash{}) {
  178. if header := self.GetHeader(head); header != nil {
  179. self.currentHeader = header
  180. }
  181. }
  182. // Restore the last known head fast block
  183. self.currentFastBlock = self.currentBlock
  184. if head := GetHeadFastBlockHash(self.chainDb); head != (common.Hash{}) {
  185. if block := self.GetBlock(head); block != nil {
  186. self.currentFastBlock = block
  187. }
  188. }
  189. // Issue a status log and return
  190. headerTd := self.GetTd(self.currentHeader.Hash())
  191. blockTd := self.GetTd(self.currentBlock.Hash())
  192. fastTd := self.GetTd(self.currentFastBlock.Hash())
  193. glog.V(logger.Info).Infof("Last header: #%d [%x…] TD=%v", self.currentHeader.Number, self.currentHeader.Hash().Bytes()[:4], headerTd)
  194. glog.V(logger.Info).Infof("Last block: #%d [%x…] TD=%v", self.currentBlock.Number(), self.currentBlock.Hash().Bytes()[:4], blockTd)
  195. glog.V(logger.Info).Infof("Fast block: #%d [%x…] TD=%v", self.currentFastBlock.Number(), self.currentFastBlock.Hash().Bytes()[:4], fastTd)
  196. return nil
  197. }
  198. // SetHead rewinds the local chain to a new head. In the case of headers, everything
  199. // above the new head will be deleted and the new one set. In the case of blocks
  200. // though, the head may be further rewound if block bodies are missing (non-archive
  201. // nodes after a fast sync).
  202. func (bc *BlockChain) SetHead(head uint64) {
  203. bc.mu.Lock()
  204. defer bc.mu.Unlock()
  205. // Figure out the highest known canonical headers and/or blocks
  206. height := uint64(0)
  207. if bc.currentHeader != nil {
  208. if hh := bc.currentHeader.Number.Uint64(); hh > height {
  209. height = hh
  210. }
  211. }
  212. if bc.currentBlock != nil {
  213. if bh := bc.currentBlock.NumberU64(); bh > height {
  214. height = bh
  215. }
  216. }
  217. if bc.currentFastBlock != nil {
  218. if fbh := bc.currentFastBlock.NumberU64(); fbh > height {
  219. height = fbh
  220. }
  221. }
  222. // Gather all the hashes that need deletion
  223. drop := make(map[common.Hash]struct{})
  224. for bc.currentHeader != nil && bc.currentHeader.Number.Uint64() > head {
  225. drop[bc.currentHeader.Hash()] = struct{}{}
  226. bc.currentHeader = bc.GetHeader(bc.currentHeader.ParentHash)
  227. }
  228. for bc.currentBlock != nil && bc.currentBlock.NumberU64() > head {
  229. drop[bc.currentBlock.Hash()] = struct{}{}
  230. bc.currentBlock = bc.GetBlock(bc.currentBlock.ParentHash())
  231. }
  232. for bc.currentFastBlock != nil && bc.currentFastBlock.NumberU64() > head {
  233. drop[bc.currentFastBlock.Hash()] = struct{}{}
  234. bc.currentFastBlock = bc.GetBlock(bc.currentFastBlock.ParentHash())
  235. }
  236. // Roll back the canonical chain numbering
  237. for i := height; i > head; i-- {
  238. DeleteCanonicalHash(bc.chainDb, i)
  239. }
  240. // Delete everything found by the above rewind
  241. for hash, _ := range drop {
  242. DeleteHeader(bc.chainDb, hash)
  243. DeleteBody(bc.chainDb, hash)
  244. DeleteTd(bc.chainDb, hash)
  245. }
  246. // Clear out any stale content from the caches
  247. bc.headerCache.Purge()
  248. bc.bodyCache.Purge()
  249. bc.bodyRLPCache.Purge()
  250. bc.blockCache.Purge()
  251. bc.futureBlocks.Purge()
  252. // Update all computed fields to the new head
  253. if bc.currentBlock == nil {
  254. bc.currentBlock = bc.genesisBlock
  255. }
  256. if bc.currentHeader == nil {
  257. bc.currentHeader = bc.genesisBlock.Header()
  258. }
  259. if bc.currentFastBlock == nil {
  260. bc.currentFastBlock = bc.genesisBlock
  261. }
  262. if err := WriteHeadBlockHash(bc.chainDb, bc.currentBlock.Hash()); err != nil {
  263. glog.Fatalf("failed to reset head block hash: %v", err)
  264. }
  265. if err := WriteHeadHeaderHash(bc.chainDb, bc.currentHeader.Hash()); err != nil {
  266. glog.Fatalf("failed to reset head header hash: %v", err)
  267. }
  268. if err := WriteHeadFastBlockHash(bc.chainDb, bc.currentFastBlock.Hash()); err != nil {
  269. glog.Fatalf("failed to reset head fast block hash: %v", err)
  270. }
  271. bc.loadLastState()
  272. }
  273. // FastSyncCommitHead sets the current head block to the one defined by the hash
  274. // irrelevant what the chain contents were prior.
  275. func (self *BlockChain) FastSyncCommitHead(hash common.Hash) error {
  276. // Make sure that both the block as well at its state trie exists
  277. block := self.GetBlock(hash)
  278. if block == nil {
  279. return fmt.Errorf("non existent block [%x…]", hash[:4])
  280. }
  281. if _, err := trie.NewSecure(block.Root(), self.chainDb); err != nil {
  282. return err
  283. }
  284. // If all checks out, manually set the head block
  285. self.mu.Lock()
  286. self.currentBlock = block
  287. self.mu.Unlock()
  288. glog.V(logger.Info).Infof("committed block #%d [%x…] as new head", block.Number(), hash[:4])
  289. return nil
  290. }
  291. // GasLimit returns the gas limit of the current HEAD block.
  292. func (self *BlockChain) GasLimit() *big.Int {
  293. self.mu.RLock()
  294. defer self.mu.RUnlock()
  295. return self.currentBlock.GasLimit()
  296. }
  297. // LastBlockHash return the hash of the HEAD block.
  298. func (self *BlockChain) LastBlockHash() common.Hash {
  299. self.mu.RLock()
  300. defer self.mu.RUnlock()
  301. return self.currentBlock.Hash()
  302. }
  303. // CurrentHeader retrieves the current head header of the canonical chain. The
  304. // header is retrieved from the blockchain's internal cache.
  305. func (self *BlockChain) CurrentHeader() *types.Header {
  306. self.mu.RLock()
  307. defer self.mu.RUnlock()
  308. return self.currentHeader
  309. }
  310. // CurrentBlock retrieves the current head block of the canonical chain. The
  311. // block is retrieved from the blockchain's internal cache.
  312. func (self *BlockChain) CurrentBlock() *types.Block {
  313. self.mu.RLock()
  314. defer self.mu.RUnlock()
  315. return self.currentBlock
  316. }
  317. // CurrentFastBlock retrieves the current fast-sync head block of the canonical
  318. // chain. The block is retrieved from the blockchain's internal cache.
  319. func (self *BlockChain) CurrentFastBlock() *types.Block {
  320. self.mu.RLock()
  321. defer self.mu.RUnlock()
  322. return self.currentFastBlock
  323. }
  324. // Status returns status information about the current chain such as the HEAD Td,
  325. // the HEAD hash and the hash of the genesis block.
  326. func (self *BlockChain) Status() (td *big.Int, currentBlock common.Hash, genesisBlock common.Hash) {
  327. self.mu.RLock()
  328. defer self.mu.RUnlock()
  329. return self.GetTd(self.currentBlock.Hash()), self.currentBlock.Hash(), self.genesisBlock.Hash()
  330. }
  331. // SetProcessor sets the processor required for making state modifications.
  332. func (self *BlockChain) SetProcessor(processor Processor) {
  333. self.procmu.Lock()
  334. defer self.procmu.Unlock()
  335. self.processor = processor
  336. }
  337. // SetValidator sets the validator which is used to validate incoming blocks.
  338. func (self *BlockChain) SetValidator(validator Validator) {
  339. self.procmu.Lock()
  340. defer self.procmu.Unlock()
  341. self.validator = validator
  342. }
  343. // Validator returns the current validator.
  344. func (self *BlockChain) Validator() Validator {
  345. self.procmu.RLock()
  346. defer self.procmu.RUnlock()
  347. return self.validator
  348. }
  349. // Processor returns the current processor.
  350. func (self *BlockChain) Processor() Processor {
  351. self.procmu.RLock()
  352. defer self.procmu.RUnlock()
  353. return self.processor
  354. }
  355. // AuxValidator returns the auxiliary validator (Proof of work atm)
  356. func (self *BlockChain) AuxValidator() pow.PoW { return self.pow }
  357. // State returns a new mutable state based on the current HEAD block.
  358. func (self *BlockChain) State() (*state.StateDB, error) {
  359. return state.New(self.CurrentBlock().Root(), self.chainDb)
  360. }
  361. // Reset purges the entire blockchain, restoring it to its genesis state.
  362. func (bc *BlockChain) Reset() {
  363. bc.ResetWithGenesisBlock(bc.genesisBlock)
  364. }
  365. // ResetWithGenesisBlock purges the entire blockchain, restoring it to the
  366. // specified genesis state.
  367. func (bc *BlockChain) ResetWithGenesisBlock(genesis *types.Block) {
  368. // Dump the entire block chain and purge the caches
  369. bc.SetHead(0)
  370. bc.mu.Lock()
  371. defer bc.mu.Unlock()
  372. // Prepare the genesis block and reinitialise the chain
  373. if err := WriteTd(bc.chainDb, genesis.Hash(), genesis.Difficulty()); err != nil {
  374. glog.Fatalf("failed to write genesis block TD: %v", err)
  375. }
  376. if err := WriteBlock(bc.chainDb, genesis); err != nil {
  377. glog.Fatalf("failed to write genesis block: %v", err)
  378. }
  379. bc.genesisBlock = genesis
  380. bc.insert(bc.genesisBlock)
  381. bc.currentBlock = bc.genesisBlock
  382. bc.currentHeader = bc.genesisBlock.Header()
  383. bc.currentFastBlock = bc.genesisBlock
  384. }
  385. // Export writes the active chain to the given writer.
  386. func (self *BlockChain) Export(w io.Writer) error {
  387. if err := self.ExportN(w, uint64(0), self.currentBlock.NumberU64()); err != nil {
  388. return err
  389. }
  390. return nil
  391. }
  392. // ExportN writes a subset of the active chain to the given writer.
  393. func (self *BlockChain) ExportN(w io.Writer, first uint64, last uint64) error {
  394. self.mu.RLock()
  395. defer self.mu.RUnlock()
  396. if first > last {
  397. return fmt.Errorf("export failed: first (%d) is greater than last (%d)", first, last)
  398. }
  399. glog.V(logger.Info).Infof("exporting %d blocks...\n", last-first+1)
  400. for nr := first; nr <= last; nr++ {
  401. block := self.GetBlockByNumber(nr)
  402. if block == nil {
  403. return fmt.Errorf("export failed on #%d: not found", nr)
  404. }
  405. if err := block.EncodeRLP(w); err != nil {
  406. return err
  407. }
  408. }
  409. return nil
  410. }
  411. // insert injects a new head block into the current block chain. This method
  412. // assumes that the block is indeed a true head. It will also reset the head
  413. // header and the head fast sync block to this very same block if they are older
  414. // or if they are on a different side chain.
  415. //
  416. // Note, this function assumes that the `mu` mutex is held!
  417. func (bc *BlockChain) insert(block *types.Block) {
  418. // If the block is on a side chain or an unknown one, force other heads onto it too
  419. updateHeads := GetCanonicalHash(bc.chainDb, block.NumberU64()) != block.Hash()
  420. // Add the block to the canonical chain number scheme and mark as the head
  421. if err := WriteCanonicalHash(bc.chainDb, block.Hash(), block.NumberU64()); err != nil {
  422. glog.Fatalf("failed to insert block number: %v", err)
  423. }
  424. if err := WriteHeadBlockHash(bc.chainDb, block.Hash()); err != nil {
  425. glog.Fatalf("failed to insert head block hash: %v", err)
  426. }
  427. bc.currentBlock = block
  428. // If the block is better than out head or is on a different chain, force update heads
  429. if updateHeads {
  430. if err := WriteHeadHeaderHash(bc.chainDb, block.Hash()); err != nil {
  431. glog.Fatalf("failed to insert head header hash: %v", err)
  432. }
  433. bc.currentHeader = block.Header()
  434. if err := WriteHeadFastBlockHash(bc.chainDb, block.Hash()); err != nil {
  435. glog.Fatalf("failed to insert head fast block hash: %v", err)
  436. }
  437. bc.currentFastBlock = block
  438. }
  439. }
  440. // Accessors
  441. func (bc *BlockChain) Genesis() *types.Block {
  442. return bc.genesisBlock
  443. }
  444. // HasHeader checks if a block header is present in the database or not, caching
  445. // it if present.
  446. func (bc *BlockChain) HasHeader(hash common.Hash) bool {
  447. return bc.GetHeader(hash) != nil
  448. }
  449. // GetHeader retrieves a block header from the database by hash, caching it if
  450. // found.
  451. func (self *BlockChain) GetHeader(hash common.Hash) *types.Header {
  452. // Short circuit if the header's already in the cache, retrieve otherwise
  453. if header, ok := self.headerCache.Get(hash); ok {
  454. return header.(*types.Header)
  455. }
  456. header := GetHeader(self.chainDb, hash)
  457. if header == nil {
  458. return nil
  459. }
  460. // Cache the found header for next time and return
  461. self.headerCache.Add(header.Hash(), header)
  462. return header
  463. }
  464. // GetHeaderByNumber retrieves a block header from the database by number,
  465. // caching it (associated with its hash) if found.
  466. func (self *BlockChain) GetHeaderByNumber(number uint64) *types.Header {
  467. hash := GetCanonicalHash(self.chainDb, number)
  468. if hash == (common.Hash{}) {
  469. return nil
  470. }
  471. return self.GetHeader(hash)
  472. }
  473. // GetBody retrieves a block body (transactions and uncles) from the database by
  474. // hash, caching it if found.
  475. func (self *BlockChain) GetBody(hash common.Hash) *types.Body {
  476. // Short circuit if the body's already in the cache, retrieve otherwise
  477. if cached, ok := self.bodyCache.Get(hash); ok {
  478. body := cached.(*types.Body)
  479. return body
  480. }
  481. body := GetBody(self.chainDb, hash)
  482. if body == nil {
  483. return nil
  484. }
  485. // Cache the found body for next time and return
  486. self.bodyCache.Add(hash, body)
  487. return body
  488. }
  489. // GetBodyRLP retrieves a block body in RLP encoding from the database by hash,
  490. // caching it if found.
  491. func (self *BlockChain) GetBodyRLP(hash common.Hash) rlp.RawValue {
  492. // Short circuit if the body's already in the cache, retrieve otherwise
  493. if cached, ok := self.bodyRLPCache.Get(hash); ok {
  494. return cached.(rlp.RawValue)
  495. }
  496. body := GetBodyRLP(self.chainDb, hash)
  497. if len(body) == 0 {
  498. return nil
  499. }
  500. // Cache the found body for next time and return
  501. self.bodyRLPCache.Add(hash, body)
  502. return body
  503. }
  504. // GetTd retrieves a block's total difficulty in the canonical chain from the
  505. // database by hash, caching it if found.
  506. func (self *BlockChain) GetTd(hash common.Hash) *big.Int {
  507. // Short circuit if the td's already in the cache, retrieve otherwise
  508. if cached, ok := self.tdCache.Get(hash); ok {
  509. return cached.(*big.Int)
  510. }
  511. td := GetTd(self.chainDb, hash)
  512. if td == nil {
  513. return nil
  514. }
  515. // Cache the found body for next time and return
  516. self.tdCache.Add(hash, td)
  517. return td
  518. }
  519. // HasBlock checks if a block is fully present in the database or not, caching
  520. // it if present.
  521. func (bc *BlockChain) HasBlock(hash common.Hash) bool {
  522. return bc.GetBlock(hash) != nil
  523. }
  524. // HasBlockAndState checks if a block and associated state trie is fully present
  525. // in the database or not, caching it if present.
  526. func (bc *BlockChain) HasBlockAndState(hash common.Hash) bool {
  527. // Check first that the block itself is known
  528. block := bc.GetBlock(hash)
  529. if block == nil {
  530. return false
  531. }
  532. // Ensure the associated state is also present
  533. _, err := state.New(block.Root(), bc.chainDb)
  534. return err == nil
  535. }
  536. // GetBlock retrieves a block from the database by hash, caching it if found.
  537. func (self *BlockChain) GetBlock(hash common.Hash) *types.Block {
  538. // Short circuit if the block's already in the cache, retrieve otherwise
  539. if block, ok := self.blockCache.Get(hash); ok {
  540. return block.(*types.Block)
  541. }
  542. block := GetBlock(self.chainDb, hash)
  543. if block == nil {
  544. return nil
  545. }
  546. // Cache the found block for next time and return
  547. self.blockCache.Add(block.Hash(), block)
  548. return block
  549. }
  550. // GetBlockByNumber retrieves a block from the database by number, caching it
  551. // (associated with its hash) if found.
  552. func (self *BlockChain) GetBlockByNumber(number uint64) *types.Block {
  553. hash := GetCanonicalHash(self.chainDb, number)
  554. if hash == (common.Hash{}) {
  555. return nil
  556. }
  557. return self.GetBlock(hash)
  558. }
  559. // GetBlockHashesFromHash retrieves a number of block hashes starting at a given
  560. // hash, fetching towards the genesis block.
  561. func (self *BlockChain) GetBlockHashesFromHash(hash common.Hash, max uint64) []common.Hash {
  562. // Get the origin header from which to fetch
  563. header := self.GetHeader(hash)
  564. if header == nil {
  565. return nil
  566. }
  567. // Iterate the headers until enough is collected or the genesis reached
  568. chain := make([]common.Hash, 0, max)
  569. for i := uint64(0); i < max; i++ {
  570. if header = self.GetHeader(header.ParentHash); header == nil {
  571. break
  572. }
  573. chain = append(chain, header.Hash())
  574. if header.Number.Cmp(common.Big0) == 0 {
  575. break
  576. }
  577. }
  578. return chain
  579. }
  580. // [deprecated by eth/62]
  581. // GetBlocksFromHash returns the block corresponding to hash and up to n-1 ancestors.
  582. func (self *BlockChain) GetBlocksFromHash(hash common.Hash, n int) (blocks []*types.Block) {
  583. for i := 0; i < n; i++ {
  584. block := self.GetBlock(hash)
  585. if block == nil {
  586. break
  587. }
  588. blocks = append(blocks, block)
  589. hash = block.ParentHash()
  590. }
  591. return
  592. }
  593. // GetUnclesInChain retrieves all the uncles from a given block backwards until
  594. // a specific distance is reached.
  595. func (self *BlockChain) GetUnclesInChain(block *types.Block, length int) []*types.Header {
  596. uncles := []*types.Header{}
  597. for i := 0; block != nil && i < length; i++ {
  598. uncles = append(uncles, block.Uncles()...)
  599. block = self.GetBlock(block.ParentHash())
  600. }
  601. return uncles
  602. }
  603. // Stop stops the blockchain service. If any imports are currently in progress
  604. // it will abort them using the procInterrupt.
  605. func (bc *BlockChain) Stop() {
  606. if !atomic.CompareAndSwapInt32(&bc.running, 0, 1) {
  607. return
  608. }
  609. close(bc.quit)
  610. atomic.StoreInt32(&bc.procInterrupt, 1)
  611. bc.wg.Wait()
  612. glog.V(logger.Info).Infoln("Chain manager stopped")
  613. }
  614. func (self *BlockChain) procFutureBlocks() {
  615. blocks := make([]*types.Block, self.futureBlocks.Len())
  616. for i, hash := range self.futureBlocks.Keys() {
  617. block, _ := self.futureBlocks.Get(hash)
  618. blocks[i] = block.(*types.Block)
  619. }
  620. if len(blocks) > 0 {
  621. types.BlockBy(types.Number).Sort(blocks)
  622. self.InsertChain(blocks)
  623. }
  624. }
  625. type writeStatus byte
  626. const (
  627. NonStatTy writeStatus = iota
  628. CanonStatTy
  629. SplitStatTy
  630. SideStatTy
  631. )
  632. // writeHeader writes a header into the local chain, given that its parent is
  633. // already known. If the total difficulty of the newly inserted header becomes
  634. // greater than the current known TD, the canonical chain is re-routed.
  635. //
  636. // Note: This method is not concurrent-safe with inserting blocks simultaneously
  637. // into the chain, as side effects caused by reorganisations cannot be emulated
  638. // without the real blocks. Hence, writing headers directly should only be done
  639. // in two scenarios: pure-header mode of operation (light clients), or properly
  640. // separated header/block phases (non-archive clients).
  641. func (self *BlockChain) writeHeader(header *types.Header) error {
  642. self.wg.Add(1)
  643. defer self.wg.Done()
  644. // Calculate the total difficulty of the header
  645. ptd := self.GetTd(header.ParentHash)
  646. if ptd == nil {
  647. return ParentError(header.ParentHash)
  648. }
  649. localTd := self.GetTd(self.currentHeader.Hash())
  650. externTd := new(big.Int).Add(header.Difficulty, ptd)
  651. // Make sure no inconsistent state is leaked during insertion
  652. self.mu.Lock()
  653. defer self.mu.Unlock()
  654. // If the total difficulty is higher than our known, add it to the canonical chain
  655. // Second clause in the if statement reduces the vulnerability to selfish mining.
  656. // Please refer to http://www.cs.cornell.edu/~ie53/publications/btcProcFC.pdf
  657. if externTd.Cmp(localTd) > 0 || (externTd.Cmp(localTd) == 0 && mrand.Float64() < 0.5) {
  658. // Delete any canonical number assignments above the new head
  659. for i := header.Number.Uint64() + 1; GetCanonicalHash(self.chainDb, i) != (common.Hash{}); i++ {
  660. DeleteCanonicalHash(self.chainDb, i)
  661. }
  662. // Overwrite any stale canonical number assignments
  663. head := self.GetHeader(header.ParentHash)
  664. for GetCanonicalHash(self.chainDb, head.Number.Uint64()) != head.Hash() {
  665. WriteCanonicalHash(self.chainDb, head.Hash(), head.Number.Uint64())
  666. head = self.GetHeader(head.ParentHash)
  667. }
  668. // Extend the canonical chain with the new header
  669. if err := WriteCanonicalHash(self.chainDb, header.Hash(), header.Number.Uint64()); err != nil {
  670. glog.Fatalf("failed to insert header number: %v", err)
  671. }
  672. if err := WriteHeadHeaderHash(self.chainDb, header.Hash()); err != nil {
  673. glog.Fatalf("failed to insert head header hash: %v", err)
  674. }
  675. self.currentHeader = types.CopyHeader(header)
  676. }
  677. // Irrelevant of the canonical status, write the header itself to the database
  678. if err := WriteTd(self.chainDb, header.Hash(), externTd); err != nil {
  679. glog.Fatalf("failed to write header total difficulty: %v", err)
  680. }
  681. if err := WriteHeader(self.chainDb, header); err != nil {
  682. glog.Fatalf("filed to write header contents: %v", err)
  683. }
  684. return nil
  685. }
  686. // InsertHeaderChain attempts to insert the given header chain in to the local
  687. // chain, possibly creating a reorg. If an error is returned, it will return the
  688. // index number of the failing header as well an error describing what went wrong.
  689. //
  690. // The verify parameter can be used to fine tune whether nonce verification
  691. // should be done or not. The reason behind the optional check is because some
  692. // of the header retrieval mechanisms already need to verfy nonces, as well as
  693. // because nonces can be verified sparsely, not needing to check each.
  694. func (self *BlockChain) InsertHeaderChain(chain []*types.Header, checkFreq int) (int, error) {
  695. self.wg.Add(1)
  696. defer self.wg.Done()
  697. // Make sure only one thread manipulates the chain at once
  698. self.chainmu.Lock()
  699. defer self.chainmu.Unlock()
  700. // Collect some import statistics to report on
  701. stats := struct{ processed, ignored int }{}
  702. start := time.Now()
  703. // Generate the list of headers that should be POW verified
  704. verify := make([]bool, len(chain))
  705. for i := 0; i < len(verify)/checkFreq; i++ {
  706. index := i*checkFreq + self.rand.Intn(checkFreq)
  707. if index >= len(verify) {
  708. index = len(verify) - 1
  709. }
  710. verify[index] = true
  711. }
  712. verify[len(verify)-1] = true // Last should always be verified to avoid junk
  713. // Create the header verification task queue and worker functions
  714. tasks := make(chan int, len(chain))
  715. for i := 0; i < len(chain); i++ {
  716. tasks <- i
  717. }
  718. close(tasks)
  719. errs, failed := make([]error, len(tasks)), int32(0)
  720. process := func(worker int) {
  721. for index := range tasks {
  722. header, hash := chain[index], chain[index].Hash()
  723. // Short circuit insertion if shutting down or processing failed
  724. if atomic.LoadInt32(&self.procInterrupt) == 1 {
  725. return
  726. }
  727. if atomic.LoadInt32(&failed) > 0 {
  728. return
  729. }
  730. // Short circuit if the header is bad or already known
  731. if BadHashes[hash] {
  732. errs[index] = BadHashError(hash)
  733. atomic.AddInt32(&failed, 1)
  734. return
  735. }
  736. if self.HasHeader(hash) {
  737. continue
  738. }
  739. // Verify that the header honors the chain parameters
  740. checkPow := verify[index]
  741. var err error
  742. if index == 0 {
  743. err = self.Validator().ValidateHeader(header, self.GetHeader(header.ParentHash), checkPow)
  744. } else {
  745. err = self.Validator().ValidateHeader(header, chain[index-1], checkPow)
  746. }
  747. if err != nil {
  748. errs[index] = err
  749. atomic.AddInt32(&failed, 1)
  750. return
  751. }
  752. }
  753. }
  754. // Start as many worker threads as goroutines allowed
  755. pending := new(sync.WaitGroup)
  756. for i := 0; i < runtime.GOMAXPROCS(0); i++ {
  757. pending.Add(1)
  758. go func(id int) {
  759. defer pending.Done()
  760. process(id)
  761. }(i)
  762. }
  763. pending.Wait()
  764. // If anything failed, report
  765. if failed > 0 {
  766. for i, err := range errs {
  767. if err != nil {
  768. return i, err
  769. }
  770. }
  771. }
  772. // All headers passed verification, import them into the database
  773. for i, header := range chain {
  774. // Short circuit insertion if shutting down
  775. if atomic.LoadInt32(&self.procInterrupt) == 1 {
  776. glog.V(logger.Debug).Infoln("premature abort during header chain processing")
  777. break
  778. }
  779. hash := header.Hash()
  780. // If the header's already known, skip it, otherwise store
  781. if self.HasHeader(hash) {
  782. stats.ignored++
  783. continue
  784. }
  785. if err := self.writeHeader(header); err != nil {
  786. return i, err
  787. }
  788. stats.processed++
  789. }
  790. // Report some public statistics so the user has a clue what's going on
  791. first, last := chain[0], chain[len(chain)-1]
  792. glog.V(logger.Info).Infof("imported %d header(s) (%d ignored) in %v. #%v [%x… / %x…]", stats.processed, stats.ignored,
  793. time.Since(start), last.Number, first.Hash().Bytes()[:4], last.Hash().Bytes()[:4])
  794. return 0, nil
  795. }
  796. // Rollback is designed to remove a chain of links from the database that aren't
  797. // certain enough to be valid.
  798. func (self *BlockChain) Rollback(chain []common.Hash) {
  799. self.mu.Lock()
  800. defer self.mu.Unlock()
  801. for i := len(chain) - 1; i >= 0; i-- {
  802. hash := chain[i]
  803. if self.currentHeader.Hash() == hash {
  804. self.currentHeader = self.GetHeader(self.currentHeader.ParentHash)
  805. WriteHeadHeaderHash(self.chainDb, self.currentHeader.Hash())
  806. }
  807. if self.currentFastBlock.Hash() == hash {
  808. self.currentFastBlock = self.GetBlock(self.currentFastBlock.ParentHash())
  809. WriteHeadFastBlockHash(self.chainDb, self.currentFastBlock.Hash())
  810. }
  811. if self.currentBlock.Hash() == hash {
  812. self.currentBlock = self.GetBlock(self.currentBlock.ParentHash())
  813. WriteHeadBlockHash(self.chainDb, self.currentBlock.Hash())
  814. }
  815. }
  816. }
  817. // InsertReceiptChain attempts to complete an already existing header chain with
  818. // transaction and receipt data.
  819. func (self *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain []types.Receipts) (int, error) {
  820. self.wg.Add(1)
  821. defer self.wg.Done()
  822. // Collect some import statistics to report on
  823. stats := struct{ processed, ignored int32 }{}
  824. start := time.Now()
  825. // Create the block importing task queue and worker functions
  826. tasks := make(chan int, len(blockChain))
  827. for i := 0; i < len(blockChain) && i < len(receiptChain); i++ {
  828. tasks <- i
  829. }
  830. close(tasks)
  831. errs, failed := make([]error, len(tasks)), int32(0)
  832. process := func(worker int) {
  833. for index := range tasks {
  834. block, receipts := blockChain[index], receiptChain[index]
  835. // Short circuit insertion if shutting down or processing failed
  836. if atomic.LoadInt32(&self.procInterrupt) == 1 {
  837. return
  838. }
  839. if atomic.LoadInt32(&failed) > 0 {
  840. return
  841. }
  842. // Short circuit if the owner header is unknown
  843. if !self.HasHeader(block.Hash()) {
  844. errs[index] = fmt.Errorf("containing header #%d [%x…] unknown", block.Number(), block.Hash().Bytes()[:4])
  845. atomic.AddInt32(&failed, 1)
  846. return
  847. }
  848. // Skip if the entire data is already known
  849. if self.HasBlock(block.Hash()) {
  850. atomic.AddInt32(&stats.ignored, 1)
  851. continue
  852. }
  853. // Compute all the non-consensus fields of the receipts
  854. transactions, logIndex := block.Transactions(), uint(0)
  855. for j := 0; j < len(receipts); j++ {
  856. // The transaction hash can be retrieved from the transaction itself
  857. receipts[j].TxHash = transactions[j].Hash()
  858. // The contract address can be derived from the transaction itself
  859. if MessageCreatesContract(transactions[j]) {
  860. from, _ := transactions[j].From()
  861. receipts[j].ContractAddress = crypto.CreateAddress(from, transactions[j].Nonce())
  862. }
  863. // The used gas can be calculated based on previous receipts
  864. if j == 0 {
  865. receipts[j].GasUsed = new(big.Int).Set(receipts[j].CumulativeGasUsed)
  866. } else {
  867. receipts[j].GasUsed = new(big.Int).Sub(receipts[j].CumulativeGasUsed, receipts[j-1].CumulativeGasUsed)
  868. }
  869. // The derived log fields can simply be set from the block and transaction
  870. for k := 0; k < len(receipts[j].Logs); k++ {
  871. receipts[j].Logs[k].BlockNumber = block.NumberU64()
  872. receipts[j].Logs[k].BlockHash = block.Hash()
  873. receipts[j].Logs[k].TxHash = receipts[j].TxHash
  874. receipts[j].Logs[k].TxIndex = uint(j)
  875. receipts[j].Logs[k].Index = logIndex
  876. logIndex++
  877. }
  878. }
  879. // Write all the data out into the database
  880. if err := WriteBody(self.chainDb, block.Hash(), &types.Body{block.Transactions(), block.Uncles()}); err != nil {
  881. errs[index] = fmt.Errorf("failed to write block body: %v", err)
  882. atomic.AddInt32(&failed, 1)
  883. glog.Fatal(errs[index])
  884. return
  885. }
  886. if err := WriteBlockReceipts(self.chainDb, block.Hash(), receipts); err != nil {
  887. errs[index] = fmt.Errorf("failed to write block receipts: %v", err)
  888. atomic.AddInt32(&failed, 1)
  889. glog.Fatal(errs[index])
  890. return
  891. }
  892. if err := WriteMipmapBloom(self.chainDb, block.NumberU64(), receipts); err != nil {
  893. errs[index] = fmt.Errorf("failed to write log blooms: %v", err)
  894. atomic.AddInt32(&failed, 1)
  895. glog.Fatal(errs[index])
  896. return
  897. }
  898. if err := WriteTransactions(self.chainDb, block); err != nil {
  899. errs[index] = fmt.Errorf("failed to write individual transactions: %v", err)
  900. atomic.AddInt32(&failed, 1)
  901. glog.Fatal(errs[index])
  902. return
  903. }
  904. if err := WriteReceipts(self.chainDb, receipts); err != nil {
  905. errs[index] = fmt.Errorf("failed to write individual receipts: %v", err)
  906. atomic.AddInt32(&failed, 1)
  907. glog.Fatal(errs[index])
  908. return
  909. }
  910. atomic.AddInt32(&stats.processed, 1)
  911. }
  912. }
  913. // Start as many worker threads as goroutines allowed
  914. pending := new(sync.WaitGroup)
  915. for i := 0; i < runtime.GOMAXPROCS(0); i++ {
  916. pending.Add(1)
  917. go func(id int) {
  918. defer pending.Done()
  919. process(id)
  920. }(i)
  921. }
  922. pending.Wait()
  923. // If anything failed, report
  924. if failed > 0 {
  925. for i, err := range errs {
  926. if err != nil {
  927. return i, err
  928. }
  929. }
  930. }
  931. if atomic.LoadInt32(&self.procInterrupt) == 1 {
  932. glog.V(logger.Debug).Infoln("premature abort during receipt chain processing")
  933. return 0, nil
  934. }
  935. // Update the head fast sync block if better
  936. self.mu.Lock()
  937. head := blockChain[len(errs)-1]
  938. if self.GetTd(self.currentFastBlock.Hash()).Cmp(self.GetTd(head.Hash())) < 0 {
  939. if err := WriteHeadFastBlockHash(self.chainDb, head.Hash()); err != nil {
  940. glog.Fatalf("failed to update head fast block hash: %v", err)
  941. }
  942. self.currentFastBlock = head
  943. }
  944. self.mu.Unlock()
  945. // Report some public statistics so the user has a clue what's going on
  946. first, last := blockChain[0], blockChain[len(blockChain)-1]
  947. glog.V(logger.Info).Infof("imported %d receipt(s) (%d ignored) in %v. #%d [%x… / %x…]", stats.processed, stats.ignored,
  948. time.Since(start), last.Number(), first.Hash().Bytes()[:4], last.Hash().Bytes()[:4])
  949. return 0, nil
  950. }
  951. // WriteBlock writes the block to the chain.
  952. func (self *BlockChain) WriteBlock(block *types.Block) (status writeStatus, err error) {
  953. self.wg.Add(1)
  954. defer self.wg.Done()
  955. // Calculate the total difficulty of the block
  956. ptd := self.GetTd(block.ParentHash())
  957. if ptd == nil {
  958. return NonStatTy, ParentError(block.ParentHash())
  959. }
  960. localTd := self.GetTd(self.currentBlock.Hash())
  961. externTd := new(big.Int).Add(block.Difficulty(), ptd)
  962. // Make sure no inconsistent state is leaked during insertion
  963. self.mu.Lock()
  964. defer self.mu.Unlock()
  965. // If the total difficulty is higher than our known, add it to the canonical chain
  966. // Second clause in the if statement reduces the vulnerability to selfish mining.
  967. // Please refer to http://www.cs.cornell.edu/~ie53/publications/btcProcFC.pdf
  968. if externTd.Cmp(localTd) > 0 || (externTd.Cmp(localTd) == 0 && mrand.Float64() < 0.5) {
  969. // Reorganize the chain if the parent is not the head block
  970. if block.ParentHash() != self.currentBlock.Hash() {
  971. if err := self.reorg(self.currentBlock, block); err != nil {
  972. return NonStatTy, err
  973. }
  974. }
  975. // Insert the block as the new head of the chain
  976. self.insert(block)
  977. status = CanonStatTy
  978. } else {
  979. status = SideStatTy
  980. }
  981. // Irrelevant of the canonical status, write the block itself to the database
  982. if err := WriteTd(self.chainDb, block.Hash(), externTd); err != nil {
  983. glog.Fatalf("failed to write block total difficulty: %v", err)
  984. }
  985. if err := WriteBlock(self.chainDb, block); err != nil {
  986. glog.Fatalf("filed to write block contents: %v", err)
  987. }
  988. self.futureBlocks.Remove(block.Hash())
  989. return
  990. }
  991. // InsertChain will attempt to insert the given chain in to the canonical chain or, otherwise, create a fork. It an error is returned
  992. // 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).
  993. func (self *BlockChain) InsertChain(chain types.Blocks) (int, error) {
  994. self.wg.Add(1)
  995. defer self.wg.Done()
  996. self.chainmu.Lock()
  997. defer self.chainmu.Unlock()
  998. // A queued approach to delivering events. This is generally
  999. // faster than direct delivery and requires much less mutex
  1000. // acquiring.
  1001. var (
  1002. stats struct{ queued, processed, ignored int }
  1003. events = make([]interface{}, 0, len(chain))
  1004. coalescedLogs vm.Logs
  1005. tstart = time.Now()
  1006. nonceChecked = make([]bool, len(chain))
  1007. )
  1008. // Start the parallel nonce verifier.
  1009. nonceAbort, nonceResults := verifyNoncesFromBlocks(self.pow, chain)
  1010. defer close(nonceAbort)
  1011. txcount := 0
  1012. for i, block := range chain {
  1013. if atomic.LoadInt32(&self.procInterrupt) == 1 {
  1014. glog.V(logger.Debug).Infoln("Premature abort during block chain processing")
  1015. break
  1016. }
  1017. bstart := time.Now()
  1018. // Wait for block i's nonce to be verified before processing
  1019. // its state transition.
  1020. for !nonceChecked[i] {
  1021. r := <-nonceResults
  1022. nonceChecked[r.index] = true
  1023. if !r.valid {
  1024. block := chain[r.index]
  1025. return r.index, &BlockNonceErr{Hash: block.Hash(), Number: block.Number(), Nonce: block.Nonce()}
  1026. }
  1027. }
  1028. if BadHashes[block.Hash()] {
  1029. err := BadHashError(block.Hash())
  1030. reportBlock(block, err)
  1031. return i, err
  1032. }
  1033. // Stage 1 validation of the block using the chain's validator
  1034. // interface.
  1035. err := self.Validator().ValidateBlock(block)
  1036. if err != nil {
  1037. if IsKnownBlockErr(err) {
  1038. stats.ignored++
  1039. continue
  1040. }
  1041. if err == BlockFutureErr {
  1042. // Allow up to MaxFuture second in the future blocks. If this limit
  1043. // is exceeded the chain is discarded and processed at a later time
  1044. // if given.
  1045. max := big.NewInt(time.Now().Unix() + maxTimeFutureBlocks)
  1046. if block.Time().Cmp(max) == 1 {
  1047. return i, fmt.Errorf("%v: BlockFutureErr, %v > %v", BlockFutureErr, block.Time(), max)
  1048. }
  1049. self.futureBlocks.Add(block.Hash(), block)
  1050. stats.queued++
  1051. continue
  1052. }
  1053. if IsParentErr(err) && self.futureBlocks.Contains(block.ParentHash()) {
  1054. self.futureBlocks.Add(block.Hash(), block)
  1055. stats.queued++
  1056. continue
  1057. }
  1058. reportBlock(block, err)
  1059. return i, err
  1060. }
  1061. // Create a new statedb using the parent block and report an
  1062. // error if it fails.
  1063. statedb, err := state.New(self.GetBlock(block.ParentHash()).Root(), self.chainDb)
  1064. if err != nil {
  1065. reportBlock(block, err)
  1066. return i, err
  1067. }
  1068. // Process block using the parent state as reference point.
  1069. receipts, logs, usedGas, err := self.processor.Process(block, statedb)
  1070. if err != nil {
  1071. reportBlock(block, err)
  1072. return i, err
  1073. }
  1074. // Validate the state using the default validator
  1075. err = self.Validator().ValidateState(block, self.GetBlock(block.ParentHash()), statedb, receipts, usedGas)
  1076. if err != nil {
  1077. reportBlock(block, err)
  1078. return i, err
  1079. }
  1080. // Write state changes to database
  1081. _, err = statedb.Commit()
  1082. if err != nil {
  1083. return i, err
  1084. }
  1085. // coalesce logs for later processing
  1086. coalescedLogs = append(coalescedLogs, logs...)
  1087. if err := WriteBlockReceipts(self.chainDb, block.Hash(), receipts); err != nil {
  1088. return i, err
  1089. }
  1090. txcount += len(block.Transactions())
  1091. // write the block to the chain and get the status
  1092. status, err := self.WriteBlock(block)
  1093. if err != nil {
  1094. return i, err
  1095. }
  1096. switch status {
  1097. case CanonStatTy:
  1098. if glog.V(logger.Debug) {
  1099. 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))
  1100. }
  1101. events = append(events, ChainEvent{block, block.Hash(), logs})
  1102. // This puts transactions in a extra db for rpc
  1103. if err := WriteTransactions(self.chainDb, block); err != nil {
  1104. return i, err
  1105. }
  1106. // store the receipts
  1107. if err := WriteReceipts(self.chainDb, receipts); err != nil {
  1108. return i, err
  1109. }
  1110. // Write map map bloom filters
  1111. if err := WriteMipmapBloom(self.chainDb, block.NumberU64(), receipts); err != nil {
  1112. return i, err
  1113. }
  1114. case SideStatTy:
  1115. if glog.V(logger.Detail) {
  1116. 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))
  1117. }
  1118. events = append(events, ChainSideEvent{block, logs})
  1119. case SplitStatTy:
  1120. events = append(events, ChainSplitEvent{block, logs})
  1121. }
  1122. stats.processed++
  1123. }
  1124. if (stats.queued > 0 || stats.processed > 0 || stats.ignored > 0) && bool(glog.V(logger.Info)) {
  1125. tend := time.Since(tstart)
  1126. start, end := chain[0], chain[len(chain)-1]
  1127. 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])
  1128. }
  1129. go self.postChainEvents(events, coalescedLogs)
  1130. return 0, nil
  1131. }
  1132. // reorgs takes two blocks, an old chain and a new chain and will reconstruct the blocks and inserts them
  1133. // to be part of the new canonical chain and accumulates potential missing transactions and post an
  1134. // event about them
  1135. func (self *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
  1136. var (
  1137. newChain types.Blocks
  1138. commonBlock *types.Block
  1139. oldStart = oldBlock
  1140. newStart = newBlock
  1141. deletedTxs types.Transactions
  1142. deletedLogs vm.Logs
  1143. // collectLogs collects the logs that were generated during the
  1144. // processing of the block that corresponds with the given hash.
  1145. // These logs are later announced as deleted.
  1146. collectLogs = func(h common.Hash) {
  1147. // Coalesce logs
  1148. receipts := GetBlockReceipts(self.chainDb, h)
  1149. for _, receipt := range receipts {
  1150. deletedLogs = append(deletedLogs, receipt.Logs...)
  1151. }
  1152. }
  1153. )
  1154. // first reduce whoever is higher bound
  1155. if oldBlock.NumberU64() > newBlock.NumberU64() {
  1156. // reduce old chain
  1157. for oldBlock = oldBlock; oldBlock != nil && oldBlock.NumberU64() != newBlock.NumberU64(); oldBlock = self.GetBlock(oldBlock.ParentHash()) {
  1158. deletedTxs = append(deletedTxs, oldBlock.Transactions()...)
  1159. collectLogs(oldBlock.Hash())
  1160. }
  1161. } else {
  1162. // reduce new chain and append new chain blocks for inserting later on
  1163. for newBlock = newBlock; newBlock != nil && newBlock.NumberU64() != oldBlock.NumberU64(); newBlock = self.GetBlock(newBlock.ParentHash()) {
  1164. newChain = append(newChain, newBlock)
  1165. }
  1166. }
  1167. if oldBlock == nil {
  1168. return fmt.Errorf("Invalid old chain")
  1169. }
  1170. if newBlock == nil {
  1171. return fmt.Errorf("Invalid new chain")
  1172. }
  1173. numSplit := newBlock.Number()
  1174. for {
  1175. if oldBlock.Hash() == newBlock.Hash() {
  1176. commonBlock = oldBlock
  1177. break
  1178. }
  1179. newChain = append(newChain, newBlock)
  1180. deletedTxs = append(deletedTxs, oldBlock.Transactions()...)
  1181. collectLogs(oldBlock.Hash())
  1182. oldBlock, newBlock = self.GetBlock(oldBlock.ParentHash()), self.GetBlock(newBlock.ParentHash())
  1183. if oldBlock == nil {
  1184. return fmt.Errorf("Invalid old chain")
  1185. }
  1186. if newBlock == nil {
  1187. return fmt.Errorf("Invalid new chain")
  1188. }
  1189. }
  1190. if glog.V(logger.Debug) {
  1191. commonHash := commonBlock.Hash()
  1192. glog.Infof("Chain split detected @ %x. Reorganising chain from #%v %x to %x", commonHash[:4], numSplit, oldStart.Hash().Bytes()[:4], newStart.Hash().Bytes()[:4])
  1193. }
  1194. var addedTxs types.Transactions
  1195. // insert blocks. Order does not matter. Last block will be written in ImportChain itself which creates the new head properly
  1196. for _, block := range newChain {
  1197. // insert the block in the canonical way, re-writing history
  1198. self.insert(block)
  1199. // write canonical receipts and transactions
  1200. if err := WriteTransactions(self.chainDb, block); err != nil {
  1201. return err
  1202. }
  1203. receipts := GetBlockReceipts(self.chainDb, block.Hash())
  1204. // write receipts
  1205. if err := WriteReceipts(self.chainDb, receipts); err != nil {
  1206. return err
  1207. }
  1208. // Write map map bloom filters
  1209. if err := WriteMipmapBloom(self.chainDb, block.NumberU64(), receipts); err != nil {
  1210. return err
  1211. }
  1212. addedTxs = append(addedTxs, block.Transactions()...)
  1213. }
  1214. // calculate the difference between deleted and added transactions
  1215. diff := types.TxDifference(deletedTxs, addedTxs)
  1216. // When transactions get deleted from the database that means the
  1217. // receipts that were created in the fork must also be deleted
  1218. for _, tx := range diff {
  1219. DeleteReceipt(self.chainDb, tx.Hash())
  1220. DeleteTransaction(self.chainDb, tx.Hash())
  1221. }
  1222. // Must be posted in a goroutine because of the transaction pool trying
  1223. // to acquire the chain manager lock
  1224. if len(diff) > 0 {
  1225. go self.eventMux.Post(RemovedTransactionEvent{diff})
  1226. }
  1227. if len(deletedLogs) > 0 {
  1228. go self.eventMux.Post(RemovedLogsEvent{deletedLogs})
  1229. }
  1230. return nil
  1231. }
  1232. // postChainEvents iterates over the events generated by a chain insertion and
  1233. // posts them into the event mux.
  1234. func (self *BlockChain) postChainEvents(events []interface{}, logs vm.Logs) {
  1235. // post event logs for further processing
  1236. self.eventMux.Post(logs)
  1237. for _, event := range events {
  1238. if event, ok := event.(ChainEvent); ok {
  1239. // We need some control over the mining operation. Acquiring locks and waiting for the miner to create new block takes too long
  1240. // and in most cases isn't even necessary.
  1241. if self.LastBlockHash() == event.Hash {
  1242. self.eventMux.Post(ChainHeadEvent{event.Block})
  1243. }
  1244. }
  1245. // Fire the insertion events individually too
  1246. self.eventMux.Post(event)
  1247. }
  1248. }
  1249. func (self *BlockChain) update() {
  1250. futureTimer := time.Tick(5 * time.Second)
  1251. for {
  1252. select {
  1253. case <-futureTimer:
  1254. self.procFutureBlocks()
  1255. case <-self.quit:
  1256. return
  1257. }
  1258. }
  1259. }
  1260. // reportBlock reports the given block and error using the canonical block
  1261. // reporting tool. Reporting the block to the service is handled in a separate
  1262. // goroutine.
  1263. func reportBlock(block *types.Block, err error) {
  1264. if glog.V(logger.Error) {
  1265. glog.Errorf("Bad block #%v (%s)\n", block.Number(), block.Hash().Hex())
  1266. glog.Errorf(" %v", err)
  1267. }
  1268. go ReportBlock(block, err)
  1269. }