Skip to content

Commit

Permalink
JSON RPC: fix missing params when all are optional (NethermindEth#7318)
Browse files Browse the repository at this point in the history
  • Loading branch information
LukaszRozmej authored Aug 8, 2024
1 parent 24d8e24 commit c374529
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
23 changes: 13 additions & 10 deletions src/Nethermind/Nethermind.JsonRpc/JsonRpcService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private async Task<JsonRpcResponse> ExecuteAsync(JsonRpcRequest request, string
bool hasMissing = false;
if (method.ExpectedParameters.Length > 0)
{
(parameters, hasMissing) = DeserializeParameters(method.ExpectedParameters, providedParameters, missingParamsCount);
(parameters, hasMissing) = DeserializeParameters(method.ExpectedParameters, providedParametersLength, providedParameters, missingParamsCount);
if (parameters is null)
{
if (_logger.IsWarn) _logger.Warn($"Incorrect JSON RPC parameters when calling {methodName} with params [{string.Join(", ", providedParameters)}]");
Expand Down Expand Up @@ -318,22 +318,25 @@ private void LogRequest(string methodName, JsonElement providedParameters, Expec
return executionParam;
}

private (object[]? parameters, bool hasMissing) DeserializeParameters(ExpectedParameter[] expectedParameters, JsonElement providedParameters, int missingParamsCount)
private (object[]? parameters, bool hasMissing) DeserializeParameters(
ExpectedParameter[] expectedParameters,
int providedParametersLength,
JsonElement providedParameters,
int missingParamsCount)
{
const int parallelThreshold = 4;
try
{
bool hasMissing = false;
int arrayLength = providedParameters.GetArrayLength();
int totalLength = arrayLength + missingParamsCount;
int totalLength = providedParametersLength + missingParamsCount;

if (totalLength == 0) return (Array.Empty<object>(), false);

object[] executionParameters = new object[totalLength];

if (arrayLength <= parallelThreshold)
if (providedParametersLength <= parallelThreshold)
{
for (int i = 0; i < arrayLength; i++)
for (int i = 0; i < providedParametersLength; i++)
{
JsonElement providedParameter = providedParameters[i];
ExpectedParameter expectedParameter = expectedParameters[i];
Expand All @@ -346,9 +349,9 @@ private void LogRequest(string methodName, JsonElement providedParameters, Expec
}
}
}
else if (arrayLength > parallelThreshold)
else if (providedParametersLength > parallelThreshold)
{
Parallel.For(0, arrayLength, (int i) =>
Parallel.For(0, providedParametersLength, (int i) =>
{
JsonElement providedParameter = providedParameters[i];
ExpectedParameter expectedParameter = expectedParameters[i];
Expand All @@ -362,11 +365,11 @@ private void LogRequest(string methodName, JsonElement providedParameters, Expec
});
}

for (int i = arrayLength; i < totalLength; i++)
for (int i = providedParametersLength; i < totalLength; i++)
{
executionParameters[i] = Type.Missing;
}
hasMissing |= arrayLength < totalLength;
hasMissing |= providedParametersLength < totalLength;
return (executionParameters, hasMissing);
}
catch (Exception e)
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 @@ -131,7 +131,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IJsonRpc
}
});

app.Run(async (ctx) =>
app.Run(async ctx =>
{
if (ctx.Request.Method == "GET")
{
Expand Down

0 comments on commit c374529

Please sign in to comment.