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