github.com/klaytn/klaytn@v1.12.1/api/backend.go (about)

     1  // Modifications Copyright 2018 The klaytn Authors
     2  // Copyright 2015 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from internal/ethapi/backend.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package api
    22  
    23  import (
    24  	"context"
    25  	"math/big"
    26  	"time"
    27  
    28  	"github.com/klaytn/klaytn"
    29  	"github.com/klaytn/klaytn/accounts"
    30  	"github.com/klaytn/klaytn/blockchain"
    31  	"github.com/klaytn/klaytn/blockchain/state"
    32  	"github.com/klaytn/klaytn/blockchain/types"
    33  	"github.com/klaytn/klaytn/blockchain/vm"
    34  	"github.com/klaytn/klaytn/common"
    35  	"github.com/klaytn/klaytn/consensus"
    36  	"github.com/klaytn/klaytn/event"
    37  	"github.com/klaytn/klaytn/networks/rpc"
    38  	"github.com/klaytn/klaytn/params"
    39  	"github.com/klaytn/klaytn/storage/database"
    40  )
    41  
    42  // Backend interface provides the common API services (that are provided by
    43  // both full and light clients) with access to necessary functions.
    44  //
    45  //go:generate mockgen -destination=api/mocks/backend_mock.go github.com/klaytn/klaytn/api Backend
    46  type Backend interface {
    47  	// General Klaytn API
    48  	Progress() klaytn.SyncProgress
    49  	ProtocolVersion() int
    50  	SuggestPrice(ctx context.Context) (*big.Int, error)
    51  	SuggestTipCap(ctx context.Context) (*big.Int, error)
    52  	UpperBoundGasPrice(ctx context.Context) *big.Int
    53  	LowerBoundGasPrice(ctx context.Context) *big.Int
    54  	ChainDB() database.DBManager
    55  	EventMux() *event.TypeMux
    56  	AccountManager() accounts.AccountManager
    57  	RPCEVMTimeout() time.Duration // global timeout for eth/klay_call/estimateGas/estimateComputationCost
    58  	RPCGasCap() *big.Int          // global gas cap for eth/klay_call/estimateGas/estimateComputationCost
    59  	RPCTxFeeCap() float64         // global tx fee cap in eth_signTransaction
    60  	Engine() consensus.Engine
    61  	FeeHistory(ctx context.Context, blockCount int, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (*big.Int, [][]*big.Int, []*big.Int, []float64, error)
    62  
    63  	// BlockChain API
    64  	SetHead(number uint64) error
    65  	HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error)
    66  	HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error)
    67  	HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error)
    68  	BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error)
    69  	BlockByHash(ctx context.Context, blockHash common.Hash) (*types.Block, error)
    70  	BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error)
    71  	StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error)
    72  	StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error)
    73  	GetBlockReceipts(ctx context.Context, blockHash common.Hash) types.Receipts
    74  	GetTxLookupInfoAndReceipt(ctx context.Context, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, *types.Receipt)
    75  	GetTxAndLookupInfo(hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64)
    76  	GetTd(blockHash common.Hash) *big.Int
    77  	GetEVM(ctx context.Context, msg blockchain.Message, state *state.StateDB, header *types.Header, vmCfg vm.Config) (*vm.EVM, func() error, error)
    78  	SubscribeChainEvent(ch chan<- blockchain.ChainEvent) event.Subscription
    79  	SubscribeChainHeadEvent(ch chan<- blockchain.ChainHeadEvent) event.Subscription
    80  	SubscribeChainSideEvent(ch chan<- blockchain.ChainSideEvent) event.Subscription
    81  	IsParallelDBWrite() bool
    82  
    83  	IsSenderTxHashIndexingEnabled() bool
    84  
    85  	// TxPool API
    86  	SendTx(ctx context.Context, signedTx *types.Transaction) error
    87  	GetPoolTransactions() (types.Transactions, error)
    88  	GetPoolTransaction(txHash common.Hash) *types.Transaction
    89  	GetPoolNonce(ctx context.Context, addr common.Address) uint64
    90  	Stats() (pending int, queued int)
    91  	TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions)
    92  	SubscribeNewTxsEvent(chan<- blockchain.NewTxsEvent) event.Subscription
    93  
    94  	ChainConfig() *params.ChainConfig
    95  	CurrentBlock() *types.Block
    96  
    97  	GetTxAndLookupInfoInCache(hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64)
    98  	GetBlockReceiptsInCache(blockHash common.Hash) types.Receipts
    99  	GetTxLookupInfoAndReceiptInCache(Hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, *types.Receipt)
   100  }
   101  
   102  func GetAPIs(apiBackend Backend, disableUnsafeDebug bool) ([]rpc.API, *EthereumAPI) {
   103  	nonceLock := new(AddrLocker)
   104  
   105  	ethAPI := NewEthereumAPI()
   106  
   107  	publicKlayAPI := NewPublicKlayAPI(apiBackend)
   108  	publicBlockChainAPI := NewPublicBlockChainAPI(apiBackend)
   109  	publicTransactionPoolAPI := NewPublicTransactionPoolAPI(apiBackend, nonceLock)
   110  	publicAccountAPI := NewPublicAccountAPI(apiBackend.AccountManager())
   111  
   112  	ethAPI.SetPublicKlayAPI(publicKlayAPI)
   113  	ethAPI.SetPublicBlockChainAPI(publicBlockChainAPI)
   114  	ethAPI.SetPublicTransactionPoolAPI(publicTransactionPoolAPI)
   115  	ethAPI.SetPublicAccountAPI(publicAccountAPI)
   116  
   117  	rpcApi := []rpc.API{
   118  		{
   119  			Namespace: "klay",
   120  			Version:   "1.0",
   121  			Service:   publicKlayAPI,
   122  			Public:    true,
   123  		}, {
   124  			Namespace: "klay",
   125  			Version:   "1.0",
   126  			Service:   publicBlockChainAPI,
   127  			Public:    true,
   128  		}, {
   129  			Namespace: "klay",
   130  			Version:   "1.0",
   131  			Service:   publicTransactionPoolAPI,
   132  			Public:    true,
   133  		}, {
   134  			Namespace: "txpool",
   135  			Version:   "1.0",
   136  			Service:   NewPublicTxPoolAPI(apiBackend),
   137  			Public:    true,
   138  		}, {
   139  			Namespace: "debug",
   140  			Version:   "1.0",
   141  			Service:   NewPublicDebugAPI(apiBackend),
   142  			Public:    false,
   143  		}, {
   144  			Namespace: "klay",
   145  			Version:   "1.0",
   146  			Service:   publicAccountAPI,
   147  			Public:    true,
   148  		}, {
   149  			Namespace: "personal",
   150  			Version:   "1.0",
   151  			Service:   NewPrivateAccountAPI(apiBackend, nonceLock),
   152  			Public:    false,
   153  		}, {
   154  			Namespace: "debug",
   155  			Version:   "1.0",
   156  			Service:   NewPrivateDebugAPI(apiBackend),
   157  			Public:    false,
   158  			IPCOnly:   disableUnsafeDebug,
   159  		},
   160  	}
   161  
   162  	return rpcApi, ethAPI
   163  }