github.com/snowblossomcoin/go-ethereum@v1.9.25/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/ethereum/go-ethereum/params"
    24  	"github.com/holiman/uint256"
    25  )
    26  
    27  var activators = map[int]func(*JumpTable){
    28  	2929: enable2929,
    29  	2200: enable2200,
    30  	1884: enable1884,
    31  	1344: enable1344,
    32  	2315: enable2315,
    33  }
    34  
    35  // EnableEIP enables the given EIP on the config.
    36  // This operation writes in-place, and callers need to ensure that the globally
    37  // defined jump tables are not polluted.
    38  func EnableEIP(eipNum int, jt *JumpTable) error {
    39  	enablerFn, ok := activators[eipNum]
    40  	if !ok {
    41  		return fmt.Errorf("undefined eip %d", eipNum)
    42  	}
    43  	enablerFn(jt)
    44  	return nil
    45  }
    46  
    47  func ValidEip(eipNum int) bool {
    48  	_, ok := activators[eipNum]
    49  	return ok
    50  }
    51  func ActivateableEips() []string {
    52  	var nums []string
    53  	for k := range activators {
    54  		nums = append(nums, fmt.Sprintf("%d", k))
    55  	}
    56  	sort.Strings(nums)
    57  	return nums
    58  }
    59  
    60  // enable1884 applies EIP-1884 to the given jump table:
    61  // - Increase cost of BALANCE to 700
    62  // - Increase cost of EXTCODEHASH to 700
    63  // - Increase cost of SLOAD to 800
    64  // - Define SELFBALANCE, with cost GasFastStep (5)
    65  func enable1884(jt *JumpTable) {
    66  	// Gas cost changes
    67  	jt[SLOAD].constantGas = params.SloadGasEIP1884
    68  	jt[BALANCE].constantGas = params.BalanceGasEIP1884
    69  	jt[EXTCODEHASH].constantGas = params.ExtcodeHashGasEIP1884
    70  
    71  	// New opcode
    72  	jt[SELFBALANCE] = &operation{
    73  		execute:     opSelfBalance,
    74  		constantGas: GasFastStep,
    75  		minStack:    minStack(0, 1),
    76  		maxStack:    maxStack(0, 1),
    77  	}
    78  }
    79  
    80  func opSelfBalance(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
    81  	balance, _ := uint256.FromBig(interpreter.evm.StateDB.GetBalance(callContext.contract.Address()))
    82  	callContext.stack.push(balance)
    83  	return nil, nil
    84  }
    85  
    86  // enable1344 applies EIP-1344 (ChainID Opcode)
    87  // - Adds an opcode that returns the current chain’s EIP-155 unique identifier
    88  func enable1344(jt *JumpTable) {
    89  	// New opcode
    90  	jt[CHAINID] = &operation{
    91  		execute:     opChainID,
    92  		constantGas: GasQuickStep,
    93  		minStack:    minStack(0, 1),
    94  		maxStack:    maxStack(0, 1),
    95  	}
    96  }
    97  
    98  // opChainID implements CHAINID opcode
    99  func opChainID(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
   100  	chainId, _ := uint256.FromBig(interpreter.evm.chainConfig.ChainID)
   101  	callContext.stack.push(chainId)
   102  	return nil, nil
   103  }
   104  
   105  // enable2200 applies EIP-2200 (Rebalance net-metered SSTORE)
   106  func enable2200(jt *JumpTable) {
   107  	jt[SLOAD].constantGas = params.SloadGasEIP2200
   108  	jt[SSTORE].dynamicGas = gasSStoreEIP2200
   109  }
   110  
   111  // enable2315 applies EIP-2315 (Simple Subroutines)
   112  // - Adds opcodes that jump to and return from subroutines
   113  func enable2315(jt *JumpTable) {
   114  	// New opcode
   115  	jt[BEGINSUB] = &operation{
   116  		execute:     opBeginSub,
   117  		constantGas: GasQuickStep,
   118  		minStack:    minStack(0, 0),
   119  		maxStack:    maxStack(0, 0),
   120  	}
   121  	// New opcode
   122  	jt[JUMPSUB] = &operation{
   123  		execute:     opJumpSub,
   124  		constantGas: GasSlowStep,
   125  		minStack:    minStack(1, 0),
   126  		maxStack:    maxStack(1, 0),
   127  		jumps:       true,
   128  	}
   129  	// New opcode
   130  	jt[RETURNSUB] = &operation{
   131  		execute:     opReturnSub,
   132  		constantGas: GasFastStep,
   133  		minStack:    minStack(0, 0),
   134  		maxStack:    maxStack(0, 0),
   135  		jumps:       true,
   136  	}
   137  }
   138  
   139  // enable2929 enables "EIP-2929: Gas cost increases for state access opcodes"
   140  // https://eips.ethereum.org/EIPS/eip-2929
   141  func enable2929(jt *JumpTable) {
   142  	jt[SSTORE].dynamicGas = gasSStoreEIP2929
   143  
   144  	jt[SLOAD].constantGas = 0
   145  	jt[SLOAD].dynamicGas = gasSLoadEIP2929
   146  
   147  	jt[EXTCODECOPY].constantGas = WarmStorageReadCostEIP2929
   148  	jt[EXTCODECOPY].dynamicGas = gasExtCodeCopyEIP2929
   149  
   150  	jt[EXTCODESIZE].constantGas = WarmStorageReadCostEIP2929
   151  	jt[EXTCODESIZE].dynamicGas = gasEip2929AccountCheck
   152  
   153  	jt[EXTCODEHASH].constantGas = WarmStorageReadCostEIP2929
   154  	jt[EXTCODEHASH].dynamicGas = gasEip2929AccountCheck
   155  
   156  	jt[BALANCE].constantGas = WarmStorageReadCostEIP2929
   157  	jt[BALANCE].dynamicGas = gasEip2929AccountCheck
   158  
   159  	jt[CALL].constantGas = WarmStorageReadCostEIP2929
   160  	jt[CALL].dynamicGas = gasCallEIP2929
   161  
   162  	jt[CALLCODE].constantGas = WarmStorageReadCostEIP2929
   163  	jt[CALLCODE].dynamicGas = gasCallCodeEIP2929
   164  
   165  	jt[STATICCALL].constantGas = WarmStorageReadCostEIP2929
   166  	jt[STATICCALL].dynamicGas = gasStaticCallEIP2929
   167  
   168  	jt[DELEGATECALL].constantGas = WarmStorageReadCostEIP2929
   169  	jt[DELEGATECALL].dynamicGas = gasDelegateCallEIP2929
   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 = gasSelfdestructEIP2929
   175  }