//1. If current value equals new value (this is a no-op), 200 gas is deducted.
//2. If current value does not equal new value
// 2.1 If original value equals current value (this storage slot has not been changed by the current execution context)
// 2.1.1 If original value is 0, 20000 gas is deducted.
// 2.1.2 Otherwise, 5000 gas is deducted. If new value is 0, add 15000 gas to refund counter.
// 2.2 If original value does not equal current value (this storage slot is dirty), 200 gas is deducted. Apply both of the following clauses.
// 2.2.1 If original value is not 0
// 2.2.1.1 If current value is 0 (also means that new value is not 0), remove 15000 gas from refund counter. We can prove that refund counter will never go below 0.
// 2.2.1.2 If new value is 0 (also means that current value is not 0), add 15000 gas to refund counter.
// 2.2.2 If original value equals new value (this storage slot is reset)
// 2.2.2.1 If original value is 0, add 19800 gas to refund counter.
// 2.2.2.2 Otherwise, add 4800 gas to refund counter.
// The legacy gas metering only takes into consideration the current state
if!evm.chainRules.IsConstantinople{
// This checks for 3 scenario's and calculates gas accordingly:
//
// 1. From a zero-value address to a non-zero value (NEW VALUE)
// 2. From a non-zero value address to a zero-value address (DELETE)
// 3. From a non-zero to a non-zero (CHANGE)
switch{
casecurrent==(common.Hash{})&&y.Sign()!=0:// 0 => non 0
returnparams.SstoreSetGas,nil
casecurrent!=(common.Hash{})&&y.Sign()==0:// non 0 => 0
evm.StateDB.AddRefund(params.SstoreRefundGas)
returnparams.SstoreClearGas,nil
default:// non 0 => non 0 (or 0 => 0)
returnparams.SstoreResetGas,nil
}
}
// The new gas metering is based on net gas costs (EIP-1283):
//
// 1. If current value equals new value (this is a no-op), 200 gas is deducted.
// 2. If current value does not equal new value
// 2.1. If original value equals current value (this storage slot has not been changed by the current execution context)
// 2.1.1. If original value is 0, 20000 gas is deducted.
// 2.1.2. Otherwise, 5000 gas is deducted. If new value is 0, add 15000 gas to refund counter.
// 2.2. If original value does not equal current value (this storage slot is dirty), 200 gas is deducted. Apply both of the following clauses.
// 2.2.1. If original value is not 0
// 2.2.1.1. If current value is 0 (also means that new value is not 0), remove 15000 gas from refund counter. We can prove that refund counter will never go below 0.
// 2.2.1.2. If new value is 0 (also means that current value is not 0), add 15000 gas to refund counter.
// 2.2.2. If original value equals new value (this storage slot is reset)
// 2.2.2.1. If original value is 0, add 19800 gas to refund counter.
// 2.2.2.2. Otherwise, add 4800 gas to refund counter.