github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/les/api_backend.go (about)

     1  // Copyright 2016 The Spectrum Authors
     2  // This file is part of the Spectrum library.
     3  //
     4  // The Spectrum 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 Spectrum 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 Spectrum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package les
    18  
    19  import (
    20  	"context"
    21  	"math/big"
    22  
    23  	"github.com/SmartMeshFoundation/Spectrum/accounts"
    24  	"github.com/SmartMeshFoundation/Spectrum/common"
    25  	"github.com/SmartMeshFoundation/Spectrum/common/math"
    26  	"github.com/SmartMeshFoundation/Spectrum/core"
    27  	"github.com/SmartMeshFoundation/Spectrum/core/bloombits"
    28  	"github.com/SmartMeshFoundation/Spectrum/core/state"
    29  	"github.com/SmartMeshFoundation/Spectrum/core/types"
    30  	"github.com/SmartMeshFoundation/Spectrum/core/vm"
    31  	"github.com/SmartMeshFoundation/Spectrum/eth/downloader"
    32  	"github.com/SmartMeshFoundation/Spectrum/eth/gasprice"
    33  	"github.com/SmartMeshFoundation/Spectrum/ethdb"
    34  	"github.com/SmartMeshFoundation/Spectrum/event"
    35  	"github.com/SmartMeshFoundation/Spectrum/light"
    36  	"github.com/SmartMeshFoundation/Spectrum/params"
    37  	"github.com/SmartMeshFoundation/Spectrum/rpc"
    38  )
    39  
    40  type LesApiBackend struct {
    41  	eth *LightEthereum
    42  	gpo *gasprice.Oracle
    43  }
    44  
    45  func (b *LesApiBackend) ChainConfig() *params.ChainConfig {
    46  	return b.eth.chainConfig
    47  }
    48  
    49  func (b *LesApiBackend) CurrentBlock() *types.Block {
    50  	return types.NewBlockWithHeader(b.eth.BlockChain().CurrentHeader())
    51  }
    52  
    53  func (b *LesApiBackend) SetHead(number uint64) {
    54  	b.eth.protocolManager.downloader.Cancel()
    55  	b.eth.blockchain.SetHead(number)
    56  }
    57  
    58  func (b *LesApiBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error) {
    59  	if blockNr == rpc.LatestBlockNumber || blockNr == rpc.PendingBlockNumber {
    60  		return b.eth.blockchain.CurrentHeader(), nil
    61  	}
    62  
    63  	return b.eth.blockchain.GetHeaderByNumberOdr(ctx, uint64(blockNr))
    64  }
    65  
    66  func (b *LesApiBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error) {
    67  	header, err := b.HeaderByNumber(ctx, blockNr)
    68  	if header == nil || err != nil {
    69  		return nil, err
    70  	}
    71  	return b.GetBlock(ctx, header.Hash())
    72  }
    73  
    74  // add by liangc : only for execute contract by hash
    75  func (b *LesApiBackend) StateAndHeaderByHash(ctx context.Context, hash *common.Hash) (*state.StateDB, *types.Header, error) {
    76  	blk, err := b.GetBlock(ctx, *hash)
    77  	if err != nil {
    78  		return nil, nil, err
    79  	}
    80  	header := blk.Header()
    81  	return light.NewState(ctx, header, b.eth.odr), header, nil
    82  }
    83  
    84  func (b *LesApiBackend) StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*state.StateDB, *types.Header, error) {
    85  	header, err := b.HeaderByNumber(ctx, blockNr)
    86  	if header == nil || err != nil {
    87  		return nil, nil, err
    88  	}
    89  	return light.NewState(ctx, header, b.eth.odr), header, nil
    90  }
    91  
    92  func (b *LesApiBackend) GetBlock(ctx context.Context, blockHash common.Hash) (*types.Block, error) {
    93  	return b.eth.blockchain.GetBlockByHash(ctx, blockHash)
    94  }
    95  
    96  func (b *LesApiBackend) GetReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error) {
    97  	return light.GetBlockReceipts(ctx, b.eth.odr, blockHash, core.GetBlockNumber(b.eth.chainDb, blockHash))
    98  }
    99  
   100  func (b *LesApiBackend) GetTd(blockHash common.Hash) *big.Int {
   101  	return b.eth.blockchain.GetTdByHash(blockHash)
   102  }
   103  
   104  func (b *LesApiBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmCfg vm.Config) (*vm.EVM, func() error, error) {
   105  	state.SetBalance(msg.From(), math.MaxBig256)
   106  	context := core.NewEVMContext(msg, header, b.eth.blockchain, nil)
   107  	return vm.NewEVM(context, state, b.eth.chainConfig, vmCfg), state.Error, nil
   108  }
   109  
   110  func (b *LesApiBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
   111  	return b.eth.txPool.Add(ctx, signedTx)
   112  }
   113  
   114  func (b *LesApiBackend) RemoveTx(txHash common.Hash) {
   115  	b.eth.txPool.RemoveTx(txHash)
   116  }
   117  
   118  func (b *LesApiBackend) GetPoolTransactions() (types.Transactions, error) {
   119  	return b.eth.txPool.GetTransactions()
   120  }
   121  
   122  func (b *LesApiBackend) GetPoolTransaction(txHash common.Hash) *types.Transaction {
   123  	return b.eth.txPool.GetTransaction(txHash)
   124  }
   125  
   126  func (b *LesApiBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
   127  	return b.eth.txPool.GetNonce(ctx, addr)
   128  }
   129  
   130  func (b *LesApiBackend) Stats() (pending int, queued int) {
   131  	return b.eth.txPool.Stats(), 0
   132  }
   133  
   134  func (b *LesApiBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
   135  	return b.eth.txPool.Content()
   136  }
   137  
   138  func (b *LesApiBackend) SubscribeTxPreEvent(ch chan<- core.TxPreEvent) event.Subscription {
   139  	return b.eth.txPool.SubscribeTxPreEvent(ch)
   140  }
   141  
   142  func (b *LesApiBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
   143  	return b.eth.blockchain.SubscribeChainEvent(ch)
   144  }
   145  
   146  func (b *LesApiBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
   147  	return b.eth.blockchain.SubscribeChainHeadEvent(ch)
   148  }
   149  
   150  func (b *LesApiBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription {
   151  	return b.eth.blockchain.SubscribeChainSideEvent(ch)
   152  }
   153  
   154  func (b *LesApiBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
   155  	return b.eth.blockchain.SubscribeLogsEvent(ch)
   156  }
   157  
   158  func (b *LesApiBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
   159  	return b.eth.blockchain.SubscribeRemovedLogsEvent(ch)
   160  }
   161  
   162  func (b *LesApiBackend) Downloader() *downloader.Downloader {
   163  	return b.eth.Downloader()
   164  }
   165  
   166  func (b *LesApiBackend) ProtocolVersion() int {
   167  	return b.eth.LesVersion() + 10000
   168  }
   169  
   170  func (b *LesApiBackend) SuggestPrice(ctx context.Context) (*big.Int, error) {
   171  	return b.gpo.SuggestPrice(ctx)
   172  }
   173  
   174  func (b *LesApiBackend) ChainDb() ethdb.Database {
   175  	return b.eth.chainDb
   176  }
   177  
   178  func (b *LesApiBackend) EventMux() *event.TypeMux {
   179  	return b.eth.eventMux
   180  }
   181  
   182  func (b *LesApiBackend) AccountManager() *accounts.Manager {
   183  	return b.eth.accountManager
   184  }
   185  
   186  func (b *LesApiBackend) BloomStatus() (uint64, uint64) {
   187  	if b.eth.bloomIndexer == nil {
   188  		return 0, 0
   189  	}
   190  	sections, _, _ := b.eth.bloomIndexer.Sections()
   191  	return light.BloomTrieFrequency, sections
   192  }
   193  
   194  func (b *LesApiBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) {
   195  	for i := 0; i < bloomFilterThreads; i++ {
   196  		go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests)
   197  	}
   198  }