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