github.com/aidoskuneen/adk-node@v0.0.0-20220315131952-2e32567cb7f4/les/api_backend.go (about)

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