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