backend.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. // Package ethapi implements the general Ethereum API functions.
  17. package ethapi
  18. import (
  19. "context"
  20. "github.com/ethereum/go-ethereum/accounts"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/consensus"
  23. "github.com/ethereum/go-ethereum/core"
  24. "github.com/ethereum/go-ethereum/core/bloombits"
  25. "github.com/ethereum/go-ethereum/core/state"
  26. "github.com/ethereum/go-ethereum/core/types"
  27. "github.com/ethereum/go-ethereum/core/vm"
  28. "github.com/ethereum/go-ethereum/eth/downloader"
  29. "github.com/ethereum/go-ethereum/ethdb"
  30. "github.com/ethereum/go-ethereum/event"
  31. "github.com/ethereum/go-ethereum/params"
  32. "github.com/ethereum/go-ethereum/rpc"
  33. "math/big"
  34. )
  35. // Backend interface provides the common API services (that are provided by
  36. // both full and light clients) with access to necessary functions.
  37. type Backend interface {
  38. // General Ethereum API
  39. Downloader() *downloader.Downloader
  40. SuggestPrice(ctx context.Context) (*big.Int, error)
  41. Chain() *core.BlockChain
  42. ChainDb() ethdb.Database
  43. AccountManager() *accounts.Manager
  44. ExtRPCEnabled() bool
  45. RPCGasCap() uint64 // global gas cap for eth_call over rpc: DoS protection
  46. RPCTxFeeCap() float64 // global tx fee cap for all transaction related APIs
  47. UnprotectedAllowed() bool // allows only for EIP155 transactions.
  48. // Blockchain API
  49. SetHead(number uint64)
  50. HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error)
  51. HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error)
  52. HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error)
  53. CurrentHeader() *types.Header
  54. CurrentBlock() *types.Block
  55. BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error)
  56. BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error)
  57. BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error)
  58. StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error)
  59. StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error)
  60. GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error)
  61. GetTd(ctx context.Context, hash common.Hash) *big.Int
  62. GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmConfig *vm.Config, blockContext *vm.BlockContext) (*vm.EVM, func() error, error)
  63. SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription
  64. SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription
  65. SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription
  66. // Transaction pool API
  67. SendTx(ctx context.Context, signedTx *types.Transaction) error
  68. SendTxFast(ctx context.Context, signedTx *types.Transaction) error
  69. SendTransactionsFast(txs types.Transactions)
  70. GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error)
  71. GetPoolTransactions() (types.Transactions, error)
  72. GetPoolTransaction(txHash common.Hash) *types.Transaction
  73. GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error)
  74. Stats() (pending int, queued int)
  75. TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions)
  76. SubscribeNewTxsEvent(chan<- core.NewTxsEvent) event.Subscription
  77. // Filter API
  78. BloomStatus() (uint64, uint64)
  79. GetLogs(ctx context.Context, blockHash common.Hash) ([][]*types.Log, error)
  80. ServiceFilter(ctx context.Context, session *bloombits.MatcherSession)
  81. SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription
  82. SubscribePendingLogsEvent(ch chan<- []*types.Log) event.Subscription
  83. SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription
  84. ChainConfig() *params.ChainConfig
  85. Engine() consensus.Engine
  86. }
  87. func GetAPIs(apiBackend Backend) []rpc.API {
  88. nonceLock := new(AddrLocker)
  89. return []rpc.API{
  90. {
  91. Namespace: "eth",
  92. Version: "1.0",
  93. Service: NewPublicEthereumAPI(apiBackend),
  94. Public: true,
  95. }, {
  96. Namespace: "eth",
  97. Version: "1.0",
  98. Service: NewPublicBlockChainAPI(apiBackend),
  99. Public: true,
  100. }, {
  101. Namespace: "eth",
  102. Version: "1.0",
  103. Service: NewPublicTransactionPoolAPI(apiBackend, nonceLock),
  104. Public: true,
  105. }, {
  106. Namespace: "txpool",
  107. Version: "1.0",
  108. Service: NewPublicTxPoolAPI(apiBackend),
  109. Public: true,
  110. }, {
  111. Namespace: "debug",
  112. Version: "1.0",
  113. Service: NewPublicDebugAPI(apiBackend),
  114. Public: true,
  115. }, {
  116. Namespace: "debug",
  117. Version: "1.0",
  118. Service: NewPrivateDebugAPI(apiBackend),
  119. }, {
  120. Namespace: "eth",
  121. Version: "1.0",
  122. Service: NewPublicAccountAPI(apiBackend.AccountManager()),
  123. Public: true,
  124. }, {
  125. Namespace: "personal",
  126. Version: "1.0",
  127. Service: NewPrivateAccountAPI(apiBackend, nonceLock),
  128. Public: false,
  129. }, {
  130. Namespace: "eth2",
  131. Version: "1.0",
  132. Service: NewEthereum2API(apiBackend),
  133. Public: true,
  134. }, {
  135. Namespace: "zdy",
  136. Version: "1.0",
  137. Service: NewPublicZdyAPI(apiBackend),
  138. Public: true,
  139. },
  140. }
  141. }