github.com/dominant-strategies/go-quai@v0.28.2/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/dominant-strategies/go-quai/common" 23 "github.com/holiman/uint256" 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 Quai contract in the state database. It contains 44 // 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 map[common.Hash]bitvec // Aggregated result of JUMPDEST analysis. 54 analysis bitvec // Locally cached result of JUMPDEST analysis 55 56 Code []byte 57 CodeHash common.Hash 58 CodeAddr *common.Address 59 Input []byte 60 61 Gas uint64 62 value *big.Int 63 } 64 65 // NewContract returns a new contract environment for the execution of EVM. 66 func NewContract(caller ContractRef, object ContractRef, value *big.Int, gas uint64) *Contract { 67 c := &Contract{CallerAddress: caller.Address(), caller: caller, self: object} 68 69 if parent, ok := caller.(*Contract); ok { 70 // Reuse JUMPDEST analysis from parent context if available. 71 c.jumpdests = parent.jumpdests 72 } else { 73 c.jumpdests = make(map[common.Hash]bitvec) 74 } 75 76 // Gas should be a pointer so it can safely be reduced through the run 77 // This pointer will be off the state transition 78 c.Gas = gas 79 // ensures a value is set 80 c.value = value 81 82 return c 83 } 84 85 func (c *Contract) validJumpdest(dest *uint256.Int) bool { 86 udest, overflow := dest.Uint64WithOverflow() 87 // PC cannot go beyond len(code) and certainly can't be bigger than 63bits. 88 // Don't bother checking for JUMPDEST in that case. 89 if overflow || udest >= uint64(len(c.Code)) { 90 return false 91 } 92 // Only JUMPDESTs allowed for destinations 93 if OpCode(c.Code[udest]) != JUMPDEST { 94 return false 95 } 96 return c.isCode(udest) 97 } 98 99 // isCode returns true if the provided PC location is an actual opcode, as 100 // opposed to a data-segment following a PUSHN operation. 101 func (c *Contract) isCode(udest uint64) bool { 102 // Do we already have an analysis laying around? 103 if c.analysis != nil { 104 return c.analysis.codeSegment(udest) 105 } 106 // Do we have a contract hash already? 107 // If we do have a hash, that means it's a 'regular' contract. For regular 108 // contracts ( not temporary initcode), we store the analysis in a map 109 if c.CodeHash != (common.Hash{}) { 110 // Does parent context have the analysis? 111 analysis, exist := c.jumpdests[c.CodeHash] 112 if !exist { 113 // Do the analysis and save in parent context 114 // We do not need to store it in c.analysis 115 analysis = codeBitmap(c.Code) 116 c.jumpdests[c.CodeHash] = analysis 117 } 118 // Also stash it in current contract for faster access 119 c.analysis = analysis 120 return analysis.codeSegment(udest) 121 } 122 // We don't have the code hash, most likely a piece of initcode not already 123 // in state trie. In that case, we do an analysis, and save it locally, so 124 // we don't have to recalculate it for every JUMP instruction in the execution 125 // However, we don't save it within the parent context 126 if c.analysis == nil { 127 c.analysis = codeBitmap(c.Code) 128 } 129 return c.analysis.codeSegment(udest) 130 } 131 132 // AsDelegate sets the contract to be a delegate call and returns the current 133 // contract (for chaining calls) 134 func (c *Contract) AsDelegate() *Contract { 135 // NOTE: caller must, at all times be a contract. It should never happen 136 // that caller is something other than a Contract. 137 parent := c.caller.(*Contract) 138 c.CallerAddress = parent.CallerAddress 139 c.value = parent.value 140 141 return c 142 } 143 144 // GetOp returns the n'th element in the contract's byte array 145 func (c *Contract) GetOp(n uint64) OpCode { 146 return OpCode(c.GetByte(n)) 147 } 148 149 // GetByte returns the n'th byte in the contract's byte array 150 func (c *Contract) GetByte(n uint64) byte { 151 if n < uint64(len(c.Code)) { 152 return c.Code[n] 153 } 154 155 return 0 156 } 157 158 // Caller returns the caller of the contract. 159 // 160 // Caller will recursively call caller when the contract is a delegate 161 // call, including that of caller's caller. 162 func (c *Contract) Caller() common.Address { 163 return c.CallerAddress 164 } 165 166 // UseGas attempts the use gas and subtracts it and returns true on success 167 func (c *Contract) UseGas(gas uint64) (ok bool) { 168 if c.Gas < gas { 169 return false 170 } 171 c.Gas -= gas 172 return true 173 } 174 175 // Address returns the contracts address 176 func (c *Contract) Address() common.Address { 177 return c.self.Address() 178 } 179 180 // Value returns the contract's value (sent to it from it's caller) 181 func (c *Contract) Value() *big.Int { 182 return c.value 183 } 184 185 // SetCallCode sets the code of the contract and address of the backing data 186 // object 187 func (c *Contract) SetCallCode(addr *common.Address, hash common.Hash, code []byte) { 188 c.Code = code 189 c.CodeHash = hash 190 c.CodeAddr = addr 191 } 192 193 // SetCodeOptionalHash can be used to provide code, but it's optional to provide hash. 194 // In case hash is not provided, the jumpdest analysis will not be saved to the parent context 195 func (c *Contract) SetCodeOptionalHash(addr *common.Address, codeAndHash *codeAndHash) { 196 c.Code = codeAndHash.code 197 c.CodeHash = codeAndHash.hash 198 c.CodeAddr = addr 199 }