github.com/JFJun/bsc@v1.0.0/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/JFJun/bsc/accounts"
    25  	"github.com/JFJun/bsc/common"
    26  	"github.com/JFJun/bsc/core"
    27  	"github.com/JFJun/bsc/core/bloombits"
    28  	"github.com/JFJun/bsc/core/rawdb"
    29  	"github.com/JFJun/bsc/core/state"
    30  	"github.com/JFJun/bsc/core/types"
    31  	"github.com/JFJun/bsc/core/vm"
    32  	"github.com/JFJun/bsc/eth/downloader"
    33  	"github.com/JFJun/bsc/eth/gasprice"
    34  	"github.com/JFJun/bsc/ethdb"
    35  	"github.com/JFJun/bsc/event"
    36  	"github.com/JFJun/bsc/light"
    37  	"github.com/JFJun/bsc/params"
    38  	"github.com/JFJun/bsc/rpc"
    39  )
    40  
    41  type LesApiBackend struct {
    42  	extRPCEnabled bool
    43  	eth           *LightEthereum
    44  	gpo           *gasprice.Oracle
    45  }
    46  
    47  func (b *LesApiBackend) ChainConfig() *params.ChainConfig {
    48  	return b.eth.chainConfig
    49  }
    50  
    51  func (b *LesApiBackend) CurrentBlock() *types.Block {
    52  	return types.NewBlockWithHeader(b.eth.BlockChain().CurrentHeader())
    53  }
    54  
    55  func (b *LesApiBackend) SetHead(number uint64) {
    56  	b.eth.handler.downloader.Cancel()
    57  	b.eth.blockchain.SetHead(number)
    58  }
    59  
    60  func (b *LesApiBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) {
    61  	if number == rpc.LatestBlockNumber || number == rpc.PendingBlockNumber {
    62  		return b.eth.blockchain.CurrentHeader(), nil
    63  	}
    64  	return b.eth.blockchain.GetHeaderByNumberOdr(ctx, uint64(number))
    65  }
    66  
    67  func (b *LesApiBackend) HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error) {
    68  	if blockNr, ok := blockNrOrHash.Number(); ok {
    69  		return b.HeaderByNumber(ctx, blockNr)
    70  	}
    71  	if hash, ok := blockNrOrHash.Hash(); ok {
    72  		header, err := b.HeaderByHash(ctx, hash)
    73  		if err != nil {
    74  			return nil, err
    75  		}
    76  		if header == nil {
    77  			return nil, errors.New("header for hash not found")
    78  		}
    79  		if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash {
    80  			return nil, errors.New("hash is not currently canonical")
    81  		}
    82  		return header, nil
    83  	}
    84  	return nil, errors.New("invalid arguments; neither block nor hash specified")
    85  }
    86  
    87  func (b *LesApiBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
    88  	return b.eth.blockchain.GetHeaderByHash(hash), nil
    89  }
    90  
    91  func (b *LesApiBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) {
    92  	header, err := b.HeaderByNumber(ctx, number)
    93  	if header == nil || err != nil {
    94  		return nil, err
    95  	}
    96  	return b.BlockByHash(ctx, header.Hash())
    97  }
    98  
    99  func (b *LesApiBackend) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) {
   100  	return b.eth.blockchain.GetBlockByHash(ctx, hash)
   101  }
   102  
   103  func (b *LesApiBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error) {
   104  	if blockNr, ok := blockNrOrHash.Number(); ok {
   105  		return b.BlockByNumber(ctx, blockNr)
   106  	}
   107  	if hash, ok := blockNrOrHash.Hash(); ok {
   108  		block, err := b.BlockByHash(ctx, hash)
   109  		if err != nil {
   110  			return nil, err
   111  		}
   112  		if block == nil {
   113  			return nil, errors.New("header found, but block body is missing")
   114  		}
   115  		if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(block.NumberU64()) != hash {
   116  			return nil, errors.New("hash is not currently canonical")
   117  		}
   118  		return block, nil
   119  	}
   120  	return nil, errors.New("invalid arguments; neither block nor hash specified")
   121  }
   122  
   123  func (b *LesApiBackend) StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error) {
   124  	header, err := b.HeaderByNumber(ctx, number)
   125  	if err != nil {
   126  		return nil, nil, err
   127  	}
   128  	if header == nil {
   129  		return nil, nil, errors.New("header not found")
   130  	}
   131  	return light.NewState(ctx, header, b.eth.odr), header, nil
   132  }
   133  
   134  func (b *LesApiBackend) StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error) {
   135  	if blockNr, ok := blockNrOrHash.Number(); ok {
   136  		return b.StateAndHeaderByNumber(ctx, blockNr)
   137  	}
   138  	if hash, ok := blockNrOrHash.Hash(); ok {
   139  		header := b.eth.blockchain.GetHeaderByHash(hash)
   140  		if header == nil {
   141  			return nil, nil, errors.New("header for hash not found")
   142  		}
   143  		if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash {
   144  			return nil, nil, errors.New("hash is not currently canonical")
   145  		}
   146  		return light.NewState(ctx, header, b.eth.odr), header, nil
   147  	}
   148  	return nil, nil, errors.New("invalid arguments; neither block nor hash specified")
   149  }
   150  
   151  func (b *LesApiBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
   152  	if number := rawdb.ReadHeaderNumber(b.eth.chainDb, hash); number != nil {
   153  		return light.GetBlockReceipts(ctx, b.eth.odr, hash, *number)
   154  	}
   155  	return nil, nil
   156  }
   157  
   158  func (b *LesApiBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) {
   159  	if number := rawdb.ReadHeaderNumber(b.eth.chainDb, hash); number != nil {
   160  		return light.GetBlockLogs(ctx, b.eth.odr, hash, *number)
   161  	}
   162  	return nil, nil
   163  }
   164  
   165  func (b *LesApiBackend) GetTd(hash common.Hash) *big.Int {
   166  	return b.eth.blockchain.GetTdByHash(hash)
   167  }
   168  
   169  func (b *LesApiBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header) (*vm.EVM, func() error, error) {
   170  	context := core.NewEVMContext(msg, header, b.eth.blockchain, nil)
   171  	return vm.NewEVM(context, state, b.eth.chainConfig, vm.Config{}), state.Error, nil
   172  }
   173  
   174  func (b *LesApiBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
   175  	return b.eth.txPool.Add(ctx, signedTx)
   176  }
   177  
   178  func (b *LesApiBackend) RemoveTx(txHash common.Hash) {
   179  	b.eth.txPool.RemoveTx(txHash)
   180  }
   181  
   182  func (b *LesApiBackend) GetPoolTransactions() (types.Transactions, error) {
   183  	return b.eth.txPool.GetTransactions()
   184  }
   185  
   186  func (b *LesApiBackend) GetPoolTransaction(txHash common.Hash) *types.Transaction {
   187  	return b.eth.txPool.GetTransaction(txHash)
   188  }
   189  
   190  func (b *LesApiBackend) GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) {
   191  	return light.GetTransaction(ctx, b.eth.odr, txHash)
   192  }
   193  
   194  func (b *LesApiBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
   195  	return b.eth.txPool.GetNonce(ctx, addr)
   196  }
   197  
   198  func (b *LesApiBackend) Stats() (pending int, queued int) {
   199  	return b.eth.txPool.Stats(), 0
   200  }
   201  
   202  func (b *LesApiBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
   203  	return b.eth.txPool.Content()
   204  }
   205  
   206  func (b *LesApiBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription {
   207  	return b.eth.txPool.SubscribeNewTxsEvent(ch)
   208  }
   209  
   210  func (b *LesApiBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
   211  	return b.eth.blockchain.SubscribeChainEvent(ch)
   212  }
   213  
   214  func (b *LesApiBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
   215  	return b.eth.blockchain.SubscribeChainHeadEvent(ch)
   216  }
   217  
   218  func (b *LesApiBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription {
   219  	return b.eth.blockchain.SubscribeChainSideEvent(ch)
   220  }
   221  
   222  func (b *LesApiBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
   223  	return b.eth.blockchain.SubscribeLogsEvent(ch)
   224  }
   225  
   226  func (b *LesApiBackend) SubscribePendingLogsEvent(ch chan<- []*types.Log) event.Subscription {
   227  	return event.NewSubscription(func(quit <-chan struct{}) error {
   228  		<-quit
   229  		return nil
   230  	})
   231  }
   232  
   233  func (b *LesApiBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
   234  	return b.eth.blockchain.SubscribeRemovedLogsEvent(ch)
   235  }
   236  
   237  func (b *LesApiBackend) Downloader() *downloader.Downloader {
   238  	return b.eth.Downloader()
   239  }
   240  
   241  func (b *LesApiBackend) ProtocolVersion() int {
   242  	return b.eth.LesVersion() + 10000
   243  }
   244  
   245  func (b *LesApiBackend) SuggestPrice(ctx context.Context) (*big.Int, error) {
   246  	return b.gpo.SuggestPrice(ctx)
   247  }
   248  
   249  func (b *LesApiBackend) ChainDb() ethdb.Database {
   250  	return b.eth.chainDb
   251  }
   252  
   253  func (b *LesApiBackend) AccountManager() *accounts.Manager {
   254  	return b.eth.accountManager
   255  }
   256  
   257  func (b *LesApiBackend) ExtRPCEnabled() bool {
   258  	return b.extRPCEnabled
   259  }
   260  
   261  func (b *LesApiBackend) RPCGasCap() *big.Int {
   262  	return b.eth.config.RPCGasCap
   263  }
   264  
   265  func (b *LesApiBackend) BloomStatus() (uint64, uint64) {
   266  	if b.eth.bloomIndexer == nil {
   267  		return 0, 0
   268  	}
   269  	sections, _, _ := b.eth.bloomIndexer.Sections()
   270  	return params.BloomBitsBlocksClient, sections
   271  }
   272  
   273  func (b *LesApiBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) {
   274  	for i := 0; i < bloomFilterThreads; i++ {
   275  		go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests)
   276  	}
   277  }