github.com/pfcoder/quorum@v2.0.3-0.20180501191142-d4a1b0958135+incompatible/eth/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 eth
    18  
    19  import (
    20  	"context"
    21  	"math/big"
    22  
    23  	"github.com/ethereum/go-ethereum/accounts"
    24  	"github.com/ethereum/go-ethereum/common"
    25  	"github.com/ethereum/go-ethereum/common/math"
    26  	"github.com/ethereum/go-ethereum/core"
    27  	"github.com/ethereum/go-ethereum/core/bloombits"
    28  	"github.com/ethereum/go-ethereum/core/state"
    29  	"github.com/ethereum/go-ethereum/core/types"
    30  	"github.com/ethereum/go-ethereum/core/vm"
    31  	"github.com/ethereum/go-ethereum/eth/downloader"
    32  	"github.com/ethereum/go-ethereum/eth/gasprice"
    33  	"github.com/ethereum/go-ethereum/ethdb"
    34  	"github.com/ethereum/go-ethereum/event"
    35  	"github.com/ethereum/go-ethereum/params"
    36  	"github.com/ethereum/go-ethereum/rpc"
    37  )
    38  
    39  // EthApiBackend implements ethapi.Backend for full nodes
    40  type EthApiBackend struct {
    41  	eth *Ethereum
    42  	gpo *gasprice.Oracle
    43  }
    44  
    45  func (b *EthApiBackend) ChainConfig() *params.ChainConfig {
    46  	return b.eth.chainConfig
    47  }
    48  
    49  func (b *EthApiBackend) CurrentBlock() *types.Block {
    50  	return b.eth.blockchain.CurrentBlock()
    51  }
    52  
    53  func (b *EthApiBackend) SetHead(number uint64) {
    54  	b.eth.protocolManager.downloader.Cancel()
    55  	b.eth.blockchain.SetHead(number)
    56  }
    57  
    58  func (b *EthApiBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error) {
    59  	// Pending block is only known by the miner
    60  	if blockNr == rpc.PendingBlockNumber {
    61  		block := b.eth.miner.PendingBlock()
    62  		return block.Header(), nil
    63  	}
    64  	// Otherwise resolve and return the block
    65  	if blockNr == rpc.LatestBlockNumber {
    66  		return b.eth.blockchain.CurrentBlock().Header(), nil
    67  	}
    68  	return b.eth.blockchain.GetHeaderByNumber(uint64(blockNr)), nil
    69  }
    70  
    71  func (b *EthApiBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error) {
    72  	// Pending block is only known by the miner
    73  	if blockNr == rpc.PendingBlockNumber {
    74  		if b.eth.protocolManager.raftMode {
    75  			// Use latest instead.
    76  			return b.eth.blockchain.CurrentBlock(), nil
    77  		}
    78  		block := b.eth.miner.PendingBlock()
    79  		return block, nil
    80  	}
    81  	// Otherwise resolve and return the block
    82  	if blockNr == rpc.LatestBlockNumber {
    83  		return b.eth.blockchain.CurrentBlock(), nil
    84  	}
    85  	return b.eth.blockchain.GetBlockByNumber(uint64(blockNr)), nil
    86  }
    87  
    88  func (b *EthApiBackend) StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (vm.MinimalApiState, *types.Header, error) {
    89  	// Pending state is only known by the miner
    90  	if blockNr == rpc.PendingBlockNumber {
    91  		if b.eth.protocolManager.raftMode {
    92  			// Use latest instead.
    93  			header, err := b.HeaderByNumber(ctx, rpc.LatestBlockNumber)
    94  			if header == nil || err != nil {
    95  				return nil, nil, err
    96  			}
    97  			publicState, privateState, err := b.eth.BlockChain().StateAt(header.Root)
    98  			return EthApiState{publicState, privateState}, header, err
    99  		}
   100  		block, publicState, privateState := b.eth.miner.Pending()
   101  		return EthApiState{publicState, privateState}, block.Header(), nil
   102  	}
   103  	// Otherwise resolve the block number and return its state
   104  	header, err := b.HeaderByNumber(ctx, blockNr)
   105  	if header == nil || err != nil {
   106  		return nil, nil, err
   107  	}
   108  	stateDb, privateState, err := b.eth.BlockChain().StateAt(header.Root)
   109  	return EthApiState{stateDb, privateState}, header, err
   110  }
   111  
   112  func (b *EthApiBackend) GetBlock(ctx context.Context, blockHash common.Hash) (*types.Block, error) {
   113  	return b.eth.blockchain.GetBlockByHash(blockHash), nil
   114  }
   115  
   116  func (b *EthApiBackend) GetReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error) {
   117  	return core.GetBlockReceipts(b.eth.chainDb, blockHash, core.GetBlockNumber(b.eth.chainDb, blockHash)), nil
   118  }
   119  
   120  func (b *EthApiBackend) GetTd(blockHash common.Hash) *big.Int {
   121  	return b.eth.blockchain.GetTdByHash(blockHash)
   122  }
   123  
   124  func (b *EthApiBackend) GetEVM(ctx context.Context, msg core.Message, state vm.MinimalApiState, header *types.Header, vmCfg vm.Config) (*vm.EVM, func() error, error) {
   125  	statedb := state.(EthApiState)
   126  	from := statedb.state.GetOrNewStateObject(msg.From())
   127  	from.SetBalance(math.MaxBig256)
   128  	vmError := func() error { return nil }
   129  
   130  	context := core.NewEVMContext(msg, header, b.eth.BlockChain(), nil)
   131  	return vm.NewEVM(context, statedb.state, statedb.privateState, b.eth.chainConfig, vmCfg), vmError, nil
   132  }
   133  
   134  func (b *EthApiBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
   135  	return b.eth.BlockChain().SubscribeRemovedLogsEvent(ch)
   136  }
   137  
   138  func (b *EthApiBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
   139  	return b.eth.BlockChain().SubscribeChainEvent(ch)
   140  }
   141  
   142  func (b *EthApiBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
   143  	return b.eth.BlockChain().SubscribeChainHeadEvent(ch)
   144  }
   145  
   146  func (b *EthApiBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription {
   147  	return b.eth.BlockChain().SubscribeChainSideEvent(ch)
   148  }
   149  
   150  func (b *EthApiBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
   151  	return b.eth.BlockChain().SubscribeLogsEvent(ch)
   152  }
   153  
   154  func (b *EthApiBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
   155  	return b.eth.txPool.AddLocal(signedTx)
   156  }
   157  
   158  func (b *EthApiBackend) GetPoolTransactions() (types.Transactions, error) {
   159  	pending, err := b.eth.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 *EthApiBackend) GetPoolTransaction(hash common.Hash) *types.Transaction {
   171  	return b.eth.txPool.Get(hash)
   172  }
   173  
   174  func (b *EthApiBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
   175  	return b.eth.txPool.State().GetNonce(addr), nil
   176  }
   177  
   178  func (b *EthApiBackend) Stats() (pending int, queued int) {
   179  	return b.eth.txPool.Stats()
   180  }
   181  
   182  func (b *EthApiBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
   183  	return b.eth.TxPool().Content()
   184  }
   185  
   186  func (b *EthApiBackend) SubscribeTxPreEvent(ch chan<- core.TxPreEvent) event.Subscription {
   187  	return b.eth.TxPool().SubscribeTxPreEvent(ch)
   188  }
   189  
   190  func (b *EthApiBackend) Downloader() *downloader.Downloader {
   191  	return b.eth.Downloader()
   192  }
   193  
   194  func (b *EthApiBackend) ProtocolVersion() int {
   195  	return b.eth.EthVersion()
   196  }
   197  
   198  func (b *EthApiBackend) SuggestPrice(ctx context.Context) (*big.Int, error) {
   199  	if b.ChainConfig().IsQuorum {
   200  		return big.NewInt(0), nil
   201  	} else {
   202  		return b.gpo.SuggestPrice(ctx)
   203  	}
   204  }
   205  
   206  func (b *EthApiBackend) ChainDb() ethdb.Database {
   207  	return b.eth.ChainDb()
   208  }
   209  
   210  func (b *EthApiBackend) EventMux() *event.TypeMux {
   211  	return b.eth.EventMux()
   212  }
   213  
   214  func (b *EthApiBackend) AccountManager() *accounts.Manager {
   215  	return b.eth.AccountManager()
   216  }
   217  
   218  func (b *EthApiBackend) BloomStatus() (uint64, uint64) {
   219  	sections, _, _ := b.eth.bloomIndexer.Sections()
   220  	return params.BloomBitsBlocks, sections
   221  }
   222  
   223  func (b *EthApiBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) {
   224  	for i := 0; i < bloomFilterThreads; i++ {
   225  		go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests)
   226  	}
   227  }
   228  
   229  type EthApiState struct {
   230  	state, privateState *state.StateDB
   231  }
   232  
   233  func (s EthApiState) GetBalance(addr common.Address) *big.Int {
   234  	if s.privateState.Exist(addr) {
   235  		return s.privateState.GetBalance(addr)
   236  	}
   237  	return s.state.GetBalance(addr)
   238  }
   239  
   240  func (s EthApiState) GetCode(addr common.Address) []byte {
   241  	if s.privateState.Exist(addr) {
   242  		return s.privateState.GetCode(addr)
   243  	}
   244  	return s.state.GetCode(addr)
   245  }
   246  
   247  func (s EthApiState) GetState(a common.Address, b common.Hash) common.Hash {
   248  	if s.privateState.Exist(a) {
   249  		return s.privateState.GetState(a, b)
   250  	}
   251  	return s.state.GetState(a, b)
   252  }
   253  
   254  func (s EthApiState) GetNonce(addr common.Address) uint64 {
   255  	if s.privateState.Exist(addr) {
   256  		return s.privateState.GetNonce(addr)
   257  	}
   258  	return s.state.GetNonce(addr)
   259  }
   260  
   261  //func (s MinimalApiState) Error