github.com/avence12/go-ethereum@v1.5.10-0.20170320123548-1dfd65f6d047/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  	"math/big"
    21  
    22  	"github.com/ethereum/go-ethereum/accounts"
    23  	"github.com/ethereum/go-ethereum/common"
    24  	"github.com/ethereum/go-ethereum/common/math"
    25  	"github.com/ethereum/go-ethereum/core"
    26  	"github.com/ethereum/go-ethereum/core/state"
    27  	"github.com/ethereum/go-ethereum/core/types"
    28  	"github.com/ethereum/go-ethereum/core/vm"
    29  	"github.com/ethereum/go-ethereum/eth/downloader"
    30  	"github.com/ethereum/go-ethereum/eth/gasprice"
    31  	"github.com/ethereum/go-ethereum/ethdb"
    32  	"github.com/ethereum/go-ethereum/event"
    33  	"github.com/ethereum/go-ethereum/internal/ethapi"
    34  	"github.com/ethereum/go-ethereum/params"
    35  	"github.com/ethereum/go-ethereum/rpc"
    36  	"golang.org/x/net/context"
    37  )
    38  
    39  // EthApiBackend implements ethapi.Backend for full nodes
    40  type EthApiBackend struct {
    41  	eth *Ethereum
    42  	gpo *gasprice.GasPriceOracle
    43  }
    44  
    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.blockchain.SetHead(number)
    55  }
    56  
    57  func (b *EthApiBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error) {
    58  	// Pending block is only known by the miner
    59  	if blockNr == rpc.PendingBlockNumber {
    60  		block := b.eth.miner.PendingBlock()
    61  		return block.Header(), nil
    62  	}
    63  	// Otherwise resolve and return the block
    64  	if blockNr == rpc.LatestBlockNumber {
    65  		return b.eth.blockchain.CurrentBlock().Header(), nil
    66  	}
    67  	return b.eth.blockchain.GetHeaderByNumber(uint64(blockNr)), nil
    68  }
    69  
    70  func (b *EthApiBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error) {
    71  	// Pending block is only known by the miner
    72  	if blockNr == rpc.PendingBlockNumber {
    73  		block := b.eth.miner.PendingBlock()
    74  		return block, nil
    75  	}
    76  	// Otherwise resolve and return the block
    77  	if blockNr == rpc.LatestBlockNumber {
    78  		return b.eth.blockchain.CurrentBlock(), nil
    79  	}
    80  	return b.eth.blockchain.GetBlockByNumber(uint64(blockNr)), nil
    81  }
    82  
    83  func (b *EthApiBackend) StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (ethapi.State, *types.Header, error) {
    84  	// Pending state is only known by the miner
    85  	if blockNr == rpc.PendingBlockNumber {
    86  		block, state := b.eth.miner.Pending()
    87  		return EthApiState{state}, block.Header(), nil
    88  	}
    89  	// Otherwise resolve the block number and return its state
    90  	header, err := b.HeaderByNumber(ctx, blockNr)
    91  	if header == nil || err != nil {
    92  		return nil, nil, err
    93  	}
    94  	stateDb, err := b.eth.BlockChain().StateAt(header.Root)
    95  	return EthApiState{stateDb}, header, err
    96  }
    97  
    98  func (b *EthApiBackend) GetBlock(ctx context.Context, blockHash common.Hash) (*types.Block, error) {
    99  	return b.eth.blockchain.GetBlockByHash(blockHash), nil
   100  }
   101  
   102  func (b *EthApiBackend) GetReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error) {
   103  	return core.GetBlockReceipts(b.eth.chainDb, blockHash, core.GetBlockNumber(b.eth.chainDb, blockHash)), nil
   104  }
   105  
   106  func (b *EthApiBackend) GetTd(blockHash common.Hash) *big.Int {
   107  	return b.eth.blockchain.GetTdByHash(blockHash)
   108  }
   109  
   110  func (b *EthApiBackend) GetEVM(ctx context.Context, msg core.Message, state ethapi.State, header *types.Header, vmCfg vm.Config) (*vm.EVM, func() error, error) {
   111  	statedb := state.(EthApiState).state
   112  	from := statedb.GetOrNewStateObject(msg.From())
   113  	from.SetBalance(math.MaxBig256)
   114  	vmError := func() error { return nil }
   115  
   116  	context := core.NewEVMContext(msg, header, b.eth.BlockChain())
   117  	return vm.NewEVM(context, statedb, b.eth.chainConfig, vmCfg), vmError, nil
   118  }
   119  
   120  func (b *EthApiBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
   121  	b.eth.txMu.Lock()
   122  	defer b.eth.txMu.Unlock()
   123  
   124  	b.eth.txPool.SetLocal(signedTx)
   125  	return b.eth.txPool.Add(signedTx)
   126  }
   127  
   128  func (b *EthApiBackend) RemoveTx(txHash common.Hash) {
   129  	b.eth.txMu.Lock()
   130  	defer b.eth.txMu.Unlock()
   131  
   132  	b.eth.txPool.Remove(txHash)
   133  }
   134  
   135  func (b *EthApiBackend) GetPoolTransactions() (types.Transactions, error) {
   136  	b.eth.txMu.Lock()
   137  	defer b.eth.txMu.Unlock()
   138  
   139  	pending, err := b.eth.txPool.Pending()
   140  	if err != nil {
   141  		return nil, err
   142  	}
   143  
   144  	var txs types.Transactions
   145  	for _, batch := range pending {
   146  		txs = append(txs, batch...)
   147  	}
   148  	return txs, nil
   149  }
   150  
   151  func (b *EthApiBackend) GetPoolTransaction(hash common.Hash) *types.Transaction {
   152  	b.eth.txMu.Lock()
   153  	defer b.eth.txMu.Unlock()
   154  
   155  	return b.eth.txPool.Get(hash)
   156  }
   157  
   158  func (b *EthApiBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
   159  	b.eth.txMu.Lock()
   160  	defer b.eth.txMu.Unlock()
   161  
   162  	return b.eth.txPool.State().GetNonce(addr), nil
   163  }
   164  
   165  func (b *EthApiBackend) Stats() (pending int, queued int) {
   166  	b.eth.txMu.Lock()
   167  	defer b.eth.txMu.Unlock()
   168  
   169  	return b.eth.txPool.Stats()
   170  }
   171  
   172  func (b *EthApiBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
   173  	b.eth.txMu.Lock()
   174  	defer b.eth.txMu.Unlock()
   175  
   176  	return b.eth.TxPool().Content()
   177  }
   178  
   179  func (b *EthApiBackend) Downloader() *downloader.Downloader {
   180  	return b.eth.Downloader()
   181  }
   182  
   183  func (b *EthApiBackend) ProtocolVersion() int {
   184  	return b.eth.EthVersion()
   185  }
   186  
   187  func (b *EthApiBackend) SuggestPrice(ctx context.Context) (*big.Int, error) {
   188  	return b.gpo.SuggestPrice(), nil
   189  }
   190  
   191  func (b *EthApiBackend) ChainDb() ethdb.Database {
   192  	return b.eth.ChainDb()
   193  }
   194  
   195  func (b *EthApiBackend) EventMux() *event.TypeMux {
   196  	return b.eth.EventMux()
   197  }
   198  
   199  func (b *EthApiBackend) AccountManager() *accounts.Manager {
   200  	return b.eth.AccountManager()
   201  }
   202  
   203  type EthApiState struct {
   204  	state *state.StateDB
   205  }
   206  
   207  func (s EthApiState) GetBalance(ctx context.Context, addr common.Address) (*big.Int, error) {
   208  	return s.state.GetBalance(addr), nil
   209  }
   210  
   211  func (s EthApiState) GetCode(ctx context.Context, addr common.Address) ([]byte, error) {
   212  	return s.state.GetCode(addr), nil
   213  }
   214  
   215  func (s EthApiState) GetState(ctx context.Context, a common.Address, b common.Hash) (common.Hash, error) {
   216  	return s.state.GetState(a, b), nil
   217  }
   218  
   219  func (s EthApiState) GetNonce(ctx context.Context, addr common.Address) (uint64, error) {
   220  	return s.state.GetNonce(addr), nil
   221  }