backend.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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/consensus"
  24. "github.com/ethereum/go-ethereum/core"
  25. "github.com/ethereum/go-ethereum/core/bloombits"
  26. "github.com/ethereum/go-ethereum/core/state"
  27. "github.com/ethereum/go-ethereum/core/types"
  28. "github.com/ethereum/go-ethereum/core/vm"
  29. "github.com/ethereum/go-ethereum/eth/downloader"
  30. "github.com/ethereum/go-ethereum/ethdb"
  31. "github.com/ethereum/go-ethereum/event"
  32. "github.com/ethereum/go-ethereum/params"
  33. "github.com/ethereum/go-ethereum/rpc"
  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. ChainDb() ethdb.Database
  42. AccountManager() *accounts.Manager
  43. ExtRPCEnabled() bool
  44. RPCGasCap() uint64 // global gas cap for eth_call over rpc: DoS protection
  45. RPCTxFeeCap() float64 // global tx fee cap for all transaction related APIs
  46. UnprotectedAllowed() bool // allows only for EIP155 transactions.
  47. // Blockchain API
  48. SetHead(number uint64)
  49. HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error)
  50. HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error)
  51. HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error)
  52. CurrentHeader() *types.Header
  53. CurrentBlock() *types.Block
  54. BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error)
  55. BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error)
  56. BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error)
  57. StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error)
  58. StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error)
  59. GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error)
  60. GetTd(ctx context.Context, hash common.Hash) *big.Int
  61. GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmConfig *vm.Config) (*vm.EVM, func() error, error)
  62. SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription
  63. SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription
  64. SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription
  65. // Transaction pool API
  66. SendTx(ctx context.Context, signedTx *types.Transaction) error
  67. GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error)
  68. GetPoolTransactions() (types.Transactions, error)
  69. GetPoolTransaction(txHash common.Hash) *types.Transaction
  70. GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error)
  71. Stats() (pending int, queued int)
  72. TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions)
  73. SubscribeNewTxsEvent(chan<- core.NewTxsEvent) event.Subscription
  74. // Filter API
  75. BloomStatus() (uint64, uint64)
  76. GetLogs(ctx context.Context, blockHash common.Hash) ([][]*types.Log, error)
  77. ServiceFilter(ctx context.Context, session *bloombits.MatcherSession)
  78. SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription
  79. SubscribePendingLogsEvent(ch chan<- []*types.Log) event.Subscription
  80. SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription
  81. ChainConfig() *params.ChainConfig
  82. Engine() consensus.Engine
  83. }
  84. func GetAPIs(apiBackend Backend) []rpc.API {
  85. nonceLock := new(AddrLocker)
  86. return []rpc.API{
  87. {
  88. Namespace: "eth",
  89. Version: "1.0",
  90. Service: NewPublicEthereumAPI(apiBackend),
  91. Public: true,
  92. }, {
  93. Namespace: "eth",
  94. Version: "1.0",
  95. Service: NewPublicBlockChainAPI(apiBackend),
  96. Public: true,
  97. }, {
  98. Namespace: "eth",
  99. Version: "1.0",
  100. Service: NewPublicTransactionPoolAPI(apiBackend, nonceLock),
  101. Public: true,
  102. }, {
  103. Namespace: "txpool",
  104. Version: "1.0",
  105. Service: NewPublicTxPoolAPI(apiBackend),
  106. Public: true,
  107. }, {
  108. Namespace: "debug",
  109. Version: "1.0",
  110. Service: NewPublicDebugAPI(apiBackend),
  111. Public: true,
  112. }, {
  113. Namespace: "debug",
  114. Version: "1.0",
  115. Service: NewPrivateDebugAPI(apiBackend),
  116. }, {
  117. Namespace: "eth",
  118. Version: "1.0",
  119. Service: NewPublicAccountAPI(apiBackend.AccountManager()),
  120. Public: true,
  121. }, {
  122. Namespace: "personal",
  123. Version: "1.0",
  124. Service: NewPrivateAccountAPI(apiBackend, nonceLock),
  125. Public: false,
  126. },
  127. }
  128. }