github.com/zhiqiangxu/go-ethereum@v1.9.16-0.20210824055606-be91cfdebc48/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/zhiqiangxu/go-ethereum/common" 23 ) 24 25 // ContractRef is a reference to the contract's backing object 26 type ContractRef interface { 27 Address() common.Address 28 } 29 30 // AccountRef implements ContractRef. 31 // 32 // Account references are used during EVM initialisation and 33 // it's primary use is to fetch addresses. Removing this object 34 // proves difficult because of the cached jump destinations which 35 // are fetched from the parent contract (i.e. the caller), which 36 // is a ContractRef. 37 type AccountRef common.Address 38 39 // Address casts AccountRef to a Address 40 func (ar AccountRef) Address() common.Address { return (common.Address)(ar) } 41 42 // Contract represents an ethereum contract in the state database. It contains 43 // the contract code, calling arguments. Contract implements ContractRef 44 type Contract struct { 45 // CallerAddress is the result of the caller which initialised this 46 // contract. However when the "call method" is delegated this value 47 // needs to be initialised to that of the caller's caller. 48 CallerAddress common.Address 49 caller ContractRef 50 self ContractRef 51 52 jumpdests map[common.Hash]bitvec // Aggregated result of JUMPDEST analysis. 53 analysis bitvec // Locally cached result of JUMPDEST analysis 54 55 Code []byte 56 CodeHash common.Hash 57 CodeAddr *common.Address 58 Input []byte 59 60 Gas uint64 61 value *big.Int 62 } 63 64 // NewContract returns a new contract environment for the execution of EVM. 65 func NewContract(caller ContractRef, object ContractRef, value *big.Int, gas uint64) *Contract { 66 c := &Contract{CallerAddress: caller.Address(), caller: caller, self: object} 67 68 if parent, ok := caller.(*Contract); ok { 69 // Reuse JUMPDEST analysis from parent context if available. 70 c.jumpdests = parent.jumpdests 71 } else { 72 c.jumpdests = make(map[common.Hash]bitvec) 73 } 74 75 // Gas should be a pointer so it can safely be reduced through the run 76 // This pointer will be off the state transition 77 c.Gas = gas 78 // ensures a value is set 79 c.value = value 80 81 return c 82 } 83 84 func (c *Contract) validJumpdest(dest *big.Int) bool { 85 udest := dest.Uint64() 86 // PC cannot go beyond len(code) and certainly can't be bigger than 63 bits. 87 // Don't bother checking for JUMPDEST in that case. 88 if dest.BitLen() >= 63 || udest >= uint64(len(c.Code)) { 89 return false 90 } 91 // Only JUMPDESTs allowed for destinations 92 if OpCode(c.Code[udest]) != JUMPDEST { 93 return false 94 } 95 return c.isCode(udest) 96 } 97 98 func (c *Contract) validJumpSubdest(udest uint64) bool { 99 // PC cannot go beyond len(code) and certainly can't be bigger than 63 bits. 100 // Don't bother checking for BEGINSUB in that case. 101 if int64(udest) < 0 || udest >= uint64(len(c.Code)) { 102 return false 103 } 104 // Only BEGINSUBs allowed for destinations 105 if OpCode(c.Code[udest]) != BEGINSUB { 106 return false 107 } 108 return c.isCode(udest) 109 } 110 111 // isCode returns true if the provided PC location is an actual opcode, as 112 // opposed to a data-segment following a PUSHN operation. 113 func (c *Contract) isCode(udest uint64) bool { 114 // Do we have a contract hash already? 115 if c.CodeHash != (common.Hash{}) { 116 // Does parent context have the analysis? 117 analysis, exist := c.jumpdests[c.CodeHash] 118 if !exist { 119 // Do the analysis and save in parent context 120 // We do not need to store it in c.analysis 121 analysis = codeBitmap(c.Code) 122 c.jumpdests[c.CodeHash] = analysis 123 } 124 // Also stash it in current contract for faster access 125 c.analysis = analysis 126 return analysis.codeSegment(udest) 127 } 128 // We don't have the code hash, most likely a piece of initcode not already 129 // in state trie. In that case, we do an analysis, and save it locally, so 130 // we don't have to recalculate it for every JUMP instruction in the execution 131 // However, we don't save it within the parent context 132 if c.analysis == nil { 133 c.analysis = codeBitmap(c.Code) 134 } 135 return c.analysis.codeSegment(udest) 136 } 137 138 // AsDelegate sets the contract to be a delegate call and returns the current 139 // contract (for chaining calls) 140 func (c *Contract) AsDelegate() *Contract { 141 // NOTE: caller must, at all times be a contract. It should never happen 142 // that caller is something other than a Contract. 143 parent := c.caller.(*Contract) 144 c.CallerAddress = parent.CallerAddress 145 c.value = parent.value 146 147 return c 148 } 149 150 // GetOp returns the n'th element in the contract's byte array 151 func (c *Contract) GetOp(n uint64) OpCode { 152 return OpCode(c.GetByte(n)) 153 } 154 155 // GetByte returns the n'th byte in the contract's byte array 156 func (c *Contract) GetByte(n uint64) byte { 157 if n < uint64(len(c.Code)) { 158 return c.Code[n] 159 } 160 161 return 0 162 } 163 164 // Caller returns the caller of the contract. 165 // 166 // Caller will recursively call caller when the contract is a delegate 167 // call, including that of caller's caller. 168 func (c *Contract) Caller() common.Address { 169 return c.CallerAddress 170 } 171 172 // UseGas attempts the use gas and subtracts it and returns true on success 173 func (c *Contract) UseGas(gas uint64) (ok bool) { 174 if c.Gas < gas { 175 return false 176 } 177 c.Gas -= gas 178 return true 179 } 180 181 // Address returns the contracts address 182 func (c *Contract) Address() common.Address { 183 return c.self.Address() 184 } 185 186 // Value returns the contract's 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 }