github.com/aswedchain/aswed@v1.0.1/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  	"errors"
    22  	"github.com/aswedchain/aswed/internal/ethapi"
    23  	"math/big"
    24  
    25  	"github.com/aswedchain/aswed/accounts"
    26  	"github.com/aswedchain/aswed/common"
    27  	"github.com/aswedchain/aswed/consensus"
    28  	"github.com/aswedchain/aswed/core"
    29  	"github.com/aswedchain/aswed/core/bloombits"
    30  	"github.com/aswedchain/aswed/core/rawdb"
    31  	"github.com/aswedchain/aswed/core/state"
    32  	"github.com/aswedchain/aswed/core/types"
    33  	"github.com/aswedchain/aswed/core/vm"
    34  	"github.com/aswedchain/aswed/eth/downloader"
    35  	"github.com/aswedchain/aswed/eth/gasprice"
    36  	"github.com/aswedchain/aswed/ethdb"
    37  	"github.com/aswedchain/aswed/event"
    38  	"github.com/aswedchain/aswed/miner"
    39  	"github.com/aswedchain/aswed/params"
    40  	"github.com/aswedchain/aswed/rpc"
    41  )
    42  
    43  // EthAPIBackend implements ethapi.Backend for full nodes
    44  type EthAPIBackend struct {
    45  	extRPCEnabled bool
    46  	eth           *Ethereum
    47  	gpo           *gasprice.Oracle
    48  	gpp           *gasprice.Prediction
    49  }
    50  
    51  // ChainConfig returns the active chain configuration.
    52  func (b *EthAPIBackend) ChainConfig() *params.ChainConfig {
    53  	return b.eth.blockchain.Config()
    54  }
    55  
    56  func (b *EthAPIBackend) CurrentBlock() *types.Block {
    57  	return b.eth.blockchain.CurrentBlock()
    58  }
    59  
    60  func (b *EthAPIBackend) SetHead(number uint64) {
    61  	b.eth.protocolManager.downloader.Cancel()
    62  	b.eth.blockchain.SetHead(number)
    63  }
    64  
    65  func (b *EthAPIBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) {
    66  	// Pending block is only known by the miner
    67  	if number == rpc.PendingBlockNumber {
    68  		block := b.eth.miner.PendingBlock()
    69  		return block.Header(), nil
    70  	}
    71  	// Otherwise resolve and return the block
    72  	if number == rpc.LatestBlockNumber {
    73  		return b.eth.blockchain.CurrentBlock().Header(), nil
    74  	}
    75  	return b.eth.blockchain.GetHeaderByNumber(uint64(number)), nil
    76  }
    77  
    78  func (b *EthAPIBackend) HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error) {
    79  	if blockNr, ok := blockNrOrHash.Number(); ok {
    80  		return b.HeaderByNumber(ctx, blockNr)
    81  	}
    82  	if hash, ok := blockNrOrHash.Hash(); ok {
    83  		header := b.eth.blockchain.GetHeaderByHash(hash)
    84  		if header == nil {
    85  			return nil, errors.New("header for hash not found")
    86  		}
    87  		if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash {
    88  			return nil, errors.New("hash is not currently canonical")
    89  		}
    90  		return header, nil
    91  	}
    92  	return nil, errors.New("invalid arguments; neither block nor hash specified")
    93  }
    94  
    95  func (b *EthAPIBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
    96  	return b.eth.blockchain.GetHeaderByHash(hash), nil
    97  }
    98  
    99  func (b *EthAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) {
   100  	// Pending block is only known by the miner
   101  	if number == rpc.PendingBlockNumber {
   102  		block := b.eth.miner.PendingBlock()
   103  		return block, nil
   104  	}
   105  	// Otherwise resolve and return the block
   106  	if number == rpc.LatestBlockNumber {
   107  		return b.eth.blockchain.CurrentBlock(), nil
   108  	}
   109  	return b.eth.blockchain.GetBlockByNumber(uint64(number)), nil
   110  }
   111  
   112  func (b *EthAPIBackend) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) {
   113  	return b.eth.blockchain.GetBlockByHash(hash), nil
   114  }
   115  
   116  func (b *EthAPIBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error) {
   117  	if blockNr, ok := blockNrOrHash.Number(); ok {
   118  		return b.BlockByNumber(ctx, blockNr)
   119  	}
   120  	if hash, ok := blockNrOrHash.Hash(); ok {
   121  		header := b.eth.blockchain.GetHeaderByHash(hash)
   122  		if header == nil {
   123  			return nil, errors.New("header for hash not found")
   124  		}
   125  		if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash {
   126  			return nil, errors.New("hash is not currently canonical")
   127  		}
   128  		block := b.eth.blockchain.GetBlock(hash, header.Number.Uint64())
   129  		if block == nil {
   130  			return nil, errors.New("header found, but block body is missing")
   131  		}
   132  		return block, nil
   133  	}
   134  	return nil, errors.New("invalid arguments; neither block nor hash specified")
   135  }
   136  
   137  func (b *EthAPIBackend) StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error) {
   138  	// Pending state is only known by the miner
   139  	if number == rpc.PendingBlockNumber {
   140  		block, state := b.eth.miner.Pending()
   141  		return state, block.Header(), nil
   142  	}
   143  	// Otherwise resolve the block number and return its state
   144  	header, err := b.HeaderByNumber(ctx, number)
   145  	if err != nil {
   146  		return nil, nil, err
   147  	}
   148  	if header == nil {
   149  		return nil, nil, errors.New("header not found")
   150  	}
   151  	stateDb, err := b.eth.BlockChain().StateAt(header.Root)
   152  	return stateDb, header, err
   153  }
   154  
   155  func (b *EthAPIBackend) StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error) {
   156  	if blockNr, ok := blockNrOrHash.Number(); ok {
   157  		return b.StateAndHeaderByNumber(ctx, blockNr)
   158  	}
   159  	if hash, ok := blockNrOrHash.Hash(); ok {
   160  		header, err := b.HeaderByHash(ctx, hash)
   161  		if err != nil {
   162  			return nil, nil, err
   163  		}
   164  		if header == nil {
   165  			return nil, nil, errors.New("header for hash not found")
   166  		}
   167  		if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash {
   168  			return nil, nil, errors.New("hash is not currently canonical")
   169  		}
   170  		stateDb, err := b.eth.BlockChain().StateAt(header.Root)
   171  		return stateDb, header, err
   172  	}
   173  	return nil, nil, errors.New("invalid arguments; neither block nor hash specified")
   174  }
   175  
   176  func (b *EthAPIBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
   177  	return b.eth.blockchain.GetReceiptsByHash(hash), nil
   178  }
   179  
   180  func (b *EthAPIBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) {
   181  	receipts := b.eth.blockchain.GetReceiptsByHash(hash)
   182  	if receipts == nil {
   183  		return nil, nil
   184  	}
   185  	logs := make([][]*types.Log, len(receipts))
   186  	for i, receipt := range receipts {
   187  		logs[i] = receipt.Logs
   188  	}
   189  	return logs, nil
   190  }
   191  
   192  func (b *EthAPIBackend) GetTd(ctx context.Context, hash common.Hash) *big.Int {
   193  	return b.eth.blockchain.GetTdByHash(hash)
   194  }
   195  
   196  func (b *EthAPIBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header) (*vm.EVM, func() error, error) {
   197  	vmError := func() error { return nil }
   198  
   199  	context := core.NewEVMContext(msg, header, b.eth.BlockChain(), nil)
   200  	return vm.NewEVM(context, state, b.eth.blockchain.Config(), *b.eth.blockchain.GetVMConfig()), vmError, nil
   201  }
   202  
   203  func (b *EthAPIBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
   204  	return b.eth.BlockChain().SubscribeRemovedLogsEvent(ch)
   205  }
   206  
   207  func (b *EthAPIBackend) SubscribePendingLogsEvent(ch chan<- []*types.Log) event.Subscription {
   208  	return b.eth.miner.SubscribePendingLogs(ch)
   209  }
   210  
   211  func (b *EthAPIBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
   212  	return b.eth.BlockChain().SubscribeChainEvent(ch)
   213  }
   214  
   215  func (b *EthAPIBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
   216  	return b.eth.BlockChain().SubscribeChainHeadEvent(ch)
   217  }
   218  
   219  func (b *EthAPIBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription {
   220  	return b.eth.BlockChain().SubscribeChainSideEvent(ch)
   221  }
   222  
   223  func (b *EthAPIBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
   224  	return b.eth.BlockChain().SubscribeLogsEvent(ch)
   225  }
   226  
   227  func (b *EthAPIBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
   228  	return b.eth.txPool.AddLocal(signedTx)
   229  }
   230  
   231  func (b *EthAPIBackend) GetPoolTransactions() (types.Transactions, error) {
   232  	pending, err := b.eth.txPool.Pending()
   233  	if err != nil {
   234  		return nil, err
   235  	}
   236  	var txs types.Transactions
   237  	for _, batch := range pending {
   238  		txs = append(txs, batch...)
   239  	}
   240  	return txs, nil
   241  }
   242  
   243  func (b *EthAPIBackend) GetPoolTransaction(hash common.Hash) *types.Transaction {
   244  	return b.eth.txPool.Get(hash)
   245  }
   246  
   247  func (b *EthAPIBackend) GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) {
   248  	tx, blockHash, blockNumber, index := rawdb.ReadTransaction(b.eth.ChainDb(), txHash)
   249  	return tx, blockHash, blockNumber, index, nil
   250  }
   251  
   252  func (b *EthAPIBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
   253  	return b.eth.txPool.Nonce(addr), nil
   254  }
   255  
   256  func (b *EthAPIBackend) Stats() (pending int, queued int) {
   257  	return b.eth.txPool.Stats()
   258  }
   259  
   260  func (b *EthAPIBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
   261  	return b.eth.TxPool().Content()
   262  }
   263  
   264  func (b *EthAPIBackend) JamIndex() int {
   265  	return b.eth.TxPool().JamIndex()
   266  }
   267  
   268  func (b *EthAPIBackend) TxPool() *core.TxPool {
   269  	return b.eth.TxPool()
   270  }
   271  
   272  func (b *EthAPIBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription {
   273  	return b.eth.TxPool().SubscribeNewTxsEvent(ch)
   274  }
   275  
   276  func (b *EthAPIBackend) Downloader() *downloader.Downloader {
   277  	return b.eth.Downloader()
   278  }
   279  
   280  func (b *EthAPIBackend) ProtocolVersion() int {
   281  	return b.eth.EthVersion()
   282  }
   283  
   284  func (b *EthAPIBackend) SuggestPrice(ctx context.Context) (*big.Int, error) {
   285  	return b.gpo.SuggestPrice(ctx)
   286  }
   287  
   288  func (b *EthAPIBackend) PricePrediction(ctx context.Context) ([]uint, error) {
   289  	return b.gpp.CurrentPrices(), nil
   290  }
   291  
   292  func (b *EthAPIBackend) ChainDb() ethdb.Database {
   293  	return b.eth.ChainDb()
   294  }
   295  
   296  func (b *EthAPIBackend) EventMux() *event.TypeMux {
   297  	return b.eth.EventMux()
   298  }
   299  
   300  func (b *EthAPIBackend) AccountManager() *accounts.Manager {
   301  	return b.eth.AccountManager()
   302  }
   303  
   304  func (b *EthAPIBackend) ExtRPCEnabled() bool {
   305  	return b.extRPCEnabled
   306  }
   307  
   308  func (b *EthAPIBackend) RPCGasCap() uint64 {
   309  	return b.eth.config.RPCGasCap
   310  }
   311  
   312  func (b *EthAPIBackend) RPCTxFeeCap() float64 {
   313  	return b.eth.config.RPCTxFeeCap
   314  }
   315  
   316  func (b *EthAPIBackend) BloomStatus() (uint64, uint64) {
   317  	sections, _, _ := b.eth.bloomIndexer.Sections()
   318  	return params.BloomBitsBlocks, sections
   319  }
   320  
   321  func (b *EthAPIBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) {
   322  	for i := 0; i < bloomFilterThreads; i++ {
   323  		go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests)
   324  	}
   325  }
   326  
   327  func (b *EthAPIBackend) Engine() consensus.Engine {
   328  	return b.eth.engine
   329  }
   330  
   331  func (b *EthAPIBackend) CurrentHeader() *types.Header {
   332  	return b.eth.blockchain.CurrentHeader()
   333  }
   334  
   335  func (b *EthAPIBackend) Miner() *miner.Miner {
   336  	return b.eth.Miner()
   337  }
   338  
   339  func (b *EthAPIBackend) StartMining(threads int) error {
   340  	return b.eth.StartMining(threads)
   341  }
   342  
   343  func (b *EthAPIBackend) TraceBlock(ctx context.Context, block *types.Block, tracer string) ([]*ethapi.TxTraceResult, error) {
   344  	api := NewPrivateDebugAPI(b.eth)
   345  	//api := tracers.NewAPI(b.eth.APIBackend)
   346  	traces, err := api.TraceBlockByTracer(ctx, block, tracer)
   347  	if err != nil {
   348  		return nil, err
   349  	}
   350  	var results []*ethapi.TxTraceResult
   351  	if len(traces) == 0 {
   352  		return results, nil
   353  	}
   354  	for _, trace := range traces {
   355  		var result = &ethapi.TxTraceResult{
   356  			Result: trace.Result,
   357  			Error: trace.Error,
   358  		}
   359  		results = append(results, result)
   360  	}
   361  	return results, nil
   362  }