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