github.com/amazechain/amc@v0.1.3/internal/vm/contract.go (about) 1 // Copyright 2023 The AmazeChain Authors 2 // This file is part of the AmazeChain library. 3 // 4 // The AmazeChain 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 AmazeChain 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 AmazeChain library. If not, see <http://www.gnu.org/licenses/>. 16 17 package vm 18 19 import ( 20 "github.com/amazechain/amc/common/types" 21 "github.com/holiman/uint256" 22 ) 23 24 // ContractRef is a reference to the contract's backing object 25 type ContractRef interface { 26 Address() types.Address 27 } 28 29 // AccountRef implements ContractRef. 30 // 31 // Account references are used during EVM initialisation and 32 // it's primary use is to fetch addresses. Removing this object 33 // proves difficult because of the cached jump destinations which 34 // are fetched from the parent contract (i.e. the caller), which 35 // is a ContractRef. 36 type AccountRef types.Address 37 38 // Address casts AccountRef to a Address 39 func (ar AccountRef) Address() types.Address { return (types.Address)(ar) } 40 41 // Contract represents an ethereum contract in the state database. It contains 42 // the contract code, calling arguments. Contract implements ContractRef 43 type Contract struct { 44 // CallerAddress is the result of the caller which initialised this 45 // contract. However when the "call method" is delegated this value 46 // needs to be initialised to that of the caller's caller. 47 CallerAddress types.Address 48 caller ContractRef 49 self ContractRef 50 jumpdests map[types.Hash][]uint64 // Aggregated result of JUMPDEST analysis. 51 analysis []uint64 // Locally cached result of JUMPDEST analysis 52 skipAnalysis bool 53 54 Code []byte 55 CodeHash types.Hash 56 CodeAddr *types.Address 57 Input []byte 58 59 Gas uint64 60 value *uint256.Int 61 } 62 63 // NewContract returns a new contract environment for the execution of EVM. 64 func NewContract(caller ContractRef, object ContractRef, value *uint256.Int, gas uint64, skipAnalysis bool) *Contract { 65 c := &Contract{CallerAddress: caller.Address(), caller: caller, self: object} 66 67 if parent, ok := caller.(*Contract); ok { 68 // Reuse JUMPDEST analysis from parent context if available. 69 c.jumpdests = parent.jumpdests 70 } else { 71 c.jumpdests = make(map[types.Hash][]uint64) 72 } 73 74 // Gas should be a pointer so it can safely be reduced through the run 75 // This pointer will be off the state transition 76 c.Gas = gas 77 // ensures a value is set 78 c.value = value 79 80 c.skipAnalysis = skipAnalysis 81 82 return c 83 } 84 85 // First result tells us if the destination is valid 86 // Second result tells us if the code bitmap was used 87 func (c *Contract) validJumpdest(dest *uint256.Int) (bool, bool) { 88 udest, overflow := dest.Uint64WithOverflow() 89 // PC cannot go beyond len(code) and certainly can't be bigger than 64bits. 90 // Don't bother checking for JUMPDEST in that case. 91 if overflow || udest >= uint64(len(c.Code)) { 92 return false, false 93 } 94 // Only JUMPDESTs allowed for destinations 95 if OpCode(c.Code[udest]) != JUMPDEST { 96 return false, false 97 } 98 if c.skipAnalysis { 99 return true, false 100 } 101 return c.isCode(udest), true 102 } 103 104 func isCodeFromAnalysis(analysis []uint64, udest uint64) bool { 105 return analysis[udest/64]&(uint64(1)<<(udest&63)) == 0 106 } 107 108 // isCode returns true if the provided PC location is an actual opcode, as 109 // opposed to a data-segment following a PUSHN operation. 110 func (c *Contract) isCode(udest uint64) bool { 111 // Do we have a contract hash already? 112 // If we do have a hash, that means it's a 'regular' contract. For regular 113 // contracts ( not temporary initcode), we store the analysis in a map 114 if c.CodeHash != (types.Hash{}) { 115 // Does parent context have the analysis? 116 analysis, exist := c.jumpdests[c.CodeHash] 117 if !exist { 118 // Do the analysis and save in parent context 119 // We do not need to store it in c.analysis 120 analysis = codeBitmap(c.Code) 121 c.jumpdests[c.CodeHash] = analysis 122 } 123 // Also stash it in current contract for faster access 124 c.analysis = analysis 125 return isCodeFromAnalysis(analysis, udest) 126 } 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 136 return isCodeFromAnalysis(c.analysis, 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 if n < uint64(len(c.Code)) { 154 return OpCode(c.Code[n]) 155 } 156 157 return STOP 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() types.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() types.Address { 179 return c.self.Address() 180 } 181 182 // Value returns the contract's value (sent to it from it's caller) 183 func (c *Contract) Value() *uint256.Int { 184 return c.value 185 } 186 187 // SetCallCode sets the code of the contract and address of the backing data 188 // object 189 func (c *Contract) SetCallCode(addr *types.Address, hash types.Hash, code []byte) { 190 c.Code = code 191 c.CodeHash = hash 192 c.CodeAddr = addr 193 } 194 195 // SetCodeOptionalHash can be used to provide code, but it's optional to provide hash. 196 // In case hash is not provided, the jumpdest analysis will not be saved to the parent context 197 func (c *Contract) SetCodeOptionalHash(addr *types.Address, codeAndHash *codeAndHash) { 198 c.Code = codeAndHash.code 199 c.CodeHash = codeAndHash.hash 200 c.CodeAddr = addr 201 }