github.com/ebakus/go-ebakus@v1.0.5-0.20200520105415-dbccef9ec421/eth/api_backend.go (about)

     1  // Copyright 2019 The ebakus/go-ebakus Authors
     2  // This file is part of the ebakus/go-ebakus library.
     3  //
     4  // The ebakus/go-ebakus 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 ebakus/go-ebakus 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 ebakus/go-ebakus library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package eth
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  	"math/big"
    23  
    24  	"github.com/ebakus/ebakusdb"
    25  
    26  	"github.com/ebakus/go-ebakus/accounts"
    27  	"github.com/ebakus/go-ebakus/common"
    28  	"github.com/ebakus/go-ebakus/common/math"
    29  	"github.com/ebakus/go-ebakus/core"
    30  	"github.com/ebakus/go-ebakus/core/bloombits"
    31  	"github.com/ebakus/go-ebakus/core/rawdb"
    32  	"github.com/ebakus/go-ebakus/core/state"
    33  	"github.com/ebakus/go-ebakus/core/types"
    34  	"github.com/ebakus/go-ebakus/core/vm"
    35  	"github.com/ebakus/go-ebakus/eth/downloader"
    36  	"github.com/ebakus/go-ebakus/eth/gasprice"
    37  	"github.com/ebakus/go-ebakus/ethdb"
    38  	"github.com/ebakus/go-ebakus/event"
    39  	"github.com/ebakus/go-ebakus/params"
    40  	"github.com/ebakus/go-ebakus/rpc"
    41  )
    42  
    43  // EthAPIBackend implements ethapi.Backend for full nodes
    44  type EthAPIBackend struct {
    45  	extRPCEnabled bool
    46  	eth           *Ebakus
    47  	gpo           *gasprice.Oracle
    48  }
    49  
    50  // ChainConfig returns the active chain configuration.
    51  func (b *EthAPIBackend) ChainConfig() *params.ChainConfig {
    52  	return b.eth.blockchain.Config()
    53  }
    54  
    55  func (b *EthAPIBackend) CurrentBlock() *types.Block {
    56  	return b.eth.blockchain.CurrentBlock()
    57  }
    58  
    59  func (b *EthAPIBackend) SetHead(number uint64) {
    60  	b.eth.protocolManager.downloader.Cancel()
    61  	b.eth.blockchain.SetHead(number)
    62  }
    63  
    64  func (b *EthAPIBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) {
    65  	// Pending block is only known by the miner
    66  	if number == rpc.PendingBlockNumber {
    67  		block := b.eth.miner.PendingBlock()
    68  		return block.Header(), nil
    69  	}
    70  	// Otherwise resolve and return the block
    71  	if number == rpc.LatestBlockNumber {
    72  		return b.eth.blockchain.CurrentBlock().Header(), nil
    73  	}
    74  	return b.eth.blockchain.GetHeaderByNumber(uint64(number)), nil
    75  }
    76  
    77  func (b *EthAPIBackend) HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error) {
    78  	if blockNr, ok := blockNrOrHash.Number(); ok {
    79  		return b.HeaderByNumber(ctx, blockNr)
    80  	}
    81  	if hash, ok := blockNrOrHash.Hash(); ok {
    82  		header := b.eth.blockchain.GetHeaderByHash(hash)
    83  		if header == nil {
    84  			return nil, errors.New("header for hash not found")
    85  		}
    86  		if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash {
    87  			return nil, errors.New("hash is not currently canonical")
    88  		}
    89  		return header, nil
    90  	}
    91  	return nil, errors.New("invalid arguments; neither block nor hash specified")
    92  }
    93  
    94  func (b *EthAPIBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
    95  	return b.eth.blockchain.GetHeaderByHash(hash), nil
    96  }
    97  
    98  func (b *EthAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) {
    99  	// Pending block is only known by the miner
   100  	if number == rpc.PendingBlockNumber {
   101  		block := b.eth.miner.PendingBlock()
   102  		return block, nil
   103  	}
   104  	// Otherwise resolve and return the block
   105  	if number == rpc.LatestBlockNumber {
   106  		return b.eth.blockchain.CurrentBlock(), nil
   107  	}
   108  	return b.eth.blockchain.GetBlockByNumber(uint64(number)), nil
   109  }
   110  
   111  func (b *EthAPIBackend) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) {
   112  	return b.eth.blockchain.GetBlockByHash(hash), nil
   113  }
   114  
   115  func (b *EthAPIBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error) {
   116  	if blockNr, ok := blockNrOrHash.Number(); ok {
   117  		return b.BlockByNumber(ctx, blockNr)
   118  	}
   119  	if hash, ok := blockNrOrHash.Hash(); ok {
   120  		header := b.eth.blockchain.GetHeaderByHash(hash)
   121  		if header == nil {
   122  			return nil, errors.New("header for hash not found")
   123  		}
   124  		if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash {
   125  			return nil, errors.New("hash is not currently canonical")
   126  		}
   127  		block := b.eth.blockchain.GetBlock(hash, header.Number.Uint64())
   128  		if block == nil {
   129  			return nil, errors.New("header found, but block body is missing")
   130  		}
   131  		return block, nil
   132  	}
   133  	return nil, errors.New("invalid arguments; neither block nor hash specified")
   134  }
   135  
   136  func (b *EthAPIBackend) StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error) {
   137  	// Pending state is only known by the miner
   138  	if number == rpc.PendingBlockNumber {
   139  		block, state := b.eth.miner.Pending()
   140  		return state, block.Header(), nil
   141  	}
   142  	// Otherwise resolve the block number and return its state
   143  	header, err := b.HeaderByNumber(ctx, number)
   144  	if err != nil {
   145  		return nil, nil, err
   146  	}
   147  	if header == nil {
   148  		return nil, nil, errors.New("header not found")
   149  	}
   150  	stateDb, err := b.eth.BlockChain().StateAt(header.Root)
   151  
   152  	return stateDb, header, err
   153  }
   154  
   155  func (b *EthAPIBackend) StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error) {
   156  	if blockNr, ok := blockNrOrHash.Number(); ok {
   157  		return b.StateAndHeaderByNumber(ctx, blockNr)
   158  	}
   159  	if hash, ok := blockNrOrHash.Hash(); ok {
   160  		header, err := b.HeaderByHash(ctx, hash)
   161  		if err != nil {
   162  			return nil, nil, err
   163  		}
   164  		if header == nil {
   165  			return nil, nil, errors.New("header for hash not found")
   166  		}
   167  		if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash {
   168  			return nil, nil, errors.New("hash is not currently canonical")
   169  		}
   170  		stateDb, err := b.eth.BlockChain().StateAt(header.Root)
   171  		return stateDb, header, err
   172  	}
   173  	return nil, nil, errors.New("invalid arguments; neither block nor hash specified")
   174  }
   175  
   176  func (b *EthAPIBackend) EbakusStateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*ebakusdb.Snapshot, *types.Header, error) {
   177  	// Pending state is only known by the miner
   178  	if blockNr == rpc.PendingBlockNumber {
   179  		block, snapshot := b.eth.miner.PendingEbakusState()
   180  		return snapshot, block.Header(), nil
   181  	}
   182  	// Otherwise resolve the block number and return its state
   183  	header, err := b.HeaderByNumber(ctx, blockNr)
   184  	if header == nil || err != nil {
   185  		return nil, nil, err
   186  	}
   187  
   188  	ebakusState, err := b.eth.BlockChain().EbakusStateAt(header.Hash(), uint64(blockNr))
   189  	return ebakusState, header, err
   190  }
   191  
   192  func (b *EthAPIBackend) EbakusStateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*ebakusdb.Snapshot, *types.Header, error) {
   193  	if blockNr, ok := blockNrOrHash.Number(); ok {
   194  		return b.EbakusStateAndHeaderByNumber(ctx, blockNr)
   195  	}
   196  	if hash, ok := blockNrOrHash.Hash(); ok {
   197  		header, err := b.HeaderByHash(ctx, hash)
   198  		if err != nil {
   199  			return nil, nil, err
   200  		}
   201  		if header == nil {
   202  			return nil, nil, errors.New("header for hash not found")
   203  		}
   204  		if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash {
   205  			return nil, nil, errors.New("hash is not currently canonical")
   206  		}
   207  		ebakusState, err := b.eth.BlockChain().EbakusStateAt(header.Hash(), header.Number.Uint64())
   208  		return ebakusState, header, err
   209  	}
   210  	return nil, nil, errors.New("invalid arguments; neither block nor hash specified")
   211  }
   212  
   213  func (b *EthAPIBackend) GetBlockAuthor(header *types.Header) (common.Address, error) {
   214  	return b.eth.engine.Author(header)
   215  }
   216  
   217  func (b *EthAPIBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
   218  	return b.eth.blockchain.GetReceiptsByHash(hash), nil
   219  }
   220  
   221  func (b *EthAPIBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) {
   222  	receipts := b.eth.blockchain.GetReceiptsByHash(hash)
   223  	if receipts == nil {
   224  		return nil, nil
   225  	}
   226  	logs := make([][]*types.Log, len(receipts))
   227  	for i, receipt := range receipts {
   228  		logs[i] = receipt.Logs
   229  	}
   230  	return logs, nil
   231  }
   232  
   233  func (b *EthAPIBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, ebakusState *ebakusdb.Snapshot, header *types.Header) (*vm.EVM, func() error, error) {
   234  	state.SetBalance(msg.From(), math.MaxBig256)
   235  	vmError := func() error { return nil }
   236  
   237  	context := core.NewEVMContext(msg, header, b.eth.BlockChain(), nil)
   238  	return vm.NewEVM(context, state, ebakusState, b.eth.blockchain.Config(), *b.eth.blockchain.GetVMConfig()), vmError, nil
   239  }
   240  
   241  func (b *EthAPIBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
   242  	return b.eth.BlockChain().SubscribeRemovedLogsEvent(ch)
   243  }
   244  
   245  func (b *EthAPIBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
   246  	return b.eth.BlockChain().SubscribeChainEvent(ch)
   247  }
   248  
   249  func (b *EthAPIBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
   250  	return b.eth.BlockChain().SubscribeChainHeadEvent(ch)
   251  }
   252  
   253  func (b *EthAPIBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription {
   254  	return b.eth.BlockChain().SubscribeChainSideEvent(ch)
   255  }
   256  
   257  func (b *EthAPIBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
   258  	return b.eth.BlockChain().SubscribeLogsEvent(ch)
   259  }
   260  
   261  func (b *EthAPIBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
   262  	return b.eth.txPool.AddLocal(signedTx)
   263  }
   264  
   265  func (b *EthAPIBackend) GetPoolTransactions() (types.Transactions, error) {
   266  	pending, err := b.eth.txPool.Pending()
   267  	if err != nil {
   268  		return nil, err
   269  	}
   270  	var txs types.Transactions
   271  	for _, batch := range pending {
   272  		txs = append(txs, batch...)
   273  	}
   274  	return txs, nil
   275  }
   276  
   277  func (b *EthAPIBackend) GetPoolTransaction(hash common.Hash) *types.Transaction {
   278  	return b.eth.txPool.Get(hash)
   279  }
   280  
   281  func (b *EthAPIBackend) GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) {
   282  	tx, blockHash, blockNumber, index := rawdb.ReadTransaction(b.eth.ChainDb(), txHash)
   283  	return tx, blockHash, blockNumber, index, nil
   284  }
   285  
   286  func (b *EthAPIBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
   287  	return b.eth.txPool.Nonce(addr), nil
   288  }
   289  
   290  func (b *EthAPIBackend) Stats() (pending int, queued int) {
   291  	return b.eth.txPool.Stats()
   292  }
   293  
   294  func (b *EthAPIBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
   295  	return b.eth.TxPool().Content()
   296  }
   297  
   298  func (b *EthAPIBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription {
   299  	return b.eth.TxPool().SubscribeNewTxsEvent(ch)
   300  }
   301  
   302  func (b *EthAPIBackend) Downloader() *downloader.Downloader {
   303  	return b.eth.Downloader()
   304  }
   305  
   306  func (b *EthAPIBackend) ProtocolVersion() int {
   307  	return b.eth.EthVersion()
   308  }
   309  
   310  func (b *EthAPIBackend) SuggestPrice(ctx context.Context) (*float64, error) {
   311  	return b.gpo.SuggestPrice(ctx)
   312  }
   313  
   314  func (b *EthAPIBackend) ChainDb() ethdb.Database {
   315  	return b.eth.ChainDb()
   316  }
   317  
   318  func (b *EthAPIBackend) EbakusDb() *ebakusdb.DB {
   319  	return b.eth.EbakusDb()
   320  }
   321  
   322  func (b *EthAPIBackend) EventMux() *event.TypeMux {
   323  	return b.eth.EventMux()
   324  }
   325  
   326  func (b *EthAPIBackend) AccountManager() *accounts.Manager {
   327  	return b.eth.AccountManager()
   328  }
   329  
   330  func (b *EthAPIBackend) ExtRPCEnabled() bool {
   331  	return b.extRPCEnabled
   332  }
   333  
   334  func (b *EthAPIBackend) RPCGasCap() *big.Int {
   335  	return b.eth.config.RPCGasCap
   336  }
   337  
   338  func (b *EthAPIBackend) MinGasPrice() float64 {
   339  	return b.eth.config.Miner.GasPrice
   340  }
   341  
   342  func (b *EthAPIBackend) EbakusdbMaxActiveIterators() uint64 {
   343  	return b.eth.config.EbakusdbMaxActiveIterators
   344  }
   345  
   346  func (b *EthAPIBackend) BloomStatus() (uint64, uint64) {
   347  	sections, _, _ := b.eth.bloomIndexer.Sections()
   348  	return params.BloomBitsBlocks, sections
   349  }
   350  
   351  func (b *EthAPIBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) {
   352  	for i := 0; i < bloomFilterThreads; i++ {
   353  		go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests)
   354  	}
   355  }