github.com/m3shine/gochain@v2.2.26+incompatible/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  	"context"
    21  	crand "crypto/rand"
    22  	"errors"
    23  	"fmt"
    24  	"math"
    25  	"math/big"
    26  	mrand "math/rand"
    27  	"sync/atomic"
    28  	"time"
    29  
    30  	"github.com/gochain-io/gochain/common"
    31  	"github.com/gochain-io/gochain/consensus"
    32  	"github.com/gochain-io/gochain/core/rawdb"
    33  	"github.com/gochain-io/gochain/core/types"
    34  	"github.com/gochain-io/gochain/log"
    35  	"github.com/gochain-io/gochain/params"
    36  	"github.com/hashicorp/golang-lru"
    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       common.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 common.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.GlobalTable()); 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.GlobalTable(), 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  	currentHeader := hc.CurrentHeader()
   146  	localTd := hc.GetTd(hc.currentHeaderHash, currentHeader.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  	hc.WriteTd(hash, number, externTd)
   151  	rawdb.WriteHeader(hc.chainDb.GlobalTable(), hc.chainDb.HeaderTable(), header)
   152  	local := chainHead{localTd, currentHeader.Number.Uint64(), currentHeader.GasUsed}
   153  	external := chainHead{externTd, header.Number.Uint64(), header.GasUsed}
   154  	if reorg(local, external) {
   155  		significant := header.ParentHash != hc.currentHeaderHash
   156  		if significant {
   157  			log.Info("Reorganizing header chain", "oldnum", currentHeader.Number, "newnum", header.Number, "oldtd", localTd, "newtd", externTd, "oldhash", hc.currentHeaderHash, "newhash", hash, "newparent", header.ParentHash)
   158  		}
   159  		start := time.Now()
   160  
   161  		// Delete any canonical number assignments above the new head
   162  		for i := number + 1; ; i++ {
   163  			hash := rawdb.ReadCanonicalHash(hc.chainDb, i)
   164  			if hash == (common.Hash{}) {
   165  				break
   166  			}
   167  			rawdb.DeleteCanonicalHash(hc.chainDb, i)
   168  		}
   169  
   170  		// Overwrite any stale canonical number assignments
   171  		var (
   172  			headHash   = header.ParentHash
   173  			headNumber = header.Number.Uint64() - 1
   174  			headHeader = hc.GetHeader(headHash, headNumber)
   175  		)
   176  		for rawdb.ReadCanonicalHash(hc.chainDb, headNumber) != headHash {
   177  			rawdb.WriteCanonicalHash(hc.chainDb, headHash, headNumber)
   178  
   179  			headHash = headHeader.ParentHash
   180  			headNumber = headHeader.Number.Uint64() - 1
   181  			headHeader = hc.GetHeader(headHash, headNumber)
   182  		}
   183  		// Extend the canonical chain with the new header
   184  		rawdb.WriteCanonicalHash(hc.chainDb, hash, number)
   185  		rawdb.WriteHeadHeaderHash(hc.chainDb.GlobalTable(), hash)
   186  		hc.currentHeaderHash = hash
   187  		hc.currentHeader.Store(types.CopyHeader(header))
   188  
   189  		if significant {
   190  			dur := time.Since(start)
   191  			log.Info("Reorganized header chain", "dur", common.PrettyDuration(dur))
   192  		}
   193  
   194  		status = CanonStatTy
   195  	} else {
   196  		status = SideStatTy
   197  	}
   198  
   199  	hc.headerCache.Add(hash, header)
   200  	hc.numberCache.Add(hash, number)
   201  
   202  	return
   203  }
   204  
   205  // WhCallback is a callback function for inserting individual headers.
   206  // A callback is used for two reasons: first, in a LightChain, status should be
   207  // processed and light chain events sent, while in a BlockChain this is not
   208  // necessary since chain events are sent after inserting blocks. Second, the
   209  // header writes should be protected by the parent chain mutex individually.
   210  type WhCallback func(*types.Header) error
   211  
   212  func (hc *HeaderChain) ValidateHeaderChain(ctx context.Context, chain []*types.Header, checkFreq int) (int, error) {
   213  	// Do a sanity check that the provided chain is actually ordered and linked
   214  	for i := 1; i < len(chain); i++ {
   215  		if chain[i].Number.Uint64() != chain[i-1].Number.Uint64()+1 || chain[i].ParentHash != chain[i-1].Hash() {
   216  			// Chain broke ancestry, log a message (programming error) and skip insertion
   217  			log.Error("Non contiguous header insert", "number", chain[i].Number, "hash", chain[i].Hash(),
   218  				"parent", chain[i].ParentHash, "prevnumber", chain[i-1].Number, "prevhash", chain[i-1].Hash())
   219  
   220  			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,
   221  				chain[i-1].Hash().Bytes()[:4], i, chain[i].Number, chain[i].Hash().Bytes()[:4], chain[i].ParentHash[:4])
   222  		}
   223  	}
   224  
   225  	// Generate the list of seal verification requests, and start the parallel verifier
   226  	seals := make([]bool, len(chain))
   227  	for i := 0; i < len(seals)/checkFreq; i++ {
   228  		index := i*checkFreq + hc.rand.Intn(checkFreq)
   229  		if index >= len(seals) {
   230  			index = len(seals) - 1
   231  		}
   232  		seals[index] = true
   233  	}
   234  	seals[len(seals)-1] = true // Last should always be verified to avoid junk
   235  
   236  	abort, results := hc.engine.VerifyHeaders(ctx, hc, chain)
   237  	defer close(abort)
   238  
   239  	// Iterate over the headers and ensure they all check out
   240  	for i, header := range chain {
   241  		// If the chain is terminating, stop processing blocks
   242  		if hc.procInterrupt() {
   243  			log.Debug("Premature abort during headers verification")
   244  			return 0, errors.New("aborted")
   245  		}
   246  		// If the header is a banned one, straight out abort
   247  		if BadHashes[header.Hash()] {
   248  			return i, ErrBlacklistedHash
   249  		}
   250  		// Otherwise wait for headers checks and ensure they pass
   251  		if err := <-results; err != nil {
   252  			return i, err
   253  		}
   254  	}
   255  
   256  	return 0, nil
   257  }
   258  
   259  // InsertHeaderChain attempts to insert the given header chain in to the local
   260  // chain, possibly creating a reorg. If an error is returned, it will return the
   261  // index number of the failing header as well an error describing what went wrong.
   262  //
   263  // The verify parameter can be used to fine tune whether nonce verification
   264  // should be done or not. The reason behind the optional check is because some
   265  // of the header retrieval mechanisms already need to verfy nonces, as well as
   266  // because nonces can be verified sparsely, not needing to check each.
   267  func (hc *HeaderChain) InsertHeaderChain(chain []*types.Header, writeHeader WhCallback, start time.Time) (int, error) {
   268  	// Collect some import statistics to report on
   269  	stats := struct{ processed, ignored int }{}
   270  	// All headers passed verification, import them into the database
   271  	for i, header := range chain {
   272  		// Short circuit insertion if shutting down
   273  		if hc.procInterrupt() {
   274  			log.Debug("Premature abort during headers import")
   275  			return i, errors.New("aborted")
   276  		}
   277  		// If the header's already known, skip it, otherwise store
   278  		if hc.HasHeader(header.Hash(), header.Number.Uint64()) {
   279  			stats.ignored++
   280  			continue
   281  		}
   282  		if err := writeHeader(header); err != nil {
   283  			return i, err
   284  		}
   285  		stats.processed++
   286  	}
   287  	// Report some public statistics so the user has a clue what's going on
   288  	last := chain[len(chain)-1]
   289  	log.Info("Imported new block headers", "count", stats.processed, "elapsed", common.PrettyDuration(time.Since(start)),
   290  		"number", last.Number, "hash", last.Hash(), "ignored", stats.ignored)
   291  
   292  	return 0, nil
   293  }
   294  
   295  // GetBlockHashesFromHash retrieves a number of block hashes starting at a given
   296  // hash, fetching towards the genesis block.
   297  func (hc *HeaderChain) GetBlockHashesFromHash(hash common.Hash, max uint64) []common.Hash {
   298  	// Get the origin header from which to fetch
   299  	header := hc.GetHeaderByHash(hash)
   300  	if header == nil {
   301  		return nil
   302  	}
   303  	// Iterate the headers until enough is collected or the genesis reached
   304  	chain := make([]common.Hash, 0, max)
   305  	for i := uint64(0); i < max; i++ {
   306  		next := header.ParentHash
   307  		if header = hc.GetHeader(next, header.Number.Uint64()-1); header == nil {
   308  			break
   309  		}
   310  		chain = append(chain, next)
   311  		if header.Number.Sign() == 0 {
   312  			break
   313  		}
   314  	}
   315  	return chain
   316  }
   317  
   318  // GetAncestor retrieves the Nth ancestor of a given block. It assumes that either the given block or
   319  // a close ancestor of it is canonical. maxNonCanonical points to a downwards counter limiting the
   320  // number of blocks to be individually checked before we reach the canonical chain.
   321  //
   322  // Note: ancestor == 0 returns the same block, 1 returns its parent and so on.
   323  func (hc *HeaderChain) GetAncestor(hash common.Hash, number, ancestor uint64, maxNonCanonical *uint64) (common.Hash, uint64) {
   324  	if ancestor > number {
   325  		return common.Hash{}, 0
   326  	}
   327  	if ancestor == 1 {
   328  		// in this case it is cheaper to just read the header
   329  		if header := hc.GetHeader(hash, number); header != nil {
   330  			return header.ParentHash, number - 1
   331  		} else {
   332  			return common.Hash{}, 0
   333  		}
   334  	}
   335  	for ancestor != 0 {
   336  		if rawdb.ReadCanonicalHash(hc.chainDb, number) == hash {
   337  			number -= ancestor
   338  			return rawdb.ReadCanonicalHash(hc.chainDb, number), number
   339  		}
   340  		if *maxNonCanonical == 0 {
   341  			return common.Hash{}, 0
   342  		}
   343  		*maxNonCanonical--
   344  		ancestor--
   345  		header := hc.GetHeader(hash, number)
   346  		if header == nil {
   347  			return common.Hash{}, 0
   348  		}
   349  		hash = header.ParentHash
   350  		number--
   351  	}
   352  	return hash, number
   353  }
   354  
   355  // GetTd retrieves a block's total difficulty in the canonical chain from the
   356  // database by hash and number, caching it if found.
   357  func (hc *HeaderChain) GetTd(hash common.Hash, number uint64) *big.Int {
   358  	// Short circuit if the td's already in the cache, retrieve otherwise
   359  	if cached, ok := hc.tdCache.Get(hash); ok {
   360  		return cached.(*big.Int)
   361  	}
   362  	td := rawdb.ReadTd(hc.chainDb.GlobalTable(), hash, number)
   363  	if td == nil {
   364  		return nil
   365  	}
   366  	// Cache the found body for next time and return
   367  	hc.tdCache.Add(hash, td)
   368  	return td
   369  }
   370  
   371  // GetTdByHash retrieves a block's total difficulty in the canonical chain from the
   372  // database by hash, caching it if found.
   373  func (hc *HeaderChain) GetTdByHash(hash common.Hash) *big.Int {
   374  	number := hc.GetBlockNumber(hash)
   375  	if number == nil {
   376  		return nil
   377  	}
   378  	return hc.GetTd(hash, *number)
   379  }
   380  
   381  // WriteTd stores a block's total difficulty into the database, also caching it
   382  // along the way.
   383  func (hc *HeaderChain) WriteTd(hash common.Hash, number uint64, td *big.Int) {
   384  	rawdb.WriteTd(hc.chainDb.GlobalTable(), hash, number, td)
   385  	hc.tdCache.Add(hash, new(big.Int).Set(td))
   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.HeaderTable(), 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.HeaderTable(), 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.GlobalTable(), head.Hash())
   441  	hc.currentHeader.Store(head)
   442  	hc.currentHeaderHash = head.Hash()
   443  }
   444  
   445  // DeleteCallback is a callback function that is called by SetHead before
   446  // each header is deleted.
   447  type DeleteCallback func(rawdb.DatabaseDeleter, common.Hash, uint64)
   448  
   449  // SetHead rewinds the local chain to a new head. Everything above the new head
   450  // will be deleted and the new one set.
   451  func (hc *HeaderChain) SetHead(head uint64, delFn DeleteCallback) {
   452  	height := uint64(0)
   453  
   454  	hdr := hc.CurrentHeader()
   455  	if hdr != nil {
   456  		height = hdr.Number.Uint64()
   457  	}
   458  	hbatch := hc.chainDb.HeaderTable().NewBatch()
   459  	bbatch := hc.chainDb.BodyTable().NewBatch()
   460  	gbatch := hc.chainDb.GlobalTable().NewBatch()
   461  	for hdr != nil && hdr.Number.Uint64() > head {
   462  		hash := hdr.Hash()
   463  		num := hdr.Number.Uint64()
   464  		if delFn != nil {
   465  			delFn(bbatch, hash, num)
   466  		}
   467  		rawdb.DeleteHeader(hbatch, hash, num)
   468  		rawdb.DeleteTd(gbatch, hash, num)
   469  
   470  		hdr = hc.GetHeader(hdr.ParentHash, hdr.Number.Uint64()-1)
   471  		hc.currentHeader.Store(hdr)
   472  	}
   473  	// Roll back the canonical chain numbering
   474  	for i := height; i > head; i-- {
   475  		rawdb.DeleteCanonicalHash(hc.chainDb, i)
   476  	}
   477  	rawdb.Must("write header batch", hbatch.Write)
   478  	rawdb.Must("write body batch", bbatch.Write)
   479  	rawdb.Must("write global batch", gbatch.Write)
   480  
   481  	// Clear out any stale content from the caches
   482  	hc.headerCache.Purge()
   483  	hc.tdCache.Purge()
   484  	hc.numberCache.Purge()
   485  
   486  	if hdr == nil {
   487  		hdr = hc.genesisHeader
   488  		hc.currentHeader.Store(hdr)
   489  	}
   490  	hc.currentHeaderHash = hdr.Hash()
   491  
   492  	rawdb.WriteHeadHeaderHash(hc.chainDb.GlobalTable(), hc.currentHeaderHash)
   493  }
   494  
   495  // SetGenesis sets a new genesis block header for the chain
   496  func (hc *HeaderChain) SetGenesis(head *types.Header) {
   497  	hc.genesisHeader = head
   498  }
   499  
   500  // Config retrieves the header chain's chain configuration.
   501  func (hc *HeaderChain) Config() *params.ChainConfig { return hc.config }
   502  
   503  // Engine retrieves the header chain's consensus engine.
   504  func (hc *HeaderChain) Engine() consensus.Engine { return hc.engine }
   505  
   506  // GetBlock implements consensus.ChainReader, and returns nil for every input as
   507  // a header chain does not have blocks available for retrieval.
   508  func (hc *HeaderChain) GetBlock(hash common.Hash, number uint64) *types.Block {
   509  	return nil
   510  }