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