github.com/klaytn/klaytn@v1.10.2/blockchain/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  
    22  	"github.com/klaytn/klaytn/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 4399:
    31  		enable4399(jt)
    32  	case 3529:
    33  		enable3529(jt)
    34  	case 2929:
    35  		enable2929(jt)
    36  	case 2200:
    37  		enable2200(jt)
    38  	case 1884:
    39  		enable1884(jt)
    40  	case 1344:
    41  		enable1344(jt)
    42  	default:
    43  		return fmt.Errorf("undefined eip %d", eipNum)
    44  	}
    45  	return nil
    46  }
    47  
    48  // enable1884 applies EIP-1884 to the given jump table:
    49  // - Increase cost of BALANCE to 700
    50  // - Increase cost of EXTCODEHASH to 700
    51  // - Increase cost of SLOAD to 800
    52  // - Define SELFBALANCE, with cost GasFastStep (5)
    53  func enable1884(jt *JumpTable) {
    54  	// Gas cost changes
    55  	jt[SLOAD].constantGas = params.SloadGasEIP1884
    56  	jt[BALANCE].constantGas = params.BalanceGasEIP1884
    57  	jt[EXTCODEHASH].constantGas = params.ExtcodeHashGasEIP1884
    58  
    59  	// New opcode
    60  	jt[SELFBALANCE] = &operation{
    61  		execute:         opSelfBalance,
    62  		constantGas:     GasFastStep,
    63  		minStack:        minStack(0, 1),
    64  		maxStack:        maxStack(0, 1),
    65  		computationCost: params.SelfBalanceComputationCost,
    66  	}
    67  }
    68  
    69  func opSelfBalance(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
    70  	balance := evm.interpreter.intPool.get().Set(evm.StateDB.GetBalance(contract.Address()))
    71  	stack.push(balance)
    72  	return nil, nil
    73  }
    74  
    75  // enable1344 applies EIP-1344 (ChainID Opcode)
    76  // - Adds an opcode that returns the current chain’s EIP-155 unique identifier
    77  func enable1344(jt *JumpTable) {
    78  	// New opcode
    79  	jt[CHAINID] = &operation{
    80  		execute:         opChainID,
    81  		constantGas:     GasQuickStep,
    82  		minStack:        minStack(0, 1),
    83  		maxStack:        maxStack(0, 1),
    84  		computationCost: params.ChainIDComputationCost,
    85  	}
    86  }
    87  
    88  // opChainID implements CHAINID opcode
    89  func opChainID(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
    90  	chainId := evm.interpreter.intPool.get().Set(evm.chainConfig.ChainID)
    91  	stack.push(chainId)
    92  	return nil, nil
    93  }
    94  
    95  // enable2200 applies EIP-2200 (Rebalance net-metered SSTORE)
    96  func enable2200(jt *JumpTable) {
    97  	jt[SLOAD].constantGas = params.SloadGasEIP2200
    98  	jt[SSTORE].dynamicGas = gasSStoreEIP2200
    99  }
   100  
   101  // enableIstanbulComputationCostModification modifies ADDMOD, MULMOD, NOT, XOR, SHL, SHR, SAR computation cost
   102  // The modification is activated with istanbulCompatible change activation.
   103  func enableIstanbulComputationCostModification(jt *JumpTable) {
   104  	jt[ADDMOD].computationCost = params.AddmodComputationCostIstanbul
   105  	jt[MULMOD].computationCost = params.MulmodComputationCostIstanbul
   106  	jt[NOT].computationCost = params.NotComputationCostIstanbul
   107  	jt[XOR].computationCost = params.XorComputationCostIstanbul
   108  	jt[SHL].computationCost = params.ShlComputationCostIstanbul
   109  	jt[SHR].computationCost = params.ShrComputationCostIstanbul
   110  	jt[SAR].computationCost = params.SarComputationCostIstanbul
   111  }
   112  
   113  // enable3198 applies EIP-3198 (BASEFEE Opcode)
   114  // - Adds an opcode that returns the current block's base fee.
   115  func enable3198(jt *JumpTable) {
   116  	// New opcode
   117  	jt[BASEFEE] = &operation{
   118  		execute:         opBaseFee,
   119  		constantGas:     GasQuickStep,
   120  		minStack:        minStack(0, 1),
   121  		maxStack:        maxStack(0, 1),
   122  		computationCost: params.BaseFeeComputationCost,
   123  	}
   124  }
   125  
   126  // opBaseFee implements BASEFEE opcode
   127  func opBaseFee(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
   128  	baseFee := evm.interpreter.intPool.get().Set(evm.Context.BaseFee)
   129  	stack.push(baseFee)
   130  	return nil, nil
   131  }
   132  
   133  // enable2929 enables "EIP-2929: Gas cost increases for state access opcodes"
   134  // https://eips.ethereum.org/EIPS/eip-2929
   135  func enable2929(jt *JumpTable) {
   136  	jt[SSTORE].dynamicGas = gasSStoreEIP2929
   137  
   138  	jt[SLOAD].constantGas = 0
   139  	jt[SLOAD].dynamicGas = gasSLoadEIP2929
   140  
   141  	jt[EXTCODECOPY].constantGas = params.WarmStorageReadCostEIP2929
   142  	jt[EXTCODECOPY].dynamicGas = gasExtCodeCopyEIP2929
   143  
   144  	jt[EXTCODESIZE].constantGas = params.WarmStorageReadCostEIP2929
   145  	jt[EXTCODESIZE].dynamicGas = gasEip2929AccountCheck
   146  
   147  	jt[EXTCODEHASH].constantGas = params.WarmStorageReadCostEIP2929
   148  	jt[EXTCODEHASH].dynamicGas = gasEip2929AccountCheck
   149  
   150  	jt[BALANCE].constantGas = params.WarmStorageReadCostEIP2929
   151  	jt[BALANCE].dynamicGas = gasEip2929AccountCheck
   152  
   153  	jt[CALL].constantGas = params.WarmStorageReadCostEIP2929
   154  	jt[CALL].dynamicGas = gasCallEIP2929
   155  
   156  	jt[CALLCODE].constantGas = params.WarmStorageReadCostEIP2929
   157  	jt[CALLCODE].dynamicGas = gasCallCodeEIP2929
   158  
   159  	jt[STATICCALL].constantGas = params.WarmStorageReadCostEIP2929
   160  	jt[STATICCALL].dynamicGas = gasStaticCallEIP2929
   161  
   162  	jt[DELEGATECALL].constantGas = params.WarmStorageReadCostEIP2929
   163  	jt[DELEGATECALL].dynamicGas = gasDelegateCallEIP2929
   164  
   165  	// This was previously part of the dynamic cost, but we're using it as a constantGas
   166  	// factor here
   167  	jt[SELFDESTRUCT].constantGas = params.SelfdestructGas
   168  	jt[SELFDESTRUCT].dynamicGas = gasSelfdestructEIP2929
   169  }
   170  
   171  // enable3529 enabled "EIP-3529: Reduction in refunds":
   172  // - Removes refunds for selfdestructs
   173  // - Reduces refunds for SSTORE
   174  // - Reduces max refunds to 20% gas
   175  func enable3529(jt *JumpTable) {
   176  	jt[SSTORE].dynamicGas = gasSStoreEIP3529
   177  	jt[SELFDESTRUCT].dynamicGas = gasSelfdestructEIP3529
   178  }
   179  
   180  // enable4399 applies EIP-4399 (PREVRANDAO Opcode)
   181  // - Change the 0x44 opcode from returning difficulty value to returning prev blockhash value
   182  func enable4399(jt *JumpTable) {
   183  	jt[PREVRANDAO] = &operation{
   184  		execute:         opRandom,
   185  		constantGas:     GasQuickStep,
   186  		minStack:        minStack(0, 1),
   187  		maxStack:        maxStack(0, 1),
   188  		computationCost: params.RandomComputationCost,
   189  	}
   190  }