backend.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. "math/big"
  20. "github.com/ethereum/go-ethereum/accounts"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/core"
  23. "github.com/ethereum/go-ethereum/core/types"
  24. "github.com/ethereum/go-ethereum/core/vm"
  25. "github.com/ethereum/go-ethereum/eth/downloader"
  26. "github.com/ethereum/go-ethereum/ethdb"
  27. "github.com/ethereum/go-ethereum/event"
  28. "github.com/ethereum/go-ethereum/rpc"
  29. "golang.org/x/net/context"
  30. )
  31. // Backend interface provides the common API services (that are provided by
  32. // both full and light clients) with access to necessary functions.
  33. type Backend interface {
  34. // general Ethereum API
  35. Downloader() *downloader.Downloader
  36. ProtocolVersion() int
  37. SuggestPrice(ctx context.Context) (*big.Int, error)
  38. ChainDb() ethdb.Database
  39. EventMux() *event.TypeMux
  40. AccountManager() *accounts.Manager
  41. // BlockChain API
  42. SetHead(number uint64)
  43. HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error)
  44. BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error)
  45. StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (State, *types.Header, error)
  46. GetBlock(ctx context.Context, blockHash common.Hash) (*types.Block, error)
  47. GetReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error)
  48. GetTd(blockHash common.Hash) *big.Int
  49. GetVMEnv(ctx context.Context, msg core.Message, state State, header *types.Header) (vm.Environment, func() error, error)
  50. // TxPool API
  51. SendTx(ctx context.Context, signedTx *types.Transaction) error
  52. RemoveTx(txHash common.Hash)
  53. GetPoolTransactions() types.Transactions
  54. GetPoolTransaction(txHash common.Hash) *types.Transaction
  55. GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error)
  56. Stats() (pending int, queued int)
  57. TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions)
  58. }
  59. type State interface {
  60. GetBalance(ctx context.Context, addr common.Address) (*big.Int, error)
  61. GetCode(ctx context.Context, addr common.Address) ([]byte, error)
  62. GetState(ctx context.Context, a common.Address, b common.Hash) (common.Hash, error)
  63. GetNonce(ctx context.Context, addr common.Address) (uint64, error)
  64. }
  65. func GetAPIs(apiBackend Backend, solcPath string) []rpc.API {
  66. compiler := makeCompilerAPIs(solcPath)
  67. all := []rpc.API{
  68. {
  69. Namespace: "eth",
  70. Version: "1.0",
  71. Service: NewPublicEthereumAPI(apiBackend),
  72. Public: true,
  73. }, {
  74. Namespace: "eth",
  75. Version: "1.0",
  76. Service: NewPublicBlockChainAPI(apiBackend),
  77. Public: true,
  78. }, {
  79. Namespace: "eth",
  80. Version: "1.0",
  81. Service: NewPublicTransactionPoolAPI(apiBackend),
  82. Public: true,
  83. }, {
  84. Namespace: "txpool",
  85. Version: "1.0",
  86. Service: NewPublicTxPoolAPI(apiBackend),
  87. Public: true,
  88. }, {
  89. Namespace: "debug",
  90. Version: "1.0",
  91. Service: NewPublicDebugAPI(apiBackend),
  92. Public: true,
  93. }, {
  94. Namespace: "debug",
  95. Version: "1.0",
  96. Service: NewPrivateDebugAPI(apiBackend),
  97. }, {
  98. Namespace: "eth",
  99. Version: "1.0",
  100. Service: NewPublicAccountAPI(apiBackend.AccountManager()),
  101. Public: true,
  102. }, {
  103. Namespace: "personal",
  104. Version: "1.0",
  105. Service: NewPrivateAccountAPI(apiBackend),
  106. Public: false,
  107. },
  108. }
  109. return append(compiler, all...)
  110. }