Batching requests
MODIFY
WalletWasabi.Gui/Rpc/JsonRpcRequest.cs
WalletWasabi.Gui/Rpc/JsonRpcRequest.cs
@@ -78,16 +78,19 @@ namespace WalletWasabi.Gui.Rpc
/// Parses the json rpc request giving back the deserialized JsonRpcRequest instance.
/// Return true if the deserialization was successful, otherwise false.
/// </summary>
- public static bool TryParse(string rawJson, out JsonRpcRequest request)
+ public static bool TryParse(string rawJson, out JsonRpcRequest[] requests, out bool isBatch)
{
try
{
- request = JsonConvert.DeserializeObject<JsonRpcRequest>(rawJson);
+ isBatch = rawJson.TrimStart().StartsWith("[");
+ rawJson = isBatch ? rawJson : $"[{rawJson}]";
+ requests = JsonConvert.DeserializeObject<JsonRpcRequest[]>(rawJson);
return true;
}
catch (JsonException)
{
- request = null;
+ requests = null;
+ isBatch = false;
return false;
}
}

MODIFY
WalletWasabi.Gui/Rpc/JsonRpcRequestHandler.cs
WalletWasabi.Gui/Rpc/JsonRpcRequestHandler.cs
@@ -48,10 +48,21 @@ namespace WalletWasabi.Gui.Rpc
/// <returns>The response that, after serialization, is returned as response.</returns>
public async Task<string> HandleAsync(string body, CancellationToken cancellationToken)
{
- if (!JsonRpcRequest.TryParse(body, out var jsonRpcRequest))
+ if (!JsonRpcRequest.TryParse(body, out var jsonRpcRequests, out var isBatch))
{
return JsonRpcResponse.CreateErrorResponse(null, JsonRpcErrorCodes.ParseError).ToJson(DefaultSettings);
}
+ var results = new List<string>();
+ foreach(var jsonRpcRequest in jsonRpcRequests)
+ {
+ cancellationToken.ThrowIfCancellationRequested();
+ results.Add(await HandleRequestAsync(jsonRpcRequest, cancellationToken));
+ }
+ return isBatch ? $"[{string.Join(",", results)}]" : results[0];
+ }
+
+ private async Task<string> HandleRequestAsync(JsonRpcRequest jsonRpcRequest, CancellationToken cancellationToken)
+ {
var methodName = jsonRpcRequest.Method;
if (!MetadataProvider.TryGetMetadata(methodName, out var prodecureMetadata))

Batching requests