github.com/ethereum-optimism/optimism/l2geth@v0.0.0-20230612200230-50b04ade19e3/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-optimism/optimism/l2geth/accounts"
    26  	"github.com/ethereum-optimism/optimism/l2geth/common"
    27  	"github.com/ethereum-optimism/optimism/l2geth/core"
    28  	"github.com/ethereum-optimism/optimism/l2geth/core/bloombits"
    29  	"github.com/ethereum-optimism/optimism/l2geth/core/state"
    30  	"github.com/ethereum-optimism/optimism/l2geth/core/types"
    31  	"github.com/ethereum-optimism/optimism/l2geth/core/vm"
    32  	"github.com/ethereum-optimism/optimism/l2geth/eth/downloader"
    33  	"github.com/ethereum-optimism/optimism/l2geth/ethdb"
    34  	"github.com/ethereum-optimism/optimism/l2geth/event"
    35  	"github.com/ethereum-optimism/optimism/l2geth/params"
    36  	"github.com/ethereum-optimism/optimism/l2geth/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  	Downloader() *downloader.Downloader
    44  	ProtocolVersion() int
    45  	SuggestPrice(ctx context.Context) (*big.Int, error)
    46  	ChainDb() ethdb.Database
    47  	AccountManager() *accounts.Manager
    48  	ExtRPCEnabled() bool
    49  	RPCGasCap() *big.Int          // global gas cap for eth_call over rpc: DoS protection
    50  	RPCEVMTimeout() time.Duration // global timeout (0=infinite) for eth_call over rpc: DoS protection
    51  
    52  	// Blockchain API
    53  	SetHead(number uint64)
    54  	HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error)
    55  	HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error)
    56  	HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error)
    57  	BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error)
    58  	BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error)
    59  	BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error)
    60  	StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error)
    61  	StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error)
    62  	GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error)
    63  	GetTd(hash common.Hash) *big.Int
    64  	GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmCfg *vm.Config) (*vm.EVM, func() error, error)
    65  	SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription
    66  	SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription
    67  	SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription
    68  
    69  	// Transaction pool API
    70  	SendTx(ctx context.Context, signedTx *types.Transaction) error
    71  	GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error)
    72  	GetPoolTransactions() (types.Transactions, error)
    73  	GetPoolTransaction(txHash common.Hash) *types.Transaction
    74  	GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error)
    75  	Stats() (pending int, queued int)
    76  	TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions)
    77  	SubscribeNewTxsEvent(chan<- core.NewTxsEvent) event.Subscription
    78  
    79  	// Filter API
    80  	BloomStatus() (uint64, uint64)
    81  	GetLogs(ctx context.Context, blockHash common.Hash) ([][]*types.Log, error)
    82  	ServiceFilter(ctx context.Context, session *bloombits.MatcherSession)
    83  	SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription
    84  	SubscribePendingLogsEvent(ch chan<- []*types.Log) event.Subscription
    85  	SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription
    86  
    87  	ChainConfig() *params.ChainConfig
    88  	CurrentBlock() *types.Block
    89  
    90  	// Optimism-specific API
    91  	IsVerifier() bool
    92  	IsSyncing() bool
    93  	GetEthContext() (uint64, uint64)
    94  	GetRollupContext() (uint64, uint64, uint64)
    95  	GasLimit() uint64
    96  	SuggestL1GasPrice(ctx context.Context) (*big.Int, error)
    97  	SetL1GasPrice(context.Context, *big.Int) error
    98  	SuggestL2GasPrice(context.Context) (*big.Int, error)
    99  	SetL2GasPrice(context.Context, *big.Int) error
   100  	IngestTransactions([]*types.Transaction) error
   101  	SequencerClientHttp() string
   102  }
   103  
   104  func GetAPIs(apiBackend Backend) []rpc.API {
   105  	nonceLock := new(AddrLocker)
   106  	return []rpc.API{
   107  		{
   108  			Namespace: "eth",
   109  			Version:   "1.0",
   110  			Service:   NewPublicEthereumAPI(apiBackend),
   111  			Public:    true,
   112  		}, {
   113  			Namespace: "eth",
   114  			Version:   "1.0",
   115  			Service:   NewPublicBlockChainAPI(apiBackend),
   116  			Public:    true,
   117  		}, {
   118  			Namespace: "eth",
   119  			Version:   "1.0",
   120  			Service:   NewPublicTransactionPoolAPI(apiBackend, nonceLock),
   121  			Public:    true,
   122  		}, {
   123  			Namespace: "rollup",
   124  			Version:   "1.0",
   125  			Service:   NewPublicRollupAPI(apiBackend),
   126  			Public:    true,
   127  		}, {
   128  			Namespace: "rollup_personal",
   129  			Version:   "1.0",
   130  			Service:   NewPrivateRollupAPI(apiBackend),
   131  		}, {
   132  			Namespace: "txpool",
   133  			Version:   "1.0",
   134  			Service:   NewPublicTxPoolAPI(apiBackend),
   135  			Public:    true,
   136  		}, {
   137  			Namespace: "debug",
   138  			Version:   "1.0",
   139  			Service:   NewPublicDebugAPI(apiBackend),
   140  			Public:    true,
   141  		}, {
   142  			Namespace: "debug",
   143  			Version:   "1.0",
   144  			Service:   NewPrivateDebugAPI(apiBackend),
   145  		}, {
   146  			Namespace: "eth",
   147  			Version:   "1.0",
   148  			Service:   NewPublicAccountAPI(apiBackend.AccountManager()),
   149  			Public:    true,
   150  		}, {
   151  			Namespace: "personal",
   152  			Version:   "1.0",
   153  			Service:   NewPrivateAccountAPI(apiBackend, nonceLock),
   154  			Public:    false,
   155  		},
   156  	}
   157  }