Skip to content

Commit

Permalink
fixup! evm halt cases do not need to report gas cost in OperationResult
Browse files Browse the repository at this point in the history
 allow for gasCost reporting in the case of InsufficientGas

Signed-off-by: Luis Pinto <[email protected]>
  • Loading branch information
lu-pinto committed Nov 27, 2024
1 parent 104f16d commit 073356f
Show file tree
Hide file tree
Showing 43 changed files with 73 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@
"sub": null
},
{
"cost": 0,
"cost": 30000,
"ex": null,
"pc": 21,
"sub": null
Expand Down Expand Up @@ -411,7 +411,7 @@
"sub": null
},
{
"cost": 0,
"cost": 30000,
"ex": null,
"pc": 21,
"sub": null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
"sub": null
},
{
"cost": 0,
"cost": 30000,
"ex": null,
"pc": 21,
"sub": null
Expand Down Expand Up @@ -320,7 +320,7 @@
"sub": null
},
{
"cost": 0,
"cost": 30000,
"ex": null,
"pc": 21,
"sub": null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
"pc" : 0,
"sub" : null
}, {
"cost" : 0,
"cost" : 30000,
"ex" : null,
"pc" : 21,
"sub" : null
Expand Down Expand Up @@ -255,7 +255,7 @@
"pc" : 0,
"sub" : null
}, {
"cost" : 0,
"cost" : 30000,
"ex" : null,
"pc" : 21,
"sub" : null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
"pc" : 9,
"sub" : null
}, {
"cost" : 0,
"cost" : 32009,
"ex" : null,
"pc" : 11,
"sub" : null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
"pc" : 0,
"sub" : null
}, {
"cost" : 0,
"cost" : 30000,
"ex" : null,
"pc" : 21,
"sub" : null
Expand Down Expand Up @@ -247,7 +247,7 @@
"pc" : 0,
"sub" : null
}, {
"cost" : 0,
"cost" : 30000,
"ex" : null,
"pc" : 21,
"sub" : null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
"pc" : 9,
"sub" : null
}, {
"cost" : 0,
"cost" : 32009,
"ex" : null,
"pc" : 11,
"sub" : null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ void shouldCaptureFrameWhenExceptionalHaltOccurs() {

final DebugOperationTracer tracer =
new DebugOperationTracer(new TraceOptions(true, true, true), false);
tracer.tracePostExecution(frame, OperationResult.insufficientGas());
tracer.tracePostExecution(frame, OperationResult.insufficientGas(50L));

final TraceFrame traceFrame = getOnlyTraceFrame(tracer);
assertThat(traceFrame.getExceptionalHaltReason())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
{"pc":3,"op":83,"gas":"0x8c6","gasCost":"0xc","memSize":0,"stack":["0x40","0x40"],"depth":4,"refund":0,"opName":"MSTORE8"},
{"pc":4,"op":96,"gas":"0x8ba","gasCost":"0x3","memSize":96,"stack":[],"depth":4,"refund":0,"opName":"PUSH1"},
{"pc":6,"op":96,"gas":"0x8b7","gasCost":"0x3","memSize":96,"stack":["0x40"],"depth":4,"refund":0,"opName":"PUSH1"},
{"pc":8,"op":85,"gas":"0x8b4","gasCost":"0x0","memSize":96,"stack":["0x40","0x40"],"depth":4,"refund":0,"opName":"SSTORE","error":"Out of gas"},
{"pc":8,"op":85,"gas":"0x8b4","gasCost":"0x8fc","memSize":96,"stack":["0x40","0x40"],"depth":4,"refund":0,"opName":"SSTORE","error":"Out of gas"},
{"pc":22,"op":96,"gas":"0x23","gasCost":"0x3","memSize":96,"stack":["0x0"],"depth":3,"refund":0,"opName":"PUSH1"},
{"pc":24,"op":243,"gas":"0x20","gasCost":"0x0","memSize":96,"stack":["0x0","0x40"],"depth":3,"refund":0,"opName":"RETURN"},
{"pc":22,"op":96,"gas":"0x48","gasCost":"0x3","memSize":96,"stack":["0x1"],"depth":2,"refund":0,"opName":"PUSH1"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
{"pc":3,"op":83,"gas":"0x8c6","gasCost":"0xc","memSize":0,"stack":["0x40","0x40"],"depth":4,"refund":0,"opName":"MSTORE8"},
{"pc":4,"op":96,"gas":"0x8ba","gasCost":"0x3","memSize":96,"stack":[],"depth":4,"refund":0,"opName":"PUSH1"},
{"pc":6,"op":96,"gas":"0x8b7","gasCost":"0x3","memSize":96,"stack":["0x40"],"depth":4,"refund":0,"opName":"PUSH1"},
{"pc":8,"op":85,"gas":"0x8b4","gasCost":"0x0","memSize":96,"stack":["0x40","0x40"],"depth":4,"refund":0,"opName":"SSTORE","error":"Out of gas"},
{"pc":8,"op":85,"gas":"0x8b4","gasCost":"0x8fc","memSize":96,"stack":["0x40","0x40"],"depth":4,"refund":0,"opName":"SSTORE","error":"Out of gas"},
{"pc":22,"op":96,"gas":"0x23","gasCost":"0x3","memSize":96,"stack":["0x0"],"depth":3,"refund":0,"opName":"PUSH1"},
{"pc":24,"op":243,"gas":"0x20","gasCost":"0x0","memSize":96,"stack":["0x0","0x40"],"depth":3,"refund":0,"opName":"RETURN"},
{"pc":22,"op":96,"gas":"0x48","gasCost":"0x3","memSize":96,"stack":["0x1"],"depth":2,"refund":0,"opName":"PUSH1"},
Expand Down
1 change: 0 additions & 1 deletion evm/src/main/java/org/hyperledger/besu/evm/EVM.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@ public void runToHalt(final MessageFrame frame, final OperationTracer tracing) {
case 0x0a -> ExpOperation.staticOperation(frame, gasCalculator);
case 0x0b -> SignExtendOperation.staticOperation(frame);
case 0x0c, 0x0d, 0x0e, 0x0f -> OperationResult.invalidOperation();

case 0x10 -> LtOperation.staticOperation(frame);
case 0x11 -> GtOperation.staticOperation(frame);
case 0x12 -> SLtOperation.staticOperation(frame);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) {
final boolean accountIsWarm = frame.warmUpAddress(to) || gasCalculator().isPrecompile(to);
final long cost = cost(frame, accountIsWarm);
if (frame.getRemainingGas() < cost) {
return OperationResult.insufficientGas();
return OperationResult.insufficientGas(cost);
}
frame.decrementRemainingGas(cost);

Expand All @@ -191,7 +191,7 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) {
final DelegatedCodeGasCostHelper.Result result =
deductDelegatedCodeGasCost(frame, gasCalculator(), contract);
if (result.status() != DelegatedCodeGasCostHelper.Status.SUCCESS) {
return OperationResult.insufficientGas();
return OperationResult.insufficientGas(result.gasCost());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) {
if (frame.isStatic()) {
return OperationResult.illegalStateChange();
} else if (frame.getRemainingGas() < cost) {
return OperationResult.insufficientGas();
return OperationResult.insufficientGas(cost);
}
final Wei value = Wei.wrap(frame.getStackItem(0));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) {
final DelegatedCodeGasCostHelper.Result result =
deductDelegatedCodeGasCost(frame, gasCalculator, contract);
if (result.status() != DelegatedCodeGasCostHelper.Status.SUCCESS) {
return OperationResult.insufficientGas();
return OperationResult.insufficientGas(result.gasCost());
}
}

Expand All @@ -136,7 +136,7 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) {
+ (accountCreation ? gasCalculator.newAccountGasCost() : 0);
long currentGas = frame.getRemainingGas() - cost;
if (currentGas < 0) {
return OperationResult.insufficientGas();
return OperationResult.insufficientGas(cost);
}

final Code code =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected AbstractFixedCostOperation(
@Override
public final OperationResult execute(final MessageFrame frame, final EVM evm) {
if (frame.getRemainingGas() < gasCost) {
return OperationResult.insufficientGas();
return OperationResult.insufficientGas(gasCost);
} else {
return executeFixedCostOperation(frame, evm);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) {
frame.warmUpAddress(address) || gasCalculator().isPrecompile(address);
final long cost = cost(accountIsWarm);
if (frame.getRemainingGas() < cost) {
return OperationResult.insufficientGas();
return OperationResult.insufficientGas(cost);
} else {
final Account account = frame.getWorldUpdater().get(address);
frame.pushStackItem(account == null ? Bytes.EMPTY : account.getBalance());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) {

final long cost = gasCalculator().dataCopyOperationGasCost(frame, memOffset, numBytes);
if (frame.getRemainingGas() < cost) {
return OperationResult.insufficientGas();
return OperationResult.insufficientGas(cost);
}

final Bytes callData = frame.getInputData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) {

final long cost = gasCalculator().dataCopyOperationGasCost(frame, memOffset, numBytes);
if (frame.getRemainingGas() < cost) {
return OperationResult.insufficientGas();
return OperationResult.insufficientGas(cost);
}

final Code code = frame.getCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) {
final int length = clampedToInt(frame.popStackItem());
final long cost = cost(frame, memOffset, length);
if (cost > frame.getRemainingGas()) {
return OperationResult.insufficientGas();
return OperationResult.insufficientGas(cost);
}

final Bytes data = code.getData(sourceOffset, length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static OperationResult staticOperation(

final long cost = gasCalculator.expOperationGasCost(numBytes);
if (frame.getRemainingGas() < cost) {
return OperationResult.insufficientGas();
return OperationResult.insufficientGas(cost);
}

byte[] numberBytes = number.toArrayUnsafe();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) {
final long cost = cost(frame, memOffset, numBytes, accountIsWarm);

if (frame.getRemainingGas() < cost) {
return OperationResult.insufficientGas();
return OperationResult.insufficientGas(cost);
}

final Account account = frame.getWorldUpdater().get(address);
Expand All @@ -99,7 +99,7 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) {
final DelegatedCodeGasCostHelper.Result result =
deductDelegatedCodeGasCost(frame, gasCalculator(), account);
if (result.status() != DelegatedCodeGasCostHelper.Status.SUCCESS) {
return OperationResult.insufficientGas();
return OperationResult.insufficientGas(result.gasCost());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) {
frame.warmUpAddress(address) || gasCalculator().isPrecompile(address);
final long cost = cost(accountIsWarm);
if (frame.getRemainingGas() < cost) {
return OperationResult.insufficientGas();
return OperationResult.insufficientGas(cost);
}

final Account account = frame.getWorldUpdater().get(address);
Expand All @@ -85,7 +85,7 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) {
final DelegatedCodeGasCostHelper.Result result =
deductDelegatedCodeGasCost(frame, gasCalculator(), account);
if (result.status() != DelegatedCodeGasCostHelper.Status.SUCCESS) {
return OperationResult.insufficientGas();
return OperationResult.insufficientGas(result.gasCost());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) {
frame.warmUpAddress(address) || gasCalculator().isPrecompile(address);
final long cost = cost(accountIsWarm);
if (frame.getRemainingGas() < cost) {
return OperationResult.insufficientGas();
return OperationResult.insufficientGas(cost);
} else {
final Account account = frame.getWorldUpdater().get(address);

if (account != null) {
final DelegatedCodeGasCostHelper.Result result =
deductDelegatedCodeGasCost(frame, gasCalculator(), account);
if (result.status() != DelegatedCodeGasCostHelper.Status.SUCCESS) {
return OperationResult.insufficientGas();
return OperationResult.insufficientGas(result.gasCost());
}
}

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

final long cost = gasCalculator().keccak256OperationGasCost(frame, from, length);
if (frame.getRemainingGas() < cost) {
return OperationResult.insufficientGas();
return OperationResult.insufficientGas(cost);
}

final Bytes bytes = frame.readMutableMemory(from, length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) {
if (frame.isStatic()) {
return OperationResult.illegalStateChange();
} else if (frame.getRemainingGas() < cost) {
return OperationResult.insufficientGas();
return OperationResult.insufficientGas(cost);
}

final Address address = frame.getRecipientAddress();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) {

final long cost = gasCalculator().dataCopyOperationGasCost(frame, Math.max(src, dst), length);
if (frame.getRemainingGas() < cost) {
return OperationResult.insufficientGas();
return OperationResult.insufficientGas(cost);
}

frame.copyMemory(dst, src, length, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) {

final long cost = gasCalculator().mLoadOperationGasCost(frame, location);
if (frame.getRemainingGas() < cost) {
return OperationResult.insufficientGas();
return OperationResult.insufficientGas(cost);
}

final Bytes value = frame.readMutableMemory(location, 32, true).copy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) {

final long cost = gasCalculator().mStore8OperationGasCost(frame, location);
if (frame.getRemainingGas() < cost) {
return OperationResult.insufficientGas();
return OperationResult.insufficientGas(cost);
}

frame.writeMemory(location, theByte, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) {

final long cost = gasCalculator().mStoreOperationGasCost(frame, location);
if (frame.getRemainingGas() < cost) {
return OperationResult.insufficientGas();
return OperationResult.insufficientGas(cost);
}

frame.writeMemoryRightAligned(location, 32, value, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,24 @@ public class OperationResult {
private final int pcIncrement;

/**
* Instantiates a new Operation result.
* Instantiates a new Operation result when due to an ExceptionalHaltReason.
*
* @param haltReason the halt reason
*/
private OperationResult(final ExceptionalHaltReason haltReason) {
this.gasCost = 0L;
this(0L, haltReason);
}

/**
* Instantiates a new Operation result when due to an ExceptionalHaltReason with an associated
* gasCost increment. Only InsufficientGas has a gas cost increment associated with the reason for
* halting, all other halt reasons should use this constructor.
*
* @param gasCost the gas cost increment that caused the execution to halt
* @param haltReason the halt reason
*/
private OperationResult(final long gasCost, final ExceptionalHaltReason haltReason) {
this.gasCost = gasCost;
this.pcIncrement = 0;
this.haltReason = haltReason;
}
Expand Down Expand Up @@ -107,10 +119,11 @@ public static OperationResult overFlow() {
/**
* Returns a halted OperationResult due to ExceptionalHaltReason.INSUFFICIENT_GAS.
*
* @param overshotGasCost gas cost that exceeded the remaining gas during execution
* @return halted OperationResult
*/
public static OperationResult insufficientGas() {
return new OperationResult(ExceptionalHaltReason.INSUFFICIENT_GAS);
public static OperationResult insufficientGas(final long overshotGasCost) {
return new OperationResult(overshotGasCost, ExceptionalHaltReason.INSUFFICIENT_GAS);
}

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

final long cost = gasCalculator().memoryExpansionGasCost(frame, from, length);
if (frame.getRemainingGas() < cost) {
return OperationResult.insufficientGas();
return OperationResult.insufficientGas(cost);
}

if (index >= code.getSubcontainerCount()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) {

final long cost = gasCalculator().dataCopyOperationGasCost(frame, memOffset, numBytes);
if (frame.getRemainingGas() < cost) {
return OperationResult.insufficientGas();
return OperationResult.insufficientGas(cost);
}

frame.writeMemory(memOffset, sourceOffset, numBytes, returnData, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) {

final long cost = gasCalculator().memoryExpansionGasCost(frame, from, length);
if (frame.getRemainingGas() < cost) {
return OperationResult.insufficientGas();
return OperationResult.insufficientGas(cost);
}

frame.setOutputData(frame.readMemory(from, length));
Expand Down
Loading

0 comments on commit 073356f

Please sign in to comment.