github.com/ontio/ontology@v1.14.4/vm/evm/eips.go (about)

     1  // Copyright (C) 2021 The Ontology Authors
     2  // Copyright 2019 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package evm
    19  
    20  import (
    21  	"fmt"
    22  
    23  	"github.com/holiman/uint256"
    24  	"github.com/ontio/ontology/vm/evm/params"
    25  )
    26  
    27  var activators = map[int]func(*JumpTable){
    28  	2200: enable2200,
    29  	1884: enable1884,
    30  	1344: enable1344,
    31  	2315: enable2315,
    32  }
    33  
    34  // EnableEIP enables the given EIP on the config.
    35  // This operation writes in-place, and callers need to ensure that the globally
    36  // defined jump tables are not polluted.
    37  func EnableEIP(eipNum int, jt *JumpTable) error {
    38  	enablerFn, ok := activators[eipNum]
    39  	if !ok {
    40  		return fmt.Errorf("undefined eip %d", eipNum)
    41  	}
    42  	enablerFn(jt)
    43  	return nil
    44  }
    45  
    46  // enable1884 applies EIP-1884 to the given jump table:
    47  // - Increase cost of BALANCE to 700
    48  // - Increase cost of EXTCODEHASH to 700
    49  // - Increase cost of SLOAD to 800
    50  // - Define SELFBALANCE, with cost GasFastStep (5)
    51  func enable1884(jt *JumpTable) {
    52  	// Gas cost changes
    53  	jt[SLOAD].constantGas = params.SloadGasEIP1884
    54  	jt[BALANCE].constantGas = params.BalanceGasEIP1884
    55  	jt[EXTCODEHASH].constantGas = params.ExtcodeHashGasEIP1884
    56  
    57  	// New opcode
    58  	jt[SELFBALANCE] = &operation{
    59  		execute:     opSelfBalance,
    60  		constantGas: GasFastStep,
    61  		minStack:    minStack(0, 1),
    62  		maxStack:    maxStack(0, 1),
    63  	}
    64  }
    65  
    66  func opSelfBalance(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
    67  	balance, _ := uint256.FromBig(interpreter.evm.StateDB.GetBalance(callContext.contract.Address()))
    68  	callContext.stack.push(balance)
    69  	return nil, nil
    70  }
    71  
    72  // enable1344 applies EIP-1344 (ChainID Opcode)
    73  // - Adds an opcode that returns the current chain’s EIP-155 unique identifier
    74  func enable1344(jt *JumpTable) {
    75  	// New opcode
    76  	jt[CHAINID] = &operation{
    77  		execute:     opChainID,
    78  		constantGas: GasQuickStep,
    79  		minStack:    minStack(0, 1),
    80  		maxStack:    maxStack(0, 1),
    81  	}
    82  }
    83  
    84  // opChainID implements CHAINID opcode
    85  func opChainID(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
    86  	chainId, _ := uint256.FromBig(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  	}
   107  	// New opcode
   108  	jt[JUMPSUB] = &operation{
   109  		execute:     opJumpSub,
   110  		constantGas: GasSlowStep,
   111  		minStack:    minStack(1, 0),
   112  		maxStack:    maxStack(1, 0),
   113  		jumps:       true,
   114  	}
   115  	// New opcode
   116  	jt[RETURNSUB] = &operation{
   117  		execute:     opReturnSub,
   118  		constantGas: GasFastStep,
   119  		minStack:    minStack(0, 0),
   120  		maxStack:    maxStack(0, 0),
   121  		jumps:       true,
   122  	}
   123  }