github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/eth/api_backend.go (about)

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