github.com/coltonfike/e2c@v21.1.0+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  	"errors"
    22  	"fmt"
    23  	"math/big"
    24  	"time"
    25  
    26  	"github.com/ethereum/go-ethereum/accounts"
    27  	"github.com/ethereum/go-ethereum/common"
    28  	"github.com/ethereum/go-ethereum/common/math"
    29  	"github.com/ethereum/go-ethereum/core"
    30  	"github.com/ethereum/go-ethereum/core/bloombits"
    31  	"github.com/ethereum/go-ethereum/core/rawdb"
    32  	"github.com/ethereum/go-ethereum/core/state"
    33  	"github.com/ethereum/go-ethereum/core/types"
    34  	"github.com/ethereum/go-ethereum/core/vm"
    35  	"github.com/ethereum/go-ethereum/eth/downloader"
    36  	"github.com/ethereum/go-ethereum/eth/gasprice"
    37  	"github.com/ethereum/go-ethereum/ethdb"
    38  	"github.com/ethereum/go-ethereum/event"
    39  	"github.com/ethereum/go-ethereum/log"
    40  	"github.com/ethereum/go-ethereum/multitenancy"
    41  	"github.com/ethereum/go-ethereum/params"
    42  	pcore "github.com/ethereum/go-ethereum/permission/core"
    43  	"github.com/ethereum/go-ethereum/rpc"
    44  	"github.com/jpmorganchase/quorum-security-plugin-sdk-go/proto"
    45  )
    46  
    47  // EthAPIBackend implements ethapi.Backend for full nodes
    48  type EthAPIBackend struct {
    49  	extRPCEnabled bool
    50  	eth           *Ethereum
    51  	gpo           *gasprice.Oracle
    52  
    53  	// Quorum
    54  	//
    55  	// hex node id from node public key
    56  	hexNodeId string
    57  
    58  	// timeout value for call
    59  	evmCallTimeOut time.Duration
    60  }
    61  
    62  // ChainConfig returns the active chain configuration.
    63  func (b *EthAPIBackend) ChainConfig() *params.ChainConfig {
    64  	return b.eth.blockchain.Config()
    65  }
    66  
    67  func (b *EthAPIBackend) CurrentBlock() *types.Block {
    68  	return b.eth.blockchain.CurrentBlock()
    69  }
    70  
    71  func (b *EthAPIBackend) SetHead(number uint64) {
    72  	b.eth.protocolManager.downloader.Cancel()
    73  	b.eth.blockchain.SetHead(number)
    74  }
    75  
    76  func (b *EthAPIBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) {
    77  	// Pending block is only known by the miner
    78  	if number == rpc.PendingBlockNumber {
    79  		block := b.eth.miner.PendingBlock()
    80  		return block.Header(), nil
    81  	}
    82  	// Otherwise resolve and return the block
    83  	if number == rpc.LatestBlockNumber {
    84  		return b.eth.blockchain.CurrentBlock().Header(), nil
    85  	}
    86  	return b.eth.blockchain.GetHeaderByNumber(uint64(number)), nil
    87  }
    88  
    89  func (b *EthAPIBackend) HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error) {
    90  	if blockNr, ok := blockNrOrHash.Number(); ok {
    91  		return b.HeaderByNumber(ctx, blockNr)
    92  	}
    93  	if hash, ok := blockNrOrHash.Hash(); ok {
    94  		header := b.eth.blockchain.GetHeaderByHash(hash)
    95  		if header == nil {
    96  			return nil, errors.New("header for hash not found")
    97  		}
    98  		if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash {
    99  			return nil, errors.New("hash is not currently canonical")
   100  		}
   101  		return header, nil
   102  	}
   103  	return nil, errors.New("invalid arguments; neither block nor hash specified")
   104  }
   105  
   106  func (b *EthAPIBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
   107  	return b.eth.blockchain.GetHeaderByHash(hash), nil
   108  }
   109  
   110  func (b *EthAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) {
   111  	// Pending block is only known by the miner
   112  	if number == rpc.PendingBlockNumber {
   113  		if b.eth.protocolManager.raftMode {
   114  			// Use latest instead.
   115  			return b.eth.blockchain.CurrentBlock(), nil
   116  		}
   117  		block := b.eth.miner.PendingBlock()
   118  		return block, nil
   119  	}
   120  	// Otherwise resolve and return the block
   121  	if number == rpc.LatestBlockNumber {
   122  		return b.eth.blockchain.CurrentBlock(), nil
   123  	}
   124  	return b.eth.blockchain.GetBlockByNumber(uint64(number)), nil
   125  }
   126  
   127  func (b *EthAPIBackend) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) {
   128  	return b.eth.blockchain.GetBlockByHash(hash), nil
   129  }
   130  
   131  func (b *EthAPIBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error) {
   132  	if blockNr, ok := blockNrOrHash.Number(); ok {
   133  		return b.BlockByNumber(ctx, blockNr)
   134  	}
   135  	if hash, ok := blockNrOrHash.Hash(); ok {
   136  		header := b.eth.blockchain.GetHeaderByHash(hash)
   137  		if header == nil {
   138  			return nil, errors.New("header for hash not found")
   139  		}
   140  		if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash {
   141  			return nil, errors.New("hash is not currently canonical")
   142  		}
   143  		block := b.eth.blockchain.GetBlock(hash, header.Number.Uint64())
   144  		if block == nil {
   145  			return nil, errors.New("header found, but block body is missing")
   146  		}
   147  		return block, nil
   148  	}
   149  	return nil, errors.New("invalid arguments; neither block nor hash specified")
   150  }
   151  
   152  func (b *EthAPIBackend) StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (vm.MinimalApiState, *types.Header, error) {
   153  	// Pending state is only known by the miner
   154  	if number == rpc.PendingBlockNumber {
   155  		// Quorum
   156  		if b.eth.protocolManager.raftMode {
   157  			// Use latest instead.
   158  			header, err := b.HeaderByNumber(ctx, rpc.LatestBlockNumber)
   159  			if header == nil || err != nil {
   160  				return nil, nil, err
   161  			}
   162  			publicState, privateState, err := b.eth.BlockChain().StateAt(header.Root)
   163  			return EthAPIState{publicState, privateState}, header, err
   164  		}
   165  		block, publicState, privateState := b.eth.miner.Pending()
   166  		return EthAPIState{publicState, privateState}, block.Header(), nil
   167  	}
   168  	// Otherwise resolve the block number and return its state
   169  	header, err := b.HeaderByNumber(ctx, number)
   170  	if err != nil {
   171  		return nil, nil, err
   172  	}
   173  	if header == nil {
   174  		return nil, nil, errors.New("header not found")
   175  	}
   176  	stateDb, privateState, err := b.eth.BlockChain().StateAt(header.Root)
   177  	return EthAPIState{stateDb, privateState}, header, err
   178  
   179  }
   180  
   181  func (b *EthAPIBackend) StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (vm.MinimalApiState, *types.Header, error) {
   182  	if blockNr, ok := blockNrOrHash.Number(); ok {
   183  		return b.StateAndHeaderByNumber(ctx, blockNr)
   184  	}
   185  	if hash, ok := blockNrOrHash.Hash(); ok {
   186  		header, err := b.HeaderByHash(ctx, hash)
   187  		if err != nil {
   188  			return nil, nil, err
   189  		}
   190  		if header == nil {
   191  			return nil, nil, errors.New("header for hash not found")
   192  		}
   193  		if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash {
   194  			return nil, nil, errors.New("hash is not currently canonical")
   195  		}
   196  		stateDb, privateState, err := b.eth.BlockChain().StateAt(header.Root)
   197  		return EthAPIState{stateDb, privateState}, header, err
   198  
   199  	}
   200  	return nil, nil, errors.New("invalid arguments; neither block nor hash specified")
   201  }
   202  
   203  func (b *EthAPIBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
   204  	return b.eth.blockchain.GetReceiptsByHash(hash), nil
   205  }
   206  
   207  func (b *EthAPIBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) {
   208  	receipts := b.eth.blockchain.GetReceiptsByHash(hash)
   209  	if receipts == nil {
   210  		return nil, nil
   211  	}
   212  	logs := make([][]*types.Log, len(receipts))
   213  	for i, receipt := range receipts {
   214  		logs[i] = receipt.Logs
   215  	}
   216  	return logs, nil
   217  }
   218  
   219  func (b *EthAPIBackend) GetTd(blockHash common.Hash) *big.Int {
   220  	return b.eth.blockchain.GetTdByHash(blockHash)
   221  }
   222  
   223  func (b *EthAPIBackend) GetEVM(ctx context.Context, msg core.Message, state vm.MinimalApiState, header *types.Header) (*vm.EVM, func() error, error) {
   224  	statedb := state.(EthAPIState)
   225  	from := statedb.state.GetOrNewStateObject(msg.From())
   226  	from.SetBalance(math.MaxBig256)
   227  	vmError := func() error { return nil }
   228  
   229  	evmCtx := core.NewEVMContext(msg, header, b.eth.BlockChain(), nil)
   230  	if _, ok := b.SupportsMultitenancy(ctx); ok {
   231  		evmCtx = core.NewMultitenancyAwareEVMContext(ctx, evmCtx)
   232  	}
   233  
   234  	// Set the private state to public state if contract address is not present in the private state
   235  	to := common.Address{}
   236  	if msg.To() != nil {
   237  		to = *msg.To()
   238  	}
   239  
   240  	privateState := statedb.privateState
   241  	if !privateState.Exist(to) {
   242  		privateState = statedb.state
   243  	}
   244  
   245  	return vm.NewEVM(evmCtx, statedb.state, privateState, b.eth.blockchain.Config(), *b.eth.blockchain.GetVMConfig()), vmError, nil
   246  }
   247  
   248  func (b *EthAPIBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
   249  	return b.eth.BlockChain().SubscribeRemovedLogsEvent(ch)
   250  }
   251  
   252  func (b *EthAPIBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
   253  	return b.eth.BlockChain().SubscribeChainEvent(ch)
   254  }
   255  
   256  func (b *EthAPIBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
   257  	return b.eth.BlockChain().SubscribeChainHeadEvent(ch)
   258  }
   259  
   260  func (b *EthAPIBackend) SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription {
   261  	return b.eth.BlockChain().SubscribeChainSideEvent(ch)
   262  }
   263  
   264  func (b *EthAPIBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
   265  	return b.eth.BlockChain().SubscribeLogsEvent(ch)
   266  }
   267  
   268  func (b *EthAPIBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error {
   269  	// validation for node need to happen here and cannot be done as a part of
   270  	// validateTx in tx_pool.go as tx_pool validation will happen in every node
   271  	if b.hexNodeId != "" && !pcore.ValidateNodeForTxn(b.hexNodeId, signedTx.From()) {
   272  		return errors.New("cannot send transaction from this node")
   273  	}
   274  	return b.eth.txPool.AddLocal(signedTx)
   275  }
   276  
   277  func (b *EthAPIBackend) GetPoolTransactions() (types.Transactions, error) {
   278  	pending, err := b.eth.txPool.Pending()
   279  	if err != nil {
   280  		return nil, err
   281  	}
   282  	var txs types.Transactions
   283  	for _, batch := range pending {
   284  		txs = append(txs, batch...)
   285  	}
   286  	return txs, nil
   287  }
   288  
   289  func (b *EthAPIBackend) GetPoolTransaction(hash common.Hash) *types.Transaction {
   290  	return b.eth.txPool.Get(hash)
   291  }
   292  
   293  func (b *EthAPIBackend) GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) {
   294  	tx, blockHash, blockNumber, index := rawdb.ReadTransaction(b.eth.ChainDb(), txHash)
   295  	return tx, blockHash, blockNumber, index, nil
   296  }
   297  
   298  func (b *EthAPIBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
   299  	return b.eth.txPool.Nonce(addr), nil
   300  }
   301  
   302  func (b *EthAPIBackend) Stats() (pending int, queued int) {
   303  	return b.eth.txPool.Stats()
   304  }
   305  
   306  func (b *EthAPIBackend) TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
   307  	return b.eth.TxPool().Content()
   308  }
   309  
   310  func (b *EthAPIBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription {
   311  	return b.eth.TxPool().SubscribeNewTxsEvent(ch)
   312  }
   313  
   314  func (b *EthAPIBackend) Downloader() *downloader.Downloader {
   315  	return b.eth.Downloader()
   316  }
   317  
   318  func (b *EthAPIBackend) ProtocolVersion() int {
   319  	return b.eth.EthVersion()
   320  }
   321  
   322  func (b *EthAPIBackend) SuggestPrice(ctx context.Context) (*big.Int, error) {
   323  	if b.ChainConfig().IsQuorum {
   324  		return big.NewInt(0), nil
   325  	} else {
   326  		return b.gpo.SuggestPrice(ctx)
   327  	}
   328  }
   329  
   330  func (b *EthAPIBackend) ChainDb() ethdb.Database {
   331  	return b.eth.ChainDb()
   332  }
   333  
   334  func (b *EthAPIBackend) EventMux() *event.TypeMux {
   335  	return b.eth.EventMux()
   336  }
   337  
   338  func (b *EthAPIBackend) AccountManager() *accounts.Manager {
   339  	return b.eth.AccountManager()
   340  }
   341  
   342  func (b *EthAPIBackend) ExtRPCEnabled() bool {
   343  	return b.extRPCEnabled
   344  }
   345  
   346  func (b *EthAPIBackend) CallTimeOut() time.Duration {
   347  	return b.evmCallTimeOut
   348  }
   349  
   350  func (b *EthAPIBackend) RPCGasCap() *big.Int {
   351  	return b.eth.config.RPCGasCap
   352  }
   353  
   354  func (b *EthAPIBackend) BloomStatus() (uint64, uint64) {
   355  	sections, _, _ := b.eth.bloomIndexer.Sections()
   356  	return params.BloomBitsBlocks, sections
   357  }
   358  
   359  func (b *EthAPIBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) {
   360  	for i := 0; i < bloomFilterThreads; i++ {
   361  		go session.Multiplex(bloomRetrievalBatch, bloomRetrievalWait, b.eth.bloomRequests)
   362  	}
   363  }
   364  
   365  // The validation of pre-requisite for multitenancy is done when EthService
   366  // is being created. So it's safe to use the config value here.
   367  func (b *EthAPIBackend) SupportsMultitenancy(rpcCtx context.Context) (*proto.PreAuthenticatedAuthenticationToken, bool) {
   368  	authToken, isPreauthenticated := rpcCtx.Value(rpc.CtxPreauthenticatedToken).(*proto.PreAuthenticatedAuthenticationToken)
   369  	if isPreauthenticated && b.eth.config.EnableMultitenancy {
   370  		return authToken, true
   371  	}
   372  	return nil, false
   373  }
   374  
   375  func (b *EthAPIBackend) AccountExtraDataStateGetterByNumber(ctx context.Context, number rpc.BlockNumber) (vm.AccountExtraDataStateGetter, error) {
   376  	s, _, err := b.StateAndHeaderByNumber(ctx, number)
   377  	return s, err
   378  }
   379  
   380  func (b *EthAPIBackend) IsAuthorized(ctx context.Context, authToken *proto.PreAuthenticatedAuthenticationToken, attributes ...*multitenancy.ContractSecurityAttribute) (bool, error) {
   381  	auth, err := b.eth.contractAuthzProvider.IsAuthorized(ctx, authToken, attributes...)
   382  	if err != nil {
   383  		log.Error("failed to perform authorization check", "err", err, "granted", string(authToken.RawToken), "ask", attributes)
   384  		return false, err
   385  	}
   386  	return auth, nil
   387  }
   388  
   389  // used by Quorum
   390  type EthAPIState struct {
   391  	state, privateState *state.StateDB
   392  }
   393  
   394  func (s EthAPIState) GetBalance(addr common.Address) *big.Int {
   395  	if s.privateState.Exist(addr) {
   396  		return s.privateState.GetBalance(addr)
   397  	}
   398  	return s.state.GetBalance(addr)
   399  }
   400  
   401  func (s EthAPIState) GetCode(addr common.Address) []byte {
   402  	if s.privateState.Exist(addr) {
   403  		return s.privateState.GetCode(addr)
   404  	}
   405  	return s.state.GetCode(addr)
   406  }
   407  
   408  func (s EthAPIState) SetNonce(addr common.Address, nonce uint64) {
   409  	if s.privateState.Exist(addr) {
   410  		s.privateState.SetNonce(addr, nonce)
   411  	} else {
   412  		s.state.SetNonce(addr, nonce)
   413  	}
   414  }
   415  
   416  func (s EthAPIState) SetCode(addr common.Address, code []byte) {
   417  	if s.privateState.Exist(addr) {
   418  		s.privateState.SetCode(addr, code)
   419  	} else {
   420  		s.state.SetCode(addr, code)
   421  	}
   422  }
   423  
   424  func (s EthAPIState) SetBalance(addr common.Address, balance *big.Int) {
   425  	if s.privateState.Exist(addr) {
   426  		s.privateState.SetBalance(addr, balance)
   427  	} else {
   428  		s.state.SetBalance(addr, balance)
   429  	}
   430  }
   431  
   432  func (s EthAPIState) SetStorage(addr common.Address, storage map[common.Hash]common.Hash) {
   433  	if s.privateState.Exist(addr) {
   434  		s.privateState.SetStorage(addr, storage)
   435  	} else {
   436  		s.state.SetStorage(addr, storage)
   437  	}
   438  }
   439  
   440  func (s EthAPIState) SetState(a common.Address, key common.Hash, value common.Hash) {
   441  	if s.privateState.Exist(a) {
   442  		s.privateState.SetState(a, key, value)
   443  	} else {
   444  		s.state.SetState(a, key, value)
   445  	}
   446  }
   447  
   448  func (s EthAPIState) GetState(a common.Address, b common.Hash) common.Hash {
   449  	if s.privateState.Exist(a) {
   450  		return s.privateState.GetState(a, b)
   451  	}
   452  	return s.state.GetState(a, b)
   453  }
   454  
   455  func (s EthAPIState) GetNonce(addr common.Address) uint64 {
   456  	if s.privateState.Exist(addr) {
   457  		return s.privateState.GetNonce(addr)
   458  	}
   459  	return s.state.GetNonce(addr)
   460  }
   461  
   462  func (s EthAPIState) GetPrivacyMetadata(addr common.Address) (*state.PrivacyMetadata, error) {
   463  	if s.privateState.Exist(addr) {
   464  		return s.privateState.GetPrivacyMetadata(addr)
   465  	}
   466  	return nil, fmt.Errorf("%x: %w", addr, common.ErrNotPrivateContract)
   467  }
   468  
   469  func (s EthAPIState) GetManagedParties(addr common.Address) ([]string, error) {
   470  	if s.privateState.Exist(addr) {
   471  		return s.privateState.GetManagedParties(addr)
   472  	}
   473  	return nil, fmt.Errorf("%x: %w", addr, common.ErrNotPrivateContract)
   474  }
   475  
   476  func (s EthAPIState) GetRLPEncodedStateObject(addr common.Address) ([]byte, error) {
   477  	getFunc := s.state.GetRLPEncodedStateObject
   478  	if s.privateState.Exist(addr) {
   479  		getFunc = s.privateState.GetRLPEncodedStateObject
   480  	}
   481  	return getFunc(addr)
   482  }
   483  
   484  func (s EthAPIState) GetProof(addr common.Address) ([][]byte, error) {
   485  	if s.privateState.Exist(addr) {
   486  		return s.privateState.GetProof(addr)
   487  	}
   488  	return s.state.GetProof(addr)
   489  }
   490  
   491  func (s EthAPIState) GetStorageProof(addr common.Address, h common.Hash) ([][]byte, error) {
   492  	if s.privateState.Exist(addr) {
   493  		return s.privateState.GetStorageProof(addr, h)
   494  	}
   495  	return s.state.GetStorageProof(addr, h)
   496  }
   497  
   498  func (s EthAPIState) StorageTrie(addr common.Address) state.Trie {
   499  	if s.privateState.Exist(addr) {
   500  		return s.privateState.StorageTrie(addr)
   501  	}
   502  	return s.state.StorageTrie(addr)
   503  }
   504  
   505  func (s EthAPIState) Error() error {
   506  	if s.privateState.Error() != nil {
   507  		return s.privateState.Error()
   508  	}
   509  	return s.state.Error()
   510  }
   511  
   512  func (s EthAPIState) GetCodeHash(addr common.Address) common.Hash {
   513  	if s.privateState.Exist(addr) {
   514  		return s.privateState.GetCodeHash(addr)
   515  	}
   516  	return s.state.GetCodeHash(addr)
   517  }
   518  
   519  //func (s MinimalApiState) Error