github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/evmcore/evm.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 evmcore
    18  
    19  import (
    20  	"math/big"
    21  
    22  	"github.com/unicornultrafoundation/go-u2u/common"
    23  	"github.com/unicornultrafoundation/go-u2u/core/vm"
    24  )
    25  
    26  // DummyChain supports retrieving headers and consensus parameters from the
    27  // current blockchain to be used during transaction processing.
    28  type DummyChain interface {
    29  	// GetHeader returns the hash corresponding to their hash.
    30  	GetHeader(common.Hash, uint64) *EvmHeader
    31  }
    32  
    33  // NewEVMBlockContext creates a new context for use in the EVM.
    34  func NewEVMBlockContext(header *EvmHeader, chain DummyChain, author *common.Address) vm.BlockContext {
    35  	var (
    36  		beneficiary common.Address
    37  		baseFee     *big.Int
    38  	)
    39  	// If we don't have an explicit author (i.e. not mining), extract from the header
    40  	if author == nil {
    41  		beneficiary = header.Coinbase
    42  	} else {
    43  		beneficiary = *author
    44  	}
    45  	if header.BaseFee != nil {
    46  		baseFee = new(big.Int).Set(header.BaseFee)
    47  	}
    48  	return vm.BlockContext{
    49  		CanTransfer: CanTransfer,
    50  		Transfer:    Transfer,
    51  		GetHash:     GetHashFn(header, chain),
    52  		Coinbase:    beneficiary,
    53  		BlockNumber: new(big.Int).Set(header.Number),
    54  		Time:        new(big.Int).SetUint64(uint64(header.Time.Unix())),
    55  		Difficulty:  big.NewInt(1),
    56  		BaseFee:     baseFee,
    57  		GasLimit:    header.GasLimit,
    58  	}
    59  }
    60  
    61  // NewEVMTxContext creates a new transaction context for a single transaction.
    62  func NewEVMTxContext(msg Message) vm.TxContext {
    63  	return vm.TxContext{
    64  		Origin:   msg.From(),
    65  		GasPrice: new(big.Int).Set(msg.GasPrice()),
    66  	}
    67  }
    68  
    69  // GetHashFn returns a GetHashFunc which retrieves header hashes by number
    70  func GetHashFn(ref *EvmHeader, chain DummyChain) func(n uint64) common.Hash {
    71  	// Cache will initially contain [refHash.parent],
    72  	// Then fill up with [refHash.p, refHash.pp, refHash.ppp, ...]
    73  	var cache []common.Hash
    74  
    75  	return func(n uint64) common.Hash {
    76  		// If there's no hash cache yet, make one
    77  		if len(cache) == 0 {
    78  			cache = append(cache, ref.ParentHash)
    79  		}
    80  		if idx := ref.Number.Uint64() - n - 1; idx < uint64(len(cache)) {
    81  			return cache[idx]
    82  		}
    83  		// No luck in the cache, but we can start iterating from the last element we already know
    84  		lastKnownHash := cache[len(cache)-1]
    85  		lastKnownNumber := ref.Number.Uint64() - uint64(len(cache))
    86  
    87  		for {
    88  			header := chain.GetHeader(lastKnownHash, lastKnownNumber)
    89  			if header == nil {
    90  				break
    91  			}
    92  			cache = append(cache, header.ParentHash)
    93  			lastKnownHash = header.ParentHash
    94  			lastKnownNumber = header.Number.Uint64() - 1
    95  			if n == lastKnownNumber {
    96  				return lastKnownHash
    97  			}
    98  		}
    99  		return common.Hash{}
   100  	}
   101  }
   102  
   103  // CanTransfer checks whether there are enough funds in the address' account to make a transfer.
   104  // This does not take the necessary gas in to account to make the transfer valid.
   105  func CanTransfer(db vm.StateDB, addr common.Address, amount *big.Int) bool {
   106  	return db.GetBalance(addr).Cmp(amount) >= 0
   107  }
   108  
   109  // Transfer subtracts amount from sender and adds amount to recipient using the given Db
   110  func Transfer(db vm.StateDB, sender, recipient common.Address, amount *big.Int) {
   111  	db.SubBalance(sender, amount)
   112  	db.AddBalance(recipient, amount)
   113  }