github.com/aaa256/atlantis@v0.0.0-20210707112435-42ee889287a2/core/vm/contract.go (about)

     1  // Copyright 2015 The go-athereum Authors
     2  // This file is part of the go-athereum library.
     3  //
     4  // The go-athereum 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-athereum 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-athereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package vm
    18  
    19  import (
    20  	"math/big"
    21  
    22  	"github.com/athereum/go-athereum/common"
    23  )
    24  
    25  // ContractRef is a reference to the contract's backing object
    26  type ContractRef interface {
    27  	Address() common.Address
    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  
    42  // Contract represents anatlantis contract in the state database. It contains
    43  // the the contract code, calling arguments. Contract implements ContractRef
    44  type Contract struct {
    45  	// CallerAddress is the result of the caller which initialised this
    46  	// contract. However when the "call method" is delegated this value
    47  	// needs to be initialised to that of the caller's caller.
    48  	CallerAddress common.Address
    49  	caller        ContractRef
    50  	self          ContractRef
    51  
    52  	jumpdests destinations // result of JUMPDEST analysis.
    53  
    54  	Code     []byte
    55  	CodeHash common.Hash
    56  	CodeAddr *common.Address
    57  	Input    []byte
    58  
    59  	Gas   uint64
    60  	value *big.Int
    61  
    62  	Args []byte
    63  
    64  	DelegateCall bool
    65  }
    66  
    67  // NewContract returns a new contract environment for the execution of EVM.
    68  func NewContract(caller ContractRef, object ContractRef, value *big.Int, gas uint64) *Contract {
    69  	c := &Contract{CallerAddress: caller.Address(), caller: caller, self: object, Args: nil}
    70  
    71  	if parent, ok := caller.(*Contract); ok {
    72  		// Reuse JUMPDEST analysis from parent context if available.
    73  		c.jumpdests = parent.jumpdests
    74  	} else {
    75  		c.jumpdests = make(destinations)
    76  	}
    77  
    78  	// Gas should be a pointer so it can safely be reduced through the run
    79  	// This pointer will be off the state transition
    80  	c.Gas = gas
    81  	// ensures a value is set
    82  	c.value = value
    83  
    84  	return c
    85  }
    86  
    87  // AsDelegate sets the contract to be a delegate call and returns the current
    88  // contract (for chaining calls)
    89  func (c *Contract) AsDelegate() *Contract {
    90  	c.DelegateCall = true
    91  	// NOTE: caller must, at all times be a contract. It should never happen
    92  	// that caller is somathing other than a Contract.
    93  	parent := c.caller.(*Contract)
    94  	c.CallerAddress = parent.CallerAddress
    95  	c.value = parent.value
    96  
    97  	return c
    98  }
    99  
   100  // GetOp returns the n'th element in the contract's byte array
   101  func (c *Contract) GetOp(n uint64) OpCode {
   102  	return OpCode(c.GetByte(n))
   103  }
   104  
   105  // GetByte returns the n'th byte in the contract's byte array
   106  func (c *Contract) GetByte(n uint64) byte {
   107  	if n < uint64(len(c.Code)) {
   108  		return c.Code[n]
   109  	}
   110  
   111  	return 0
   112  }
   113  
   114  // Caller returns the caller of the contract.
   115  //
   116  // Caller will recursively call caller when the contract is a delegate
   117  // call, including that of caller's caller.
   118  func (c *Contract) Caller() common.Address {
   119  	return c.CallerAddress
   120  }
   121  
   122  // UseGas attempts the use gas and subtracts it and returns true on success
   123  func (c *Contract) UseGas(gas uint64) (ok bool) {
   124  	if c.Gas < gas {
   125  		return false
   126  	}
   127  	c.Gas -= gas
   128  	return true
   129  }
   130  
   131  // Address returns the contracts address
   132  func (c *Contract) Address() common.Address {
   133  	return c.self.Address()
   134  }
   135  
   136  // Value returns the contracts value (sent to it from it's caller)
   137  func (c *Contract) Value() *big.Int {
   138  	return c.value
   139  }
   140  
   141  // SetCode sets the code to the contract
   142  func (c *Contract) SetCode(hash common.Hash, code []byte) {
   143  	c.Code = code
   144  	c.CodeHash = hash
   145  }
   146  
   147  // SetCallCode sets the code of the contract and address of the backing data
   148  // object
   149  func (c *Contract) SetCallCode(addr *common.Address, hash common.Hash, code []byte) {
   150  	c.Code = code
   151  	c.CodeHash = hash
   152  	c.CodeAddr = addr
   153  }