github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/internal/ethapi/backend.go (about)

     1  // Copyright 2015 The Spectrum Authors
     2  // This file is part of the Spectrum library.
     3  //
     4  // The Spectrum 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 Spectrum 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 Spectrum 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/SmartMeshFoundation/Spectrum/accounts"
    25  	"github.com/SmartMeshFoundation/Spectrum/common"
    26  	"github.com/SmartMeshFoundation/Spectrum/core"
    27  	"github.com/SmartMeshFoundation/Spectrum/core/state"
    28  	"github.com/SmartMeshFoundation/Spectrum/core/types"
    29  	"github.com/SmartMeshFoundation/Spectrum/core/vm"
    30  	"github.com/SmartMeshFoundation/Spectrum/eth/downloader"
    31  	"github.com/SmartMeshFoundation/Spectrum/ethdb"
    32  	"github.com/SmartMeshFoundation/Spectrum/event"
    33  	"github.com/SmartMeshFoundation/Spectrum/params"
    34  	"github.com/SmartMeshFoundation/Spectrum/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  	//add by liangc : only for execute contract by hash
    53  	StateAndHeaderByHash(ctx context.Context, hash *common.Hash) (*state.StateDB, *types.Header, error)
    54  	GetBlock(ctx context.Context, blockHash common.Hash) (*types.Block, error)
    55  	GetReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error)
    56  	GetTd(blockHash common.Hash) *big.Int
    57  	GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmCfg vm.Config) (*vm.EVM, func() error, error)
    58  	SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription
    59  	SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription
    60  	SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription
    61  
    62  	// TxPool API
    63  	SendTx(ctx context.Context, signedTx *types.Transaction) error
    64  	GetPoolTransactions() (types.Transactions, error)
    65  	GetPoolTransaction(txHash common.Hash) *types.Transaction
    66  	GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error)
    67  	Stats() (pending int, queued int)
    68  	TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions)
    69  	SubscribeTxPreEvent(chan<- core.TxPreEvent) event.Subscription
    70  
    71  	ChainConfig() *params.ChainConfig
    72  	CurrentBlock() *types.Block
    73  }
    74  
    75  func GetAPIs(apiBackend Backend) []rpc.API {
    76  	nonceLock := new(AddrLocker)
    77  	return []rpc.API{
    78  		{
    79  			Namespace: "eth",
    80  			Version:   "1.0",
    81  			Service:   NewPublicEthereumAPI(apiBackend),
    82  			Public:    true,
    83  		}, {
    84  			Namespace: "eth",
    85  			Version:   "1.0",
    86  			Service:   NewPublicBlockChainAPI(apiBackend),
    87  			Public:    true,
    88  		}, {
    89  			Namespace: "eth",
    90  			Version:   "1.0",
    91  			Service:   NewPublicTransactionPoolAPI(apiBackend, nonceLock),
    92  			Public:    true,
    93  		}, {
    94  			Namespace: "txpool",
    95  			Version:   "1.0",
    96  			Service:   NewPublicTxPoolAPI(apiBackend),
    97  			Public:    true,
    98  		}, {
    99  			Namespace: "debug",
   100  			Version:   "1.0",
   101  			Service:   NewPublicDebugAPI(apiBackend),
   102  			Public:    true,
   103  		}, {
   104  			Namespace: "debug",
   105  			Version:   "1.0",
   106  			Service:   NewPrivateDebugAPI(apiBackend),
   107  		}, {
   108  			Namespace: "eth",
   109  			Version:   "1.0",
   110  			Service:   NewPublicAccountAPI(apiBackend.AccountManager()),
   111  			Public:    true,
   112  		}, {
   113  			Namespace: "personal",
   114  			Version:   "1.0",
   115  			Service:   NewPrivateAccountAPI(apiBackend, nonceLock),
   116  			Public:    false,
   117  		},
   118  	}
   119  }