github.com/cryptotooltop/go-ethereum@v0.0.0-20231103184714-151d1922f3e5/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/holiman/uint256" 24 25 "github.com/scroll-tech/go-ethereum/params" 26 ) 27 28 var activators = map[int]func(*JumpTable){ 29 3855: enable3855, 30 3860: enable3860, 31 3529: enable3529, 32 3198: enable3198, 33 2929: enable2929, 34 2200: enable2200, 35 1884: enable1884, 36 1344: enable1344, 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 // SELFDESTRUCT is disabled in Scroll 148 // jt[SELFDESTRUCT].constantGas = params.SelfdestructGasEIP150 149 // jt[SELFDESTRUCT].dynamicGas = gasSelfdestructEIP2929 150 } 151 152 // enable3529 enabled "EIP-3529: Reduction in refunds": 153 // - Removes refunds for selfdestructs 154 // - Reduces refunds for SSTORE 155 // - Reduces max refunds to 20% gas 156 func enable3529(jt *JumpTable) { 157 jt[SSTORE].dynamicGas = gasSStoreEIP3529 158 159 // SELFDESTRUCT is disabled in Scroll 160 // jt[SELFDESTRUCT].dynamicGas = gasSelfdestructEIP3529 161 } 162 163 // enable3198 applies EIP-3198 (BASEFEE Opcode) 164 // - Adds an opcode that returns the current block's base fee. 165 func enable3198(jt *JumpTable) { 166 // New opcode 167 jt[BASEFEE] = &operation{ 168 execute: opBaseFee, 169 constantGas: GasQuickStep, 170 minStack: minStack(0, 1), 171 maxStack: maxStack(0, 1), 172 } 173 } 174 175 // opBaseFee implements BASEFEE opcode 176 func opBaseFee(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 177 baseFee, _ := uint256.FromBig(interpreter.evm.Context.BaseFee) 178 scope.Stack.push(baseFee) 179 return nil, nil 180 } 181 182 // enable3855 applies EIP-3855 (PUSH0 opcode) 183 func enable3855(jt *JumpTable) { 184 // New opcode 185 jt[PUSH0] = &operation{ 186 execute: opPush0, 187 constantGas: GasQuickStep, 188 minStack: minStack(0, 1), 189 maxStack: maxStack(0, 1), 190 } 191 } 192 193 // opPush0 implements the PUSH0 opcode 194 func opPush0(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { 195 scope.Stack.push(new(uint256.Int)) 196 return nil, nil 197 } 198 199 // ebnable3860 enables "EIP-3860: Limit and meter initcode" 200 // https://eips.ethereum.org/EIPS/eip-3860 201 func enable3860(jt *JumpTable) { 202 jt[CREATE].dynamicGas = gasCreateEip3860 203 jt[CREATE2].dynamicGas = gasCreate2Eip3860 204 }