Skip to content

Commit

Permalink
Fix negative gas cost in debug_traceTransaction traces (NethermindEth…
Browse files Browse the repository at this point in the history
  • Loading branch information
natebeauregard committed Mar 15, 2024
1 parent 5d1818d commit ea0bbf0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
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

0 comments on commit ea0bbf0

Please sign in to comment.