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