github.com/klaytn/klaytn@v1.10.2/blockchain/vm/contract.go (about)

     1  // Modifications Copyright 2018 The klaytn Authors
     2  // Copyright 2015 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from core/vm/contract.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package vm
    22  
    23  import (
    24  	"math/big"
    25  
    26  	"github.com/klaytn/klaytn/blockchain/types"
    27  	"github.com/klaytn/klaytn/common"
    28  )
    29  
    30  // AccountRef implements ContractRef.
    31  //
    32  // Account references are used during EVM initialisation and
    33  // it's primary use is to fetch addresses. Removing this object
    34  // proves difficult because of the cached jump destinations which
    35  // are fetched from the parent contract (i.e. the caller), which
    36  // is a ContractRef.
    37  type AccountRef common.Address
    38  
    39  // Address casts AccountRef to a Address
    40  func (ar AccountRef) Address() common.Address  { return (common.Address)(ar) }
    41  func (ar AccountRef) FeePayer() common.Address { return ar.Address() }
    42  
    43  // Contract represents a smart 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  	FeePayerAddress common.Address
    51  	caller          types.ContractRef
    52  	self            types.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
    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 types.ContractRef, object types.ContractRef, value *big.Int, gas uint64) *Contract {
    68  	c := &Contract{CallerAddress: caller.Address(), FeePayerAddress: caller.FeePayer(), 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 *big.Int) bool {
    87  	udest := dest.Uint64()
    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 dest.BitLen() >= 63 || 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  	// Do we have it locally already?
    98  	if c.analysis != nil {
    99  		return c.analysis.codeSegment(udest)
   100  	}
   101  	// If we have the code hash (but no analysis), we should look into the
   102  	// parent analysis map and see if the analysis has been made previously
   103  	if c.CodeHash != (common.Hash{}) {
   104  		analysis, exist := c.jumpdests[c.CodeHash]
   105  		if !exist {
   106  			// Do the analysis and save in parent context
   107  			analysis = codeBitmap(c.Code)
   108  			c.jumpdests[c.CodeHash] = analysis
   109  		}
   110  		// Also stash it in current contract for faster access
   111  		c.analysis = analysis
   112  		return analysis.codeSegment(udest)
   113  	}
   114  	// We don't have the code hash, most likely a piece of initcode not already
   115  	// in state trie. In that case, we do an analysis, and save it locally, so
   116  	// we don't have to recalculate it for every JUMP instruction in the execution
   117  	// However, we don't save it within the parent context
   118  	c.analysis = codeBitmap(c.Code)
   119  	return c.analysis.codeSegment(udest)
   120  }
   121  
   122  // AsDelegate sets the contract to be a delegate call and returns the current
   123  // contract (for chaining calls)
   124  func (c *Contract) AsDelegate() *Contract {
   125  	// NOTE: caller must, at all times be a contract. It should never happen
   126  	// that caller is something other than a Contract.
   127  	parent := c.caller.(*Contract)
   128  	c.CallerAddress = parent.CallerAddress
   129  	c.value = parent.value
   130  
   131  	return c
   132  }
   133  
   134  // GetOp returns the n'th element in the contract's byte array
   135  func (c *Contract) GetOp(n uint64) OpCode {
   136  	return OpCode(c.GetByte(n))
   137  }
   138  
   139  // GetByte returns the n'th byte in the contract's byte array
   140  func (c *Contract) GetByte(n uint64) byte {
   141  	if n < uint64(len(c.Code)) {
   142  		return c.Code[n]
   143  	}
   144  
   145  	return 0
   146  }
   147  
   148  // Caller returns the caller of the contract.
   149  //
   150  // Caller will recursively call caller when the contract is a delegate
   151  // call, including that of caller's caller.
   152  func (c *Contract) Caller() common.Address {
   153  	return c.CallerAddress
   154  }
   155  
   156  // UseGas attempts the use gas and subtracts it and returns true on success
   157  func (c *Contract) UseGas(gas uint64) (ok bool) {
   158  	if c.Gas < gas {
   159  		return false
   160  	}
   161  	c.Gas -= gas
   162  	return true
   163  }
   164  
   165  // Address returns the contracts address
   166  func (c *Contract) Address() common.Address {
   167  	return c.self.Address()
   168  }
   169  
   170  func (c *Contract) FeePayer() common.Address {
   171  	return c.FeePayerAddress
   172  }
   173  
   174  // Value returns the contracts value (sent to it from it's caller)
   175  func (c *Contract) Value() *big.Int {
   176  	return c.value
   177  }
   178  
   179  // SetCallCode sets the code of the contract and address of the backing data
   180  // object
   181  func (c *Contract) SetCallCode(addr *common.Address, hash common.Hash, code []byte) {
   182  	c.Code = code
   183  	c.CodeHash = hash
   184  	c.CodeAddr = addr
   185  }
   186  
   187  // SetCodeOptionalHash can be used to provide code, but it's optional to provide hash.
   188  // In case hash is not provided, the jumpdest analysis will not be saved to the parent context
   189  func (c *Contract) SetCodeOptionalHash(addr *common.Address, codeAndHash *codeAndHash) {
   190  	c.Code = codeAndHash.code
   191  	c.CodeHash = codeAndHash.hash
   192  	c.CodeAddr = addr
   193  }