github.com/cryptotooltop/go-ethereum@v0.0.0-20231103184714-151d1922f3e5/core/vm/contract.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 vm
    18  
    19  import (
    20  	"math/big"
    21  
    22  	"github.com/holiman/uint256"
    23  
    24  	"github.com/scroll-tech/go-ethereum/common"
    25  )
    26  
    27  // ContractRef is a reference to the contract's backing object
    28  type ContractRef interface {
    29  	Address() common.Address
    30  }
    31  
    32  // AccountRef implements ContractRef.
    33  //
    34  // Account references are used during EVM initialisation and
    35  // it's primary use is to fetch addresses. Removing this object
    36  // proves difficult because of the cached jump destinations which
    37  // are fetched from the parent contract (i.e. the caller), which
    38  // is a ContractRef.
    39  type AccountRef common.Address
    40  
    41  // Address casts AccountRef to a Address
    42  func (ar AccountRef) Address() common.Address { return (common.Address)(ar) }
    43  
    44  // Contract represents an ethereum contract in the state database. It contains
    45  // the contract code, calling arguments. Contract implements ContractRef
    46  type Contract struct {
    47  	// CallerAddress is the result of the caller which initialised this
    48  	// contract. However when the "call method" is delegated this value
    49  	// needs to be initialised to that of the caller's caller.
    50  	CallerAddress common.Address
    51  	caller        ContractRef
    52  	self          ContractRef
    53  
    54  	jumpdests map[common.Hash]bitvec // Aggregated result of JUMPDEST analysis.
    55  	analysis  bitvec                 // Locally cached result of JUMPDEST analysis
    56  
    57  	Code     []byte
    58  	CodeHash common.Hash // Keccak code hash
    59  	CodeAddr *common.Address
    60  	Input    []byte
    61  
    62  	Gas   uint64
    63  	value *big.Int
    64  }
    65  
    66  // NewContract returns a new contract environment for the execution of EVM.
    67  func NewContract(caller ContractRef, object ContractRef, value *big.Int, gas uint64) *Contract {
    68  	c := &Contract{CallerAddress: caller.Address(), caller: caller, self: object}
    69  
    70  	if parent, ok := caller.(*Contract); ok {
    71  		// Reuse JUMPDEST analysis from parent context if available.
    72  		c.jumpdests = parent.jumpdests
    73  	} else {
    74  		c.jumpdests = make(map[common.Hash]bitvec)
    75  	}
    76  
    77  	// Gas should be a pointer so it can safely be reduced through the run
    78  	// This pointer will be off the state transition
    79  	c.Gas = gas
    80  	// ensures a value is set
    81  	c.value = value
    82  
    83  	return c
    84  }
    85  
    86  func (c *Contract) validJumpdest(dest *uint256.Int) bool {
    87  	udest, overflow := dest.Uint64WithOverflow()
    88  	// PC cannot go beyond len(code) and certainly can't be bigger than 63bits.
    89  	// Don't bother checking for JUMPDEST in that case.
    90  	if overflow || udest >= uint64(len(c.Code)) {
    91  		return false
    92  	}
    93  	// Only JUMPDESTs allowed for destinations
    94  	if OpCode(c.Code[udest]) != JUMPDEST {
    95  		return false
    96  	}
    97  	return c.isCode(udest)
    98  }
    99  
   100  // isCode returns true if the provided PC location is an actual opcode, as
   101  // opposed to a data-segment following a PUSHN operation.
   102  func (c *Contract) isCode(udest uint64) bool {
   103  	// Do we already have an analysis laying around?
   104  	if c.analysis != nil {
   105  		return c.analysis.codeSegment(udest)
   106  	}
   107  	// Do we have a contract hash already?
   108  	// If we do have a hash, that means it's a 'regular' contract. For regular
   109  	// contracts ( not temporary initcode), we store the analysis in a map
   110  	if c.CodeHash != (common.Hash{}) {
   111  		// Does parent context have the analysis?
   112  		analysis, exist := c.jumpdests[c.CodeHash]
   113  		if !exist {
   114  			// Do the analysis and save in parent context
   115  			// We do not need to store it in c.analysis
   116  			analysis = codeBitmap(c.Code)
   117  			c.jumpdests[c.CodeHash] = analysis
   118  		}
   119  		// Also stash it in current contract for faster access
   120  		c.analysis = analysis
   121  		return analysis.codeSegment(udest)
   122  	}
   123  	// We don't have the code hash, most likely a piece of initcode not already
   124  	// in state trie. In that case, we do an analysis, and save it locally, so
   125  	// we don't have to recalculate it for every JUMP instruction in the execution
   126  	// However, we don't save it within the parent context
   127  	if c.analysis == nil {
   128  		c.analysis = codeBitmap(c.Code)
   129  	}
   130  	return c.analysis.codeSegment(udest)
   131  }
   132  
   133  // AsDelegate sets the contract to be a delegate call and returns the current
   134  // contract (for chaining calls)
   135  func (c *Contract) AsDelegate() *Contract {
   136  	// NOTE: caller must, at all times be a contract. It should never happen
   137  	// that caller is something other than a Contract.
   138  	parent := c.caller.(*Contract)
   139  	c.CallerAddress = parent.CallerAddress
   140  	c.value = parent.value
   141  
   142  	return c
   143  }
   144  
   145  // GetOp returns the n'th element in the contract's byte array
   146  func (c *Contract) GetOp(n uint64) OpCode {
   147  	return OpCode(c.GetByte(n))
   148  }
   149  
   150  // GetByte returns the n'th byte in the contract's byte array
   151  func (c *Contract) GetByte(n uint64) byte {
   152  	if n < uint64(len(c.Code)) {
   153  		return c.Code[n]
   154  	}
   155  
   156  	return 0
   157  }
   158  
   159  // Caller returns the caller of the contract.
   160  //
   161  // Caller will recursively call caller when the contract is a delegate
   162  // call, including that of caller's caller.
   163  func (c *Contract) Caller() common.Address {
   164  	return c.CallerAddress
   165  }
   166  
   167  // UseGas attempts the use gas and subtracts it and returns true on success
   168  func (c *Contract) UseGas(gas uint64) (ok bool) {
   169  	if c.Gas < gas {
   170  		return false
   171  	}
   172  	c.Gas -= gas
   173  	return true
   174  }
   175  
   176  // Address returns the contracts address
   177  func (c *Contract) Address() common.Address {
   178  	return c.self.Address()
   179  }
   180  
   181  // Value returns the contract's value (sent to it from it's caller)
   182  func (c *Contract) Value() *big.Int {
   183  	return c.value
   184  }
   185  
   186  // SetCallCode sets the code of the contract and address of the backing data
   187  // object
   188  func (c *Contract) SetCallCode(addr *common.Address, hash common.Hash, code []byte) {
   189  	c.Code = code
   190  	c.CodeHash = hash
   191  	c.CodeAddr = addr
   192  }
   193  
   194  // SetCodeOptionalHash can be used to provide code, but it's optional to provide hash.
   195  // In case hash is not provided, the jumpdest analysis will not be saved to the parent context
   196  func (c *Contract) SetCodeOptionalHash(addr *common.Address, codeAndHash *codeAndHash) {
   197  	c.Code = codeAndHash.code
   198  	c.CodeHash = codeAndHash.hash
   199  	c.CodeAddr = addr
   200  }