github.com/edxfund/validator@v1.8.16-0.20181020093046-c1def72855da/light/lightchain.go (about)

     1  // Copyright 2016 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 light
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  	"math/big"
    23  	"sync"
    24  	"sync/atomic"
    25  	"time"
    26  
    27  	"github.com/EDXFund/Validator/common"
    28  	"github.com/EDXFund/Validator/consensus"
    29  	"github.com/EDXFund/Validator/core"
    30  	"github.com/EDXFund/Validator/core/rawdb"
    31  	"github.com/EDXFund/Validator/core/state"
    32  	"github.com/EDXFund/Validator/core/types"
    33  	"github.com/EDXFund/Validator/ethdb"
    34  	"github.com/EDXFund/Validator/event"
    35  	"github.com/EDXFund/Validator/log"
    36  	"github.com/EDXFund/Validator/params"
    37  	"github.com/EDXFund/Validator/rlp"
    38  	"github.com/hashicorp/golang-lru"
    39  )
    40  
    41  var (
    42  	bodyCacheLimit  = 256
    43  	blockCacheLimit = 256
    44  )
    45  
    46  // LightChain represents a canonical chain that by default only handles block
    47  // headers, downloading block bodies and receipts on demand through an ODR
    48  // interface. It only does header validation during chain insertion.
    49  type LightChain struct {
    50  	hc            *core.HeaderChain
    51  	indexerConfig *IndexerConfig
    52  	chainDb       ethdb.Database
    53  	odr           OdrBackend
    54  	chainFeed     event.Feed
    55  	chainSideFeed event.Feed
    56  	chainHeadFeed event.Feed
    57  	scope         event.SubscriptionScope
    58  	genesisBlock  *types.Block
    59  
    60  	mu      sync.RWMutex
    61  	chainmu sync.RWMutex
    62  
    63  	bodyCache    *lru.Cache // Cache for the most recent block bodies
    64  	bodyRLPCache *lru.Cache // Cache for the most recent block bodies in RLP encoded format
    65  	blockCache   *lru.Cache // Cache for the most recent entire blocks
    66  
    67  	quit    chan struct{}
    68  	running int32 // running must be called automically
    69  	// procInterrupt must be atomically called
    70  	procInterrupt int32 // interrupt signaler for block processing
    71  	wg            sync.WaitGroup
    72  
    73  	shardId uint16
    74  	engine  consensus.Engine
    75  }
    76  
    77  // NewLightChain returns a fully initialised light chain using information
    78  // available in the database. It initialises the default Ethereum header
    79  // validator.
    80  func NewLightChain(odr OdrBackend, config *params.ChainConfig, engine consensus.Engine, shardId uint16) (*LightChain, error) {
    81  	bodyCache, _ := lru.New(bodyCacheLimit)
    82  	bodyRLPCache, _ := lru.New(bodyCacheLimit)
    83  	blockCache, _ := lru.New(blockCacheLimit)
    84  
    85  	bc := &LightChain{
    86  		chainDb:       odr.Database(),
    87  		indexerConfig: odr.IndexerConfig(),
    88  		odr:           odr,
    89  		quit:          make(chan struct{}),
    90  		bodyCache:     bodyCache,
    91  		bodyRLPCache:  bodyRLPCache,
    92  		blockCache:    blockCache,
    93  		engine:        engine,
    94  		shardId:       shardId,
    95  	}
    96  	var err error
    97  	bc.hc, err = core.NewHeaderChain(odr.Database(), config, bc.engine, shardId, bc.getProcInterrupt)
    98  	if err != nil {
    99  		return nil, err
   100  	}
   101  	bc.genesisBlock, _ = bc.GetBlockByNumber(NoOdr, 0)
   102  	if bc.genesisBlock == nil {
   103  		return nil, core.ErrNoGenesis
   104  	}
   105  	if cp, ok := trustedCheckpoints[bc.genesisBlock.Hash()]; ok {
   106  		bc.addTrustedCheckpoint(cp)
   107  	}
   108  	if err := bc.loadLastState(); err != nil {
   109  		return nil, err
   110  	}
   111  	// Check the current state of the block hashes and make sure that we do not have any of the bad blocks in our chain
   112  	for hash := range core.BadHashes {
   113  		if header := bc.GetHeaderByHash(hash); header != nil {
   114  			log.Error("Found bad hash, rewinding chain", "number", header.Number, "hash", header.ParentHash)
   115  			bc.SetHead(header.Number.Uint64() - 1)
   116  			log.Error("Chain rewind was successful, resuming normal operation")
   117  		}
   118  	}
   119  	return bc, nil
   120  }
   121  func (self *LightChain) ShardId() uint16 { return self.shardId }
   122  
   123  // addTrustedCheckpoint adds a trusted checkpoint to the blockchain
   124  func (self *LightChain) addTrustedCheckpoint(cp TrustedCheckpoint) {
   125  	if self.odr.ChtIndexer() != nil {
   126  		StoreChtRoot(self.chainDb, cp.SectionIdx, cp.SectionHead, cp.CHTRoot)
   127  		self.odr.ChtIndexer().AddCheckpoint(cp.SectionIdx, cp.SectionHead)
   128  	}
   129  	if self.odr.BloomTrieIndexer() != nil {
   130  		StoreBloomTrieRoot(self.chainDb, cp.SectionIdx, cp.SectionHead, cp.BloomRoot)
   131  		self.odr.BloomTrieIndexer().AddCheckpoint(cp.SectionIdx, cp.SectionHead)
   132  	}
   133  	if self.odr.BloomIndexer() != nil {
   134  		self.odr.BloomIndexer().AddCheckpoint(cp.SectionIdx, cp.SectionHead)
   135  	}
   136  	log.Info("Added trusted checkpoint", "chain", cp.name, "block", (cp.SectionIdx+1)*self.indexerConfig.ChtSize-1, "hash", cp.SectionHead)
   137  }
   138  
   139  func (self *LightChain) getProcInterrupt() bool {
   140  	return atomic.LoadInt32(&self.procInterrupt) == 1
   141  }
   142  
   143  // Odr returns the ODR backend of the chain
   144  func (self *LightChain) Odr() OdrBackend {
   145  	return self.odr
   146  }
   147  
   148  // loadLastState loads the last known chain state from the database. This method
   149  // assumes that the chain manager mutex is held.
   150  func (self *LightChain) loadLastState() error {
   151  	if head := rawdb.ReadHeadHeaderHash(self.chainDb, self.shardId); head == (common.Hash{}) {
   152  		// Corrupt or empty database, init from scratch
   153  		self.Reset()
   154  	} else {
   155  		if header := self.GetHeaderByHash(head); header != nil {
   156  			self.hc.SetCurrentHeader(header)
   157  		}
   158  	}
   159  
   160  	// Issue a status log and return
   161  	header := self.hc.CurrentHeader()
   162  	headerTd := self.GetTd(header.Hash(), header.Number.Uint64())
   163  	log.Info("Loaded most recent local header", "number", header.Number, "hash", header.Hash(), "td", headerTd)
   164  
   165  	return nil
   166  }
   167  
   168  // SetHead rewinds the local chain to a new head. Everything above the new
   169  // head will be deleted and the new one set.
   170  func (bc *LightChain) SetHead(head uint64) {
   171  	bc.mu.Lock()
   172  	defer bc.mu.Unlock()
   173  
   174  	bc.hc.SetHead(head, nil)
   175  	bc.loadLastState()
   176  }
   177  
   178  // GasLimit returns the gas limit of the current HEAD block.
   179  func (self *LightChain) GasLimit() uint64 {
   180  	return self.hc.CurrentHeader().GasLimit
   181  }
   182  
   183  // Reset purges the entire blockchain, restoring it to its genesis state.
   184  func (bc *LightChain) Reset() {
   185  	bc.ResetWithGenesisBlock(bc.genesisBlock)
   186  }
   187  
   188  // ResetWithGenesisBlock purges the entire blockchain, restoring it to the
   189  // specified genesis state.
   190  func (bc *LightChain) ResetWithGenesisBlock(genesis *types.Block) {
   191  	// Dump the entire block chain and purge the caches
   192  	bc.SetHead(0)
   193  
   194  	bc.mu.Lock()
   195  	defer bc.mu.Unlock()
   196  
   197  	// Prepare the genesis block and reinitialise the chain
   198  	rawdb.WriteTd(bc.chainDb, genesis.Hash(), bc.shardId, genesis.NumberU64(), genesis.Difficulty())
   199  	rawdb.WriteBlock(bc.chainDb, genesis)
   200  
   201  	bc.genesisBlock = genesis
   202  	bc.hc.SetGenesis(bc.genesisBlock.Header())
   203  	bc.hc.SetCurrentHeader(bc.genesisBlock.Header())
   204  }
   205  
   206  // Accessors
   207  
   208  // Engine retrieves the light chain's consensus engine.
   209  func (bc *LightChain) Engine() consensus.Engine { return bc.engine }
   210  
   211  // Genesis returns the genesis block
   212  func (bc *LightChain) Genesis() *types.Block {
   213  	return bc.genesisBlock
   214  }
   215  
   216  // State returns a new mutable state based on the current HEAD block.
   217  func (bc *LightChain) State() (*state.StateDB, error) {
   218  	return nil, errors.New("not implemented, needs client/server interface split")
   219  }
   220  
   221  // GetBody retrieves a block body (transactions and uncles) from the database
   222  // or ODR service by hash, caching it if found.
   223  func (self *LightChain) GetBody(ctx context.Context, hash common.Hash) (*types.Body, error) {
   224  	// Short circuit if the body's already in the cache, retrieve otherwise
   225  	if cached, ok := self.bodyCache.Get(hash); ok {
   226  		body := cached.(*types.Body)
   227  		return body, nil
   228  	}
   229  	number := self.hc.GetBlockNumber(hash)
   230  	if number == nil {
   231  		return nil, errors.New("unknown block")
   232  	}
   233  	body, err := GetBody(ctx, self.odr, hash, self.shardId,*number)
   234  	if err != nil {
   235  		return nil, err
   236  	}
   237  	// Cache the found body for next time and return
   238  	self.bodyCache.Add(hash, body)
   239  	return body, nil
   240  }
   241  
   242  // GetBodyRLP retrieves a block body in RLP encoding from the database or
   243  // ODR service by hash, caching it if found.
   244  func (self *LightChain) GetBodyRLP(ctx context.Context, hash common.Hash) (rlp.RawValue, error) {
   245  	// Short circuit if the body's already in the cache, retrieve otherwise
   246  	if cached, ok := self.bodyRLPCache.Get(hash); ok {
   247  		return cached.(rlp.RawValue), nil
   248  	}
   249  	number := self.hc.GetBlockNumber(hash)
   250  	if number == nil {
   251  		return nil, errors.New("unknown block")
   252  	}
   253  	body, err := GetBodyRLP(ctx, self.odr, hash, self.shardId,*number)
   254  	if err != nil {
   255  		return nil, err
   256  	}
   257  	// Cache the found body for next time and return
   258  	self.bodyRLPCache.Add(hash, body)
   259  	return body, nil
   260  }
   261  
   262  // HasBlock checks if a block is fully present in the database or not, caching
   263  // it if present.
   264  func (bc *LightChain) HasBlock(hash common.Hash, number uint64) bool {
   265  	blk, _ := bc.GetBlock(NoOdr, hash, number)
   266  	return blk != nil
   267  }
   268  
   269  // GetBlock retrieves a block from the database or ODR service by hash and number,
   270  // caching it if found.
   271  func (self *LightChain) GetBlock(ctx context.Context, hash common.Hash, number uint64) (*types.Block, error) {
   272  	// Short circuit if the block's already in the cache, retrieve otherwise
   273  	if block, ok := self.blockCache.Get(hash); ok {
   274  		return block.(*types.Block), nil
   275  	}
   276  	block, err := GetBlock(ctx, self.odr, hash,self.shardId, number)
   277  	if err != nil {
   278  		return nil, err
   279  	}
   280  	// Cache the found block for next time and return
   281  	self.blockCache.Add(block.Hash(), block)
   282  	return block, nil
   283  }
   284  
   285  // GetBlockByHash retrieves a block from the database or ODR service by hash,
   286  // caching it if found.
   287  func (self *LightChain) GetBlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) {
   288  	number := self.hc.GetBlockNumber(hash)
   289  	if number == nil {
   290  		return nil, errors.New("unknown block")
   291  	}
   292  	return self.GetBlock(ctx, hash, *number)
   293  }
   294  
   295  // GetBlockByNumber retrieves a block from the database or ODR service by
   296  // number, caching it (associated with its hash) if found.
   297  func (self *LightChain) GetBlockByNumber(ctx context.Context, number uint64) (*types.Block, error) {
   298  	hash, err := GetCanonicalHash(ctx, self.odr, self.shardId, number)
   299  	if hash == (common.Hash{}) || err != nil {
   300  		return nil, err
   301  	}
   302  	return self.GetBlock(ctx, hash, number)
   303  }
   304  
   305  // Stop stops the blockchain service. If any imports are currently in progress
   306  // it will abort them using the procInterrupt.
   307  func (bc *LightChain) Stop() {
   308  	if !atomic.CompareAndSwapInt32(&bc.running, 0, 1) {
   309  		return
   310  	}
   311  	close(bc.quit)
   312  	atomic.StoreInt32(&bc.procInterrupt, 1)
   313  
   314  	bc.wg.Wait()
   315  	log.Info("Blockchain manager stopped")
   316  }
   317  
   318  // Rollback is designed to remove a chain of links from the database that aren't
   319  // certain enough to be valid.
   320  func (self *LightChain) Rollback(chain []common.Hash) {
   321  	self.mu.Lock()
   322  	defer self.mu.Unlock()
   323  
   324  	for i := len(chain) - 1; i >= 0; i-- {
   325  		hash := chain[i]
   326  
   327  		if head := self.hc.CurrentHeader(); head.Hash() == hash {
   328  			self.hc.SetCurrentHeader(self.GetHeader(head.ParentHash, head.Number.Uint64()-1))
   329  		}
   330  	}
   331  }
   332  
   333  // postChainEvents iterates over the events generated by a chain insertion and
   334  // posts them into the event feed.
   335  func (self *LightChain) postChainEvents(events []interface{}) {
   336  	for _, event := range events {
   337  		switch ev := event.(type) {
   338  		case core.ChainEvent:
   339  			if self.CurrentHeader().Hash() == ev.Hash {
   340  				self.chainHeadFeed.Send(core.ChainHeadEvent{Block: ev.Block})
   341  			}
   342  			self.chainFeed.Send(ev)
   343  		case core.ChainSideEvent:
   344  			self.chainSideFeed.Send(ev)
   345  		}
   346  	}
   347  }
   348  
   349  // InsertHeaderChain attempts to insert the given header chain in to the local
   350  // chain, possibly creating a reorg. If an error is returned, it will return the
   351  // index number of the failing header as well an error describing what went wrong.
   352  //
   353  // The verify parameter can be used to fine tune whether nonce verification
   354  // should be done or not. The reason behind the optional check is because some
   355  // of the header retrieval mechanisms already need to verfy nonces, as well as
   356  // because nonces can be verified sparsely, not needing to check each.
   357  //
   358  // In the case of a light chain, InsertHeaderChain also creates and posts light
   359  // chain events when necessary.
   360  func (self *LightChain) InsertHeaderChain(chain []*types.Header, checkFreq int) (int, error) {
   361  	start := time.Now()
   362  	if i, err := self.hc.ValidateHeaderChain(chain, checkFreq); err != nil {
   363  		return i, err
   364  	}
   365  
   366  	// Make sure only one thread manipulates the chain at once
   367  	self.chainmu.Lock()
   368  	defer func() {
   369  		self.chainmu.Unlock()
   370  		time.Sleep(time.Millisecond * 10) // ugly hack; do not hog chain lock in case syncing is CPU-limited by validation
   371  	}()
   372  
   373  	self.wg.Add(1)
   374  	defer self.wg.Done()
   375  
   376  	var events []interface{}
   377  	whFunc := func(header *types.Header) error {
   378  		self.mu.Lock()
   379  		defer self.mu.Unlock()
   380  
   381  		status, err := self.hc.WriteHeader(header)
   382  
   383  		switch status {
   384  		case core.CanonStatTy:
   385  			log.Debug("Inserted new header", "number", header.Number, "hash", header.Hash())
   386  			events = append(events, core.ChainEvent{Block: types.NewBlockWithHeader(header), Hash: header.Hash()})
   387  
   388  		case core.SideStatTy:
   389  			log.Debug("Inserted forked header", "number", header.Number, "hash", header.Hash())
   390  			events = append(events, core.ChainSideEvent{Block: types.NewBlockWithHeader(header)})
   391  		}
   392  		return err
   393  	}
   394  	i, err := self.hc.InsertHeaderChain(chain, whFunc, start)
   395  	self.postChainEvents(events)
   396  	return i, err
   397  }
   398  
   399  // CurrentHeader retrieves the current head header of the canonical chain. The
   400  // header is retrieved from the HeaderChain's internal cache.
   401  func (self *LightChain) CurrentHeader() *types.Header {
   402  	return self.hc.CurrentHeader()
   403  }
   404  
   405  // GetTd retrieves a block's total difficulty in the canonical chain from the
   406  // database by hash and number, caching it if found.
   407  func (self *LightChain) GetTd(hash common.Hash, number uint64) *big.Int {
   408  	return self.hc.GetTd(hash, number)
   409  }
   410  
   411  // GetTdByHash retrieves a block's total difficulty in the canonical chain from the
   412  // database by hash, caching it if found.
   413  func (self *LightChain) GetTdByHash(hash common.Hash) *big.Int {
   414  	return self.hc.GetTdByHash(hash)
   415  }
   416  
   417  // GetHeader retrieves a block header from the database by hash and number,
   418  // caching it if found.
   419  func (self *LightChain) GetHeader(hash common.Hash, number uint64) *types.Header {
   420  	return self.hc.GetHeader(hash, number)
   421  }
   422  
   423  // GetHeaderByHash retrieves a block header from the database by hash, caching it if
   424  // found.
   425  func (self *LightChain) GetHeaderByHash(hash common.Hash) *types.Header {
   426  	return self.hc.GetHeaderByHash(hash)
   427  }
   428  
   429  // HasHeader checks if a block header is present in the database or not, caching
   430  // it if present.
   431  func (bc *LightChain) HasHeader(hash common.Hash, number uint64) bool {
   432  	return bc.hc.HasHeader(hash, number)
   433  }
   434  
   435  // GetBlockHashesFromHash retrieves a number of block hashes starting at a given
   436  // hash, fetching towards the genesis block.
   437  func (self *LightChain) GetBlockHashesFromHash(hash common.Hash, max uint64) []common.Hash {
   438  	return self.hc.GetBlockHashesFromHash(hash, max)
   439  }
   440  
   441  // GetAncestor retrieves the Nth ancestor of a given block. It assumes that either the given block or
   442  // a close ancestor of it is canonical. maxNonCanonical points to a downwards counter limiting the
   443  // number of blocks to be individually checked before we reach the canonical chain.
   444  //
   445  // Note: ancestor == 0 returns the same block, 1 returns its parent and so on.
   446  func (bc *LightChain) GetAncestor(hash common.Hash, number, ancestor uint64, maxNonCanonical *uint64) (common.Hash, uint64) {
   447  	bc.chainmu.Lock()
   448  	defer bc.chainmu.Unlock()
   449  
   450  	return bc.hc.GetAncestor(hash, number, ancestor, maxNonCanonical)
   451  }
   452  
   453  // GetHeaderByNumber retrieves a block header from the database by number,
   454  // caching it (associated with its hash) if found.
   455  func (self *LightChain) GetHeaderByNumber(number uint64) *types.Header {
   456  	return self.hc.GetHeaderByNumber(number)
   457  }
   458  
   459  // GetHeaderByNumberOdr retrieves a block header from the database or network
   460  // by number, caching it (associated with its hash) if found.
   461  func (self *LightChain) GetHeaderByNumberOdr(ctx context.Context, number uint64) (*types.Header, error) {
   462  	if header := self.hc.GetHeaderByNumber(number); header != nil {
   463  		return header, nil
   464  	}
   465  	return GetHeaderByNumber(ctx, self.odr, self.shardId,number)
   466  }
   467  
   468  // Config retrieves the header chain's chain configuration.
   469  func (self *LightChain) Config() *params.ChainConfig { return self.hc.Config() }
   470  
   471  func (self *LightChain) SyncCht(ctx context.Context) bool {
   472  	// If we don't have a CHT indexer, abort
   473  	if self.odr.ChtIndexer() == nil {
   474  		return false
   475  	}
   476  	// Ensure the remote CHT head is ahead of us
   477  	head := self.CurrentHeader().Number.Uint64()
   478  	sections, _, _ := self.odr.ChtIndexer().Sections()
   479  
   480  	latest := sections*self.indexerConfig.ChtSize - 1
   481  	if clique := self.hc.Config().Clique; clique != nil {
   482  		latest -= latest % clique.Epoch // epoch snapshot for clique
   483  	}
   484  	if head >= latest {
   485  		return false
   486  	}
   487  	// Retrieve the latest useful header and update to it
   488  	if header, err := GetHeaderByNumber(ctx, self.odr, self.shardId,latest); header != nil && err == nil {
   489  		self.mu.Lock()
   490  		defer self.mu.Unlock()
   491  
   492  		// Ensure the chain didn't move past the latest block while retrieving it
   493  		if self.hc.CurrentHeader().Number.Uint64() < header.Number.Uint64() {
   494  			log.Info("Updated latest header based on CHT", "number", header.Number, "hash", header.Hash())
   495  			self.hc.SetCurrentHeader(header)
   496  		}
   497  		return true
   498  	}
   499  	return false
   500  }
   501  
   502  // LockChain locks the chain mutex for reading so that multiple canonical hashes can be
   503  // retrieved while it is guaranteed that they belong to the same version of the chain
   504  func (self *LightChain) LockChain() {
   505  	self.chainmu.RLock()
   506  }
   507  
   508  // UnlockChain unlocks the chain mutex
   509  func (self *LightChain) UnlockChain() {
   510  	self.chainmu.RUnlock()
   511  }
   512  
   513  // SubscribeChainEvent registers a subscription of ChainEvent.
   514  func (self *LightChain) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
   515  	return self.scope.Track(self.chainFeed.Subscribe(ch))
   516  }
   517  
   518  // SubscribeChainHeadEvent registers a subscription of ChainHeadEvent.
   519  func (self *LightChain) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
   520  	return self.scope.Track(self.chainHeadFeed.Subscribe(ch))
   521  }
   522  
   523  // SubscribeChainSideEvent registers a subscription of ChainSideEvent.
   524  func (self *LightChain) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription {
   525  	return self.scope.Track(self.chainSideFeed.Subscribe(ch))
   526  }
   527  
   528  // SubscribeLogsEvent implements the interface of filters.Backend
   529  // LightChain does not send logs events, so return an empty subscription.
   530  func (self *LightChain) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
   531  	return self.scope.Track(new(event.Feed).Subscribe(ch))
   532  }
   533  
   534  // SubscribeRemovedLogsEvent implements the interface of filters.Backend
   535  // LightChain does not send core.RemovedLogsEvent, so return an empty subscription.
   536  func (self *LightChain) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
   537  	return self.scope.Track(new(event.Feed).Subscribe(ch))
   538  }