github.com/theQRL/go-zond@v0.1.1/core/vm/operations_acl.go (about)

     1  // Copyright 2020 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  	"errors"
    21  
    22  	"github.com/theQRL/go-zond/common"
    23  	"github.com/theQRL/go-zond/common/math"
    24  	"github.com/theQRL/go-zond/params"
    25  )
    26  
    27  func makeGasSStoreFunc(clearingRefund uint64) gasFunc {
    28  	return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
    29  		// If we fail the minimum gas availability invariant, fail (0)
    30  		if contract.Gas <= params.SstoreSentryGasEIP2200 {
    31  			return 0, errors.New("not enough gas for reentrancy sentry")
    32  		}
    33  		// Gas sentry honoured, do the actual gas calculation based on the stored value
    34  		var (
    35  			y, x    = stack.Back(1), stack.peek()
    36  			slot    = common.Hash(x.Bytes32())
    37  			current = evm.StateDB.GetState(contract.Address(), slot)
    38  			cost    = uint64(0)
    39  		)
    40  		// Check slot presence in the access list
    41  		if addrPresent, slotPresent := evm.StateDB.SlotInAccessList(contract.Address(), slot); !slotPresent {
    42  			cost = params.ColdSloadCostEIP2929
    43  			// If the caller cannot afford the cost, this change will be rolled back
    44  			evm.StateDB.AddSlotToAccessList(contract.Address(), slot)
    45  			if !addrPresent {
    46  				// Once we're done with YOLOv2 and schedule this for mainnet, might
    47  				// be good to remove this panic here, which is just really a
    48  				// canary to have during testing
    49  				panic("impossible case: address was not present in access list during sstore op")
    50  			}
    51  		}
    52  		value := common.Hash(y.Bytes32())
    53  
    54  		if current == value { // noop (1)
    55  			// EIP 2200 original clause:
    56  			//		return params.SloadGasEIP2200, nil
    57  			return cost + params.WarmStorageReadCostEIP2929, nil // SLOAD_GAS
    58  		}
    59  		original := evm.StateDB.GetCommittedState(contract.Address(), x.Bytes32())
    60  		if original == current {
    61  			if original == (common.Hash{}) { // create slot (2.1.1)
    62  				return cost + params.SstoreSetGasEIP2200, nil
    63  			}
    64  			if value == (common.Hash{}) { // delete slot (2.1.2b)
    65  				evm.StateDB.AddRefund(clearingRefund)
    66  			}
    67  			// EIP-2200 original clause:
    68  			//		return params.SstoreResetGasEIP2200, nil // write existing slot (2.1.2)
    69  			return cost + (params.SstoreResetGasEIP2200 - params.ColdSloadCostEIP2929), nil // write existing slot (2.1.2)
    70  		}
    71  		if original != (common.Hash{}) {
    72  			if current == (common.Hash{}) { // recreate slot (2.2.1.1)
    73  				evm.StateDB.SubRefund(clearingRefund)
    74  			} else if value == (common.Hash{}) { // delete slot (2.2.1.2)
    75  				evm.StateDB.AddRefund(clearingRefund)
    76  			}
    77  		}
    78  		if original == value {
    79  			if original == (common.Hash{}) { // reset to original inexistent slot (2.2.2.1)
    80  				// EIP 2200 Original clause:
    81  				//evm.StateDB.AddRefund(params.SstoreSetGasEIP2200 - params.SloadGasEIP2200)
    82  				evm.StateDB.AddRefund(params.SstoreSetGasEIP2200 - params.WarmStorageReadCostEIP2929)
    83  			} else { // reset to original existing slot (2.2.2.2)
    84  				// EIP 2200 Original clause:
    85  				//	evm.StateDB.AddRefund(params.SstoreResetGasEIP2200 - params.SloadGasEIP2200)
    86  				// - SSTORE_RESET_GAS redefined as (5000 - COLD_SLOAD_COST)
    87  				// - SLOAD_GAS redefined as WARM_STORAGE_READ_COST
    88  				// Final: (5000 - COLD_SLOAD_COST) - WARM_STORAGE_READ_COST
    89  				evm.StateDB.AddRefund((params.SstoreResetGasEIP2200 - params.ColdSloadCostEIP2929) - params.WarmStorageReadCostEIP2929)
    90  			}
    91  		}
    92  		// EIP-2200 original clause:
    93  		//return params.SloadGasEIP2200, nil // dirty update (2.2)
    94  		return cost + params.WarmStorageReadCostEIP2929, nil // dirty update (2.2)
    95  	}
    96  }
    97  
    98  // gasSLoadEIP2929 calculates dynamic gas for SLOAD according to EIP-2929
    99  // For SLOAD, if the (address, storage_key) pair (where address is the address of the contract
   100  // whose storage is being read) is not yet in accessed_storage_keys,
   101  // charge 2100 gas and add the pair to accessed_storage_keys.
   102  // If the pair is already in accessed_storage_keys, charge 100 gas.
   103  func gasSLoadEIP2929(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   104  	loc := stack.peek()
   105  	slot := common.Hash(loc.Bytes32())
   106  	// Check slot presence in the access list
   107  	if _, slotPresent := evm.StateDB.SlotInAccessList(contract.Address(), slot); !slotPresent {
   108  		// If the caller cannot afford the cost, this change will be rolled back
   109  		// If he does afford it, we can skip checking the same thing later on, during execution
   110  		evm.StateDB.AddSlotToAccessList(contract.Address(), slot)
   111  		return params.ColdSloadCostEIP2929, nil
   112  	}
   113  	return params.WarmStorageReadCostEIP2929, nil
   114  }
   115  
   116  // gasExtCodeCopyEIP2929 implements extcodecopy according to EIP-2929
   117  // EIP spec:
   118  // > If the target is not in accessed_addresses,
   119  // > charge COLD_ACCOUNT_ACCESS_COST gas, and add the address to accessed_addresses.
   120  // > Otherwise, charge WARM_STORAGE_READ_COST gas.
   121  func gasExtCodeCopyEIP2929(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   122  	// memory expansion first (dynamic part of pre-2929 implementation)
   123  	gas, err := gasExtCodeCopy(evm, contract, stack, mem, memorySize)
   124  	if err != nil {
   125  		return 0, err
   126  	}
   127  	addr := common.Address(stack.peek().Bytes20())
   128  	// Check slot presence in the access list
   129  	if !evm.StateDB.AddressInAccessList(addr) {
   130  		evm.StateDB.AddAddressToAccessList(addr)
   131  		var overflow bool
   132  		// We charge (cold-warm), since 'warm' is already charged as constantGas
   133  		if gas, overflow = math.SafeAdd(gas, params.ColdAccountAccessCostEIP2929-params.WarmStorageReadCostEIP2929); overflow {
   134  			return 0, ErrGasUintOverflow
   135  		}
   136  		return gas, nil
   137  	}
   138  	return gas, nil
   139  }
   140  
   141  // gasEip2929AccountCheck checks whether the first stack item (as address) is present in the access list.
   142  // If it is, this method returns '0', otherwise 'cold-warm' gas, presuming that the opcode using it
   143  // is also using 'warm' as constant factor.
   144  // This method is used by:
   145  // - extcodehash,
   146  // - extcodesize,
   147  // - (ext) balance
   148  func gasEip2929AccountCheck(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   149  	addr := common.Address(stack.peek().Bytes20())
   150  	// Check slot presence in the access list
   151  	if !evm.StateDB.AddressInAccessList(addr) {
   152  		// If the caller cannot afford the cost, this change will be rolled back
   153  		evm.StateDB.AddAddressToAccessList(addr)
   154  		// The warm storage read cost is already charged as constantGas
   155  		return params.ColdAccountAccessCostEIP2929 - params.WarmStorageReadCostEIP2929, nil
   156  	}
   157  	return 0, nil
   158  }
   159  
   160  func makeCallVariantGasCallEIP2929(oldCalculator gasFunc) gasFunc {
   161  	return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   162  		addr := common.Address(stack.Back(1).Bytes20())
   163  		// Check slot presence in the access list
   164  		warmAccess := evm.StateDB.AddressInAccessList(addr)
   165  		// The WarmStorageReadCostEIP2929 (100) is already deducted in the form of a constant cost, so
   166  		// the cost to charge for cold access, if any, is Cold - Warm
   167  		coldCost := params.ColdAccountAccessCostEIP2929 - params.WarmStorageReadCostEIP2929
   168  		if !warmAccess {
   169  			evm.StateDB.AddAddressToAccessList(addr)
   170  			// Charge the remaining difference here already, to correctly calculate available
   171  			// gas for call
   172  			if !contract.UseGas(coldCost) {
   173  				return 0, ErrOutOfGas
   174  			}
   175  		}
   176  		// Now call the old calculator, which takes into account
   177  		// - create new account
   178  		// - transfer value
   179  		// - memory expansion
   180  		// - 63/64ths rule
   181  		gas, err := oldCalculator(evm, contract, stack, mem, memorySize)
   182  		if warmAccess || err != nil {
   183  			return gas, err
   184  		}
   185  		// In case of a cold access, we temporarily add the cold charge back, and also
   186  		// add it to the returned gas. By adding it to the return, it will be charged
   187  		// outside of this function, as part of the dynamic gas, and that will make it
   188  		// also become correctly reported to tracers.
   189  		contract.Gas += coldCost
   190  		return gas + coldCost, nil
   191  	}
   192  }
   193  
   194  var (
   195  	gasCallEIP2929         = makeCallVariantGasCallEIP2929(gasCall)
   196  	gasDelegateCallEIP2929 = makeCallVariantGasCallEIP2929(gasDelegateCall)
   197  	gasStaticCallEIP2929   = makeCallVariantGasCallEIP2929(gasStaticCall)
   198  	gasCallCodeEIP2929     = makeCallVariantGasCallEIP2929(gasCallCode)
   199  	gasSelfdestructEIP2929 = makeSelfdestructGasFn(true)
   200  	// gasSelfdestructEIP3529 implements the changes in EIP-2539 (no refunds)
   201  	gasSelfdestructEIP3529 = makeSelfdestructGasFn(false)
   202  
   203  	// gasSStoreEIP2929 implements gas cost for SSTORE according to EIP-2929
   204  	//
   205  	// When calling SSTORE, check if the (address, storage_key) pair is in accessed_storage_keys.
   206  	// If it is not, charge an additional COLD_SLOAD_COST gas, and add the pair to accessed_storage_keys.
   207  	// Additionally, modify the parameters defined in EIP 2200 as follows:
   208  	//
   209  	// Parameter 	Old value 	New value
   210  	// SLOAD_GAS 	800 	= WARM_STORAGE_READ_COST
   211  	// SSTORE_RESET_GAS 	5000 	5000 - COLD_SLOAD_COST
   212  	//
   213  	//The other parameters defined in EIP 2200 are unchanged.
   214  	// see gasSStoreEIP2200(...) in core/vm/gas_table.go for more info about how EIP 2200 is specified
   215  	gasSStoreEIP2929 = makeGasSStoreFunc(params.SstoreClearsScheduleRefundEIP2200)
   216  
   217  	// gasSStoreEIP2539 implements gas cost for SSTORE according to EIP-2539
   218  	// Replace `SSTORE_CLEARS_SCHEDULE` with `SSTORE_RESET_GAS + ACCESS_LIST_STORAGE_KEY_COST` (4,800)
   219  	gasSStoreEIP3529 = makeGasSStoreFunc(params.SstoreClearsScheduleRefundEIP3529)
   220  )
   221  
   222  // makeSelfdestructGasFn can create the selfdestruct dynamic gas function for EIP-2929 and EIP-2539
   223  func makeSelfdestructGasFn(refundsEnabled bool) gasFunc {
   224  	gasFunc := func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
   225  		var (
   226  			gas     uint64
   227  			address = common.Address(stack.peek().Bytes20())
   228  		)
   229  		if !evm.StateDB.AddressInAccessList(address) {
   230  			// If the caller cannot afford the cost, this change will be rolled back
   231  			evm.StateDB.AddAddressToAccessList(address)
   232  			gas = params.ColdAccountAccessCostEIP2929
   233  		}
   234  		// if empty and transfers value
   235  		if evm.StateDB.Empty(address) && evm.StateDB.GetBalance(contract.Address()).Sign() != 0 {
   236  			gas += params.CreateBySelfdestructGas
   237  		}
   238  		if refundsEnabled && !evm.StateDB.HasSelfDestructed(contract.Address()) {
   239  			evm.StateDB.AddRefund(params.SelfdestructRefundGas)
   240  		}
   241  		return gas, nil
   242  	}
   243  	return gasFunc
   244  }