github.com/aquanetwork/aquachain@v1.7.8/aqua/api_backend.go (about)

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