github.com/ethw3/go-ethereuma@v0.0.0-20221013053120-c14602a4c23c/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/ethw3/go-ethereuma/params"
    24  	"github.com/holiman/uint256"
    25  )
    26  
    27  var activators = map[int]func(*JumpTable){
    28  	3855: enable3855,
    29  	3529: enable3529,
    30  	3198: enable3198,
    31  	2929: enable2929,
    32  	2200: enable2200,
    33  	1884: enable1884,
    34  	1344: enable1344,
    35  }
    36  
    37  // EnableEIP enables the given EIP on the config.
    38  // This operation writes in-place, and callers need to ensure that the globally
    39  // defined jump tables are not polluted.
    40  func EnableEIP(eipNum int, jt *JumpTable) error {
    41  	enablerFn, ok := activators[eipNum]
    42  	if !ok {
    43  		return fmt.Errorf("undefined eip %d", eipNum)
    44  	}
    45  	enablerFn(jt)
    46  	return nil
    47  }
    48  
    49  func ValidEip(eipNum int) bool {
    50  	_, ok := activators[eipNum]
    51  	return ok
    52  }
    53  func ActivateableEips() []string {
    54  	var nums []string
    55  	for k := range activators {
    56  		nums = append(nums, fmt.Sprintf("%d", k))
    57  	}
    58  	sort.Strings(nums)
    59  	return nums
    60  }
    61  
    62  // enable1884 applies EIP-1884 to the given jump table:
    63  // - Increase cost of BALANCE to 700
    64  // - Increase cost of EXTCODEHASH to 700
    65  // - Increase cost of SLOAD to 800
    66  // - Define SELFBALANCE, with cost GasFastStep (5)
    67  func enable1884(jt *JumpTable) {
    68  	// Gas cost changes
    69  	jt[SLOAD].constantGas = params.SloadGasEIP1884
    70  	jt[BALANCE].constantGas = params.BalanceGasEIP1884
    71  	jt[EXTCODEHASH].constantGas = params.ExtcodeHashGasEIP1884
    72  
    73  	// New opcode
    74  	jt[SELFBALANCE] = &operation{
    75  		execute:     opSelfBalance,
    76  		constantGas: GasFastStep,
    77  		minStack:    minStack(0, 1),
    78  		maxStack:    maxStack(0, 1),
    79  	}
    80  }
    81  
    82  func opSelfBalance(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
    83  	balance, _ := uint256.FromBig(interpreter.evm.StateDB.GetBalance(scope.Contract.Address()))
    84  	scope.Stack.push(balance)
    85  	return nil, nil
    86  }
    87  
    88  // enable1344 applies EIP-1344 (ChainID Opcode)
    89  // - Adds an opcode that returns the current chain’s EIP-155 unique identifier
    90  func enable1344(jt *JumpTable) {
    91  	// New opcode
    92  	jt[CHAINID] = &operation{
    93  		execute:     opChainID,
    94  		constantGas: GasQuickStep,
    95  		minStack:    minStack(0, 1),
    96  		maxStack:    maxStack(0, 1),
    97  	}
    98  }
    99  
   100  // opChainID implements CHAINID opcode
   101  func opChainID(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   102  	if interpreter.evm.chainConfig.IsEthPoWFork(interpreter.evm.Context.BlockNumber) {
   103  		chainId, _ := uint256.FromBig(interpreter.evm.chainConfig.ChainID_ALT)
   104  		scope.Stack.push(chainId)
   105  	} else {
   106  		chainId, _ := uint256.FromBig(interpreter.evm.chainConfig.ChainID)
   107  		scope.Stack.push(chainId)
   108  	}
   109  	return nil, nil
   110  }
   111  
   112  // enable2200 applies EIP-2200 (Rebalance net-metered SSTORE)
   113  func enable2200(jt *JumpTable) {
   114  	jt[SLOAD].constantGas = params.SloadGasEIP2200
   115  	jt[SSTORE].dynamicGas = gasSStoreEIP2200
   116  }
   117  
   118  // enable2929 enables "EIP-2929: Gas cost increases for state access opcodes"
   119  // https://eips.ethereum.org/EIPS/eip-2929
   120  func enable2929(jt *JumpTable) {
   121  	jt[SSTORE].dynamicGas = gasSStoreEIP2929
   122  
   123  	jt[SLOAD].constantGas = 0
   124  	jt[SLOAD].dynamicGas = gasSLoadEIP2929
   125  
   126  	jt[EXTCODECOPY].constantGas = params.WarmStorageReadCostEIP2929
   127  	jt[EXTCODECOPY].dynamicGas = gasExtCodeCopyEIP2929
   128  
   129  	jt[EXTCODESIZE].constantGas = params.WarmStorageReadCostEIP2929
   130  	jt[EXTCODESIZE].dynamicGas = gasEip2929AccountCheck
   131  
   132  	jt[EXTCODEHASH].constantGas = params.WarmStorageReadCostEIP2929
   133  	jt[EXTCODEHASH].dynamicGas = gasEip2929AccountCheck
   134  
   135  	jt[BALANCE].constantGas = params.WarmStorageReadCostEIP2929
   136  	jt[BALANCE].dynamicGas = gasEip2929AccountCheck
   137  
   138  	jt[CALL].constantGas = params.WarmStorageReadCostEIP2929
   139  	jt[CALL].dynamicGas = gasCallEIP2929
   140  
   141  	jt[CALLCODE].constantGas = params.WarmStorageReadCostEIP2929
   142  	jt[CALLCODE].dynamicGas = gasCallCodeEIP2929
   143  
   144  	jt[STATICCALL].constantGas = params.WarmStorageReadCostEIP2929
   145  	jt[STATICCALL].dynamicGas = gasStaticCallEIP2929
   146  
   147  	jt[DELEGATECALL].constantGas = params.WarmStorageReadCostEIP2929
   148  	jt[DELEGATECALL].dynamicGas = gasDelegateCallEIP2929
   149  
   150  	// This was previously part of the dynamic cost, but we're using it as a constantGas
   151  	// factor here
   152  	jt[SELFDESTRUCT].constantGas = params.SelfdestructGasEIP150
   153  	jt[SELFDESTRUCT].dynamicGas = gasSelfdestructEIP2929
   154  }
   155  
   156  // enable3529 enabled "EIP-3529: Reduction in refunds":
   157  // - Removes refunds for selfdestructs
   158  // - Reduces refunds for SSTORE
   159  // - Reduces max refunds to 20% gas
   160  func enable3529(jt *JumpTable) {
   161  	jt[SSTORE].dynamicGas = gasSStoreEIP3529
   162  	jt[SELFDESTRUCT].dynamicGas = gasSelfdestructEIP3529
   163  }
   164  
   165  // enable3198 applies EIP-3198 (BASEFEE Opcode)
   166  // - Adds an opcode that returns the current block's base fee.
   167  func enable3198(jt *JumpTable) {
   168  	// New opcode
   169  	jt[BASEFEE] = &operation{
   170  		execute:     opBaseFee,
   171  		constantGas: GasQuickStep,
   172  		minStack:    minStack(0, 1),
   173  		maxStack:    maxStack(0, 1),
   174  	}
   175  }
   176  
   177  // opBaseFee implements BASEFEE opcode
   178  func opBaseFee(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   179  	baseFee, _ := uint256.FromBig(interpreter.evm.Context.BaseFee)
   180  	scope.Stack.push(baseFee)
   181  	return nil, nil
   182  }
   183  
   184  // enable3855 applies EIP-3855 (PUSH0 opcode)
   185  func enable3855(jt *JumpTable) {
   186  	// New opcode
   187  	jt[PUSH0] = &operation{
   188  		execute:     opPush0,
   189  		constantGas: GasQuickStep,
   190  		minStack:    minStack(0, 1),
   191  		maxStack:    maxStack(0, 1),
   192  	}
   193  }
   194  
   195  // opPush0 implements the PUSH0 opcode
   196  func opPush0(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   197  	scope.Stack.push(new(uint256.Int))
   198  	return nil, nil
   199  }