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