github.com/bamzi/go-ethereum@v1.6.7-0.20170704111104-138f26c93af1/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  	"github.com/ethereum/go-ethereum/core"
    27  	"github.com/ethereum/go-ethereum/core/state"
    28  	"github.com/ethereum/go-ethereum/core/types"
    29  	"github.com/ethereum/go-ethereum/core/vm"
    30  	"github.com/ethereum/go-ethereum/eth/downloader"
    31  	"github.com/ethereum/go-ethereum/eth/gasprice"
    32  	"github.com/ethereum/go-ethereum/ethdb"
    33  	"github.com/ethereum/go-ethereum/event"
    34  	"github.com/ethereum/go-ethereum/params"
    35  	"github.com/ethereum/go-ethereum/rpc"
    36  )
    37  
    38  // EthApiBackend implements ethapi.Backend for full nodes
    39  type EthApiBackend struct {
    40  	eth *Ethereum
    41  	gpo *gasprice.Oracle
    42  }
    43  
    44  func (b *EthApiBackend) ChainConfig() *params.ChainConfig {
    45  	return b.eth.chainConfig
    46  }
    47  
    48  func (b *EthApiBackend) CurrentBlock() *types.Block {
    49  	return b.eth.blockchain.CurrentBlock()
    50  }
    51  
    52  func (b *EthApiBackend) SetHead(number uint64) {
    53  	b.eth.protocolManager.downloader.Cancel()
    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) (*state.StateDB, *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 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 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 *state.StateDB, header *types.Header, vmCfg vm.Config) (*vm.EVM, func() error, error) {
   111  	state.SetBalance(msg.From(), math.MaxBig256)
   112  	vmError := func() error { return nil }
   113  
   114  	context := core.NewEVMContext(msg, header, b.eth.BlockChain(), nil)
   115  	return vm.NewEVM(context, state, b.eth.chainConfig, vmCfg), vmError, nil
   116  }
   117  
   118  func (b *EthApiBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
   119  	b.eth.txMu.Lock()
   120  	defer b.eth.txMu.Unlock()
   121  
   122  	b.eth.txPool.SetLocal(signedTx)
   123  	return b.eth.txPool.Add(signedTx)
   124  }
   125  
   126  func (b *EthApiBackend) RemoveTx(txHash common.Hash) {
   127  	b.eth.txMu.Lock()
   128  	defer b.eth.txMu.Unlock()
   129  
   130  	b.eth.txPool.Remove(txHash)
   131  }
   132  
   133  func (b *EthApiBackend) GetPoolTransactions() (types.Transactions, error) {
   134  	b.eth.txMu.Lock()
   135  	defer b.eth.txMu.Unlock()
   136  
   137  	pending, err := b.eth.txPool.Pending()
   138  	if err != nil {
   139  		return nil, err
   140  	}
   141  
   142  	var txs types.Transactions
   143  	for _, batch := range pending {
   144  		txs = append(txs, batch...)
   145  	}
   146  	return txs, nil
   147  }
   148  
   149  func (b *EthApiBackend) GetPoolTransaction(hash common.Hash) *types.Transaction {
   150  	b.eth.txMu.Lock()
   151  	defer b.eth.txMu.Unlock()
   152  
   153  	return b.eth.txPool.Get(hash)
   154  }
   155  
   156  func (b *EthApiBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
   157  	b.eth.txMu.Lock()
   158  	defer b.eth.txMu.Unlock()
   159  
   160  	return b.eth.txPool.State().GetNonce(addr), nil
   161  }
   162  
   163  func (b *EthApiBackend) Stats() (pending int, queued int) {
   164  	b.eth.txMu.Lock()
   165  	defer b.eth.txMu.Unlock()
   166  
   167  	return b.eth.txPool.Stats()
   168  }
   169  
   170  func (b *EthApiBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
   171  	b.eth.txMu.Lock()
   172  	defer b.eth.txMu.Unlock()
   173  
   174  	return b.eth.TxPool().Content()
   175  }
   176  
   177  func (b *EthApiBackend) Downloader() *downloader.Downloader {
   178  	return b.eth.Downloader()
   179  }
   180  
   181  func (b *EthApiBackend) ProtocolVersion() int {
   182  	return b.eth.EthVersion()
   183  }
   184  
   185  func (b *EthApiBackend) SuggestPrice(ctx context.Context) (*big.Int, error) {
   186  	return b.gpo.SuggestPrice(ctx)
   187  }
   188  
   189  func (b *EthApiBackend) ChainDb() ethdb.Database {
   190  	return b.eth.ChainDb()
   191  }
   192  
   193  func (b *EthApiBackend) EventMux() *event.TypeMux {
   194  	return b.eth.EventMux()
   195  }
   196  
   197  func (b *EthApiBackend) AccountManager() *accounts.Manager {
   198  	return b.eth.AccountManager()
   199  }