github.com/smalaichami/go-bowhead@v0.0.0-20180311002552-16302db95eaa/aht/api_backend.go (about)

     1  // Copyright 2015 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 eth
    18  
    19  import (
    20  	"context"
    21  	"math/big"
    22  
    23  	"github.com/smalaichami/go-bowhead/accounts"
    24  	"github.com/smalaichami/go-bowhead/common"
    25  	"github.com/smalaichami/go-bowhead/common/math"
    26  	"github.com/smalaichami/go-bowhead/core"
    27  	"github.com/smalaichami/go-bowhead/core/bloombits"
    28  	"github.com/smalaichami/go-bowhead/core/state"
    29  	"github.com/smalaichami/go-bowhead/core/types"
    30  	"github.com/smalaichami/go-bowhead/core/vm"
    31  	"github.com/smalaichami/go-bowhead/aht/downloader"
    32  	"github.com/smalaichami/go-bowhead/aht/gasprice"
    33  	"github.com/smalaichami/go-bowhead/ahtdb"
    34  	"github.com/smalaichami/go-bowhead/event"
    35  	"github.com/smalaichami/go-bowhead/params"
    36  	"github.com/smalaichami/go-bowhead/rpc"
    37  )
    38  
    39  // EthApiBackend implements ethapi.Backend for full nodes
    40  type EthApiBackend struct {
    41  	eth *Ethereum
    42  	gpo *gasprice.Oracle
    43  }
    44  
    45  func (b *EthApiBackend) ChainConfig() *params.ChainConfig {
    46  	return b.eth.chainConfig
    47  }
    48  
    49  func (b *EthApiBackend) CurrentBlock() *types.Block {
    50  	return b.eth.blockchain.CurrentBlock()
    51  }
    52  
    53  func (b *EthApiBackend) SetHead(number uint64) {
    54  	b.eth.protocolManager.downloader.Cancel()
    55  	b.eth.blockchain.SetHead(number)
    56  }
    57  
    58  func (b *EthApiBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error) {
    59  	// Pending block is only known by the miner
    60  	if blockNr == rpc.PendingBlockNumber {
    61  		block := b.eth.miner.PendingBlock()
    62  		return block.Header(), nil
    63  	}
    64  	// Otherwise resolve and return the block
    65  	if blockNr == rpc.LatestBlockNumber {
    66  		return b.eth.blockchain.CurrentBlock().Header(), nil
    67  	}
    68  	return b.eth.blockchain.GetHeaderByNumber(uint64(blockNr)), nil
    69  }
    70  
    71  func (b *EthApiBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error) {
    72  	// Pending block is only known by the miner
    73  	if blockNr == rpc.PendingBlockNumber {
    74  		block := b.eth.miner.PendingBlock()
    75  		return block, nil
    76  	}
    77  	// Otherwise resolve and return the block
    78  	if blockNr == rpc.LatestBlockNumber {
    79  		return b.eth.blockchain.CurrentBlock(), nil
    80  	}
    81  	return b.eth.blockchain.GetBlockByNumber(uint64(blockNr)), nil
    82  }
    83  
    84  func (b *EthApiBackend) StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*state.StateDB, *types.Header, error) {
    85  	// Pending state is only known by the miner
    86  	if blockNr == rpc.PendingBlockNumber {
    87  		block, state := b.eth.miner.Pending()
    88  		return state, block.Header(), nil
    89  	}
    90  	// Otherwise resolve the block number and return its state
    91  	header, err := b.HeaderByNumber(ctx, blockNr)
    92  	if header == nil || err != nil {
    93  		return nil, nil, err
    94  	}
    95  	stateDb, err := b.eth.BlockChain().StateAt(header.Root)
    96  	return stateDb, header, err
    97  }
    98  
    99  func (b *EthApiBackend) GetBlock(ctx context.Context, blockHash common.Hash) (*types.Block, error) {
   100  	return b.eth.blockchain.GetBlockByHash(blockHash), nil
   101  }
   102  
   103  func (b *EthApiBackend) GetReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error) {
   104  	return core.GetBlockReceipts(b.eth.chainDb, blockHash, core.GetBlockNumber(b.eth.chainDb, blockHash)), nil
   105  }
   106  
   107  func (b *EthApiBackend) GetLogs(ctx context.Context, blockHash common.Hash) ([][]*types.Log, error) {
   108  	receipts := core.GetBlockReceipts(b.eth.chainDb, blockHash, core.GetBlockNumber(b.eth.chainDb, blockHash))
   109  	if receipts == nil {
   110  		return nil, nil
   111  	}
   112  	logs := make([][]*types.Log, len(receipts))
   113  	for i, receipt := range receipts {
   114  		logs[i] = receipt.Logs
   115  	}
   116  	return logs, nil
   117  }
   118  
   119  func (b *EthApiBackend) GetTd(blockHash common.Hash) *big.Int {
   120  	return b.eth.blockchain.GetTdByHash(blockHash)
   121  }
   122  
   123  func (b *EthApiBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmCfg vm.Config) (*vm.EVM, func() error, error) {
   124  	state.SetBalance(msg.From(), math.MaxBig256)
   125  	vmError := func() error { return nil }
   126  
   127  	context := core.NewEVMContext(msg, header, b.eth.BlockChain(), nil)
   128  	return vm.NewEVM(context, state, b.eth.chainConfig, vmCfg), vmError, nil
   129  }
   130  
   131  func (b *EthApiBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
   132  	return b.eth.BlockChain().SubscribeRemovedLogsEvent(ch)
   133  }
   134  
   135  func (b *EthApiBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
   136  	return b.eth.BlockChain().SubscribeChainEvent(ch)
   137  }
   138  
   139  func (b *EthApiBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
   140  	return b.eth.BlockChain().SubscribeChainHeadEvent(ch)
   141  }
   142  
   143  func (b *EthApiBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription {
   144  	return b.eth.BlockChain().SubscribeChainSideEvent(ch)
   145  }
   146  
   147  func (b *EthApiBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
   148  	return b.eth.BlockChain().SubscribeLogsEvent(ch)
   149  }
   150  
   151  func (b *EthApiBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
   152  	return b.eth.txPool.AddLocal(signedTx)
   153  }
   154  
   155  func (b *EthApiBackend) GetPoolTransactions() (types.Transactions, error) {
   156  	pending, err := b.eth.txPool.Pending()
   157  	if err != nil {
   158  		return nil, err
   159  	}
   160  	var txs types.Transactions
   161  	for _, batch := range pending {
   162  		txs = append(txs, batch...)
   163  	}
   164  	return txs, nil
   165  }
   166  
   167  func (b *EthApiBackend) GetPoolTransaction(hash common.Hash) *types.Transaction {
   168  	return b.eth.txPool.Get(hash)
   169  }
   170  
   171  func (b *EthApiBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
   172  	return b.eth.txPool.State().GetNonce(addr), nil
   173  }
   174  
   175  func (b *EthApiBackend) Stats() (pending int, queued int) {
   176  	return b.eth.txPool.Stats()
   177  }
   178  
   179  func (b *EthApiBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
   180  	return b.eth.TxPool().Content()
   181  }
   182  
   183  func (b *EthApiBackend) SubscribeTxPreEvent(ch chan<- core.TxPreEvent) event.Subscription {
   184  	return b.eth.TxPool().SubscribeTxPreEvent(ch)
   185  }
   186  
   187  func (b *EthApiBackend) Downloader() *downloader.Downloader {
   188  	return b.eth.Downloader()
   189  }
   190  
   191  func (b *EthApiBackend) ProtocolVersion() int {
   192  	return b.eth.EthVersion()
   193  }
   194  
   195  func (b *EthApiBackend) SuggestPrice(ctx context.Context) (*big.Int, error) {
   196  	return b.gpo.SuggestPrice(ctx)
   197  }
   198  
   199  func (b *EthApiBackend) ChainDb() ethdb.Database {
   200  	return b.eth.ChainDb()
   201  }
   202  
   203  func (b *EthApiBackend) EventMux() *event.TypeMux {
   204  	return b.eth.EventMux()
   205  }
   206  
   207  func (b *EthApiBackend) AccountManager() *accounts.Manager {
   208  	return b.eth.AccountManager()
   209  }
   210  
   211  func (b *EthApiBackend) BloomStatus() (uint64, uint64) {
   212  	sections, _, _ := b.eth.bloomIndexer.Sections()
   213  	return params.BloomBitsBlocks, sections
   214  }
   215  
   216  func (b *EthApiBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) {
   217  	for i := 0; i < bloomFilterThreads; i++ {
   218  		go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests)
   219  	}
   220  }