From 073356f565f7df8d7472b2588f392d39853e412d Mon Sep 17 00:00:00 2001 From: Luis Pinto Date: Wed, 27 Nov 2024 14:11:47 +0000 Subject: [PATCH] fixup! evm halt cases do not need to report gas cost in OperationResult allow for gasCost reporting in the case of InsufficientGas Signed-off-by: Luis Pinto --- ...race_replayBlockTransactions_all_0x1E.json | 4 ++-- .../trace_replayBlockTransactions_0x1E.json | 4 ++-- .../trace-call/trace_call_1e_0_vmTrace.json | 4 ++-- .../trace-call/trace_call_20_0_vmTrace.json | 2 +- .../trace_rawTransaction_1e_0_vmTrace.json | 4 ++-- .../trace_rawTransaction_20_0_vmTrace.json | 2 +- .../ethereum/vm/DebugOperationTracerTest.java | 2 +- .../besu/evmtool/trace/charge-intrinsic.json | 2 +- .../besu/evmtool/trace/warm-contract.json | 2 +- .../java/org/hyperledger/besu/evm/EVM.java | 1 - .../evm/operation/AbstractCallOperation.java | 4 ++-- .../operation/AbstractCreateOperation.java | 2 +- .../operation/AbstractExtCallOperation.java | 4 ++-- .../operation/AbstractFixedCostOperation.java | 2 +- .../besu/evm/operation/BalanceOperation.java | 2 +- .../evm/operation/CallDataCopyOperation.java | 2 +- .../besu/evm/operation/CodeCopyOperation.java | 2 +- .../besu/evm/operation/DataCopyOperation.java | 2 +- .../besu/evm/operation/ExpOperation.java | 2 +- .../evm/operation/ExtCodeCopyOperation.java | 4 ++-- .../evm/operation/ExtCodeHashOperation.java | 4 ++-- .../evm/operation/ExtCodeSizeOperation.java | 4 ++-- .../evm/operation/Keccak256Operation.java | 2 +- .../besu/evm/operation/LogOperation.java | 2 +- .../besu/evm/operation/MCopyOperation.java | 2 +- .../besu/evm/operation/MLoadOperation.java | 2 +- .../besu/evm/operation/MStore8Operation.java | 2 +- .../besu/evm/operation/MStoreOperation.java | 2 +- .../besu/evm/operation/OperationResult.java | 21 +++++++++++++++---- .../operation/ReturnContractOperation.java | 2 +- .../operation/ReturnDataCopyOperation.java | 2 +- .../besu/evm/operation/ReturnOperation.java | 2 +- .../besu/evm/operation/RevertOperation.java | 2 +- .../besu/evm/operation/SLoadOperation.java | 2 +- .../besu/evm/operation/SStoreOperation.java | 4 ++-- .../evm/operation/SelfDestructOperation.java | 2 +- .../besu/evm/operation/TLoadOperation.java | 2 +- .../besu/evm/operation/TStoreOperation.java | 2 +- .../evm/operation/ChainIdOperationTest.java | 4 +--- .../evm/operation/DataCopyOperationTest.java | 4 +--- .../evm/operation/ExtCallOperationTest.java | 12 ++++------- .../ExtDelegateCallOperationTest.java | 6 ++---- .../operation/ExtStaticCallOperationTest.java | 6 ++---- 43 files changed, 73 insertions(+), 73 deletions(-) diff --git a/ethereum/api/src/test/resources/org/hyperledger/besu/ethereum/api/jsonrpc/trace/specs/replay-trace-transaction/all/trace_replayBlockTransactions_all_0x1E.json b/ethereum/api/src/test/resources/org/hyperledger/besu/ethereum/api/jsonrpc/trace/specs/replay-trace-transaction/all/trace_replayBlockTransactions_all_0x1E.json index 06bf36e8572..79db8eb1685 100644 --- a/ethereum/api/src/test/resources/org/hyperledger/besu/ethereum/api/jsonrpc/trace/specs/replay-trace-transaction/all/trace_replayBlockTransactions_all_0x1E.json +++ b/ethereum/api/src/test/resources/org/hyperledger/besu/ethereum/api/jsonrpc/trace/specs/replay-trace-transaction/all/trace_replayBlockTransactions_all_0x1E.json @@ -244,7 +244,7 @@ "sub": null }, { - "cost": 0, + "cost": 30000, "ex": null, "pc": 21, "sub": null @@ -411,7 +411,7 @@ "sub": null }, { - "cost": 0, + "cost": 30000, "ex": null, "pc": 21, "sub": null diff --git a/ethereum/api/src/test/resources/org/hyperledger/besu/ethereum/api/jsonrpc/trace/specs/replay-trace-transaction/vm-trace/trace_replayBlockTransactions_0x1E.json b/ethereum/api/src/test/resources/org/hyperledger/besu/ethereum/api/jsonrpc/trace/specs/replay-trace-transaction/vm-trace/trace_replayBlockTransactions_0x1E.json index 4be5f214b01..9ce63ddadef 100644 --- a/ethereum/api/src/test/resources/org/hyperledger/besu/ethereum/api/jsonrpc/trace/specs/replay-trace-transaction/vm-trace/trace_replayBlockTransactions_0x1E.json +++ b/ethereum/api/src/test/resources/org/hyperledger/besu/ethereum/api/jsonrpc/trace/specs/replay-trace-transaction/vm-trace/trace_replayBlockTransactions_0x1E.json @@ -153,7 +153,7 @@ "sub": null }, { - "cost": 0, + "cost": 30000, "ex": null, "pc": 21, "sub": null @@ -320,7 +320,7 @@ "sub": null }, { - "cost": 0, + "cost": 30000, "ex": null, "pc": 21, "sub": null diff --git a/ethereum/api/src/test/resources/org/hyperledger/besu/ethereum/api/jsonrpc/trace/specs/trace-call/trace_call_1e_0_vmTrace.json b/ethereum/api/src/test/resources/org/hyperledger/besu/ethereum/api/jsonrpc/trace/specs/trace-call/trace_call_1e_0_vmTrace.json index 03aa0b9f782..01037fa1b1f 100644 --- a/ethereum/api/src/test/resources/org/hyperledger/besu/ethereum/api/jsonrpc/trace/specs/trace-call/trace_call_1e_0_vmTrace.json +++ b/ethereum/api/src/test/resources/org/hyperledger/besu/ethereum/api/jsonrpc/trace/specs/trace-call/trace_call_1e_0_vmTrace.json @@ -124,7 +124,7 @@ "pc" : 0, "sub" : null }, { - "cost" : 0, + "cost" : 30000, "ex" : null, "pc" : 21, "sub" : null @@ -255,7 +255,7 @@ "pc" : 0, "sub" : null }, { - "cost" : 0, + "cost" : 30000, "ex" : null, "pc" : 21, "sub" : null diff --git a/ethereum/api/src/test/resources/org/hyperledger/besu/ethereum/api/jsonrpc/trace/specs/trace-call/trace_call_20_0_vmTrace.json b/ethereum/api/src/test/resources/org/hyperledger/besu/ethereum/api/jsonrpc/trace/specs/trace-call/trace_call_20_0_vmTrace.json index f0501f7b965..4dcd0500389 100644 --- a/ethereum/api/src/test/resources/org/hyperledger/besu/ethereum/api/jsonrpc/trace/specs/trace-call/trace_call_20_0_vmTrace.json +++ b/ethereum/api/src/test/resources/org/hyperledger/besu/ethereum/api/jsonrpc/trace/specs/trace-call/trace_call_20_0_vmTrace.json @@ -160,7 +160,7 @@ "pc" : 9, "sub" : null }, { - "cost" : 0, + "cost" : 32009, "ex" : null, "pc" : 11, "sub" : null diff --git a/ethereum/api/src/test/resources/org/hyperledger/besu/ethereum/api/jsonrpc/trace/specs/trace-raw-transaction/trace_rawTransaction_1e_0_vmTrace.json b/ethereum/api/src/test/resources/org/hyperledger/besu/ethereum/api/jsonrpc/trace/specs/trace-raw-transaction/trace_rawTransaction_1e_0_vmTrace.json index 9db0441adbc..1ae49e09f5d 100644 --- a/ethereum/api/src/test/resources/org/hyperledger/besu/ethereum/api/jsonrpc/trace/specs/trace-raw-transaction/trace_rawTransaction_1e_0_vmTrace.json +++ b/ethereum/api/src/test/resources/org/hyperledger/besu/ethereum/api/jsonrpc/trace/specs/trace-raw-transaction/trace_rawTransaction_1e_0_vmTrace.json @@ -116,7 +116,7 @@ "pc" : 0, "sub" : null }, { - "cost" : 0, + "cost" : 30000, "ex" : null, "pc" : 21, "sub" : null @@ -247,7 +247,7 @@ "pc" : 0, "sub" : null }, { - "cost" : 0, + "cost" : 30000, "ex" : null, "pc" : 21, "sub" : null diff --git a/ethereum/api/src/test/resources/org/hyperledger/besu/ethereum/api/jsonrpc/trace/specs/trace-raw-transaction/trace_rawTransaction_20_0_vmTrace.json b/ethereum/api/src/test/resources/org/hyperledger/besu/ethereum/api/jsonrpc/trace/specs/trace-raw-transaction/trace_rawTransaction_20_0_vmTrace.json index 3d86dd20223..2915d4fd346 100644 --- a/ethereum/api/src/test/resources/org/hyperledger/besu/ethereum/api/jsonrpc/trace/specs/trace-raw-transaction/trace_rawTransaction_20_0_vmTrace.json +++ b/ethereum/api/src/test/resources/org/hyperledger/besu/ethereum/api/jsonrpc/trace/specs/trace-raw-transaction/trace_rawTransaction_20_0_vmTrace.json @@ -152,7 +152,7 @@ "pc" : 9, "sub" : null }, { - "cost" : 0, + "cost" : 32009, "ex" : null, "pc" : 11, "sub" : null diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/vm/DebugOperationTracerTest.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/vm/DebugOperationTracerTest.java index 4a4c13f9a83..eb0e31713df 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/vm/DebugOperationTracerTest.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/vm/DebugOperationTracerTest.java @@ -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()) diff --git a/ethereum/evmtool/src/test/resources/org/hyperledger/besu/evmtool/trace/charge-intrinsic.json b/ethereum/evmtool/src/test/resources/org/hyperledger/besu/evmtool/trace/charge-intrinsic.json index 2b0ab01c14f..17dc28d35a0 100644 --- a/ethereum/evmtool/src/test/resources/org/hyperledger/besu/evmtool/trace/charge-intrinsic.json +++ b/ethereum/evmtool/src/test/resources/org/hyperledger/besu/evmtool/trace/charge-intrinsic.json @@ -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"}, diff --git a/ethereum/evmtool/src/test/resources/org/hyperledger/besu/evmtool/trace/warm-contract.json b/ethereum/evmtool/src/test/resources/org/hyperledger/besu/evmtool/trace/warm-contract.json index f8799908ffc..6a62a6eb86a 100644 --- a/ethereum/evmtool/src/test/resources/org/hyperledger/besu/evmtool/trace/warm-contract.json +++ b/ethereum/evmtool/src/test/resources/org/hyperledger/besu/evmtool/trace/warm-contract.json @@ -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"}, diff --git a/evm/src/main/java/org/hyperledger/besu/evm/EVM.java b/evm/src/main/java/org/hyperledger/besu/evm/EVM.java index 3b707e923da..7f685b2ce90 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/EVM.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/EVM.java @@ -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); diff --git a/evm/src/main/java/org/hyperledger/besu/evm/operation/AbstractCallOperation.java b/evm/src/main/java/org/hyperledger/besu/evm/operation/AbstractCallOperation.java index bb3494d26ff..7a4f44f535e 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/operation/AbstractCallOperation.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/operation/AbstractCallOperation.java @@ -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); @@ -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()); } } diff --git a/evm/src/main/java/org/hyperledger/besu/evm/operation/AbstractCreateOperation.java b/evm/src/main/java/org/hyperledger/besu/evm/operation/AbstractCreateOperation.java index 00512c42be3..4f1753f46f9 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/operation/AbstractCreateOperation.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/operation/AbstractCreateOperation.java @@ -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)); diff --git a/evm/src/main/java/org/hyperledger/besu/evm/operation/AbstractExtCallOperation.java b/evm/src/main/java/org/hyperledger/besu/evm/operation/AbstractExtCallOperation.java index fbfe0f8c306..42e9a408252 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/operation/AbstractExtCallOperation.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/operation/AbstractExtCallOperation.java @@ -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()); } } @@ -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 = diff --git a/evm/src/main/java/org/hyperledger/besu/evm/operation/AbstractFixedCostOperation.java b/evm/src/main/java/org/hyperledger/besu/evm/operation/AbstractFixedCostOperation.java index 3c3d56593c7..83085e60aec 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/operation/AbstractFixedCostOperation.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/operation/AbstractFixedCostOperation.java @@ -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); } diff --git a/evm/src/main/java/org/hyperledger/besu/evm/operation/BalanceOperation.java b/evm/src/main/java/org/hyperledger/besu/evm/operation/BalanceOperation.java index aa64bcdd93d..27f9449a847 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/operation/BalanceOperation.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/operation/BalanceOperation.java @@ -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()); diff --git a/evm/src/main/java/org/hyperledger/besu/evm/operation/CallDataCopyOperation.java b/evm/src/main/java/org/hyperledger/besu/evm/operation/CallDataCopyOperation.java index d08c8513025..139f7662a35 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/operation/CallDataCopyOperation.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/operation/CallDataCopyOperation.java @@ -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(); diff --git a/evm/src/main/java/org/hyperledger/besu/evm/operation/CodeCopyOperation.java b/evm/src/main/java/org/hyperledger/besu/evm/operation/CodeCopyOperation.java index 818844d89ce..44653987e15 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/operation/CodeCopyOperation.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/operation/CodeCopyOperation.java @@ -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(); diff --git a/evm/src/main/java/org/hyperledger/besu/evm/operation/DataCopyOperation.java b/evm/src/main/java/org/hyperledger/besu/evm/operation/DataCopyOperation.java index 8e97eb5fae8..d3e51d146bb 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/operation/DataCopyOperation.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/operation/DataCopyOperation.java @@ -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); diff --git a/evm/src/main/java/org/hyperledger/besu/evm/operation/ExpOperation.java b/evm/src/main/java/org/hyperledger/besu/evm/operation/ExpOperation.java index d386226964c..beb81145f9c 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/operation/ExpOperation.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/operation/ExpOperation.java @@ -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(); diff --git a/evm/src/main/java/org/hyperledger/besu/evm/operation/ExtCodeCopyOperation.java b/evm/src/main/java/org/hyperledger/besu/evm/operation/ExtCodeCopyOperation.java index fe89d5c3f0e..4e9b09fdfd9 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/operation/ExtCodeCopyOperation.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/operation/ExtCodeCopyOperation.java @@ -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); @@ -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()); } } diff --git a/evm/src/main/java/org/hyperledger/besu/evm/operation/ExtCodeHashOperation.java b/evm/src/main/java/org/hyperledger/besu/evm/operation/ExtCodeHashOperation.java index 3170e826318..d9ed6d1c08b 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/operation/ExtCodeHashOperation.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/operation/ExtCodeHashOperation.java @@ -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); @@ -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()); } } diff --git a/evm/src/main/java/org/hyperledger/besu/evm/operation/ExtCodeSizeOperation.java b/evm/src/main/java/org/hyperledger/besu/evm/operation/ExtCodeSizeOperation.java index e0fe167f4ec..40085cb0d89 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/operation/ExtCodeSizeOperation.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/operation/ExtCodeSizeOperation.java @@ -74,7 +74,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); @@ -82,7 +82,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()); } } diff --git a/evm/src/main/java/org/hyperledger/besu/evm/operation/Keccak256Operation.java b/evm/src/main/java/org/hyperledger/besu/evm/operation/Keccak256Operation.java index e1f1fa554ce..1ced28ab2a8 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/operation/Keccak256Operation.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/operation/Keccak256Operation.java @@ -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); diff --git a/evm/src/main/java/org/hyperledger/besu/evm/operation/LogOperation.java b/evm/src/main/java/org/hyperledger/besu/evm/operation/LogOperation.java index b02237cd7ba..014f5a71f38 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/operation/LogOperation.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/operation/LogOperation.java @@ -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(); diff --git a/evm/src/main/java/org/hyperledger/besu/evm/operation/MCopyOperation.java b/evm/src/main/java/org/hyperledger/besu/evm/operation/MCopyOperation.java index bed9d64f813..51b080f028a 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/operation/MCopyOperation.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/operation/MCopyOperation.java @@ -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); diff --git a/evm/src/main/java/org/hyperledger/besu/evm/operation/MLoadOperation.java b/evm/src/main/java/org/hyperledger/besu/evm/operation/MLoadOperation.java index 8f177b4175e..c812500de67 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/operation/MLoadOperation.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/operation/MLoadOperation.java @@ -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(); diff --git a/evm/src/main/java/org/hyperledger/besu/evm/operation/MStore8Operation.java b/evm/src/main/java/org/hyperledger/besu/evm/operation/MStore8Operation.java index 18147011aea..8e48920574f 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/operation/MStore8Operation.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/operation/MStore8Operation.java @@ -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); diff --git a/evm/src/main/java/org/hyperledger/besu/evm/operation/MStoreOperation.java b/evm/src/main/java/org/hyperledger/besu/evm/operation/MStoreOperation.java index 7070eba17bb..b243d080b72 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/operation/MStoreOperation.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/operation/MStoreOperation.java @@ -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); diff --git a/evm/src/main/java/org/hyperledger/besu/evm/operation/OperationResult.java b/evm/src/main/java/org/hyperledger/besu/evm/operation/OperationResult.java index caf58848871..e8fe67f1838 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/operation/OperationResult.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/operation/OperationResult.java @@ -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; } @@ -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); } /** diff --git a/evm/src/main/java/org/hyperledger/besu/evm/operation/ReturnContractOperation.java b/evm/src/main/java/org/hyperledger/besu/evm/operation/ReturnContractOperation.java index c8de8741ab7..cc7a07ff899 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/operation/ReturnContractOperation.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/operation/ReturnContractOperation.java @@ -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()) { diff --git a/evm/src/main/java/org/hyperledger/besu/evm/operation/ReturnDataCopyOperation.java b/evm/src/main/java/org/hyperledger/besu/evm/operation/ReturnDataCopyOperation.java index a0094dd4843..a8c77738944 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/operation/ReturnDataCopyOperation.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/operation/ReturnDataCopyOperation.java @@ -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); diff --git a/evm/src/main/java/org/hyperledger/besu/evm/operation/ReturnOperation.java b/evm/src/main/java/org/hyperledger/besu/evm/operation/ReturnOperation.java index a72d01c7513..2206aafda6a 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/operation/ReturnOperation.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/operation/ReturnOperation.java @@ -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)); diff --git a/evm/src/main/java/org/hyperledger/besu/evm/operation/RevertOperation.java b/evm/src/main/java/org/hyperledger/besu/evm/operation/RevertOperation.java index b101fd5270f..f010083108f 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/operation/RevertOperation.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/operation/RevertOperation.java @@ -44,7 +44,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); } final Bytes reason = frame.readMemory(from, length); diff --git a/evm/src/main/java/org/hyperledger/besu/evm/operation/SLoadOperation.java b/evm/src/main/java/org/hyperledger/besu/evm/operation/SLoadOperation.java index 3fef0a67549..ce1c8b0bbf9 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/operation/SLoadOperation.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/operation/SLoadOperation.java @@ -55,7 +55,7 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) { final boolean slotIsWarm = frame.warmUpStorage(address, key); final long cost = slotIsWarm ? warmCost : coldCost; if (frame.getRemainingGas() < cost) { - return OperationResult.insufficientGas(); + return OperationResult.insufficientGas(cost); } else { frame.pushStackItem(account.getStorageValue(UInt256.fromBytes(key))); diff --git a/evm/src/main/java/org/hyperledger/besu/evm/operation/SStoreOperation.java b/evm/src/main/java/org/hyperledger/besu/evm/operation/SStoreOperation.java index fa94451ba13..139e3f60d40 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/operation/SStoreOperation.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/operation/SStoreOperation.java @@ -82,9 +82,9 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) { if (frame.isStatic()) { return OperationResult.illegalStateChange(); } else if (remainingGas < cost) { - return OperationResult.insufficientGas(); + return OperationResult.insufficientGas(cost); } else if (remainingGas <= minimumGasRemaining) { - return OperationResult.insufficientGas(); + return OperationResult.insufficientGas(minimumGasRemaining); } // Increment the refund counter. diff --git a/evm/src/main/java/org/hyperledger/besu/evm/operation/SelfDestructOperation.java b/evm/src/main/java/org/hyperledger/besu/evm/operation/SelfDestructOperation.java index 72d9b7556c1..2417f21661c 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/operation/SelfDestructOperation.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/operation/SelfDestructOperation.java @@ -71,7 +71,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); } // We passed preliminary checks, get mutable accounts. diff --git a/evm/src/main/java/org/hyperledger/besu/evm/operation/TLoadOperation.java b/evm/src/main/java/org/hyperledger/besu/evm/operation/TLoadOperation.java index b122837f7eb..2bb7c5efb4a 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/operation/TLoadOperation.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/operation/TLoadOperation.java @@ -38,7 +38,7 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) { final long cost = gasCalculator().getTransientLoadOperationGasCost(); final Bytes32 slot = UInt256.fromBytes(frame.popStackItem()); if (frame.getRemainingGas() < cost) { - return OperationResult.insufficientGas(); + return OperationResult.insufficientGas(cost); } else { frame.pushStackItem(frame.getTransientStorageValue(frame.getRecipientAddress(), slot)); diff --git a/evm/src/main/java/org/hyperledger/besu/evm/operation/TStoreOperation.java b/evm/src/main/java/org/hyperledger/besu/evm/operation/TStoreOperation.java index 939ff2b2f56..75936547462 100644 --- a/evm/src/main/java/org/hyperledger/besu/evm/operation/TStoreOperation.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/operation/TStoreOperation.java @@ -43,7 +43,7 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) { if (frame.isStatic()) { return OperationResult.illegalStateChange(); } else if (remainingGas < cost) { - return OperationResult.insufficientGas(); + return OperationResult.insufficientGas(cost); } else { frame.setTransientStorageValue(frame.getRecipientAddress(), key, value); return new OperationResult(cost); diff --git a/evm/src/test/java/org/hyperledger/besu/evm/operation/ChainIdOperationTest.java b/evm/src/test/java/org/hyperledger/besu/evm/operation/ChainIdOperationTest.java index c501479ebf0..15ac2904dc9 100644 --- a/evm/src/test/java/org/hyperledger/besu/evm/operation/ChainIdOperationTest.java +++ b/evm/src/test/java/org/hyperledger/besu/evm/operation/ChainIdOperationTest.java @@ -64,9 +64,7 @@ void shouldCalculateGasPrice(final String chainIdString, final int expectedGas) Bytes32 chainId = Bytes32.fromHexString(chainIdString); ChainIdOperation operation = new ChainIdOperation(new ConstantinopleGasCalculator(), chainId); final OperationResult result = operation.execute(messageFrame, null); - if (result.getHaltReason() == null) { - assertThat(result.getGasCost()).isEqualTo(expectedGas); - } + assertThat(result.getGasCost()).isEqualTo(expectedGas); } @Test diff --git a/evm/src/test/java/org/hyperledger/besu/evm/operation/DataCopyOperationTest.java b/evm/src/test/java/org/hyperledger/besu/evm/operation/DataCopyOperationTest.java index 861913fc9f0..5da9d34f9d7 100644 --- a/evm/src/test/java/org/hyperledger/besu/evm/operation/DataCopyOperationTest.java +++ b/evm/src/test/java/org/hyperledger/besu/evm/operation/DataCopyOperationTest.java @@ -151,9 +151,7 @@ void testMCopy( assertThat(frame.memoryWordSize()).isEqualTo((expected.size() + 31) / 32); assertThat(frame.readMemory(0, expected.size())).isEqualTo(expected); - if (result.getHaltReason() == null) { - assertThat(result.getGasCost()).isEqualTo(gasCost); - } + assertThat(result.getGasCost()).isEqualTo(gasCost); } @Test diff --git a/evm/src/test/java/org/hyperledger/besu/evm/operation/ExtCallOperationTest.java b/evm/src/test/java/org/hyperledger/besu/evm/operation/ExtCallOperationTest.java index 297515eda8c..e50ae6b147d 100644 --- a/evm/src/test/java/org/hyperledger/besu/evm/operation/ExtCallOperationTest.java +++ b/evm/src/test/java/org/hyperledger/besu/evm/operation/ExtCallOperationTest.java @@ -96,7 +96,7 @@ static Iterable data() { Arguments.of( "Invalid code", 384100, - 100, + 0, 384100, ExceptionalHaltReason.INVALID_CODE, CONTRACT_ADDRESS, @@ -140,9 +140,7 @@ void gasTest( var result = operation.execute(messageFrame, EOF_EVM); - if (result.getHaltReason() == null) { - assertThat(result.getGasCost()).isEqualTo(chargedGas); - } + assertThat(result.getGasCost()).isEqualTo(chargedGas); assertThat(result.getHaltReason()).isEqualTo(haltReason); MessageFrame childFrame = messageFrame.getMessageFrameStack().getFirst(); @@ -167,7 +165,7 @@ static Iterable valueData() { Arguments.of( "static context", 40000, - 9000, + 0, 40000, ExceptionalHaltReason.ILLEGAL_STATE_CHANGE, CONTRACT_ADDRESS, @@ -232,9 +230,7 @@ void callWithValueTest( var result = operation.execute(messageFrame, EOF_EVM); - if (result.getHaltReason() == null) { - assertThat(result.getGasCost()).isEqualTo(chargedGas); - } + assertThat(result.getGasCost()).isEqualTo(chargedGas); assertThat(result.getHaltReason()).isEqualTo(haltReason); MessageFrame childFrame = messageFrame.getMessageFrameStack().getFirst(); diff --git a/evm/src/test/java/org/hyperledger/besu/evm/operation/ExtDelegateCallOperationTest.java b/evm/src/test/java/org/hyperledger/besu/evm/operation/ExtDelegateCallOperationTest.java index 66b343b87d6..ad14f06adfd 100644 --- a/evm/src/test/java/org/hyperledger/besu/evm/operation/ExtDelegateCallOperationTest.java +++ b/evm/src/test/java/org/hyperledger/besu/evm/operation/ExtDelegateCallOperationTest.java @@ -99,7 +99,7 @@ static Iterable data() { Arguments.of( "Invalid code", 384100, - 100, + 0, 384100, ExceptionalHaltReason.INVALID_CODE, CONTRACT_ADDRESS, @@ -143,9 +143,7 @@ void gasTest( var result = operation.execute(messageFrame, EOF_EVM); - if (result.getHaltReason() == null) { - assertThat(result.getGasCost()).isEqualTo(chargedGas); - } + assertThat(result.getGasCost()).isEqualTo(chargedGas); assertThat(result.getHaltReason()).isEqualTo(haltReason); MessageFrame childFrame = messageFrame.getMessageFrameStack().getFirst(); diff --git a/evm/src/test/java/org/hyperledger/besu/evm/operation/ExtStaticCallOperationTest.java b/evm/src/test/java/org/hyperledger/besu/evm/operation/ExtStaticCallOperationTest.java index 4e03201a328..ab51eef7cee 100644 --- a/evm/src/test/java/org/hyperledger/besu/evm/operation/ExtStaticCallOperationTest.java +++ b/evm/src/test/java/org/hyperledger/besu/evm/operation/ExtStaticCallOperationTest.java @@ -96,7 +96,7 @@ static Iterable data() { Arguments.of( "Invalid code", 384100, - 100, + 0, 384100, ExceptionalHaltReason.INVALID_CODE, CONTRACT_ADDRESS, @@ -139,9 +139,7 @@ void gasTest( var result = operation.execute(messageFrame, EOF_EVM); - if (result.getHaltReason() == null) { - assertThat(result.getGasCost()).isEqualTo(chargedGas); - } + assertThat(result.getGasCost()).isEqualTo(chargedGas); assertThat(result.getHaltReason()).isEqualTo(haltReason); MessageFrame childFrame = messageFrame.getMessageFrameStack().getFirst();