github.com/ethereum-optimism/optimism/l2geth@v0.0.0-20230612200230-50b04ade19e3/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  
    22  	"github.com/ethereum-optimism/optimism/l2geth/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 opSelfBalance(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
    64  	balance := interpreter.intPool.get().Set(interpreter.evm.StateDB.GetBalance(contract.Address()))
    65  	stack.push(balance)
    66  	return nil, nil
    67  }
    68  
    69  // enable1344 applies EIP-1344 (ChainID Opcode)
    70  // - Adds an opcode that returns the current chain’s EIP-155 unique identifier
    71  func enable1344(jt *JumpTable) {
    72  	// New opcode
    73  	jt[CHAINID] = operation{
    74  		execute:     opChainID,
    75  		constantGas: GasQuickStep,
    76  		minStack:    minStack(0, 1),
    77  		maxStack:    maxStack(0, 1),
    78  		valid:       true,
    79  	}
    80  }
    81  
    82  // opChainID implements CHAINID opcode
    83  func opChainID(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
    84  	chainId := interpreter.intPool.get().Set(interpreter.evm.chainConfig.ChainID)
    85  	stack.push(chainId)
    86  	return nil, nil
    87  }
    88  
    89  // enable2200 applies EIP-2200 (Rebalance net-metered SSTORE)
    90  func enable2200(jt *JumpTable) {
    91  	jt[SSTORE].dynamicGas = gasSStoreEIP2200
    92  }
    93  
    94  // enable2929 enables "EIP-2929: Gas cost increases for state access opcodes"
    95  // https://eips.ethereum.org/EIPS/eip-2929
    96  func enable2929(jt *JumpTable) {
    97  	jt[SSTORE].dynamicGas = gasSStoreEIP2929
    98  
    99  	jt[SLOAD].constantGas = 0
   100  	jt[SLOAD].dynamicGas = gasSLoadEIP2929
   101  
   102  	jt[EXTCODECOPY].constantGas = params.WarmStorageReadCostEIP2929
   103  	jt[EXTCODECOPY].dynamicGas = gasExtCodeCopyEIP2929
   104  
   105  	jt[EXTCODESIZE].constantGas = params.WarmStorageReadCostEIP2929
   106  	jt[EXTCODESIZE].dynamicGas = gasEip2929AccountCheck
   107  
   108  	jt[EXTCODEHASH].constantGas = params.WarmStorageReadCostEIP2929
   109  	jt[EXTCODEHASH].dynamicGas = gasEip2929AccountCheck
   110  
   111  	jt[BALANCE].constantGas = params.WarmStorageReadCostEIP2929
   112  	jt[BALANCE].dynamicGas = gasEip2929AccountCheck
   113  
   114  	jt[CALL].constantGas = params.WarmStorageReadCostEIP2929
   115  	jt[CALL].dynamicGas = gasCallEIP2929
   116  
   117  	jt[CALLCODE].constantGas = params.WarmStorageReadCostEIP2929
   118  	jt[CALLCODE].dynamicGas = gasCallCodeEIP2929
   119  
   120  	jt[STATICCALL].constantGas = params.WarmStorageReadCostEIP2929
   121  	jt[STATICCALL].dynamicGas = gasStaticCallEIP2929
   122  
   123  	jt[DELEGATECALL].constantGas = params.WarmStorageReadCostEIP2929
   124  	jt[DELEGATECALL].dynamicGas = gasDelegateCallEIP2929
   125  
   126  	// This was previously part of the dynamic cost, but we're using it as a constantGas
   127  	// factor here
   128  	jt[SELFDESTRUCT].constantGas = params.SelfdestructGasEIP150
   129  	jt[SELFDESTRUCT].dynamicGas = gasSelfdestructEIP2929
   130  }
   131  
   132  // enable3529 enabled "EIP-3529: Reduction in refunds":
   133  // - Removes refunds for selfdestructs
   134  // - Reduces refunds for SSTORE
   135  // - Reduces max refunds to 20% gas
   136  func enable3529(jt *JumpTable) {
   137  	jt[SSTORE].dynamicGas = gasSStoreEIP3529
   138  	jt[SELFDESTRUCT].dynamicGas = gasSelfdestructEIP3529
   139  }
   140  
   141  // UsingOVM
   142  // Optimism specific changes
   143  func enableMinimal2929(jt *JumpTable) {
   144  	jt[SLOAD].constantGas = 0
   145  	jt[SLOAD].dynamicGas = gasSLoadEIP2929Optimism
   146  
   147  	jt[EXTCODECOPY].constantGas = params.WarmStorageReadCostEIP2929
   148  	jt[EXTCODECOPY].dynamicGas = gasExtCodeCopyEIP2929Optimism
   149  
   150  	jt[EXTCODESIZE].constantGas = params.WarmStorageReadCostEIP2929
   151  	jt[EXTCODESIZE].dynamicGas = gasEip2929AccountCheckOptimism
   152  
   153  	jt[EXTCODEHASH].constantGas = params.WarmStorageReadCostEIP2929
   154  	jt[EXTCODEHASH].dynamicGas = gasEip2929AccountCheckOptimism
   155  
   156  	jt[BALANCE].constantGas = params.WarmStorageReadCostEIP2929
   157  	jt[BALANCE].dynamicGas = gasEip2929AccountCheckOptimism
   158  
   159  	jt[CALL].constantGas = params.WarmStorageReadCostEIP2929
   160  	jt[CALL].dynamicGas = gasCallEIP2929Optimism
   161  
   162  	jt[CALLCODE].constantGas = params.WarmStorageReadCostEIP2929
   163  	jt[CALLCODE].dynamicGas = gasCallCodeEIP2929Optimism
   164  
   165  	jt[STATICCALL].constantGas = params.WarmStorageReadCostEIP2929
   166  	jt[STATICCALL].dynamicGas = gasStaticCallEIP2929Optimism
   167  
   168  	jt[DELEGATECALL].constantGas = params.WarmStorageReadCostEIP2929
   169  	jt[DELEGATECALL].dynamicGas = gasDelegateCallEIP2929Optimism
   170  
   171  	// This was previously part of the dynamic cost, but we're using it as a constantGas
   172  	// factor here
   173  	jt[SELFDESTRUCT].constantGas = params.SelfdestructGasEIP150
   174  	jt[SELFDESTRUCT].dynamicGas = gasSelfdestructEIP2929Optimism
   175  }