github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/core/headerchain.go (about)

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