From ea0bbf084d0462340c9bd5d4c9990a5edfc853c7 Mon Sep 17 00:00:00 2001 From: Nate Beauregard <51711291+natebeauregard@users.noreply.github.com> Date: Fri, 15 Mar 2024 06:38:03 -0400 Subject: [PATCH] Fix negative gas cost in debug_traceTransaction traces (#6831) --- .../Tracing/GethLikeTxMemoryTracerTests.cs | 25 ++++++++++++++++++- .../Tracing/GethStyle/GethLikeTxTracer.cs | 11 +++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/Nethermind/Nethermind.Evm.Test/Tracing/GethLikeTxMemoryTracerTests.cs b/src/Nethermind/Nethermind.Evm.Test/Tracing/GethLikeTxMemoryTracerTests.cs index ae5ceab5c4b..878e1496570 100644 --- a/src/Nethermind/Nethermind.Evm.Test/Tracing/GethLikeTxMemoryTracerTests.cs +++ b/src/Nethermind/Nethermind.Evm.Test/Tracing/GethLikeTxMemoryTracerTests.cs @@ -16,7 +16,7 @@ namespace Nethermind.Evm.Test.Tracing; public class GethLikeTxMemoryTracerTests : VirtualMachineTestsBase { [Test] - public void Can_trace_gas() + public void Can_trace_gas_halt_with_stop() { byte[] code = Prepare.EvmCode .PushData("0x1") @@ -38,6 +38,29 @@ public void Can_trace_gas() } } + [Test] + public void Can_trace_gas_halt_with_return() + { + byte[] code = Prepare.EvmCode + .PushData("0x1") + .PushData("0x2") + .Op(Instruction.ADD) + .Op(Instruction.RETURN) + .Done; + + int[] gasCosts = new int[] { 3, 3, 3, 0 }; + + GethLikeTxTrace trace = ExecuteAndTrace(code); + + int gasTotal = 0; + for (int i = 0; i < gasCosts.Length; i++) + { + Assert.That(trace.Entries[i].Gas, Is.EqualTo(79000 - gasTotal), $"gas[{i}]"); + Assert.That(trace.Entries[i].GasCost, Is.EqualTo(gasCosts[i]), $"gasCost[{i}]"); + gasTotal += gasCosts[i]; + } + } + [Test] [Todo("Verify the exact error string in Geth")] public void Can_trace_stack_underflow_failure() diff --git a/src/Nethermind/Nethermind.Evm/Tracing/GethStyle/GethLikeTxTracer.cs b/src/Nethermind/Nethermind.Evm/Tracing/GethStyle/GethLikeTxTracer.cs index 13f4f1c5707..3292c20b667 100644 --- a/src/Nethermind/Nethermind.Evm/Tracing/GethStyle/GethLikeTxTracer.cs +++ b/src/Nethermind/Nethermind.Evm/Tracing/GethStyle/GethLikeTxTracer.cs @@ -66,6 +66,7 @@ public override void MarkAsFailed(Address recipient, long gasSpent, byte[]? outp protected TEntry? CurrentTraceEntry { get; set; } protected GethLikeTxTracer(GethTraceOptions options) : base(options) { } + private bool _gasCostAlreadySetForCurrentOp; public override void StartOperation(int depth, long gas, Instruction opcode, int pc, bool isPostMerge = false) { @@ -77,11 +78,19 @@ public override void StartOperation(int depth, long gas, Instruction opcode, int CurrentTraceEntry.Gas = gas; CurrentTraceEntry.Opcode = opcode.GetName(isPostMerge); CurrentTraceEntry.ProgramCounter = pc; + _gasCostAlreadySetForCurrentOp = false; } public override void ReportOperationError(EvmExceptionType error) => CurrentTraceEntry.Error = GetErrorDescription(error); - public override void ReportOperationRemainingGas(long gas) => CurrentTraceEntry.GasCost = CurrentTraceEntry.Gas - gas; + public override void ReportOperationRemainingGas(long gas) + { + if (!_gasCostAlreadySetForCurrentOp) + { + CurrentTraceEntry.GasCost = CurrentTraceEntry.Gas - gas; + _gasCostAlreadySetForCurrentOp = true; + } + } public override void SetOperationMemorySize(ulong newSize) => CurrentTraceEntry.UpdateMemorySize(newSize);