Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SLOAD_GAS cost to 200 in Aztlan Gas Calculator (#23) #382

Merged
merged 9 commits into from
Feb 20, 2020
Prev Previous commit
Next Next commit
Update SLOAD_GAS cost to 200 in Aztlan Gas Calculator
Change SLOAD_GAS cost in Aztlan Gas Calculator from 800 to 200 and
update functions that use SLOAD_GAS.

Signed-off-by: edwardmack <ed@edwardmack.com>
Signed-off-by: Edward Mack <ed@edwardmack.com>
  • Loading branch information
edwardmack committed Feb 10, 2020
commit e070eb44358010a92863aeabb346a461e696bba8
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,90 @@
*/
package org.hyperledger.besu.ethereum.mainnet;

import org.hyperledger.besu.ethereum.core.Account;
import org.hyperledger.besu.ethereum.core.Gas;

import org.apache.tuweni.units.bigints.UInt256;

/*
* This class implements changes to gas calculation for ECIP-1061 https://ecips.ethereumclassic.org/ECIPs/ecip-1061
* ECIP-1061 is EIP-1679 without EIP-1884 implemented, this class reverses the changes made in EIP-1884.
*/
public class AztlanGasCalculator extends IstanbulGasCalculator {
private static final Gas BALANCE_OPERATION_GAS_COST = Gas.of(400);
private static final Gas EXTCODE_HASH_COST = Gas.of(400);
private static final Gas SLOAD_GAS = Gas.of(200);
private static final Gas SSTORE_SET_GAS = Gas.of(20_000);
private static final Gas SSTORE_RESET_GAS = Gas.of(5_000);
private static final Gas SSTORE_CLEARS_SCHEDULE = Gas.of(15_000);

private static final Gas SSTORE_SET_GAS_LESS_SLOAD_GAS = SSTORE_SET_GAS.minus(SLOAD_GAS);
private static final Gas SSTORE_RESET_GAS_LESS_SLOAD_GAS = SSTORE_RESET_GAS.minus(SLOAD_GAS);
private static final Gas NEGATIVE_SSTORE_CLEARS_SCHEDULE = Gas.ZERO.minus(SSTORE_CLEARS_SCHEDULE);

@Override
// As per https://eips.ethereum.org/EIPS/eip-2200
public Gas calculateStorageCost(
final Account account, final UInt256 key, final UInt256 newValue) {

final UInt256 currentValue = account.getStorageValue(key);
if (currentValue.equals(newValue)) {
return SLOAD_GAS;
} else {
final UInt256 originalValue = account.getOriginalStorageValue(key);
if (originalValue.equals(currentValue)) {
return originalValue.isZero() ? SSTORE_SET_GAS : SSTORE_RESET_GAS;
} else {
return SLOAD_GAS;
}
}
}

@Override
// As per https://eips.ethereum.org/EIPS/eip-2200
public Gas calculateStorageRefundAmount(
final Account account, final UInt256 key, final UInt256 newValue) {

final UInt256 currentValue = account.getStorageValue(key);
if (currentValue.equals(newValue)) {
return Gas.ZERO;
} else {
final UInt256 originalValue = account.getOriginalStorageValue(key);
if (originalValue.equals(currentValue)) {
if (originalValue.isZero()) {
return Gas.ZERO;
} else if (newValue.isZero()) {
return SSTORE_CLEARS_SCHEDULE;
} else {
return Gas.ZERO;
}
} else {
Gas refund = Gas.ZERO;
if (!originalValue.isZero()) {
if (currentValue.isZero()) {
refund = NEGATIVE_SSTORE_CLEARS_SCHEDULE;
} else if (newValue.isZero()) {
refund = SSTORE_CLEARS_SCHEDULE;
}
}

if (originalValue.equals(newValue)) {
refund =
refund.plus(
originalValue.isZero()
? SSTORE_SET_GAS_LESS_SLOAD_GAS
: SSTORE_RESET_GAS_LESS_SLOAD_GAS);
}
return refund;
}
}
}

@Override
// As per https://eips.ethereum.org/EIPS/eip-1884
public Gas getSloadOperationGasCost() {
return SLOAD_GAS;
}

@Override
public Gas getBalanceOperationGasCost() {
Expand Down