github.com/m3shine/gochain@v2.2.26+incompatible/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  
    24  	"github.com/gochain-io/gochain/accounts"
    25  	"github.com/gochain-io/gochain/common"
    26  	"github.com/gochain-io/gochain/core"
    27  	"github.com/gochain-io/gochain/core/state"
    28  	"github.com/gochain-io/gochain/core/types"
    29  	"github.com/gochain-io/gochain/core/vm"
    30  	"github.com/gochain-io/gochain/eth/downloader"
    31  	"github.com/gochain-io/gochain/event"
    32  	"github.com/gochain-io/gochain/params"
    33  	"github.com/gochain-io/gochain/rpc"
    34  )
    35  
    36  // Backend interface provides the common API services (that are provided by
    37  // both full and light clients) with access to necessary functions.
    38  type Backend interface {
    39  	// General Ethereum API
    40  	Downloader() *downloader.Downloader
    41  	ProtocolVersion() int
    42  	SuggestPrice(ctx context.Context) (*big.Int, error)
    43  	ChainDb() common.Database
    44  	EventMux() *event.TypeMux
    45  	AccountManager() *accounts.Manager
    46  
    47  	// BlockChain API
    48  	SetHead(number uint64)
    49  	HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error)
    50  	BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error)
    51  	StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*state.StateDB, *types.Header, error)
    52  	GetBlock(ctx context.Context, blockHash common.Hash) (*types.Block, error)
    53  	GetReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error)
    54  	GetTd(blockHash common.Hash) *big.Int
    55  	GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmCfg vm.Config) (*vm.EVM, error)
    56  	SubscribeChainEvent(ch chan<- core.ChainEvent, name string)
    57  	UnsubscribeChainEvent(ch chan<- core.ChainEvent)
    58  	SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent, name string)
    59  	UnsubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent)
    60  	SubscribeChainSideEvent(ch chan<- core.ChainSideEvent, name string)
    61  	UnsubscribeChainSideEvent(ch chan<- core.ChainSideEvent)
    62  
    63  	// TxPool API
    64  	SendTx(ctx context.Context, signedTx *types.Transaction) error
    65  	GetPoolTransactions() types.Transactions
    66  	GetPoolTransaction(txHash common.Hash) *types.Transaction
    67  	GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error)
    68  	Stats() (pending int, queued int)
    69  	TxPoolContent(context.Context) (map[common.Address]types.Transactions, map[common.Address]types.Transactions)
    70  	SubscribeNewTxsEvent(chan<- core.NewTxsEvent, string)
    71  	UnsubscribeNewTxsEvent(chan<- core.NewTxsEvent)
    72  
    73  	ChainConfig() *params.ChainConfig
    74  	CurrentBlock() *types.Block
    75  	// InitialSupply returns the initial total supply from the genesis allocation,
    76  	// or nil if a custom genesis is not available.
    77  	InitialSupply() *big.Int
    78  	// GenesisAlloc returns the initial genesis allocation, or nil if a custom genesis is not available.
    79  	GenesisAlloc() core.GenesisAlloc
    80  }
    81  
    82  func GetAPIs(apiBackend Backend) []rpc.API {
    83  	nonceLock := newAddrLocker()
    84  	return []rpc.API{
    85  		{
    86  			Namespace: "eth",
    87  			Version:   "1.0",
    88  			Service:   NewPublicEthereumAPI(apiBackend),
    89  			Public:    true,
    90  		}, {
    91  			Namespace: "eth",
    92  			Version:   "1.0",
    93  			Service:   NewPublicBlockChainAPI(apiBackend),
    94  			Public:    true,
    95  		}, {
    96  			Namespace: "eth",
    97  			Version:   "1.0",
    98  			Service:   NewPublicTransactionPoolAPI(apiBackend, nonceLock),
    99  			Public:    true,
   100  		}, {
   101  			Namespace: "txpool",
   102  			Version:   "1.0",
   103  			Service:   NewPublicTxPoolAPI(apiBackend),
   104  			Public:    true,
   105  		}, {
   106  			Namespace: "debug",
   107  			Version:   "1.0",
   108  			Service:   NewPublicDebugAPI(apiBackend),
   109  			Public:    true,
   110  		}, {
   111  			Namespace: "debug",
   112  			Version:   "1.0",
   113  			Service:   NewPrivateDebugAPI(apiBackend),
   114  		}, {
   115  			Namespace: "eth",
   116  			Version:   "1.0",
   117  			Service:   NewPublicAccountAPI(apiBackend.AccountManager()),
   118  			Public:    true,
   119  		}, {
   120  			Namespace: "personal",
   121  			Version:   "1.0",
   122  			Service:   NewPrivateAccountAPI(apiBackend, nonceLock),
   123  			Public:    false,
   124  		},
   125  	}
   126  }