github.com/ethxdao/go-ethereum@v0.0.0-20221218102228-5ae34a9cc189/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  	"time"
    24  
    25  	"github.com/ethxdao/go-ethereum/accounts"
    26  	"github.com/ethxdao/go-ethereum/common"
    27  	"github.com/ethxdao/go-ethereum/consensus"
    28  	"github.com/ethxdao/go-ethereum/core"
    29  	"github.com/ethxdao/go-ethereum/core/state"
    30  	"github.com/ethxdao/go-ethereum/core/types"
    31  	"github.com/ethxdao/go-ethereum/core/vm"
    32  	"github.com/ethxdao/go-ethereum/eth/filters"
    33  	"github.com/ethxdao/go-ethereum/ethdb"
    34  	"github.com/ethxdao/go-ethereum/event"
    35  	"github.com/ethxdao/go-ethereum/params"
    36  	"github.com/ethxdao/go-ethereum/rpc"
    37  )
    38  
    39  // Backend interface provides the common API services (that are provided by
    40  // both full and light clients) with access to necessary functions.
    41  type Backend interface {
    42  	// General Ethereum API
    43  	SyncProgress() ethereum.SyncProgress
    44  
    45  	SuggestGasTipCap(ctx context.Context) (*big.Int, error)
    46  	FeeHistory(ctx context.Context, blockCount int, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (*big.Int, [][]*big.Int, []*big.Int, []float64, error)
    47  	ChainDb() ethdb.Database
    48  	AccountManager() *accounts.Manager
    49  	ExtRPCEnabled() bool
    50  	RPCGasCap() uint64            // global gas cap for eth_call over rpc: DoS protection
    51  	RPCEVMTimeout() time.Duration // global timeout for eth_call over rpc: DoS protection
    52  	RPCTxFeeCap() float64         // global tx fee cap for all transaction related APIs
    53  	UnprotectedAllowed() bool     // allows only for EIP155 transactions.
    54  
    55  	// Blockchain API
    56  	SetHead(number uint64)
    57  	HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error)
    58  	HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error)
    59  	HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error)
    60  	CurrentHeader() *types.Header
    61  	CurrentBlock() *types.Block
    62  	BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error)
    63  	BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error)
    64  	BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error)
    65  	StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error)
    66  	StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error)
    67  	PendingBlockAndReceipts() (*types.Block, types.Receipts)
    68  	GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error)
    69  	GetTd(ctx context.Context, hash common.Hash) *big.Int
    70  	GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmConfig *vm.Config) (*vm.EVM, func() error, error)
    71  	SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription
    72  	SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription
    73  	SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription
    74  
    75  	// Transaction pool API
    76  	SendTx(ctx context.Context, signedTx *types.Transaction) error
    77  	GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error)
    78  	GetPoolTransactions() (types.Transactions, error)
    79  	GetPoolTransaction(txHash common.Hash) *types.Transaction
    80  	GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error)
    81  	Stats() (pending int, queued int)
    82  	TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions)
    83  	TxPoolContentFrom(addr common.Address) (types.Transactions, types.Transactions)
    84  	SubscribeNewTxsEvent(chan<- core.NewTxsEvent) event.Subscription
    85  
    86  	ChainConfig() *params.ChainConfig
    87  	Engine() consensus.Engine
    88  
    89  	// eth/filters needs to be initialized from this backend type, so methods needed by
    90  	// it must also be included here.
    91  	filters.Backend
    92  }
    93  
    94  func GetAPIs(apiBackend Backend) []rpc.API {
    95  	nonceLock := new(AddrLocker)
    96  	return []rpc.API{
    97  		{
    98  			Namespace: "eth",
    99  			Service:   NewEthereumAPI(apiBackend),
   100  		}, {
   101  			Namespace: "eth",
   102  			Service:   NewBlockChainAPI(apiBackend),
   103  		}, {
   104  			Namespace: "eth",
   105  			Service:   NewTransactionAPI(apiBackend, nonceLock),
   106  		}, {
   107  			Namespace: "txpool",
   108  			Service:   NewTxPoolAPI(apiBackend),
   109  		}, {
   110  			Namespace: "debug",
   111  			Service:   NewDebugAPI(apiBackend),
   112  		}, {
   113  			Namespace: "eth",
   114  			Service:   NewEthereumAccountAPI(apiBackend.AccountManager()),
   115  		}, {
   116  			Namespace: "personal",
   117  			Service:   NewPersonalAccountAPI(apiBackend, nonceLock),
   118  		},
   119  	}
   120  }