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