Skip to content
Open
Show file tree
Hide file tree
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 @@ -145,7 +145,7 @@ public long getSloadOperationGasCost() {

// Redefined costs from EIP-2929
@Override
public long callOperationGasCost(
public long callOperationStaticGasCost(
final MessageFrame frame,
final long stipend,
final long inputDataOffset,
Expand All @@ -156,7 +156,7 @@ public long callOperationGasCost(
final Address recipientAddress,
final boolean accountIsWarm) {
final long baseCost =
super.callOperationGasCost(
super.callOperationStaticGasCost(
frame,
stipend,
inputDataOffset,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ public long newAccountGasCost() {
}

@Override
public long callOperationGasCost(
public long callOperationStaticGasCost(
final MessageFrame frame,
final long stipend,
final long inputDataOffset,
Expand All @@ -308,6 +308,23 @@ public long callOperationGasCost(
cost = clampedAdd(cost, callValueTransferGasCost());
}

return cost;
}

@Override
public long callOperationGasCost(
final MessageFrame frame,
final long staticCallCost,
final long stipend,
final long inputDataOffset,
final long inputDataLength,
final long outputDataOffset,
final long outputDataLength,
final Wei transferValue,
final Address recipientAddress,
final boolean accountIsWarm) {
long cost = staticCallCost;

final Account recipient = frame.getWorldUpdater().get(recipientAddress);
if (recipient == null) {
cost = clampedAdd(cost, newAccountGasCost());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,10 @@ public interface GasCalculator {
long newAccountGasCost();

/**
* Returns the gas cost for one of the various CALL operations.
* Returns the total call cost given a pre-computed static call cost.
*
* @param frame The current frame
* @param staticCallCost The static call cost
* @param stipend The gas stipend being provided by the CALL caller
* @param inputDataOffset The offset in memory to retrieve the CALL input data
* @param inputDataLength The CALL input data length
Expand All @@ -181,15 +182,44 @@ public interface GasCalculator {
* @return The gas cost for the CALL operation
*/
long callOperationGasCost(
MessageFrame frame,
long stipend,
long inputDataOffset,
long inputDataLength,
long outputDataOffset,
long outputDataLength,
Wei transferValue,
Address recipientAddress,
boolean accountIsWarm);
final MessageFrame frame,
final long staticCallCost,
final long stipend,
final long inputDataOffset,
final long inputDataLength,
final long outputDataOffset,
final long outputDataLength,
final Wei transferValue,
final Address recipientAddress,
final boolean accountIsWarm);

/**
* Returns the static gas cost to execute a call operation.
*
* <p>This method <strong>must not</strong> access or mutate world state and it must not mutate
* {@code frame.getEip7928AccessList()}.
*
* @param frame The current frame
* @param stipend The gas stipend being provided by the CALL caller
* @param inputDataOffset The offset in memory to retrieve the CALL input data
* @param inputDataLength The CALL input data length
* @param outputDataOffset The offset in memory to place the CALL output data
* @param outputDataLength The CALL output data length
* @param transferValue The wei being transferred
* @param recipientAddress The CALL recipient (may be null if self destructed or new) address
* @param accountIsWarm The address of the contract is "warm" as per EIP-2929
* @return The static gas cost for the CALL operation
*/
long callOperationStaticGasCost(
final MessageFrame frame,
final long stipend,
final long inputDataOffset,
final long inputDataLength,
final long outputDataOffset,
final long outputDataLength,
final Wei transferValue,
final Address recipientAddress,
final boolean accountIsWarm);

/**
* Gets additional call stipend.
Expand Down Expand Up @@ -377,15 +407,15 @@ long callOperationGasCost(
* @return the cost for executing the self destruct operation
*/
default long selfDestructOperationGasCost(final Account recipient, final Wei inheritance) {
return selfDestructOperationBaseGasCost();
return selfDestructOperationStaticGasCost();
}

/**
* Returns the base cost for executing a {@link SelfDestructOperation}.
* Returns the static cost for executing a {@link SelfDestructOperation}.
*
* @return the base cost for executing a {@link SelfDestructOperation}
*/
default long selfDestructOperationBaseGasCost() {
default long selfDestructOperationStaticGasCost() {
return 0L;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class SpuriousDragonGasCalculator extends TangerineWhistleGasCalculator {
public SpuriousDragonGasCalculator() {}

@Override
public long callOperationGasCost(
public long callOperationStaticGasCost(
final MessageFrame frame,
final long stipend,
final long inputDataOffset,
Expand All @@ -49,16 +49,34 @@ public long callOperationGasCost(

long cost = clampedAdd(callOperationBaseGasCost(), memoryExpansionCost);

final boolean isTransferValueZero = transferValue.isZero();

if (!isTransferValueZero) {
if (!transferValue.isZero()) {
cost = clampedAdd(cost, callValueTransferGasCost());
}

return cost;
}

@Override
public long callOperationGasCost(
final MessageFrame frame,
final long staticCallCost,
final long stipend,
final long inputDataOffset,
final long inputDataLength,
final long outputDataOffset,
final long outputDataLength,
final Wei transferValue,
final Address recipientAddress,
final boolean accountIsWarm) {
if (transferValue.isZero()) {
return staticCallCost;
}

final Account recipient = frame.getWorldUpdater().get(recipientAddress);
frame.getEip7928AccessList().ifPresent(t -> t.addTouchedAccount(recipientAddress));
if (recipient == null || recipient.isEmpty()) {
cost = clampedAdd(cost, newAccountGasCost());
}
long cost = staticCallCost;
final Account recipient = frame.getWorldUpdater().get(recipientAddress);
frame.getEip7928AccessList().ifPresent(t -> t.addTouchedAccount(recipientAddress));
if (recipient == null || recipient.isEmpty()) {
cost = clampedAdd(cost, newAccountGasCost());
}

return cost;
Expand All @@ -74,7 +92,7 @@ public long selfDestructOperationGasCost(final Account recipient, final Wei inhe
if ((recipient == null || recipient.isEmpty()) && !inheritance.isZero()) {
return SELFDESTRUCT_OPERATION_CREATES_NEW_ACCOUNT;
} else {
return selfDestructOperationBaseGasCost();
return selfDestructOperationStaticGasCost();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public long callOperationBaseGasCost() {
}

@Override
public long callOperationGasCost(
public long callOperationStaticGasCost(
final MessageFrame frame,
final long stipend,
final long inputDataOffset,
Expand All @@ -81,6 +81,23 @@ public long callOperationGasCost(
cost = clampedAdd(cost, callValueTransferGasCost());
}

return cost;
}

@Override
public long callOperationGasCost(
final MessageFrame frame,
final long staticCallCost,
final long stipend,
final long inputDataOffset,
final long inputDataLength,
final long outputDataOffset,
final long outputDataLength,
final Wei transferValue,
final Address recipientAddress,
final boolean accountIsWarm) {
long cost = staticCallCost;

final Account recipient = frame.getWorldUpdater().get(recipientAddress);
if (recipient == null) {
cost = clampedAdd(cost, newAccountGasCost());
Expand Down Expand Up @@ -124,12 +141,12 @@ public long selfDestructOperationGasCost(final Account recipient, final Wei inhe
if (recipient == null) {
return SELFDESTRUCT_OPERATION_CREATES_NEW_ACCOUNT;
} else {
return selfDestructOperationBaseGasCost();
return selfDestructOperationStaticGasCost();
}
}

@Override
public long selfDestructOperationBaseGasCost() {
public long selfDestructOperationStaticGasCost() {
return SELFDESTRUCT_OPERATION_GAS_COST;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,44 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) {

final Address to = to(frame);
final boolean accountIsWarm = frame.warmUpAddress(to) || gasCalculator().isPrecompile(to);
long cost = cost(frame, accountIsWarm);
final long stipend = gas(frame);
final long inputDataOffset = inputDataOffset(frame);
final long inputDataLength = inputDataLength(frame);
final long outputDataOffset = outputDataOffset(frame);
final long outputDataLength = outputDataLength(frame);
final Wei transferValue = value(frame);
final Address recipientAddress = address(frame);

final long staticCost =
gasCalculator()
.callOperationStaticGasCost(
frame,
stipend,
inputDataOffset,
inputDataLength,
outputDataOffset,
outputDataLength,
transferValue,
recipientAddress,
accountIsWarm);

if (frame.getRemainingGas() < staticCost) {
return new OperationResult(staticCost, ExceptionalHaltReason.INSUFFICIENT_GAS);
}

long cost =
gasCalculator()
.callOperationGasCost(
frame,
staticCost,
stipend,
inputDataOffset,
inputDataLength,
outputDataOffset,
outputDataLength,
transferValue,
recipientAddress,
accountIsWarm);
if (frame.getRemainingGas() < cost) {
return new OperationResult(cost, ExceptionalHaltReason.INSUFFICIENT_GAS);
}
Expand Down Expand Up @@ -255,34 +292,6 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) {
return new OperationResult(cost, null, 0);
}

/**
* Calculates Cost.
*
* @param frame the frame
* @param accountIsWarm whether the contract being called is "warm" as per EIP-2929.
* @return the long
*/
public long cost(final MessageFrame frame, final boolean accountIsWarm) {
final long stipend = gas(frame);
final long inputDataOffset = inputDataOffset(frame);
final long inputDataLength = inputDataLength(frame);
final long outputDataOffset = outputDataOffset(frame);
final long outputDataLength = outputDataLength(frame);
final Address recipientAddress = address(frame);
GasCalculator gasCalculator = gasCalculator();

return gasCalculator.callOperationGasCost(
frame,
stipend,
inputDataOffset,
inputDataLength,
outputDataOffset,
outputDataLength,
value(frame),
recipientAddress,
accountIsWarm);
}

/**
* Complete.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) {
frame.warmUpAddress(beneficiaryAddress) || gasCalculator().isPrecompile(beneficiaryAddress);
final long beneficiaryAccessCost =
beneficiaryIsWarm ? 0L : gasCalculator().getColdAccountAccessCost();
final long baseCost =
gasCalculator().selfDestructOperationBaseGasCost() + beneficiaryAccessCost;
final long staticCost =
gasCalculator().selfDestructOperationStaticGasCost() + beneficiaryAccessCost;

if (frame.getRemainingGas() < baseCost) {
return new OperationResult(baseCost, ExceptionalHaltReason.INSUFFICIENT_GAS);
if (frame.getRemainingGas() < staticCost) {
return new OperationResult(staticCost, ExceptionalHaltReason.INSUFFICIENT_GAS);
}

final Account beneficiaryNullable = getAccount(beneficiaryAddress, frame);
Expand Down