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