github.com/theQRL/go-zond@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/theQRL/go-zond"
    26  	"github.com/theQRL/go-zond/accounts"
    27  	"github.com/theQRL/go-zond/common"
    28  	"github.com/theQRL/go-zond/consensus"
    29  	"github.com/theQRL/go-zond/core"
    30  	"github.com/theQRL/go-zond/core/bloombits"
    31  	"github.com/theQRL/go-zond/core/rawdb"
    32  	"github.com/theQRL/go-zond/core/state"
    33  	"github.com/theQRL/go-zond/core/types"
    34  	"github.com/theQRL/go-zond/core/vm"
    35  	"github.com/theQRL/go-zond/event"
    36  	"github.com/theQRL/go-zond/light"
    37  	"github.com/theQRL/go-zond/params"
    38  	"github.com/theQRL/go-zond/rpc"
    39  	"github.com/theQRL/go-zond/zond/gasprice"
    40  	"github.com/theQRL/go-zond/zond/tracers"
    41  	"github.com/theQRL/go-zond/zonddb"
    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.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) GetBody(ctx context.Context, hash common.Hash, number rpc.BlockNumber) (*types.Body, error) {
   133  	return light.GetBody(ctx, b.eth.odr, hash, uint64(number))
   134  }
   135  
   136  func (b *LesApiBackend) PendingBlockAndReceipts() (*types.Block, types.Receipts) {
   137  	return nil, nil
   138  }
   139  
   140  func (b *LesApiBackend) StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error) {
   141  	header, err := b.HeaderByNumber(ctx, number)
   142  	if err != nil {
   143  		return nil, nil, err
   144  	}
   145  	if header == nil {
   146  		return nil, nil, errors.New("header not found")
   147  	}
   148  	return light.NewState(ctx, header, b.eth.odr), header, nil
   149  }
   150  
   151  func (b *LesApiBackend) StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error) {
   152  	if blockNr, ok := blockNrOrHash.Number(); ok {
   153  		return b.StateAndHeaderByNumber(ctx, blockNr)
   154  	}
   155  	if hash, ok := blockNrOrHash.Hash(); ok {
   156  		header := b.eth.blockchain.GetHeaderByHash(hash)
   157  		if header == nil {
   158  			return nil, nil, errors.New("header for hash not found")
   159  		}
   160  		if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash {
   161  			return nil, nil, errors.New("hash is not currently canonical")
   162  		}
   163  		return light.NewState(ctx, header, b.eth.odr), header, nil
   164  	}
   165  	return nil, nil, errors.New("invalid arguments; neither block nor hash specified")
   166  }
   167  
   168  func (b *LesApiBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
   169  	if number := rawdb.ReadHeaderNumber(b.eth.chainDb, hash); number != nil {
   170  		return light.GetBlockReceipts(ctx, b.eth.odr, hash, *number)
   171  	}
   172  	return nil, nil
   173  }
   174  
   175  func (b *LesApiBackend) GetLogs(ctx context.Context, hash common.Hash, number uint64) ([][]*types.Log, error) {
   176  	return light.GetBlockLogs(ctx, b.eth.odr, hash, number)
   177  }
   178  
   179  func (b *LesApiBackend) GetTd(ctx context.Context, hash common.Hash) *big.Int {
   180  	if number := rawdb.ReadHeaderNumber(b.eth.chainDb, hash); number != nil {
   181  		return b.eth.blockchain.GetTdOdr(ctx, hash, *number)
   182  	}
   183  	return nil
   184  }
   185  
   186  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) {
   187  	if vmConfig == nil {
   188  		vmConfig = new(vm.Config)
   189  	}
   190  	txContext := core.NewEVMTxContext(msg)
   191  	context := core.NewEVMBlockContext(header, b.eth.blockchain, nil)
   192  	if blockCtx != nil {
   193  		context = *blockCtx
   194  	}
   195  	return vm.NewEVM(context, txContext, state, b.eth.chainConfig, *vmConfig), state.Error
   196  }
   197  
   198  func (b *LesApiBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
   199  	return b.eth.txPool.Add(ctx, signedTx)
   200  }
   201  
   202  func (b *LesApiBackend) RemoveTx(txHash common.Hash) {
   203  	b.eth.txPool.RemoveTx(txHash)
   204  }
   205  
   206  func (b *LesApiBackend) GetPoolTransactions() (types.Transactions, error) {
   207  	return b.eth.txPool.GetTransactions()
   208  }
   209  
   210  func (b *LesApiBackend) GetPoolTransaction(txHash common.Hash) *types.Transaction {
   211  	return b.eth.txPool.GetTransaction(txHash)
   212  }
   213  
   214  func (b *LesApiBackend) GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) {
   215  	return light.GetTransaction(ctx, b.eth.odr, txHash)
   216  }
   217  
   218  func (b *LesApiBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
   219  	return b.eth.txPool.GetNonce(ctx, addr)
   220  }
   221  
   222  func (b *LesApiBackend) Stats() (pending int, queued int) {
   223  	return b.eth.txPool.Stats(), 0
   224  }
   225  
   226  func (b *LesApiBackend) TxPoolContent() (map[common.Address][]*types.Transaction, map[common.Address][]*types.Transaction) {
   227  	return b.eth.txPool.Content()
   228  }
   229  
   230  func (b *LesApiBackend) TxPoolContentFrom(addr common.Address) ([]*types.Transaction, []*types.Transaction) {
   231  	return b.eth.txPool.ContentFrom(addr)
   232  }
   233  
   234  func (b *LesApiBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription {
   235  	return b.eth.txPool.SubscribeNewTxsEvent(ch)
   236  }
   237  
   238  func (b *LesApiBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
   239  	return b.eth.blockchain.SubscribeChainEvent(ch)
   240  }
   241  
   242  func (b *LesApiBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
   243  	return b.eth.blockchain.SubscribeChainHeadEvent(ch)
   244  }
   245  
   246  func (b *LesApiBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription {
   247  	return b.eth.blockchain.SubscribeChainSideEvent(ch)
   248  }
   249  
   250  func (b *LesApiBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
   251  	return b.eth.blockchain.SubscribeLogsEvent(ch)
   252  }
   253  
   254  func (b *LesApiBackend) SubscribePendingLogsEvent(ch chan<- []*types.Log) event.Subscription {
   255  	return event.NewSubscription(func(quit <-chan struct{}) error {
   256  		<-quit
   257  		return nil
   258  	})
   259  }
   260  
   261  func (b *LesApiBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
   262  	return b.eth.blockchain.SubscribeRemovedLogsEvent(ch)
   263  }
   264  
   265  func (b *LesApiBackend) SyncProgress() zond.SyncProgress {
   266  	return zond.SyncProgress{}
   267  }
   268  
   269  func (b *LesApiBackend) ProtocolVersion() int {
   270  	return b.eth.LesVersion() + 10000
   271  }
   272  
   273  func (b *LesApiBackend) SuggestGasTipCap(ctx context.Context) (*big.Int, error) {
   274  	return b.gpo.SuggestTipCap(ctx)
   275  }
   276  
   277  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) {
   278  	return b.gpo.FeeHistory(ctx, blockCount, lastBlock, rewardPercentiles)
   279  }
   280  
   281  func (b *LesApiBackend) ChainDb() zonddb.Database {
   282  	return b.eth.chainDb
   283  }
   284  
   285  func (b *LesApiBackend) AccountManager() *accounts.Manager {
   286  	return b.eth.accountManager
   287  }
   288  
   289  func (b *LesApiBackend) ExtRPCEnabled() bool {
   290  	return b.extRPCEnabled
   291  }
   292  
   293  func (b *LesApiBackend) UnprotectedAllowed() bool {
   294  	return b.allowUnprotectedTxs
   295  }
   296  
   297  func (b *LesApiBackend) RPCGasCap() uint64 {
   298  	return b.eth.config.RPCGasCap
   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, readOnly bool, preferDisk bool) (*state.StateDB, tracers.StateReleaseFunc, 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, tracers.StateReleaseFunc, error) {
   336  	return b.eth.stateAtTransaction(ctx, block, txIndex, reexec)
   337  }