github.com/bigzoro/my_simplechain@v0.0.0-20240315012955-8ad0a2a29bb9/core/vm/eips.go (about) 1 // Copyright 2019 The go-simplechain Authors 2 // This file is part of the go-simplechain library. 3 // 4 // The go-simplechain 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-simplechain 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-simplechain library. If not, see <http://www.gnu.org/licenses/>. 16 17 package vm 18 19 import ( 20 "fmt" 21 22 "github.com/bigzoro/my_simplechain/params" 23 ) 24 25 // EnableEIP enables the given EIP on the config. 26 // This operation writes in-place, and callers need to ensure that the globally 27 // defined jump tables are not polluted. 28 func EnableEIP(eipNum int, jt *JumpTable) error { 29 switch eipNum { 30 case 2200: 31 enable2200(jt) 32 case 1884: 33 enable1884(jt) 34 case 1344: 35 enable1344(jt) 36 default: 37 return fmt.Errorf("undefined eip %d", eipNum) 38 } 39 return nil 40 } 41 42 // enable1884 applies EIP-1884 to the given jump table: 43 // - Increase cost of BALANCE to 700 44 // - Increase cost of EXTCODEHASH to 700 45 // - Increase cost of SLOAD to 800 46 // - Define SELFBALANCE, with cost GasFastStep (5) 47 func enable1884(jt *JumpTable) { 48 // Gas cost changes 49 jt[BALANCE].constantGas = params.BalanceGasEIP1884 50 jt[EXTCODEHASH].constantGas = params.ExtcodeHashGasEIP1884 51 jt[SLOAD].constantGas = params.SloadGasEIP1884 52 53 // New opcode 54 jt[SELFBALANCE] = operation{ 55 execute: opSelfBalance, 56 constantGas: GasFastStep, 57 minStack: minStack(0, 1), 58 maxStack: maxStack(0, 1), 59 valid: true, 60 } 61 } 62 63 func enableNonce(jt *JumpTable) { 64 jt[NONCE] = operation{ 65 execute: opNonce, 66 constantGas: GasQuickStep, 67 minStack: minStack(0, 1), 68 maxStack: maxStack(0, 1), 69 valid: true, 70 } 71 } 72 73 func opNonce(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 74 stack.push(interpreter.intPool.get().Set(interpreter.evm.Nonce)) 75 return nil, nil 76 } 77 78 func opSelfBalance(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 79 balance := interpreter.intPool.get().Set(interpreter.evm.StateDB.GetBalance(contract.Address())) 80 stack.push(balance) 81 return nil, nil 82 } 83 84 // enable1344 applies EIP-1344 (ChainID Opcode) 85 // - Adds an opcode that returns the current chain’s EIP-155 unique identifier 86 func enable1344(jt *JumpTable) { 87 // New opcode 88 jt[CHAINID] = operation{ 89 execute: opChainID, 90 constantGas: GasQuickStep, 91 minStack: minStack(0, 1), 92 maxStack: maxStack(0, 1), 93 valid: true, 94 } 95 } 96 97 // opChainID implements CHAINID opcode 98 func opChainID(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 99 chainId := interpreter.intPool.get().Set(interpreter.evm.chainConfig.ChainID) 100 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[SSTORE].dynamicGas = gasSStoreEIP2200 107 }