github.com/core-coin/go-core@v1.1.7/core/vm/contract.go (about) 1 // Copyright 2015 by the Authors 2 // This file is part of the go-core library. 3 // 4 // The go-core 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-core 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-core library. If not, see <http://www.gnu.org/licenses/>. 16 17 package vm 18 19 import ( 20 "github.com/core-coin/uint256" 21 "math/big" 22 23 "github.com/core-coin/go-core/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 CVM 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 core 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 Energy uint64 62 value *big.Int 63 } 64 65 // NewContract returns a new contract environment for the execution of CVM. 66 func NewContract(caller ContractRef, object ContractRef, value *big.Int, energy 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 // Energy should be a pointer so it can safely be reduced through the run 77 // This pointer will be off the state transition 78 c.Energy = energy 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 63 bits. 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 func (c *Contract) validJumpSubdest(udest uint64) bool { 100 // PC cannot go beyond len(code) and certainly can't be bigger than 63 bits. 101 // Don't bother checking for BEGINSUB in that case. 102 if int64(udest) < 0 || udest >= uint64(len(c.Code)) { 103 return false 104 } 105 // Only BEGINSUBs allowed for destinations 106 if OpCode(c.Code[udest]) != BEGINSUB { 107 return false 108 } 109 return c.isCode(udest) 110 } 111 112 // isCode returns true if the provided PC location is an actual opcode, as 113 // opposed to a data-segment following a PUSHN operation. 114 func (c *Contract) isCode(udest uint64) bool { 115 // Do we have a contract hash already? 116 if c.CodeHash != (common.Hash{}) { 117 // Does parent context have the analysis? 118 analysis, exist := c.jumpdests[c.CodeHash] 119 if !exist { 120 // Do the analysis and save in parent context 121 // We do not need to store it in c.analysis 122 analysis = codeBitmap(c.Code) 123 c.jumpdests[c.CodeHash] = analysis 124 } 125 // Also stash it in current contract for faster access 126 c.analysis = analysis 127 return analysis.codeSegment(udest) 128 } 129 // We don't have the code hash, most likely a piece of initcode not already 130 // in state trie. In that case, we do an analysis, and save it locally, so 131 // we don't have to recalculate it for every JUMP instruction in the execution 132 // However, we don't save it within the parent context 133 if c.analysis == nil { 134 c.analysis = codeBitmap(c.Code) 135 } 136 return c.analysis.codeSegment(udest) 137 } 138 139 // AsDelegate sets the contract to be a delegate call and returns the current 140 // contract (for chaining calls) 141 func (c *Contract) AsDelegate() *Contract { 142 // NOTE: caller must, at all times be a contract. It should never happen 143 // that caller is something other than a Contract. 144 parent := c.caller.(*Contract) 145 c.CallerAddress = parent.CallerAddress 146 c.value = parent.value 147 148 return c 149 } 150 151 // GetOp returns the n'th element in the contract's byte array 152 func (c *Contract) GetOp(n uint64) OpCode { 153 return OpCode(c.GetByte(n)) 154 } 155 156 // GetByte returns the n'th byte in the contract's byte array 157 func (c *Contract) GetByte(n uint64) byte { 158 if n < uint64(len(c.Code)) { 159 return c.Code[n] 160 } 161 162 return 0 163 } 164 165 // Caller returns the caller of the contract. 166 // 167 // Caller will recursively call caller when the contract is a delegate 168 // call, including that of caller's caller. 169 func (c *Contract) Caller() common.Address { 170 return c.CallerAddress 171 } 172 173 // UseEnergy attempts the use energy and subtracts it and returns true on success 174 func (c *Contract) UseEnergy(energy uint64) (ok bool) { 175 if c.Energy < energy { 176 return false 177 } 178 c.Energy -= energy 179 return true 180 } 181 182 // Address returns the contracts address 183 func (c *Contract) Address() common.Address { 184 return c.self.Address() 185 } 186 187 // Value returns the contract's value (sent to it from it's caller) 188 func (c *Contract) Value() *big.Int { 189 return c.value 190 } 191 192 // SetCallCode sets the code of the contract and address of the backing data 193 // object 194 func (c *Contract) SetCallCode(addr *common.Address, hash common.Hash, code []byte) { 195 c.Code = code 196 c.CodeHash = hash 197 c.CodeAddr = addr 198 } 199 200 // SetCodeOptionalHash can be used to provide code, but it's optional to provide hash. 201 // In case hash is not provided, the jumpdest analysis will not be saved to the parent context 202 func (c *Contract) SetCodeOptionalHash(addr *common.Address, codeAndHash *codeAndHash) { 203 c.Code = codeAndHash.code 204 c.CodeHash = codeAndHash.hash 205 c.CodeAddr = addr 206 }