github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/core/vm/contract.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar 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-aigar 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-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package vm
    19  
    20  import (
    21  	"math/big"
    22  
    23  	"github.com/AigarNetwork/aigar/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 *big.Int) bool {
    86  	udest := dest.Uint64()
    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 dest.BitLen() >= 63 || 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  	// Do we have a contract hash already?
    97  	if c.CodeHash != (common.Hash{}) {
    98  		// Does parent context have the analysis?
    99  		analysis, exist := c.jumpdests[c.CodeHash]
   100  		if !exist {
   101  			// Do the analysis and save in parent context
   102  			// We do not need to store it in c.analysis
   103  			analysis = codeBitmap(c.Code)
   104  			c.jumpdests[c.CodeHash] = analysis
   105  		}
   106  		return analysis.codeSegment(udest)
   107  	}
   108  	// We don't have the code hash, most likely a piece of initcode not already
   109  	// in state trie. In that case, we do an analysis, and save it locally, so
   110  	// we don't have to recalculate it for every JUMP instruction in the execution
   111  	// However, we don't save it within the parent context
   112  	if c.analysis == nil {
   113  		c.analysis = codeBitmap(c.Code)
   114  	}
   115  	return c.analysis.codeSegment(udest)
   116  }
   117  
   118  // AsDelegate sets the contract to be a delegate call and returns the current
   119  // contract (for chaining calls)
   120  func (c *Contract) AsDelegate() *Contract {
   121  	// NOTE: caller must, at all times be a contract. It should never happen
   122  	// that caller is something other than a Contract.
   123  	parent := c.caller.(*Contract)
   124  	c.CallerAddress = parent.CallerAddress
   125  	c.value = parent.value
   126  
   127  	return c
   128  }
   129  
   130  // GetOp returns the n'th element in the contract's byte array
   131  func (c *Contract) GetOp(n uint64) OpCode {
   132  	return OpCode(c.GetByte(n))
   133  }
   134  
   135  // GetByte returns the n'th byte in the contract's byte array
   136  func (c *Contract) GetByte(n uint64) byte {
   137  	if n < uint64(len(c.Code)) {
   138  		return c.Code[n]
   139  	}
   140  
   141  	return 0
   142  }
   143  
   144  // Caller returns the caller of the contract.
   145  //
   146  // Caller will recursively call caller when the contract is a delegate
   147  // call, including that of caller's caller.
   148  func (c *Contract) Caller() common.Address {
   149  	return c.CallerAddress
   150  }
   151  
   152  // UseGas attempts the use gas and subtracts it and returns true on success
   153  func (c *Contract) UseGas(gas uint64) (ok bool) {
   154  	if c.Gas < gas {
   155  		return false
   156  	}
   157  	c.Gas -= gas
   158  	return true
   159  }
   160  
   161  // Address returns the contracts address
   162  func (c *Contract) Address() common.Address {
   163  	return c.self.Address()
   164  }
   165  
   166  // Value returns the contract's value (sent to it from it's caller)
   167  func (c *Contract) Value() *big.Int {
   168  	return c.value
   169  }
   170  
   171  // SetCallCode sets the code of the contract and address of the backing data
   172  // object
   173  func (c *Contract) SetCallCode(addr *common.Address, hash common.Hash, code []byte) {
   174  	c.Code = code
   175  	c.CodeHash = hash
   176  	c.CodeAddr = addr
   177  }
   178  
   179  // SetCodeOptionalHash can be used to provide code, but it's optional to provide hash.
   180  // In case hash is not provided, the jumpdest analysis will not be saved to the parent context
   181  func (c *Contract) SetCodeOptionalHash(addr *common.Address, codeAndHash *codeAndHash) {
   182  	c.Code = codeAndHash.code
   183  	c.CodeHash = codeAndHash.hash
   184  	c.CodeAddr = addr
   185  }