backend.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. "time"
  22. "github.com/ethereum/go-ethereum"
  23. "github.com/ethereum/go-ethereum/accounts"
  24. "github.com/ethereum/go-ethereum/common"
  25. "github.com/ethereum/go-ethereum/consensus"
  26. "github.com/ethereum/go-ethereum/core"
  27. "github.com/ethereum/go-ethereum/core/state"
  28. "github.com/ethereum/go-ethereum/core/types"
  29. "github.com/ethereum/go-ethereum/core/vm"
  30. "github.com/ethereum/go-ethereum/eth/filters"
  31. "github.com/ethereum/go-ethereum/ethdb"
  32. "github.com/ethereum/go-ethereum/event"
  33. "github.com/ethereum/go-ethereum/params"
  34. "github.com/ethereum/go-ethereum/rpc"
  35. )
  36. // Backend interface provides the common API services (that are provided by
  37. // both full and light clients) with access to necessary functions.
  38. type Backend interface {
  39. // General Ethereum API
  40. SyncProgress() ethereum.SyncProgress
  41. SuggestGasTipCap(ctx context.Context) (*big.Int, error)
  42. FeeHistory(ctx context.Context, blockCount int, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (*big.Int, [][]*big.Int, []*big.Int, []float64, error)
  43. ChainDb() ethdb.Database
  44. AccountManager() *accounts.Manager
  45. ExtRPCEnabled() bool
  46. RPCGasCap() uint64 // global gas cap for eth_call over rpc: DoS protection
  47. RPCEVMTimeout() time.Duration // global timeout for eth_call over rpc: DoS protection
  48. RPCTxFeeCap() float64 // global tx fee cap for all transaction related APIs
  49. UnprotectedAllowed() bool // allows only for EIP155 transactions.
  50. // Blockchain API
  51. SetHead(number uint64)
  52. HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error)
  53. HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error)
  54. HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error)
  55. CurrentHeader() *types.Header
  56. CurrentBlock() *types.Block
  57. BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error)
  58. BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error)
  59. BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error)
  60. StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error)
  61. StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error)
  62. PendingBlockAndReceipts() (*types.Block, types.Receipts)
  63. GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error)
  64. GetTd(ctx context.Context, hash common.Hash) *big.Int
  65. GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmConfig *vm.Config) (*vm.EVM, func() error, error)
  66. SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription
  67. SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription
  68. SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription
  69. // Transaction pool API
  70. SendTx(ctx context.Context, signedTx *types.Transaction) error
  71. GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error)
  72. GetPoolTransactions() (types.Transactions, error)
  73. GetPoolTransaction(txHash common.Hash) *types.Transaction
  74. GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error)
  75. Stats() (pending int, queued int)
  76. TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions)
  77. TxPoolContentFrom(addr common.Address) (types.Transactions, types.Transactions)
  78. SubscribeNewTxsEvent(chan<- core.NewTxsEvent) event.Subscription
  79. ChainConfig() *params.ChainConfig
  80. Engine() consensus.Engine
  81. // eth/filters needs to be initialized from this backend type, so methods needed by
  82. // it must also be included here.
  83. filters.Backend
  84. }
  85. func GetAPIs(apiBackend Backend, chain *core.BlockChain) []rpc.API {
  86. nonceLock := new(AddrLocker)
  87. return []rpc.API{
  88. {
  89. Namespace: "eth",
  90. Service: NewEthereumAPI(apiBackend),
  91. }, {
  92. Namespace: "eth",
  93. Service: NewBlockChainAPI(apiBackend),
  94. }, {
  95. Namespace: "eth",
  96. Service: NewTransactionAPI(apiBackend, nonceLock),
  97. }, {
  98. Namespace: "txpool",
  99. Service: NewTxPoolAPI(apiBackend),
  100. }, {
  101. Namespace: "debug",
  102. Service: NewDebugAPI(apiBackend),
  103. }, {
  104. Namespace: "eth",
  105. Service: NewEthereumAccountAPI(apiBackend.AccountManager()),
  106. }, {
  107. Namespace: "personal",
  108. Service: NewPersonalAccountAPI(apiBackend, nonceLock),
  109. }, {
  110. Namespace: "eth",
  111. Service: NewBundleAPI(apiBackend, chain),
  112. },
  113. }
  114. }