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