github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/internal/ethapi/backend.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 12:09:40</date>
    10  //</624342641303097344>
    11  
    12  
    13  //包ethapi实现一般的以太坊API功能。
    14  package ethapi
    15  
    16  import (
    17  	"context"
    18  	"math/big"
    19  
    20  	"github.com/ethereum/go-ethereum/accounts"
    21  	"github.com/ethereum/go-ethereum/common"
    22  	"github.com/ethereum/go-ethereum/core"
    23  	"github.com/ethereum/go-ethereum/core/state"
    24  	"github.com/ethereum/go-ethereum/core/types"
    25  	"github.com/ethereum/go-ethereum/core/vm"
    26  	"github.com/ethereum/go-ethereum/eth/downloader"
    27  	"github.com/ethereum/go-ethereum/ethdb"
    28  	"github.com/ethereum/go-ethereum/event"
    29  	"github.com/ethereum/go-ethereum/params"
    30  	"github.com/ethereum/go-ethereum/rpc"
    31  )
    32  
    33  //后端接口提供公共API服务(由
    34  //全功能和轻功能客户机)。
    35  type Backend interface {
    36  //通用以太坊API
    37  	Downloader() *downloader.Downloader
    38  	ProtocolVersion() int
    39  	SuggestPrice(ctx context.Context) (*big.Int, error)
    40  	ChainDb() ethdb.Database
    41  	EventMux() *event.TypeMux
    42  	AccountManager() *accounts.Manager
    43  
    44  //块链API
    45  	SetHead(number uint64)
    46  	HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error)
    47  	BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error)
    48  	StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*state.StateDB, *types.Header, error)
    49  	GetBlock(ctx context.Context, blockHash common.Hash) (*types.Block, error)
    50  	GetReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error)
    51  	GetTd(blockHash common.Hash) *big.Int
    52  	GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmCfg vm.Config) (*vm.EVM, func() error, error)
    53  	SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription
    54  	SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription
    55  //subscribeChainsideEvent(ch chan<-core.chainsideEvent)事件。订阅
    56  
    57  //TXPL API
    58  	SendTx(ctx context.Context, signedTx *types.Transaction) error
    59  	GetPoolTransactions() (types.Transactions, error)
    60  	GetPoolTransaction(txHash common.Hash) *types.Transaction
    61  	GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error)
    62  	Stats() (pending int, queued int)
    63  	TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions)
    64  	SubscribeNewTxsEvent(chan<- core.NewTxsEvent) event.Subscription
    65  
    66  	ChainConfig() *params.ChainConfig
    67  	CurrentBlock() *types.Block
    68  }
    69  
    70  func GetAPIs(apiBackend Backend) []rpc.API {
    71  	nonceLock := new(AddrLocker)
    72  	return []rpc.API{
    73  		{
    74  			Namespace: "eth",
    75  			Version:   "1.0",
    76  			Service:   NewPublicEthereumAPI(apiBackend),
    77  			Public:    true,
    78  		}, {
    79  			Namespace: "eth",
    80  			Version:   "1.0",
    81  			Service:   NewPublicBlockChainAPI(apiBackend),
    82  			Public:    true,
    83  		}, {
    84  			Namespace: "eth",
    85  			Version:   "1.0",
    86  			Service:   NewPublicTransactionPoolAPI(apiBackend, nonceLock),
    87  			Public:    true,
    88  		}, {
    89  			Namespace: "txpool",
    90  			Version:   "1.0",
    91  			Service:   NewPublicTxPoolAPI(apiBackend),
    92  			Public:    true,
    93  		}, {
    94  			Namespace: "debug",
    95  			Version:   "1.0",
    96  			Service:   NewPublicDebugAPI(apiBackend),
    97  			Public:    true,
    98  		}, {
    99  			Namespace: "debug",
   100  			Version:   "1.0",
   101  			Service:   NewPrivateDebugAPI(apiBackend),
   102  		}, {
   103  			Namespace: "eth",
   104  			Version:   "1.0",
   105  			Service:   NewPublicAccountAPI(apiBackend.AccountManager()),
   106  			Public:    true,
   107  		}, {
   108  			Namespace: "personal",
   109  			Version:   "1.0",
   110  			Service:   NewPrivateAccountAPI(apiBackend, nonceLock),
   111  			Public:    false,
   112  		},
   113  	}
   114  }
   115