Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Oracle] Optimize neofs return type #554

Merged
merged 12 commits into from
Mar 15, 2021
36 changes: 23 additions & 13 deletions src/OracleService/Protocols/OracleNeoFSProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public void Dispose()
Utility.Log(nameof(OracleNeoFSProtocol), LogLevel.Debug, $"Request: {uri.AbsoluteUri}");
try
{
byte[] res = await GetAsync(uri, Settings.Default.NeoFS.EndPoint, cancellation);
Utility.Log(nameof(OracleNeoFSProtocol), LogLevel.Debug, $"NeoFS result: {res.ToHexString()}");
return (OracleResponseCode.Success, Convert.ToBase64String(res));
string res = await GetAsync(uri, Settings.Default.NeoFS.EndPoint, cancellation);
Utility.Log(nameof(OracleNeoFSProtocol), LogLevel.Debug, $"NeoFS result: {res}");
return (OracleResponseCode.Success, res);
}
catch (Exception e)
{
Expand All @@ -51,7 +51,16 @@ public void Dispose()
}
}

private Task<byte[]> GetAsync(Uri uri, string host, CancellationToken cancellation)

/// <summary>
/// GetAsync returns neofs object from the provided url.
/// If Command is not provided, full object is requested.
/// </summary>
/// <param name="uri">URI scheme is "neofs:<Container-ID>/<Object-ID>/<Command>/<offset|length>".</param>
/// <param name="host">Client host.</param>
/// <param name="cancellation">Cancellation token object.</param>
/// <returns>Returns neofs object.</returns>
private Task<string> GetAsync(Uri uri, string host, CancellationToken cancellation)
{
string[] ps = uri.AbsolutePath.Split("/");
if (ps.Length < 2) throw new FormatException("Invalid neofs url");
Expand All @@ -76,36 +85,37 @@ private Task<byte[]> GetAsync(Uri uri, string host, CancellationToken cancellati
};
}

private static async Task<byte[]> GetPayloadAsync(Client client, Address addr, CancellationToken cancellation)
private static async Task<string> GetPayloadAsync(Client client, Address addr, CancellationToken cancellation)
{
Object obj = await client.GetObject(cancellation, new GetObjectParams() { Address = addr }, new CallOptions { Ttl = 2 });
return obj.Payload.ToByteArray();
return obj.Payload.ToString(Utility.StrictUTF8);
}

private static Task<byte[]> GetRangeAsync(Client client, Address addr, string[] ps, CancellationToken cancellation)
private static async Task<string> GetRangeAsync(Client client, Address addr, string[] ps, CancellationToken cancellation)
{
if (ps.Length == 0) throw new FormatException("missing object range (expected 'Offset|Length')");
Range range = ParseRange(ps[0]);
return client.GetObjectPayloadRangeData(cancellation, new RangeDataParams() { Address = addr, Range = range }, new CallOptions { Ttl = 2 });
var res = await client.GetObjectPayloadRangeData(cancellation, new RangeDataParams() { Address = addr, Range = range }, new CallOptions { Ttl = 2 });
return Utility.StrictUTF8.GetString(res);
}

private static async Task<byte[]> GetHeaderAsync(Client client, Address addr, CancellationToken cancellation)
private static async Task<string> GetHeaderAsync(Client client, Address addr, CancellationToken cancellation)
{
var obj = await client.GetObjectHeader(cancellation, new ObjectHeaderParams() { Address = addr }, new CallOptions { Ttl = 2 });
return Utility.StrictUTF8.GetBytes(obj.ToString());
return obj.ToString();
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
}

private static async Task<byte[]> GetHashAsync(Client client, Address addr, string[] ps, CancellationToken cancellation)
private static async Task<string> GetHashAsync(Client client, Address addr, string[] ps, CancellationToken cancellation)
{
if (ps.Length == 0 || ps[0] == "")
{
Object obj = await client.GetObjectHeader(cancellation, new ObjectHeaderParams() { Address = addr }, new CallOptions { Ttl = 2 });
return obj.PayloadChecksum.Sum.ToByteArray();
return $"\"{new UInt256(obj.PayloadChecksum.Sum.ToByteArray())}\"";
}
Range range = ParseRange(ps[0]);
List<byte[]> hashes = await client.GetObjectPayloadRangeHash(cancellation, new RangeChecksumParams() { Address = addr, Ranges = new List<Range>() { range }, Type = ChecksumType.Sha256, Salt = Array.Empty<byte>() }, new CallOptions { Ttl = 2 });
if (hashes.Count == 0) throw new Exception("empty response, object range is invalid (expected 'Offset|Length')");
return hashes[0];
return $"\"{new UInt256(hashes[0])}\"";
}

private static Range ParseRange(string s)
Expand Down