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