github.com/devfans/go-ethereum@v1.5.10-0.20170326212234-7419d0c38291/internal/ethapi/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 ethapi implements the general Ethereum API functions.
    18  package ethapi
    19  
    20  import (
    21  	"context"
    22  	"math/big"
    23  
    24  	"github.com/ethereum/go-ethereum/accounts"
    25  	"github.com/ethereum/go-ethereum/common"
    26  	"github.com/ethereum/go-ethereum/core"
    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/ethdb"
    31  	"github.com/ethereum/go-ethereum/event"
    32  	"github.com/ethereum/go-ethereum/params"
    33  	"github.com/ethereum/go-ethereum/rpc"
    34  )
    35  
    36  // Backend interface provides the common API services (that are provided by
    37  // both full and light clients) with access to necessary functions.
    38  type Backend interface {
    39  	// general Ethereum API
    40  	Downloader() *downloader.Downloader
    41  	ProtocolVersion() int
    42  	SuggestPrice(ctx context.Context) (*big.Int, error)
    43  	ChainDb() ethdb.Database
    44  	EventMux() *event.TypeMux
    45  	AccountManager() *accounts.Manager
    46  	// BlockChain API
    47  	SetHead(number uint64)
    48  	HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error)
    49  	BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error)
    50  	StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (State, *types.Header, error)
    51  	GetBlock(ctx context.Context, blockHash common.Hash) (*types.Block, error)
    52  	GetReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error)
    53  	GetTd(blockHash common.Hash) *big.Int
    54  	GetEVM(ctx context.Context, msg core.Message, state State, header *types.Header, vmCfg vm.Config) (*vm.EVM, func() error, error)
    55  	// TxPool API
    56  	SendTx(ctx context.Context, signedTx *types.Transaction) error
    57  	RemoveTx(txHash common.Hash)
    58  	GetPoolTransactions() (types.Transactions, error)
    59  	GetPoolTransaction(txHash common.Hash) *types.Transaction
    60  	GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error)
    61  	Stats() (pending int, queued int)
    62  	TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions)
    63  
    64  	ChainConfig() *params.ChainConfig
    65  	CurrentBlock() *types.Block
    66  }
    67  
    68  type State interface {
    69  	GetBalance(ctx context.Context, addr common.Address) (*big.Int, error)
    70  	GetCode(ctx context.Context, addr common.Address) ([]byte, error)
    71  	GetState(ctx context.Context, a common.Address, b common.Hash) (common.Hash, error)
    72  	GetNonce(ctx context.Context, addr common.Address) (uint64, error)
    73  }
    74  
    75  func GetAPIs(apiBackend Backend, solcPath string) []rpc.API {
    76  	return []rpc.API{
    77  		{
    78  			Namespace: "eth",
    79  			Version:   "1.0",
    80  			Service:   NewPublicEthereumAPI(apiBackend),
    81  			Public:    true,
    82  		}, {
    83  			Namespace: "eth",
    84  			Version:   "1.0",
    85  			Service:   NewPublicBlockChainAPI(apiBackend),
    86  			Public:    true,
    87  		}, {
    88  			Namespace: "eth",
    89  			Version:   "1.0",
    90  			Service:   NewPublicTransactionPoolAPI(apiBackend),
    91  			Public:    true,
    92  		}, {
    93  			Namespace: "txpool",
    94  			Version:   "1.0",
    95  			Service:   NewPublicTxPoolAPI(apiBackend),
    96  			Public:    true,
    97  		}, {
    98  			Namespace: "debug",
    99  			Version:   "1.0",
   100  			Service:   NewPublicDebugAPI(apiBackend),
   101  			Public:    true,
   102  		}, {
   103  			Namespace: "debug",
   104  			Version:   "1.0",
   105  			Service:   NewPrivateDebugAPI(apiBackend),
   106  		}, {
   107  			Namespace: "eth",
   108  			Version:   "1.0",
   109  			Service:   NewPublicAccountAPI(apiBackend.AccountManager()),
   110  			Public:    true,
   111  		}, {
   112  			Namespace: "personal",
   113  			Version:   "1.0",
   114  			Service:   NewPrivateAccountAPI(apiBackend),
   115  			Public:    false,
   116  		},
   117  	}
   118  }