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