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

Don't use tracing for non-traced rpc calls #6988

Merged
merged 3 commits into from
May 7, 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
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 ExecuteCode<TTracingInstructions, TTracingRefunds, TTracingSt
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 ExecuteCode<TTracingInstructions, TTracingRefunds, TTracingSt
#endif
Instruction instruction = (Instruction)code[programCounter];

if (isCancelable && _txTracer.IsCancelled)
{
ThrowOperationCanceledException();
}
Comment on lines +822 to +825
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is that much faster than IsTracingInstructions?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IsTracingInstructions == true comes with the additional load of copying every stack data change, memory copy etc


// 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 ExecuteCode<TTracingInstructions, TTracingRefunds, TTracingSt
exceptionType = EvmExceptionType.AccessViolation;
ReturnFailure:
return GetFailureReturn<TTracingInstructions>(gasAvailable, exceptionType);

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

[SkipLocalsInit]
Expand Down