github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/core/headerchain.go (about)

     1  // Copyright 2015 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
    18  
    19  import (
    20  	crand "crypto/rand"
    21  	"errors"
    22  	"fmt"
    23  	"math"
    24  	"math/big"
    25  	mrand "math/rand"
    26  	"sync/atomic"
    27  	"time"
    28  
    29  	lru "github.com/hashicorp/golang-lru"
    30  	"github.com/vntchain/go-vnt/common"
    31  	"github.com/vntchain/go-vnt/consensus"
    32  	"github.com/vntchain/go-vnt/core/rawdb"
    33  	"github.com/vntchain/go-vnt/core/types"
    34  	"github.com/vntchain/go-vnt/log"
    35  	"github.com/vntchain/go-vnt/params"
    36  	"github.com/vntchain/go-vnt/vntdb"
    37  )
    38  
    39  const (
    40  	headerCacheLimit = 512
    41  	tdCacheLimit     = 1024
    42  	numberCacheLimit = 2048
    43  )
    44  
    45  // HeaderChain implements the basic block header chain logic that is shared by
    46  // core.BlockChain and light.LightChain. It is not usable in itself, only as
    47  // a part of either structure.
    48  // It is not thread safe either, the encapsulating chain structures should do
    49  // the necessary mutex locking/unlocking.
    50  type HeaderChain struct {
    51  	config *params.ChainConfig
    52  
    53  	chainDb       vntdb.Database
    54  	genesisHeader *types.Header
    55  
    56  	currentHeader     atomic.Value // Current head of the header chain (may be above the block chain!)
    57  	currentHeaderHash common.Hash  // Hash of the current head of the header chain (prevent recomputing all the time)
    58  
    59  	headerCache *lru.Cache // Cache for the most recent block headers
    60  	tdCache     *lru.Cache // Cache for the most recent block total difficulties
    61  	numberCache *lru.Cache // Cache for the most recent block numbers
    62  
    63  	procInterrupt func() bool
    64  
    65  	rand   *mrand.Rand
    66  	engine consensus.Engine
    67  }
    68  
    69  // NewHeaderChain creates a new HeaderChain structure.
    70  //  getValidator should return the parent's validator
    71  //  procInterrupt points to the parent's interrupt semaphore
    72  //  wg points to the parent's shutdown wait group
    73  func NewHeaderChain(chainDb vntdb.Database, config *params.ChainConfig, engine consensus.Engine, procInterrupt func() bool) (*HeaderChain, error) {
    74  	headerCache, _ := lru.New(headerCacheLimit)
    75  	tdCache, _ := lru.New(tdCacheLimit)
    76  	numberCache, _ := lru.New(numberCacheLimit)
    77  
    78  	// Seed a fast but crypto originating random generator
    79  	seed, err := crand.Int(crand.Reader, big.NewInt(math.MaxInt64))
    80  	if err != nil {
    81  		return nil, err
    82  	}
    83  
    84  	hc := &HeaderChain{
    85  		config:        config,
    86  		chainDb:       chainDb,
    87  		headerCache:   headerCache,
    88  		tdCache:       tdCache,
    89  		numberCache:   numberCache,
    90  		procInterrupt: procInterrupt,
    91  		rand:          mrand.New(mrand.NewSource(seed.Int64())),
    92  		engine:        engine,
    93  	}
    94  
    95  	hc.genesisHeader = hc.GetHeaderByNumber(0)
    96  	if hc.genesisHeader == nil {
    97  		return nil, ErrNoGenesis
    98  	}
    99  
   100  	hc.currentHeader.Store(hc.genesisHeader)
   101  	if head := rawdb.ReadHeadBlockHash(chainDb); head != (common.Hash{}) {
   102  		if chead := hc.GetHeaderByHash(head); chead != nil {
   103  			hc.currentHeader.Store(chead)
   104  		}
   105  	}
   106  	hc.currentHeaderHash = hc.CurrentHeader().Hash()
   107  
   108  	return hc, nil
   109  }
   110  
   111  // GetBlockNumber retrieves the block number belonging to the given hash
   112  // from the cache or database
   113  func (hc *HeaderChain) GetBlockNumber(hash common.Hash) *uint64 {
   114  	if cached, ok := hc.numberCache.Get(hash); ok {
   115  		number := cached.(uint64)
   116  		return &number
   117  	}
   118  	number := rawdb.ReadHeaderNumber(hc.chainDb, hash)
   119  	if number != nil {
   120  		hc.numberCache.Add(hash, *number)
   121  	}
   122  	return number
   123  }
   124  
   125  // WriteHeader writes a header into the local chain, given that its parent is
   126  // already known. If the total difficulty of the newly inserted header becomes
   127  // greater than the current known TD, the canonical chain is re-routed.
   128  //
   129  // Note: This method is not concurrent-safe with inserting blocks simultaneously
   130  // into the chain, as side effects caused by reorganisations cannot be emulated
   131  // without the real blocks. Hence, writing headers directly should only be done
   132  // in two scenarios: pure-header mode of operation (light clients), or properly
   133  // separated header/block phases (non-archive clients).
   134  func (hc *HeaderChain) WriteHeader(header *types.Header) (status WriteStatus, err error) {
   135  	// Cache some values to prevent constant recalculation
   136  	var (
   137  		hash   = header.Hash()
   138  		number = header.Number.Uint64()
   139  	)
   140  	// Calculate the total difficulty of the header
   141  	ptd := hc.GetTd(header.ParentHash, number-1)
   142  	if ptd == nil {
   143  		return NonStatTy, consensus.ErrUnknownAncestor
   144  	}
   145  	curHeader := hc.CurrentHeader()
   146  	localTd := hc.GetTd(hc.currentHeaderHash, curHeader.Number.Uint64())
   147  	externTd := new(big.Int).Add(header.Difficulty, ptd)
   148  
   149  	// Irrelevant of the canonical status, write the td and header to the database
   150  	if err := hc.WriteTd(hash, number, externTd); err != nil {
   151  		log.Crit("Failed to write header total difficulty", "err", err)
   152  	}
   153  
   154  	// Not insert header in irreversible
   155  	if curHeader.Number.Uint64() > header.Number.Uint64() {
   156  		return NonStatTy, fmt.Errorf("header height is irreversible")
   157  	}
   158  
   159  	rawdb.WriteHeader(hc.chainDb, header)
   160  
   161  	headerIsCanon := func(header *types.Header) bool {
   162  		return header.Time.Cmp(curHeader.Time) < 0
   163  	}
   164  	if externTd.Cmp(localTd) > 0 || headerIsCanon(header) {
   165  		// Delete any canonical number assignments above the new head
   166  		for i := number + 1; ; i++ {
   167  			hash := rawdb.ReadCanonicalHash(hc.chainDb, i)
   168  			if hash == (common.Hash{}) {
   169  				break
   170  			}
   171  			rawdb.DeleteCanonicalHash(hc.chainDb, i)
   172  		}
   173  		// Overwrite any stale canonical number assignments
   174  		var (
   175  			headHash   = header.ParentHash
   176  			headNumber = header.Number.Uint64() - 1
   177  			headHeader = hc.GetHeader(headHash, headNumber)
   178  		)
   179  		for rawdb.ReadCanonicalHash(hc.chainDb, headNumber) != headHash {
   180  			rawdb.WriteCanonicalHash(hc.chainDb, headHash, headNumber)
   181  
   182  			headHash = headHeader.ParentHash
   183  			headNumber = headHeader.Number.Uint64() - 1
   184  			headHeader = hc.GetHeader(headHash, headNumber)
   185  		}
   186  		// Extend the canonical chain with the new header
   187  		rawdb.WriteCanonicalHash(hc.chainDb, hash, number)
   188  		rawdb.WriteHeadHeaderHash(hc.chainDb, hash)
   189  
   190  		hc.currentHeaderHash = hash
   191  		hc.currentHeader.Store(types.CopyHeader(header))
   192  
   193  		status = CanonStatTy
   194  	} else {
   195  		status = SideStatTy
   196  	}
   197  
   198  	hc.headerCache.Add(hash, header)
   199  	hc.numberCache.Add(hash, number)
   200  
   201  	return
   202  }
   203  
   204  // WhCallback is a callback function for inserting individual headers.
   205  // A callback is used for two reasons: first, in a LightChain, status should be
   206  // processed and light chain events sent, while in a BlockChain this is not
   207  // necessary since chain events are sent after inserting blocks. Second, the
   208  // header writes should be protected by the parent chain mutex individually.
   209  type WhCallback func(*types.Header) error
   210  
   211  func (hc *HeaderChain) ValidateHeaderChain(chain []*types.Header, checkFreq int) (int, error) {
   212  	// Do a sanity check that the provided chain is actually ordered and linked
   213  	for i := 1; i < len(chain); i++ {
   214  		if chain[i].Number.Uint64() != chain[i-1].Number.Uint64()+1 || chain[i].ParentHash != chain[i-1].Hash() {
   215  			// Chain broke ancestry, log a messge (programming error) and skip insertion
   216  			log.Error("Non contiguous header insert", "number", chain[i].Number, "hash", chain[i].Hash(),
   217  				"parent", chain[i].ParentHash, "prevnumber", chain[i-1].Number, "prevhash", chain[i-1].Hash())
   218  
   219  			return 0, fmt.Errorf("non contiguous insert: item %d is #%d [%x…], item %d is #%d [%x…] (parent [%x…])", i-1, chain[i-1].Number,
   220  				chain[i-1].Hash().Bytes()[:4], i, chain[i].Number, chain[i].Hash().Bytes()[:4], chain[i].ParentHash[:4])
   221  		}
   222  	}
   223  
   224  	// Generate the list of seal verification requests, and start the parallel verifier
   225  	seals := make([]bool, len(chain))
   226  	for i := 0; i < len(seals)/checkFreq; i++ {
   227  		index := i*checkFreq + hc.rand.Intn(checkFreq)
   228  		if index >= len(seals) {
   229  			index = len(seals) - 1
   230  		}
   231  		seals[index] = true
   232  	}
   233  	seals[len(seals)-1] = true // Last should always be verified to avoid junk
   234  
   235  	abort, results := hc.engine.VerifyHeaders(hc, chain, seals)
   236  	defer close(abort)
   237  
   238  	// Iterate over the headers and ensure they all check out
   239  	for i, header := range chain {
   240  		// If the chain is terminating, stop processing blocks
   241  		if hc.procInterrupt() {
   242  			log.Debug("Premature abort during headers verification")
   243  			return 0, errors.New("aborted")
   244  		}
   245  		// If the header is a banned one, straight out abort
   246  		if BadHashes[header.Hash()] {
   247  			return i, ErrBlacklistedHash
   248  		}
   249  		// Otherwise wait for headers checks and ensure they pass
   250  		if err := <-results; err != nil {
   251  			return i, err
   252  		}
   253  	}
   254  
   255  	return 0, nil
   256  }
   257  
   258  // InsertHeaderChain attempts to insert the given header chain in to the local
   259  // chain, possibly creating a reorg. If an error is returned, it will return the
   260  // index number of the failing header as well an error describing what went wrong.
   261  //
   262  // The verify parameter can be used to fine tune whether nonce verification
   263  // should be done or not. The reason behind the optional check is because some
   264  // of the header retrieval mechanisms already need to verfy nonces, as well as
   265  // because nonces can be verified sparsely, not needing to check each.
   266  func (hc *HeaderChain) InsertHeaderChain(chain []*types.Header, writeHeader WhCallback, start time.Time) (int, error) {
   267  	// Collect some import statistics to report on
   268  	stats := struct{ processed, ignored int }{}
   269  	// All headers passed verification, import them into the database
   270  	for i, header := range chain {
   271  		// Short circuit insertion if shutting down
   272  		if hc.procInterrupt() {
   273  			log.Debug("Premature abort during headers import")
   274  			return i, errors.New("aborted")
   275  		}
   276  		// If the header's already known, skip it, otherwise store
   277  		if hc.HasHeader(header.Hash(), header.Number.Uint64()) {
   278  			stats.ignored++
   279  			continue
   280  		}
   281  		if err := writeHeader(header); err != nil {
   282  			return i, err
   283  		}
   284  		stats.processed++
   285  	}
   286  	// Report some public statistics so the user has a clue what's going on
   287  	last := chain[len(chain)-1]
   288  	log.Info("Imported new block headers", "count", stats.processed, "elapsed", common.PrettyDuration(time.Since(start)),
   289  		"number", last.Number, "hash", last.Hash(), "ignored", stats.ignored)
   290  
   291  	return 0, nil
   292  }
   293  
   294  // GetBlockHashesFromHash retrieves a number of block hashes starting at a given
   295  // hash, fetching towards the genesis block.
   296  func (hc *HeaderChain) GetBlockHashesFromHash(hash common.Hash, max uint64) []common.Hash {
   297  	// Get the origin header from which to fetch
   298  	header := hc.GetHeaderByHash(hash)
   299  	if header == nil {
   300  		return nil
   301  	}
   302  	// Iterate the headers until enough is collected or the genesis reached
   303  	chain := make([]common.Hash, 0, max)
   304  	for i := uint64(0); i < max; i++ {
   305  		next := header.ParentHash
   306  		if header = hc.GetHeader(next, header.Number.Uint64()-1); header == nil {
   307  			break
   308  		}
   309  		chain = append(chain, next)
   310  		if header.Number.Sign() == 0 {
   311  			break
   312  		}
   313  	}
   314  	return chain
   315  }
   316  
   317  // GetAncestor retrieves the Nth ancestor of a given block. It assumes that either the given block or
   318  // a close ancestor of it is canonical. maxNonCanonical points to a downwards counter limiting the
   319  // number of blocks to be individually checked before we reach the canonical chain.
   320  //
   321  // Note: ancestor == 0 returns the same block, 1 returns its parent and so on.
   322  func (hc *HeaderChain) GetAncestor(hash common.Hash, number, ancestor uint64, maxNonCanonical *uint64) (common.Hash, uint64) {
   323  	if ancestor > number {
   324  		return common.Hash{}, 0
   325  	}
   326  	if ancestor == 1 {
   327  		// in this case it is cheaper to just read the header
   328  		if header := hc.GetHeader(hash, number); header != nil {
   329  			return header.ParentHash, number - 1
   330  		} else {
   331  			return common.Hash{}, 0
   332  		}
   333  	}
   334  	for ancestor != 0 {
   335  		if rawdb.ReadCanonicalHash(hc.chainDb, number) == hash {
   336  			number -= ancestor
   337  			return rawdb.ReadCanonicalHash(hc.chainDb, number), number
   338  		}
   339  		if *maxNonCanonical == 0 {
   340  			return common.Hash{}, 0
   341  		}
   342  		*maxNonCanonical--
   343  		ancestor--
   344  		header := hc.GetHeader(hash, number)
   345  		if header == nil {
   346  			return common.Hash{}, 0
   347  		}
   348  		hash = header.ParentHash
   349  		number--
   350  	}
   351  	return hash, number
   352  }
   353  
   354  // GetTd retrieves a block's total difficulty in the canonical chain from the
   355  // database by hash and number, caching it if found.
   356  func (hc *HeaderChain) GetTd(hash common.Hash, number uint64) *big.Int {
   357  	// Short circuit if the td's already in the cache, retrieve otherwise
   358  	if cached, ok := hc.tdCache.Get(hash); ok {
   359  		return cached.(*big.Int)
   360  	}
   361  	td := rawdb.ReadTd(hc.chainDb, hash, number)
   362  	if td == nil {
   363  		return nil
   364  	}
   365  	// Cache the found body for next time and return
   366  	hc.tdCache.Add(hash, td)
   367  	return td
   368  }
   369  
   370  // GetTdByHash retrieves a block's total difficulty in the canonical chain from the
   371  // database by hash, caching it if found.
   372  func (hc *HeaderChain) GetTdByHash(hash common.Hash) *big.Int {
   373  	number := hc.GetBlockNumber(hash)
   374  	if number == nil {
   375  		return nil
   376  	}
   377  	return hc.GetTd(hash, *number)
   378  }
   379  
   380  // WriteTd stores a block's total difficulty into the database, also caching it
   381  // along the way.
   382  func (hc *HeaderChain) WriteTd(hash common.Hash, number uint64, td *big.Int) error {
   383  	rawdb.WriteTd(hc.chainDb, hash, number, td)
   384  	hc.tdCache.Add(hash, new(big.Int).Set(td))
   385  	return nil
   386  }
   387  
   388  // GetHeader retrieves a block header from the database by hash and number,
   389  // caching it if found.
   390  func (hc *HeaderChain) GetHeader(hash common.Hash, number uint64) *types.Header {
   391  	// Short circuit if the header's already in the cache, retrieve otherwise
   392  	if header, ok := hc.headerCache.Get(hash); ok {
   393  		return header.(*types.Header)
   394  	}
   395  	header := rawdb.ReadHeader(hc.chainDb, hash, number)
   396  	if header == nil {
   397  		return nil
   398  	}
   399  	// Cache the found header for next time and return
   400  	hc.headerCache.Add(hash, header)
   401  	return header
   402  }
   403  
   404  // GetHeaderByHash retrieves a block header from the database by hash, caching it if
   405  // found.
   406  func (hc *HeaderChain) GetHeaderByHash(hash common.Hash) *types.Header {
   407  	number := hc.GetBlockNumber(hash)
   408  	if number == nil {
   409  		return nil
   410  	}
   411  	return hc.GetHeader(hash, *number)
   412  }
   413  
   414  // HasHeader checks if a block header is present in the database or not.
   415  func (hc *HeaderChain) HasHeader(hash common.Hash, number uint64) bool {
   416  	if hc.numberCache.Contains(hash) || hc.headerCache.Contains(hash) {
   417  		return true
   418  	}
   419  	return rawdb.HasHeader(hc.chainDb, hash, number)
   420  }
   421  
   422  // GetHeaderByNumber retrieves a block header from the database by number,
   423  // caching it (associated with its hash) if found.
   424  func (hc *HeaderChain) GetHeaderByNumber(number uint64) *types.Header {
   425  	hash := rawdb.ReadCanonicalHash(hc.chainDb, number)
   426  	if hash == (common.Hash{}) {
   427  		return nil
   428  	}
   429  	return hc.GetHeader(hash, number)
   430  }
   431  
   432  // CurrentHeader retrieves the current head header of the canonical chain. The
   433  // header is retrieved from the HeaderChain's internal cache.
   434  func (hc *HeaderChain) CurrentHeader() *types.Header {
   435  	return hc.currentHeader.Load().(*types.Header)
   436  }
   437  
   438  // SetCurrentHeader sets the current head header of the canonical chain.
   439  func (hc *HeaderChain) SetCurrentHeader(head *types.Header) {
   440  	rawdb.WriteHeadHeaderHash(hc.chainDb, head.Hash())
   441  
   442  	hc.currentHeader.Store(head)
   443  	hc.currentHeaderHash = head.Hash()
   444  }
   445  
   446  // DeleteCallback is a callback function that is called by SetHead before
   447  // each header is deleted.
   448  type DeleteCallback func(common.Hash, uint64)
   449  
   450  // SetHead rewinds the local chain to a new head. Everything above the new head
   451  // will be deleted and the new one set.
   452  func (hc *HeaderChain) SetHead(head uint64, delFn DeleteCallback) {
   453  	height := uint64(0)
   454  
   455  	if hdr := hc.CurrentHeader(); hdr != nil {
   456  		height = hdr.Number.Uint64()
   457  	}
   458  
   459  	for hdr := hc.CurrentHeader(); hdr != nil && hdr.Number.Uint64() > head; hdr = hc.CurrentHeader() {
   460  		hash := hdr.Hash()
   461  		num := hdr.Number.Uint64()
   462  		if delFn != nil {
   463  			delFn(hash, num)
   464  		}
   465  		rawdb.DeleteHeader(hc.chainDb, hash, num)
   466  		rawdb.DeleteTd(hc.chainDb, hash, num)
   467  
   468  		hc.currentHeader.Store(hc.GetHeader(hdr.ParentHash, hdr.Number.Uint64()-1))
   469  	}
   470  	// Roll back the canonical chain numbering
   471  	for i := height; i > head; i-- {
   472  		rawdb.DeleteCanonicalHash(hc.chainDb, i)
   473  	}
   474  	// Clear out any stale content from the caches
   475  	hc.headerCache.Purge()
   476  	hc.tdCache.Purge()
   477  	hc.numberCache.Purge()
   478  
   479  	if hc.CurrentHeader() == nil {
   480  		hc.currentHeader.Store(hc.genesisHeader)
   481  	}
   482  	hc.currentHeaderHash = hc.CurrentHeader().Hash()
   483  
   484  	rawdb.WriteHeadHeaderHash(hc.chainDb, hc.currentHeaderHash)
   485  }
   486  
   487  // SetGenesis sets a new genesis block header for the chain
   488  func (hc *HeaderChain) SetGenesis(head *types.Header) {
   489  	hc.genesisHeader = head
   490  }
   491  
   492  // Config retrieves the header chain's chain configuration.
   493  func (hc *HeaderChain) Config() *params.ChainConfig { return hc.config }
   494  
   495  // Engine retrieves the header chain's consensus engine.
   496  func (hc *HeaderChain) Engine() consensus.Engine { return hc.engine }
   497  
   498  // GetBlock implements consensus.ChainReader, and returns nil for every input as
   499  // a header chain does not have blocks available for retrieval.
   500  func (hc *HeaderChain) GetBlock(hash common.Hash, number uint64) *types.Block {
   501  	return nil
   502  }