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