github.com/bamzi/go-ethereum@v1.6.7-0.20170704111104-138f26c93af1/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/ethereum/go-ethereum/accounts"
    24  	"github.com/ethereum/go-ethereum/common"
    25  	"github.com/ethereum/go-ethereum/common/math"
    26  	"github.com/ethereum/go-ethereum/core"
    27  	"github.com/ethereum/go-ethereum/core/state"
    28  	"github.com/ethereum/go-ethereum/core/types"
    29  	"github.com/ethereum/go-ethereum/core/vm"
    30  	"github.com/ethereum/go-ethereum/eth/downloader"
    31  	"github.com/ethereum/go-ethereum/eth/gasprice"
    32  	"github.com/ethereum/go-ethereum/ethdb"
    33  	"github.com/ethereum/go-ethereum/event"
    34  	"github.com/ethereum/go-ethereum/light"
    35  	"github.com/ethereum/go-ethereum/params"
    36  	"github.com/ethereum/go-ethereum/rpc"
    37  )
    38  
    39  type LesApiBackend struct {
    40  	eth *LightEthereum
    41  	gpo *gasprice.Oracle
    42  }
    43  
    44  func (b *LesApiBackend) ChainConfig() *params.ChainConfig {
    45  	return b.eth.chainConfig
    46  }
    47  
    48  func (b *LesApiBackend) CurrentBlock() *types.Block {
    49  	return types.NewBlockWithHeader(b.eth.BlockChain().CurrentHeader())
    50  }
    51  
    52  func (b *LesApiBackend) SetHead(number uint64) {
    53  	b.eth.protocolManager.downloader.Cancel()
    54  	b.eth.blockchain.SetHead(number)
    55  }
    56  
    57  func (b *LesApiBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error) {
    58  	if blockNr == rpc.LatestBlockNumber || blockNr == rpc.PendingBlockNumber {
    59  		return b.eth.blockchain.CurrentHeader(), nil
    60  	}
    61  
    62  	return b.eth.blockchain.GetHeaderByNumberOdr(ctx, uint64(blockNr))
    63  }
    64  
    65  func (b *LesApiBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error) {
    66  	header, err := b.HeaderByNumber(ctx, blockNr)
    67  	if header == nil || err != nil {
    68  		return nil, err
    69  	}
    70  	return b.GetBlock(ctx, header.Hash())
    71  }
    72  
    73  func (b *LesApiBackend) StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*state.StateDB, *types.Header, error) {
    74  	header, err := b.HeaderByNumber(ctx, blockNr)
    75  	if header == nil || err != nil {
    76  		return nil, nil, err
    77  	}
    78  	return light.NewState(ctx, header, b.eth.odr), header, nil
    79  }
    80  
    81  func (b *LesApiBackend) GetBlock(ctx context.Context, blockHash common.Hash) (*types.Block, error) {
    82  	return b.eth.blockchain.GetBlockByHash(ctx, blockHash)
    83  }
    84  
    85  func (b *LesApiBackend) GetReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error) {
    86  	return light.GetBlockReceipts(ctx, b.eth.odr, blockHash, core.GetBlockNumber(b.eth.chainDb, blockHash))
    87  }
    88  
    89  func (b *LesApiBackend) GetTd(blockHash common.Hash) *big.Int {
    90  	return b.eth.blockchain.GetTdByHash(blockHash)
    91  }
    92  
    93  func (b *LesApiBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmCfg vm.Config) (*vm.EVM, func() error, error) {
    94  	state.SetBalance(msg.From(), math.MaxBig256)
    95  	context := core.NewEVMContext(msg, header, b.eth.blockchain, nil)
    96  	return vm.NewEVM(context, state, b.eth.chainConfig, vmCfg), state.Error, nil
    97  }
    98  
    99  func (b *LesApiBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
   100  	return b.eth.txPool.Add(ctx, signedTx)
   101  }
   102  
   103  func (b *LesApiBackend) RemoveTx(txHash common.Hash) {
   104  	b.eth.txPool.RemoveTx(txHash)
   105  }
   106  
   107  func (b *LesApiBackend) GetPoolTransactions() (types.Transactions, error) {
   108  	return b.eth.txPool.GetTransactions()
   109  }
   110  
   111  func (b *LesApiBackend) GetPoolTransaction(txHash common.Hash) *types.Transaction {
   112  	return b.eth.txPool.GetTransaction(txHash)
   113  }
   114  
   115  func (b *LesApiBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
   116  	return b.eth.txPool.GetNonce(ctx, addr)
   117  }
   118  
   119  func (b *LesApiBackend) Stats() (pending int, queued int) {
   120  	return b.eth.txPool.Stats(), 0
   121  }
   122  
   123  func (b *LesApiBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
   124  	return b.eth.txPool.Content()
   125  }
   126  
   127  func (b *LesApiBackend) Downloader() *downloader.Downloader {
   128  	return b.eth.Downloader()
   129  }
   130  
   131  func (b *LesApiBackend) ProtocolVersion() int {
   132  	return b.eth.LesVersion() + 10000
   133  }
   134  
   135  func (b *LesApiBackend) SuggestPrice(ctx context.Context) (*big.Int, error) {
   136  	return b.gpo.SuggestPrice(ctx)
   137  }
   138  
   139  func (b *LesApiBackend) ChainDb() ethdb.Database {
   140  	return b.eth.chainDb
   141  }
   142  
   143  func (b *LesApiBackend) EventMux() *event.TypeMux {
   144  	return b.eth.eventMux
   145  }
   146  
   147  func (b *LesApiBackend) AccountManager() *accounts.Manager {
   148  	return b.eth.accountManager
   149  }