github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/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/intfoundation/intchain/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 63bits. 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 // Do we have a contract hash already? 96 if c.CodeHash != (common.Hash{}) { 97 // Does parent context have the analysis? 98 analysis, exist := c.jumpdests[c.CodeHash] 99 if !exist { 100 // Do the analysis and save in parent context 101 // We do not need to store it in c.analysis 102 analysis = codeBitmap(c.Code) 103 c.jumpdests[c.CodeHash] = analysis 104 } 105 return analysis.codeSegment(udest) 106 } 107 // We don't have the code hash, most likely a piece of initcode not already 108 // in state trie. In that case, we do an analysis, and save it locally, so 109 // we don't have to recalculate it for every JUMP instruction in the execution 110 // However, we don't save it within the parent context 111 if c.analysis == nil { 112 c.analysis = codeBitmap(c.Code) 113 } 114 return c.analysis.codeSegment(udest) 115 } 116 117 // AsDelegate sets the contract to be a delegate call and returns the current 118 // contract (for chaining calls) 119 func (c *Contract) AsDelegate() *Contract { 120 // NOTE: caller must, at all times be a contract. It should never happen 121 // that caller is something other than a Contract. 122 parent := c.caller.(*Contract) 123 c.CallerAddress = parent.CallerAddress 124 c.value = parent.value 125 126 return c 127 } 128 129 // GetOp returns the n'th element in the contract's byte array 130 func (c *Contract) GetOp(n uint64) OpCode { 131 return OpCode(c.GetByte(n)) 132 } 133 134 // GetByte returns the n'th byte in the contract's byte array 135 func (c *Contract) GetByte(n uint64) byte { 136 if n < uint64(len(c.Code)) { 137 return c.Code[n] 138 } 139 140 return 0 141 } 142 143 // Caller returns the caller of the contract. 144 // 145 // Caller will recursively call caller when the contract is a delegate 146 // call, including that of caller's caller. 147 func (c *Contract) Caller() common.Address { 148 return c.CallerAddress 149 } 150 151 // UseGas attempts the use gas and subtracts it and returns true on success 152 func (c *Contract) UseGas(gas uint64) (ok bool) { 153 if c.Gas < gas { 154 return false 155 } 156 c.Gas -= gas 157 return true 158 } 159 160 // Address returns the contracts address 161 func (c *Contract) Address() common.Address { 162 return c.self.Address() 163 } 164 165 // Value returns the contract's value (sent to it from it's caller) 166 func (c *Contract) Value() *big.Int { 167 return c.value 168 } 169 170 // SetCallCode sets the code of the contract and address of the backing data 171 // object 172 func (c *Contract) SetCallCode(addr *common.Address, hash common.Hash, code []byte) { 173 c.Code = code 174 c.CodeHash = hash 175 c.CodeAddr = addr 176 } 177 178 // SetCodeOptionalHash can be used to provide code, but it's optional to provide hash. 179 // In case hash is not provided, the jumpdest analysis will not be saved to the parent context 180 func (c *Contract) SetCodeOptionalHash(addr *common.Address, codeAndHash *codeAndHash) { 181 c.Code = codeAndHash.code 182 c.CodeHash = codeAndHash.hash 183 c.CodeAddr = addr 184 }