github.com/fff-chain/go-fff@v0.0.0-20220726032732-1c84420b8a99/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 lru "github.com/hashicorp/golang-lru" 23 24 "github.com/fff-chain/go-fff/common" 25 "github.com/fff-chain/go-fff/metrics" 26 "github.com/holiman/uint256" 27 ) 28 29 const codeBitmapCacheSize = 2000 30 31 var ( 32 codeBitmapCache, _ = lru.New(codeBitmapCacheSize) 33 34 contractCodeBitmapHitMeter = metrics.NewRegisteredMeter("vm/contract/code/bitmap/hit", nil) 35 contractCodeBitmapMissMeter = metrics.NewRegisteredMeter("vm/contract/code/bitmap/miss", nil) 36 ) 37 38 // ContractRef is a reference to the contract's backing object 39 type ContractRef interface { 40 Address() common.Address 41 } 42 43 // AccountRef implements ContractRef. 44 // 45 // Account references are used during EVM initialisation and 46 // it's primary use is to fetch addresses. Removing this object 47 // proves difficult because of the cached jump destinations which 48 // are fetched from the parent contract (i.e. the caller), which 49 // is a ContractRef. 50 type AccountRef common.Address 51 52 // Address casts AccountRef to a Address 53 func (ar AccountRef) Address() common.Address { return (common.Address)(ar) } 54 55 // Contract represents an ethereum contract in the state database. It contains 56 // the contract code, calling arguments. Contract implements ContractRef 57 type Contract struct { 58 // CallerAddress is the result of the caller which initialised this 59 // contract. However when the "call method" is delegated this value 60 // needs to be initialised to that of the caller's caller. 61 CallerAddress common.Address 62 caller ContractRef 63 self ContractRef 64 65 jumpdests map[common.Hash]bitvec // Aggregated result of JUMPDEST analysis. 66 analysis bitvec // Locally cached result of JUMPDEST analysis 67 68 Code []byte 69 CodeHash common.Hash 70 CodeAddr *common.Address 71 Input []byte 72 73 Gas uint64 74 value *big.Int 75 } 76 77 // NewContract returns a new contract environment for the execution of EVM. 78 func NewContract(caller ContractRef, object ContractRef, value *big.Int, gas uint64) *Contract { 79 c := &Contract{CallerAddress: caller.Address(), caller: caller, self: object} 80 81 if parent, ok := caller.(*Contract); ok { 82 // Reuse JUMPDEST analysis from parent context if available. 83 c.jumpdests = parent.jumpdests 84 } else { 85 c.jumpdests = make(map[common.Hash]bitvec) 86 } 87 88 // Gas should be a pointer so it can safely be reduced through the run 89 // This pointer will be off the state transition 90 c.Gas = gas 91 // ensures a value is set 92 c.value = value 93 94 return c 95 } 96 97 func (c *Contract) validJumpdest(dest *uint256.Int) bool { 98 udest, overflow := dest.Uint64WithOverflow() 99 // PC cannot go beyond len(code) and certainly can't be bigger than 63bits. 100 // Don't bother checking for JUMPDEST in that case. 101 if overflow || udest >= uint64(len(c.Code)) { 102 return false 103 } 104 // Only JUMPDESTs allowed for destinations 105 if OpCode(c.Code[udest]) != JUMPDEST { 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 already have an analysis laying around? 115 if c.analysis != nil { 116 return c.analysis.codeSegment(udest) 117 } 118 // Do we have a contract hash already? 119 // If we do have a hash, that means it's a 'regular' contract. For regular 120 // contracts ( not temporary initcode), we store the analysis in a map 121 if c.CodeHash != (common.Hash{}) { 122 // Does parent context have the analysis? 123 analysis, exist := c.jumpdests[c.CodeHash] 124 if !exist { 125 if cached, ok := codeBitmapCache.Get(c.CodeHash); ok { 126 contractCodeBitmapHitMeter.Mark(1) 127 analysis = cached.(bitvec) 128 } else { 129 // Do the analysis and save in parent context 130 // We do not need to store it in c.analysis 131 analysis = codeBitmap(c.Code) 132 c.jumpdests[c.CodeHash] = analysis 133 contractCodeBitmapMissMeter.Mark(1) 134 codeBitmapCache.Add(c.CodeHash, analysis) 135 } 136 } 137 // Also stash it in current contract for faster access 138 c.analysis = analysis 139 return analysis.codeSegment(udest) 140 } 141 // We don't have the code hash, most likely a piece of initcode not already 142 // in state trie. In that case, we do an analysis, and save it locally, so 143 // we don't have to recalculate it for every JUMP instruction in the execution 144 // However, we don't save it within the parent context 145 if c.analysis == nil { 146 c.analysis = codeBitmap(c.Code) 147 } 148 return c.analysis.codeSegment(udest) 149 } 150 151 // AsDelegate sets the contract to be a delegate call and returns the current 152 // contract (for chaining calls) 153 func (c *Contract) AsDelegate() *Contract { 154 // NOTE: caller must, at all times be a contract. It should never happen 155 // that caller is something other than a Contract. 156 parent := c.caller.(*Contract) 157 c.CallerAddress = parent.CallerAddress 158 c.value = parent.value 159 160 return c 161 } 162 163 // GetOp returns the n'th element in the contract's byte array 164 func (c *Contract) GetOp(n uint64) OpCode { 165 return OpCode(c.GetByte(n)) 166 } 167 168 // GetByte returns the n'th byte in the contract's byte array 169 func (c *Contract) GetByte(n uint64) byte { 170 if n < uint64(len(c.Code)) { 171 return c.Code[n] 172 } 173 174 return 0 175 } 176 177 // Caller returns the caller of the contract. 178 // 179 // Caller will recursively call caller when the contract is a delegate 180 // call, including that of caller's caller. 181 func (c *Contract) Caller() common.Address { 182 return c.CallerAddress 183 } 184 185 // UseGas attempts the use gas and subtracts it and returns true on success 186 func (c *Contract) UseGas(gas uint64) (ok bool) { 187 if c.Gas < gas { 188 return false 189 } 190 c.Gas -= gas 191 return true 192 } 193 194 // Address returns the contracts address 195 func (c *Contract) Address() common.Address { 196 return c.self.Address() 197 } 198 199 // Value returns the contract's value (sent to it from it's caller) 200 func (c *Contract) Value() *big.Int { 201 return c.value 202 } 203 204 // SetCallCode sets the code of the contract and address of the backing data 205 // object 206 func (c *Contract) SetCallCode(addr *common.Address, hash common.Hash, code []byte) { 207 c.Code = code 208 c.CodeHash = hash 209 c.CodeAddr = addr 210 } 211 212 // SetCodeOptionalHash can be used to provide code, but it's optional to provide hash. 213 // In case hash is not provided, the jumpdest analysis will not be saved to the parent context 214 func (c *Contract) SetCodeOptionalHash(addr *common.Address, codeAndHash *codeAndHash) { 215 c.Code = codeAndHash.code 216 c.CodeHash = codeAndHash.hash 217 c.CodeAddr = addr 218 }