gitlab.com/flarenetwork/coreth@v0.1.1/core/vm/eips.go (about)

     1  // (c) 2019-2020, Ava Labs, Inc.
     2  //
     3  // This file is a derived work, based on the go-ethereum library whose original
     4  // notices appear below.
     5  //
     6  // It is distributed under a license compatible with the licensing terms of the
     7  // original code from which it is derived.
     8  //
     9  // Much love to the original authors for their work.
    10  // **********
    11  // Copyright 2019 The go-ethereum Authors
    12  // This file is part of the go-ethereum library.
    13  //
    14  // The go-ethereum library is free software: you can redistribute it and/or modify
    15  // it under the terms of the GNU Lesser General Public License as published by
    16  // the Free Software Foundation, either version 3 of the License, or
    17  // (at your option) any later version.
    18  //
    19  // The go-ethereum library is distributed in the hope that it will be useful,
    20  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    21  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    22  // GNU Lesser General Public License for more details.
    23  //
    24  // You should have received a copy of the GNU Lesser General Public License
    25  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    26  
    27  package vm
    28  
    29  import (
    30  	"fmt"
    31  	"sort"
    32  
    33  	"github.com/holiman/uint256"
    34  	"gitlab.com/flarenetwork/coreth/params"
    35  )
    36  
    37  var activators = map[int]func(*JumpTable){
    38  	3198: enable3198,
    39  	2929: enable2929,
    40  	2200: enable2200,
    41  	1884: enable1884,
    42  	1344: enable1344,
    43  }
    44  
    45  // EnableEIP enables the given EIP on the config.
    46  // This operation writes in-place, and callers need to ensure that the globally
    47  // defined jump tables are not polluted.
    48  func EnableEIP(eipNum int, jt *JumpTable) error {
    49  	enablerFn, ok := activators[eipNum]
    50  	if !ok {
    51  		return fmt.Errorf("undefined eip %d", eipNum)
    52  	}
    53  	enablerFn(jt)
    54  	return nil
    55  }
    56  
    57  func ValidEip(eipNum int) bool {
    58  	_, ok := activators[eipNum]
    59  	return ok
    60  }
    61  func ActivateableEips() []string {
    62  	var nums []string
    63  	for k := range activators {
    64  		nums = append(nums, fmt.Sprintf("%d", k))
    65  	}
    66  	sort.Strings(nums)
    67  	return nums
    68  }
    69  
    70  // enable1884 applies EIP-1884 to the given jump table:
    71  // - Increase cost of BALANCE to 700
    72  // - Increase cost of EXTCODEHASH to 700
    73  // - Increase cost of SLOAD to 800
    74  // - Define SELFBALANCE, with cost GasFastStep (5)
    75  func enable1884(jt *JumpTable) {
    76  	// Gas cost changes
    77  	jt[SLOAD].constantGas = params.SloadGasEIP1884
    78  	jt[BALANCE].constantGas = params.BalanceGasEIP1884
    79  	jt[EXTCODEHASH].constantGas = params.ExtcodeHashGasEIP1884
    80  
    81  	// New opcode
    82  	jt[SELFBALANCE] = &operation{
    83  		execute:     opSelfBalance,
    84  		constantGas: GasFastStep,
    85  		minStack:    minStack(0, 1),
    86  		maxStack:    maxStack(0, 1),
    87  	}
    88  }
    89  
    90  func opSelfBalance(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
    91  	balance, _ := uint256.FromBig(interpreter.evm.StateDB.GetBalance(scope.Contract.Address()))
    92  	scope.Stack.push(balance)
    93  	return nil, nil
    94  }
    95  
    96  // enable1344 applies EIP-1344 (ChainID Opcode)
    97  // - Adds an opcode that returns the current chain’s EIP-155 unique identifier
    98  func enable1344(jt *JumpTable) {
    99  	// New opcode
   100  	jt[CHAINID] = &operation{
   101  		execute:     opChainID,
   102  		constantGas: GasQuickStep,
   103  		minStack:    minStack(0, 1),
   104  		maxStack:    maxStack(0, 1),
   105  	}
   106  }
   107  
   108  // opChainID implements CHAINID opcode
   109  func opChainID(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   110  	chainId, _ := uint256.FromBig(interpreter.evm.chainConfig.ChainID)
   111  	scope.Stack.push(chainId)
   112  	return nil, nil
   113  }
   114  
   115  // enable2200 applies EIP-2200 (Rebalance net-metered SSTORE)
   116  func enable2200(jt *JumpTable) {
   117  	jt[SLOAD].constantGas = params.SloadGasEIP2200
   118  	jt[SSTORE].dynamicGas = gasSStoreEIP2200
   119  }
   120  
   121  // enable2929 enables "EIP-2929: Gas cost increases for state access opcodes"
   122  // https://eips.ethereum.org/EIPS/eip-2929
   123  func enable2929(jt *JumpTable) {
   124  	jt[SSTORE].dynamicGas = gasSStoreEIP2929
   125  
   126  	jt[SLOAD].constantGas = 0
   127  	jt[SLOAD].dynamicGas = gasSLoadEIP2929
   128  
   129  	jt[EXTCODECOPY].constantGas = params.WarmStorageReadCostEIP2929
   130  	jt[EXTCODECOPY].dynamicGas = gasExtCodeCopyEIP2929
   131  
   132  	jt[EXTCODESIZE].constantGas = params.WarmStorageReadCostEIP2929
   133  	jt[EXTCODESIZE].dynamicGas = gasEip2929AccountCheck
   134  
   135  	jt[EXTCODEHASH].constantGas = params.WarmStorageReadCostEIP2929
   136  	jt[EXTCODEHASH].dynamicGas = gasEip2929AccountCheck
   137  
   138  	jt[BALANCE].constantGas = params.WarmStorageReadCostEIP2929
   139  	jt[BALANCE].dynamicGas = gasEip2929AccountCheck
   140  
   141  	jt[CALL].constantGas = params.WarmStorageReadCostEIP2929
   142  	jt[CALL].dynamicGas = gasCallEIP2929
   143  
   144  	jt[CALLCODE].constantGas = params.WarmStorageReadCostEIP2929
   145  	jt[CALLCODE].dynamicGas = gasCallCodeEIP2929
   146  
   147  	jt[STATICCALL].constantGas = params.WarmStorageReadCostEIP2929
   148  	jt[STATICCALL].dynamicGas = gasStaticCallEIP2929
   149  
   150  	jt[DELEGATECALL].constantGas = params.WarmStorageReadCostEIP2929
   151  	jt[DELEGATECALL].dynamicGas = gasDelegateCallEIP2929
   152  
   153  	// This was previously part of the dynamic cost, but we're using it as a constantGas
   154  	// factor here
   155  	jt[SELFDESTRUCT].constantGas = params.SelfdestructGasEIP150
   156  	jt[SELFDESTRUCT].dynamicGas = gasSelfdestructEIP2929
   157  }
   158  
   159  // enableAP1 disables gas refunds for SSTORE and SELFDESTRUCT. It is very
   160  // similar to EIP-3298: Removal of Refunds [DRAFT]
   161  // (https://eips.ethereum.org/EIPS/eip-3298).
   162  func enableAP1(jt *JumpTable) {
   163  	jt[SSTORE].dynamicGas = gasSStoreAP1
   164  	jt[SELFDESTRUCT].dynamicGas = gasSelfdestructAP1
   165  	jt[CALLEX].dynamicGas = gasCallExpertAP1
   166  }
   167  
   168  func enableAP2(jt *JumpTable) {
   169  	jt[BALANCEMC] = nil
   170  	jt[CALLEX] = nil
   171  }
   172  
   173  // enable3198 applies EIP-3198 (BASEFEE Opcode)
   174  // - Adds an opcode that returns the current block's base fee.
   175  func enable3198(jt *JumpTable) {
   176  	// New opcode
   177  	jt[BASEFEE] = &operation{
   178  		execute:     opBaseFee,
   179  		constantGas: GasQuickStep,
   180  		minStack:    minStack(0, 1),
   181  		maxStack:    maxStack(0, 1),
   182  	}
   183  }
   184  
   185  // opBaseFee implements BASEFEE opcode
   186  func opBaseFee(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
   187  	baseFee, _ := uint256.FromBig(interpreter.evm.Context.BaseFee)
   188  	scope.Stack.push(baseFee)
   189  	return nil, nil
   190  }