[#28] Client: Apply code optimizations

Signed-off-by: Pavel Gross <p.gross@yadro.com>
This commit is contained in:
Pavel Gross 2024-11-18 16:57:20 +03:00
parent 766f61a5f7
commit 749000a090
57 changed files with 845 additions and 1116 deletions

View file

@ -1,6 +1,4 @@
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FrostFS.Netmap;
@ -47,10 +45,6 @@ internal sealed class NetmapServiceProvider : ContextAccessor
internal async Task<FrostFsNodeInfo> GetLocalNodeInfoAsync(PrmNodeInfo args)
{
var ctx = args.Context!;
ctx.Key ??= ClientContext.Key?.ECDsaKey;
if (ctx.Key == null)
throw new ArgumentNullException(nameof(args), "Key is null");
var request = new LocalNodeInfoRequest
{
@ -58,7 +52,7 @@ internal sealed class NetmapServiceProvider : ContextAccessor
};
request.AddMetaHeader(args.XHeaders);
request.Sign(ctx.Key);
request.Sign(ClientContext.Key.ECDsaKey);
var response = await netmapServiceClient.LocalNodeInfoAsync(request, null, ctx.Deadline, ctx.CancellationToken);
@ -69,15 +63,10 @@ internal sealed class NetmapServiceProvider : ContextAccessor
internal async Task<NetworkInfoResponse> GetNetworkInfoAsync(CallContext ctx)
{
ctx.Key ??= ClientContext.Key?.ECDsaKey;
if (ctx.Key == null)
throw new ArgumentNullException(nameof(ctx), "Key is null");
var request = new NetworkInfoRequest();
request.AddMetaHeader(null);
request.Sign(ctx.Key);
request.Sign(ClientContext.Key.ECDsaKey);
var response = await netmapServiceClient.NetworkInfoAsync(request, null, ctx.Deadline, ctx.CancellationToken)
.ConfigureAwait(false);
@ -90,15 +79,11 @@ internal sealed class NetmapServiceProvider : ContextAccessor
internal async Task<FrostFsNetmapSnapshot> GetNetmapSnapshotAsync(PrmNetmapSnapshot args)
{
var ctx = args.Context!;
ctx.Key ??= ClientContext.Key?.ECDsaKey;
if (ctx.Key == null)
throw new ArgumentNullException(nameof(args), "Key is null");
var request = new NetmapSnapshotRequest();
request.AddMetaHeader(args.XHeaders);
request.Sign(ctx.Key);
request.Sign(ClientContext.Key.ECDsaKey);
var response = await netmapServiceClient.NetmapSnapshotAsync(request, null, ctx.Deadline, ctx.CancellationToken);
@ -107,12 +92,16 @@ internal sealed class NetmapServiceProvider : ContextAccessor
return response.ToModel();
}
private static bool GetBoolValue(byte[] bytes)
private static bool GetBoolValue(ReadOnlySpan<byte> bytes)
{
return bytes.Any(b => b != 0);
for (int i = bytes.Length - 1; i >= 0; i--)
if (bytes[i] != 0)
return true;
return false;
}
private static ulong GetLongValue(byte[] bytes)
private static ulong GetLongValue(ReadOnlySpan<byte> bytes)
{
ulong val = 0;
for (var i = bytes.Length - 1; i >= 0; i--)
@ -123,24 +112,50 @@ internal sealed class NetmapServiceProvider : ContextAccessor
private static void SetNetworksParam(Parameter param, NetworkSettings settings)
{
var key = Encoding.UTF8.GetString(param.Key.ToByteArray());
var key = param.Key.ToStringUtf8();
var valueBytes = param.Value.ToByteArray();
var valueBytes = param.Value.Span;
switch (key)
{
case "AuditFee": settings.AuditFee = GetLongValue(valueBytes); break;
case "BasicIncomeRate": settings.BasicIncomeRate = GetLongValue(valueBytes); break;
case "ContainerFee": settings.ContainerFee = GetLongValue(valueBytes); break;
case "ContainerAliasFee": settings.ContainerAliasFee = GetLongValue(valueBytes); break;
case "EpochDuration": settings.EpochDuration = GetLongValue(valueBytes); break;
case "InnerRingCandidateFee": settings.InnerRingCandidateFee = GetLongValue(valueBytes); break;
case "MaxECDataCount": settings.MaxECDataCount = GetLongValue(valueBytes); break;
case "MaxECParityCount": settings.MaxECParityCount = GetLongValue(valueBytes); break;
case "MaxObjectSize": settings.MaxObjectSize = GetLongValue(valueBytes); break;
case "WithdrawFee": settings.WithdrawFee = GetLongValue(valueBytes); break;
case "HomomorphicHashingDisabled": settings.HomomorphicHashingDisabled = GetBoolValue(valueBytes); break;
case "MaintenanceModeAllowed": settings.MaintenanceModeAllowed = GetBoolValue(valueBytes); break;
default: settings.UnnamedSettings.Add(key, valueBytes); break;
case "AuditFee":
settings.AuditFee = GetLongValue(valueBytes);
break;
case "BasicIncomeRate":
settings.BasicIncomeRate = GetLongValue(valueBytes);
break;
case "ContainerFee":
settings.ContainerFee = GetLongValue(valueBytes);
break;
case "ContainerAliasFee":
settings.ContainerAliasFee = GetLongValue(valueBytes);
break;
case "EpochDuration":
settings.EpochDuration = GetLongValue(valueBytes);
break;
case "InnerRingCandidateFee":
settings.InnerRingCandidateFee = GetLongValue(valueBytes);
break;
case "MaxECDataCount":
settings.MaxECDataCount = GetLongValue(valueBytes);
break;
case "MaxECParityCount":
settings.MaxECParityCount = GetLongValue(valueBytes);
break;
case "MaxObjectSize":
settings.MaxObjectSize = GetLongValue(valueBytes);
break;
case "WithdrawFee":
settings.WithdrawFee = GetLongValue(valueBytes);
break;
case "HomomorphicHashingDisabled":
settings.HomomorphicHashingDisabled = GetBoolValue(valueBytes);
break;
case "MaintenanceModeAllowed":
settings.MaintenanceModeAllowed = GetBoolValue(valueBytes);
break;
default:
settings.UnnamedSettings.Add(key, valueBytes.ToArray());
break;
}
}
}