github.com/vantum/vantum@v0.0.0-20180815184342-fe37d5f7a990/eth/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/vantum/vantum/accounts"
    24  	"github.com/vantum/vantum/common"
    25  	"github.com/vantum/vantum/common/math"
    26  	"github.com/vantum/vantum/core"
    27  	"github.com/vantum/vantum/core/bloombits"
    28  	"github.com/vantum/vantum/core/state"
    29  	"github.com/vantum/vantum/core/types"
    30  	"github.com/vantum/vantum/core/vm"
    31  	"github.com/vantum/vantum/eth/downloader"
    32  	"github.com/vantum/vantum/eth/gasprice"
    33  	"github.com/vantum/vantum/ethdb"
    34  	"github.com/vantum/vantum/event"
    35  	"github.com/vantum/vantum/params"
    36  	"github.com/vantum/vantum/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) GetTd(blockHash common.Hash) *big.Int {
   108  	return b.eth.blockchain.GetTdByHash(blockHash)
   109  }
   110  
   111  func (b *EthApiBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmCfg vm.Config) (*vm.EVM, func() error, error) {
   112  	state.SetBalance(msg.From(), math.MaxBig256)
   113  	vmError := func() error { return nil }
   114  
   115  	context := core.NewEVMContext(msg, header, b.eth.BlockChain(), nil)
   116  	return vm.NewEVM(context, state, b.eth.chainConfig, vmCfg), vmError, nil
   117  }
   118  
   119  func (b *EthApiBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
   120  	return b.eth.BlockChain().SubscribeRemovedLogsEvent(ch)
   121  }
   122  
   123  func (b *EthApiBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
   124  	return b.eth.BlockChain().SubscribeChainEvent(ch)
   125  }
   126  
   127  func (b *EthApiBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
   128  	return b.eth.BlockChain().SubscribeChainHeadEvent(ch)
   129  }
   130  
   131  func (b *EthApiBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription {
   132  	return b.eth.BlockChain().SubscribeChainSideEvent(ch)
   133  }
   134  
   135  func (b *EthApiBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
   136  	return b.eth.BlockChain().SubscribeLogsEvent(ch)
   137  }
   138  
   139  func (b *EthApiBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
   140  	return b.eth.txPool.AddLocal(signedTx)
   141  }
   142  
   143  func (b *EthApiBackend) GetPoolTransactions() (types.Transactions, error) {
   144  	pending, err := b.eth.txPool.Pending()
   145  	if err != nil {
   146  		return nil, err
   147  	}
   148  	var txs types.Transactions
   149  	for _, batch := range pending {
   150  		txs = append(txs, batch...)
   151  	}
   152  	return txs, nil
   153  }
   154  
   155  func (b *EthApiBackend) GetPoolTransaction(hash common.Hash) *types.Transaction {
   156  	return b.eth.txPool.Get(hash)
   157  }
   158  
   159  func (b *EthApiBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
   160  	return b.eth.txPool.State().GetNonce(addr), nil
   161  }
   162  
   163  func (b *EthApiBackend) Stats() (pending int, queued int) {
   164  	return b.eth.txPool.Stats()
   165  }
   166  
   167  func (b *EthApiBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
   168  	return b.eth.TxPool().Content()
   169  }
   170  
   171  func (b *EthApiBackend) SubscribeTxPreEvent(ch chan<- core.TxPreEvent) event.Subscription {
   172  	return b.eth.TxPool().SubscribeTxPreEvent(ch)
   173  }
   174  
   175  func (b *EthApiBackend) Downloader() *downloader.Downloader {
   176  	return b.eth.Downloader()
   177  }
   178  
   179  func (b *EthApiBackend) ProtocolVersion() int {
   180  	return b.eth.EthVersion()
   181  }
   182  
   183  func (b *EthApiBackend) SuggestPrice(ctx context.Context) (*big.Int, error) {
   184  	return b.gpo.SuggestPrice(ctx)
   185  }
   186  
   187  func (b *EthApiBackend) ChainDb() ethdb.Database {
   188  	return b.eth.ChainDb()
   189  }
   190  
   191  func (b *EthApiBackend) EventMux() *event.TypeMux {
   192  	return b.eth.EventMux()
   193  }
   194  
   195  func (b *EthApiBackend) AccountManager() *accounts.Manager {
   196  	return b.eth.AccountManager()
   197  }
   198  
   199  func (b *EthApiBackend) BloomStatus() (uint64, uint64) {
   200  	sections, _, _ := b.eth.bloomIndexer.Sections()
   201  	return params.BloomBitsBlocks, sections
   202  }
   203  
   204  func (b *EthApiBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) {
   205  	for i := 0; i < bloomFilterThreads; i++ {
   206  		go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests)
   207  	}
   208  }