github.com/Unheilbar/quorum@v1.0.0/core/evm.go (about)

     1  // Copyright 2016 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 core
    18  
    19  import (
    20  	"math/big"
    21  
    22  	"github.com/ethereum/go-ethereum/common"
    23  	"github.com/ethereum/go-ethereum/consensus"
    24  	"github.com/ethereum/go-ethereum/core/mps"
    25  	"github.com/ethereum/go-ethereum/core/state"
    26  	"github.com/ethereum/go-ethereum/core/types"
    27  	"github.com/ethereum/go-ethereum/core/vm"
    28  	"github.com/ethereum/go-ethereum/params"
    29  )
    30  
    31  // ChainContext supports retrieving headers and consensus parameters from the
    32  // current blockchain to be used during transaction processing.
    33  // TODO: Split this interface for the quorum functions ex: ChainContextWithQuorum
    34  type ChainContext interface {
    35  	// Engine retrieves the chain's consensus engine.
    36  	Engine() consensus.Engine
    37  
    38  	// GetHeader returns the hash corresponding to their hash.
    39  	GetHeader(common.Hash, uint64) *types.Header
    40  
    41  	// Config retrieves the chain's fork configuration
    42  	Config() *params.ChainConfig
    43  
    44  	// Quorum
    45  
    46  	// QuorumConfig retrieves the Quorum chain's configuration
    47  	QuorumConfig() *QuorumChainConfig
    48  
    49  	// PrivateStateManager returns the private state manager
    50  	PrivateStateManager() mps.PrivateStateManager
    51  
    52  	// CheckAndSetPrivateState updates the private state as a part contract state extension
    53  	CheckAndSetPrivateState(txLogs []*types.Log, privateState *state.StateDB, psi types.PrivateStateIdentifier)
    54  
    55  	// End Quorum
    56  }
    57  
    58  // NewEVMBlockContext creates a new context for use in the EVM.
    59  func NewEVMBlockContext(header *types.Header, chain ChainContext, author *common.Address) vm.BlockContext {
    60  	// If we don't have an explicit author (i.e. not mining), extract from the header
    61  	var beneficiary common.Address
    62  	if author == nil {
    63  		beneficiary, _ = chain.Engine().Author(header) // Ignore error, we're past header validation
    64  	} else {
    65  		beneficiary = *author
    66  	}
    67  	return vm.BlockContext{
    68  		CanTransfer: CanTransfer,
    69  		Transfer:    Transfer,
    70  		GetHash:     GetHashFn(header, chain),
    71  		Coinbase:    beneficiary,
    72  		BlockNumber: new(big.Int).Set(header.Number),
    73  		Time:        new(big.Int).SetUint64(header.Time),
    74  		Difficulty:  new(big.Int).Set(header.Difficulty),
    75  		GasLimit:    header.GasLimit,
    76  	}
    77  }
    78  
    79  // NewEVMTxContext creates a new transaction context for a single transaction.
    80  func NewEVMTxContext(msg Message) vm.TxContext {
    81  	return vm.TxContext{
    82  		Origin:   msg.From(),
    83  		GasPrice: new(big.Int).Set(msg.GasPrice()),
    84  	}
    85  }
    86  
    87  // GetHashFn returns a GetHashFunc which retrieves header hashes by number
    88  func GetHashFn(ref *types.Header, chain ChainContext) func(n uint64) common.Hash {
    89  	// Cache will initially contain [refHash.parent],
    90  	// Then fill up with [refHash.p, refHash.pp, refHash.ppp, ...]
    91  	var cache []common.Hash
    92  
    93  	return func(n uint64) common.Hash {
    94  		// If there's no hash cache yet, make one
    95  		if len(cache) == 0 {
    96  			cache = append(cache, ref.ParentHash)
    97  		}
    98  		if idx := ref.Number.Uint64() - n - 1; idx < uint64(len(cache)) {
    99  			return cache[idx]
   100  		}
   101  		// No luck in the cache, but we can start iterating from the last element we already know
   102  		lastKnownHash := cache[len(cache)-1]
   103  		lastKnownNumber := ref.Number.Uint64() - uint64(len(cache))
   104  
   105  		for {
   106  			header := chain.GetHeader(lastKnownHash, lastKnownNumber)
   107  			if header == nil {
   108  				break
   109  			}
   110  			cache = append(cache, header.ParentHash)
   111  			lastKnownHash = header.ParentHash
   112  			lastKnownNumber = header.Number.Uint64() - 1
   113  			if n == lastKnownNumber {
   114  				return lastKnownHash
   115  			}
   116  		}
   117  		return common.Hash{}
   118  	}
   119  }
   120  
   121  // CanTransfer checks whether there are enough funds in the address' account to make a transfer.
   122  // This does not take the necessary gas in to account to make the transfer valid.
   123  func CanTransfer(db vm.StateDB, addr common.Address, amount *big.Int) bool {
   124  	return db.GetBalance(addr).Cmp(amount) >= 0
   125  }
   126  
   127  // Transfer subtracts amount from sender and adds amount to recipient using the given Db
   128  func Transfer(db vm.StateDB, sender, recipient common.Address, amount *big.Int) {
   129  	db.SubBalance(sender, amount)
   130  	db.AddBalance(recipient, amount)
   131  }