backend.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 2016 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/common/compiler"
  23. "github.com/ethereum/go-ethereum/core"
  24. "github.com/ethereum/go-ethereum/core/types"
  25. "github.com/ethereum/go-ethereum/core/vm"
  26. "github.com/ethereum/go-ethereum/eth/downloader"
  27. "github.com/ethereum/go-ethereum/ethdb"
  28. "github.com/ethereum/go-ethereum/event"
  29. "github.com/ethereum/go-ethereum/rpc"
  30. "golang.org/x/net/context"
  31. )
  32. // Backend interface provides the common API services (that are provided by
  33. // both full and light clients) with access to necessary functions.
  34. type Backend interface {
  35. // general Ethereum API
  36. Downloader() *downloader.Downloader
  37. ProtocolVersion() int
  38. SuggestPrice(ctx context.Context) (*big.Int, error)
  39. ChainDb() ethdb.Database
  40. EventMux() *event.TypeMux
  41. AccountManager() *accounts.Manager
  42. // BlockChain API
  43. SetHead(number uint64)
  44. HeaderByNumber(blockNr rpc.BlockNumber) *types.Header
  45. BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error)
  46. StateAndHeaderByNumber(blockNr rpc.BlockNumber) (State, *types.Header, error)
  47. GetBlock(ctx context.Context, blockHash common.Hash) (*types.Block, error)
  48. GetReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error)
  49. GetTd(blockHash common.Hash) *big.Int
  50. GetVMEnv(ctx context.Context, msg core.Message, state State, header *types.Header) (vm.Environment, func() error, error)
  51. // TxPool API
  52. SendTx(ctx context.Context, signedTx *types.Transaction) error
  53. RemoveTx(txHash common.Hash)
  54. GetPoolTransactions() types.Transactions
  55. GetPoolTransaction(txHash common.Hash) *types.Transaction
  56. GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error)
  57. Stats() (pending int, queued int)
  58. TxPoolContent() (map[common.Address]map[uint64][]*types.Transaction, map[common.Address]map[uint64][]*types.Transaction)
  59. }
  60. type State interface {
  61. GetBalance(ctx context.Context, addr common.Address) (*big.Int, error)
  62. GetCode(ctx context.Context, addr common.Address) ([]byte, error)
  63. GetState(ctx context.Context, a common.Address, b common.Hash) (common.Hash, error)
  64. GetNonce(ctx context.Context, addr common.Address) (uint64, error)
  65. }
  66. func GetAPIs(apiBackend Backend, solcPath *string, solc **compiler.Solidity) []rpc.API {
  67. return []rpc.API{
  68. {
  69. Namespace: "eth",
  70. Version: "1.0",
  71. Service: NewPublicEthereumAPI(apiBackend, solcPath, solc),
  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: "admin",
  90. Version: "1.0",
  91. Service: NewPrivateAdminAPI(apiBackend, solcPath, solc),
  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),
  110. Public: false,
  111. },
  112. }
  113. }