github.com/ethereum-optimism/optimism/l2geth@v0.0.0-20230612200230-50b04ade19e3/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-optimism/optimism/l2geth/common"
    23  	"github.com/ethereum-optimism/optimism/l2geth/consensus"
    24  	"github.com/ethereum-optimism/optimism/l2geth/core/types"
    25  	"github.com/ethereum-optimism/optimism/l2geth/core/vm"
    26  	"github.com/ethereum-optimism/optimism/l2geth/rollup/dump"
    27  	"github.com/ethereum-optimism/optimism/l2geth/rollup/rcfg"
    28  )
    29  
    30  // ChainContext supports retrieving headers and consensus parameters from the
    31  // current blockchain to be used during transaction processing.
    32  type ChainContext interface {
    33  	// Engine retrieves the chain's consensus engine.
    34  	Engine() consensus.Engine
    35  
    36  	// GetHeader returns the hash corresponding to their hash.
    37  	GetHeader(common.Hash, uint64) *types.Header
    38  }
    39  
    40  // NewEVMContext creates a new context for use in the EVM.
    41  func NewEVMContext(msg Message, header *types.Header, chain ChainContext, author *common.Address) vm.Context {
    42  	// If we don't have an explicit author (i.e. not mining), extract from the header
    43  	var beneficiary common.Address
    44  	if author == nil {
    45  		beneficiary, _ = chain.Engine().Author(header) // Ignore error, we're past header validation
    46  	} else {
    47  		beneficiary = *author
    48  	}
    49  	if rcfg.UsingOVM {
    50  		// When using the OVM, we must:
    51  		// - Set the Time to be the msg.L1Timestamp
    52  		return vm.Context{
    53  			CanTransfer:   CanTransfer,
    54  			Transfer:      Transfer,
    55  			GetHash:       GetHashFn(header, chain),
    56  			Origin:        msg.From(),
    57  			Coinbase:      dump.OvmFeeWallet, // Coinbase is the fee vault.
    58  			BlockNumber:   new(big.Int).Set(header.Number),
    59  			Time:          new(big.Int).SetUint64(msg.L1Timestamp()),
    60  			Difficulty:    new(big.Int), // Difficulty always returns zero.
    61  			GasLimit:      header.GasLimit,
    62  			GasPrice:      new(big.Int).Set(msg.GasPrice()),
    63  			L1BlockNumber: msg.L1BlockNumber(),
    64  		}
    65  	} else {
    66  		return vm.Context{
    67  			CanTransfer: CanTransfer,
    68  			Transfer:    Transfer,
    69  			GetHash:     GetHashFn(header, chain),
    70  			Origin:      msg.From(),
    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  			GasPrice:    new(big.Int).Set(msg.GasPrice()),
    77  		}
    78  	}
    79  }
    80  
    81  // GetHashFn returns a GetHashFunc which retrieves header hashes by number
    82  func GetHashFn(ref *types.Header, chain ChainContext) func(n uint64) common.Hash {
    83  	var cache map[uint64]common.Hash
    84  
    85  	return func(n uint64) common.Hash {
    86  		// If there's no hash cache yet, make one
    87  		if cache == nil {
    88  			cache = map[uint64]common.Hash{
    89  				ref.Number.Uint64() - 1: ref.ParentHash,
    90  			}
    91  		}
    92  		// Try to fulfill the request from the cache
    93  		if hash, ok := cache[n]; ok {
    94  			return hash
    95  		}
    96  		// Not cached, iterate the blocks and cache the hashes
    97  		for header := chain.GetHeader(ref.ParentHash, ref.Number.Uint64()-1); header != nil; header = chain.GetHeader(header.ParentHash, header.Number.Uint64()-1) {
    98  			cache[header.Number.Uint64()-1] = header.ParentHash
    99  			if n == header.Number.Uint64()-1 {
   100  				return header.ParentHash
   101  			}
   102  		}
   103  		return common.Hash{}
   104  	}
   105  }
   106  
   107  // CanTransfer checks whether there are enough funds in the address' account to make a transfer.
   108  // This does not take the necessary gas in to account to make the transfer valid.
   109  func CanTransfer(db vm.StateDB, addr common.Address, amount *big.Int) bool {
   110  	return db.GetBalance(addr).Cmp(amount) >= 0
   111  }
   112  
   113  // Transfer subtracts amount from sender and adds amount to recipient using the given Db
   114  func Transfer(db vm.StateDB, sender, recipient common.Address, amount *big.Int) {
   115  	db.SubBalance(sender, amount)
   116  	db.AddBalance(recipient, amount)
   117  }