github.com/digdeepmining/go-atheios@v1.5.13-0.20180902133602-d5687a2e6f43/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/atheioschain/go-atheios/common" 23 ) 24 25 // ContractRef is a reference to the contract's backing object 26 type ContractRef interface { 27 ReturnGas(*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 *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 *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 c.UsedGas = new(big.Int) 74 75 return c 76 } 77 78 // AsDelegate sets the contract to be a delegate call and returns the current 79 // contract (for chaining calls) 80 func (c *Contract) AsDelegate() *Contract { 81 c.DelegateCall = true 82 // NOTE: caller must, at all times be a contract. It should never happen 83 // that caller is something other than a Contract. 84 c.CallerAddress = c.caller.(*Contract).CallerAddress 85 return c 86 } 87 88 // GetOp returns the n'th element in the contract's byte array 89 func (c *Contract) GetOp(n uint64) OpCode { 90 return OpCode(c.GetByte(n)) 91 } 92 93 // GetByte returns the n'th byte in the contract's byte array 94 func (c *Contract) GetByte(n uint64) byte { 95 if n < uint64(len(c.Code)) { 96 return c.Code[n] 97 } 98 99 return 0 100 } 101 102 // Caller returns the caller of the contract. 103 // 104 // Caller will recursively call caller when the contract is a delegate 105 // call, including that of caller's caller. 106 func (c *Contract) Caller() common.Address { 107 return c.CallerAddress 108 } 109 110 // Finalise finalises the contract and returning any remaining gas to the original 111 // caller. 112 func (c *Contract) Finalise() { 113 // Return the remaining gas to the caller 114 c.caller.ReturnGas(c.Gas) 115 } 116 117 // UseGas attempts the use gas and subtracts it and returns true on success 118 func (c *Contract) UseGas(gas *big.Int) (ok bool) { 119 ok = useGas(c.Gas, gas) 120 if ok { 121 c.UsedGas.Add(c.UsedGas, gas) 122 } 123 return 124 } 125 126 // ReturnGas adds the given gas back to itself. 127 func (c *Contract) ReturnGas(gas *big.Int) { 128 // Return the gas to the context 129 c.Gas.Add(c.Gas, gas) 130 c.UsedGas.Sub(c.UsedGas, gas) 131 } 132 133 // Address returns the contracts address 134 func (c *Contract) Address() common.Address { 135 return c.self.Address() 136 } 137 138 // Value returns the contracts value (sent to it from it's caller) 139 func (c *Contract) Value() *big.Int { 140 return c.value 141 } 142 143 // SetCode sets the code to the contract 144 func (self *Contract) SetCode(hash common.Hash, code []byte) { 145 self.Code = code 146 self.CodeHash = hash 147 } 148 149 // SetCallCode sets the code of the contract and address of the backing data 150 // object 151 func (self *Contract) SetCallCode(addr *common.Address, hash common.Hash, code []byte) { 152 self.Code = code 153 self.CodeHash = hash 154 self.CodeAddr = addr 155 } 156 157 // EachStorage iterates the contract's storage and calls a method for every key 158 // value pair. 159 func (self *Contract) ForEachStorage(cb func(key, value common.Hash) bool) { 160 self.caller.ForEachStorage(cb) 161 }