backend.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. "math/big"
  21. "github.com/ethereum/go-ethereum/accounts"
  22. "github.com/ethereum/go-ethereum/common"
  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. )
  34. // Backend interface provides the common API services (that are provided by
  35. // both full and light clients) with access to necessary functions.
  36. type Backend interface {
  37. // General Ethereum API
  38. Downloader() *downloader.Downloader
  39. ProtocolVersion() int
  40. SuggestPrice(ctx context.Context) (*big.Int, error)
  41. ChainDb() ethdb.Database
  42. AccountManager() *accounts.Manager
  43. ExtRPCEnabled() bool
  44. RPCTxFeeCap() float64 // global tx fee cap for all transaction related APIs
  45. RPCGasCap() uint64 // global gas cap for eth_call over rpc: DoS protection
  46. // Blockchain API
  47. SetHead(number uint64)
  48. HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error)
  49. HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error)
  50. HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error)
  51. BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error)
  52. BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error)
  53. BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error)
  54. StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error)
  55. StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error)
  56. GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error)
  57. GetTd(hash common.Hash) *big.Int
  58. GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header) (*vm.EVM, func() error, error)
  59. SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription
  60. SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription
  61. SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription
  62. // Transaction pool API
  63. SendTx(ctx context.Context, signedTx *types.Transaction) error
  64. GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error)
  65. GetPoolTransactions() (types.Transactions, error)
  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() (map[common.Address]types.Transactions, map[common.Address]types.Transactions)
  70. SubscribeNewTxsEvent(chan<- core.NewTxsEvent) event.Subscription
  71. // Filter API
  72. BloomStatus() (uint64, uint64)
  73. GetLogs(ctx context.Context, blockHash common.Hash) ([][]*types.Log, error)
  74. ServiceFilter(ctx context.Context, session *bloombits.MatcherSession)
  75. SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription
  76. SubscribePendingLogsEvent(ch chan<- []*types.Log) event.Subscription
  77. SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription
  78. ChainConfig() *params.ChainConfig
  79. CurrentBlock() *types.Block
  80. }
  81. func GetAPIs(apiBackend Backend) []rpc.API {
  82. nonceLock := new(AddrLocker)
  83. return []rpc.API{
  84. {
  85. Namespace: "eth",
  86. Version: "1.0",
  87. Service: NewPublicEthereumAPI(apiBackend),
  88. Public: true,
  89. }, {
  90. Namespace: "eth",
  91. Version: "1.0",
  92. Service: NewPublicBlockChainAPI(apiBackend),
  93. Public: true,
  94. }, {
  95. Namespace: "eth",
  96. Version: "1.0",
  97. Service: NewPublicTransactionPoolAPI(apiBackend, nonceLock),
  98. Public: true,
  99. }, {
  100. Namespace: "txpool",
  101. Version: "1.0",
  102. Service: NewPublicTxPoolAPI(apiBackend),
  103. Public: true,
  104. }, {
  105. Namespace: "debug",
  106. Version: "1.0",
  107. Service: NewPublicDebugAPI(apiBackend),
  108. Public: true,
  109. }, {
  110. Namespace: "debug",
  111. Version: "1.0",
  112. Service: NewPrivateDebugAPI(apiBackend),
  113. }, {
  114. Namespace: "eth",
  115. Version: "1.0",
  116. Service: NewPublicAccountAPI(apiBackend.AccountManager()),
  117. Public: true,
  118. }, {
  119. Namespace: "personal",
  120. Version: "1.0",
  121. Service: NewPrivateAccountAPI(apiBackend, nonceLock),
  122. Public: false,
  123. },
  124. }
  125. }