github.com/beyonderyue/gochain@v2.2.26+incompatible/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  	"math/big"
    22  
    23  	"github.com/gochain-io/gochain/accounts"
    24  	"github.com/gochain-io/gochain/common"
    25  	"github.com/gochain-io/gochain/common/math"
    26  	"github.com/gochain-io/gochain/core"
    27  	"github.com/gochain-io/gochain/core/bloombits"
    28  	"github.com/gochain-io/gochain/core/rawdb"
    29  	"github.com/gochain-io/gochain/core/state"
    30  	"github.com/gochain-io/gochain/core/types"
    31  	"github.com/gochain-io/gochain/core/vm"
    32  	"github.com/gochain-io/gochain/eth/downloader"
    33  	"github.com/gochain-io/gochain/eth/gasprice"
    34  	"github.com/gochain-io/gochain/event"
    35  	"github.com/gochain-io/gochain/light"
    36  	"github.com/gochain-io/gochain/params"
    37  	"github.com/gochain-io/gochain/rpc"
    38  )
    39  
    40  type LesApiBackend struct {
    41  	eth           *LightGoChain
    42  	initialSupply *big.Int
    43  	gpo           *gasprice.Oracle
    44  }
    45  
    46  func (b *LesApiBackend) ChainConfig() *params.ChainConfig {
    47  	return b.eth.chainConfig
    48  }
    49  
    50  func (b *LesApiBackend) InitialSupply() *big.Int {
    51  	return b.initialSupply
    52  }
    53  
    54  func (b *LesApiBackend) GenesisAlloc() core.GenesisAlloc {
    55  	if g := b.eth.config.Genesis; g != nil {
    56  		return g.Alloc
    57  	}
    58  	return nil
    59  }
    60  
    61  func (b *LesApiBackend) CurrentBlock() *types.Block {
    62  	return types.NewBlockWithHeader(b.eth.BlockChain().CurrentHeader())
    63  }
    64  
    65  func (b *LesApiBackend) SetHead(number uint64) {
    66  	b.eth.protocolManager.downloader.Cancel()
    67  	b.eth.blockchain.SetHead(number)
    68  }
    69  
    70  func (b *LesApiBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error) {
    71  	if blockNr == rpc.LatestBlockNumber || blockNr == rpc.PendingBlockNumber {
    72  		return b.eth.blockchain.CurrentHeader(), nil
    73  	}
    74  
    75  	return b.eth.blockchain.GetHeaderByNumberOdr(ctx, uint64(blockNr))
    76  }
    77  
    78  func (b *LesApiBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
    79  	return b.eth.blockchain.GetHeaderByHash(hash), nil
    80  }
    81  
    82  func (b *LesApiBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error) {
    83  	header, err := b.HeaderByNumber(ctx, blockNr)
    84  	if header == nil || err != nil {
    85  		return nil, err
    86  	}
    87  	return b.GetBlock(ctx, header.Hash())
    88  }
    89  
    90  func (b *LesApiBackend) StateQuery(ctx context.Context, blockNr rpc.BlockNumber, fn func(*state.StateDB) error) error {
    91  	header, err := b.HeaderByNumber(ctx, blockNr)
    92  	if header == nil || err != nil {
    93  		return err
    94  	}
    95  	return fn(light.NewState(ctx, header, b.eth.odr))
    96  }
    97  
    98  func (b *LesApiBackend) StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*state.StateDB, *types.Header, error) {
    99  	header, err := b.HeaderByNumber(ctx, blockNr)
   100  	if header == nil || err != nil {
   101  		return nil, nil, err
   102  	}
   103  	return light.NewState(ctx, header, b.eth.odr), header, nil
   104  }
   105  
   106  func (b *LesApiBackend) GetBlock(ctx context.Context, blockHash common.Hash) (*types.Block, error) {
   107  	return b.eth.blockchain.GetBlockByHash(ctx, blockHash)
   108  }
   109  
   110  func (b *LesApiBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
   111  	if number := rawdb.ReadHeaderNumber(b.eth.chainDb.GlobalTable(), hash); number != nil {
   112  		return light.GetBlockReceipts(ctx, b.eth.odr, hash, *number)
   113  	}
   114  	return nil, nil
   115  }
   116  
   117  func (b *LesApiBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) {
   118  	if number := rawdb.ReadHeaderNumber(b.eth.chainDb.GlobalTable(), hash); number != nil {
   119  		return light.GetBlockLogs(ctx, b.eth.odr, hash, *number)
   120  	}
   121  	return nil, nil
   122  }
   123  
   124  func (b *LesApiBackend) GetTd(blockHash common.Hash) *big.Int {
   125  	return b.eth.blockchain.GetTdByHash(blockHash)
   126  }
   127  
   128  func (b *LesApiBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmCfg vm.Config) (*vm.EVM, error) {
   129  	state.SetBalance(msg.From(), math.MaxBig256)
   130  	context := core.NewEVMContext(msg, header, b.eth.blockchain, nil)
   131  	return vm.NewEVM(context, state, b.eth.chainConfig, vmCfg), nil
   132  }
   133  
   134  func (b *LesApiBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
   135  	return b.eth.txPool.Add(ctx, signedTx)
   136  }
   137  
   138  func (b *LesApiBackend) RemoveTx(txHash common.Hash) {
   139  	b.eth.txPool.RemoveTx(txHash)
   140  }
   141  
   142  func (b *LesApiBackend) GetPoolTransactions() types.Transactions {
   143  	return b.eth.txPool.GetTransactions()
   144  }
   145  
   146  func (b *LesApiBackend) GetPoolTransaction(txHash common.Hash) *types.Transaction {
   147  	return b.eth.txPool.GetTransaction(txHash)
   148  }
   149  
   150  func (b *LesApiBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
   151  	return b.eth.txPool.GetNonce(ctx, addr)
   152  }
   153  
   154  func (b *LesApiBackend) Stats() (pending int, queued int) {
   155  	return b.eth.txPool.Stats(), 0
   156  }
   157  
   158  func (b *LesApiBackend) TxPoolContent(ctx context.Context) (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
   159  	return b.eth.txPool.Content(ctx)
   160  }
   161  
   162  func (b *LesApiBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent, name string) {
   163  	b.eth.txPool.SubscribeNewTxsEvent(ch, name)
   164  }
   165  
   166  func (b *LesApiBackend) UnsubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) {
   167  	b.eth.txPool.UnsubscribeNewTxsEvent(ch)
   168  }
   169  
   170  func (b *LesApiBackend) SubscribeChainEvent(ch chan<- core.ChainEvent, name string) {
   171  	b.eth.blockchain.SubscribeChainEvent(ch, name)
   172  }
   173  
   174  func (b *LesApiBackend) UnsubscribeChainEvent(ch chan<- core.ChainEvent) {
   175  	b.eth.blockchain.UnsubscribeChainEvent(ch)
   176  }
   177  
   178  func (b *LesApiBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent, name string) {
   179  	b.eth.blockchain.SubscribeChainHeadEvent(ch, name)
   180  }
   181  
   182  func (b *LesApiBackend) UnsubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) {
   183  	b.eth.blockchain.UnsubscribeChainHeadEvent(ch)
   184  }
   185  
   186  func (b *LesApiBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent, name string) {
   187  	b.eth.blockchain.SubscribeChainSideEvent(ch, name)
   188  }
   189  
   190  func (b *LesApiBackend) UnsubscribeChainSideEvent(ch chan<- core.ChainSideEvent) {
   191  	b.eth.blockchain.UnsubscribeChainSideEvent(ch)
   192  }
   193  
   194  func (b *LesApiBackend) SubscribeLogsEvent(ch chan<- []*types.Log, name string) {}
   195  
   196  func (b *LesApiBackend) UnsubscribeLogsEvent(ch chan<- []*types.Log) {}
   197  
   198  func (b *LesApiBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent, name string) {}
   199  
   200  func (b *LesApiBackend) UnsubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) {}
   201  
   202  func (b *LesApiBackend) Downloader() *downloader.Downloader {
   203  	return b.eth.Downloader()
   204  }
   205  
   206  func (b *LesApiBackend) ProtocolVersion() int {
   207  	return b.eth.LesVersion() + 10000
   208  }
   209  
   210  func (b *LesApiBackend) SuggestPrice(ctx context.Context) (*big.Int, error) {
   211  	return b.gpo.SuggestPrice(ctx)
   212  }
   213  
   214  func (b *LesApiBackend) ChainDb() common.Database {
   215  	return b.eth.chainDb
   216  }
   217  
   218  func (b *LesApiBackend) EventMux() *event.TypeMux {
   219  	return b.eth.eventMux
   220  }
   221  
   222  func (b *LesApiBackend) AccountManager() *accounts.Manager {
   223  	return b.eth.accountManager
   224  }
   225  
   226  func (b *LesApiBackend) BloomStatus() (uint64, uint64) {
   227  	if b.eth.bloomIndexer == nil {
   228  		return 0, 0
   229  	}
   230  	sections, _, _ := b.eth.bloomIndexer.Sections()
   231  	return light.BloomTrieFrequency, sections
   232  }
   233  
   234  func (b *LesApiBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) {
   235  	for i := 0; i < bloomFilterThreads; i++ {
   236  		go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests)
   237  	}
   238  }