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