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