github.com/klaytn/klaytn@v1.10.2/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  //go:generate mockgen -destination=api/mocks/backend_mock.go github.com/klaytn/klaytn/api Backend
    43  // Backend interface provides the common API services (that are provided by
    44  // both full and light clients) with access to necessary functions.
    45  type Backend interface {
    46  	// General Klaytn API
    47  	Progress() klaytn.SyncProgress
    48  	ProtocolVersion() int
    49  	SuggestPrice(ctx context.Context) (*big.Int, error)
    50  	UpperBoundGasPrice(ctx context.Context) *big.Int
    51  	LowerBoundGasPrice(ctx context.Context) *big.Int
    52  	ChainDB() database.DBManager
    53  	EventMux() *event.TypeMux
    54  	AccountManager() accounts.AccountManager
    55  	RPCEVMTimeout() time.Duration // global timeout for klay_call
    56  	RPCGasCap() *big.Int          // global gas cap for klay_call over rpc: DoS protection
    57  	RPCTxFeeCap() float64         // global tx fee cap for all transaction related APIs
    58  	Engine() consensus.Engine
    59  	FeeHistory(ctx context.Context, blockCount int, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (*big.Int, [][]*big.Int, []*big.Int, []float64, error)
    60  
    61  	// BlockChain API
    62  	SetHead(number uint64)
    63  	HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error)
    64  	HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error)
    65  	HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error)
    66  	BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error)
    67  	BlockByHash(ctx context.Context, blockHash common.Hash) (*types.Block, error)
    68  	BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error)
    69  	StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error)
    70  	StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error)
    71  	GetBlockReceipts(ctx context.Context, blockHash common.Hash) types.Receipts
    72  	GetTxLookupInfoAndReceipt(ctx context.Context, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, *types.Receipt)
    73  	GetTxAndLookupInfo(hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64)
    74  	GetTd(blockHash common.Hash) *big.Int
    75  	GetEVM(ctx context.Context, msg blockchain.Message, state *state.StateDB, header *types.Header, vmCfg vm.Config) (*vm.EVM, func() error, error)
    76  	SubscribeChainEvent(ch chan<- blockchain.ChainEvent) event.Subscription
    77  	SubscribeChainHeadEvent(ch chan<- blockchain.ChainHeadEvent) event.Subscription
    78  	SubscribeChainSideEvent(ch chan<- blockchain.ChainSideEvent) event.Subscription
    79  	IsParallelDBWrite() bool
    80  
    81  	IsSenderTxHashIndexingEnabled() bool
    82  
    83  	// TxPool API
    84  	SendTx(ctx context.Context, signedTx *types.Transaction) error
    85  	GetPoolTransactions() (types.Transactions, error)
    86  	GetPoolTransaction(txHash common.Hash) *types.Transaction
    87  	GetPoolNonce(ctx context.Context, addr common.Address) uint64
    88  	Stats() (pending int, queued int)
    89  	TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions)
    90  	SubscribeNewTxsEvent(chan<- blockchain.NewTxsEvent) event.Subscription
    91  
    92  	ChainConfig() *params.ChainConfig
    93  	CurrentBlock() *types.Block
    94  
    95  	GetTxAndLookupInfoInCache(hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64)
    96  	GetBlockReceiptsInCache(blockHash common.Hash) types.Receipts
    97  	GetTxLookupInfoAndReceiptInCache(Hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, *types.Receipt)
    98  }
    99  
   100  func GetAPIs(apiBackend Backend, disableUnsafeDebug bool) ([]rpc.API, *EthereumAPI) {
   101  	nonceLock := new(AddrLocker)
   102  
   103  	ethAPI := NewEthereumAPI()
   104  
   105  	publicKlayAPI := NewPublicKlayAPI(apiBackend)
   106  	publicBlockChainAPI := NewPublicBlockChainAPI(apiBackend)
   107  	publicTransactionPoolAPI := NewPublicTransactionPoolAPI(apiBackend, nonceLock)
   108  	publicAccountAPI := NewPublicAccountAPI(apiBackend.AccountManager())
   109  
   110  	ethAPI.SetPublicKlayAPI(publicKlayAPI)
   111  	ethAPI.SetPublicBlockChainAPI(publicBlockChainAPI)
   112  	ethAPI.SetPublicTransactionPoolAPI(publicTransactionPoolAPI)
   113  	ethAPI.SetPublicAccountAPI(publicAccountAPI)
   114  
   115  	rpcApi := []rpc.API{
   116  		{
   117  			Namespace: "klay",
   118  			Version:   "1.0",
   119  			Service:   publicKlayAPI,
   120  			Public:    true,
   121  		}, {
   122  			Namespace: "klay",
   123  			Version:   "1.0",
   124  			Service:   publicBlockChainAPI,
   125  			Public:    true,
   126  		}, {
   127  			Namespace: "klay",
   128  			Version:   "1.0",
   129  			Service:   publicTransactionPoolAPI,
   130  			Public:    true,
   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:    false,
   141  		}, {
   142  			Namespace: "klay",
   143  			Version:   "1.0",
   144  			Service:   publicAccountAPI,
   145  			Public:    true,
   146  		}, {
   147  			Namespace: "personal",
   148  			Version:   "1.0",
   149  			Service:   NewPrivateAccountAPI(apiBackend, nonceLock),
   150  			Public:    false,
   151  		},
   152  	}
   153  	privateDebugApi := []rpc.API{
   154  		{
   155  			Namespace: "debug",
   156  			Version:   "1.0",
   157  			Service:   NewPrivateDebugAPI(apiBackend),
   158  			Public:    false,
   159  		},
   160  	}
   161  	if !disableUnsafeDebug {
   162  		rpcApi = append(rpcApi, privateDebugApi...)
   163  	}
   164  
   165  	return rpcApi, ethAPI
   166  }