Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Commit

Permalink
[PAN-2966] - Implement EIP-2200 - Net Gas Metering Revised (#1743)
Browse files Browse the repository at this point in the history
Note that EIP-1706 was already implemented elsewhere and this is
just the gas calculation updates.
  • Loading branch information
Danno Ferrin authored Jul 23, 2019
1 parent ed57a9b commit 400045c
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 220 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,27 @@
*/
package tech.pegasys.pantheon.ethereum.mainnet;

import tech.pegasys.pantheon.ethereum.core.Account;
import tech.pegasys.pantheon.ethereum.core.Gas;
import tech.pegasys.pantheon.ethereum.core.Transaction;
import tech.pegasys.pantheon.util.bytes.BytesValue;
import tech.pegasys.pantheon.util.uint.UInt256;

public class IstanbulGasCalculator extends ConstantinopleFixGasCalculator {

private static final Gas TX_DATA_ZERO_COST = Gas.of(4L);
private static final Gas TX_DATA_NON_ZERO_COST = Gas.of(16L);
private static final Gas TX_BASE_COST = Gas.of(21_000L);

private static final Gas SLOAD_GAS = Gas.of(800);
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
public Gas transactionIntrinsicGasCost(final Transaction transaction) {
final BytesValue payload = transaction.getPayload();
Expand All @@ -45,4 +56,62 @@ public Gas transactionIntrinsicGasCost(final Transaction transaction) {

return cost;
}

@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;
}
}
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit 400045c

Please sign in to comment.