github.com/puffscoin/go-puffscoin@v0.0.0-20190701205704-e48ad5c90fa1/eth/api_backend.go (about)

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