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