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