blockchain.go 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857
  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. "sync"
  25. "sync/atomic"
  26. "time"
  27. "github.com/ethereum/go-ethereum/common"
  28. "github.com/ethereum/go-ethereum/common/mclock"
  29. "github.com/ethereum/go-ethereum/common/prque"
  30. "github.com/ethereum/go-ethereum/consensus"
  31. "github.com/ethereum/go-ethereum/core/rawdb"
  32. "github.com/ethereum/go-ethereum/core/state"
  33. "github.com/ethereum/go-ethereum/core/types"
  34. "github.com/ethereum/go-ethereum/core/vm"
  35. "github.com/ethereum/go-ethereum/ethdb"
  36. "github.com/ethereum/go-ethereum/event"
  37. "github.com/ethereum/go-ethereum/log"
  38. "github.com/ethereum/go-ethereum/metrics"
  39. "github.com/ethereum/go-ethereum/params"
  40. "github.com/ethereum/go-ethereum/rlp"
  41. "github.com/ethereum/go-ethereum/trie"
  42. "github.com/hashicorp/golang-lru"
  43. )
  44. var (
  45. accountReadTimer = metrics.NewRegisteredTimer("chain/account/reads", nil)
  46. accountHashTimer = metrics.NewRegisteredTimer("chain/account/hashes", nil)
  47. accountUpdateTimer = metrics.NewRegisteredTimer("chain/account/updates", nil)
  48. accountCommitTimer = metrics.NewRegisteredTimer("chain/account/commits", nil)
  49. storageReadTimer = metrics.NewRegisteredTimer("chain/storage/reads", nil)
  50. storageHashTimer = metrics.NewRegisteredTimer("chain/storage/hashes", nil)
  51. storageUpdateTimer = metrics.NewRegisteredTimer("chain/storage/updates", nil)
  52. storageCommitTimer = metrics.NewRegisteredTimer("chain/storage/commits", nil)
  53. blockInsertTimer = metrics.NewRegisteredTimer("chain/inserts", nil)
  54. blockValidationTimer = metrics.NewRegisteredTimer("chain/validation", nil)
  55. blockExecutionTimer = metrics.NewRegisteredTimer("chain/execution", nil)
  56. blockWriteTimer = metrics.NewRegisteredTimer("chain/write", nil)
  57. blockPrefetchExecuteTimer = metrics.NewRegisteredTimer("chain/prefetch/executes", nil)
  58. blockPrefetchInterruptMeter = metrics.NewRegisteredMeter("chain/prefetch/interrupts", nil)
  59. )
  60. const (
  61. bodyCacheLimit = 256
  62. blockCacheLimit = 256
  63. receiptsCacheLimit = 32
  64. maxFutureBlocks = 256
  65. maxTimeFutureBlocks = 30
  66. badBlockLimit = 10
  67. triesInMemory = 128
  68. // BlockChainVersion ensures that an incompatible database forces a resync from scratch.
  69. //
  70. // Changelog:
  71. //
  72. // - Version 4
  73. // The following incompatible database changes were added:
  74. // * the `BlockNumber`, `TxHash`, `TxIndex`, `BlockHash` and `Index` fields of log are deleted
  75. // * the `Bloom` field of receipt is deleted
  76. // * the `BlockIndex` and `TxIndex` fields of txlookup are deleted
  77. // - Version 5
  78. // The following incompatible database changes were added:
  79. // * the `TxHash`, `GasCost`, and `ContractAddress` fields are no longer stored for a receipt
  80. // * the `TxHash`, `GasCost`, and `ContractAddress` fields are computed by looking up the
  81. // receipts' corresponding block
  82. // - Version 6
  83. // The following incompatible database changes were added:
  84. // * Transaction lookup information stores the corresponding block number instead of block hash
  85. BlockChainVersion uint64 = 6
  86. )
  87. // CacheConfig contains the configuration values for the trie caching/pruning
  88. // that's resident in a blockchain.
  89. type CacheConfig struct {
  90. TrieCleanLimit int // Memory allowance (MB) to use for caching trie nodes in memory
  91. TrieCleanNoPrefetch bool // Whether to disable heuristic state prefetching for followup blocks
  92. TrieDirtyLimit int // Memory limit (MB) at which to start flushing dirty trie nodes to disk
  93. TrieDirtyDisabled bool // Whether to disable trie write caching and GC altogether (archive node)
  94. TrieTimeLimit time.Duration // Time limit after which to flush the current in-memory trie to disk
  95. }
  96. // BlockChain represents the canonical chain given a database with a genesis
  97. // block. The Blockchain manages chain imports, reverts, chain reorganisations.
  98. //
  99. // Importing blocks in to the block chain happens according to the set of rules
  100. // defined by the two stage Validator. Processing of blocks is done using the
  101. // Processor which processes the included transaction. The validation of the state
  102. // is done in the second part of the Validator. Failing results in aborting of
  103. // the import.
  104. //
  105. // The BlockChain also helps in returning blocks from **any** chain included
  106. // in the database as well as blocks that represents the canonical chain. It's
  107. // important to note that GetBlock can return any block and does not need to be
  108. // included in the canonical one where as GetBlockByNumber always represents the
  109. // canonical chain.
  110. type BlockChain struct {
  111. chainConfig *params.ChainConfig // Chain & network configuration
  112. cacheConfig *CacheConfig // Cache configuration for pruning
  113. db ethdb.Database // Low level persistent database to store final content in
  114. triegc *prque.Prque // Priority queue mapping block numbers to tries to gc
  115. gcproc time.Duration // Accumulates canonical block processing for trie dumping
  116. hc *HeaderChain
  117. rmLogsFeed event.Feed
  118. chainFeed event.Feed
  119. chainSideFeed event.Feed
  120. chainHeadFeed event.Feed
  121. logsFeed event.Feed
  122. blockProcFeed event.Feed
  123. scope event.SubscriptionScope
  124. genesisBlock *types.Block
  125. chainmu sync.RWMutex // blockchain insertion lock
  126. checkpoint int // checkpoint counts towards the new checkpoint
  127. currentBlock atomic.Value // Current head of the block chain
  128. currentFastBlock atomic.Value // Current head of the fast-sync chain (may be above the block chain!)
  129. stateCache state.Database // State database to reuse between imports (contains state cache)
  130. bodyCache *lru.Cache // Cache for the most recent block bodies
  131. bodyRLPCache *lru.Cache // Cache for the most recent block bodies in RLP encoded format
  132. receiptsCache *lru.Cache // Cache for the most recent receipts per block
  133. blockCache *lru.Cache // Cache for the most recent entire blocks
  134. futureBlocks *lru.Cache // future blocks are blocks added for later processing
  135. quit chan struct{} // blockchain quit channel
  136. running int32 // running must be called atomically
  137. // procInterrupt must be atomically called
  138. procInterrupt int32 // interrupt signaler for block processing
  139. wg sync.WaitGroup // chain processing wait group for shutting down
  140. engine consensus.Engine
  141. validator Validator // Block and state validator interface
  142. prefetcher Prefetcher // Block state prefetcher interface
  143. processor Processor // Block transaction processor interface
  144. vmConfig vm.Config
  145. badBlocks *lru.Cache // Bad block cache
  146. shouldPreserve func(*types.Block) bool // Function used to determine whether should preserve the given block.
  147. }
  148. // NewBlockChain returns a fully initialised block chain using information
  149. // available in the database. It initialises the default Ethereum Validator and
  150. // Processor.
  151. func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *params.ChainConfig, engine consensus.Engine, vmConfig vm.Config, shouldPreserve func(block *types.Block) bool) (*BlockChain, error) {
  152. if cacheConfig == nil {
  153. cacheConfig = &CacheConfig{
  154. TrieCleanLimit: 256,
  155. TrieDirtyLimit: 256,
  156. TrieTimeLimit: 5 * time.Minute,
  157. }
  158. }
  159. bodyCache, _ := lru.New(bodyCacheLimit)
  160. bodyRLPCache, _ := lru.New(bodyCacheLimit)
  161. receiptsCache, _ := lru.New(receiptsCacheLimit)
  162. blockCache, _ := lru.New(blockCacheLimit)
  163. futureBlocks, _ := lru.New(maxFutureBlocks)
  164. badBlocks, _ := lru.New(badBlockLimit)
  165. bc := &BlockChain{
  166. chainConfig: chainConfig,
  167. cacheConfig: cacheConfig,
  168. db: db,
  169. triegc: prque.New(nil),
  170. stateCache: state.NewDatabaseWithCache(db, cacheConfig.TrieCleanLimit),
  171. quit: make(chan struct{}),
  172. shouldPreserve: shouldPreserve,
  173. bodyCache: bodyCache,
  174. bodyRLPCache: bodyRLPCache,
  175. receiptsCache: receiptsCache,
  176. blockCache: blockCache,
  177. futureBlocks: futureBlocks,
  178. engine: engine,
  179. vmConfig: vmConfig,
  180. badBlocks: badBlocks,
  181. }
  182. bc.validator = NewBlockValidator(chainConfig, bc, engine)
  183. bc.prefetcher = newStatePrefetcher(chainConfig, bc, engine)
  184. bc.processor = NewStateProcessor(chainConfig, bc, engine)
  185. var err error
  186. bc.hc, err = NewHeaderChain(db, chainConfig, engine, bc.getProcInterrupt)
  187. if err != nil {
  188. return nil, err
  189. }
  190. bc.genesisBlock = bc.GetBlockByNumber(0)
  191. if bc.genesisBlock == nil {
  192. return nil, ErrNoGenesis
  193. }
  194. if err := bc.loadLastState(); err != nil {
  195. return nil, err
  196. }
  197. // Check the current state of the block hashes and make sure that we do not have any of the bad blocks in our chain
  198. for hash := range BadHashes {
  199. if header := bc.GetHeaderByHash(hash); header != nil {
  200. // get the canonical block corresponding to the offending header's number
  201. headerByNumber := bc.GetHeaderByNumber(header.Number.Uint64())
  202. // make sure the headerByNumber (if present) is in our current canonical chain
  203. if headerByNumber != nil && headerByNumber.Hash() == header.Hash() {
  204. log.Error("Found bad hash, rewinding chain", "number", header.Number, "hash", header.ParentHash)
  205. bc.SetHead(header.Number.Uint64() - 1)
  206. log.Error("Chain rewind was successful, resuming normal operation")
  207. }
  208. }
  209. }
  210. // Take ownership of this particular state
  211. go bc.update()
  212. return bc, nil
  213. }
  214. func (bc *BlockChain) getProcInterrupt() bool {
  215. return atomic.LoadInt32(&bc.procInterrupt) == 1
  216. }
  217. // GetVMConfig returns the block chain VM config.
  218. func (bc *BlockChain) GetVMConfig() *vm.Config {
  219. return &bc.vmConfig
  220. }
  221. // loadLastState loads the last known chain state from the database. This method
  222. // assumes that the chain manager mutex is held.
  223. func (bc *BlockChain) loadLastState() error {
  224. // Restore the last known head block
  225. head := rawdb.ReadHeadBlockHash(bc.db)
  226. if head == (common.Hash{}) {
  227. // Corrupt or empty database, init from scratch
  228. log.Warn("Empty database, resetting chain")
  229. return bc.Reset()
  230. }
  231. // Make sure the entire head block is available
  232. currentBlock := bc.GetBlockByHash(head)
  233. if currentBlock == nil {
  234. // Corrupt or empty database, init from scratch
  235. log.Warn("Head block missing, resetting chain", "hash", head)
  236. return bc.Reset()
  237. }
  238. // Make sure the state associated with the block is available
  239. if _, err := state.New(currentBlock.Root(), bc.stateCache); err != nil {
  240. // Dangling block without a state associated, init from scratch
  241. log.Warn("Head state missing, repairing chain", "number", currentBlock.Number(), "hash", currentBlock.Hash())
  242. if err := bc.repair(&currentBlock); err != nil {
  243. return err
  244. }
  245. }
  246. // Everything seems to be fine, set as the head block
  247. bc.currentBlock.Store(currentBlock)
  248. // Restore the last known head header
  249. currentHeader := currentBlock.Header()
  250. if head := rawdb.ReadHeadHeaderHash(bc.db); head != (common.Hash{}) {
  251. if header := bc.GetHeaderByHash(head); header != nil {
  252. currentHeader = header
  253. }
  254. }
  255. bc.hc.SetCurrentHeader(currentHeader)
  256. // Restore the last known head fast block
  257. bc.currentFastBlock.Store(currentBlock)
  258. if head := rawdb.ReadHeadFastBlockHash(bc.db); head != (common.Hash{}) {
  259. if block := bc.GetBlockByHash(head); block != nil {
  260. bc.currentFastBlock.Store(block)
  261. }
  262. }
  263. // Issue a status log for the user
  264. currentFastBlock := bc.CurrentFastBlock()
  265. headerTd := bc.GetTd(currentHeader.Hash(), currentHeader.Number.Uint64())
  266. blockTd := bc.GetTd(currentBlock.Hash(), currentBlock.NumberU64())
  267. fastTd := bc.GetTd(currentFastBlock.Hash(), currentFastBlock.NumberU64())
  268. log.Info("Loaded most recent local header", "number", currentHeader.Number, "hash", currentHeader.Hash(), "td", headerTd, "age", common.PrettyAge(time.Unix(int64(currentHeader.Time), 0)))
  269. log.Info("Loaded most recent local full block", "number", currentBlock.Number(), "hash", currentBlock.Hash(), "td", blockTd, "age", common.PrettyAge(time.Unix(int64(currentBlock.Time()), 0)))
  270. log.Info("Loaded most recent local fast block", "number", currentFastBlock.Number(), "hash", currentFastBlock.Hash(), "td", fastTd, "age", common.PrettyAge(time.Unix(int64(currentFastBlock.Time()), 0)))
  271. return nil
  272. }
  273. // SetHead rewinds the local chain to a new head. In the case of headers, everything
  274. // above the new head will be deleted and the new one set. In the case of blocks
  275. // though, the head may be further rewound if block bodies are missing (non-archive
  276. // nodes after a fast sync).
  277. func (bc *BlockChain) SetHead(head uint64) error {
  278. log.Warn("Rewinding blockchain", "target", head)
  279. bc.chainmu.Lock()
  280. defer bc.chainmu.Unlock()
  281. // Rewind the header chain, deleting all block bodies until then
  282. delFn := func(db ethdb.Writer, hash common.Hash, num uint64) {
  283. rawdb.DeleteBody(db, hash, num)
  284. }
  285. bc.hc.SetHead(head, delFn)
  286. currentHeader := bc.hc.CurrentHeader()
  287. // Clear out any stale content from the caches
  288. bc.bodyCache.Purge()
  289. bc.bodyRLPCache.Purge()
  290. bc.receiptsCache.Purge()
  291. bc.blockCache.Purge()
  292. bc.futureBlocks.Purge()
  293. // Rewind the block chain, ensuring we don't end up with a stateless head block
  294. if currentBlock := bc.CurrentBlock(); currentBlock != nil && currentHeader.Number.Uint64() < currentBlock.NumberU64() {
  295. bc.currentBlock.Store(bc.GetBlock(currentHeader.Hash(), currentHeader.Number.Uint64()))
  296. }
  297. if currentBlock := bc.CurrentBlock(); currentBlock != nil {
  298. if _, err := state.New(currentBlock.Root(), bc.stateCache); err != nil {
  299. // Rewound state missing, rolled back to before pivot, reset to genesis
  300. bc.currentBlock.Store(bc.genesisBlock)
  301. }
  302. }
  303. // Rewind the fast block in a simpleton way to the target head
  304. if currentFastBlock := bc.CurrentFastBlock(); currentFastBlock != nil && currentHeader.Number.Uint64() < currentFastBlock.NumberU64() {
  305. bc.currentFastBlock.Store(bc.GetBlock(currentHeader.Hash(), currentHeader.Number.Uint64()))
  306. }
  307. // If either blocks reached nil, reset to the genesis state
  308. if currentBlock := bc.CurrentBlock(); currentBlock == nil {
  309. bc.currentBlock.Store(bc.genesisBlock)
  310. }
  311. if currentFastBlock := bc.CurrentFastBlock(); currentFastBlock == nil {
  312. bc.currentFastBlock.Store(bc.genesisBlock)
  313. }
  314. currentBlock := bc.CurrentBlock()
  315. currentFastBlock := bc.CurrentFastBlock()
  316. rawdb.WriteHeadBlockHash(bc.db, currentBlock.Hash())
  317. rawdb.WriteHeadFastBlockHash(bc.db, currentFastBlock.Hash())
  318. return bc.loadLastState()
  319. }
  320. // FastSyncCommitHead sets the current head block to the one defined by the hash
  321. // irrelevant what the chain contents were prior.
  322. func (bc *BlockChain) FastSyncCommitHead(hash common.Hash) error {
  323. // Make sure that both the block as well at its state trie exists
  324. block := bc.GetBlockByHash(hash)
  325. if block == nil {
  326. return fmt.Errorf("non existent block [%x…]", hash[:4])
  327. }
  328. if _, err := trie.NewSecure(block.Root(), bc.stateCache.TrieDB()); err != nil {
  329. return err
  330. }
  331. // If all checks out, manually set the head block
  332. bc.chainmu.Lock()
  333. bc.currentBlock.Store(block)
  334. bc.chainmu.Unlock()
  335. log.Info("Committed new head block", "number", block.Number(), "hash", hash)
  336. return nil
  337. }
  338. // GasLimit returns the gas limit of the current HEAD block.
  339. func (bc *BlockChain) GasLimit() uint64 {
  340. return bc.CurrentBlock().GasLimit()
  341. }
  342. // CurrentBlock retrieves the current head block of the canonical chain. The
  343. // block is retrieved from the blockchain's internal cache.
  344. func (bc *BlockChain) CurrentBlock() *types.Block {
  345. return bc.currentBlock.Load().(*types.Block)
  346. }
  347. // CurrentFastBlock retrieves the current fast-sync head block of the canonical
  348. // chain. The block is retrieved from the blockchain's internal cache.
  349. func (bc *BlockChain) CurrentFastBlock() *types.Block {
  350. return bc.currentFastBlock.Load().(*types.Block)
  351. }
  352. // Validator returns the current validator.
  353. func (bc *BlockChain) Validator() Validator {
  354. return bc.validator
  355. }
  356. // Processor returns the current processor.
  357. func (bc *BlockChain) Processor() Processor {
  358. return bc.processor
  359. }
  360. // State returns a new mutable state based on the current HEAD block.
  361. func (bc *BlockChain) State() (*state.StateDB, error) {
  362. return bc.StateAt(bc.CurrentBlock().Root())
  363. }
  364. // StateAt returns a new mutable state based on a particular point in time.
  365. func (bc *BlockChain) StateAt(root common.Hash) (*state.StateDB, error) {
  366. return state.New(root, bc.stateCache)
  367. }
  368. // StateCache returns the caching database underpinning the blockchain instance.
  369. func (bc *BlockChain) StateCache() state.Database {
  370. return bc.stateCache
  371. }
  372. // Reset purges the entire blockchain, restoring it to its genesis state.
  373. func (bc *BlockChain) Reset() error {
  374. return bc.ResetWithGenesisBlock(bc.genesisBlock)
  375. }
  376. // ResetWithGenesisBlock purges the entire blockchain, restoring it to the
  377. // specified genesis state.
  378. func (bc *BlockChain) ResetWithGenesisBlock(genesis *types.Block) error {
  379. // Dump the entire block chain and purge the caches
  380. if err := bc.SetHead(0); err != nil {
  381. return err
  382. }
  383. bc.chainmu.Lock()
  384. defer bc.chainmu.Unlock()
  385. // Prepare the genesis block and reinitialise the chain
  386. if err := bc.hc.WriteTd(genesis.Hash(), genesis.NumberU64(), genesis.Difficulty()); err != nil {
  387. log.Crit("Failed to write genesis block TD", "err", err)
  388. }
  389. rawdb.WriteBlock(bc.db, genesis)
  390. bc.genesisBlock = genesis
  391. bc.insert(bc.genesisBlock)
  392. bc.currentBlock.Store(bc.genesisBlock)
  393. bc.hc.SetGenesis(bc.genesisBlock.Header())
  394. bc.hc.SetCurrentHeader(bc.genesisBlock.Header())
  395. bc.currentFastBlock.Store(bc.genesisBlock)
  396. return nil
  397. }
  398. // repair tries to repair the current blockchain by rolling back the current block
  399. // until one with associated state is found. This is needed to fix incomplete db
  400. // writes caused either by crashes/power outages, or simply non-committed tries.
  401. //
  402. // This method only rolls back the current block. The current header and current
  403. // fast block are left intact.
  404. func (bc *BlockChain) repair(head **types.Block) error {
  405. for {
  406. // Abort if we've rewound to a head block that does have associated state
  407. if _, err := state.New((*head).Root(), bc.stateCache); err == nil {
  408. log.Info("Rewound blockchain to past state", "number", (*head).Number(), "hash", (*head).Hash())
  409. return nil
  410. }
  411. // Otherwise rewind one block and recheck state availability there
  412. block := bc.GetBlock((*head).ParentHash(), (*head).NumberU64()-1)
  413. if block == nil {
  414. return fmt.Errorf("missing block %d [%x]", (*head).NumberU64()-1, (*head).ParentHash())
  415. }
  416. *head = block
  417. }
  418. }
  419. // Export writes the active chain to the given writer.
  420. func (bc *BlockChain) Export(w io.Writer) error {
  421. return bc.ExportN(w, uint64(0), bc.CurrentBlock().NumberU64())
  422. }
  423. // ExportN writes a subset of the active chain to the given writer.
  424. func (bc *BlockChain) ExportN(w io.Writer, first uint64, last uint64) error {
  425. bc.chainmu.RLock()
  426. defer bc.chainmu.RUnlock()
  427. if first > last {
  428. return fmt.Errorf("export failed: first (%d) is greater than last (%d)", first, last)
  429. }
  430. log.Info("Exporting batch of blocks", "count", last-first+1)
  431. start, reported := time.Now(), time.Now()
  432. for nr := first; nr <= last; nr++ {
  433. block := bc.GetBlockByNumber(nr)
  434. if block == nil {
  435. return fmt.Errorf("export failed on #%d: not found", nr)
  436. }
  437. if err := block.EncodeRLP(w); err != nil {
  438. return err
  439. }
  440. if time.Since(reported) >= statsReportLimit {
  441. log.Info("Exporting blocks", "exported", block.NumberU64()-first, "elapsed", common.PrettyDuration(time.Since(start)))
  442. reported = time.Now()
  443. }
  444. }
  445. return nil
  446. }
  447. // insert injects a new head block into the current block chain. This method
  448. // assumes that the block is indeed a true head. It will also reset the head
  449. // header and the head fast sync block to this very same block if they are older
  450. // or if they are on a different side chain.
  451. //
  452. // Note, this function assumes that the `mu` mutex is held!
  453. func (bc *BlockChain) insert(block *types.Block) {
  454. // If the block is on a side chain or an unknown one, force other heads onto it too
  455. updateHeads := rawdb.ReadCanonicalHash(bc.db, block.NumberU64()) != block.Hash()
  456. // Add the block to the canonical chain number scheme and mark as the head
  457. rawdb.WriteCanonicalHash(bc.db, block.Hash(), block.NumberU64())
  458. rawdb.WriteHeadBlockHash(bc.db, block.Hash())
  459. bc.currentBlock.Store(block)
  460. // If the block is better than our head or is on a different chain, force update heads
  461. if updateHeads {
  462. bc.hc.SetCurrentHeader(block.Header())
  463. rawdb.WriteHeadFastBlockHash(bc.db, block.Hash())
  464. bc.currentFastBlock.Store(block)
  465. }
  466. }
  467. // Genesis retrieves the chain's genesis block.
  468. func (bc *BlockChain) Genesis() *types.Block {
  469. return bc.genesisBlock
  470. }
  471. // GetBody retrieves a block body (transactions and uncles) from the database by
  472. // hash, caching it if found.
  473. func (bc *BlockChain) GetBody(hash common.Hash) *types.Body {
  474. // Short circuit if the body's already in the cache, retrieve otherwise
  475. if cached, ok := bc.bodyCache.Get(hash); ok {
  476. body := cached.(*types.Body)
  477. return body
  478. }
  479. number := bc.hc.GetBlockNumber(hash)
  480. if number == nil {
  481. return nil
  482. }
  483. body := rawdb.ReadBody(bc.db, hash, *number)
  484. if body == nil {
  485. return nil
  486. }
  487. // Cache the found body for next time and return
  488. bc.bodyCache.Add(hash, body)
  489. return body
  490. }
  491. // GetBodyRLP retrieves a block body in RLP encoding from the database by hash,
  492. // caching it if found.
  493. func (bc *BlockChain) GetBodyRLP(hash common.Hash) rlp.RawValue {
  494. // Short circuit if the body's already in the cache, retrieve otherwise
  495. if cached, ok := bc.bodyRLPCache.Get(hash); ok {
  496. return cached.(rlp.RawValue)
  497. }
  498. number := bc.hc.GetBlockNumber(hash)
  499. if number == nil {
  500. return nil
  501. }
  502. body := rawdb.ReadBodyRLP(bc.db, hash, *number)
  503. if len(body) == 0 {
  504. return nil
  505. }
  506. // Cache the found body for next time and return
  507. bc.bodyRLPCache.Add(hash, body)
  508. return body
  509. }
  510. // HasBlock checks if a block is fully present in the database or not.
  511. func (bc *BlockChain) HasBlock(hash common.Hash, number uint64) bool {
  512. if bc.blockCache.Contains(hash) {
  513. return true
  514. }
  515. return rawdb.HasBody(bc.db, hash, number)
  516. }
  517. // HasFastBlock checks if a fast block is fully present in the database or not.
  518. func (bc *BlockChain) HasFastBlock(hash common.Hash, number uint64) bool {
  519. if !bc.HasBlock(hash, number) {
  520. return false
  521. }
  522. if bc.receiptsCache.Contains(hash) {
  523. return true
  524. }
  525. return rawdb.HasReceipts(bc.db, hash, number)
  526. }
  527. // HasState checks if state trie is fully present in the database or not.
  528. func (bc *BlockChain) HasState(hash common.Hash) bool {
  529. _, err := bc.stateCache.OpenTrie(hash)
  530. return err == nil
  531. }
  532. // HasBlockAndState checks if a block and associated state trie is fully present
  533. // in the database or not, caching it if present.
  534. func (bc *BlockChain) HasBlockAndState(hash common.Hash, number uint64) bool {
  535. // Check first that the block itself is known
  536. block := bc.GetBlock(hash, number)
  537. if block == nil {
  538. return false
  539. }
  540. return bc.HasState(block.Root())
  541. }
  542. // GetBlock retrieves a block from the database by hash and number,
  543. // caching it if found.
  544. func (bc *BlockChain) GetBlock(hash common.Hash, number uint64) *types.Block {
  545. // Short circuit if the block's already in the cache, retrieve otherwise
  546. if block, ok := bc.blockCache.Get(hash); ok {
  547. return block.(*types.Block)
  548. }
  549. block := rawdb.ReadBlock(bc.db, hash, number)
  550. if block == nil {
  551. return nil
  552. }
  553. // Cache the found block for next time and return
  554. bc.blockCache.Add(block.Hash(), block)
  555. return block
  556. }
  557. // GetBlockByHash retrieves a block from the database by hash, caching it if found.
  558. func (bc *BlockChain) GetBlockByHash(hash common.Hash) *types.Block {
  559. number := bc.hc.GetBlockNumber(hash)
  560. if number == nil {
  561. return nil
  562. }
  563. return bc.GetBlock(hash, *number)
  564. }
  565. // GetBlockByNumber retrieves a block from the database by number, caching it
  566. // (associated with its hash) if found.
  567. func (bc *BlockChain) GetBlockByNumber(number uint64) *types.Block {
  568. hash := rawdb.ReadCanonicalHash(bc.db, number)
  569. if hash == (common.Hash{}) {
  570. return nil
  571. }
  572. return bc.GetBlock(hash, number)
  573. }
  574. // GetReceiptsByHash retrieves the receipts for all transactions in a given block.
  575. func (bc *BlockChain) GetReceiptsByHash(hash common.Hash) types.Receipts {
  576. if receipts, ok := bc.receiptsCache.Get(hash); ok {
  577. return receipts.(types.Receipts)
  578. }
  579. number := rawdb.ReadHeaderNumber(bc.db, hash)
  580. if number == nil {
  581. return nil
  582. }
  583. receipts := rawdb.ReadReceipts(bc.db, hash, *number, bc.chainConfig)
  584. if receipts == nil {
  585. return nil
  586. }
  587. bc.receiptsCache.Add(hash, receipts)
  588. return receipts
  589. }
  590. // GetBlocksFromHash returns the block corresponding to hash and up to n-1 ancestors.
  591. // [deprecated by eth/62]
  592. func (bc *BlockChain) GetBlocksFromHash(hash common.Hash, n int) (blocks []*types.Block) {
  593. number := bc.hc.GetBlockNumber(hash)
  594. if number == nil {
  595. return nil
  596. }
  597. for i := 0; i < n; i++ {
  598. block := bc.GetBlock(hash, *number)
  599. if block == nil {
  600. break
  601. }
  602. blocks = append(blocks, block)
  603. hash = block.ParentHash()
  604. *number--
  605. }
  606. return
  607. }
  608. // GetUnclesInChain retrieves all the uncles from a given block backwards until
  609. // a specific distance is reached.
  610. func (bc *BlockChain) GetUnclesInChain(block *types.Block, length int) []*types.Header {
  611. uncles := []*types.Header{}
  612. for i := 0; block != nil && i < length; i++ {
  613. uncles = append(uncles, block.Uncles()...)
  614. block = bc.GetBlock(block.ParentHash(), block.NumberU64()-1)
  615. }
  616. return uncles
  617. }
  618. // TrieNode retrieves a blob of data associated with a trie node (or code hash)
  619. // either from ephemeral in-memory cache, or from persistent storage.
  620. func (bc *BlockChain) TrieNode(hash common.Hash) ([]byte, error) {
  621. return bc.stateCache.TrieDB().Node(hash)
  622. }
  623. // Stop stops the blockchain service. If any imports are currently in progress
  624. // it will abort them using the procInterrupt.
  625. func (bc *BlockChain) Stop() {
  626. if !atomic.CompareAndSwapInt32(&bc.running, 0, 1) {
  627. return
  628. }
  629. // Unsubscribe all subscriptions registered from blockchain
  630. bc.scope.Close()
  631. close(bc.quit)
  632. atomic.StoreInt32(&bc.procInterrupt, 1)
  633. bc.wg.Wait()
  634. // Ensure the state of a recent block is also stored to disk before exiting.
  635. // We're writing three different states to catch different restart scenarios:
  636. // - HEAD: So we don't need to reprocess any blocks in the general case
  637. // - HEAD-1: So we don't do large reorgs if our HEAD becomes an uncle
  638. // - HEAD-127: So we have a hard limit on the number of blocks reexecuted
  639. if !bc.cacheConfig.TrieDirtyDisabled {
  640. triedb := bc.stateCache.TrieDB()
  641. for _, offset := range []uint64{0, 1, triesInMemory - 1} {
  642. if number := bc.CurrentBlock().NumberU64(); number > offset {
  643. recent := bc.GetBlockByNumber(number - offset)
  644. log.Info("Writing cached state to disk", "block", recent.Number(), "hash", recent.Hash(), "root", recent.Root())
  645. if err := triedb.Commit(recent.Root(), true); err != nil {
  646. log.Error("Failed to commit recent state trie", "err", err)
  647. }
  648. }
  649. }
  650. for !bc.triegc.Empty() {
  651. triedb.Dereference(bc.triegc.PopItem().(common.Hash))
  652. }
  653. if size, _ := triedb.Size(); size != 0 {
  654. log.Error("Dangling trie nodes after full cleanup")
  655. }
  656. }
  657. log.Info("Blockchain manager stopped")
  658. }
  659. func (bc *BlockChain) procFutureBlocks() {
  660. blocks := make([]*types.Block, 0, bc.futureBlocks.Len())
  661. for _, hash := range bc.futureBlocks.Keys() {
  662. if block, exist := bc.futureBlocks.Peek(hash); exist {
  663. blocks = append(blocks, block.(*types.Block))
  664. }
  665. }
  666. if len(blocks) > 0 {
  667. types.BlockBy(types.Number).Sort(blocks)
  668. // Insert one by one as chain insertion needs contiguous ancestry between blocks
  669. for i := range blocks {
  670. bc.InsertChain(blocks[i : i+1])
  671. }
  672. }
  673. }
  674. // WriteStatus status of write
  675. type WriteStatus byte
  676. const (
  677. NonStatTy WriteStatus = iota
  678. CanonStatTy
  679. SideStatTy
  680. )
  681. // Rollback is designed to remove a chain of links from the database that aren't
  682. // certain enough to be valid.
  683. func (bc *BlockChain) Rollback(chain []common.Hash) {
  684. bc.chainmu.Lock()
  685. defer bc.chainmu.Unlock()
  686. for i := len(chain) - 1; i >= 0; i-- {
  687. hash := chain[i]
  688. currentHeader := bc.hc.CurrentHeader()
  689. if currentHeader.Hash() == hash {
  690. bc.hc.SetCurrentHeader(bc.GetHeader(currentHeader.ParentHash, currentHeader.Number.Uint64()-1))
  691. }
  692. if currentFastBlock := bc.CurrentFastBlock(); currentFastBlock.Hash() == hash {
  693. newFastBlock := bc.GetBlock(currentFastBlock.ParentHash(), currentFastBlock.NumberU64()-1)
  694. bc.currentFastBlock.Store(newFastBlock)
  695. rawdb.WriteHeadFastBlockHash(bc.db, newFastBlock.Hash())
  696. }
  697. if currentBlock := bc.CurrentBlock(); currentBlock.Hash() == hash {
  698. newBlock := bc.GetBlock(currentBlock.ParentHash(), currentBlock.NumberU64()-1)
  699. bc.currentBlock.Store(newBlock)
  700. rawdb.WriteHeadBlockHash(bc.db, newBlock.Hash())
  701. }
  702. }
  703. }
  704. // InsertReceiptChain attempts to complete an already existing header chain with
  705. // transaction and receipt data.
  706. func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain []types.Receipts) (int, error) {
  707. bc.wg.Add(1)
  708. defer bc.wg.Done()
  709. // Do a sanity check that the provided chain is actually ordered and linked
  710. for i := 1; i < len(blockChain); i++ {
  711. if blockChain[i].NumberU64() != blockChain[i-1].NumberU64()+1 || blockChain[i].ParentHash() != blockChain[i-1].Hash() {
  712. log.Error("Non contiguous receipt insert", "number", blockChain[i].Number(), "hash", blockChain[i].Hash(), "parent", blockChain[i].ParentHash(),
  713. "prevnumber", blockChain[i-1].Number(), "prevhash", blockChain[i-1].Hash())
  714. return 0, fmt.Errorf("non contiguous insert: item %d is #%d [%x…], item %d is #%d [%x…] (parent [%x…])", i-1, blockChain[i-1].NumberU64(),
  715. blockChain[i-1].Hash().Bytes()[:4], i, blockChain[i].NumberU64(), blockChain[i].Hash().Bytes()[:4], blockChain[i].ParentHash().Bytes()[:4])
  716. }
  717. }
  718. var (
  719. stats = struct{ processed, ignored int32 }{}
  720. start = time.Now()
  721. bytes = 0
  722. batch = bc.db.NewBatch()
  723. )
  724. for i, block := range blockChain {
  725. receipts := receiptChain[i]
  726. // Short circuit insertion if shutting down or processing failed
  727. if atomic.LoadInt32(&bc.procInterrupt) == 1 {
  728. return 0, nil
  729. }
  730. // Short circuit if the owner header is unknown
  731. if !bc.HasHeader(block.Hash(), block.NumberU64()) {
  732. return i, fmt.Errorf("containing header #%d [%x…] unknown", block.Number(), block.Hash().Bytes()[:4])
  733. }
  734. // Skip if the entire data is already known
  735. if bc.HasBlock(block.Hash(), block.NumberU64()) {
  736. stats.ignored++
  737. continue
  738. }
  739. // Compute all the non-consensus fields of the receipts
  740. if err := receipts.DeriveFields(bc.chainConfig, block.Hash(), block.NumberU64(), block.Transactions()); err != nil {
  741. return i, fmt.Errorf("failed to derive receipts data: %v", err)
  742. }
  743. // Write all the data out into the database
  744. rawdb.WriteBody(batch, block.Hash(), block.NumberU64(), block.Body())
  745. rawdb.WriteReceipts(batch, block.Hash(), block.NumberU64(), receipts)
  746. rawdb.WriteTxLookupEntries(batch, block)
  747. stats.processed++
  748. if batch.ValueSize() >= ethdb.IdealBatchSize {
  749. if err := batch.Write(); err != nil {
  750. return 0, err
  751. }
  752. bytes += batch.ValueSize()
  753. batch.Reset()
  754. }
  755. }
  756. if batch.ValueSize() > 0 {
  757. bytes += batch.ValueSize()
  758. if err := batch.Write(); err != nil {
  759. return 0, err
  760. }
  761. }
  762. // Update the head fast sync block if better
  763. bc.chainmu.Lock()
  764. head := blockChain[len(blockChain)-1]
  765. if td := bc.GetTd(head.Hash(), head.NumberU64()); td != nil { // Rewind may have occurred, skip in that case
  766. currentFastBlock := bc.CurrentFastBlock()
  767. if bc.GetTd(currentFastBlock.Hash(), currentFastBlock.NumberU64()).Cmp(td) < 0 {
  768. rawdb.WriteHeadFastBlockHash(bc.db, head.Hash())
  769. bc.currentFastBlock.Store(head)
  770. }
  771. }
  772. bc.chainmu.Unlock()
  773. context := []interface{}{
  774. "count", stats.processed, "elapsed", common.PrettyDuration(time.Since(start)),
  775. "number", head.Number(), "hash", head.Hash(), "age", common.PrettyAge(time.Unix(int64(head.Time()), 0)),
  776. "size", common.StorageSize(bytes),
  777. }
  778. if stats.ignored > 0 {
  779. context = append(context, []interface{}{"ignored", stats.ignored}...)
  780. }
  781. log.Info("Imported new block receipts", context...)
  782. return 0, nil
  783. }
  784. var lastWrite uint64
  785. // writeBlockWithoutState writes only the block and its metadata to the database,
  786. // but does not write any state. This is used to construct competing side forks
  787. // up to the point where they exceed the canonical total difficulty.
  788. func (bc *BlockChain) writeBlockWithoutState(block *types.Block, td *big.Int) (err error) {
  789. bc.wg.Add(1)
  790. defer bc.wg.Done()
  791. if err := bc.hc.WriteTd(block.Hash(), block.NumberU64(), td); err != nil {
  792. return err
  793. }
  794. rawdb.WriteBlock(bc.db, block)
  795. return nil
  796. }
  797. // writeKnownBlock updates the head block flag with a known block
  798. // and introduces chain reorg if necessary.
  799. func (bc *BlockChain) writeKnownBlock(block *types.Block) error {
  800. bc.wg.Add(1)
  801. defer bc.wg.Done()
  802. current := bc.CurrentBlock()
  803. if block.ParentHash() != current.Hash() {
  804. if err := bc.reorg(current, block); err != nil {
  805. return err
  806. }
  807. }
  808. // Write the positional metadata for transaction/receipt lookups.
  809. // Preimages here is empty, ignore it.
  810. rawdb.WriteTxLookupEntries(bc.db, block)
  811. bc.insert(block)
  812. return nil
  813. }
  814. // WriteBlockWithState writes the block and all associated state to the database.
  815. func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types.Receipt, state *state.StateDB) (status WriteStatus, err error) {
  816. bc.chainmu.Lock()
  817. defer bc.chainmu.Unlock()
  818. return bc.writeBlockWithState(block, receipts, state)
  819. }
  820. // writeBlockWithState writes the block and all associated state to the database,
  821. // but is expects the chain mutex to be held.
  822. func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.Receipt, state *state.StateDB) (status WriteStatus, err error) {
  823. bc.wg.Add(1)
  824. defer bc.wg.Done()
  825. // Calculate the total difficulty of the block
  826. ptd := bc.GetTd(block.ParentHash(), block.NumberU64()-1)
  827. if ptd == nil {
  828. return NonStatTy, consensus.ErrUnknownAncestor
  829. }
  830. // Make sure no inconsistent state is leaked during insertion
  831. currentBlock := bc.CurrentBlock()
  832. localTd := bc.GetTd(currentBlock.Hash(), currentBlock.NumberU64())
  833. externTd := new(big.Int).Add(block.Difficulty(), ptd)
  834. // Irrelevant of the canonical status, write the block itself to the database
  835. if err := bc.hc.WriteTd(block.Hash(), block.NumberU64(), externTd); err != nil {
  836. return NonStatTy, err
  837. }
  838. rawdb.WriteBlock(bc.db, block)
  839. root, err := state.Commit(bc.chainConfig.IsEIP158(block.Number()))
  840. if err != nil {
  841. return NonStatTy, err
  842. }
  843. triedb := bc.stateCache.TrieDB()
  844. // If we're running an archive node, always flush
  845. if bc.cacheConfig.TrieDirtyDisabled {
  846. if err := triedb.Commit(root, false); err != nil {
  847. return NonStatTy, err
  848. }
  849. } else {
  850. // Full but not archive node, do proper garbage collection
  851. triedb.Reference(root, common.Hash{}) // metadata reference to keep trie alive
  852. bc.triegc.Push(root, -int64(block.NumberU64()))
  853. if current := block.NumberU64(); current > triesInMemory {
  854. // If we exceeded our memory allowance, flush matured singleton nodes to disk
  855. var (
  856. nodes, imgs = triedb.Size()
  857. limit = common.StorageSize(bc.cacheConfig.TrieDirtyLimit) * 1024 * 1024
  858. )
  859. if nodes > limit || imgs > 4*1024*1024 {
  860. triedb.Cap(limit - ethdb.IdealBatchSize)
  861. }
  862. // Find the next state trie we need to commit
  863. chosen := current - triesInMemory
  864. // If we exceeded out time allowance, flush an entire trie to disk
  865. if bc.gcproc > bc.cacheConfig.TrieTimeLimit {
  866. // If the header is missing (canonical chain behind), we're reorging a low
  867. // diff sidechain. Suspend committing until this operation is completed.
  868. header := bc.GetHeaderByNumber(chosen)
  869. if header == nil {
  870. log.Warn("Reorg in progress, trie commit postponed", "number", chosen)
  871. } else {
  872. // If we're exceeding limits but haven't reached a large enough memory gap,
  873. // warn the user that the system is becoming unstable.
  874. if chosen < lastWrite+triesInMemory && bc.gcproc >= 2*bc.cacheConfig.TrieTimeLimit {
  875. log.Info("State in memory for too long, committing", "time", bc.gcproc, "allowance", bc.cacheConfig.TrieTimeLimit, "optimum", float64(chosen-lastWrite)/triesInMemory)
  876. }
  877. // Flush an entire trie and restart the counters
  878. triedb.Commit(header.Root, true)
  879. lastWrite = chosen
  880. bc.gcproc = 0
  881. }
  882. }
  883. // Garbage collect anything below our required write retention
  884. for !bc.triegc.Empty() {
  885. root, number := bc.triegc.Pop()
  886. if uint64(-number) > chosen {
  887. bc.triegc.Push(root, number)
  888. break
  889. }
  890. triedb.Dereference(root.(common.Hash))
  891. }
  892. }
  893. }
  894. // Write other block data using a batch.
  895. batch := bc.db.NewBatch()
  896. rawdb.WriteReceipts(batch, block.Hash(), block.NumberU64(), receipts)
  897. // If the total difficulty is higher than our known, add it to the canonical chain
  898. // Second clause in the if statement reduces the vulnerability to selfish mining.
  899. // Please refer to http://www.cs.cornell.edu/~ie53/publications/btcProcFC.pdf
  900. reorg := externTd.Cmp(localTd) > 0
  901. currentBlock = bc.CurrentBlock()
  902. if !reorg && externTd.Cmp(localTd) == 0 {
  903. // Split same-difficulty blocks by number, then preferentially select
  904. // the block generated by the local miner as the canonical block.
  905. if block.NumberU64() < currentBlock.NumberU64() {
  906. reorg = true
  907. } else if block.NumberU64() == currentBlock.NumberU64() {
  908. var currentPreserve, blockPreserve bool
  909. if bc.shouldPreserve != nil {
  910. currentPreserve, blockPreserve = bc.shouldPreserve(currentBlock), bc.shouldPreserve(block)
  911. }
  912. reorg = !currentPreserve && (blockPreserve || mrand.Float64() < 0.5)
  913. }
  914. }
  915. if reorg {
  916. // Reorganise the chain if the parent is not the head block
  917. if block.ParentHash() != currentBlock.Hash() {
  918. if err := bc.reorg(currentBlock, block); err != nil {
  919. return NonStatTy, err
  920. }
  921. }
  922. // Write the positional metadata for transaction/receipt lookups and preimages
  923. rawdb.WriteTxLookupEntries(batch, block)
  924. rawdb.WritePreimages(batch, state.Preimages())
  925. status = CanonStatTy
  926. } else {
  927. status = SideStatTy
  928. }
  929. if err := batch.Write(); err != nil {
  930. return NonStatTy, err
  931. }
  932. // Set new head.
  933. if status == CanonStatTy {
  934. bc.insert(block)
  935. }
  936. bc.futureBlocks.Remove(block.Hash())
  937. return status, nil
  938. }
  939. // addFutureBlock checks if the block is within the max allowed window to get
  940. // accepted for future processing, and returns an error if the block is too far
  941. // ahead and was not added.
  942. func (bc *BlockChain) addFutureBlock(block *types.Block) error {
  943. max := uint64(time.Now().Unix() + maxTimeFutureBlocks)
  944. if block.Time() > max {
  945. return fmt.Errorf("future block timestamp %v > allowed %v", block.Time(), max)
  946. }
  947. bc.futureBlocks.Add(block.Hash(), block)
  948. return nil
  949. }
  950. // InsertChain attempts to insert the given batch of blocks in to the canonical
  951. // chain or, otherwise, create a fork. If an error is returned it will return
  952. // the index number of the failing block as well an error describing what went
  953. // wrong.
  954. //
  955. // After insertion is done, all accumulated events will be fired.
  956. func (bc *BlockChain) InsertChain(chain types.Blocks) (int, error) {
  957. // Sanity check that we have something meaningful to import
  958. if len(chain) == 0 {
  959. return 0, nil
  960. }
  961. bc.blockProcFeed.Send(true)
  962. defer bc.blockProcFeed.Send(false)
  963. // Remove already known canon-blocks
  964. var (
  965. block, prev *types.Block
  966. )
  967. // Do a sanity check that the provided chain is actually ordered and linked
  968. for i := 1; i < len(chain); i++ {
  969. block = chain[i]
  970. prev = chain[i-1]
  971. if block.NumberU64() != prev.NumberU64()+1 || block.ParentHash() != prev.Hash() {
  972. // Chain broke ancestry, log a message (programming error) and skip insertion
  973. log.Error("Non contiguous block insert", "number", block.Number(), "hash", block.Hash(),
  974. "parent", block.ParentHash(), "prevnumber", prev.Number(), "prevhash", prev.Hash())
  975. return 0, fmt.Errorf("non contiguous insert: item %d is #%d [%x…], item %d is #%d [%x…] (parent [%x…])", i-1, prev.NumberU64(),
  976. prev.Hash().Bytes()[:4], i, block.NumberU64(), block.Hash().Bytes()[:4], block.ParentHash().Bytes()[:4])
  977. }
  978. }
  979. // Pre-checks passed, start the full block imports
  980. bc.wg.Add(1)
  981. bc.chainmu.Lock()
  982. n, events, logs, err := bc.insertChain(chain, true)
  983. bc.chainmu.Unlock()
  984. bc.wg.Done()
  985. bc.PostChainEvents(events, logs)
  986. return n, err
  987. }
  988. // insertChain is the internal implementation of InsertChain, which assumes that
  989. // 1) chains are contiguous, and 2) The chain mutex is held.
  990. //
  991. // This method is split out so that import batches that require re-injecting
  992. // historical blocks can do so without releasing the lock, which could lead to
  993. // racey behaviour. If a sidechain import is in progress, and the historic state
  994. // is imported, but then new canon-head is added before the actual sidechain
  995. // completes, then the historic state could be pruned again
  996. func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, []interface{}, []*types.Log, error) {
  997. // If the chain is terminating, don't even bother starting up
  998. if atomic.LoadInt32(&bc.procInterrupt) == 1 {
  999. return 0, nil, nil, nil
  1000. }
  1001. // Start a parallel signature recovery (signer will fluke on fork transition, minimal perf loss)
  1002. senderCacher.recoverFromBlocks(types.MakeSigner(bc.chainConfig, chain[0].Number()), chain)
  1003. // A queued approach to delivering events. This is generally
  1004. // faster than direct delivery and requires much less mutex
  1005. // acquiring.
  1006. var (
  1007. stats = insertStats{startTime: mclock.Now()}
  1008. events = make([]interface{}, 0, len(chain))
  1009. lastCanon *types.Block
  1010. coalescedLogs []*types.Log
  1011. )
  1012. // Start the parallel header verifier
  1013. headers := make([]*types.Header, len(chain))
  1014. seals := make([]bool, len(chain))
  1015. for i, block := range chain {
  1016. headers[i] = block.Header()
  1017. seals[i] = verifySeals
  1018. }
  1019. abort, results := bc.engine.VerifyHeaders(bc, headers, seals)
  1020. defer close(abort)
  1021. // Peek the error for the first block to decide the directing import logic
  1022. it := newInsertIterator(chain, results, bc.validator)
  1023. block, err := it.next()
  1024. // Left-trim all the known blocks
  1025. if err == ErrKnownBlock {
  1026. // First block (and state) is known
  1027. // 1. We did a roll-back, and should now do a re-import
  1028. // 2. The block is stored as a sidechain, and is lying about it's stateroot, and passes a stateroot
  1029. // from the canonical chain, which has not been verified.
  1030. // Skip all known blocks that are behind us
  1031. var (
  1032. current = bc.CurrentBlock()
  1033. localTd = bc.GetTd(current.Hash(), current.NumberU64())
  1034. externTd = bc.GetTd(block.ParentHash(), block.NumberU64()-1) // The first block can't be nil
  1035. )
  1036. for block != nil && err == ErrKnownBlock {
  1037. externTd = new(big.Int).Add(externTd, block.Difficulty())
  1038. if localTd.Cmp(externTd) < 0 {
  1039. break
  1040. }
  1041. log.Debug("Ignoring already known block", "number", block.Number(), "hash", block.Hash())
  1042. stats.ignored++
  1043. block, err = it.next()
  1044. }
  1045. // The remaining blocks are still known blocks, the only scenario here is:
  1046. // During the fast sync, the pivot point is already submitted but rollback
  1047. // happens. Then node resets the head full block to a lower height via `rollback`
  1048. // and leaves a few known blocks in the database.
  1049. //
  1050. // When node runs a fast sync again, it can re-import a batch of known blocks via
  1051. // `insertChain` while a part of them have higher total difficulty than current
  1052. // head full block(new pivot point).
  1053. for block != nil && err == ErrKnownBlock {
  1054. log.Debug("Writing previously known block", "number", block.Number(), "hash", block.Hash())
  1055. if err := bc.writeKnownBlock(block); err != nil {
  1056. return it.index, nil, nil, err
  1057. }
  1058. lastCanon = block
  1059. block, err = it.next()
  1060. }
  1061. // Falls through to the block import
  1062. }
  1063. switch {
  1064. // First block is pruned, insert as sidechain and reorg only if TD grows enough
  1065. case err == consensus.ErrPrunedAncestor:
  1066. log.Debug("Pruned ancestor, inserting as sidechain", "number", block.Number(), "hash", block.Hash())
  1067. return bc.insertSideChain(block, it)
  1068. // First block is future, shove it (and all children) to the future queue (unknown ancestor)
  1069. case err == consensus.ErrFutureBlock || (err == consensus.ErrUnknownAncestor && bc.futureBlocks.Contains(it.first().ParentHash())):
  1070. for block != nil && (it.index == 0 || err == consensus.ErrUnknownAncestor) {
  1071. log.Debug("Future block, postponing import", "number", block.Number(), "hash", block.Hash())
  1072. if err := bc.addFutureBlock(block); err != nil {
  1073. return it.index, events, coalescedLogs, err
  1074. }
  1075. block, err = it.next()
  1076. }
  1077. stats.queued += it.processed()
  1078. stats.ignored += it.remaining()
  1079. // If there are any still remaining, mark as ignored
  1080. return it.index, events, coalescedLogs, err
  1081. // Some other error occurred, abort
  1082. case err != nil:
  1083. stats.ignored += len(it.chain)
  1084. bc.reportBlock(block, nil, err)
  1085. return it.index, events, coalescedLogs, err
  1086. }
  1087. // No validation errors for the first block (or chain prefix skipped)
  1088. for ; block != nil && err == nil || err == ErrKnownBlock; block, err = it.next() {
  1089. // If the chain is terminating, stop processing blocks
  1090. if atomic.LoadInt32(&bc.procInterrupt) == 1 {
  1091. log.Debug("Premature abort during blocks processing")
  1092. break
  1093. }
  1094. // If the header is a banned one, straight out abort
  1095. if BadHashes[block.Hash()] {
  1096. bc.reportBlock(block, nil, ErrBlacklistedHash)
  1097. return it.index, events, coalescedLogs, ErrBlacklistedHash
  1098. }
  1099. // If the block is known (in the middle of the chain), it's a special case for
  1100. // Clique blocks where they can share state among each other, so importing an
  1101. // older block might complete the state of the subsequent one. In this case,
  1102. // just skip the block (we already validated it once fully (and crashed), since
  1103. // its header and body was already in the database).
  1104. if err == ErrKnownBlock {
  1105. logger := log.Debug
  1106. if bc.chainConfig.Clique == nil {
  1107. logger = log.Warn
  1108. }
  1109. logger("Inserted known block", "number", block.Number(), "hash", block.Hash(),
  1110. "uncles", len(block.Uncles()), "txs", len(block.Transactions()), "gas", block.GasUsed(),
  1111. "root", block.Root())
  1112. if err := bc.writeKnownBlock(block); err != nil {
  1113. return it.index, nil, nil, err
  1114. }
  1115. stats.processed++
  1116. // We can assume that logs are empty here, since the only way for consecutive
  1117. // Clique blocks to have the same state is if there are no transactions.
  1118. events = append(events, ChainEvent{block, block.Hash(), nil})
  1119. lastCanon = block
  1120. continue
  1121. }
  1122. // Retrieve the parent block and it's state to execute on top
  1123. start := time.Now()
  1124. parent := it.previous()
  1125. if parent == nil {
  1126. parent = bc.GetHeader(block.ParentHash(), block.NumberU64()-1)
  1127. }
  1128. statedb, err := state.New(parent.Root, bc.stateCache)
  1129. if err != nil {
  1130. return it.index, events, coalescedLogs, err
  1131. }
  1132. // If we have a followup block, run that against the current state to pre-cache
  1133. // transactions and probabilistically some of the account/storage trie nodes.
  1134. var followupInterrupt uint32
  1135. if !bc.cacheConfig.TrieCleanNoPrefetch {
  1136. if followup, err := it.peek(); followup != nil && err == nil {
  1137. go func(start time.Time) {
  1138. throwaway, _ := state.New(parent.Root, bc.stateCache)
  1139. bc.prefetcher.Prefetch(followup, throwaway, bc.vmConfig, &followupInterrupt)
  1140. blockPrefetchExecuteTimer.Update(time.Since(start))
  1141. if atomic.LoadUint32(&followupInterrupt) == 1 {
  1142. blockPrefetchInterruptMeter.Mark(1)
  1143. }
  1144. }(time.Now())
  1145. }
  1146. }
  1147. // Process block using the parent state as reference point
  1148. substart := time.Now()
  1149. receipts, logs, usedGas, err := bc.processor.Process(block, statedb, bc.vmConfig)
  1150. if err != nil {
  1151. bc.reportBlock(block, receipts, err)
  1152. atomic.StoreUint32(&followupInterrupt, 1)
  1153. return it.index, events, coalescedLogs, err
  1154. }
  1155. // Update the metrics touched during block processing
  1156. accountReadTimer.Update(statedb.AccountReads) // Account reads are complete, we can mark them
  1157. storageReadTimer.Update(statedb.StorageReads) // Storage reads are complete, we can mark them
  1158. accountUpdateTimer.Update(statedb.AccountUpdates) // Account updates are complete, we can mark them
  1159. storageUpdateTimer.Update(statedb.StorageUpdates) // Storage updates are complete, we can mark them
  1160. triehash := statedb.AccountHashes + statedb.StorageHashes // Save to not double count in validation
  1161. trieproc := statedb.AccountReads + statedb.AccountUpdates
  1162. trieproc += statedb.StorageReads + statedb.StorageUpdates
  1163. blockExecutionTimer.Update(time.Since(substart) - trieproc - triehash)
  1164. // Validate the state using the default validator
  1165. substart = time.Now()
  1166. if err := bc.validator.ValidateState(block, statedb, receipts, usedGas); err != nil {
  1167. bc.reportBlock(block, receipts, err)
  1168. atomic.StoreUint32(&followupInterrupt, 1)
  1169. return it.index, events, coalescedLogs, err
  1170. }
  1171. proctime := time.Since(start)
  1172. // Update the metrics touched during block validation
  1173. accountHashTimer.Update(statedb.AccountHashes) // Account hashes are complete, we can mark them
  1174. storageHashTimer.Update(statedb.StorageHashes) // Storage hashes are complete, we can mark them
  1175. blockValidationTimer.Update(time.Since(substart) - (statedb.AccountHashes + statedb.StorageHashes - triehash))
  1176. // Write the block to the chain and get the status.
  1177. substart = time.Now()
  1178. status, err := bc.writeBlockWithState(block, receipts, statedb)
  1179. if err != nil {
  1180. atomic.StoreUint32(&followupInterrupt, 1)
  1181. return it.index, events, coalescedLogs, err
  1182. }
  1183. atomic.StoreUint32(&followupInterrupt, 1)
  1184. // Update the metrics touched during block commit
  1185. accountCommitTimer.Update(statedb.AccountCommits) // Account commits are complete, we can mark them
  1186. storageCommitTimer.Update(statedb.StorageCommits) // Storage commits are complete, we can mark them
  1187. blockWriteTimer.Update(time.Since(substart) - statedb.AccountCommits - statedb.StorageCommits)
  1188. blockInsertTimer.UpdateSince(start)
  1189. switch status {
  1190. case CanonStatTy:
  1191. log.Debug("Inserted new block", "number", block.Number(), "hash", block.Hash(),
  1192. "uncles", len(block.Uncles()), "txs", len(block.Transactions()), "gas", block.GasUsed(),
  1193. "elapsed", common.PrettyDuration(time.Since(start)),
  1194. "root", block.Root())
  1195. coalescedLogs = append(coalescedLogs, logs...)
  1196. events = append(events, ChainEvent{block, block.Hash(), logs})
  1197. lastCanon = block
  1198. // Only count canonical blocks for GC processing time
  1199. bc.gcproc += proctime
  1200. case SideStatTy:
  1201. log.Debug("Inserted forked block", "number", block.Number(), "hash", block.Hash(),
  1202. "diff", block.Difficulty(), "elapsed", common.PrettyDuration(time.Since(start)),
  1203. "txs", len(block.Transactions()), "gas", block.GasUsed(), "uncles", len(block.Uncles()),
  1204. "root", block.Root())
  1205. events = append(events, ChainSideEvent{block})
  1206. default:
  1207. // This in theory is impossible, but lets be nice to our future selves and leave
  1208. // a log, instead of trying to track down blocks imports that don't emit logs.
  1209. log.Warn("Inserted block with unknown status", "number", block.Number(), "hash", block.Hash(),
  1210. "diff", block.Difficulty(), "elapsed", common.PrettyDuration(time.Since(start)),
  1211. "txs", len(block.Transactions()), "gas", block.GasUsed(), "uncles", len(block.Uncles()),
  1212. "root", block.Root())
  1213. }
  1214. stats.processed++
  1215. stats.usedGas += usedGas
  1216. dirty, _ := bc.stateCache.TrieDB().Size()
  1217. stats.report(chain, it.index, dirty)
  1218. }
  1219. // Any blocks remaining here? The only ones we care about are the future ones
  1220. if block != nil && err == consensus.ErrFutureBlock {
  1221. if err := bc.addFutureBlock(block); err != nil {
  1222. return it.index, events, coalescedLogs, err
  1223. }
  1224. block, err = it.next()
  1225. for ; block != nil && err == consensus.ErrUnknownAncestor; block, err = it.next() {
  1226. if err := bc.addFutureBlock(block); err != nil {
  1227. return it.index, events, coalescedLogs, err
  1228. }
  1229. stats.queued++
  1230. }
  1231. }
  1232. stats.ignored += it.remaining()
  1233. // Append a single chain head event if we've progressed the chain
  1234. if lastCanon != nil && bc.CurrentBlock().Hash() == lastCanon.Hash() {
  1235. events = append(events, ChainHeadEvent{lastCanon})
  1236. }
  1237. return it.index, events, coalescedLogs, err
  1238. }
  1239. // insertSideChain is called when an import batch hits upon a pruned ancestor
  1240. // error, which happens when a sidechain with a sufficiently old fork-block is
  1241. // found.
  1242. //
  1243. // The method writes all (header-and-body-valid) blocks to disk, then tries to
  1244. // switch over to the new chain if the TD exceeded the current chain.
  1245. func (bc *BlockChain) insertSideChain(block *types.Block, it *insertIterator) (int, []interface{}, []*types.Log, error) {
  1246. var (
  1247. externTd *big.Int
  1248. current = bc.CurrentBlock()
  1249. )
  1250. // The first sidechain block error is already verified to be ErrPrunedAncestor.
  1251. // Since we don't import them here, we expect ErrUnknownAncestor for the remaining
  1252. // ones. Any other errors means that the block is invalid, and should not be written
  1253. // to disk.
  1254. err := consensus.ErrPrunedAncestor
  1255. for ; block != nil && (err == consensus.ErrPrunedAncestor); block, err = it.next() {
  1256. // Check the canonical state root for that number
  1257. if number := block.NumberU64(); current.NumberU64() >= number {
  1258. canonical := bc.GetBlockByNumber(number)
  1259. if canonical != nil && canonical.Hash() == block.Hash() {
  1260. // Not a sidechain block, this is a re-import of a canon block which has it's state pruned
  1261. continue
  1262. }
  1263. if canonical != nil && canonical.Root() == block.Root() {
  1264. // This is most likely a shadow-state attack. When a fork is imported into the
  1265. // database, and it eventually reaches a block height which is not pruned, we
  1266. // just found that the state already exist! This means that the sidechain block
  1267. // refers to a state which already exists in our canon chain.
  1268. //
  1269. // If left unchecked, we would now proceed importing the blocks, without actually
  1270. // having verified the state of the previous blocks.
  1271. log.Warn("Sidechain ghost-state attack detected", "number", block.NumberU64(), "sideroot", block.Root(), "canonroot", canonical.Root())
  1272. // If someone legitimately side-mines blocks, they would still be imported as usual. However,
  1273. // we cannot risk writing unverified blocks to disk when they obviously target the pruning
  1274. // mechanism.
  1275. return it.index, nil, nil, errors.New("sidechain ghost-state attack")
  1276. }
  1277. }
  1278. if externTd == nil {
  1279. externTd = bc.GetTd(block.ParentHash(), block.NumberU64()-1)
  1280. }
  1281. externTd = new(big.Int).Add(externTd, block.Difficulty())
  1282. if !bc.HasBlock(block.Hash(), block.NumberU64()) {
  1283. start := time.Now()
  1284. if err := bc.writeBlockWithoutState(block, externTd); err != nil {
  1285. return it.index, nil, nil, err
  1286. }
  1287. log.Debug("Injected sidechain block", "number", block.Number(), "hash", block.Hash(),
  1288. "diff", block.Difficulty(), "elapsed", common.PrettyDuration(time.Since(start)),
  1289. "txs", len(block.Transactions()), "gas", block.GasUsed(), "uncles", len(block.Uncles()),
  1290. "root", block.Root())
  1291. }
  1292. }
  1293. // At this point, we've written all sidechain blocks to database. Loop ended
  1294. // either on some other error or all were processed. If there was some other
  1295. // error, we can ignore the rest of those blocks.
  1296. //
  1297. // If the externTd was larger than our local TD, we now need to reimport the previous
  1298. // blocks to regenerate the required state
  1299. localTd := bc.GetTd(current.Hash(), current.NumberU64())
  1300. if localTd.Cmp(externTd) > 0 {
  1301. log.Info("Sidechain written to disk", "start", it.first().NumberU64(), "end", it.previous().Number, "sidetd", externTd, "localtd", localTd)
  1302. return it.index, nil, nil, err
  1303. }
  1304. // Gather all the sidechain hashes (full blocks may be memory heavy)
  1305. var (
  1306. hashes []common.Hash
  1307. numbers []uint64
  1308. )
  1309. parent := it.previous()
  1310. for parent != nil && !bc.HasState(parent.Root) {
  1311. hashes = append(hashes, parent.Hash())
  1312. numbers = append(numbers, parent.Number.Uint64())
  1313. parent = bc.GetHeader(parent.ParentHash, parent.Number.Uint64()-1)
  1314. }
  1315. if parent == nil {
  1316. return it.index, nil, nil, errors.New("missing parent")
  1317. }
  1318. // Import all the pruned blocks to make the state available
  1319. var (
  1320. blocks []*types.Block
  1321. memory common.StorageSize
  1322. )
  1323. for i := len(hashes) - 1; i >= 0; i-- {
  1324. // Append the next block to our batch
  1325. block := bc.GetBlock(hashes[i], numbers[i])
  1326. blocks = append(blocks, block)
  1327. memory += block.Size()
  1328. // If memory use grew too large, import and continue. Sadly we need to discard
  1329. // all raised events and logs from notifications since we're too heavy on the
  1330. // memory here.
  1331. if len(blocks) >= 2048 || memory > 64*1024*1024 {
  1332. log.Info("Importing heavy sidechain segment", "blocks", len(blocks), "start", blocks[0].NumberU64(), "end", block.NumberU64())
  1333. if _, _, _, err := bc.insertChain(blocks, false); err != nil {
  1334. return 0, nil, nil, err
  1335. }
  1336. blocks, memory = blocks[:0], 0
  1337. // If the chain is terminating, stop processing blocks
  1338. if atomic.LoadInt32(&bc.procInterrupt) == 1 {
  1339. log.Debug("Premature abort during blocks processing")
  1340. return 0, nil, nil, nil
  1341. }
  1342. }
  1343. }
  1344. if len(blocks) > 0 {
  1345. log.Info("Importing sidechain segment", "start", blocks[0].NumberU64(), "end", blocks[len(blocks)-1].NumberU64())
  1346. return bc.insertChain(blocks, false)
  1347. }
  1348. return 0, nil, nil, nil
  1349. }
  1350. // reorg takes two blocks, an old chain and a new chain and will reconstruct the
  1351. // blocks and inserts them to be part of the new canonical chain and accumulates
  1352. // potential missing transactions and post an event about them.
  1353. func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
  1354. var (
  1355. newChain types.Blocks
  1356. oldChain types.Blocks
  1357. commonBlock *types.Block
  1358. deletedTxs types.Transactions
  1359. addedTxs types.Transactions
  1360. deletedLogs []*types.Log
  1361. rebirthLogs []*types.Log
  1362. // collectLogs collects the logs that were generated during the
  1363. // processing of the block that corresponds with the given hash.
  1364. // These logs are later announced as deleted or reborn
  1365. collectLogs = func(hash common.Hash, removed bool) {
  1366. number := bc.hc.GetBlockNumber(hash)
  1367. if number == nil {
  1368. return
  1369. }
  1370. receipts := rawdb.ReadReceipts(bc.db, hash, *number, bc.chainConfig)
  1371. for _, receipt := range receipts {
  1372. for _, log := range receipt.Logs {
  1373. l := *log
  1374. if removed {
  1375. l.Removed = true
  1376. deletedLogs = append(deletedLogs, &l)
  1377. } else {
  1378. rebirthLogs = append(rebirthLogs, &l)
  1379. }
  1380. }
  1381. }
  1382. }
  1383. )
  1384. // Reduce the longer chain to the same number as the shorter one
  1385. if oldBlock.NumberU64() > newBlock.NumberU64() {
  1386. // Old chain is longer, gather all transactions and logs as deleted ones
  1387. for ; oldBlock != nil && oldBlock.NumberU64() != newBlock.NumberU64(); oldBlock = bc.GetBlock(oldBlock.ParentHash(), oldBlock.NumberU64()-1) {
  1388. oldChain = append(oldChain, oldBlock)
  1389. deletedTxs = append(deletedTxs, oldBlock.Transactions()...)
  1390. collectLogs(oldBlock.Hash(), true)
  1391. }
  1392. } else {
  1393. // New chain is longer, stash all blocks away for subsequent insertion
  1394. for ; newBlock != nil && newBlock.NumberU64() != oldBlock.NumberU64(); newBlock = bc.GetBlock(newBlock.ParentHash(), newBlock.NumberU64()-1) {
  1395. newChain = append(newChain, newBlock)
  1396. }
  1397. }
  1398. if oldBlock == nil {
  1399. return fmt.Errorf("invalid old chain")
  1400. }
  1401. if newBlock == nil {
  1402. return fmt.Errorf("invalid new chain")
  1403. }
  1404. // Both sides of the reorg are at the same number, reduce both until the common
  1405. // ancestor is found
  1406. for {
  1407. // If the common ancestor was found, bail out
  1408. if oldBlock.Hash() == newBlock.Hash() {
  1409. commonBlock = oldBlock
  1410. break
  1411. }
  1412. // Remove an old block as well as stash away a new block
  1413. oldChain = append(oldChain, oldBlock)
  1414. deletedTxs = append(deletedTxs, oldBlock.Transactions()...)
  1415. collectLogs(oldBlock.Hash(), true)
  1416. newChain = append(newChain, newBlock)
  1417. // Step back with both chains
  1418. oldBlock = bc.GetBlock(oldBlock.ParentHash(), oldBlock.NumberU64()-1)
  1419. if oldBlock == nil {
  1420. return fmt.Errorf("invalid old chain")
  1421. }
  1422. newBlock = bc.GetBlock(newBlock.ParentHash(), newBlock.NumberU64()-1)
  1423. if newBlock == nil {
  1424. return fmt.Errorf("invalid new chain")
  1425. }
  1426. }
  1427. // Ensure the user sees large reorgs
  1428. if len(oldChain) > 0 && len(newChain) > 0 {
  1429. logFn := log.Debug
  1430. if len(oldChain) > 63 {
  1431. logFn = log.Warn
  1432. }
  1433. logFn("Chain split detected", "number", commonBlock.Number(), "hash", commonBlock.Hash(),
  1434. "drop", len(oldChain), "dropfrom", oldChain[0].Hash(), "add", len(newChain), "addfrom", newChain[0].Hash())
  1435. } else {
  1436. log.Error("Impossible reorg, please file an issue", "oldnum", oldBlock.Number(), "oldhash", oldBlock.Hash(), "newnum", newBlock.Number(), "newhash", newBlock.Hash())
  1437. }
  1438. // Insert the new chain(except the head block(reverse order)),
  1439. // taking care of the proper incremental order.
  1440. for i := len(newChain) - 1; i >= 1; i-- {
  1441. // Insert the block in the canonical way, re-writing history
  1442. bc.insert(newChain[i])
  1443. // Collect reborn logs due to chain reorg
  1444. collectLogs(newChain[i].Hash(), false)
  1445. // Write lookup entries for hash based transaction/receipt searches
  1446. rawdb.WriteTxLookupEntries(bc.db, newChain[i])
  1447. addedTxs = append(addedTxs, newChain[i].Transactions()...)
  1448. }
  1449. // When transactions get deleted from the database, the receipts that were
  1450. // created in the fork must also be deleted
  1451. batch := bc.db.NewBatch()
  1452. for _, tx := range types.TxDifference(deletedTxs, addedTxs) {
  1453. rawdb.DeleteTxLookupEntry(batch, tx.Hash())
  1454. }
  1455. // Delete any canonical number assignments above the new head
  1456. number := bc.CurrentBlock().NumberU64()
  1457. for i := number + 1; ; i++ {
  1458. hash := rawdb.ReadCanonicalHash(bc.db, i)
  1459. if hash == (common.Hash{}) {
  1460. break
  1461. }
  1462. rawdb.DeleteCanonicalHash(batch, i)
  1463. }
  1464. batch.Write()
  1465. // If any logs need to be fired, do it now. In theory we could avoid creating
  1466. // this goroutine if there are no events to fire, but realistcally that only
  1467. // ever happens if we're reorging empty blocks, which will only happen on idle
  1468. // networks where performance is not an issue either way.
  1469. //
  1470. // TODO(karalabe): Can we get rid of the goroutine somehow to guarantee correct
  1471. // event ordering?
  1472. go func() {
  1473. if len(deletedLogs) > 0 {
  1474. bc.rmLogsFeed.Send(RemovedLogsEvent{deletedLogs})
  1475. }
  1476. if len(rebirthLogs) > 0 {
  1477. bc.logsFeed.Send(rebirthLogs)
  1478. }
  1479. if len(oldChain) > 0 {
  1480. for _, block := range oldChain {
  1481. bc.chainSideFeed.Send(ChainSideEvent{Block: block})
  1482. }
  1483. }
  1484. }()
  1485. return nil
  1486. }
  1487. // PostChainEvents iterates over the events generated by a chain insertion and
  1488. // posts them into the event feed.
  1489. // TODO: Should not expose PostChainEvents. The chain events should be posted in WriteBlock.
  1490. func (bc *BlockChain) PostChainEvents(events []interface{}, logs []*types.Log) {
  1491. // post event logs for further processing
  1492. if logs != nil {
  1493. bc.logsFeed.Send(logs)
  1494. }
  1495. for _, event := range events {
  1496. switch ev := event.(type) {
  1497. case ChainEvent:
  1498. bc.chainFeed.Send(ev)
  1499. case ChainHeadEvent:
  1500. bc.chainHeadFeed.Send(ev)
  1501. case ChainSideEvent:
  1502. bc.chainSideFeed.Send(ev)
  1503. }
  1504. }
  1505. }
  1506. func (bc *BlockChain) update() {
  1507. futureTimer := time.NewTicker(5 * time.Second)
  1508. defer futureTimer.Stop()
  1509. for {
  1510. select {
  1511. case <-futureTimer.C:
  1512. bc.procFutureBlocks()
  1513. case <-bc.quit:
  1514. return
  1515. }
  1516. }
  1517. }
  1518. // BadBlocks returns a list of the last 'bad blocks' that the client has seen on the network
  1519. func (bc *BlockChain) BadBlocks() []*types.Block {
  1520. blocks := make([]*types.Block, 0, bc.badBlocks.Len())
  1521. for _, hash := range bc.badBlocks.Keys() {
  1522. if blk, exist := bc.badBlocks.Peek(hash); exist {
  1523. block := blk.(*types.Block)
  1524. blocks = append(blocks, block)
  1525. }
  1526. }
  1527. return blocks
  1528. }
  1529. // addBadBlock adds a bad block to the bad-block LRU cache
  1530. func (bc *BlockChain) addBadBlock(block *types.Block) {
  1531. bc.badBlocks.Add(block.Hash(), block)
  1532. }
  1533. // reportBlock logs a bad block error.
  1534. func (bc *BlockChain) reportBlock(block *types.Block, receipts types.Receipts, err error) {
  1535. bc.addBadBlock(block)
  1536. var receiptString string
  1537. for i, receipt := range receipts {
  1538. receiptString += fmt.Sprintf("\t %d: cumulative: %v gas: %v contract: %v status: %v tx: %v logs: %v bloom: %x state: %x\n",
  1539. i, receipt.CumulativeGasUsed, receipt.GasUsed, receipt.ContractAddress.Hex(),
  1540. receipt.Status, receipt.TxHash.Hex(), receipt.Logs, receipt.Bloom, receipt.PostState)
  1541. }
  1542. log.Error(fmt.Sprintf(`
  1543. ########## BAD BLOCK #########
  1544. Chain config: %v
  1545. Number: %v
  1546. Hash: 0x%x
  1547. %v
  1548. Error: %v
  1549. ##############################
  1550. `, bc.chainConfig, block.Number(), block.Hash(), receiptString, err))
  1551. }
  1552. // InsertHeaderChain attempts to insert the given header chain in to the local
  1553. // chain, possibly creating a reorg. If an error is returned, it will return the
  1554. // index number of the failing header as well an error describing what went wrong.
  1555. //
  1556. // The verify parameter can be used to fine tune whether nonce verification
  1557. // should be done or not. The reason behind the optional check is because some
  1558. // of the header retrieval mechanisms already need to verify nonces, as well as
  1559. // because nonces can be verified sparsely, not needing to check each.
  1560. func (bc *BlockChain) InsertHeaderChain(chain []*types.Header, checkFreq int) (int, error) {
  1561. start := time.Now()
  1562. if i, err := bc.hc.ValidateHeaderChain(chain, checkFreq); err != nil {
  1563. return i, err
  1564. }
  1565. // Make sure only one thread manipulates the chain at once
  1566. bc.chainmu.Lock()
  1567. defer bc.chainmu.Unlock()
  1568. bc.wg.Add(1)
  1569. defer bc.wg.Done()
  1570. whFunc := func(header *types.Header) error {
  1571. _, err := bc.hc.WriteHeader(header)
  1572. return err
  1573. }
  1574. return bc.hc.InsertHeaderChain(chain, whFunc, start)
  1575. }
  1576. // CurrentHeader retrieves the current head header of the canonical chain. The
  1577. // header is retrieved from the HeaderChain's internal cache.
  1578. func (bc *BlockChain) CurrentHeader() *types.Header {
  1579. return bc.hc.CurrentHeader()
  1580. }
  1581. // GetTd retrieves a block's total difficulty in the canonical chain from the
  1582. // database by hash and number, caching it if found.
  1583. func (bc *BlockChain) GetTd(hash common.Hash, number uint64) *big.Int {
  1584. return bc.hc.GetTd(hash, number)
  1585. }
  1586. // GetTdByHash retrieves a block's total difficulty in the canonical chain from the
  1587. // database by hash, caching it if found.
  1588. func (bc *BlockChain) GetTdByHash(hash common.Hash) *big.Int {
  1589. return bc.hc.GetTdByHash(hash)
  1590. }
  1591. // GetHeader retrieves a block header from the database by hash and number,
  1592. // caching it if found.
  1593. func (bc *BlockChain) GetHeader(hash common.Hash, number uint64) *types.Header {
  1594. return bc.hc.GetHeader(hash, number)
  1595. }
  1596. // GetHeaderByHash retrieves a block header from the database by hash, caching it if
  1597. // found.
  1598. func (bc *BlockChain) GetHeaderByHash(hash common.Hash) *types.Header {
  1599. return bc.hc.GetHeaderByHash(hash)
  1600. }
  1601. // HasHeader checks if a block header is present in the database or not, caching
  1602. // it if present.
  1603. func (bc *BlockChain) HasHeader(hash common.Hash, number uint64) bool {
  1604. return bc.hc.HasHeader(hash, number)
  1605. }
  1606. // GetBlockHashesFromHash retrieves a number of block hashes starting at a given
  1607. // hash, fetching towards the genesis block.
  1608. func (bc *BlockChain) GetBlockHashesFromHash(hash common.Hash, max uint64) []common.Hash {
  1609. return bc.hc.GetBlockHashesFromHash(hash, max)
  1610. }
  1611. // GetAncestor retrieves the Nth ancestor of a given block. It assumes that either the given block or
  1612. // a close ancestor of it is canonical. maxNonCanonical points to a downwards counter limiting the
  1613. // number of blocks to be individually checked before we reach the canonical chain.
  1614. //
  1615. // Note: ancestor == 0 returns the same block, 1 returns its parent and so on.
  1616. func (bc *BlockChain) GetAncestor(hash common.Hash, number, ancestor uint64, maxNonCanonical *uint64) (common.Hash, uint64) {
  1617. bc.chainmu.RLock()
  1618. defer bc.chainmu.RUnlock()
  1619. return bc.hc.GetAncestor(hash, number, ancestor, maxNonCanonical)
  1620. }
  1621. // GetHeaderByNumber retrieves a block header from the database by number,
  1622. // caching it (associated with its hash) if found.
  1623. func (bc *BlockChain) GetHeaderByNumber(number uint64) *types.Header {
  1624. return bc.hc.GetHeaderByNumber(number)
  1625. }
  1626. // Config retrieves the blockchain's chain configuration.
  1627. func (bc *BlockChain) Config() *params.ChainConfig { return bc.chainConfig }
  1628. // Engine retrieves the blockchain's consensus engine.
  1629. func (bc *BlockChain) Engine() consensus.Engine { return bc.engine }
  1630. // SubscribeRemovedLogsEvent registers a subscription of RemovedLogsEvent.
  1631. func (bc *BlockChain) SubscribeRemovedLogsEvent(ch chan<- RemovedLogsEvent) event.Subscription {
  1632. return bc.scope.Track(bc.rmLogsFeed.Subscribe(ch))
  1633. }
  1634. // SubscribeChainEvent registers a subscription of ChainEvent.
  1635. func (bc *BlockChain) SubscribeChainEvent(ch chan<- ChainEvent) event.Subscription {
  1636. return bc.scope.Track(bc.chainFeed.Subscribe(ch))
  1637. }
  1638. // SubscribeChainHeadEvent registers a subscription of ChainHeadEvent.
  1639. func (bc *BlockChain) SubscribeChainHeadEvent(ch chan<- ChainHeadEvent) event.Subscription {
  1640. return bc.scope.Track(bc.chainHeadFeed.Subscribe(ch))
  1641. }
  1642. // SubscribeChainSideEvent registers a subscription of ChainSideEvent.
  1643. func (bc *BlockChain) SubscribeChainSideEvent(ch chan<- ChainSideEvent) event.Subscription {
  1644. return bc.scope.Track(bc.chainSideFeed.Subscribe(ch))
  1645. }
  1646. // SubscribeLogsEvent registers a subscription of []*types.Log.
  1647. func (bc *BlockChain) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
  1648. return bc.scope.Track(bc.logsFeed.Subscribe(ch))
  1649. }
  1650. // SubscribeBlockProcessingEvent registers a subscription of bool where true means
  1651. // block processing has started while false means it has stopped.
  1652. func (bc *BlockChain) SubscribeBlockProcessingEvent(ch chan<- bool) event.Subscription {
  1653. return bc.scope.Track(bc.blockProcFeed.Subscribe(ch))
  1654. }