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