github.com/MetalBlockchain/subnet-evm@v0.4.9/core/vm/contract.go (about) 1 // (c) 2019-2020, Ava Labs, Inc. 2 // 3 // This file is a derived work, based on the go-ethereum library whose original 4 // notices appear below. 5 // 6 // It is distributed under a license compatible with the licensing terms of the 7 // original code from which it is derived. 8 // 9 // Much love to the original authors for their work. 10 // ********** 11 // Copyright 2015 The go-ethereum Authors 12 // This file is part of the go-ethereum library. 13 // 14 // The go-ethereum library is free software: you can redistribute it and/or modify 15 // it under the terms of the GNU Lesser General Public License as published by 16 // the Free Software Foundation, either version 3 of the License, or 17 // (at your option) any later version. 18 // 19 // The go-ethereum library is distributed in the hope that it will be useful, 20 // but WITHOUT ANY WARRANTY; without even the implied warranty of 21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 // GNU Lesser General Public License for more details. 23 // 24 // You should have received a copy of the GNU Lesser General Public License 25 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 26 27 package vm 28 29 import ( 30 "math/big" 31 32 "github.com/ethereum/go-ethereum/common" 33 "github.com/holiman/uint256" 34 ) 35 36 // ContractRef is a reference to the contract's backing object 37 type ContractRef interface { 38 Address() common.Address 39 } 40 41 // AccountRef implements ContractRef. 42 // 43 // Account references are used during EVM initialisation and 44 // it's primary use is to fetch addresses. Removing this object 45 // proves difficult because of the cached jump destinations which 46 // are fetched from the parent contract (i.e. the caller), which 47 // is a ContractRef. 48 type AccountRef common.Address 49 50 // Address casts AccountRef to a Address 51 func (ar AccountRef) Address() common.Address { return (common.Address)(ar) } 52 53 // Contract represents an ethereum contract in the state database. It contains 54 // the contract code, calling arguments. Contract implements ContractRef 55 type Contract struct { 56 // CallerAddress is the result of the caller which initialised this 57 // contract. However when the "call method" is delegated this value 58 // needs to be initialised to that of the caller's caller. 59 CallerAddress common.Address 60 caller ContractRef 61 self ContractRef 62 63 jumpdests map[common.Hash]bitvec // Aggregated result of JUMPDEST analysis. 64 analysis bitvec // Locally cached result of JUMPDEST analysis 65 66 Code []byte 67 CodeHash common.Hash 68 CodeAddr *common.Address 69 Input []byte 70 71 Gas uint64 72 value *big.Int 73 } 74 75 // NewContract returns a new contract environment for the execution of EVM. 76 func NewContract(caller ContractRef, object ContractRef, value *big.Int, gas uint64) *Contract { 77 c := &Contract{CallerAddress: caller.Address(), caller: caller, self: object} 78 79 if parent, ok := caller.(*Contract); ok { 80 // Reuse JUMPDEST analysis from parent context if available. 81 c.jumpdests = parent.jumpdests 82 } else { 83 c.jumpdests = make(map[common.Hash]bitvec) 84 } 85 86 // Gas should be a pointer so it can safely be reduced through the run 87 // This pointer will be off the state transition 88 c.Gas = gas 89 // ensures a value is set 90 c.value = value 91 92 return c 93 } 94 95 func (c *Contract) validJumpdest(dest *uint256.Int) bool { 96 udest, overflow := dest.Uint64WithOverflow() 97 // PC cannot go beyond len(code) and certainly can't be bigger than 63bits. 98 // Don't bother checking for JUMPDEST in that case. 99 if overflow || udest >= uint64(len(c.Code)) { 100 return false 101 } 102 // Only JUMPDESTs allowed for destinations 103 if OpCode(c.Code[udest]) != JUMPDEST { 104 return false 105 } 106 return c.isCode(udest) 107 } 108 109 // isCode returns true if the provided PC location is an actual opcode, as 110 // opposed to a data-segment following a PUSHN operation. 111 func (c *Contract) isCode(udest uint64) bool { 112 // Do we already have an analysis laying around? 113 if c.analysis != nil { 114 return c.analysis.codeSegment(udest) 115 } 116 // Do we have a contract hash already? 117 // If we do have a hash, that means it's a 'regular' contract. For regular 118 // contracts ( not temporary initcode), we store the analysis in a map 119 if c.CodeHash != (common.Hash{}) { 120 // Does parent context have the analysis? 121 analysis, exist := c.jumpdests[c.CodeHash] 122 if !exist { 123 // Do the analysis and save in parent context 124 // We do not need to store it in c.analysis 125 analysis = codeBitmap(c.Code) 126 c.jumpdests[c.CodeHash] = analysis 127 } 128 // Also stash it in current contract for faster access 129 c.analysis = analysis 130 return analysis.codeSegment(udest) 131 } 132 // We don't have the code hash, most likely a piece of initcode not already 133 // in state trie. In that case, we do an analysis, and save it locally, so 134 // we don't have to recalculate it for every JUMP instruction in the execution 135 // However, we don't save it within the parent context 136 if c.analysis == nil { 137 c.analysis = codeBitmap(c.Code) 138 } 139 return c.analysis.codeSegment(udest) 140 } 141 142 // AsDelegate sets the contract to be a delegate call and returns the current 143 // contract (for chaining calls) 144 func (c *Contract) AsDelegate() *Contract { 145 // NOTE: caller must, at all times be a contract. It should never happen 146 // that caller is something other than a Contract. 147 parent := c.caller.(*Contract) 148 c.CallerAddress = parent.CallerAddress 149 c.value = parent.value 150 151 return c 152 } 153 154 // GetOp returns the n'th element in the contract's byte array 155 func (c *Contract) GetOp(n uint64) OpCode { 156 if n < uint64(len(c.Code)) { 157 return OpCode(c.Code[n]) 158 } 159 160 return STOP 161 } 162 163 // Caller returns the caller of the contract. 164 // 165 // Caller will recursively call caller when the contract is a delegate 166 // call, including that of caller's caller. 167 func (c *Contract) Caller() common.Address { 168 return c.CallerAddress 169 } 170 171 // UseGas attempts the use gas and subtracts it and returns true on success 172 func (c *Contract) UseGas(gas uint64) (ok bool) { 173 if c.Gas < gas { 174 return false 175 } 176 c.Gas -= gas 177 return true 178 } 179 180 // Address returns the contracts address 181 func (c *Contract) Address() common.Address { 182 return c.self.Address() 183 } 184 185 // Value returns the contract's value (sent to it from it's caller) 186 func (c *Contract) Value() *big.Int { 187 return c.value 188 } 189 190 // SetCallCode sets the code of the contract and address of the backing data 191 // object 192 func (c *Contract) SetCallCode(addr *common.Address, hash common.Hash, code []byte) { 193 c.Code = code 194 c.CodeHash = hash 195 c.CodeAddr = addr 196 } 197 198 // SetCodeOptionalHash can be used to provide code, but it's optional to provide hash. 199 // In case hash is not provided, the jumpdest analysis will not be saved to the parent context 200 func (c *Contract) SetCodeOptionalHash(addr *common.Address, codeAndHash *codeAndHash) { 201 c.Code = codeAndHash.code 202 c.CodeHash = codeAndHash.hash 203 c.CodeAddr = addr 204 }