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