-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(net): introduce abstractions for Remoting (#607)
- Loading branch information
1 parent
6e0b976
commit 69444fa
Showing
15 changed files
with
478 additions
and
5 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<Project> | ||
|
||
<PropertyGroup> | ||
<Nullable>enable</Nullable> | ||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> | ||
<LangVersion>12.0</LangVersion> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Roslynator.Analyzers"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Roslynator.Formatting.Analyzers"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
</Project> |
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,18 @@ | ||
<Project> | ||
|
||
<PropertyGroup> | ||
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally> | ||
<CentralPackageTransitivePinningEnabled>true</CentralPackageTransitivePinningEnabled> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageVersion Include="Ensure.That" Version="10.1.0" /> | ||
<PackageVersion Include="IsExternalInit" Version="1.0.3" /> | ||
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.2" /> | ||
<PackageVersion Include="RequiredMemberAttribute" Version="1.0.0" /> | ||
<PackageVersion Include="Roslynator.Analyzers" Version="4.12.8" /> | ||
<PackageVersion Include="Roslynator.Formatting.Analyzers" Version="4.12.8" /> | ||
<PackageVersion Include="Substrate.NET.API" Version="0.9.24-rc6" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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 |
---|---|---|
@@ -1,10 +1,7 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>netstandard2.1;net8.0</TargetFrameworks> | ||
<Nullable>enable</Nullable> | ||
<WarningsAsErrors>nullable;4014</WarningsAsErrors> | ||
<LangVersion>latest</LangVersion> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
</Project> |
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,33 @@ | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Substrate.Gear.Api.Generated.Model.gprimitives; | ||
using GasUnit = Substrate.NetApi.Model.Types.Primitive.U64; | ||
using ValueUnit = Substrate.NetApi.Model.Types.Primitive.U128; | ||
|
||
namespace Sails.Remoting.Abstractions; | ||
|
||
public interface IRemoting | ||
{ | ||
Task<(ActorId ProgramId, byte[] EncodedReply)> ActivateAsync( | ||
CodeId codeId, | ||
IReadOnlyCollection<byte> salt, | ||
IReadOnlyCollection<byte> encodedPayload, | ||
GasUnit? gasLimit, | ||
ValueUnit value, | ||
CancellationToken cancellationToken); | ||
|
||
Task<byte[]> MessageAsync( | ||
ActorId programId, | ||
IReadOnlyCollection<byte> encodedPayload, | ||
GasUnit? gasLimit, | ||
ValueUnit value, | ||
CancellationToken cancellationToken); | ||
|
||
Task<byte[]> QueryAsync( | ||
ActorId programId, | ||
IReadOnlyCollection<byte> encodedPayload, | ||
GasUnit? gasLimit, | ||
ValueUnit value, | ||
CancellationToken cancellationToken); | ||
} |
54 changes: 54 additions & 0 deletions
54
net/src/Sails.Remoting.Abstractions/IRemotingExtensions.cs
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,54 @@ | ||
using Substrate.Gear.Api.Generated.Model.gprimitives; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using System.Threading; | ||
using EnsureThat; | ||
using ValueUnit = Substrate.NetApi.Model.Types.Primitive.U128; | ||
|
||
namespace Sails.Remoting.Abstractions; | ||
|
||
public static class IRemotingExtensions | ||
{ | ||
public static Task<(ActorId ProgramId, byte[] EncodedReply)> ActivateAsync( | ||
this IRemoting remoting, | ||
CodeId codeId, | ||
IReadOnlyCollection<byte> salt, | ||
IReadOnlyCollection<byte> encodedPayload, | ||
CancellationToken cancellationToken) | ||
=> EnsureArg.IsNotNull(remoting, nameof(remoting)) | ||
.ActivateAsync( | ||
codeId, | ||
salt, | ||
encodedPayload, | ||
gasLimit: null, | ||
ZeroValue, | ||
cancellationToken); | ||
|
||
public static Task<byte[]> MessageAsync( | ||
this IRemoting remoting, | ||
ActorId programId, | ||
IReadOnlyCollection<byte> encodedPayload, | ||
CancellationToken cancellationToken) | ||
=> EnsureArg.IsNotNull(remoting, nameof(remoting)) | ||
.MessageAsync( | ||
programId, | ||
encodedPayload, | ||
gasLimit: null, | ||
ZeroValue, | ||
cancellationToken); | ||
|
||
public static Task<byte[]> QueryAsync( | ||
this IRemoting remoting, | ||
ActorId programId, | ||
IReadOnlyCollection<byte> encodedPayload, | ||
CancellationToken cancellationToken) | ||
=> EnsureArg.IsNotNull(remoting, nameof(remoting)) | ||
.QueryAsync( | ||
programId, | ||
encodedPayload, | ||
gasLimit: null, | ||
ZeroValue, | ||
cancellationToken); | ||
|
||
private static readonly ValueUnit ZeroValue = new(0); | ||
} |
15 changes: 15 additions & 0 deletions
15
net/src/Sails.Remoting.Abstractions/Sails.Remoting.Abstractions.csproj
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,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Ensure.That" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Substrate.Gear.Api\Substrate.Gear.Api.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
23 changes: 23 additions & 0 deletions
23
net/src/Sails.Remoting/DependencyInjection/IServiceCollectionExtensions.cs
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,23 @@ | ||
using EnsureThat; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Sails.Remoting.Abstractions; | ||
using Sails.Remoting.Options; | ||
|
||
namespace Sails.Remoting.DependencyInjection; | ||
|
||
public static class IServiceCollectionExtensions | ||
{ | ||
public static IServiceCollection AddRemotingViaSubstrateClient( | ||
this IServiceCollection services, | ||
RemotingViaSubstrateClientOptions options) | ||
{ | ||
EnsureArg.IsNotNull(services, nameof(services)); | ||
EnsureArg.IsNotNull(options, nameof(options)); | ||
|
||
services.AddSingleton(options); | ||
|
||
services.AddTransient<IRemoting, RemotingViaSubstrateClient>(); | ||
|
||
return services; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
net/src/Sails.Remoting/Options/RemotingViaSubstrateClientOptions.cs
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,8 @@ | ||
using System; | ||
|
||
namespace Sails.Remoting.Options; | ||
|
||
public sealed record RemotingViaSubstrateClientOptions | ||
{ | ||
public Uri? GearNodeUri { get; init; } | ||
} |
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,88 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using EnsureThat; | ||
using Sails.Remoting.Abstractions; | ||
using Sails.Remoting.Options; | ||
using Substrate.Gear.Api.Generated; | ||
using Substrate.Gear.Api.Generated.Model.gprimitives; | ||
using Substrate.NetApi.Model.Extrinsics; | ||
using GasUnit = Substrate.NetApi.Model.Types.Primitive.U64; | ||
using ValueUnit = Substrate.NetApi.Model.Types.Primitive.U128; | ||
|
||
namespace Sails.Remoting; | ||
|
||
internal sealed class RemotingViaSubstrateClient : IDisposable, IRemoting | ||
{ | ||
public RemotingViaSubstrateClient(RemotingViaSubstrateClientOptions options) | ||
{ | ||
EnsureArg.IsNotNull(options, nameof(options)); | ||
EnsureArg.IsNotNull(options.GearNodeUri, nameof(options.GearNodeUri)); | ||
|
||
this.nodeClient = new SubstrateClientExt(options.GearNodeUri, ChargeTransactionPayment.Default()); | ||
this.isNodeClientConnected = false; | ||
} | ||
|
||
private readonly SubstrateClientExt nodeClient; | ||
private bool isNodeClientConnected; | ||
|
||
public void Dispose() | ||
{ | ||
this.nodeClient.Dispose(); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
public async Task<(ActorId ProgramId, byte[] EncodedReply)> ActivateAsync( | ||
CodeId codeId, | ||
IReadOnlyCollection<byte> salt, | ||
IReadOnlyCollection<byte> encodedPayload, | ||
GasUnit? gasLimit, | ||
ValueUnit value, | ||
CancellationToken cancellationToken) | ||
{ | ||
EnsureArg.IsNotNull(codeId, nameof(codeId)); | ||
EnsureArg.IsNotNull(salt, nameof(salt)); | ||
EnsureArg.IsNotNull(encodedPayload, nameof(encodedPayload)); | ||
|
||
await this.GetConnectedNodeClientAsync(cancellationToken).ConfigureAwait(false); | ||
|
||
throw new NotImplementedException(); | ||
} | ||
|
||
public Task<byte[]> MessageAsync( | ||
ActorId programId, | ||
IReadOnlyCollection<byte> encodedPayload, | ||
GasUnit? gasLimit, | ||
ValueUnit value, | ||
CancellationToken cancellationToken) | ||
{ | ||
EnsureArg.IsNotNull(programId, nameof(programId)); | ||
EnsureArg.IsNotNull(encodedPayload, nameof(encodedPayload)); | ||
|
||
throw new NotImplementedException(); | ||
} | ||
|
||
public Task<byte[]> QueryAsync( | ||
ActorId programId, | ||
IReadOnlyCollection<byte> encodedPayload, | ||
GasUnit? gasLimit, | ||
ValueUnit value, | ||
CancellationToken cancellationToken) | ||
{ | ||
EnsureArg.IsNotNull(programId, nameof(programId)); | ||
EnsureArg.IsNotNull(encodedPayload, nameof(encodedPayload)); | ||
|
||
throw new NotImplementedException(); | ||
} | ||
|
||
private async Task<SubstrateClientExt> GetConnectedNodeClientAsync(CancellationToken cancellationToken) | ||
{ | ||
if (!this.isNodeClientConnected) | ||
{ | ||
await this.nodeClient.ConnectAsync(cancellationToken).ConfigureAwait(false); | ||
this.isNodeClientConnected = true; | ||
} | ||
return this.nodeClient; | ||
} | ||
} |
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,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="IsExternalInit"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" /> | ||
</ItemGroup> | ||
|
||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Sails.Remoting.Abstractions\Sails.Remoting.Abstractions.csproj" /> | ||
<ProjectReference Include="..\Substrate.Gear.Client\Substrate.Gear.Client.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,21 @@ | ||
using GasUnit = Substrate.NetApi.Model.Types.Primitive.U64; | ||
|
||
namespace Substrate.Gear.Client; | ||
|
||
public sealed record GasInfo | ||
{ | ||
/// Represents minimum gas limit required for execution. | ||
public required GasUnit MinLimit { get; init; } | ||
/// Gas amount that we reserve for some other on-chain interactions. | ||
public required GasUnit Reserved { get; init; } | ||
/// Contains number of gas burned during message processing. | ||
public required GasUnit Burned { get; init; } | ||
/// The value may be returned if a program happens to be executed | ||
/// the second or next time in a block. | ||
public required GasUnit MayBeReturned { get; init; } | ||
/// Was the message placed into waitlist at the end of calculating. | ||
/// | ||
/// This flag shows, that `min_limit` makes sense and have some guarantees | ||
/// only before insertion into waitlist. | ||
public bool IsInWaitList { get; init; } | ||
} |
Oops, something went wrong.