backend.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/state"
  25. "github.com/ethereum/go-ethereum/core/types"
  26. "github.com/ethereum/go-ethereum/core/vm"
  27. "github.com/ethereum/go-ethereum/eth/downloader"
  28. "github.com/ethereum/go-ethereum/ethdb"
  29. "github.com/ethereum/go-ethereum/event"
  30. "github.com/ethereum/go-ethereum/params"
  31. "github.com/ethereum/go-ethereum/rpc"
  32. )
  33. // Backend interface provides the common API services (that are provided by
  34. // both full and light clients) with access to necessary functions.
  35. type Backend interface {
  36. // General Ethereum 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. ExtRPCEnabled() bool
  44. RPCGasCap() *big.Int // global gas cap for eth_call over rpc: DoS protection
  45. // BlockChain API
  46. SetHead(number uint64)
  47. HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error)
  48. BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error)
  49. StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*state.StateDB, *types.Header, error)
  50. GetBlock(ctx context.Context, blockHash common.Hash) (*types.Block, error)
  51. GetReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error)
  52. GetTd(blockHash common.Hash) *big.Int
  53. GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header) (*vm.EVM, func() error, error)
  54. SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription
  55. SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription
  56. SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription
  57. // TxPool API
  58. SendTx(ctx context.Context, signedTx *types.Transaction) error
  59. GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error)
  60. GetPoolTransactions() (types.Transactions, error)
  61. GetPoolTransaction(txHash common.Hash) *types.Transaction
  62. GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error)
  63. Stats() (pending int, queued int)
  64. TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions)
  65. SubscribeNewTxsEvent(chan<- core.NewTxsEvent) event.Subscription
  66. ChainConfig() *params.ChainConfig
  67. CurrentBlock() *types.Block
  68. }
  69. func GetAPIs(apiBackend Backend) []rpc.API {
  70. nonceLock := new(AddrLocker)
  71. return []rpc.API{
  72. {
  73. Namespace: "eth",
  74. Version: "1.0",
  75. Service: NewPublicEthereumAPI(apiBackend),
  76. Public: true,
  77. }, {
  78. Namespace: "eth",
  79. Version: "1.0",
  80. Service: NewPublicBlockChainAPI(apiBackend),
  81. Public: true,
  82. }, {
  83. Namespace: "eth",
  84. Version: "1.0",
  85. Service: NewPublicTransactionPoolAPI(apiBackend, nonceLock),
  86. Public: true,
  87. }, {
  88. Namespace: "txpool",
  89. Version: "1.0",
  90. Service: NewPublicTxPoolAPI(apiBackend),
  91. Public: true,
  92. }, {
  93. Namespace: "debug",
  94. Version: "1.0",
  95. Service: NewPublicDebugAPI(apiBackend),
  96. Public: true,
  97. }, {
  98. Namespace: "debug",
  99. Version: "1.0",
  100. Service: NewPrivateDebugAPI(apiBackend),
  101. }, {
  102. Namespace: "eth",
  103. Version: "1.0",
  104. Service: NewPublicAccountAPI(apiBackend.AccountManager()),
  105. Public: true,
  106. }, {
  107. Namespace: "personal",
  108. Version: "1.0",
  109. Service: NewPrivateAccountAPI(apiBackend, nonceLock),
  110. Public: false,
  111. },
  112. }
  113. }