-
Notifications
You must be signed in to change notification settings - Fork 100
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
Integrate NeoFS into oracle module #518
Merged
Merged
Changes from all commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
6d9cc91
Integrate NeoFS into oracle module
doubiliu 96edad1
Rename
doubiliu 7103dd0
Fix bug
doubiliu 6918692
sync
doubiliu 29ad815
Fix bug
doubiliu 564a274
Fix UT
doubiliu 216e4a5
Clean using
shargon 6055b23
Optimize
shargon 69a9b96
Reorder using
shargon 168772e
change to base64
doubiliu 4fab776
change to random
doubiliu f2c4f1a
Merge branch 'AddFs' of github.com:doubiliu/neo-modules into AddFs
doubiliu 4271fef
Fix Conflicts
doubiliu 889063e
Fix bug
doubiliu 1e4ba46
Merge branch 'master' into AddFs
erikzhang 45b27f0
Rename
erikzhang c9d553a
change to single node
doubiliu 1737d7f
Merge branch 'master' into AddFs
doubiliu 600510d
Remove AttachWallet
erikzhang c969a80
Rename
erikzhang 7858eba
Remove empty line
erikzhang 6fcd803
Update OracleNeoFSProtocol.cs
erikzhang 3f14021
Fix bug
doubiliu b382515
Merge branch 'master' into AddFs
doubiliu f9e8d18
Merge branch 'AddFs' of github.com:doubiliu/neo-modules into AddFs
doubiliu eb5b3e2
fix bug
doubiliu 12ce5e3
neofs request timeout
320a00c
url check
80292e3
Update OracleNeoFSProtocol.cs
erikzhang 9362337
Update config.json
erikzhang 5a81e73
apply timeout
081ba3e
Fix memory leak
erikzhang 283ad69
async
erikzhang a73077d
update neofs api
e42ad1a
remove neofs request host check
d64199f
Fix bug of URL encode
doubiliu 5a38300
Merge branch 'AddFs' of github.com:doubiliu/neo-modules into AddFs
doubiliu b7c757d
Fix format
doubiliu 38d3348
add default salt, format
d459ce8
Return JSON string of ObjectHeader
53ca335
Merge branch 'master' into AddFs
7f54e78
Merge branch 'master' into AddFs
46cef90
Merge branch 'master' into AddFs
doubiliu 5402997
Optimize
erikzhang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
using Neo.Cryptography.ECC; | ||
using Neo.FileSystem.API.Client; | ||
using Neo.FileSystem.API.Client.ObjectParams; | ||
using Neo.FileSystem.API.Cryptography; | ||
using Neo.FileSystem.API.Refs; | ||
using Neo.Network.P2P.Payloads; | ||
using Neo.Wallets; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using System.Web; | ||
using Object = Neo.FileSystem.API.Object.Object; | ||
using Range = Neo.FileSystem.API.Object.Range; | ||
|
||
namespace Neo.Plugins | ||
{ | ||
class OracleNeoFSProtocol : IOracleProtocol | ||
{ | ||
private readonly System.Security.Cryptography.ECDsa privateKey; | ||
|
||
public OracleNeoFSProtocol(Wallet wallet, ECPoint[] oracles) | ||
{ | ||
byte[] key = oracles.Select(p => wallet.GetAccount(p)).Where(p => p is not null && p.HasKey && !p.Lock).FirstOrDefault().GetKey().PrivateKey; | ||
privateKey = key.LoadPrivateKey(); | ||
} | ||
|
||
public void Configure() | ||
{ | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
privateKey.Dispose(); | ||
} | ||
|
||
public async Task<(OracleResponseCode, string)> ProcessAsync(Uri uri, CancellationToken cancellation) | ||
{ | ||
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)); | ||
} | ||
catch (Exception e) | ||
{ | ||
Utility.Log(nameof(OracleNeoFSProtocol), LogLevel.Debug, $"NeoFS result: error,{e.Message}"); | ||
return (OracleResponseCode.Error, null); | ||
} | ||
} | ||
|
||
private Task<byte[]> GetAsync(Uri uri, string host, CancellationToken cancellation) | ||
{ | ||
string[] ps = uri.AbsolutePath.Split("/"); | ||
if (ps.Length < 2) throw new FormatException("Invalid neofs url"); | ||
ContainerID containerID = ContainerID.FromBase58String(ps[0]); | ||
ObjectID objectID = ObjectID.FromBase58String(ps[1]); | ||
Address objectAddr = new() | ||
{ | ||
ContainerId = containerID, | ||
ObjectId = objectID | ||
}; | ||
Client client = new(privateKey, host); | ||
var tokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellation); | ||
tokenSource.CancelAfter(Settings.Default.NeoFS.Timeout); | ||
if (ps.Length == 2) | ||
return GetPayloadAsync(client, objectAddr, tokenSource.Token); | ||
return ps[2] switch | ||
{ | ||
"range" => GetRangeAsync(client, objectAddr, ps.Skip(3).ToArray(), tokenSource.Token), | ||
"header" => GetHeaderAsync(client, objectAddr, tokenSource.Token), | ||
"hash" => GetHashAsync(client, objectAddr, ps.Skip(3).ToArray(), tokenSource.Token), | ||
_ => throw new Exception("invalid command") | ||
}; | ||
} | ||
|
||
private static async Task<byte[]> 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(); | ||
} | ||
|
||
private static Task<byte[]> 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 }); | ||
} | ||
|
||
private static async Task<byte[]> 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()); | ||
} | ||
|
||
private static async Task<byte[]> 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(); | ||
} | ||
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]; | ||
} | ||
|
||
private static Range ParseRange(string s) | ||
{ | ||
string url = HttpUtility.UrlDecode(s); | ||
int sepIndex = url.IndexOf("|"); | ||
if (sepIndex < 0) throw new Exception("object range is invalid (expected 'Offset|Length')"); | ||
ulong offset = ulong.Parse(url[..sepIndex]); | ||
ulong length = ulong.Parse(url[(sepIndex + 1)..]); | ||
return new Range() { Offset = offset, Length = length }; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need it be async here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the difference?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regardless of whether it is an
async
method, our purpose is to return aTask
. Now since it can return aTask
directly, it is not necessary as anasync
method. Becauseasync
methods have additional overhead (although very low).