github.com/theQRL/go-zond@v0.2.1/zond/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 zond
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  	"math/big"
    23  	"time"
    24  
    25  	"github.com/theQRL/go-zond"
    26  	"github.com/theQRL/go-zond/accounts"
    27  	"github.com/theQRL/go-zond/common"
    28  	"github.com/theQRL/go-zond/consensus"
    29  	"github.com/theQRL/go-zond/core"
    30  	"github.com/theQRL/go-zond/core/bloombits"
    31  	"github.com/theQRL/go-zond/core/rawdb"
    32  	"github.com/theQRL/go-zond/core/state"
    33  	"github.com/theQRL/go-zond/core/txpool"
    34  	"github.com/theQRL/go-zond/core/types"
    35  	"github.com/theQRL/go-zond/core/vm"
    36  	"github.com/theQRL/go-zond/event"
    37  	"github.com/theQRL/go-zond/params"
    38  	"github.com/theQRL/go-zond/rpc"
    39  	"github.com/theQRL/go-zond/zond/gasprice"
    40  	"github.com/theQRL/go-zond/zond/tracers"
    41  	"github.com/theQRL/go-zond/zonddb"
    42  )
    43  
    44  // ZondAPIBackend implements zondapi.Backend for full nodes
    45  type ZondAPIBackend struct {
    46  	extRPCEnabled bool
    47  	zond          *Zond
    48  	gpo           *gasprice.Oracle
    49  }
    50  
    51  // ChainConfig returns the active chain configuration.
    52  func (b *ZondAPIBackend) ChainConfig() *params.ChainConfig {
    53  	return b.zond.blockchain.Config()
    54  }
    55  
    56  func (b *ZondAPIBackend) CurrentBlock() *types.Header {
    57  	return b.zond.blockchain.CurrentBlock()
    58  }
    59  
    60  func (b *ZondAPIBackend) SetHead(number uint64) {
    61  	b.zond.handler.downloader.Cancel()
    62  	b.zond.blockchain.SetHead(number)
    63  }
    64  
    65  func (b *ZondAPIBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) {
    66  	// Pending block is only known by the miner
    67  	if number == rpc.PendingBlockNumber {
    68  		block, _, _ := b.zond.miner.Pending()
    69  		if block == nil {
    70  			return nil, errors.New("pending block is not available")
    71  		}
    72  		return block.Header(), nil
    73  	}
    74  	// Otherwise resolve and return the block
    75  	if number == rpc.LatestBlockNumber {
    76  		return b.zond.blockchain.CurrentBlock(), nil
    77  	}
    78  	if number == rpc.FinalizedBlockNumber {
    79  		block := b.zond.blockchain.CurrentFinalBlock()
    80  		if block == nil {
    81  			return nil, errors.New("finalized block not found")
    82  		}
    83  		return block, nil
    84  	}
    85  	if number == rpc.SafeBlockNumber {
    86  		block := b.zond.blockchain.CurrentSafeBlock()
    87  		if block == nil {
    88  			return nil, errors.New("safe block not found")
    89  		}
    90  		return block, nil
    91  	}
    92  	return b.zond.blockchain.GetHeaderByNumber(uint64(number)), nil
    93  }
    94  
    95  func (b *ZondAPIBackend) HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error) {
    96  	if blockNr, ok := blockNrOrHash.Number(); ok {
    97  		return b.HeaderByNumber(ctx, blockNr)
    98  	}
    99  	if hash, ok := blockNrOrHash.Hash(); ok {
   100  		header := b.zond.blockchain.GetHeaderByHash(hash)
   101  		if header == nil {
   102  			return nil, errors.New("header for hash not found")
   103  		}
   104  		if blockNrOrHash.RequireCanonical && b.zond.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash {
   105  			return nil, errors.New("hash is not currently canonical")
   106  		}
   107  		return header, nil
   108  	}
   109  	return nil, errors.New("invalid arguments; neither block nor hash specified")
   110  }
   111  
   112  func (b *ZondAPIBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
   113  	return b.zond.blockchain.GetHeaderByHash(hash), nil
   114  }
   115  
   116  func (b *ZondAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) {
   117  	// Pending block is only known by the miner
   118  	if number == rpc.PendingBlockNumber {
   119  		block, _, _ := b.zond.miner.Pending()
   120  		if block == nil {
   121  			return nil, errors.New("pending block is not available")
   122  		}
   123  		return block, nil
   124  	}
   125  	// Otherwise resolve and return the block
   126  	if number == rpc.LatestBlockNumber {
   127  		header := b.zond.blockchain.CurrentBlock()
   128  		return b.zond.blockchain.GetBlock(header.Hash(), header.Number.Uint64()), nil
   129  	}
   130  	if number == rpc.FinalizedBlockNumber {
   131  		header := b.zond.blockchain.CurrentFinalBlock()
   132  		if header == nil {
   133  			return nil, errors.New("finalized block not found")
   134  		}
   135  		return b.zond.blockchain.GetBlock(header.Hash(), header.Number.Uint64()), nil
   136  	}
   137  	if number == rpc.SafeBlockNumber {
   138  		header := b.zond.blockchain.CurrentSafeBlock()
   139  		if header == nil {
   140  			return nil, errors.New("safe block not found")
   141  		}
   142  		return b.zond.blockchain.GetBlock(header.Hash(), header.Number.Uint64()), nil
   143  	}
   144  	return b.zond.blockchain.GetBlockByNumber(uint64(number)), nil
   145  }
   146  
   147  func (b *ZondAPIBackend) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) {
   148  	return b.zond.blockchain.GetBlockByHash(hash), nil
   149  }
   150  
   151  // GetBody returns body of a block. It does not resolve special block numbers.
   152  func (b *ZondAPIBackend) GetBody(ctx context.Context, hash common.Hash, number rpc.BlockNumber) (*types.Body, error) {
   153  	if number < 0 || hash == (common.Hash{}) {
   154  		return nil, errors.New("invalid arguments; expect hash and no special block numbers")
   155  	}
   156  	if body := b.zond.blockchain.GetBody(hash); body != nil {
   157  		return body, nil
   158  	}
   159  	return nil, errors.New("block body not found")
   160  }
   161  
   162  func (b *ZondAPIBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error) {
   163  	if blockNr, ok := blockNrOrHash.Number(); ok {
   164  		return b.BlockByNumber(ctx, blockNr)
   165  	}
   166  	if hash, ok := blockNrOrHash.Hash(); ok {
   167  		header := b.zond.blockchain.GetHeaderByHash(hash)
   168  		if header == nil {
   169  			return nil, errors.New("header for hash not found")
   170  		}
   171  		if blockNrOrHash.RequireCanonical && b.zond.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash {
   172  			return nil, errors.New("hash is not currently canonical")
   173  		}
   174  		block := b.zond.blockchain.GetBlock(hash, header.Number.Uint64())
   175  		if block == nil {
   176  			return nil, errors.New("header found, but block body is missing")
   177  		}
   178  		return block, nil
   179  	}
   180  	return nil, errors.New("invalid arguments; neither block nor hash specified")
   181  }
   182  
   183  func (b *ZondAPIBackend) Pending() (*types.Block, types.Receipts, *state.StateDB) {
   184  	return b.zond.miner.Pending()
   185  }
   186  
   187  func (b *ZondAPIBackend) StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error) {
   188  	// Pending state is only known by the miner
   189  	if number == rpc.PendingBlockNumber {
   190  		block, _, state := b.zond.miner.Pending()
   191  		if block == nil || state == nil {
   192  			return nil, nil, errors.New("pending state is not available")
   193  		}
   194  		return state, block.Header(), nil
   195  	}
   196  	// Otherwise resolve the block number and return its state
   197  	header, err := b.HeaderByNumber(ctx, number)
   198  	if err != nil {
   199  		return nil, nil, err
   200  	}
   201  	if header == nil {
   202  		return nil, nil, errors.New("header not found")
   203  	}
   204  	stateDb, err := b.zond.BlockChain().StateAt(header.Root)
   205  	if err != nil {
   206  		return nil, nil, err
   207  	}
   208  	return stateDb, header, nil
   209  }
   210  
   211  func (b *ZondAPIBackend) StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error) {
   212  	if blockNr, ok := blockNrOrHash.Number(); ok {
   213  		return b.StateAndHeaderByNumber(ctx, blockNr)
   214  	}
   215  	if hash, ok := blockNrOrHash.Hash(); ok {
   216  		header, err := b.HeaderByHash(ctx, hash)
   217  		if err != nil {
   218  			return nil, nil, err
   219  		}
   220  		if header == nil {
   221  			return nil, nil, errors.New("header for hash not found")
   222  		}
   223  		if blockNrOrHash.RequireCanonical && b.zond.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash {
   224  			return nil, nil, errors.New("hash is not currently canonical")
   225  		}
   226  		stateDb, err := b.zond.BlockChain().StateAt(header.Root)
   227  		if err != nil {
   228  			return nil, nil, err
   229  		}
   230  		return stateDb, header, nil
   231  	}
   232  	return nil, nil, errors.New("invalid arguments; neither block nor hash specified")
   233  }
   234  
   235  func (b *ZondAPIBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
   236  	return b.zond.blockchain.GetReceiptsByHash(hash), nil
   237  }
   238  
   239  func (b *ZondAPIBackend) GetLogs(ctx context.Context, hash common.Hash, number uint64) ([][]*types.Log, error) {
   240  	return rawdb.ReadLogs(b.zond.chainDb, hash, number), nil
   241  }
   242  
   243  func (b *ZondAPIBackend) GetZVM(ctx context.Context, msg *core.Message, state *state.StateDB, header *types.Header, vmConfig *vm.Config, blockCtx *vm.BlockContext) *vm.ZVM {
   244  	if vmConfig == nil {
   245  		vmConfig = b.zond.blockchain.GetVMConfig()
   246  	}
   247  	txContext := core.NewZVMTxContext(msg)
   248  	var context vm.BlockContext
   249  	if blockCtx != nil {
   250  		context = *blockCtx
   251  	} else {
   252  		context = core.NewZVMBlockContext(header, b.zond.BlockChain(), nil)
   253  	}
   254  	return vm.NewZVM(context, txContext, state, b.ChainConfig(), *vmConfig)
   255  }
   256  
   257  func (b *ZondAPIBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
   258  	return b.zond.BlockChain().SubscribeRemovedLogsEvent(ch)
   259  }
   260  
   261  func (b *ZondAPIBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
   262  	return b.zond.BlockChain().SubscribeChainEvent(ch)
   263  }
   264  
   265  func (b *ZondAPIBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
   266  	return b.zond.BlockChain().SubscribeChainHeadEvent(ch)
   267  }
   268  
   269  func (b *ZondAPIBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription {
   270  	return b.zond.BlockChain().SubscribeChainSideEvent(ch)
   271  }
   272  
   273  func (b *ZondAPIBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
   274  	return b.zond.BlockChain().SubscribeLogsEvent(ch)
   275  }
   276  
   277  func (b *ZondAPIBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
   278  	return b.zond.txPool.Add([]*types.Transaction{signedTx}, true, false)[0]
   279  }
   280  
   281  func (b *ZondAPIBackend) GetPoolTransactions() (types.Transactions, error) {
   282  	pending := b.zond.txPool.Pending(txpool.PendingFilter{})
   283  	var txs types.Transactions
   284  	for _, batch := range pending {
   285  		for _, lazy := range batch {
   286  			if tx := lazy.Resolve(); tx != nil {
   287  				txs = append(txs, tx)
   288  			}
   289  		}
   290  	}
   291  	return txs, nil
   292  }
   293  
   294  func (b *ZondAPIBackend) GetPoolTransaction(hash common.Hash) *types.Transaction {
   295  	return b.zond.txPool.Get(hash)
   296  }
   297  
   298  func (b *ZondAPIBackend) GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) {
   299  	tx, blockHash, blockNumber, index := rawdb.ReadTransaction(b.zond.ChainDb(), txHash)
   300  	return tx, blockHash, blockNumber, index, nil
   301  }
   302  
   303  func (b *ZondAPIBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
   304  	return b.zond.txPool.Nonce(addr), nil
   305  }
   306  
   307  func (b *ZondAPIBackend) Stats() (runnable int, blocked int) {
   308  	return b.zond.txPool.Stats()
   309  }
   310  
   311  func (b *ZondAPIBackend) TxPoolContent() (map[common.Address][]*types.Transaction, map[common.Address][]*types.Transaction) {
   312  	return b.zond.txPool.Content()
   313  }
   314  
   315  func (b *ZondAPIBackend) TxPoolContentFrom(addr common.Address) ([]*types.Transaction, []*types.Transaction) {
   316  	return b.zond.txPool.ContentFrom(addr)
   317  }
   318  
   319  func (b *ZondAPIBackend) TxPool() *txpool.TxPool {
   320  	return b.zond.txPool
   321  }
   322  
   323  func (b *ZondAPIBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription {
   324  	return b.zond.txPool.SubscribeTransactions(ch)
   325  }
   326  
   327  func (b *ZondAPIBackend) SyncProgress() zond.SyncProgress {
   328  	return b.zond.Downloader().Progress()
   329  }
   330  
   331  func (b *ZondAPIBackend) SuggestGasTipCap(ctx context.Context) (*big.Int, error) {
   332  	return b.gpo.SuggestTipCap(ctx)
   333  }
   334  
   335  func (b *ZondAPIBackend) FeeHistory(ctx context.Context, blockCount uint64, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (firstBlock *big.Int, reward [][]*big.Int, baseFee []*big.Int, gasUsedRatio []float64, err error) {
   336  	return b.gpo.FeeHistory(ctx, blockCount, lastBlock, rewardPercentiles)
   337  }
   338  
   339  func (b *ZondAPIBackend) ChainDb() zonddb.Database {
   340  	return b.zond.ChainDb()
   341  }
   342  
   343  func (b *ZondAPIBackend) EventMux() *event.TypeMux {
   344  	return b.zond.EventMux()
   345  }
   346  
   347  func (b *ZondAPIBackend) AccountManager() *accounts.Manager {
   348  	return b.zond.AccountManager()
   349  }
   350  
   351  func (b *ZondAPIBackend) ExtRPCEnabled() bool {
   352  	return b.extRPCEnabled
   353  }
   354  
   355  func (b *ZondAPIBackend) RPCGasCap() uint64 {
   356  	return b.zond.config.RPCGasCap
   357  }
   358  
   359  func (b *ZondAPIBackend) RPCZVMTimeout() time.Duration {
   360  	return b.zond.config.RPCZVMTimeout
   361  }
   362  
   363  func (b *ZondAPIBackend) RPCTxFeeCap() float64 {
   364  	return b.zond.config.RPCTxFeeCap
   365  }
   366  
   367  func (b *ZondAPIBackend) BloomStatus() (uint64, uint64) {
   368  	sections, _, _ := b.zond.bloomIndexer.Sections()
   369  	return params.BloomBitsBlocks, sections
   370  }
   371  
   372  func (b *ZondAPIBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) {
   373  	for i := 0; i < bloomFilterThreads; i++ {
   374  		go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.zond.bloomRequests)
   375  	}
   376  }
   377  
   378  func (b *ZondAPIBackend) Engine() consensus.Engine {
   379  	return b.zond.engine
   380  }
   381  
   382  func (b *ZondAPIBackend) CurrentHeader() *types.Header {
   383  	return b.zond.blockchain.CurrentHeader()
   384  }
   385  
   386  func (b *ZondAPIBackend) StateAtBlock(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, readOnly bool, preferDisk bool) (*state.StateDB, tracers.StateReleaseFunc, error) {
   387  	return b.zond.stateAtBlock(ctx, block, reexec, base, readOnly, preferDisk)
   388  }
   389  
   390  func (b *ZondAPIBackend) StateAtTransaction(ctx context.Context, block *types.Block, txIndex int, reexec uint64) (*core.Message, vm.BlockContext, *state.StateDB, tracers.StateReleaseFunc, error) {
   391  	return b.zond.stateAtTransaction(ctx, block, txIndex, reexec)
   392  }