Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix negative gas cost in debug_traceTransaction traces #6831

Merged
merged 1 commit into from
Mar 15, 2024
Merged
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 @@ -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")
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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);

Expand Down