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