github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/internal/ethapi/backend.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar 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-aigar 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-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  // Package ethapi implements the general Ethereum API functions.
    19  package ethapi
    20  
    21  import (
    22  	"context"
    23  	"math/big"
    24  
    25  	"github.com/AigarNetwork/aigar/accounts"
    26  	"github.com/AigarNetwork/aigar/common"
    27  	"github.com/AigarNetwork/aigar/core"
    28  	"github.com/AigarNetwork/aigar/core/bloombits"
    29  	"github.com/AigarNetwork/aigar/core/state"
    30  	"github.com/AigarNetwork/aigar/core/types"
    31  	"github.com/AigarNetwork/aigar/core/vm"
    32  	"github.com/AigarNetwork/aigar/eth/downloader"
    33  	"github.com/AigarNetwork/aigar/ethdb"
    34  	"github.com/AigarNetwork/aigar/event"
    35  	"github.com/AigarNetwork/aigar/params"
    36  	"github.com/AigarNetwork/aigar/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  	EventMux() *event.TypeMux
    48  	AccountManager() *accounts.Manager
    49  	ExtRPCEnabled() bool
    50  	RPCGasCap() *big.Int // global gas cap 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) (*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  	SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription
    85  
    86  	ChainConfig() *params.ChainConfig
    87  	CurrentBlock() *types.Block
    88  }
    89  
    90  func GetAPIs(apiBackend Backend) []rpc.API {
    91  	nonceLock := new(AddrLocker)
    92  	return []rpc.API{
    93  		{
    94  			Namespace: "eth",
    95  			Version:   "1.0",
    96  			Service:   NewPublicEthereumAPI(apiBackend),
    97  			Public:    true,
    98  		}, {
    99  			Namespace: "eth",
   100  			Version:   "1.0",
   101  			Service:   NewPublicBlockChainAPI(apiBackend),
   102  			Public:    true,
   103  		}, {
   104  			Namespace: "eth",
   105  			Version:   "1.0",
   106  			Service:   NewPublicTransactionPoolAPI(apiBackend, nonceLock),
   107  			Public:    true,
   108  		}, {
   109  			Namespace: "txpool",
   110  			Version:   "1.0",
   111  			Service:   NewPublicTxPoolAPI(apiBackend),
   112  			Public:    true,
   113  		}, {
   114  			Namespace: "debug",
   115  			Version:   "1.0",
   116  			Service:   NewPublicDebugAPI(apiBackend),
   117  			Public:    true,
   118  		}, {
   119  			Namespace: "debug",
   120  			Version:   "1.0",
   121  			Service:   NewPrivateDebugAPI(apiBackend),
   122  		}, {
   123  			Namespace: "eth",
   124  			Version:   "1.0",
   125  			Service:   NewPublicAccountAPI(apiBackend.AccountManager()),
   126  			Public:    true,
   127  		}, {
   128  			Namespace: "personal",
   129  			Version:   "1.0",
   130  			Service:   NewPrivateAccountAPI(apiBackend, nonceLock),
   131  			Public:    false,
   132  		},
   133  	}
   134  }