github.com/MetalBlockchain/subnet-evm@v0.4.9/eth/api_backend.go (about)

     1  // (c) 2019-2020, Ava Labs, Inc.
     2  //
     3  // This file is a derived work, based on the go-ethereum library whose original
     4  // notices appear below.
     5  //
     6  // It is distributed under a license compatible with the licensing terms of the
     7  // original code from which it is derived.
     8  //
     9  // Much love to the original authors for their work.
    10  // **********
    11  // Copyright 2015 The go-ethereum Authors
    12  // This file is part of the go-ethereum library.
    13  //
    14  // The go-ethereum library is free software: you can redistribute it and/or modify
    15  // it under the terms of the GNU Lesser General Public License as published by
    16  // the Free Software Foundation, either version 3 of the License, or
    17  // (at your option) any later version.
    18  //
    19  // The go-ethereum library is distributed in the hope that it will be useful,
    20  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    21  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    22  // GNU Lesser General Public License for more details.
    23  //
    24  // You should have received a copy of the GNU Lesser General Public License
    25  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    26  
    27  package eth
    28  
    29  import (
    30  	"context"
    31  	"errors"
    32  	"math/big"
    33  	"time"
    34  
    35  	"github.com/MetalBlockchain/subnet-evm/accounts"
    36  	"github.com/MetalBlockchain/subnet-evm/commontype"
    37  	"github.com/MetalBlockchain/subnet-evm/consensus"
    38  	"github.com/MetalBlockchain/subnet-evm/consensus/dummy"
    39  	"github.com/MetalBlockchain/subnet-evm/core"
    40  	"github.com/MetalBlockchain/subnet-evm/core/bloombits"
    41  	"github.com/MetalBlockchain/subnet-evm/core/rawdb"
    42  	"github.com/MetalBlockchain/subnet-evm/core/state"
    43  	"github.com/MetalBlockchain/subnet-evm/core/types"
    44  	"github.com/MetalBlockchain/subnet-evm/core/vm"
    45  	"github.com/MetalBlockchain/subnet-evm/eth/gasprice"
    46  	"github.com/MetalBlockchain/subnet-evm/ethdb"
    47  	"github.com/MetalBlockchain/subnet-evm/params"
    48  	"github.com/MetalBlockchain/subnet-evm/rpc"
    49  	"github.com/ethereum/go-ethereum/common"
    50  	"github.com/ethereum/go-ethereum/event"
    51  )
    52  
    53  var ErrUnfinalizedData = errors.New("cannot query unfinalized data")
    54  
    55  // EthAPIBackend implements ethapi.Backend for full nodes
    56  type EthAPIBackend struct {
    57  	extRPCEnabled            bool
    58  	allowUnprotectedTxs      bool
    59  	allowUnprotectedTxHashes map[common.Hash]struct{} // Invariant: read-only after creation.
    60  	eth                      *Ethereum
    61  	gpo                      *gasprice.Oracle
    62  }
    63  
    64  // ChainConfig returns the active chain configuration.
    65  func (b *EthAPIBackend) ChainConfig() *params.ChainConfig {
    66  	return b.eth.blockchain.Config()
    67  }
    68  
    69  func (b *EthAPIBackend) GetVMConfig() *vm.Config {
    70  	return b.eth.blockchain.GetVMConfig()
    71  }
    72  
    73  func (b *EthAPIBackend) CurrentBlock() *types.Block {
    74  	return b.eth.blockchain.CurrentBlock()
    75  }
    76  
    77  func (b *EthAPIBackend) LastAcceptedBlock() *types.Block {
    78  	return b.eth.LastAcceptedBlock()
    79  }
    80  
    81  func (b *EthAPIBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) {
    82  	if err := ctx.Err(); err != nil {
    83  		return nil, err
    84  	}
    85  	// Treat requests for the pending, latest, or accepted block
    86  	// identically.
    87  	acceptedBlock := b.eth.LastAcceptedBlock()
    88  	if number.IsAccepted() {
    89  		return acceptedBlock.Header(), nil
    90  	}
    91  
    92  	if !b.GetVMConfig().AllowUnfinalizedQueries && acceptedBlock != nil {
    93  		if number.Int64() > acceptedBlock.Number().Int64() {
    94  			return nil, ErrUnfinalizedData
    95  		}
    96  	}
    97  
    98  	return b.eth.blockchain.GetHeaderByNumber(uint64(number)), nil
    99  }
   100  
   101  func (b *EthAPIBackend) GetFeeConfigAt(parent *types.Header) (commontype.FeeConfig, *big.Int, error) {
   102  	return b.eth.blockchain.GetFeeConfigAt(parent)
   103  }
   104  
   105  func (b *EthAPIBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
   106  	if err := ctx.Err(); err != nil {
   107  		return nil, err
   108  	}
   109  
   110  	header := b.eth.blockchain.GetHeaderByHash(hash)
   111  	if header == nil {
   112  		return nil, nil
   113  	}
   114  
   115  	if b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash {
   116  		return nil, nil
   117  	}
   118  
   119  	acceptedBlock := b.eth.LastAcceptedBlock()
   120  	if !b.GetVMConfig().AllowUnfinalizedQueries && acceptedBlock != nil {
   121  		if header.Number.Cmp(acceptedBlock.Number()) > 0 {
   122  			return nil, ErrUnfinalizedData
   123  		}
   124  	}
   125  	return header, nil
   126  }
   127  
   128  func (b *EthAPIBackend) HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error) {
   129  	if blockNr, ok := blockNrOrHash.Number(); ok {
   130  		return b.HeaderByNumber(ctx, blockNr)
   131  	}
   132  	if hash, ok := blockNrOrHash.Hash(); ok {
   133  		header, err := b.HeaderByHash(ctx, hash)
   134  		if err != nil {
   135  			return nil, err
   136  		}
   137  		if header == nil {
   138  			return nil, errors.New("header for hash not found")
   139  		}
   140  		return header, nil
   141  	}
   142  	return nil, errors.New("invalid arguments; neither block nor hash specified")
   143  }
   144  
   145  func (b *EthAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) {
   146  	if err := ctx.Err(); err != nil {
   147  		return nil, err
   148  	}
   149  	// Treat requests for the pending, latest, or accepted block
   150  	// identically.
   151  	acceptedBlock := b.eth.LastAcceptedBlock()
   152  	if number.IsAccepted() {
   153  		return acceptedBlock, nil
   154  	}
   155  
   156  	if !b.GetVMConfig().AllowUnfinalizedQueries && acceptedBlock != nil {
   157  		if number.Int64() > acceptedBlock.Number().Int64() {
   158  			return nil, ErrUnfinalizedData
   159  		}
   160  	}
   161  
   162  	return b.eth.blockchain.GetBlockByNumber(uint64(number)), nil
   163  }
   164  
   165  func (b *EthAPIBackend) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) {
   166  	if err := ctx.Err(); err != nil {
   167  		return nil, err
   168  	}
   169  
   170  	block := b.eth.blockchain.GetBlockByHash(hash)
   171  	if block == nil {
   172  		return nil, nil
   173  	}
   174  
   175  	number := block.Number()
   176  	if b.eth.blockchain.GetCanonicalHash(number.Uint64()) != hash {
   177  		return nil, nil
   178  	}
   179  
   180  	acceptedBlock := b.eth.LastAcceptedBlock()
   181  	if !b.GetVMConfig().AllowUnfinalizedQueries && acceptedBlock != nil {
   182  		if number.Cmp(acceptedBlock.Number()) > 0 {
   183  			return nil, ErrUnfinalizedData
   184  		}
   185  	}
   186  	return block, nil
   187  }
   188  
   189  func (b *EthAPIBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error) {
   190  	if blockNr, ok := blockNrOrHash.Number(); ok {
   191  		return b.BlockByNumber(ctx, blockNr)
   192  	}
   193  	if err := ctx.Err(); err != nil {
   194  		return nil, err
   195  	}
   196  	if hash, ok := blockNrOrHash.Hash(); ok {
   197  		block, err := b.BlockByHash(ctx, hash)
   198  		if err != nil {
   199  			return nil, err
   200  		}
   201  		if block == nil {
   202  			return nil, errors.New("header for hash not found")
   203  		}
   204  		return block, nil
   205  	}
   206  	return nil, errors.New("invalid arguments; neither block nor hash specified")
   207  }
   208  
   209  func (b *EthAPIBackend) BadBlocks() ([]*types.Block, []*core.BadBlockReason) {
   210  	return b.eth.blockchain.BadBlocks()
   211  }
   212  
   213  func (b *EthAPIBackend) StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error) {
   214  	// Request the block by its number and retrieve its state
   215  	header, err := b.HeaderByNumber(ctx, number)
   216  	if err != nil {
   217  		return nil, nil, err
   218  	}
   219  	if header == nil {
   220  		return nil, nil, errors.New("header not found")
   221  	}
   222  	stateDb, err := b.eth.BlockChain().StateAt(header.Root)
   223  	return stateDb, header, err
   224  }
   225  
   226  func (b *EthAPIBackend) StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error) {
   227  	if blockNr, ok := blockNrOrHash.Number(); ok {
   228  		return b.StateAndHeaderByNumber(ctx, blockNr)
   229  	}
   230  	if err := ctx.Err(); err != nil {
   231  		return nil, nil, err
   232  	}
   233  	if hash, ok := blockNrOrHash.Hash(); ok {
   234  		header, err := b.HeaderByHash(ctx, hash)
   235  		if err != nil {
   236  			return nil, nil, err
   237  		}
   238  		if header == nil {
   239  			return nil, nil, errors.New("header for hash not found")
   240  		}
   241  		stateDb, err := b.eth.BlockChain().StateAt(header.Root)
   242  		return stateDb, header, err
   243  	}
   244  	return nil, nil, errors.New("invalid arguments; neither block nor hash specified")
   245  }
   246  
   247  func (b *EthAPIBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
   248  	if err := ctx.Err(); err != nil {
   249  		return nil, err
   250  	}
   251  	return b.eth.blockchain.GetReceiptsByHash(hash), nil
   252  }
   253  
   254  func (b *EthAPIBackend) GetLogs(ctx context.Context, hash common.Hash, number uint64) ([][]*types.Log, error) {
   255  	if err := ctx.Err(); err != nil {
   256  		return nil, err
   257  	}
   258  	return b.eth.blockchain.GetLogs(hash, number), nil
   259  }
   260  
   261  func (b *EthAPIBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmConfig *vm.Config) (*vm.EVM, func() error, error) {
   262  	vmError := func() error { return nil }
   263  	if vmConfig == nil {
   264  		vmConfig = b.eth.blockchain.GetVMConfig()
   265  	}
   266  	txContext := core.NewEVMTxContext(msg)
   267  	context := core.NewEVMBlockContext(header, b.eth.BlockChain(), nil)
   268  	return vm.NewEVM(context, txContext, state, b.eth.blockchain.Config(), *vmConfig), vmError, nil
   269  }
   270  
   271  func (b *EthAPIBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
   272  	return b.eth.BlockChain().SubscribeRemovedLogsEvent(ch)
   273  }
   274  
   275  func (b *EthAPIBackend) SubscribePendingLogsEvent(ch chan<- []*types.Log) event.Subscription {
   276  	return b.eth.miner.SubscribePendingLogs(ch)
   277  }
   278  
   279  func (b *EthAPIBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
   280  	return b.eth.BlockChain().SubscribeChainEvent(ch)
   281  }
   282  
   283  func (b *EthAPIBackend) SubscribeChainAcceptedEvent(ch chan<- core.ChainEvent) event.Subscription {
   284  	return b.eth.BlockChain().SubscribeChainAcceptedEvent(ch)
   285  }
   286  
   287  func (b *EthAPIBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
   288  	return b.eth.BlockChain().SubscribeChainHeadEvent(ch)
   289  }
   290  
   291  func (b *EthAPIBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription {
   292  	return b.eth.BlockChain().SubscribeChainSideEvent(ch)
   293  }
   294  
   295  func (b *EthAPIBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
   296  	return b.eth.BlockChain().SubscribeLogsEvent(ch)
   297  }
   298  
   299  func (b *EthAPIBackend) SubscribeAcceptedLogsEvent(ch chan<- []*types.Log) event.Subscription {
   300  	return b.eth.BlockChain().SubscribeAcceptedLogsEvent(ch)
   301  }
   302  
   303  func (b *EthAPIBackend) SubscribeAcceptedTransactionEvent(ch chan<- core.NewTxsEvent) event.Subscription {
   304  	return b.eth.BlockChain().SubscribeAcceptedTransactionEvent(ch)
   305  }
   306  
   307  func (b *EthAPIBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
   308  	if err := ctx.Err(); err != nil {
   309  		return err
   310  	}
   311  	return b.eth.txPool.AddLocal(signedTx)
   312  }
   313  
   314  func (b *EthAPIBackend) GetPoolTransactions() (types.Transactions, error) {
   315  	pending := b.eth.txPool.Pending(false)
   316  	var txs types.Transactions
   317  	for _, batch := range pending {
   318  		txs = append(txs, batch...)
   319  	}
   320  	return txs, nil
   321  }
   322  
   323  func (b *EthAPIBackend) GetPoolTransaction(hash common.Hash) *types.Transaction {
   324  	return b.eth.txPool.Get(hash)
   325  }
   326  
   327  func (b *EthAPIBackend) GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) {
   328  	// Note: we only index transactions during Accept, so the below check against unfinalized queries is technically redundant, but
   329  	// we keep it for defense in depth.
   330  	tx, blockHash, blockNumber, index := rawdb.ReadTransaction(b.eth.ChainDb(), txHash)
   331  
   332  	// Respond as if the transaction does not exist if it is not yet in an
   333  	// accepted block. We explicitly choose not to error here to avoid breaking
   334  	// expectations with clients (expect an empty response when a transaction
   335  	// does not exist).
   336  	acceptedBlock := b.eth.LastAcceptedBlock()
   337  	if !b.GetVMConfig().AllowUnfinalizedQueries && acceptedBlock != nil && tx != nil {
   338  		if blockNumber > acceptedBlock.NumberU64() {
   339  			return nil, common.Hash{}, 0, 0, nil
   340  		}
   341  	}
   342  
   343  	return tx, blockHash, blockNumber, index, nil
   344  }
   345  
   346  func (b *EthAPIBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
   347  	return b.eth.txPool.Nonce(addr), nil
   348  }
   349  
   350  func (b *EthAPIBackend) Stats() (pending int, queued int) {
   351  	return b.eth.txPool.Stats()
   352  }
   353  
   354  func (b *EthAPIBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
   355  	return b.eth.txPool.Content()
   356  }
   357  
   358  func (b *EthAPIBackend) TxPoolContentFrom(addr common.Address) (types.Transactions, types.Transactions) {
   359  	return b.eth.txPool.ContentFrom(addr)
   360  }
   361  
   362  func (b *EthAPIBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription {
   363  	return b.eth.txPool.SubscribeNewTxsEvent(ch)
   364  }
   365  
   366  func (b *EthAPIBackend) EstimateBaseFee(ctx context.Context) (*big.Int, error) {
   367  	return b.gpo.EstimateBaseFee(ctx)
   368  }
   369  
   370  func (b *EthAPIBackend) SuggestPrice(ctx context.Context) (*big.Int, error) {
   371  	return b.gpo.SuggestPrice(ctx)
   372  }
   373  
   374  func (b *EthAPIBackend) SuggestGasTipCap(ctx context.Context) (*big.Int, error) {
   375  	return b.gpo.SuggestTipCap(ctx)
   376  }
   377  
   378  func (b *EthAPIBackend) FeeHistory(ctx context.Context, blockCount int, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (firstBlock *big.Int, reward [][]*big.Int, baseFee []*big.Int, gasUsedRatio []float64, err error) {
   379  	return b.gpo.FeeHistory(ctx, blockCount, lastBlock, rewardPercentiles)
   380  }
   381  
   382  func (b *EthAPIBackend) ChainDb() ethdb.Database {
   383  	return b.eth.ChainDb()
   384  }
   385  
   386  func (b *EthAPIBackend) EventMux() *event.TypeMux {
   387  	return b.eth.EventMux()
   388  }
   389  
   390  func (b *EthAPIBackend) AccountManager() *accounts.Manager {
   391  	return b.eth.AccountManager()
   392  }
   393  
   394  func (b *EthAPIBackend) ExtRPCEnabled() bool {
   395  	return b.extRPCEnabled
   396  }
   397  
   398  func (b *EthAPIBackend) UnprotectedAllowed(tx *types.Transaction) bool {
   399  	if b.allowUnprotectedTxs {
   400  		return true
   401  	}
   402  
   403  	// Check for special cased transaction hashes:
   404  	// Note: this map is read-only after creation, so it is safe to read from it on multiple threads.
   405  	if _, ok := b.allowUnprotectedTxHashes[tx.Hash()]; ok {
   406  		return true
   407  	}
   408  
   409  	// Check for "predictable pattern" (Nick's Signature: https://weka.medium.com/how-to-send-ether-to-11-440-people-187e332566b7)
   410  	v, r, s := tx.RawSignatureValues()
   411  	if v == nil || r == nil || s == nil {
   412  		return false
   413  	}
   414  
   415  	return tx.Nonce() == 0 && r.Cmp(s) == 0
   416  }
   417  
   418  func (b *EthAPIBackend) RPCGasCap() uint64 {
   419  	return b.eth.config.RPCGasCap
   420  }
   421  
   422  func (b *EthAPIBackend) RPCEVMTimeout() time.Duration {
   423  	return b.eth.config.RPCEVMTimeout
   424  }
   425  
   426  func (b *EthAPIBackend) RPCTxFeeCap() float64 {
   427  	return b.eth.config.RPCTxFeeCap
   428  }
   429  
   430  func (b *EthAPIBackend) BloomStatus() (uint64, uint64) {
   431  	sections, _, _ := b.eth.bloomIndexer.Sections()
   432  	return params.BloomBitsBlocks, sections
   433  }
   434  
   435  func (b *EthAPIBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) {
   436  	for i := 0; i < bloomFilterThreads; i++ {
   437  		go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests)
   438  	}
   439  }
   440  
   441  func (b *EthAPIBackend) Engine() consensus.Engine {
   442  	return b.eth.engine
   443  }
   444  
   445  func (b *EthAPIBackend) CurrentHeader() *types.Header {
   446  	return b.eth.blockchain.CurrentHeader()
   447  }
   448  
   449  func (b *EthAPIBackend) GetMaxBlocksPerRequest() int64 {
   450  	return b.eth.settings.MaxBlocksPerRequest
   451  }
   452  
   453  func (b *EthAPIBackend) StateAtBlock(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, checkLive bool, preferDisk bool) (*state.StateDB, error) {
   454  	return b.eth.StateAtBlock(block, reexec, base, checkLive, preferDisk)
   455  }
   456  
   457  func (b *EthAPIBackend) StateAtTransaction(ctx context.Context, block *types.Block, txIndex int, reexec uint64) (core.Message, vm.BlockContext, *state.StateDB, error) {
   458  	return b.eth.stateAtTransaction(block, txIndex, reexec)
   459  }
   460  
   461  func (b *EthAPIBackend) MinRequiredTip(ctx context.Context, header *types.Header) (*big.Int, error) {
   462  	return dummy.MinRequiredTip(b.ChainConfig(), header)
   463  }