github.com/ccm-chain/ccmchain@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 "github.com/ccm-chain/ccmchain/params" 24 "github.com/holiman/uint256" 25 ) 26 27 var activators = map[int]func(*JumpTable){ 28 2200: enable2200, 29 1884: enable1884, 30 1344: enable1344, 31 2315: enable2315, 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, callContext *callCtx) ([]byte, error) { 80 balance, _ := uint256.FromBig(interpreter.evm.StateDB.GetBalance(callContext.contract.Address())) 81 callContext.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, callContext *callCtx) ([]byte, error) { 99 chainId, _ := uint256.FromBig(interpreter.evm.chainConfig.ChainID) 100 callContext.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 // enable2315 applies EIP-2315 (Simple Subroutines) 111 // - Adds opcodes that jump to and return from subroutines 112 func enable2315(jt *JumpTable) { 113 // New opcode 114 jt[BEGINSUB] = &operation{ 115 execute: opBeginSub, 116 constantGas: GasQuickStep, 117 minStack: minStack(0, 0), 118 maxStack: maxStack(0, 0), 119 } 120 // New opcode 121 jt[JUMPSUB] = &operation{ 122 execute: opJumpSub, 123 constantGas: GasSlowStep, 124 minStack: minStack(1, 0), 125 maxStack: maxStack(1, 0), 126 jumps: true, 127 } 128 // New opcode 129 jt[RETURNSUB] = &operation{ 130 execute: opReturnSub, 131 constantGas: GasFastStep, 132 minStack: minStack(0, 0), 133 maxStack: maxStack(0, 0), 134 jumps: true, 135 } 136 }