github.com/bloxroute-labs/bor@v0.1.4/eth/api_backend.go (about)

     1  // Copyright 2015 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 eth
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  	"math/big"
    23  
    24  	"github.com/maticnetwork/bor/accounts"
    25  	"github.com/maticnetwork/bor/common"
    26  	"github.com/maticnetwork/bor/common/math"
    27  	"github.com/maticnetwork/bor/core"
    28  	"github.com/maticnetwork/bor/core/bloombits"
    29  	"github.com/maticnetwork/bor/core/rawdb"
    30  	"github.com/maticnetwork/bor/core/state"
    31  	"github.com/maticnetwork/bor/core/types"
    32  	"github.com/maticnetwork/bor/core/vm"
    33  	"github.com/maticnetwork/bor/eth/downloader"
    34  	"github.com/maticnetwork/bor/eth/gasprice"
    35  	"github.com/maticnetwork/bor/ethdb"
    36  	"github.com/maticnetwork/bor/event"
    37  	"github.com/maticnetwork/bor/params"
    38  	"github.com/maticnetwork/bor/rpc"
    39  )
    40  
    41  // EthAPIBackend implements ethapi.Backend for full nodes
    42  type EthAPIBackend struct {
    43  	extRPCEnabled bool
    44  	eth           *Ethereum
    45  	gpo           *gasprice.Oracle
    46  }
    47  
    48  // ChainConfig returns the active chain configuration.
    49  func (b *EthAPIBackend) ChainConfig() *params.ChainConfig {
    50  	return b.eth.blockchain.Config()
    51  }
    52  
    53  func (b *EthAPIBackend) CurrentBlock() *types.Block {
    54  	return b.eth.blockchain.CurrentBlock()
    55  }
    56  
    57  func (b *EthAPIBackend) SetHead(number uint64) {
    58  	b.eth.protocolManager.downloader.Cancel()
    59  	b.eth.blockchain.SetHead(number)
    60  }
    61  
    62  func (b *EthAPIBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error) {
    63  	// Pending block is only known by the miner
    64  	if blockNr == rpc.PendingBlockNumber {
    65  		block := b.eth.miner.PendingBlock()
    66  		return block.Header(), nil
    67  	}
    68  	// Otherwise resolve and return the block
    69  	if blockNr == rpc.LatestBlockNumber {
    70  		return b.eth.blockchain.CurrentBlock().Header(), nil
    71  	}
    72  	return b.eth.blockchain.GetHeaderByNumber(uint64(blockNr)), nil
    73  }
    74  
    75  func (b *EthAPIBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
    76  	return b.eth.blockchain.GetHeaderByHash(hash), nil
    77  }
    78  
    79  func (b *EthAPIBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error) {
    80  	// Pending block is only known by the miner
    81  	if blockNr == rpc.PendingBlockNumber {
    82  		block := b.eth.miner.PendingBlock()
    83  		return block, nil
    84  	}
    85  	// Otherwise resolve and return the block
    86  	if blockNr == rpc.LatestBlockNumber {
    87  		return b.eth.blockchain.CurrentBlock(), nil
    88  	}
    89  	return b.eth.blockchain.GetBlockByNumber(uint64(blockNr)), nil
    90  }
    91  
    92  func (b *EthAPIBackend) StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*state.StateDB, *types.Header, error) {
    93  	// Pending state is only known by the miner
    94  	if blockNr == rpc.PendingBlockNumber {
    95  		block, state := b.eth.miner.Pending()
    96  		return state, block.Header(), nil
    97  	}
    98  	// Otherwise resolve the block number and return its state
    99  	header, err := b.HeaderByNumber(ctx, blockNr)
   100  	if err != nil {
   101  		return nil, nil, err
   102  	}
   103  	if header == nil {
   104  		return nil, nil, errors.New("header not found")
   105  	}
   106  	stateDb, err := b.eth.BlockChain().StateAt(header.Root)
   107  	return stateDb, header, err
   108  }
   109  
   110  func (b *EthAPIBackend) GetBlock(ctx context.Context, hash common.Hash) (*types.Block, error) {
   111  	return b.eth.blockchain.GetBlockByHash(hash), nil
   112  }
   113  
   114  func (b *EthAPIBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
   115  	return b.eth.blockchain.GetReceiptsByHash(hash), nil
   116  }
   117  
   118  func (b *EthAPIBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) {
   119  	receipts := b.eth.blockchain.GetReceiptsByHash(hash)
   120  	if receipts == nil {
   121  		return nil, nil
   122  	}
   123  	logs := make([][]*types.Log, len(receipts))
   124  	for i, receipt := range receipts {
   125  		logs[i] = receipt.Logs
   126  	}
   127  	return logs, nil
   128  }
   129  
   130  func (b *EthAPIBackend) GetTd(blockHash common.Hash) *big.Int {
   131  	return b.eth.blockchain.GetTdByHash(blockHash)
   132  }
   133  
   134  func (b *EthAPIBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header) (*vm.EVM, func() error, error) {
   135  	state.SetBalance(msg.From(), math.MaxBig256)
   136  	vmError := func() error { return nil }
   137  
   138  	context := core.NewEVMContext(msg, header, b.eth.BlockChain(), nil)
   139  	return vm.NewEVM(context, state, b.eth.blockchain.Config(), *b.eth.blockchain.GetVMConfig()), vmError, nil
   140  }
   141  
   142  func (b *EthAPIBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
   143  	return b.eth.BlockChain().SubscribeRemovedLogsEvent(ch)
   144  }
   145  
   146  func (b *EthAPIBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
   147  	return b.eth.BlockChain().SubscribeChainEvent(ch)
   148  }
   149  
   150  func (b *EthAPIBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
   151  	return b.eth.BlockChain().SubscribeChainHeadEvent(ch)
   152  }
   153  
   154  func (b *EthAPIBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription {
   155  	return b.eth.BlockChain().SubscribeChainSideEvent(ch)
   156  }
   157  
   158  func (b *EthAPIBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
   159  	return b.eth.BlockChain().SubscribeLogsEvent(ch)
   160  }
   161  
   162  func (b *EthAPIBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
   163  	return b.eth.txPool.AddLocal(signedTx)
   164  }
   165  
   166  func (b *EthAPIBackend) GetPoolTransactions() (types.Transactions, error) {
   167  	pending, err := b.eth.txPool.Pending()
   168  	if err != nil {
   169  		return nil, err
   170  	}
   171  	var txs types.Transactions
   172  	for _, batch := range pending {
   173  		txs = append(txs, batch...)
   174  	}
   175  	return txs, nil
   176  }
   177  
   178  func (b *EthAPIBackend) GetPoolTransaction(hash common.Hash) *types.Transaction {
   179  	return b.eth.txPool.Get(hash)
   180  }
   181  
   182  func (b *EthAPIBackend) GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) {
   183  	tx, blockHash, blockNumber, index := rawdb.ReadTransaction(b.eth.ChainDb(), txHash)
   184  	return tx, blockHash, blockNumber, index, nil
   185  }
   186  
   187  func (b *EthAPIBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
   188  	return b.eth.txPool.Nonce(addr), nil
   189  }
   190  
   191  func (b *EthAPIBackend) Stats() (pending int, queued int) {
   192  	return b.eth.txPool.Stats()
   193  }
   194  
   195  func (b *EthAPIBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
   196  	return b.eth.TxPool().Content()
   197  }
   198  
   199  func (b *EthAPIBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription {
   200  	return b.eth.TxPool().SubscribeNewTxsEvent(ch)
   201  }
   202  
   203  func (b *EthAPIBackend) Downloader() *downloader.Downloader {
   204  	return b.eth.Downloader()
   205  }
   206  
   207  func (b *EthAPIBackend) ProtocolVersion() int {
   208  	return b.eth.EthVersion()
   209  }
   210  
   211  func (b *EthAPIBackend) SuggestPrice(ctx context.Context) (*big.Int, error) {
   212  	return b.gpo.SuggestPrice(ctx)
   213  }
   214  
   215  func (b *EthAPIBackend) ChainDb() ethdb.Database {
   216  	return b.eth.ChainDb()
   217  }
   218  
   219  func (b *EthAPIBackend) EventMux() *event.TypeMux {
   220  	return b.eth.EventMux()
   221  }
   222  
   223  func (b *EthAPIBackend) AccountManager() *accounts.Manager {
   224  	return b.eth.AccountManager()
   225  }
   226  
   227  func (b *EthAPIBackend) ExtRPCEnabled() bool {
   228  	return b.extRPCEnabled
   229  }
   230  
   231  func (b *EthAPIBackend) RPCGasCap() *big.Int {
   232  	return b.eth.config.RPCGasCap
   233  }
   234  
   235  func (b *EthAPIBackend) BloomStatus() (uint64, uint64) {
   236  	sections, _, _ := b.eth.bloomIndexer.Sections()
   237  	return params.BloomBitsBlocks, sections
   238  }
   239  
   240  func (b *EthAPIBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) {
   241  	for i := 0; i < bloomFilterThreads; i++ {
   242  		go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests)
   243  	}
   244  }