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