github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/core/vm/eips.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-aigar library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 package vm 19 20 import ( 21 "fmt" 22 23 "github.com/AigarNetwork/aigar/params" 24 ) 25 26 // EnableEIP enables the given EIP on the config. 27 // This operation writes in-place, and callers need to ensure that the globally 28 // defined jump tables are not polluted. 29 func EnableEIP(eipNum int, jt *JumpTable) error { 30 switch eipNum { 31 case 2200: 32 enable2200(jt) 33 case 1884: 34 enable1884(jt) 35 case 1344: 36 enable1344(jt) 37 default: 38 return fmt.Errorf("undefined eip %d", eipNum) 39 } 40 return nil 41 } 42 43 // enable1884 applies EIP-1884 to the given jump table: 44 // - Increase cost of BALANCE to 700 45 // - Increase cost of EXTCODEHASH to 700 46 // - Increase cost of SLOAD to 800 47 // - Define SELFBALANCE, with cost GasFastStep (5) 48 func enable1884(jt *JumpTable) { 49 // Gas cost changes 50 jt[BALANCE].constantGas = params.BalanceGasEIP1884 51 jt[EXTCODEHASH].constantGas = params.ExtcodeHashGasEIP1884 52 jt[SLOAD].constantGas = params.SloadGasEIP1884 53 54 // New opcode 55 jt[SELFBALANCE] = operation{ 56 execute: opSelfBalance, 57 constantGas: GasFastStep, 58 minStack: minStack(0, 1), 59 maxStack: maxStack(0, 1), 60 valid: true, 61 } 62 } 63 64 func opSelfBalance(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 65 balance := interpreter.intPool.get().Set(interpreter.evm.StateDB.GetBalance(contract.Address())) 66 stack.push(balance) 67 return nil, nil 68 } 69 70 // enable1344 applies EIP-1344 (ChainID Opcode) 71 // - Adds an opcode that returns the current chain’s EIP-155 unique identifier 72 func enable1344(jt *JumpTable) { 73 // New opcode 74 jt[CHAINID] = operation{ 75 execute: opChainID, 76 constantGas: GasQuickStep, 77 minStack: minStack(0, 1), 78 maxStack: maxStack(0, 1), 79 valid: true, 80 } 81 } 82 83 // opChainID implements CHAINID opcode 84 func opChainID(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { 85 chainId := interpreter.intPool.get().Set(interpreter.evm.chainConfig.ChainID) 86 stack.push(chainId) 87 return nil, nil 88 } 89 90 // enable2200 applies EIP-2200 (Rebalance net-metered SSTORE) 91 func enable2200(jt *JumpTable) { 92 jt[SSTORE].dynamicGas = gasSStoreEIP2200 93 }