github.com/palisadeinc/bor@v0.0.0-20230615125219-ab7196213d15/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  	"time"
    24  
    25  	"github.com/ethereum/go-ethereum"
    26  	"github.com/ethereum/go-ethereum/accounts"
    27  	"github.com/ethereum/go-ethereum/common"
    28  	"github.com/ethereum/go-ethereum/consensus"
    29  	"github.com/ethereum/go-ethereum/core"
    30  	"github.com/ethereum/go-ethereum/core/bloombits"
    31  	"github.com/ethereum/go-ethereum/core/rawdb"
    32  	"github.com/ethereum/go-ethereum/core/state"
    33  	"github.com/ethereum/go-ethereum/core/types"
    34  	"github.com/ethereum/go-ethereum/core/vm"
    35  	"github.com/ethereum/go-ethereum/eth/gasprice"
    36  	"github.com/ethereum/go-ethereum/ethdb"
    37  	"github.com/ethereum/go-ethereum/event"
    38  	"github.com/ethereum/go-ethereum/light"
    39  	"github.com/ethereum/go-ethereum/params"
    40  	"github.com/ethereum/go-ethereum/rpc"
    41  )
    42  
    43  type LesApiBackend struct {
    44  	extRPCEnabled       bool
    45  	allowUnprotectedTxs bool
    46  	eth                 *LightEthereum
    47  	gpo                 *gasprice.Oracle
    48  }
    49  
    50  func (b *LesApiBackend) ChainConfig() *params.ChainConfig {
    51  	return b.eth.chainConfig
    52  }
    53  
    54  func (b *LesApiBackend) CurrentBlock() *types.Block {
    55  	return types.NewBlockWithHeader(b.eth.BlockChain().CurrentHeader())
    56  }
    57  
    58  func (b *LesApiBackend) SetHead(number uint64) {
    59  	b.eth.handler.downloader.Cancel()
    60  	b.eth.blockchain.SetHead(number)
    61  }
    62  
    63  func (b *LesApiBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) {
    64  	// Return the latest current as the pending one since there
    65  	// is no pending notion in the light client. TODO(rjl493456442)
    66  	// unify the behavior of `HeaderByNumber` and `PendingBlockAndReceipts`.
    67  	if number == rpc.PendingBlockNumber {
    68  		return b.eth.blockchain.CurrentHeader(), nil
    69  	}
    70  	if number == rpc.LatestBlockNumber {
    71  		return b.eth.blockchain.CurrentHeader(), nil
    72  	}
    73  	return b.eth.blockchain.GetHeaderByNumberOdr(ctx, uint64(number))
    74  }
    75  
    76  func (b *LesApiBackend) HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error) {
    77  	if blockNr, ok := blockNrOrHash.Number(); ok {
    78  		return b.HeaderByNumber(ctx, blockNr)
    79  	}
    80  	if hash, ok := blockNrOrHash.Hash(); ok {
    81  		header, err := b.HeaderByHash(ctx, hash)
    82  		if err != nil {
    83  			return nil, err
    84  		}
    85  		if header == nil {
    86  			return nil, errors.New("header for hash not found")
    87  		}
    88  		if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash {
    89  			return nil, errors.New("hash is not currently canonical")
    90  		}
    91  		return header, nil
    92  	}
    93  	return nil, errors.New("invalid arguments; neither block nor hash specified")
    94  }
    95  
    96  func (b *LesApiBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
    97  	return b.eth.blockchain.GetHeaderByHash(hash), nil
    98  }
    99  
   100  func (b *LesApiBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) {
   101  	header, err := b.HeaderByNumber(ctx, number)
   102  	if header == nil || err != nil {
   103  		return nil, err
   104  	}
   105  	return b.BlockByHash(ctx, header.Hash())
   106  }
   107  
   108  func (b *LesApiBackend) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) {
   109  	return b.eth.blockchain.GetBlockByHash(ctx, hash)
   110  }
   111  
   112  func (b *LesApiBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error) {
   113  	if blockNr, ok := blockNrOrHash.Number(); ok {
   114  		return b.BlockByNumber(ctx, blockNr)
   115  	}
   116  	if hash, ok := blockNrOrHash.Hash(); ok {
   117  		block, err := b.BlockByHash(ctx, hash)
   118  		if err != nil {
   119  			return nil, err
   120  		}
   121  		if block == nil {
   122  			return nil, errors.New("header found, but block body is missing")
   123  		}
   124  		if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(block.NumberU64()) != hash {
   125  			return nil, errors.New("hash is not currently canonical")
   126  		}
   127  		return block, nil
   128  	}
   129  	return nil, errors.New("invalid arguments; neither block nor hash specified")
   130  }
   131  
   132  func (b *LesApiBackend) PendingBlockAndReceipts() (*types.Block, types.Receipts) {
   133  	return nil, nil
   134  }
   135  
   136  func (b *LesApiBackend) StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error) {
   137  	header, err := b.HeaderByNumber(ctx, number)
   138  	if err != nil {
   139  		return nil, nil, err
   140  	}
   141  	if header == nil {
   142  		return nil, nil, errors.New("header not found")
   143  	}
   144  	return light.NewState(ctx, header, b.eth.odr), header, nil
   145  }
   146  
   147  func (b *LesApiBackend) StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error) {
   148  	if blockNr, ok := blockNrOrHash.Number(); ok {
   149  		return b.StateAndHeaderByNumber(ctx, blockNr)
   150  	}
   151  	if hash, ok := blockNrOrHash.Hash(); ok {
   152  		header := b.eth.blockchain.GetHeaderByHash(hash)
   153  		if header == nil {
   154  			return nil, nil, errors.New("header for hash not found")
   155  		}
   156  		if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash {
   157  			return nil, nil, errors.New("hash is not currently canonical")
   158  		}
   159  		return light.NewState(ctx, header, b.eth.odr), header, nil
   160  	}
   161  	return nil, nil, errors.New("invalid arguments; neither block nor hash specified")
   162  }
   163  
   164  func (b *LesApiBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
   165  	if number := rawdb.ReadHeaderNumber(b.eth.chainDb, hash); number != nil {
   166  		return light.GetBlockReceipts(ctx, b.eth.odr, hash, *number)
   167  	}
   168  	return nil, nil
   169  }
   170  
   171  func (b *LesApiBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) {
   172  	if number := rawdb.ReadHeaderNumber(b.eth.chainDb, hash); number != nil {
   173  		return light.GetBlockLogs(ctx, b.eth.odr, hash, *number)
   174  	}
   175  	return nil, nil
   176  }
   177  
   178  func (b *LesApiBackend) GetTd(ctx context.Context, hash common.Hash) *big.Int {
   179  	if number := rawdb.ReadHeaderNumber(b.eth.chainDb, hash); number != nil {
   180  		return b.eth.blockchain.GetTdOdr(ctx, hash, *number)
   181  	}
   182  	return nil
   183  }
   184  
   185  func (b *LesApiBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmConfig *vm.Config) (*vm.EVM, func() error, error) {
   186  	if vmConfig == nil {
   187  		vmConfig = new(vm.Config)
   188  	}
   189  	txContext := core.NewEVMTxContext(msg)
   190  	context := core.NewEVMBlockContext(header, b.eth.blockchain, nil)
   191  	return vm.NewEVM(context, txContext, state, b.eth.chainConfig, *vmConfig), state.Error, nil
   192  }
   193  
   194  func (b *LesApiBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
   195  	return b.eth.txPool.Add(ctx, signedTx)
   196  }
   197  
   198  func (b *LesApiBackend) RemoveTx(txHash common.Hash) {
   199  	b.eth.txPool.RemoveTx(txHash)
   200  }
   201  
   202  func (b *LesApiBackend) GetPoolTransactions() (types.Transactions, error) {
   203  	return b.eth.txPool.GetTransactions()
   204  }
   205  
   206  func (b *LesApiBackend) GetPoolTransaction(txHash common.Hash) *types.Transaction {
   207  	return b.eth.txPool.GetTransaction(txHash)
   208  }
   209  
   210  func (b *LesApiBackend) GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) {
   211  	return light.GetTransaction(ctx, b.eth.odr, txHash)
   212  }
   213  
   214  func (b *LesApiBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
   215  	return b.eth.txPool.GetNonce(ctx, addr)
   216  }
   217  
   218  func (b *LesApiBackend) Stats() (pending int, queued int) {
   219  	return b.eth.txPool.Stats(), 0
   220  }
   221  
   222  func (b *LesApiBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
   223  	return b.eth.txPool.Content()
   224  }
   225  
   226  func (b *LesApiBackend) TxPoolContentFrom(addr common.Address) (types.Transactions, types.Transactions) {
   227  	return b.eth.txPool.ContentFrom(addr)
   228  }
   229  
   230  func (b *LesApiBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription {
   231  	return b.eth.txPool.SubscribeNewTxsEvent(ch)
   232  }
   233  
   234  func (b *LesApiBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
   235  	return b.eth.blockchain.SubscribeChainEvent(ch)
   236  }
   237  
   238  func (b *LesApiBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
   239  	return b.eth.blockchain.SubscribeChainHeadEvent(ch)
   240  }
   241  
   242  func (b *LesApiBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription {
   243  	return b.eth.blockchain.SubscribeChainSideEvent(ch)
   244  }
   245  
   246  func (b *LesApiBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
   247  	return b.eth.blockchain.SubscribeLogsEvent(ch)
   248  }
   249  
   250  func (b *LesApiBackend) SubscribePendingLogsEvent(ch chan<- []*types.Log) event.Subscription {
   251  	return event.NewSubscription(func(quit <-chan struct{}) error {
   252  		<-quit
   253  		return nil
   254  	})
   255  }
   256  
   257  func (b *LesApiBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
   258  	return b.eth.blockchain.SubscribeRemovedLogsEvent(ch)
   259  }
   260  
   261  func (b *LesApiBackend) SyncProgress() ethereum.SyncProgress {
   262  	return b.eth.Downloader().Progress()
   263  }
   264  
   265  func (b *LesApiBackend) ProtocolVersion() int {
   266  	return b.eth.LesVersion() + 10000
   267  }
   268  
   269  func (b *LesApiBackend) SuggestGasTipCap(ctx context.Context) (*big.Int, error) {
   270  	return b.gpo.SuggestTipCap(ctx)
   271  }
   272  
   273  func (b *LesApiBackend) FeeHistory(ctx context.Context, blockCount int, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (firstBlock *big.Int, reward [][]*big.Int, baseFee []*big.Int, gasUsedRatio []float64, err error) {
   274  	return b.gpo.FeeHistory(ctx, blockCount, lastBlock, rewardPercentiles)
   275  }
   276  
   277  func (b *LesApiBackend) ChainDb() ethdb.Database {
   278  	return b.eth.chainDb
   279  }
   280  
   281  func (b *LesApiBackend) AccountManager() *accounts.Manager {
   282  	return b.eth.accountManager
   283  }
   284  
   285  func (b *LesApiBackend) ExtRPCEnabled() bool {
   286  	return b.extRPCEnabled
   287  }
   288  
   289  func (b *LesApiBackend) UnprotectedAllowed() bool {
   290  	return b.allowUnprotectedTxs
   291  }
   292  
   293  func (b *LesApiBackend) RPCGasCap() uint64 {
   294  	return b.eth.config.RPCGasCap
   295  }
   296  
   297  func (b *LesApiBackend) RPCRpcReturnDataLimit() uint64 {
   298  	return b.eth.config.RPCReturnDataLimit
   299  }
   300  
   301  func (b *LesApiBackend) RPCEVMTimeout() time.Duration {
   302  	return b.eth.config.RPCEVMTimeout
   303  }
   304  
   305  func (b *LesApiBackend) RPCTxFeeCap() float64 {
   306  	return b.eth.config.RPCTxFeeCap
   307  }
   308  
   309  func (b *LesApiBackend) BloomStatus() (uint64, uint64) {
   310  	if b.eth.bloomIndexer == nil {
   311  		return 0, 0
   312  	}
   313  	sections, _, _ := b.eth.bloomIndexer.Sections()
   314  	return params.BloomBitsBlocksClient, sections
   315  }
   316  
   317  func (b *LesApiBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) {
   318  	for i := 0; i < bloomFilterThreads; i++ {
   319  		go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests)
   320  	}
   321  }
   322  
   323  func (b *LesApiBackend) Engine() consensus.Engine {
   324  	return b.eth.engine
   325  }
   326  
   327  func (b *LesApiBackend) CurrentHeader() *types.Header {
   328  	return b.eth.blockchain.CurrentHeader()
   329  }
   330  
   331  func (b *LesApiBackend) StateAtBlock(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, checkLive bool, preferDisk bool) (*state.StateDB, error) {
   332  	return b.eth.stateAtBlock(ctx, block, reexec)
   333  }
   334  
   335  func (b *LesApiBackend) StateAtTransaction(ctx context.Context, block *types.Block, txIndex int, reexec uint64) (core.Message, vm.BlockContext, *state.StateDB, error) {
   336  	return b.eth.stateAtTransaction(ctx, block, txIndex, reexec)
   337  }
   338  
   339  //
   340  // Bor related functions
   341  //
   342  
   343  func (b *LesApiBackend) GetBorBlockReceipt(ctx context.Context, hash common.Hash) (*types.Receipt, error) {
   344  	return nil, errors.New("not implemented")
   345  }
   346  
   347  func (b *LesApiBackend) GetBorBlockLogs(ctx context.Context, hash common.Hash) ([]*types.Log, error) {
   348  	return nil, errors.New("not implemented")
   349  }
   350  
   351  func (b *LesApiBackend) GetBorBlockTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) {
   352  	return nil, common.Hash{}, 0, 0, errors.New("not implemented")
   353  }
   354  
   355  func (b *LesApiBackend) GetBorBlockTransactionWithBlockHash(ctx context.Context, txHash common.Hash, blockHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) {
   356  	return nil, common.Hash{}, 0, 0, errors.New("not implemented")
   357  }
   358  
   359  func (b *LesApiBackend) GetCheckpointWhitelist() map[uint64]common.Hash {
   360  	return nil
   361  }
   362  
   363  func (b *LesApiBackend) PurgeCheckpointWhitelist() {
   364  }