github.com/tdcblockchain/tdcblockchain@v0.0.0-20191111034745-805c65ade158/les/api_backend.go (about)

     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  
    17  package les
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  	"math/big"
    23  
    24  	"github.com/ethereum/go-ethereum/accounts"
    25  	"github.com/ethereum/go-ethereum/common"
    26  	"github.com/ethereum/go-ethereum/common/math"
    27  	"github.com/ethereum/go-ethereum/core"
    28  	"github.com/ethereum/go-ethereum/core/bloombits"
    29  	"github.com/ethereum/go-ethereum/core/rawdb"
    30  	"github.com/ethereum/go-ethereum/core/state"
    31  	"github.com/ethereum/go-ethereum/core/types"
    32  	"github.com/ethereum/go-ethereum/core/vm"
    33  	"github.com/ethereum/go-ethereum/eth/downloader"
    34  	"github.com/ethereum/go-ethereum/eth/gasprice"
    35  	"github.com/ethereum/go-ethereum/ethdb"
    36  	"github.com/ethereum/go-ethereum/event"
    37  	"github.com/ethereum/go-ethereum/light"
    38  	"github.com/ethereum/go-ethereum/params"
    39  	"github.com/ethereum/go-ethereum/rpc"
    40  )
    41  
    42  type LesApiBackend struct {
    43  	extRPCEnabled bool
    44  	eth           *LightEthereum
    45  	gpo           *gasprice.Oracle
    46  }
    47  
    48  func (b *LesApiBackend) ChainConfig() *params.ChainConfig {
    49  	return b.eth.chainConfig
    50  }
    51  
    52  func (b *LesApiBackend) CurrentBlock() *types.Block {
    53  	return types.NewBlockWithHeader(b.eth.BlockChain().CurrentHeader())
    54  }
    55  
    56  func (b *LesApiBackend) SetHead(number uint64) {
    57  	b.eth.handler.downloader.Cancel()
    58  	b.eth.blockchain.SetHead(number)
    59  }
    60  
    61  func (b *LesApiBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) {
    62  	if number == rpc.LatestBlockNumber || number == rpc.PendingBlockNumber {
    63  		return b.eth.blockchain.CurrentHeader(), nil
    64  	}
    65  	return b.eth.blockchain.GetHeaderByNumberOdr(ctx, uint64(number))
    66  }
    67  
    68  func (b *LesApiBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
    69  	return b.eth.blockchain.GetHeaderByHash(hash), nil
    70  }
    71  
    72  func (b *LesApiBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) {
    73  	header, err := b.HeaderByNumber(ctx, number)
    74  	if header == nil || err != nil {
    75  		return nil, err
    76  	}
    77  	return b.BlockByHash(ctx, header.Hash())
    78  }
    79  
    80  func (b *LesApiBackend) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) {
    81  	return b.eth.blockchain.GetBlockByHash(ctx, hash)
    82  }
    83  
    84  func (b *LesApiBackend) StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error) {
    85  	header, err := b.HeaderByNumber(ctx, number)
    86  	if err != nil {
    87  		return nil, nil, err
    88  	}
    89  	if header == nil {
    90  		return nil, nil, errors.New("header not found")
    91  	}
    92  	return light.NewState(ctx, header, b.eth.odr), header, nil
    93  }
    94  
    95  func (b *LesApiBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
    96  	if number := rawdb.ReadHeaderNumber(b.eth.chainDb, hash); number != nil {
    97  		return light.GetBlockReceipts(ctx, b.eth.odr, hash, *number)
    98  	}
    99  	return nil, nil
   100  }
   101  
   102  func (b *LesApiBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) {
   103  	if number := rawdb.ReadHeaderNumber(b.eth.chainDb, hash); number != nil {
   104  		return light.GetBlockLogs(ctx, b.eth.odr, hash, *number)
   105  	}
   106  	return nil, nil
   107  }
   108  
   109  func (b *LesApiBackend) GetTd(hash common.Hash) *big.Int {
   110  	return b.eth.blockchain.GetTdByHash(hash)
   111  }
   112  
   113  func (b *LesApiBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header) (*vm.EVM, func() error, error) {
   114  	state.SetBalance(msg.From(), math.MaxBig256)
   115  	context := core.NewEVMContext(msg, header, b.eth.blockchain, nil)
   116  	return vm.NewEVM(context, state, b.eth.chainConfig, vm.Config{}), state.Error, nil
   117  }
   118  
   119  func (b *LesApiBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
   120  	return b.eth.txPool.Add(ctx, signedTx)
   121  }
   122  
   123  func (b *LesApiBackend) RemoveTx(txHash common.Hash) {
   124  	b.eth.txPool.RemoveTx(txHash)
   125  }
   126  
   127  func (b *LesApiBackend) GetPoolTransactions() (types.Transactions, error) {
   128  	return b.eth.txPool.GetTransactions()
   129  }
   130  
   131  func (b *LesApiBackend) GetPoolTransaction(txHash common.Hash) *types.Transaction {
   132  	return b.eth.txPool.GetTransaction(txHash)
   133  }
   134  
   135  func (b *LesApiBackend) GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) {
   136  	return light.GetTransaction(ctx, b.eth.odr, txHash)
   137  }
   138  
   139  func (b *LesApiBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
   140  	return b.eth.txPool.GetNonce(ctx, addr)
   141  }
   142  
   143  func (b *LesApiBackend) Stats() (pending int, queued int) {
   144  	return b.eth.txPool.Stats(), 0
   145  }
   146  
   147  func (b *LesApiBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
   148  	return b.eth.txPool.Content()
   149  }
   150  
   151  func (b *LesApiBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription {
   152  	return b.eth.txPool.SubscribeNewTxsEvent(ch)
   153  }
   154  
   155  func (b *LesApiBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
   156  	return b.eth.blockchain.SubscribeChainEvent(ch)
   157  }
   158  
   159  func (b *LesApiBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
   160  	return b.eth.blockchain.SubscribeChainHeadEvent(ch)
   161  }
   162  
   163  func (b *LesApiBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription {
   164  	return b.eth.blockchain.SubscribeChainSideEvent(ch)
   165  }
   166  
   167  func (b *LesApiBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
   168  	return b.eth.blockchain.SubscribeLogsEvent(ch)
   169  }
   170  
   171  func (b *LesApiBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
   172  	return b.eth.blockchain.SubscribeRemovedLogsEvent(ch)
   173  }
   174  
   175  func (b *LesApiBackend) Downloader() *downloader.Downloader {
   176  	return b.eth.Downloader()
   177  }
   178  
   179  func (b *LesApiBackend) ProtocolVersion() int {
   180  	return b.eth.LesVersion() + 10000
   181  }
   182  
   183  func (b *LesApiBackend) SuggestPrice(ctx context.Context) (*big.Int, error) {
   184  	return b.gpo.SuggestPrice(ctx)
   185  }
   186  
   187  func (b *LesApiBackend) ChainDb() ethdb.Database {
   188  	return b.eth.chainDb
   189  }
   190  
   191  func (b *LesApiBackend) EventMux() *event.TypeMux {
   192  	return b.eth.eventMux
   193  }
   194  
   195  func (b *LesApiBackend) AccountManager() *accounts.Manager {
   196  	return b.eth.accountManager
   197  }
   198  
   199  func (b *LesApiBackend) ExtRPCEnabled() bool {
   200  	return b.extRPCEnabled
   201  }
   202  
   203  func (b *LesApiBackend) RPCGasCap() *big.Int {
   204  	return b.eth.config.RPCGasCap
   205  }
   206  
   207  func (b *LesApiBackend) BloomStatus() (uint64, uint64) {
   208  	if b.eth.bloomIndexer == nil {
   209  		return 0, 0
   210  	}
   211  	sections, _, _ := b.eth.bloomIndexer.Sections()
   212  	return params.BloomBitsBlocksClient, sections
   213  }
   214  
   215  func (b *LesApiBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) {
   216  	for i := 0; i < bloomFilterThreads; i++ {
   217  		go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests)
   218  	}
   219  }