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