Skip to content
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 @@ -31,7 +31,7 @@ private static void RegisterNativeTracers()
RegisterTracer(NativeCallTracer.CallTracer, static (options, _, transaction, _) => new NativeCallTracer(transaction, options));
}

private static void RegisterTracer(string tracerName, GethLikeNativeTracerFactoryDelegate tracerDelegate)
public static void RegisterTracer(string tracerName, GethLikeNativeTracerFactoryDelegate tracerDelegate)
{
_tracers.Add(tracerName, tracerDelegate);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ public static bool ConsumeLogEmission(ref EthereumGasPolicy gas, long topicCount
public static void ConsumeDataCopyGas(ref EthereumGasPolicy gas, bool isExternalCode, long baseCost, long dataCost)
=> Consume(ref gas, baseCost + dataCost);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void OnBeforeInstructionTrace(in EthereumGasPolicy gas, int pc, Instruction instruction, int depth) { }

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void OnAfterInstructionTrace(in EthereumGasPolicy gas) { }

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static EthereumGasPolicy Max(in EthereumGasPolicy a, in EthereumGasPolicy b) =>
a.Value >= b.Value ? a : b;
Expand Down
17 changes: 17 additions & 0 deletions src/Nethermind/Nethermind.Evm/GasPolicy/IGasPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,21 @@ static abstract bool UpdateMemoryCost(ref TSelf gas,
/// <param name="baseCost">Fixed opcode cost.</param>
/// <param name="dataCost">Per-word copy cost.</param>
static abstract void ConsumeDataCopyGas(ref TSelf gas, bool isExternalCode, long baseCost, long dataCost);

/// <summary>
/// Hook called before instruction execution when tracing is active.
/// Allows gas policies to capture pre-execution state.
/// </summary>
/// <param name="gas">The current gas state.</param>
/// <param name="pc">The program counter before incrementing.</param>
/// <param name="instruction">The instruction about to be executed.</param>
/// <param name="depth">The current call depth.</param>
static abstract void OnBeforeInstructionTrace(in TSelf gas, int pc, Instruction instruction, int depth);

/// <summary>
/// Hook called after instruction execution when tracing is active.
/// Allows gas policies to capture post-execution state.
/// </summary>
/// <param name="gas">The current gas state after execution.</param>
static abstract void OnAfterInstructionTrace(in TSelf gas);
}
7 changes: 7 additions & 0 deletions src/Nethermind/Nethermind.Evm/VirtualMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1250,6 +1250,9 @@ protected virtual unsafe CallResult RunByteCode<TTracingInst, TCancelable>(
if (TCancelable.IsActive && _txTracer.IsCancelled)
ThrowOperationCanceledException();

// Call gas policy hook before instruction execution.
TGasPolicy.OnBeforeInstructionTrace(in gas, programCounter, instruction, VmState.Env.CallDepth);

// If tracing is enabled, start an instruction trace.
if (TTracingInst.IsActive)
StartInstructionTrace(instruction, TGasPolicy.GetRemainingGas(in gas), programCounter, in stack);
Expand Down Expand Up @@ -1278,6 +1281,10 @@ protected virtual unsafe CallResult RunByteCode<TTracingInst, TCancelable>(
OpCodeCount += opCodeCount;
goto OutOfGas;
}

// Call gas policy hook after instruction execution.
TGasPolicy.OnAfterInstructionTrace(in gas);

// If an exception occurred, exit the loop.
if (exceptionType != EvmExceptionType.None)
break;
Expand Down