github.com/jimmyx0x/go-ethereum@v1.10.28/core/vm/eips.go (about) 1 // Copyright 2019 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 "fmt" 21 "sort" 22 23 "github.com/ethereum/go-ethereum/common" 24 "github.com/ethereum/go-ethereum/params" 25 "github.com/holiman/uint256" 26 ) 27 28 var activators = map[int]func(*JumpTable){ 29 3855: enable3855, 30 3529: enable3529, 31 3198: enable3198, 32 2929: enable2929, 33 2200: enable2200, 34 1884: enable1884, 35 1344: enable1344, 36 1153: enable1153, 37 } 38 39 // EnableEIP enables the given EIP on the config. 40 // This operation writes in-place, and callers need to ensure that the globally 41 // defined jump tables are not polluted. 42 func EnableEIP(eipNum int, jt *JumpTable) error { 43 enablerFn, ok := activators[eipNum] 44 if !ok { 45 return fmt.Errorf("undefined eip %d", eipNum) 46 } 47 enablerFn(jt) 48 return nil 49 } 50 51 func ValidEip(eipNum int) bool { 52 _, ok := activators[eipNum] 53 return ok 54 } 55 func ActivateableEips() []string { 56 var nums []string 57 for k := range activators { 58 nums = append(nums, fmt.Sprintf("%d", k)) 59 } 60 sort.Strings(nums) 61 return nums 62 } 63 64 // enable1884 applies EIP-1884 to the given jump table: 65 // - Increase cost of BALANCE to 700 66 // - Increase cost of EXTCODEHASH to 700 67 // - Increase cost of SLOAD to 800 68 // - Define SELFBALANCE, with cost GasFastStep (5) 69 func enable1884(jt *JumpTable) { 70 // Gas cost changes 71 jt[SLOAD].constantGas = params.SloadGasEIP1884 72 jt[BALANCE].constantGas = params.BalanceGasEIP1884 73 jt[EXTCODEHASH].constantGas = params.ExtcodeHashGasEIP1884 74 75 // New opcode 76 jt[SELFBALANCE] = &operation{ 77 execute: opSelfBalance, 78 constantGas: GasFastStep, 79 minStack: minStack(0, 1), 80 maxStack: maxStack(0, 1), 81 } 82 } 83 84 func opSelfBalance(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 85 balance, _ := uint256.FromBig(interpreter.evm.StateDB.GetBalance(scope.Contract.Address())) 86 scope.Stack.push(balance) 87 return nil, nil 88 } 89 90 // enable1344 applies EIP-1344 (ChainID Opcode) 91 // - Adds an opcode that returns the current chain’s EIP-155 unique identifier 92 func enable1344(jt *JumpTable) { 93 // New opcode 94 jt[CHAINID] = &operation{ 95 execute: opChainID, 96 constantGas: GasQuickStep, 97 minStack: minStack(0, 1), 98 maxStack: maxStack(0, 1), 99 } 100 } 101 102 // opChainID implements CHAINID opcode 103 func opChainID(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 104 chainId, _ := uint256.FromBig(interpreter.evm.chainConfig.ChainID) 105 scope.Stack.push(chainId) 106 return nil, nil 107 } 108 109 // enable2200 applies EIP-2200 (Rebalance net-metered SSTORE) 110 func enable2200(jt *JumpTable) { 111 jt[SLOAD].constantGas = params.SloadGasEIP2200 112 jt[SSTORE].dynamicGas = gasSStoreEIP2200 113 } 114 115 // enable2929 enables "EIP-2929: Gas cost increases for state access opcodes" 116 // https://eips.ethereum.org/EIPS/eip-2929 117 func enable2929(jt *JumpTable) { 118 jt[SSTORE].dynamicGas = gasSStoreEIP2929 119 120 jt[SLOAD].constantGas = 0 121 jt[SLOAD].dynamicGas = gasSLoadEIP2929 122 123 jt[EXTCODECOPY].constantGas = params.WarmStorageReadCostEIP2929 124 jt[EXTCODECOPY].dynamicGas = gasExtCodeCopyEIP2929 125 126 jt[EXTCODESIZE].constantGas = params.WarmStorageReadCostEIP2929 127 jt[EXTCODESIZE].dynamicGas = gasEip2929AccountCheck 128 129 jt[EXTCODEHASH].constantGas = params.WarmStorageReadCostEIP2929 130 jt[EXTCODEHASH].dynamicGas = gasEip2929AccountCheck 131 132 jt[BALANCE].constantGas = params.WarmStorageReadCostEIP2929 133 jt[BALANCE].dynamicGas = gasEip2929AccountCheck 134 135 jt[CALL].constantGas = params.WarmStorageReadCostEIP2929 136 jt[CALL].dynamicGas = gasCallEIP2929 137 138 jt[CALLCODE].constantGas = params.WarmStorageReadCostEIP2929 139 jt[CALLCODE].dynamicGas = gasCallCodeEIP2929 140 141 jt[STATICCALL].constantGas = params.WarmStorageReadCostEIP2929 142 jt[STATICCALL].dynamicGas = gasStaticCallEIP2929 143 144 jt[DELEGATECALL].constantGas = params.WarmStorageReadCostEIP2929 145 jt[DELEGATECALL].dynamicGas = gasDelegateCallEIP2929 146 147 // This was previously part of the dynamic cost, but we're using it as a constantGas 148 // factor here 149 jt[SELFDESTRUCT].constantGas = params.SelfdestructGasEIP150 150 jt[SELFDESTRUCT].dynamicGas = gasSelfdestructEIP2929 151 } 152 153 // enable3529 enabled "EIP-3529: Reduction in refunds": 154 // - Removes refunds for selfdestructs 155 // - Reduces refunds for SSTORE 156 // - Reduces max refunds to 20% gas 157 func enable3529(jt *JumpTable) { 158 jt[SSTORE].dynamicGas = gasSStoreEIP3529 159 jt[SELFDESTRUCT].dynamicGas = gasSelfdestructEIP3529 160 } 161 162 // enable3198 applies EIP-3198 (BASEFEE Opcode) 163 // - Adds an opcode that returns the current block's base fee. 164 func enable3198(jt *JumpTable) { 165 // New opcode 166 jt[BASEFEE] = &operation{ 167 execute: opBaseFee, 168 constantGas: GasQuickStep, 169 minStack: minStack(0, 1), 170 maxStack: maxStack(0, 1), 171 } 172 } 173 174 // enable1153 applies EIP-1153 "Transient Storage" 175 // - Adds TLOAD that reads from transient storage 176 // - Adds TSTORE that writes to transient storage 177 func enable1153(jt *JumpTable) { 178 jt[TLOAD] = &operation{ 179 execute: opTload, 180 constantGas: params.WarmStorageReadCostEIP2929, 181 minStack: minStack(1, 1), 182 maxStack: maxStack(1, 1), 183 } 184 185 jt[TSTORE] = &operation{ 186 execute: opTstore, 187 constantGas: params.WarmStorageReadCostEIP2929, 188 minStack: minStack(2, 0), 189 maxStack: maxStack(2, 0), 190 } 191 } 192 193 // opTload implements TLOAD opcode 194 func opTload(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 195 loc := scope.Stack.peek() 196 hash := common.Hash(loc.Bytes32()) 197 val := interpreter.evm.StateDB.GetTransientState(scope.Contract.Address(), hash) 198 loc.SetBytes(val.Bytes()) 199 return nil, nil 200 } 201 202 // opTstore implements TSTORE opcode 203 func opTstore(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 204 if interpreter.readOnly { 205 return nil, ErrWriteProtection 206 } 207 loc := scope.Stack.pop() 208 val := scope.Stack.pop() 209 interpreter.evm.StateDB.SetTransientState(scope.Contract.Address(), loc.Bytes32(), val.Bytes32()) 210 return nil, nil 211 } 212 213 // opBaseFee implements BASEFEE opcode 214 func opBaseFee(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 215 baseFee, _ := uint256.FromBig(interpreter.evm.Context.BaseFee) 216 scope.Stack.push(baseFee) 217 return nil, nil 218 } 219 220 // enable3855 applies EIP-3855 (PUSH0 opcode) 221 func enable3855(jt *JumpTable) { 222 // New opcode 223 jt[PUSH0] = &operation{ 224 execute: opPush0, 225 constantGas: GasQuickStep, 226 minStack: minStack(0, 1), 227 maxStack: maxStack(0, 1), 228 } 229 } 230 231 // opPush0 implements the PUSH0 opcode 232 func opPush0(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 233 scope.Stack.push(new(uint256.Int)) 234 return nil, nil 235 }