github.com/Elemental-core/elementalcore@v0.0.0-20191206075037-63891242267a/internal/ethapi/backend.go (about) 1 // Copyright 2015 The elementalcore Authors 2 // This file is part of the elementalcore library. 3 // 4 // The elementalcore 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 elementalcore 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 elementalcore library. If not, see <http://www.gnu.org/licenses/>. 16 17 // Package ethapi implements the general Ethereum API functions. 18 package ethapi 19 20 import ( 21 "context" 22 "math/big" 23 24 "github.com/Elemental-core/elementalcore/accounts" 25 "github.com/Elemental-core/elementalcore/common" 26 "github.com/Elemental-core/elementalcore/core" 27 "github.com/Elemental-core/elementalcore/core/state" 28 "github.com/Elemental-core/elementalcore/core/types" 29 "github.com/Elemental-core/elementalcore/core/vm" 30 "github.com/Elemental-core/elementalcore/eth/downloader" 31 "github.com/Elemental-core/elementalcore/ethdb" 32 "github.com/Elemental-core/elementalcore/event" 33 "github.com/Elemental-core/elementalcore/params" 34 "github.com/Elemental-core/elementalcore/rpc" 35 ) 36 37 // Backend interface provides the common API services (that are provided by 38 // both full and light clients) with access to necessary functions. 39 type Backend interface { 40 // general Ethereum API 41 Downloader() *downloader.Downloader 42 ProtocolVersion() int 43 SuggestPrice(ctx context.Context) (*big.Int, error) 44 ChainDb() ethdb.Database 45 EventMux() *event.TypeMux 46 AccountManager() *accounts.Manager 47 // BlockChain API 48 SetHead(number uint64) 49 HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error) 50 BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error) 51 StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*state.StateDB, *types.Header, error) 52 GetBlock(ctx context.Context, blockHash common.Hash) (*types.Block, error) 53 GetReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error) 54 GetTd(blockHash common.Hash) *big.Int 55 GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmCfg vm.Config) (*vm.EVM, func() error, error) 56 SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription 57 SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription 58 59 // TxPool API 60 SendTx(ctx context.Context, signedTx *types.Transaction) error 61 GetPoolTransactions() (types.Transactions, error) 62 GetPoolTransaction(txHash common.Hash) *types.Transaction 63 GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) 64 Stats() (pending int, queued int) 65 TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) 66 SubscribeTxPreEvent(chan<- core.TxPreEvent) event.Subscription 67 68 ChainConfig() *params.ChainConfig 69 CurrentBlock() *types.Block 70 } 71 72 func GetAPIs(apiBackend Backend) []rpc.API { 73 nonceLock := new(AddrLocker) 74 return []rpc.API{ 75 { 76 Namespace: "eth", 77 Version: "1.0", 78 Service: NewPublicEthereumAPI(apiBackend), 79 Public: true, 80 }, { 81 Namespace: "eth", 82 Version: "1.0", 83 Service: NewPublicBlockChainAPI(apiBackend), 84 Public: true, 85 }, { 86 Namespace: "eth", 87 Version: "1.0", 88 Service: NewPublicTransactionPoolAPI(apiBackend, nonceLock), 89 Public: true, 90 }, { 91 Namespace: "txpool", 92 Version: "1.0", 93 Service: NewPublicTxPoolAPI(apiBackend), 94 Public: true, 95 }, { 96 Namespace: "debug", 97 Version: "1.0", 98 Service: NewPublicDebugAPI(apiBackend), 99 Public: true, 100 }, { 101 Namespace: "debug", 102 Version: "1.0", 103 Service: NewPrivateDebugAPI(apiBackend), 104 }, { 105 Namespace: "eth", 106 Version: "1.0", 107 Service: NewPublicAccountAPI(apiBackend.AccountManager()), 108 Public: true, 109 }, { 110 Namespace: "personal", 111 Version: "1.0", 112 Service: NewPrivateAccountAPI(apiBackend, nonceLock), 113 Public: false, 114 }, 115 } 116 }