github.com/codingfuture/orig-energi3@v0.8.4/energi/api/backend.go (about)

     1  // Copyright 2019 The Energi Core Authors
     2  // This file is part of the Energi Core library.
     3  //
     4  // The Energi Core 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 Energi Core 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 Energi Core library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // Package ethapi implements the general Ethereum API functions.
    18  package api
    19  
    20  import (
    21  	"context"
    22  	"math/big"
    23  
    24  	"github.com/ethereum/go-ethereum/accounts"
    25  	"github.com/ethereum/go-ethereum/accounts/abi/bind"
    26  	"github.com/ethereum/go-ethereum/common"
    27  	"github.com/ethereum/go-ethereum/core"
    28  	"github.com/ethereum/go-ethereum/core/state"
    29  	"github.com/ethereum/go-ethereum/core/types"
    30  	"github.com/ethereum/go-ethereum/core/vm"
    31  	"github.com/ethereum/go-ethereum/eth/downloader"
    32  	"github.com/ethereum/go-ethereum/ethdb"
    33  	"github.com/ethereum/go-ethereum/event"
    34  	"github.com/ethereum/go-ethereum/params"
    35  	"github.com/ethereum/go-ethereum/rpc"
    36  )
    37  
    38  // Backend interface provides the common API services (that are provided by
    39  // both full and light clients) with access to necessary functions.
    40  type Backend interface {
    41  	// General Ethereum API
    42  	Downloader() *downloader.Downloader
    43  	ProtocolVersion() int
    44  	SuggestPrice(ctx context.Context) (*big.Int, error)
    45  	ChainDb() ethdb.Database
    46  	EventMux() *event.TypeMux
    47  	AccountManager() *accounts.Manager
    48  	RPCGasCap() *big.Int // global gas cap for eth_call over rpc: DoS protection
    49  
    50  	// BlockChain API
    51  	SetHead(number uint64)
    52  	HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error)
    53  	BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error)
    54  	StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*state.StateDB, *types.Header, error)
    55  	GetBlock(ctx context.Context, blockHash common.Hash) (*types.Block, error)
    56  	GetReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error)
    57  	GetTd(blockHash common.Hash) *big.Int
    58  	GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header) (*vm.EVM, func() error, error)
    59  	SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription
    60  	SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription
    61  	SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription
    62  
    63  	// TxPool API
    64  	SendTx(ctx context.Context, signedTx *types.Transaction) error
    65  	GetPoolTransactions() (types.Transactions, error)
    66  	GetPoolTransaction(txHash common.Hash) *types.Transaction
    67  	GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error)
    68  	Stats() (pending int, queued int)
    69  	TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions)
    70  	SubscribeNewTxsEvent(chan<- core.NewTxsEvent) event.Subscription
    71  
    72  	ChainConfig() *params.ChainConfig
    73  	CurrentBlock() *types.Block
    74  
    75  	AddLocalCheckpoint(num uint64, hash common.Hash) error
    76  	ListCheckpoints() []core.CheckpointInfo
    77  	CheckpointSignatures(cp core.Checkpoint) []core.CheckpointSignature
    78  
    79  	IsPublicService() bool
    80  	OnSyncedHeadUpdates(cb func())
    81  }
    82  
    83  func createSignerCallback(
    84  	backend Backend,
    85  	password *string,
    86  ) bind.SignerFn {
    87  	return func(
    88  		signer types.Signer,
    89  		addr common.Address,
    90  		tx *types.Transaction,
    91  	) (*types.Transaction, error) {
    92  		account := accounts.Account{Address: addr}
    93  		wallet, err := backend.AccountManager().Find(account)
    94  		if err != nil {
    95  			return nil, err
    96  		}
    97  
    98  		chain_id := backend.ChainConfig().ChainID
    99  
   100  		if password == nil {
   101  			return wallet.SignTx(
   102  				account, tx, chain_id)
   103  		}
   104  
   105  		return wallet.SignTxWithPassphrase(
   106  			account, *password, tx, chain_id)
   107  	}
   108  }