github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/les/api_backend.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:38</date>
    10  //</624450093092704256>
    11  
    12  
    13  package les
    14  
    15  import (
    16  	"context"
    17  	"math/big"
    18  
    19  	"github.com/ethereum/go-ethereum/accounts"
    20  	"github.com/ethereum/go-ethereum/common"
    21  	"github.com/ethereum/go-ethereum/common/math"
    22  	"github.com/ethereum/go-ethereum/core"
    23  	"github.com/ethereum/go-ethereum/core/bloombits"
    24  	"github.com/ethereum/go-ethereum/core/rawdb"
    25  	"github.com/ethereum/go-ethereum/core/state"
    26  	"github.com/ethereum/go-ethereum/core/types"
    27  	"github.com/ethereum/go-ethereum/core/vm"
    28  	"github.com/ethereum/go-ethereum/eth/downloader"
    29  	"github.com/ethereum/go-ethereum/eth/gasprice"
    30  	"github.com/ethereum/go-ethereum/ethdb"
    31  	"github.com/ethereum/go-ethereum/event"
    32  	"github.com/ethereum/go-ethereum/light"
    33  	"github.com/ethereum/go-ethereum/params"
    34  	"github.com/ethereum/go-ethereum/rpc"
    35  )
    36  
    37  type LesApiBackend struct {
    38  	eth *LightEthereum
    39  	gpo *gasprice.Oracle
    40  }
    41  
    42  func (b *LesApiBackend) ChainConfig() *params.ChainConfig {
    43  	return b.eth.chainConfig
    44  }
    45  
    46  func (b *LesApiBackend) CurrentBlock() *types.Block {
    47  	return types.NewBlockWithHeader(b.eth.BlockChain().CurrentHeader())
    48  }
    49  
    50  func (b *LesApiBackend) SetHead(number uint64) {
    51  	b.eth.protocolManager.downloader.Cancel()
    52  	b.eth.blockchain.SetHead(number)
    53  }
    54  
    55  func (b *LesApiBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error) {
    56  	if blockNr == rpc.LatestBlockNumber || blockNr == rpc.PendingBlockNumber {
    57  		return b.eth.blockchain.CurrentHeader(), nil
    58  	}
    59  	return b.eth.blockchain.GetHeaderByNumberOdr(ctx, uint64(blockNr))
    60  }
    61  
    62  func (b *LesApiBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
    63  	return b.eth.blockchain.GetHeaderByHash(hash), nil
    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  func (b *LesApiBackend) StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*state.StateDB, *types.Header, error) {
    75  	header, err := b.HeaderByNumber(ctx, blockNr)
    76  	if header == nil || err != nil {
    77  		return nil, nil, err
    78  	}
    79  	return light.NewState(ctx, header, b.eth.odr), header, nil
    80  }
    81  
    82  func (b *LesApiBackend) GetBlock(ctx context.Context, blockHash common.Hash) (*types.Block, error) {
    83  	return b.eth.blockchain.GetBlockByHash(ctx, blockHash)
    84  }
    85  
    86  func (b *LesApiBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
    87  	if number := rawdb.ReadHeaderNumber(b.eth.chainDb, hash); number != nil {
    88  		return light.GetBlockReceipts(ctx, b.eth.odr, hash, *number)
    89  	}
    90  	return nil, nil
    91  }
    92  
    93  func (b *LesApiBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) {
    94  	if number := rawdb.ReadHeaderNumber(b.eth.chainDb, hash); number != nil {
    95  		return light.GetBlockLogs(ctx, b.eth.odr, hash, *number)
    96  	}
    97  	return nil, nil
    98  }
    99  
   100  func (b *LesApiBackend) GetTd(hash common.Hash) *big.Int {
   101  	return b.eth.blockchain.GetTdByHash(hash)
   102  }
   103  
   104  func (b *LesApiBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header) (*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, vm.Config{}), 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) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription {
   139  	return b.eth.txPool.SubscribeNewTxsEvent(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 params.BloomBitsBlocksClient, 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  }
   199