diff --git a/src/Nethermind/Nethermind.JsonRpc.Test/JsonRpcServiceTests.cs b/src/Nethermind/Nethermind.JsonRpc.Test/JsonRpcServiceTests.cs index 6748030a5bf..0485a2e4528 100644 --- a/src/Nethermind/Nethermind.JsonRpc.Test/JsonRpcServiceTests.cs +++ b/src/Nethermind/Nethermind.JsonRpc.Test/JsonRpcServiceTests.cs @@ -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!; diff --git a/src/Nethermind/Nethermind.JsonRpc.Test/Modules/RpcModuleProviderTests.cs b/src/Nethermind/Nethermind.JsonRpc.Test/Modules/RpcModuleProviderTests.cs index 980d8f169c2..707fff9b4e0 100644 --- a/src/Nethermind/Nethermind.JsonRpc.Test/Modules/RpcModuleProviderTests.cs +++ b/src/Nethermind/Nethermind.JsonRpc.Test/Modules/RpcModuleProviderTests.cs @@ -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() { diff --git a/src/Nethermind/Nethermind.JsonRpc/JsonRpcContext.cs b/src/Nethermind/Nethermind.JsonRpc/JsonRpcContext.cs index 9f130a4bee6..c145607924d 100644 --- a/src/Nethermind/Nethermind.JsonRpc/JsonRpcContext.cs +++ b/src/Nethermind/Nethermind.JsonRpc/JsonRpcContext.cs @@ -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 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); @@ -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; + } + } } } diff --git a/src/Nethermind/Nethermind.JsonRpc/Modules/Eth/EthRpcModule.cs b/src/Nethermind/Nethermind.JsonRpc/Modules/Eth/EthRpcModule.cs index 0573300b66b..5c6b7b31e89 100644 --- a/src/Nethermind/Nethermind.JsonRpc/Modules/Eth/EthRpcModule.cs +++ b/src/Nethermind/Nethermind.JsonRpc/Modules/Eth/EthRpcModule.cs @@ -620,7 +620,9 @@ public ResultWrapper> 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>.Fail($"Too many logs requested. Max logs per response is {_rpcConfig.MaxLogsPerResponse}.", ErrorCodes.LimitExceeded); diff --git a/src/Nethermind/Nethermind.JsonRpc/WebSockets/JsonRpcSocketsClient.cs b/src/Nethermind/Nethermind.JsonRpc/WebSockets/JsonRpcSocketsClient.cs index 1ea4d983058..1176e351741 100644 --- a/src/Nethermind/Nethermind.JsonRpc/WebSockets/JsonRpcSocketsClient.cs +++ b/src/Nethermind/Nethermind.JsonRpc/WebSockets/JsonRpcSocketsClient.cs @@ -48,6 +48,7 @@ public override void Dispose() { base.Dispose(); _sendSemaphore.Dispose(); + _jsonRpcContext.Dispose(); Closed?.Invoke(this, EventArgs.Empty); } diff --git a/src/Nethermind/Nethermind.Runner/JsonRpc/Startup.cs b/src/Nethermind/Nethermind.Runner/JsonRpc/Startup.cs index b6d8b779302..5c6c24f6156 100644 --- a/src/Nethermind/Nethermind.Runner/JsonRpc/Startup.cs +++ b/src/Nethermind/Nethermind.Runner/JsonRpc/Startup.cs @@ -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)