github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/core/vm/contract.go (about) 1 // This file is part of the go-sberex library. The go-sberex library is 2 // free software: you can redistribute it and/or modify it under the terms 3 // of the GNU Lesser General Public License as published by the Free 4 // Software Foundation, either version 3 of the License, or (at your option) 5 // any later version. 6 // 7 // The go-sberex library is distributed in the hope that it will be useful, 8 // but WITHOUT ANY WARRANTY; without even the implied warranty of 9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 10 // General Public License <http://www.gnu.org/licenses/> for more details. 11 12 package vm 13 14 import ( 15 "math/big" 16 17 "github.com/Sberex/go-sberex/common" 18 ) 19 20 // ContractRef is a reference to the contract's backing object 21 type ContractRef interface { 22 Address() common.Address 23 } 24 25 // AccountRef implements ContractRef. 26 // 27 // Account references are used during EVM initialisation and 28 // it's primary use is to fetch addresses. Removing this object 29 // proves difficult because of the cached jump destinations which 30 // are fetched from the parent contract (i.e. the caller), which 31 // is a ContractRef. 32 type AccountRef common.Address 33 34 // Address casts AccountRef to a Address 35 func (ar AccountRef) Address() common.Address { return (common.Address)(ar) } 36 37 // Contract represents an sberex contract in the state database. It contains 38 // the the contract code, calling arguments. Contract implements ContractRef 39 type Contract struct { 40 // CallerAddress is the result of the caller which initialised this 41 // contract. However when the "call method" is delegated this value 42 // needs to be initialised to that of the caller's caller. 43 CallerAddress common.Address 44 caller ContractRef 45 self ContractRef 46 47 jumpdests destinations // result of JUMPDEST analysis. 48 49 Code []byte 50 CodeHash common.Hash 51 CodeAddr *common.Address 52 Input []byte 53 54 Gas uint64 55 value *big.Int 56 57 Args []byte 58 59 DelegateCall bool 60 } 61 62 // NewContract returns a new contract environment for the execution of EVM. 63 func NewContract(caller ContractRef, object ContractRef, value *big.Int, gas uint64) *Contract { 64 c := &Contract{CallerAddress: caller.Address(), caller: caller, self: object, Args: nil} 65 66 if parent, ok := caller.(*Contract); ok { 67 // Reuse JUMPDEST analysis from parent context if available. 68 c.jumpdests = parent.jumpdests 69 } else { 70 c.jumpdests = make(destinations) 71 } 72 73 // Gas should be a pointer so it can safely be reduced through the run 74 // This pointer will be off the state transition 75 c.Gas = gas 76 // ensures a value is set 77 c.value = value 78 79 return c 80 } 81 82 // AsDelegate sets the contract to be a delegate call and returns the current 83 // contract (for chaining calls) 84 func (c *Contract) AsDelegate() *Contract { 85 c.DelegateCall = true 86 // NOTE: caller must, at all times be a contract. It should never happen 87 // that caller is something other than a Contract. 88 parent := c.caller.(*Contract) 89 c.CallerAddress = parent.CallerAddress 90 c.value = parent.value 91 92 return c 93 } 94 95 // GetOp returns the n'th element in the contract's byte array 96 func (c *Contract) GetOp(n uint64) OpCode { 97 return OpCode(c.GetByte(n)) 98 } 99 100 // GetByte returns the n'th byte in the contract's byte array 101 func (c *Contract) GetByte(n uint64) byte { 102 if n < uint64(len(c.Code)) { 103 return c.Code[n] 104 } 105 106 return 0 107 } 108 109 // Caller returns the caller of the contract. 110 // 111 // Caller will recursively call caller when the contract is a delegate 112 // call, including that of caller's caller. 113 func (c *Contract) Caller() common.Address { 114 return c.CallerAddress 115 } 116 117 // UseGas attempts the use gas and subtracts it and returns true on success 118 func (c *Contract) UseGas(gas uint64) (ok bool) { 119 if c.Gas < gas { 120 return false 121 } 122 c.Gas -= gas 123 return true 124 } 125 126 // Address returns the contracts address 127 func (c *Contract) Address() common.Address { 128 return c.self.Address() 129 } 130 131 // Value returns the contracts value (sent to it from it's caller) 132 func (c *Contract) Value() *big.Int { 133 return c.value 134 } 135 136 // SetCode sets the code to the contract 137 func (self *Contract) SetCode(hash common.Hash, code []byte) { 138 self.Code = code 139 self.CodeHash = hash 140 } 141 142 // SetCallCode sets the code of the contract and address of the backing data 143 // object 144 func (self *Contract) SetCallCode(addr *common.Address, hash common.Hash, code []byte) { 145 self.Code = code 146 self.CodeHash = hash 147 self.CodeAddr = addr 148 }