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