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