github.com/theQRL/go-zond@v0.1.1/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  	"github.com/theQRL/go-zond/common"
    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  // isCode returns true if the provided PC location is an actual opcode, as
   100  // opposed to a data-segment following a PUSHN operation.
   101  func (c *Contract) isCode(udest uint64) bool {
   102  	// Do we already have an analysis laying around?
   103  	if c.analysis != nil {
   104  		return c.analysis.codeSegment(udest)
   105  	}
   106  	// Do we have a contract hash already?
   107  	// If we do have a hash, that means it's a 'regular' contract. For regular
   108  	// contracts ( not temporary initcode), we store the analysis in a map
   109  	if c.CodeHash != (common.Hash{}) {
   110  		// Does parent context have the analysis?
   111  		analysis, exist := c.jumpdests[c.CodeHash]
   112  		if !exist {
   113  			// Do the analysis and save in parent context
   114  			// We do not need to store it in c.analysis
   115  			analysis = codeBitmap(c.Code)
   116  			c.jumpdests[c.CodeHash] = analysis
   117  		}
   118  		// Also stash it in current contract for faster access
   119  		c.analysis = analysis
   120  		return analysis.codeSegment(udest)
   121  	}
   122  	// We don't have the code hash, most likely a piece of initcode not already
   123  	// in state trie. In that case, we do an analysis, and save it locally, so
   124  	// we don't have to recalculate it for every JUMP instruction in the execution
   125  	// However, we don't save it within the parent context
   126  	if c.analysis == nil {
   127  		c.analysis = codeBitmap(c.Code)
   128  	}
   129  	return c.analysis.codeSegment(udest)
   130  }
   131  
   132  // AsDelegate sets the contract to be a delegate call and returns the current
   133  // contract (for chaining calls)
   134  func (c *Contract) AsDelegate() *Contract {
   135  	// NOTE: caller must, at all times be a contract. It should never happen
   136  	// that caller is something other than a Contract.
   137  	parent := c.caller.(*Contract)
   138  	c.CallerAddress = parent.CallerAddress
   139  	c.value = parent.value
   140  
   141  	return c
   142  }
   143  
   144  // GetOp returns the n'th element in the contract's byte array
   145  func (c *Contract) GetOp(n uint64) OpCode {
   146  	if n < uint64(len(c.Code)) {
   147  		return OpCode(c.Code[n])
   148  	}
   149  
   150  	return STOP
   151  }
   152  
   153  // Caller returns the caller of the contract.
   154  //
   155  // Caller will recursively call caller when the contract is a delegate
   156  // call, including that of caller's caller.
   157  func (c *Contract) Caller() common.Address {
   158  	return c.CallerAddress
   159  }
   160  
   161  // UseGas attempts the use gas and subtracts it and returns true on success
   162  func (c *Contract) UseGas(gas uint64) (ok bool) {
   163  	if c.Gas < gas {
   164  		return false
   165  	}
   166  	c.Gas -= gas
   167  	return true
   168  }
   169  
   170  // Address returns the contracts address
   171  func (c *Contract) Address() common.Address {
   172  	return c.self.Address()
   173  }
   174  
   175  // Value returns the contract's value (sent to it from it's caller)
   176  func (c *Contract) Value() *big.Int {
   177  	return c.value
   178  }
   179  
   180  // SetCallCode sets the code of the contract and address of the backing data
   181  // object
   182  func (c *Contract) SetCallCode(addr *common.Address, hash common.Hash, code []byte) {
   183  	c.Code = code
   184  	c.CodeHash = hash
   185  	c.CodeAddr = addr
   186  }
   187  
   188  // SetCodeOptionalHash can be used to provide code, but it's optional to provide hash.
   189  // In case hash is not provided, the jumpdest analysis will not be saved to the parent context
   190  func (c *Contract) SetCodeOptionalHash(addr *common.Address, codeAndHash *codeAndHash) {
   191  	c.Code = codeAndHash.code
   192  	c.CodeHash = codeAndHash.hash
   193  	c.CodeAddr = addr
   194  }