github.com/amazechain/amc@v0.1.3/internal/vm/eips.go (about)

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