github.com/zhiqiangxu/go-ethereum@v1.9.16-0.20210824055606-be91cfdebc48/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  
    22  	"github.com/zhiqiangxu/go-ethereum/params"
    23  )
    24  
    25  // EnableEIP enables the given EIP on the config.
    26  // This operation writes in-place, and callers need to ensure that the globally
    27  // defined jump tables are not polluted.
    28  func EnableEIP(eipNum int, jt *JumpTable) error {
    29  	switch eipNum {
    30  	case 2200:
    31  		enable2200(jt)
    32  	case 1884:
    33  		enable1884(jt)
    34  	case 1344:
    35  		enable1344(jt)
    36  	case 2315:
    37  		enable2315(jt)
    38  	default:
    39  		return fmt.Errorf("undefined eip %d", eipNum)
    40  	}
    41  	return nil
    42  }
    43  
    44  // enable1884 applies EIP-1884 to the given jump table:
    45  // - Increase cost of BALANCE to 700
    46  // - Increase cost of EXTCODEHASH to 700
    47  // - Increase cost of SLOAD to 800
    48  // - Define SELFBALANCE, with cost GasFastStep (5)
    49  func enable1884(jt *JumpTable) {
    50  	// Gas cost changes
    51  	jt[SLOAD].constantGas = params.SloadGasEIP1884
    52  	jt[BALANCE].constantGas = params.BalanceGasEIP1884
    53  	jt[EXTCODEHASH].constantGas = params.ExtcodeHashGasEIP1884
    54  
    55  	// New opcode
    56  	jt[SELFBALANCE] = operation{
    57  		execute:     opSelfBalance,
    58  		constantGas: GasFastStep,
    59  		minStack:    minStack(0, 1),
    60  		maxStack:    maxStack(0, 1),
    61  		valid:       true,
    62  	}
    63  }
    64  
    65  func opSelfBalance(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
    66  	balance := interpreter.intPool.get().Set(interpreter.evm.StateDB.GetBalance(callContext.contract.Address()))
    67  	callContext.stack.push(balance)
    68  	return nil, nil
    69  }
    70  
    71  // enable1344 applies EIP-1344 (ChainID Opcode)
    72  // - Adds an opcode that returns the current chain’s EIP-155 unique identifier
    73  func enable1344(jt *JumpTable) {
    74  	// New opcode
    75  	jt[CHAINID] = operation{
    76  		execute:     opChainID,
    77  		constantGas: GasQuickStep,
    78  		minStack:    minStack(0, 1),
    79  		maxStack:    maxStack(0, 1),
    80  		valid:       true,
    81  	}
    82  }
    83  
    84  // opChainID implements CHAINID opcode
    85  func opChainID(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
    86  	chainId := interpreter.intPool.get().Set(interpreter.evm.chainConfig.ChainID)
    87  	callContext.stack.push(chainId)
    88  	return nil, nil
    89  }
    90  
    91  // enable2200 applies EIP-2200 (Rebalance net-metered SSTORE)
    92  func enable2200(jt *JumpTable) {
    93  	jt[SLOAD].constantGas = params.SloadGasEIP2200
    94  	jt[SSTORE].dynamicGas = gasSStoreEIP2200
    95  }
    96  
    97  // enable2315 applies EIP-2315 (Simple Subroutines)
    98  // - Adds opcodes that jump to and return from subroutines
    99  func enable2315(jt *JumpTable) {
   100  	// New opcode
   101  	jt[BEGINSUB] = operation{
   102  		execute:     opBeginSub,
   103  		constantGas: GasQuickStep,
   104  		minStack:    minStack(0, 0),
   105  		maxStack:    maxStack(0, 0),
   106  		valid:       true,
   107  	}
   108  	// New opcode
   109  	jt[JUMPSUB] = operation{
   110  		execute:     opJumpSub,
   111  		constantGas: GasSlowStep,
   112  		minStack:    minStack(1, 0),
   113  		maxStack:    maxStack(1, 0),
   114  		jumps:       true,
   115  		valid:       true,
   116  	}
   117  	// New opcode
   118  	jt[RETURNSUB] = operation{
   119  		execute:     opReturnSub,
   120  		constantGas: GasFastStep,
   121  		minStack:    minStack(0, 0),
   122  		maxStack:    maxStack(0, 0),
   123  		valid:       true,
   124  		jumps:       true,
   125  	}
   126  }