github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/neatptc/api_backend.go (about)

     1  package neatptc
     2  
     3  import (
     4  	"context"
     5  	"math/big"
     6  
     7  	"github.com/neatlab/neatio/chain/accounts"
     8  	"github.com/neatlab/neatio/chain/consensus"
     9  	"github.com/neatlab/neatio/chain/core"
    10  	"github.com/neatlab/neatio/chain/core/bloombits"
    11  	"github.com/neatlab/neatio/chain/core/state"
    12  	"github.com/neatlab/neatio/chain/core/types"
    13  	"github.com/neatlab/neatio/chain/core/vm"
    14  	"github.com/neatlab/neatio/neatdb"
    15  	"github.com/neatlab/neatio/neatptc/downloader"
    16  	"github.com/neatlab/neatio/neatptc/gasprice"
    17  	"github.com/neatlab/neatio/network/rpc"
    18  	"github.com/neatlab/neatio/params"
    19  	"github.com/neatlab/neatio/utilities/common"
    20  	"github.com/neatlab/neatio/utilities/common/math"
    21  	"github.com/neatlab/neatio/utilities/event"
    22  )
    23  
    24  type EthApiBackend struct {
    25  	eth *NeatIO
    26  	gpo *gasprice.Oracle
    27  
    28  	crossChainHelper core.CrossChainHelper
    29  }
    30  
    31  func (b *EthApiBackend) ChainConfig() *params.ChainConfig {
    32  	return b.eth.chainConfig
    33  }
    34  
    35  func (b *EthApiBackend) CurrentBlock() *types.Block {
    36  	return b.eth.blockchain.CurrentBlock()
    37  }
    38  
    39  func (b *EthApiBackend) SetHead(number uint64) {
    40  	b.eth.protocolManager.downloader.Cancel()
    41  	b.eth.blockchain.SetHead(number)
    42  }
    43  
    44  func (b *EthApiBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error) {
    45  
    46  	if blockNr == rpc.PendingBlockNumber {
    47  		block := b.eth.miner.PendingBlock()
    48  		return block.Header(), nil
    49  	}
    50  
    51  	if blockNr == rpc.LatestBlockNumber {
    52  		return b.eth.blockchain.CurrentBlock().Header(), nil
    53  	}
    54  	return b.eth.blockchain.GetHeaderByNumber(uint64(blockNr)), nil
    55  }
    56  
    57  func (b *EthApiBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error) {
    58  
    59  	if blockNr == rpc.PendingBlockNumber {
    60  		block := b.eth.miner.PendingBlock()
    61  		return block, nil
    62  	}
    63  
    64  	if blockNr == rpc.LatestBlockNumber {
    65  		return b.eth.blockchain.CurrentBlock(), nil
    66  	}
    67  	return b.eth.blockchain.GetBlockByNumber(uint64(blockNr)), nil
    68  }
    69  
    70  func (b *EthApiBackend) StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*state.StateDB, *types.Header, error) {
    71  
    72  	if blockNr == rpc.PendingBlockNumber {
    73  		block, state := b.eth.miner.Pending()
    74  		return state, block.Header(), nil
    75  	}
    76  
    77  	header, err := b.HeaderByNumber(ctx, blockNr)
    78  	if header == nil || err != nil {
    79  		return nil, nil, err
    80  	}
    81  	stateDb, err := b.eth.BlockChain().StateAt(header.Root)
    82  	return stateDb, header, err
    83  }
    84  
    85  func (b *EthApiBackend) GetBlock(ctx context.Context, hash common.Hash) (*types.Block, error) {
    86  	return b.eth.blockchain.GetBlockByHash(hash), nil
    87  }
    88  
    89  func (b *EthApiBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
    90  	return b.eth.blockchain.GetReceiptsByHash(hash), nil
    91  }
    92  
    93  func (b *EthApiBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) {
    94  	receipts := b.eth.blockchain.GetReceiptsByHash(hash)
    95  	if receipts == nil {
    96  		return nil, nil
    97  	}
    98  	logs := make([][]*types.Log, len(receipts))
    99  	for i, receipt := range receipts {
   100  		logs[i] = receipt.Logs
   101  	}
   102  	return logs, nil
   103  }
   104  
   105  func (b *EthApiBackend) GetTd(blockHash common.Hash) *big.Int {
   106  	return b.eth.blockchain.GetTdByHash(blockHash)
   107  }
   108  
   109  func (b *EthApiBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmCfg vm.Config) (*vm.EVM, func() error, error) {
   110  	state.SetBalance(msg.From(), math.MaxBig256)
   111  	vmError := func() error { return nil }
   112  
   113  	context := core.NewEVMContext(msg, header, b.eth.BlockChain(), nil)
   114  	return vm.NewEVM(context, state, b.eth.chainConfig, vmCfg), vmError, nil
   115  }
   116  
   117  func (b *EthApiBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
   118  	return b.eth.BlockChain().SubscribeRemovedLogsEvent(ch)
   119  }
   120  
   121  func (b *EthApiBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
   122  	return b.eth.BlockChain().SubscribeChainEvent(ch)
   123  }
   124  
   125  func (b *EthApiBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
   126  	return b.eth.BlockChain().SubscribeChainHeadEvent(ch)
   127  }
   128  
   129  func (b *EthApiBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription {
   130  	return b.eth.BlockChain().SubscribeChainSideEvent(ch)
   131  }
   132  
   133  func (b *EthApiBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
   134  	return b.eth.BlockChain().SubscribeLogsEvent(ch)
   135  }
   136  
   137  func (b *EthApiBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
   138  	return b.eth.txPool.AddLocal(signedTx)
   139  }
   140  
   141  func (b *EthApiBackend) GetPoolTransactions() (types.Transactions, error) {
   142  	pending, err := b.eth.txPool.Pending()
   143  	if err != nil {
   144  		return nil, err
   145  	}
   146  	var txs types.Transactions
   147  	for _, batch := range pending {
   148  		txs = append(txs, batch...)
   149  	}
   150  	return txs, nil
   151  }
   152  
   153  func (b *EthApiBackend) GetPoolTransaction(hash common.Hash) *types.Transaction {
   154  	return b.eth.txPool.Get(hash)
   155  }
   156  
   157  func (b *EthApiBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
   158  	return b.eth.txPool.State().GetNonce(addr), nil
   159  }
   160  
   161  func (b *EthApiBackend) Stats() (pending int, queued int) {
   162  	return b.eth.txPool.Stats()
   163  }
   164  
   165  func (b *EthApiBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
   166  	return b.eth.TxPool().Content()
   167  }
   168  
   169  func (b *EthApiBackend) SubscribeTxPreEvent(ch chan<- core.TxPreEvent) event.Subscription {
   170  	return b.eth.TxPool().SubscribeTxPreEvent(ch)
   171  }
   172  
   173  func (b *EthApiBackend) Downloader() *downloader.Downloader {
   174  	return b.eth.Downloader()
   175  }
   176  
   177  func (b *EthApiBackend) ProtocolVersion() int {
   178  	return b.eth.EthVersion()
   179  }
   180  
   181  func (b *EthApiBackend) SuggestPrice(ctx context.Context) (*big.Int, error) {
   182  	return b.gpo.SuggestPrice(ctx)
   183  }
   184  
   185  func (b *EthApiBackend) ChainDb() neatdb.Database {
   186  	return b.eth.ChainDb()
   187  }
   188  
   189  func (b *EthApiBackend) EventMux() *event.TypeMux {
   190  	return b.eth.EventMux()
   191  }
   192  
   193  func (b *EthApiBackend) AccountManager() *accounts.Manager {
   194  	return b.eth.AccountManager()
   195  }
   196  
   197  func (b *EthApiBackend) BloomStatus() (uint64, uint64) {
   198  	sections, _, _ := b.eth.bloomIndexer.Sections()
   199  	return params.BloomBitsBlocks, sections
   200  }
   201  
   202  func (b *EthApiBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) {
   203  	for i := 0; i < bloomFilterThreads; i++ {
   204  		go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests)
   205  	}
   206  }
   207  
   208  func (b *EthApiBackend) GetCrossChainHelper() core.CrossChainHelper {
   209  	return b.crossChainHelper
   210  }
   211  
   212  func (b *EthApiBackend) BroadcastTX3ProofData(proofData *types.TX3ProofData) {
   213  	b.eth.protocolManager.BroadcastTX3ProofData(proofData.Header.Hash(), proofData)
   214  }
   215  
   216  func (b *EthApiBackend) Engine() consensus.Engine {
   217  	return b.eth.Engine()
   218  }