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