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