github.com/core-coin/go-core/v2@v2.1.9/core/vm/contract.go (about)

     1  // Copyright 2015 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core 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-core 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-core library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package vm
    18  
    19  import (
    20  	"math/big"
    21  
    22  	"github.com/core-coin/uint256"
    23  
    24  	"github.com/core-coin/go-core/v2/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 CVM 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 core 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
    59  	CodeAddr *common.Address
    60  	Input    []byte
    61  
    62  	Energy uint64
    63  	value  *big.Int
    64  }
    65  
    66  // NewContract returns a new contract environment for the execution of CVM.
    67  func NewContract(caller ContractRef, object ContractRef, value *big.Int, energy 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  	// Energy should be a pointer so it can safely be reduced through the run
    78  	// This pointer will be off the state transition
    79  	c.Energy = energy
    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  func (c *Contract) validJumpSubdest(udest uint64) bool {
   101  	// PC cannot go beyond len(code) and certainly can't be bigger than 63 bits.
   102  	// Don't bother checking for BEGINSUB in that case.
   103  	if int64(udest) < 0 || udest >= uint64(len(c.Code)) {
   104  		return false
   105  	}
   106  	// Only BEGINSUBs allowed for destinations
   107  	if OpCode(c.Code[udest]) != BEGINSUB {
   108  		return false
   109  	}
   110  	return c.isCode(udest)
   111  }
   112  
   113  // isCode returns true if the provided PC location is an actual opcode, as
   114  // opposed to a data-segment following a PUSHN operation.
   115  func (c *Contract) isCode(udest uint64) bool {
   116  	// Do we already have an analysis laying around?
   117  	if c.analysis != nil {
   118  		return c.analysis.codeSegment(udest)
   119  	}
   120  	// Do we have a contract hash already?
   121  	// If we do have a hash, that means it's a 'regular' contract. For regular
   122  	// contracts ( not temporary initcode), we store the analysis in a map
   123  	if c.CodeHash != (common.Hash{}) {
   124  		// Does parent context have the analysis?
   125  		analysis, exist := c.jumpdests[c.CodeHash]
   126  		if !exist {
   127  			// Do the analysis and save in parent context
   128  			// We do not need to store it in c.analysis
   129  			analysis = codeBitmap(c.Code)
   130  			c.jumpdests[c.CodeHash] = analysis
   131  		}
   132  		// Also stash it in current contract for faster access
   133  		c.analysis = analysis
   134  		return analysis.codeSegment(udest)
   135  	}
   136  	// We don't have the code hash, most likely a piece of initcode not already
   137  	// in state trie. In that case, we do an analysis, and save it locally, so
   138  	// we don't have to recalculate it for every JUMP instruction in the execution
   139  	// However, we don't save it within the parent context
   140  	if c.analysis == nil {
   141  		c.analysis = codeBitmap(c.Code)
   142  	}
   143  	return c.analysis.codeSegment(udest)
   144  }
   145  
   146  // AsDelegate sets the contract to be a delegate call and returns the current
   147  // contract (for chaining calls)
   148  func (c *Contract) AsDelegate() *Contract {
   149  	// NOTE: caller must, at all times be a contract. It should never happen
   150  	// that caller is something other than a Contract.
   151  	parent := c.caller.(*Contract)
   152  	c.CallerAddress = parent.CallerAddress
   153  	c.value = parent.value
   154  
   155  	return c
   156  }
   157  
   158  // GetOp returns the n'th element in the contract's byte array
   159  func (c *Contract) GetOp(n uint64) OpCode {
   160  	return OpCode(c.GetByte(n))
   161  }
   162  
   163  // GetByte returns the n'th byte in the contract's byte array
   164  func (c *Contract) GetByte(n uint64) byte {
   165  	if n < uint64(len(c.Code)) {
   166  		return c.Code[n]
   167  	}
   168  
   169  	return 0
   170  }
   171  
   172  // Caller returns the caller of the contract.
   173  //
   174  // Caller will recursively call caller when the contract is a delegate
   175  // call, including that of caller's caller.
   176  func (c *Contract) Caller() common.Address {
   177  	return c.CallerAddress
   178  }
   179  
   180  // UseEnergy attempts the use energy and subtracts it and returns true on success
   181  func (c *Contract) UseEnergy(energy uint64) (ok bool) {
   182  	if c.Energy < energy {
   183  		return false
   184  	}
   185  	c.Energy -= energy
   186  	return true
   187  }
   188  
   189  // Address returns the contracts address
   190  func (c *Contract) Address() common.Address {
   191  	return c.self.Address()
   192  }
   193  
   194  // Value returns the contract's value (sent to it from it's caller)
   195  func (c *Contract) Value() *big.Int {
   196  	return c.value
   197  }
   198  
   199  // SetCallCode sets the code of the contract and address of the backing data
   200  // object
   201  func (c *Contract) SetCallCode(addr *common.Address, hash common.Hash, code []byte) {
   202  	c.Code = code
   203  	c.CodeHash = hash
   204  	c.CodeAddr = addr
   205  }
   206  
   207  // SetCodeOptionalHash can be used to provide code, but it's optional to provide hash.
   208  // In case hash is not provided, the jumpdest analysis will not be saved to the parent context
   209  func (c *Contract) SetCodeOptionalHash(addr *common.Address, codeAndHash *codeAndHash) {
   210  	c.Code = codeAndHash.code
   211  	c.CodeHash = codeAndHash.hash
   212  	c.CodeAddr = addr
   213  }