github.com/Goplush/go-ethereum@v0.0.0-20191031044858-21506be82b68/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/Fantom-foundation/go-ethereum/accounts"
    25  	"github.com/Fantom-foundation/go-ethereum/common"
    26  	"github.com/Fantom-foundation/go-ethereum/core"
    27  	"github.com/Fantom-foundation/go-ethereum/core/bloombits"
    28  	"github.com/Fantom-foundation/go-ethereum/core/state"
    29  	"github.com/Fantom-foundation/go-ethereum/core/types"
    30  	"github.com/Fantom-foundation/go-ethereum/core/vm"
    31  	"github.com/Fantom-foundation/go-ethereum/eth/downloader"
    32  	"github.com/Fantom-foundation/go-ethereum/ethdb"
    33  	"github.com/Fantom-foundation/go-ethereum/event"
    34  	"github.com/Fantom-foundation/go-ethereum/params"
    35  	"github.com/Fantom-foundation/go-ethereum/rpc"
    36  )
    37  
    38  // Backend interface provides the common API services (that are provided by
    39  // both full and light clients) with access to necessary functions.
    40  type Backend interface {
    41  	// General Ethereum API
    42  	Downloader() *downloader.Downloader
    43  	ProtocolVersion() int
    44  	SuggestPrice(ctx context.Context) (*big.Int, error)
    45  	ChainDb() ethdb.Database
    46  	EventMux() *event.TypeMux
    47  	AccountManager() *accounts.Manager
    48  	ExtRPCEnabled() bool
    49  	RPCGasCap() *big.Int // global gas cap for eth_call over rpc: DoS protection
    50  
    51  	// Blockchain API
    52  	SetHead(number uint64)
    53  	HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error)
    54  	HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error)
    55  	HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error)
    56  	BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error)
    57  	BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error)
    58  	BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error)
    59  	StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error)
    60  	StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error)
    61  	GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error)
    62  	GetTd(hash common.Hash) *big.Int
    63  	GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header) (*vm.EVM, func() error, error)
    64  	SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription
    65  	SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription
    66  	SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription
    67  
    68  	// Transaction pool API
    69  	SendTx(ctx context.Context, signedTx *types.Transaction) error
    70  	GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error)
    71  	GetPoolTransactions() (types.Transactions, error)
    72  	GetPoolTransaction(txHash common.Hash) *types.Transaction
    73  	GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error)
    74  	Stats() (pending int, queued int)
    75  	TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions)
    76  	SubscribeNewTxsEvent(chan<- core.NewTxsEvent) event.Subscription
    77  
    78  	// Filter API
    79  	BloomStatus() (uint64, uint64)
    80  	GetLogs(ctx context.Context, blockHash common.Hash) ([][]*types.Log, error)
    81  	ServiceFilter(ctx context.Context, session *bloombits.MatcherSession)
    82  	SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription
    83  	SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription
    84  
    85  	ChainConfig() *params.ChainConfig
    86  	CurrentBlock() *types.Block
    87  }
    88  
    89  func GetAPIs(apiBackend Backend) []rpc.API {
    90  	nonceLock := new(AddrLocker)
    91  	return []rpc.API{
    92  		{
    93  			Namespace: "eth",
    94  			Version:   "1.0",
    95  			Service:   NewPublicEthereumAPI(apiBackend),
    96  			Public:    true,
    97  		}, {
    98  			Namespace: "eth",
    99  			Version:   "1.0",
   100  			Service:   NewPublicBlockChainAPI(apiBackend),
   101  			Public:    true,
   102  		}, {
   103  			Namespace: "eth",
   104  			Version:   "1.0",
   105  			Service:   NewPublicTransactionPoolAPI(apiBackend, nonceLock),
   106  			Public:    true,
   107  		}, {
   108  			Namespace: "txpool",
   109  			Version:   "1.0",
   110  			Service:   NewPublicTxPoolAPI(apiBackend),
   111  			Public:    true,
   112  		}, {
   113  			Namespace: "debug",
   114  			Version:   "1.0",
   115  			Service:   NewPublicDebugAPI(apiBackend),
   116  			Public:    true,
   117  		}, {
   118  			Namespace: "debug",
   119  			Version:   "1.0",
   120  			Service:   NewPrivateDebugAPI(apiBackend),
   121  		}, {
   122  			Namespace: "eth",
   123  			Version:   "1.0",
   124  			Service:   NewPublicAccountAPI(apiBackend.AccountManager()),
   125  			Public:    true,
   126  		}, {
   127  			Namespace: "personal",
   128  			Version:   "1.0",
   129  			Service:   NewPrivateAccountAPI(apiBackend, nonceLock),
   130  			Public:    false,
   131  		},
   132  	}
   133  }