github.com/palisadeinc/bor@v0.0.0-20230615125219-ab7196213d15/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/ethereum/go-ethereum"
    26  	"github.com/ethereum/go-ethereum/accounts"
    27  	"github.com/ethereum/go-ethereum/common"
    28  	"github.com/ethereum/go-ethereum/consensus"
    29  	"github.com/ethereum/go-ethereum/core"
    30  	"github.com/ethereum/go-ethereum/core/bloombits"
    31  	"github.com/ethereum/go-ethereum/core/state"
    32  	"github.com/ethereum/go-ethereum/core/types"
    33  	"github.com/ethereum/go-ethereum/core/vm"
    34  	"github.com/ethereum/go-ethereum/ethdb"
    35  	"github.com/ethereum/go-ethereum/event"
    36  	"github.com/ethereum/go-ethereum/params"
    37  	"github.com/ethereum/go-ethereum/rpc"
    38  )
    39  
    40  // Backend interface provides the common API services (that are provided by
    41  // both full and light clients) with access to necessary functions.
    42  type Backend interface {
    43  	// General Ethereum API
    44  	SyncProgress() ethereum.SyncProgress
    45  
    46  	SuggestGasTipCap(ctx context.Context) (*big.Int, error)
    47  	FeeHistory(ctx context.Context, blockCount int, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (*big.Int, [][]*big.Int, []*big.Int, []float64, error)
    48  	ChainDb() ethdb.Database
    49  	AccountManager() *accounts.Manager
    50  	ExtRPCEnabled() bool
    51  	RPCGasCap() uint64             // global gas cap for eth_call over rpc: DoS protection
    52  	RPCRpcReturnDataLimit() uint64 // Maximum size (in bytes) a result of an rpc request could have
    53  	RPCEVMTimeout() time.Duration  // global timeout for eth_call over rpc: DoS protection
    54  	RPCTxFeeCap() float64          // global tx fee cap for all transaction related APIs
    55  	UnprotectedAllowed() bool      // allows only for EIP155 transactions.
    56  
    57  	// Blockchain API
    58  	SetHead(number uint64)
    59  	HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error)
    60  	HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error)
    61  	HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error)
    62  	CurrentHeader() *types.Header
    63  	CurrentBlock() *types.Block
    64  	BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error)
    65  	BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error)
    66  	BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error)
    67  	StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error)
    68  	StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error)
    69  	GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error)
    70  	GetTd(ctx context.Context, hash common.Hash) *big.Int
    71  	GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmConfig *vm.Config) (*vm.EVM, func() error, error)
    72  	SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription
    73  	SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription
    74  	SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription
    75  
    76  	// Transaction pool API
    77  	SendTx(ctx context.Context, signedTx *types.Transaction) error
    78  	GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error)
    79  	GetPoolTransactions() (types.Transactions, error)
    80  	GetPoolTransaction(txHash common.Hash) *types.Transaction
    81  	GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error)
    82  	Stats() (pending int, queued int)
    83  	TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions)
    84  	TxPoolContentFrom(addr common.Address) (types.Transactions, types.Transactions)
    85  	SubscribeNewTxsEvent(chan<- core.NewTxsEvent) event.Subscription
    86  
    87  	// Filter API
    88  	BloomStatus() (uint64, uint64)
    89  	GetLogs(ctx context.Context, blockHash common.Hash) ([][]*types.Log, error)
    90  	ServiceFilter(ctx context.Context, session *bloombits.MatcherSession)
    91  	SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription
    92  	SubscribePendingLogsEvent(ch chan<- []*types.Log) event.Subscription
    93  	SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription
    94  
    95  	// Bor related APIs
    96  	SubscribeStateSyncEvent(ch chan<- core.StateSyncEvent) event.Subscription
    97  	GetRootHash(ctx context.Context, starBlockNr uint64, endBlockNr uint64) (string, error)
    98  	GetBorBlockReceipt(ctx context.Context, hash common.Hash) (*types.Receipt, error)
    99  	GetBorBlockLogs(ctx context.Context, hash common.Hash) ([]*types.Log, error)
   100  	GetBorBlockTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error)
   101  	GetBorBlockTransactionWithBlockHash(ctx context.Context, txHash common.Hash, blockHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error)
   102  	SubscribeChain2HeadEvent(ch chan<- core.Chain2HeadEvent) event.Subscription
   103  	GetCheckpointWhitelist() map[uint64]common.Hash
   104  	PurgeCheckpointWhitelist()
   105  
   106  	ChainConfig() *params.ChainConfig
   107  	Engine() consensus.Engine
   108  }
   109  
   110  func GetAPIs(apiBackend Backend) []rpc.API {
   111  	nonceLock := new(AddrLocker)
   112  	return []rpc.API{
   113  		{
   114  			Namespace: "eth",
   115  			Version:   "1.0",
   116  			Service:   NewPublicEthereumAPI(apiBackend),
   117  			Public:    true,
   118  		}, {
   119  			Namespace: "eth",
   120  			Version:   "1.0",
   121  			Service:   NewPublicBlockChainAPI(apiBackend),
   122  			Public:    true,
   123  		}, {
   124  			Namespace: "eth",
   125  			Version:   "1.0",
   126  			Service:   NewPublicTransactionPoolAPI(apiBackend, nonceLock),
   127  			Public:    true,
   128  		}, {
   129  			Namespace: "txpool",
   130  			Version:   "1.0",
   131  			Service:   NewPublicTxPoolAPI(apiBackend),
   132  			Public:    true,
   133  		}, {
   134  			Namespace: "debug",
   135  			Version:   "1.0",
   136  			Service:   NewPublicDebugAPI(apiBackend),
   137  			Public:    true,
   138  		}, {
   139  			Namespace: "debug",
   140  			Version:   "1.0",
   141  			Service:   NewPrivateDebugAPI(apiBackend),
   142  		}, {
   143  			Namespace: "eth",
   144  			Version:   "1.0",
   145  			Service:   NewPublicAccountAPI(apiBackend.AccountManager()),
   146  			Public:    true,
   147  		}, {
   148  			Namespace: "personal",
   149  			Version:   "1.0",
   150  			Service:   NewPrivateAccountAPI(apiBackend, nonceLock),
   151  			Public:    false,
   152  		},
   153  	}
   154  }