blockchain.go 47 KB

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