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

Merged
merged 1 commit into from
Feb 10, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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