github.com/quinndk/ethereum_read@v0.0.0-20181211143958-29c55eec3237/go-ethereum-master_read/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/ethereum/go-ethereum/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 an ethereum contract in the state database. It contains
    43  // the the contract code, calling arguments. Contract implements ContractRef
    44  // 数据库中的以太坊智能合约,包括合约代码和调用参数
    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  	// 合约调用者
    50  	CallerAddress common.Address
    51  	caller        ContractRef
    52  	self          ContractRef
    53  
    54  	// JUMPDEST分析的结果
    55  	jumpdests destinations // result of JUMPDEST analysis.
    56  
    57  	// 合约代码
    58  	Code     []byte
    59  	CodeHash common.Hash
    60  	// 合约地址
    61  	CodeAddr *common.Address
    62  	Input    []byte
    63  
    64  	Gas   uint64
    65  	value *big.Int
    66  
    67  	Args []byte
    68  
    69  	// 是否委托调用
    70  	DelegateCall bool
    71  }
    72  
    73  // NewContract returns a new contract environment for the execution of EVM.
    74  // 为EVM创建合约环境
    75  func NewContract(caller ContractRef, object ContractRef, value *big.Int, gas uint64) *Contract {
    76  	c := &Contract{CallerAddress: caller.Address(), caller: caller, self: object, Args: nil}
    77  
    78  	if parent, ok := caller.(*Contract); ok {
    79  		// Reuse JUMPDEST analysis from parent context if available.
    80  		c.jumpdests = parent.jumpdests
    81  	} else {
    82  		c.jumpdests = make(destinations)
    83  	}
    84  
    85  	// Gas should be a pointer so it can safely be reduced through the run
    86  	// This pointer will be off the state transition
    87  	c.Gas = gas
    88  	// ensures a value is set
    89  	c.value = value
    90  
    91  	return c
    92  }
    93  
    94  // AsDelegate sets the contract to be a delegate call and returns the current
    95  // contract (for chaining calls)
    96  func (c *Contract) AsDelegate() *Contract {
    97  	c.DelegateCall = true
    98  	// NOTE: caller must, at all times be a contract. It should never happen
    99  	// that caller is something other than a Contract.
   100  	parent := c.caller.(*Contract)
   101  	c.CallerAddress = parent.CallerAddress
   102  	c.value = parent.value
   103  
   104  	return c
   105  }
   106  
   107  // GetOp returns the n'th element in the contract's byte array
   108  func (c *Contract) GetOp(n uint64) OpCode {
   109  	return OpCode(c.GetByte(n))
   110  }
   111  
   112  // GetByte returns the n'th byte in the contract's byte array
   113  func (c *Contract) GetByte(n uint64) byte {
   114  	if n < uint64(len(c.Code)) {
   115  		return c.Code[n]
   116  	}
   117  
   118  	return 0
   119  }
   120  
   121  // Caller returns the caller of the contract.
   122  //
   123  // Caller will recursively call caller when the contract is a delegate
   124  // call, including that of caller's caller.
   125  func (c *Contract) Caller() common.Address {
   126  	return c.CallerAddress
   127  }
   128  
   129  // UseGas attempts the use gas and subtracts it and returns true on success
   130  func (c *Contract) UseGas(gas uint64) (ok bool) {
   131  	if c.Gas < gas {
   132  		return false
   133  	}
   134  	c.Gas -= gas
   135  	return true
   136  }
   137  
   138  // Address returns the contracts address
   139  func (c *Contract) Address() common.Address {
   140  	return c.self.Address()
   141  }
   142  
   143  // Value returns the contracts value (sent to it from it's caller)
   144  func (c *Contract) Value() *big.Int {
   145  	return c.value
   146  }
   147  
   148  // SetCode sets the code to the contract
   149  func (c *Contract) SetCode(hash common.Hash, code []byte) {
   150  	c.Code = code
   151  	c.CodeHash = hash
   152  }
   153  
   154  // SetCallCode sets the code of the contract and address of the backing data
   155  // object
   156  func (c *Contract) SetCallCode(addr *common.Address, hash common.Hash, code []byte) {
   157  	c.Code = code
   158  	c.CodeHash = hash
   159  	c.CodeAddr = addr
   160  }