github.com/cheng762/platon-go@v1.8.17-0.20190529111256-7deff2d7be26/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/PlatONnetwork/PlatON-Go/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 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 ABI []byte 60 ABIHash common.Hash 61 ABIAddr *common.Address 62 63 Gas uint64 64 value *big.Int 65 66 Args []byte 67 68 DelegateCall bool 69 } 70 71 // NewContract returns a new contract environment for the execution of EVM. 72 func NewContract(caller ContractRef, object ContractRef, value *big.Int, gas uint64) *Contract { 73 c := &Contract{CallerAddress: caller.Address(), caller: caller, self: object, Args: nil} 74 75 if parent, ok := caller.(*Contract); ok { 76 // Reuse JUMPDEST analysis from parent context if available. 77 c.jumpdests = parent.jumpdests 78 } else { 79 c.jumpdests = make(destinations) 80 } 81 82 // Gas should be a pointer so it can safely be reduced through the run 83 // This pointer will be off the state transition 84 c.Gas = gas 85 // ensures a value is set 86 c.value = value 87 88 return c 89 } 90 91 // AsDelegate sets the contract to be a delegate call and returns the current 92 // contract (for chaining calls) 93 func (c *Contract) AsDelegate() *Contract { 94 c.DelegateCall = true 95 // NOTE: caller must, at all times be a contract. It should never happen 96 // that caller is something other than a Contract. 97 parent := c.caller.(*Contract) 98 c.CallerAddress = parent.CallerAddress 99 c.value = parent.value 100 101 return c 102 } 103 104 // GetOp returns the n'th element in the contract's byte array 105 func (c *Contract) GetOp(n uint64) OpCode { 106 return OpCode(c.GetByte(n)) 107 } 108 109 // GetByte returns the n'th byte in the contract's byte array 110 func (c *Contract) GetByte(n uint64) byte { 111 if n < uint64(len(c.Code)) { 112 return c.Code[n] 113 } 114 115 return 0 116 } 117 118 // Caller returns the caller of the contract. 119 // 120 // Caller will recursively call caller when the contract is a delegate 121 // call, including that of caller's caller. 122 func (c *Contract) Caller() common.Address { 123 return c.CallerAddress 124 } 125 126 // UseGas attempts the use gas and subtracts it and returns true on success 127 func (c *Contract) UseGas(gas uint64) (ok bool) { 128 if c.Gas < gas { 129 return false 130 } 131 c.Gas -= gas 132 return true 133 } 134 135 // Address returns the contracts address 136 func (c *Contract) Address() common.Address { 137 return c.self.Address() 138 } 139 140 // Value returns the contracts value (sent to it from it's caller) 141 func (c *Contract) Value() *big.Int { 142 return c.value 143 } 144 145 // SetCode sets the code to the contract 146 func (c *Contract) SetCode(hash common.Hash, code []byte) { 147 c.Code = code 148 c.CodeHash = hash 149 } 150 151 // SetCallCode sets the code of the contract and address of the backing data 152 // object 153 func (c *Contract) SetCallCode(addr *common.Address, hash common.Hash, code []byte) { 154 c.Code = code 155 c.CodeHash = hash 156 c.CodeAddr = addr 157 } 158 159 // todo: 160 func (c *Contract) SetCallAbi(addr *common.Address, hash common.Hash, abi []byte) { 161 c.ABI = abi 162 c.ABIHash = hash 163 c.ABIAddr = addr 164 }