|
|
@ -24,25 +24,8 @@ import ( |
|
|
|
"github.com/ethereum/go-ethereum/params" |
|
|
|
"github.com/ethereum/go-ethereum/params" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
const ( |
|
|
|
func makeGasSStoreFunc(clearingRefund uint64) gasFunc { |
|
|
|
ColdAccountAccessCostEIP2929 = uint64(2600) // COLD_ACCOUNT_ACCESS_COST
|
|
|
|
return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { |
|
|
|
ColdSloadCostEIP2929 = uint64(2100) // COLD_SLOAD_COST
|
|
|
|
|
|
|
|
WarmStorageReadCostEIP2929 = uint64(100) // WARM_STORAGE_READ_COST
|
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// gasSStoreEIP2929 implements gas cost for SSTORE according to EIP-2929
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// When calling SSTORE, check if the (address, storage_key) pair is in accessed_storage_keys.
|
|
|
|
|
|
|
|
// If it is not, charge an additional COLD_SLOAD_COST gas, and add the pair to accessed_storage_keys.
|
|
|
|
|
|
|
|
// Additionally, modify the parameters defined in EIP 2200 as follows:
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// Parameter Old value New value
|
|
|
|
|
|
|
|
// SLOAD_GAS 800 = WARM_STORAGE_READ_COST
|
|
|
|
|
|
|
|
// SSTORE_RESET_GAS 5000 5000 - COLD_SLOAD_COST
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
//The other parameters defined in EIP 2200 are unchanged.
|
|
|
|
|
|
|
|
// see gasSStoreEIP2200(...) in core/vm/gas_table.go for more info about how EIP 2200 is specified
|
|
|
|
|
|
|
|
func gasSStoreEIP2929(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { |
|
|
|
|
|
|
|
// If we fail the minimum gas availability invariant, fail (0)
|
|
|
|
// If we fail the minimum gas availability invariant, fail (0)
|
|
|
|
if contract.Gas <= params.SstoreSentryGasEIP2200 { |
|
|
|
if contract.Gas <= params.SstoreSentryGasEIP2200 { |
|
|
|
return 0, errors.New("not enough gas for reentrancy sentry") |
|
|
|
return 0, errors.New("not enough gas for reentrancy sentry") |
|
|
@ -56,7 +39,7 @@ func gasSStoreEIP2929(evm *EVM, contract *Contract, stack *Stack, mem *Memory, m |
|
|
|
) |
|
|
|
) |
|
|
|
// Check slot presence in the access list
|
|
|
|
// Check slot presence in the access list
|
|
|
|
if addrPresent, slotPresent := evm.StateDB.SlotInAccessList(contract.Address(), slot); !slotPresent { |
|
|
|
if addrPresent, slotPresent := evm.StateDB.SlotInAccessList(contract.Address(), slot); !slotPresent { |
|
|
|
cost = ColdSloadCostEIP2929 |
|
|
|
cost = params.ColdSloadCostEIP2929 |
|
|
|
// If the caller cannot afford the cost, this change will be rolled back
|
|
|
|
// If the caller cannot afford the cost, this change will be rolled back
|
|
|
|
evm.StateDB.AddSlotToAccessList(contract.Address(), slot) |
|
|
|
evm.StateDB.AddSlotToAccessList(contract.Address(), slot) |
|
|
|
if !addrPresent { |
|
|
|
if !addrPresent { |
|
|
@ -71,7 +54,7 @@ func gasSStoreEIP2929(evm *EVM, contract *Contract, stack *Stack, mem *Memory, m |
|
|
|
if current == value { // noop (1)
|
|
|
|
if current == value { // noop (1)
|
|
|
|
// EIP 2200 original clause:
|
|
|
|
// EIP 2200 original clause:
|
|
|
|
// return params.SloadGasEIP2200, nil
|
|
|
|
// return params.SloadGasEIP2200, nil
|
|
|
|
return cost + WarmStorageReadCostEIP2929, nil // SLOAD_GAS
|
|
|
|
return cost + params.WarmStorageReadCostEIP2929, nil // SLOAD_GAS
|
|
|
|
} |
|
|
|
} |
|
|
|
original := evm.StateDB.GetCommittedState(contract.Address(), x.Bytes32()) |
|
|
|
original := evm.StateDB.GetCommittedState(contract.Address(), x.Bytes32()) |
|
|
|
if original == current { |
|
|
|
if original == current { |
|
|
@ -79,36 +62,37 @@ func gasSStoreEIP2929(evm *EVM, contract *Contract, stack *Stack, mem *Memory, m |
|
|
|
return cost + params.SstoreSetGasEIP2200, nil |
|
|
|
return cost + params.SstoreSetGasEIP2200, nil |
|
|
|
} |
|
|
|
} |
|
|
|
if value == (common.Hash{}) { // delete slot (2.1.2b)
|
|
|
|
if value == (common.Hash{}) { // delete slot (2.1.2b)
|
|
|
|
evm.StateDB.AddRefund(params.SstoreClearsScheduleRefundEIP2200) |
|
|
|
evm.StateDB.AddRefund(clearingRefund) |
|
|
|
} |
|
|
|
} |
|
|
|
// EIP-2200 original clause:
|
|
|
|
// EIP-2200 original clause:
|
|
|
|
// return params.SstoreResetGasEIP2200, nil // write existing slot (2.1.2)
|
|
|
|
// return params.SstoreResetGasEIP2200, nil // write existing slot (2.1.2)
|
|
|
|
return cost + (params.SstoreResetGasEIP2200 - ColdSloadCostEIP2929), nil // write existing slot (2.1.2)
|
|
|
|
return cost + (params.SstoreResetGasEIP2200 - params.ColdSloadCostEIP2929), nil // write existing slot (2.1.2)
|
|
|
|
} |
|
|
|
} |
|
|
|
if original != (common.Hash{}) { |
|
|
|
if original != (common.Hash{}) { |
|
|
|
if current == (common.Hash{}) { // recreate slot (2.2.1.1)
|
|
|
|
if current == (common.Hash{}) { // recreate slot (2.2.1.1)
|
|
|
|
evm.StateDB.SubRefund(params.SstoreClearsScheduleRefundEIP2200) |
|
|
|
evm.StateDB.SubRefund(clearingRefund) |
|
|
|
} else if value == (common.Hash{}) { // delete slot (2.2.1.2)
|
|
|
|
} else if value == (common.Hash{}) { // delete slot (2.2.1.2)
|
|
|
|
evm.StateDB.AddRefund(params.SstoreClearsScheduleRefundEIP2200) |
|
|
|
evm.StateDB.AddRefund(clearingRefund) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if original == value { |
|
|
|
if original == value { |
|
|
|
if original == (common.Hash{}) { // reset to original inexistent slot (2.2.2.1)
|
|
|
|
if original == (common.Hash{}) { // reset to original inexistent slot (2.2.2.1)
|
|
|
|
// EIP 2200 Original clause:
|
|
|
|
// EIP 2200 Original clause:
|
|
|
|
//evm.StateDB.AddRefund(params.SstoreSetGasEIP2200 - params.SloadGasEIP2200)
|
|
|
|
//evm.StateDB.AddRefund(params.SstoreSetGasEIP2200 - params.SloadGasEIP2200)
|
|
|
|
evm.StateDB.AddRefund(params.SstoreSetGasEIP2200 - WarmStorageReadCostEIP2929) |
|
|
|
evm.StateDB.AddRefund(params.SstoreSetGasEIP2200 - params.WarmStorageReadCostEIP2929) |
|
|
|
} else { // reset to original existing slot (2.2.2.2)
|
|
|
|
} else { // reset to original existing slot (2.2.2.2)
|
|
|
|
// EIP 2200 Original clause:
|
|
|
|
// EIP 2200 Original clause:
|
|
|
|
// evm.StateDB.AddRefund(params.SstoreResetGasEIP2200 - params.SloadGasEIP2200)
|
|
|
|
// evm.StateDB.AddRefund(params.SstoreResetGasEIP2200 - params.SloadGasEIP2200)
|
|
|
|
// - SSTORE_RESET_GAS redefined as (5000 - COLD_SLOAD_COST)
|
|
|
|
// - SSTORE_RESET_GAS redefined as (5000 - COLD_SLOAD_COST)
|
|
|
|
// - SLOAD_GAS redefined as WARM_STORAGE_READ_COST
|
|
|
|
// - SLOAD_GAS redefined as WARM_STORAGE_READ_COST
|
|
|
|
// Final: (5000 - COLD_SLOAD_COST) - WARM_STORAGE_READ_COST
|
|
|
|
// Final: (5000 - COLD_SLOAD_COST) - WARM_STORAGE_READ_COST
|
|
|
|
evm.StateDB.AddRefund((params.SstoreResetGasEIP2200 - ColdSloadCostEIP2929) - WarmStorageReadCostEIP2929) |
|
|
|
evm.StateDB.AddRefund((params.SstoreResetGasEIP2200 - params.ColdSloadCostEIP2929) - params.WarmStorageReadCostEIP2929) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
// EIP-2200 original clause:
|
|
|
|
// EIP-2200 original clause:
|
|
|
|
//return params.SloadGasEIP2200, nil // dirty update (2.2)
|
|
|
|
//return params.SloadGasEIP2200, nil // dirty update (2.2)
|
|
|
|
return cost + WarmStorageReadCostEIP2929, nil // dirty update (2.2)
|
|
|
|
return cost + params.WarmStorageReadCostEIP2929, nil // dirty update (2.2)
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// gasSLoadEIP2929 calculates dynamic gas for SLOAD according to EIP-2929
|
|
|
|
// gasSLoadEIP2929 calculates dynamic gas for SLOAD according to EIP-2929
|
|
|
@ -124,9 +108,9 @@ func gasSLoadEIP2929(evm *EVM, contract *Contract, stack *Stack, mem *Memory, me |
|
|
|
// If the caller cannot afford the cost, this change will be rolled back
|
|
|
|
// If the caller cannot afford the cost, this change will be rolled back
|
|
|
|
// If he does afford it, we can skip checking the same thing later on, during execution
|
|
|
|
// If he does afford it, we can skip checking the same thing later on, during execution
|
|
|
|
evm.StateDB.AddSlotToAccessList(contract.Address(), slot) |
|
|
|
evm.StateDB.AddSlotToAccessList(contract.Address(), slot) |
|
|
|
return ColdSloadCostEIP2929, nil |
|
|
|
return params.ColdSloadCostEIP2929, nil |
|
|
|
} |
|
|
|
} |
|
|
|
return WarmStorageReadCostEIP2929, nil |
|
|
|
return params.WarmStorageReadCostEIP2929, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// gasExtCodeCopyEIP2929 implements extcodecopy according to EIP-2929
|
|
|
|
// gasExtCodeCopyEIP2929 implements extcodecopy according to EIP-2929
|
|
|
@ -146,7 +130,7 @@ func gasExtCodeCopyEIP2929(evm *EVM, contract *Contract, stack *Stack, mem *Memo |
|
|
|
evm.StateDB.AddAddressToAccessList(addr) |
|
|
|
evm.StateDB.AddAddressToAccessList(addr) |
|
|
|
var overflow bool |
|
|
|
var overflow bool |
|
|
|
// We charge (cold-warm), since 'warm' is already charged as constantGas
|
|
|
|
// We charge (cold-warm), since 'warm' is already charged as constantGas
|
|
|
|
if gas, overflow = math.SafeAdd(gas, ColdAccountAccessCostEIP2929-WarmStorageReadCostEIP2929); overflow { |
|
|
|
if gas, overflow = math.SafeAdd(gas, params.ColdAccountAccessCostEIP2929-params.WarmStorageReadCostEIP2929); overflow { |
|
|
|
return 0, ErrGasUintOverflow |
|
|
|
return 0, ErrGasUintOverflow |
|
|
|
} |
|
|
|
} |
|
|
|
return gas, nil |
|
|
|
return gas, nil |
|
|
@ -168,7 +152,7 @@ func gasEip2929AccountCheck(evm *EVM, contract *Contract, stack *Stack, mem *Mem |
|
|
|
// If the caller cannot afford the cost, this change will be rolled back
|
|
|
|
// If the caller cannot afford the cost, this change will be rolled back
|
|
|
|
evm.StateDB.AddAddressToAccessList(addr) |
|
|
|
evm.StateDB.AddAddressToAccessList(addr) |
|
|
|
// The warm storage read cost is already charged as constantGas
|
|
|
|
// The warm storage read cost is already charged as constantGas
|
|
|
|
return ColdAccountAccessCostEIP2929 - WarmStorageReadCostEIP2929, nil |
|
|
|
return params.ColdAccountAccessCostEIP2929 - params.WarmStorageReadCostEIP2929, nil |
|
|
|
} |
|
|
|
} |
|
|
|
return 0, nil |
|
|
|
return 0, nil |
|
|
|
} |
|
|
|
} |
|
|
@ -180,7 +164,7 @@ func makeCallVariantGasCallEIP2929(oldCalculator gasFunc) gasFunc { |
|
|
|
warmAccess := evm.StateDB.AddressInAccessList(addr) |
|
|
|
warmAccess := evm.StateDB.AddressInAccessList(addr) |
|
|
|
// The WarmStorageReadCostEIP2929 (100) is already deducted in the form of a constant cost, so
|
|
|
|
// The WarmStorageReadCostEIP2929 (100) is already deducted in the form of a constant cost, so
|
|
|
|
// the cost to charge for cold access, if any, is Cold - Warm
|
|
|
|
// the cost to charge for cold access, if any, is Cold - Warm
|
|
|
|
coldCost := ColdAccountAccessCostEIP2929 - WarmStorageReadCostEIP2929 |
|
|
|
coldCost := params.ColdAccountAccessCostEIP2929 - params.WarmStorageReadCostEIP2929 |
|
|
|
if !warmAccess { |
|
|
|
if !warmAccess { |
|
|
|
evm.StateDB.AddAddressToAccessList(addr) |
|
|
|
evm.StateDB.AddAddressToAccessList(addr) |
|
|
|
// Charge the remaining difference here already, to correctly calculate available
|
|
|
|
// Charge the remaining difference here already, to correctly calculate available
|
|
|
@ -212,9 +196,32 @@ var ( |
|
|
|
gasDelegateCallEIP2929 = makeCallVariantGasCallEIP2929(gasDelegateCall) |
|
|
|
gasDelegateCallEIP2929 = makeCallVariantGasCallEIP2929(gasDelegateCall) |
|
|
|
gasStaticCallEIP2929 = makeCallVariantGasCallEIP2929(gasStaticCall) |
|
|
|
gasStaticCallEIP2929 = makeCallVariantGasCallEIP2929(gasStaticCall) |
|
|
|
gasCallCodeEIP2929 = makeCallVariantGasCallEIP2929(gasCallCode) |
|
|
|
gasCallCodeEIP2929 = makeCallVariantGasCallEIP2929(gasCallCode) |
|
|
|
|
|
|
|
gasSelfdestructEIP2929 = makeSelfdestructGasFn(true) |
|
|
|
|
|
|
|
// gasSelfdestructEIP3529 implements the changes in EIP-2539 (no refunds)
|
|
|
|
|
|
|
|
gasSelfdestructEIP3529 = makeSelfdestructGasFn(false) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// gasSStoreEIP2929 implements gas cost for SSTORE according to EIP-2929
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// When calling SSTORE, check if the (address, storage_key) pair is in accessed_storage_keys.
|
|
|
|
|
|
|
|
// If it is not, charge an additional COLD_SLOAD_COST gas, and add the pair to accessed_storage_keys.
|
|
|
|
|
|
|
|
// Additionally, modify the parameters defined in EIP 2200 as follows:
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// Parameter Old value New value
|
|
|
|
|
|
|
|
// SLOAD_GAS 800 = WARM_STORAGE_READ_COST
|
|
|
|
|
|
|
|
// SSTORE_RESET_GAS 5000 5000 - COLD_SLOAD_COST
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
//The other parameters defined in EIP 2200 are unchanged.
|
|
|
|
|
|
|
|
// see gasSStoreEIP2200(...) in core/vm/gas_table.go for more info about how EIP 2200 is specified
|
|
|
|
|
|
|
|
gasSStoreEIP2929 = makeGasSStoreFunc(params.SstoreClearsScheduleRefundEIP2200) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// gasSStoreEIP2539 implements gas cost for SSTORE according to EPI-2539
|
|
|
|
|
|
|
|
// Replace `SSTORE_CLEARS_SCHEDULE` with `SSTORE_RESET_GAS + ACCESS_LIST_STORAGE_KEY_COST` (4,800)
|
|
|
|
|
|
|
|
gasSStoreEIP3529 = makeGasSStoreFunc(params.SstoreClearsScheduleRefundEIP3529) |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
func gasSelfdestructEIP2929(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { |
|
|
|
// makeSelfdestructGasFn can create the selfdestruct dynamic gas function for EIP-2929 and EIP-2539
|
|
|
|
|
|
|
|
func makeSelfdestructGasFn(refundsEnabled bool) gasFunc { |
|
|
|
|
|
|
|
gasFunc := func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { |
|
|
|
var ( |
|
|
|
var ( |
|
|
|
gas uint64 |
|
|
|
gas uint64 |
|
|
|
address = common.Address(stack.peek().Bytes20()) |
|
|
|
address = common.Address(stack.peek().Bytes20()) |
|
|
@ -222,15 +229,16 @@ func gasSelfdestructEIP2929(evm *EVM, contract *Contract, stack *Stack, mem *Mem |
|
|
|
if !evm.StateDB.AddressInAccessList(address) { |
|
|
|
if !evm.StateDB.AddressInAccessList(address) { |
|
|
|
// If the caller cannot afford the cost, this change will be rolled back
|
|
|
|
// If the caller cannot afford the cost, this change will be rolled back
|
|
|
|
evm.StateDB.AddAddressToAccessList(address) |
|
|
|
evm.StateDB.AddAddressToAccessList(address) |
|
|
|
gas = ColdAccountAccessCostEIP2929 |
|
|
|
gas = params.ColdAccountAccessCostEIP2929 |
|
|
|
} |
|
|
|
} |
|
|
|
// if empty and transfers value
|
|
|
|
// if empty and transfers value
|
|
|
|
if evm.StateDB.Empty(address) && evm.StateDB.GetBalance(contract.Address()).Sign() != 0 { |
|
|
|
if evm.StateDB.Empty(address) && evm.StateDB.GetBalance(contract.Address()).Sign() != 0 { |
|
|
|
gas += params.CreateBySelfdestructGas |
|
|
|
gas += params.CreateBySelfdestructGas |
|
|
|
} |
|
|
|
} |
|
|
|
if !evm.StateDB.HasSuicided(contract.Address()) { |
|
|
|
if refundsEnabled && !evm.StateDB.HasSuicided(contract.Address()) { |
|
|
|
evm.StateDB.AddRefund(params.SelfdestructRefundGas) |
|
|
|
evm.StateDB.AddRefund(params.SelfdestructRefundGas) |
|
|
|
} |
|
|
|
} |
|
|
|
return gas, nil |
|
|
|
return gas, nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return gasFunc |
|
|
|
} |
|
|
|
} |
|
|
|