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