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