github.com/nova-foundation/go-ethereum@v1.0.1/core/blockchain_reader.go (about)

     1  // Copyright 2021 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  	"math/big"
    21  
    22  	"github.com/Nova-foundation/go-ethereum/common"
    23  	"github.com/Nova-foundation/go-ethereum/consensus"
    24  	"github.com/Nova-foundation/go-ethereum/core/rawdb"
    25  	"github.com/Nova-foundation/go-ethereum/core/state"
    26  	"github.com/Nova-foundation/go-ethereum/core/state/snapshot"
    27  	"github.com/Nova-foundation/go-ethereum/core/types"
    28  	"github.com/Nova-foundation/go-ethereum/core/vm"
    29  	"github.com/Nova-foundation/go-ethereum/event"
    30  	"github.com/Nova-foundation/go-ethereum/params"
    31  	"github.com/Nova-foundation/go-ethereum/rlp"
    32  	"github.com/Nova-foundation/go-ethereum/trie"
    33  )
    34  
    35  // CurrentHeader retrieves the current head header of the canonical chain. The
    36  // header is retrieved from the HeaderChain's internal cache.
    37  func (bc *BlockChain) CurrentHeader() *types.Header {
    38  	return bc.hc.CurrentHeader()
    39  }
    40  
    41  // CurrentBlock retrieves the current head block of the canonical chain. The
    42  // block is retrieved from the blockchain's internal cache.
    43  func (bc *BlockChain) CurrentBlock() *types.Header {
    44  	return bc.currentBlock.Load()
    45  }
    46  
    47  // CurrentSnapBlock retrieves the current snap-sync head block of the canonical
    48  // chain. The block is retrieved from the blockchain's internal cache.
    49  func (bc *BlockChain) CurrentSnapBlock() *types.Header {
    50  	return bc.currentSnapBlock.Load()
    51  }
    52  
    53  // CurrentFinalBlock retrieves the current finalized block of the canonical
    54  // chain. The block is retrieved from the blockchain's internal cache.
    55  func (bc *BlockChain) CurrentFinalBlock() *types.Header {
    56  	return bc.currentFinalBlock.Load()
    57  }
    58  
    59  // CurrentSafeBlock retrieves the current safe block of the canonical
    60  // chain. The block is retrieved from the blockchain's internal cache.
    61  func (bc *BlockChain) CurrentSafeBlock() *types.Header {
    62  	return bc.currentSafeBlock.Load()
    63  }
    64  
    65  // HasHeader checks if a block header is present in the database or not, caching
    66  // it if present.
    67  func (bc *BlockChain) HasHeader(hash common.Hash, number uint64) bool {
    68  	return bc.hc.HasHeader(hash, number)
    69  }
    70  
    71  // GetHeader retrieves a block header from the database by hash and number,
    72  // caching it if found.
    73  func (bc *BlockChain) GetHeader(hash common.Hash, number uint64) *types.Header {
    74  	return bc.hc.GetHeader(hash, number)
    75  }
    76  
    77  // GetHeaderByHash retrieves a block header from the database by hash, caching it if
    78  // found.
    79  func (bc *BlockChain) GetHeaderByHash(hash common.Hash) *types.Header {
    80  	return bc.hc.GetHeaderByHash(hash)
    81  }
    82  
    83  // GetHeaderByNumber retrieves a block header from the database by number,
    84  // caching it (associated with its hash) if found.
    85  func (bc *BlockChain) GetHeaderByNumber(number uint64) *types.Header {
    86  	return bc.hc.GetHeaderByNumber(number)
    87  }
    88  
    89  // GetHeadersFrom returns a contiguous segment of headers, in rlp-form, going
    90  // backwards from the given number.
    91  func (bc *BlockChain) GetHeadersFrom(number, count uint64) []rlp.RawValue {
    92  	return bc.hc.GetHeadersFrom(number, count)
    93  }
    94  
    95  // GetBody retrieves a block body (transactions and uncles) from the database by
    96  // hash, caching it if found.
    97  func (bc *BlockChain) GetBody(hash common.Hash) *types.Body {
    98  	// Short circuit if the body's already in the cache, retrieve otherwise
    99  	if cached, ok := bc.bodyCache.Get(hash); ok {
   100  		return cached
   101  	}
   102  	number := bc.hc.GetBlockNumber(hash)
   103  	if number == nil {
   104  		return nil
   105  	}
   106  	body := rawdb.ReadBody(bc.db, hash, *number)
   107  	if body == nil {
   108  		return nil
   109  	}
   110  	// Cache the found body for next time and return
   111  	bc.bodyCache.Add(hash, body)
   112  	return body
   113  }
   114  
   115  // GetBodyRLP retrieves a block body in RLP encoding from the database by hash,
   116  // caching it if found.
   117  func (bc *BlockChain) GetBodyRLP(hash common.Hash) rlp.RawValue {
   118  	// Short circuit if the body's already in the cache, retrieve otherwise
   119  	if cached, ok := bc.bodyRLPCache.Get(hash); ok {
   120  		return cached
   121  	}
   122  	number := bc.hc.GetBlockNumber(hash)
   123  	if number == nil {
   124  		return nil
   125  	}
   126  	body := rawdb.ReadBodyRLP(bc.db, hash, *number)
   127  	if len(body) == 0 {
   128  		return nil
   129  	}
   130  	// Cache the found body for next time and return
   131  	bc.bodyRLPCache.Add(hash, body)
   132  	return body
   133  }
   134  
   135  // HasBlock checks if a block is fully present in the database or not.
   136  func (bc *BlockChain) HasBlock(hash common.Hash, number uint64) bool {
   137  	if bc.blockCache.Contains(hash) {
   138  		return true
   139  	}
   140  	if !bc.HasHeader(hash, number) {
   141  		return false
   142  	}
   143  	return rawdb.HasBody(bc.db, hash, number)
   144  }
   145  
   146  // HasFastBlock checks if a fast block is fully present in the database or not.
   147  func (bc *BlockChain) HasFastBlock(hash common.Hash, number uint64) bool {
   148  	if !bc.HasBlock(hash, number) {
   149  		return false
   150  	}
   151  	if bc.receiptsCache.Contains(hash) {
   152  		return true
   153  	}
   154  	return rawdb.HasReceipts(bc.db, hash, number)
   155  }
   156  
   157  // GetBlock retrieves a block from the database by hash and number,
   158  // caching it if found.
   159  func (bc *BlockChain) GetBlock(hash common.Hash, number uint64) *types.Block {
   160  	// Short circuit if the block's already in the cache, retrieve otherwise
   161  	if block, ok := bc.blockCache.Get(hash); ok {
   162  		return block
   163  	}
   164  	block := rawdb.ReadBlock(bc.db, hash, number)
   165  	if block == nil {
   166  		return nil
   167  	}
   168  	// Cache the found block for next time and return
   169  	bc.blockCache.Add(block.Hash(), block)
   170  	return block
   171  }
   172  
   173  // GetBlockByHash retrieves a block from the database by hash, caching it if found.
   174  func (bc *BlockChain) GetBlockByHash(hash common.Hash) *types.Block {
   175  	number := bc.hc.GetBlockNumber(hash)
   176  	if number == nil {
   177  		return nil
   178  	}
   179  	return bc.GetBlock(hash, *number)
   180  }
   181  
   182  // GetBlockByNumber retrieves a block from the database by number, caching it
   183  // (associated with its hash) if found.
   184  func (bc *BlockChain) GetBlockByNumber(number uint64) *types.Block {
   185  	hash := rawdb.ReadCanonicalHash(bc.db, number)
   186  	if hash == (common.Hash{}) {
   187  		return nil
   188  	}
   189  	return bc.GetBlock(hash, number)
   190  }
   191  
   192  // GetBlocksFromHash returns the block corresponding to hash and up to n-1 ancestors.
   193  // [deprecated by eth/62]
   194  func (bc *BlockChain) GetBlocksFromHash(hash common.Hash, n int) (blocks []*types.Block) {
   195  	number := bc.hc.GetBlockNumber(hash)
   196  	if number == nil {
   197  		return nil
   198  	}
   199  	for i := 0; i < n; i++ {
   200  		block := bc.GetBlock(hash, *number)
   201  		if block == nil {
   202  			break
   203  		}
   204  		blocks = append(blocks, block)
   205  		hash = block.ParentHash()
   206  		*number--
   207  	}
   208  	return
   209  }
   210  
   211  // GetReceiptsByHash retrieves the receipts for all transactions in a given block.
   212  func (bc *BlockChain) GetReceiptsByHash(hash common.Hash) types.Receipts {
   213  	if receipts, ok := bc.receiptsCache.Get(hash); ok {
   214  		return receipts
   215  	}
   216  	number := rawdb.ReadHeaderNumber(bc.db, hash)
   217  	if number == nil {
   218  		return nil
   219  	}
   220  	header := bc.GetHeader(hash, *number)
   221  	if header == nil {
   222  		return nil
   223  	}
   224  	receipts := rawdb.ReadReceipts(bc.db, hash, *number, header.Time, bc.chainConfig)
   225  	if receipts == nil {
   226  		return nil
   227  	}
   228  	bc.receiptsCache.Add(hash, receipts)
   229  	return receipts
   230  }
   231  
   232  // GetUnclesInChain retrieves all the uncles from a given block backwards until
   233  // a specific distance is reached.
   234  func (bc *BlockChain) GetUnclesInChain(block *types.Block, length int) []*types.Header {
   235  	uncles := []*types.Header{}
   236  	for i := 0; block != nil && i < length; i++ {
   237  		uncles = append(uncles, block.Uncles()...)
   238  		block = bc.GetBlock(block.ParentHash(), block.NumberU64()-1)
   239  	}
   240  	return uncles
   241  }
   242  
   243  // GetCanonicalHash returns the canonical hash for a given block number
   244  func (bc *BlockChain) GetCanonicalHash(number uint64) common.Hash {
   245  	return bc.hc.GetCanonicalHash(number)
   246  }
   247  
   248  // GetAncestor retrieves the Nth ancestor of a given block. It assumes that either the given block or
   249  // a close ancestor of it is canonical. maxNonCanonical points to a downwards counter limiting the
   250  // number of blocks to be individually checked before we reach the canonical chain.
   251  //
   252  // Note: ancestor == 0 returns the same block, 1 returns its parent and so on.
   253  func (bc *BlockChain) GetAncestor(hash common.Hash, number, ancestor uint64, maxNonCanonical *uint64) (common.Hash, uint64) {
   254  	return bc.hc.GetAncestor(hash, number, ancestor, maxNonCanonical)
   255  }
   256  
   257  // GetTransactionLookup retrieves the lookup associate with the given transaction
   258  // hash from the cache or database.
   259  func (bc *BlockChain) GetTransactionLookup(hash common.Hash) *rawdb.LegacyTxLookupEntry {
   260  	// Short circuit if the txlookup already in the cache, retrieve otherwise
   261  	if lookup, exist := bc.txLookupCache.Get(hash); exist {
   262  		return lookup
   263  	}
   264  	tx, blockHash, blockNumber, txIndex := rawdb.ReadTransaction(bc.db, hash)
   265  	if tx == nil {
   266  		return nil
   267  	}
   268  	lookup := &rawdb.LegacyTxLookupEntry{BlockHash: blockHash, BlockIndex: blockNumber, Index: txIndex}
   269  	bc.txLookupCache.Add(hash, lookup)
   270  	return lookup
   271  }
   272  
   273  // GetTd retrieves a block's total difficulty in the canonical chain from the
   274  // database by hash and number, caching it if found.
   275  func (bc *BlockChain) GetTd(hash common.Hash, number uint64) *big.Int {
   276  	return bc.hc.GetTd(hash, number)
   277  }
   278  
   279  // HasState checks if state trie is fully present in the database or not.
   280  func (bc *BlockChain) HasState(hash common.Hash) bool {
   281  	_, err := bc.stateCache.OpenTrie(hash)
   282  	return err == nil
   283  }
   284  
   285  // HasBlockAndState checks if a block and associated state trie is fully present
   286  // in the database or not, caching it if present.
   287  func (bc *BlockChain) HasBlockAndState(hash common.Hash, number uint64) bool {
   288  	// Check first that the block itself is known
   289  	block := bc.GetBlock(hash, number)
   290  	if block == nil {
   291  		return false
   292  	}
   293  	return bc.HasState(block.Root())
   294  }
   295  
   296  // TrieNode retrieves a blob of data associated with a trie node
   297  // either from ephemeral in-memory cache, or from persistent storage.
   298  func (bc *BlockChain) TrieNode(hash common.Hash) ([]byte, error) {
   299  	return bc.stateCache.TrieDB().Node(hash)
   300  }
   301  
   302  // ContractCodeWithPrefix retrieves a blob of data associated with a contract
   303  // hash either from ephemeral in-memory cache, or from persistent storage.
   304  //
   305  // If the code doesn't exist in the in-memory cache, check the storage with
   306  // new code scheme.
   307  func (bc *BlockChain) ContractCodeWithPrefix(hash common.Hash) ([]byte, error) {
   308  	type codeReader interface {
   309  		ContractCodeWithPrefix(addrHash, codeHash common.Hash) ([]byte, error)
   310  	}
   311  	return bc.stateCache.(codeReader).ContractCodeWithPrefix(common.Hash{}, hash)
   312  }
   313  
   314  // State returns a new mutable state based on the current HEAD block.
   315  func (bc *BlockChain) State() (*state.StateDB, error) {
   316  	return bc.StateAt(bc.CurrentBlock().Root)
   317  }
   318  
   319  // StateAt returns a new mutable state based on a particular point in time.
   320  func (bc *BlockChain) StateAt(root common.Hash) (*state.StateDB, error) {
   321  	return state.New(root, bc.stateCache, bc.snaps)
   322  }
   323  
   324  // Config retrieves the chain's fork configuration.
   325  func (bc *BlockChain) Config() *params.ChainConfig { return bc.chainConfig }
   326  
   327  // Engine retrieves the blockchain's consensus engine.
   328  func (bc *BlockChain) Engine() consensus.Engine { return bc.engine }
   329  
   330  // Snapshots returns the blockchain snapshot tree.
   331  func (bc *BlockChain) Snapshots() *snapshot.Tree {
   332  	return bc.snaps
   333  }
   334  
   335  // Validator returns the current validator.
   336  func (bc *BlockChain) Validator() Validator {
   337  	return bc.validator
   338  }
   339  
   340  // Processor returns the current processor.
   341  func (bc *BlockChain) Processor() Processor {
   342  	return bc.processor
   343  }
   344  
   345  // StateCache returns the caching database underpinning the blockchain instance.
   346  func (bc *BlockChain) StateCache() state.Database {
   347  	return bc.stateCache
   348  }
   349  
   350  // GasLimit returns the gas limit of the current HEAD block.
   351  func (bc *BlockChain) GasLimit() uint64 {
   352  	return bc.CurrentBlock().GasLimit
   353  }
   354  
   355  // Genesis retrieves the chain's genesis block.
   356  func (bc *BlockChain) Genesis() *types.Block {
   357  	return bc.genesisBlock
   358  }
   359  
   360  // GetVMConfig returns the block chain VM config.
   361  func (bc *BlockChain) GetVMConfig() *vm.Config {
   362  	return &bc.vmConfig
   363  }
   364  
   365  // SetTxLookupLimit is responsible for updating the txlookup limit to the
   366  // original one stored in db if the new mismatches with the old one.
   367  func (bc *BlockChain) SetTxLookupLimit(limit uint64) {
   368  	bc.txLookupLimit = limit
   369  }
   370  
   371  // TxLookupLimit retrieves the txlookup limit used by blockchain to prune
   372  // stale transaction indices.
   373  func (bc *BlockChain) TxLookupLimit() uint64 {
   374  	return bc.txLookupLimit
   375  }
   376  
   377  // TrieDB retrieves the low level trie database used for data storage.
   378  func (bc *BlockChain) TrieDB() *trie.Database {
   379  	return bc.triedb
   380  }
   381  
   382  // SubscribeRemovedLogsEvent registers a subscription of RemovedLogsEvent.
   383  func (bc *BlockChain) SubscribeRemovedLogsEvent(ch chan<- RemovedLogsEvent) event.Subscription {
   384  	return bc.scope.Track(bc.rmLogsFeed.Subscribe(ch))
   385  }
   386  
   387  // SubscribeChainEvent registers a subscription of ChainEvent.
   388  func (bc *BlockChain) SubscribeChainEvent(ch chan<- ChainEvent) event.Subscription {
   389  	return bc.scope.Track(bc.chainFeed.Subscribe(ch))
   390  }
   391  
   392  // SubscribeChainHeadEvent registers a subscription of ChainHeadEvent.
   393  func (bc *BlockChain) SubscribeChainHeadEvent(ch chan<- ChainHeadEvent) event.Subscription {
   394  	return bc.scope.Track(bc.chainHeadFeed.Subscribe(ch))
   395  }
   396  
   397  // SubscribeChainSideEvent registers a subscription of ChainSideEvent.
   398  func (bc *BlockChain) SubscribeChainSideEvent(ch chan<- ChainSideEvent) event.Subscription {
   399  	return bc.scope.Track(bc.chainSideFeed.Subscribe(ch))
   400  }
   401  
   402  // SubscribeLogsEvent registers a subscription of []*types.Log.
   403  func (bc *BlockChain) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
   404  	return bc.scope.Track(bc.logsFeed.Subscribe(ch))
   405  }
   406  
   407  // SubscribeBlockProcessingEvent registers a subscription of bool where true means
   408  // block processing has started while false means it has stopped.
   409  func (bc *BlockChain) SubscribeBlockProcessingEvent(ch chan<- bool) event.Subscription {
   410  	return bc.scope.Track(bc.blockProcFeed.Subscribe(ch))
   411  }