Skip to content

Commit

Permalink
Don't limit eth_getLogs on authenticated context (#7149)
Browse files Browse the repository at this point in the history
  • Loading branch information
LukaszRozmej authored Jun 9, 2024
1 parent bd2f370 commit 04674fa
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,17 @@ public class JsonRpcServiceTests
[SetUp]
public void Initialize()
{
Assembly jConfig = typeof(JsonRpcConfig).Assembly;
_configurationProvider = new ConfigProvider();
_logManager = LimboLogs.Instance;
_context = new JsonRpcContext(RpcEndpoint.Http);
}

[TearDown]
public void TearDown()
{
_context?.Dispose();
}

private IJsonRpcService _jsonRpcService = null!;
private IConfigProvider _configurationProvider = null!;
private ILogManager _logManager = null!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ public void Initialize()
_context = new JsonRpcContext(RpcEndpoint.Http);
}

[TearDown]
public void TearDown()
{
_context?.Dispose();
}

[Test]
public void Module_provider_will_recognize_disabled_modules()
{
Expand Down
14 changes: 13 additions & 1 deletion src/Nethermind/Nethermind.JsonRpc/JsonRpcContext.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Threading;
using Nethermind.JsonRpc.Modules;

namespace Nethermind.JsonRpc
{
public class JsonRpcContext
public class JsonRpcContext : IDisposable
{
public static AsyncLocal<JsonRpcContext?> Current { get; private set; } = new();

public static JsonRpcContext Http(JsonRpcUrl url) => new(RpcEndpoint.Http, url: url);
public static JsonRpcContext WebSocket(JsonRpcUrl url) => new(RpcEndpoint.Ws, url: url);

Expand All @@ -16,11 +20,19 @@ public JsonRpcContext(RpcEndpoint rpcEndpoint, IJsonRpcDuplexClient? duplexClien
DuplexClient = duplexClient;
Url = url;
IsAuthenticated = Url?.IsAuthenticated == true || RpcEndpoint == RpcEndpoint.IPC;
Current.Value = this;
}

public RpcEndpoint RpcEndpoint { get; }
public IJsonRpcDuplexClient? DuplexClient { get; }
public JsonRpcUrl? Url { get; }
public bool IsAuthenticated { get; }
public void Dispose()
{
if (Current.Value == this)
{
Current.Value = null;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,9 @@ public ResultWrapper<IEnumerable<FilterLog>> eth_getLogs(Filter filter)
foreach (FilterLog log in filterLogs)
{
logs.Add(log);
if (_rpcConfig.MaxLogsPerResponse != 0 && logs.Count > _rpcConfig.MaxLogsPerResponse)
if (JsonRpcContext.Current.Value?.IsAuthenticated != true // not authenticated
&& _rpcConfig.MaxLogsPerResponse != 0 // not unlimited
&& logs.Count > _rpcConfig.MaxLogsPerResponse)
{
logs.Dispose();
return ResultWrapper<IEnumerable<FilterLog>>.Fail($"Too many logs requested. Max logs per response is {_rpcConfig.MaxLogsPerResponse}.", ErrorCodes.LimitExceeded);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public override void Dispose()
{
base.Dispose();
_sendSemaphore.Dispose();
_jsonRpcContext.Dispose();
Closed?.Invoke(this, EventArgs.Empty);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Runner/JsonRpc/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ await PushErrorResponse(StatusCodes.Status403Forbidden, ErrorCodes.InvalidReques
CountingPipeReader request = new(ctx.Request.BodyReader);
try
{
JsonRpcContext jsonRpcContext = JsonRpcContext.Http(jsonRpcUrl);
using JsonRpcContext jsonRpcContext = JsonRpcContext.Http(jsonRpcUrl);
await foreach (JsonRpcResult result in jsonRpcProcessor.ProcessAsync(request, jsonRpcContext))
{
using (result)
Expand Down

0 comments on commit 04674fa

Please sign in to comment.