blockchain.go 46 KB

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