github.com/r8d8/go-ethereum@v5.5.2+incompatible/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/ethereumproject/go-ethereum/common" 23 ) 24 25 // ContractRef is a reference to the contract's backing object 26 type ContractRef interface { 27 ReturnGas(*big.Int, *big.Int) 28 Address() common.Address 29 Value() *big.Int 30 SetCode(common.Hash, []byte) 31 ForEachStorage(callback func(key, value common.Hash) bool) 32 } 33 34 // Contract represents an ethereum contract in the state database. It contains 35 // the the contract code, calling arguments. Contract implements ContractRef 36 type Contract struct { 37 // CallerAddress is the result of the caller which initialised this 38 // contract. However when the "call method" is delegated this value 39 // needs to be initialised to that of the caller's caller. 40 CallerAddress common.Address 41 caller ContractRef 42 self ContractRef 43 44 jumpdests destinations // result of JUMPDEST analysis. 45 46 Code []byte 47 CodeHash common.Hash 48 CodeAddr *common.Address 49 Input []byte 50 51 value, Gas, UsedGas, Price *big.Int 52 53 Args []byte 54 55 DelegateCall bool 56 } 57 58 // NewContract returns a new contract environment for the execution of EVM. 59 func NewContract(caller ContractRef, object ContractRef, value, gas, price *big.Int) *Contract { 60 c := &Contract{CallerAddress: caller.Address(), caller: caller, self: object, Args: nil} 61 62 if parent, ok := caller.(*Contract); ok { 63 // Reuse JUMPDEST analysis from parent context if available. 64 c.jumpdests = parent.jumpdests 65 } else { 66 c.jumpdests = make(destinations) 67 } 68 69 // Gas should be a pointer so it can safely be reduced through the run 70 // This pointer will be off the state transition 71 c.Gas = gas //new(big.Int).Set(gas) 72 c.value = new(big.Int).Set(value) 73 // In most cases price and value are pointers to transaction objects 74 // and we don't want the transaction's values to change. 75 c.Price = new(big.Int).Set(price) 76 c.UsedGas = new(big.Int) 77 78 return c 79 } 80 81 // AsDelegate sets the contract to be a delegate call and returns the current 82 // contract (for chaining calls) 83 func (c *Contract) AsDelegate() *Contract { 84 c.DelegateCall = true 85 // NOTE: caller must, at all times be a contract. It should never happen 86 // that caller is something other than a Contract. 87 c.CallerAddress = c.caller.(*Contract).CallerAddress 88 return c 89 } 90 91 // GetOp returns the n'th element in the contract's byte array 92 func (c *Contract) GetOp(n uint64) OpCode { 93 return OpCode(c.GetByte(n)) 94 } 95 96 // GetByte returns the n'th byte in the contract's byte array 97 func (c *Contract) GetByte(n uint64) byte { 98 if n < uint64(len(c.Code)) { 99 return c.Code[n] 100 } 101 102 return 0 103 } 104 105 // Caller returns the caller of the contract. 106 // 107 // Caller will recursively call caller when the contract is a delegate 108 // call, including that of caller's caller. 109 func (c *Contract) Caller() common.Address { 110 return c.CallerAddress 111 } 112 113 // Finalise finalises the contract and returning any remaining gas to the original 114 // caller. 115 func (c *Contract) Finalise() { 116 // Return the remaining gas to the caller 117 c.caller.ReturnGas(c.Gas, c.Price) 118 } 119 120 // UseGas attempts the use gas and subtracts it and returns true on success 121 func (c *Contract) UseGas(gas *big.Int) (ok bool) { 122 ok = useGas(c.Gas, gas) 123 if ok { 124 c.UsedGas.Add(c.UsedGas, gas) 125 } 126 return 127 } 128 129 // ReturnGas adds the given gas back to itself. 130 func (c *Contract) ReturnGas(gas, price *big.Int) { 131 // Return the gas to the context 132 c.Gas.Add(c.Gas, gas) 133 c.UsedGas.Sub(c.UsedGas, gas) 134 } 135 136 // Address returns the contracts address 137 func (c *Contract) Address() common.Address { 138 return c.self.Address() 139 } 140 141 // Value returns the contracts value (sent to it from it's caller) 142 func (c *Contract) Value() *big.Int { 143 return c.value 144 } 145 146 // SetCode sets the code to the contract 147 func (self *Contract) SetCode(hash common.Hash, code []byte) { 148 self.Code = code 149 self.CodeHash = hash 150 } 151 152 // SetCallCode sets the code of the contract and address of the backing data 153 // object 154 func (self *Contract) SetCallCode(addr *common.Address, hash common.Hash, code []byte) { 155 self.Code = code 156 self.CodeHash = hash 157 self.CodeAddr = addr 158 } 159 160 // EachStorage iterates the contract's storage and calls a method for every key 161 // value pair. 162 func (self *Contract) ForEachStorage(cb func(key, value common.Hash) bool) { 163 self.caller.ForEachStorage(cb) 164 }