Skip to content

Commit

Permalink
Don't use tracing for non-traced rpc calls (#6988)
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams committed May 7, 2024
1 parent bf382fe commit 7f750d3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/Nethermind/Nethermind.Evm/Tracing/CancellationTxTracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public CancellationTxTracer(ITxTracer innerTracer, CancellationToken token = def
_token = token;
}

public bool IsCancelable => true;
public bool IsCancelled => _token.IsCancellationRequested;

public bool IsTracingReceipt
{
get => _isTracingReceipt || _innerTracer.IsTracingReceipt;
Expand Down
2 changes: 2 additions & 0 deletions src/Nethermind/Nethermind.Evm/Tracing/ITxTracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace Nethermind.Evm.Tracing;

public interface ITxTracer : IWorldStateTracer, IDisposable
{
bool IsCancelable => false;
bool IsCancelled => false;
/// <summary>
/// Defines whether MarkAsSuccess or MarkAsFailed will be called
/// </summary>
Expand Down
12 changes: 2 additions & 10 deletions src/Nethermind/Nethermind.Evm/Tracing/TracerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,9 @@ namespace Nethermind.Evm.Tracing;

public static class TracerExtensions
{
public static CancellationTxTracer WithCancellation(this ITxTracer txTracer, CancellationToken cancellationToken, bool setDefaultCancellations = true)
public static CancellationTxTracer WithCancellation(this ITxTracer txTracer, CancellationToken cancellationToken)
{
return !setDefaultCancellations
? new(txTracer, cancellationToken)
: new(txTracer, cancellationToken)
{
IsTracingActions = true,
IsTracingOpLevelStorage = true,
IsTracingInstructions = true, // a little bit costly but almost all are simple calls
IsTracingRefunds = true
};
return new(txTracer, cancellationToken);
}

public static CancellationBlockTracer WithCancellation(this IBlockTracer blockTracer, CancellationToken cancellationToken) =>
Expand Down
10 changes: 10 additions & 0 deletions src/Nethermind/Nethermind.Evm/VirtualMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,7 @@ private CallResult ExecuteCall<TTracingInstructions>(EvmState vmState, ReadOnlyM
SkipInit(out StorageCell storageCell);
object returnData;
ZeroPaddedSpan slice;
bool isCancelable = _txTracer.IsCancelable;
uint codeLength = (uint)code.Length;
while ((uint)programCounter < codeLength)
{
Expand All @@ -818,6 +819,11 @@ private CallResult ExecuteCall<TTracingInstructions>(EvmState vmState, ReadOnlyM
#endif
Instruction instruction = (Instruction)code[programCounter];

if (isCancelable && _txTracer.IsCancelled)
{
ThrowOperationCanceledException();
}

// Evaluated to constant at compile time and code elided if not tracing
if (typeof(TTracingInstructions) == typeof(IsTracing))
StartInstructionTrace(instruction, vmState, gasAvailable, programCounter, in stack);
Expand Down Expand Up @@ -2166,6 +2172,10 @@ private CallResult ExecuteCall<TTracingInstructions>(EvmState vmState, ReadOnlyM
exceptionType = EvmExceptionType.AccessViolation;
ReturnFailure:
return GetFailureReturn<TTracingInstructions>(gasAvailable, exceptionType);

[DoesNotReturn]
static void ThrowOperationCanceledException() =>
throw new OperationCanceledException("Cancellation Requested");
}

[SkipLocalsInit]
Expand Down

0 comments on commit 7f750d3

Please sign in to comment.