Skip to content

Commit

Permalink
Merge branch 'master' into vo/client-gen-dotnet
Browse files Browse the repository at this point in the history
  • Loading branch information
vobradovich committed Oct 28, 2024
2 parents 50fac71 + 85fdc54 commit ece5890
Show file tree
Hide file tree
Showing 21 changed files with 894 additions and 130 deletions.
3 changes: 1 addition & 2 deletions net/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Ensure.That" Version="10.1.0" />
<PackageVersion Include="IsExternalInit" Version="1.0.3" />
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand All @@ -14,7 +13,7 @@
<PrivateAssets>all</PrivateAssets>
</PackageVersion>
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.2" />
<PackageVersion Include="RequiredMemberAttribute" Version="1.0.0" />
<PackageVersion Include="PolySharp" Version="1.14.1" />
<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" />
Expand Down
32 changes: 30 additions & 2 deletions net/src/Sails.Remoting.Abstractions/IRemoting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,49 @@ namespace Sails.Remoting.Abstractions;

public interface IRemoting
{
Task<(ActorId ProgramId, byte[] EncodedReply)> ActivateAsync(
/// <summary>
/// Activates/creates a program from previously uploaded code.
/// </summary>
/// <param name="codeId"></param>
/// <param name="salt"></param>
/// <param name="encodedPayload"></param>
/// <param name="gasLimit"></param>
/// <param name="value"></param>
/// <param name="cancellationToken"></param>
/// <returns>A task for obtaining activated program ID and SCALE-encoded reply.</returns>
Task<Task<(ActorId ProgramId, byte[] EncodedReply)>> ActivateAsync(
CodeId codeId,
IReadOnlyCollection<byte> salt,
IReadOnlyCollection<byte> encodedPayload,
GasUnit? gasLimit,
ValueUnit value,
CancellationToken cancellationToken);

Task<byte[]> MessageAsync(
/// <summary>
/// Sends a message to a program for execution.
/// </summary>
/// <param name="programId"></param>
/// <param name="encodedPayload"></param>
/// <param name="gasLimit"></param>
/// <param name="value"></param>
/// <param name="cancellationToken"></param>
/// <returns>A task for obtaining SCALE-encoded reply.</returns>
Task<Task<byte[]>> MessageAsync(
ActorId programId,
IReadOnlyCollection<byte> encodedPayload,
GasUnit? gasLimit,
ValueUnit value,
CancellationToken cancellationToken);

/// <summary>
/// Queries a program for information.
/// </summary>
/// <param name="programId"></param>
/// <param name="encodedPayload"></param>
/// <param name="gasLimit"></param>
/// <param name="value"></param>
/// <param name="cancellationToken"></param>
/// <returns>SCALE-encoded reply.</returns>
Task<byte[]> QueryAsync(
ActorId programId,
IReadOnlyCollection<byte> encodedPayload,
Expand Down
14 changes: 9 additions & 5 deletions net/src/Sails.Remoting.Abstractions/IRemotingExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
using Substrate.Gear.Api.Generated.Model.gprimitives;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using EnsureThat;
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 static class IRemotingExtensions
{
public static Task<(ActorId ProgramId, byte[] EncodedReply)> ActivateAsync(
/// <inheritdoc cref="IRemoting.ActivateAsync(CodeId, IReadOnlyCollection{byte}, IReadOnlyCollection{byte}, GasUnit?, ValueUnit, CancellationToken)"/>
public static Task<Task<(ActorId ProgramId, byte[] EncodedReply)>> ActivateAsync(
this IRemoting remoting,
CodeId codeId,
IReadOnlyCollection<byte> salt,
Expand All @@ -24,7 +26,8 @@ public static class IRemotingExtensions
ZeroValue,
cancellationToken);

public static Task<byte[]> MessageAsync(
/// <inheritdoc cref="IRemoting.MessageAsync(ActorId, IReadOnlyCollection{byte}, GasUnit?, ValueUnit, CancellationToken)"/>
public static Task<Task<byte[]>> MessageAsync(
this IRemoting remoting,
ActorId programId,
IReadOnlyCollection<byte> encodedPayload,
Expand All @@ -37,6 +40,7 @@ public static Task<byte[]> MessageAsync(
ZeroValue,
cancellationToken);

/// <inheritdoc cref="IRemoting.QueryAsync(ActorId, IReadOnlyCollection{byte}, GasUnit?, ValueUnit, CancellationToken)"/>
public static Task<byte[]> QueryAsync(
this IRemoting remoting,
ActorId programId,
Expand Down
14 changes: 14 additions & 0 deletions net/src/Sails.Remoting.Abstractions/IRemotingProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Substrate.NetApi.Model.Types;

namespace Sails.Remoting.Abstractions;

public interface IRemotingProvider
{
/// <summary>
/// Creates an instance implementing the <see cref="IRemoting"/> interface
/// with initial account for signing transactions.
/// </summary>
/// <param name="signingAccount"></param>
/// <returns></returns>
IRemoting CreateRemoting(Account signingAccount);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ namespace Sails.Remoting.DependencyInjection;

public static class IServiceCollectionExtensions
{
public static IServiceCollection AddRemotingViaSubstrateClient(
public static IServiceCollection AddRemotingViaNodeClient(
this IServiceCollection services,
RemotingViaSubstrateClientOptions options)
NodeClientOptions options)
{
EnsureArg.IsNotNull(services, nameof(services));
EnsureArg.IsNotNull(options, nameof(options));

services.AddSingleton(options);
services.AddSingleton<INodeClientProvider>(_ => new NodeClientProvider(options));

services.AddTransient<IRemoting, RemotingViaSubstrateClient>();
services.AddTransient<IRemotingProvider>(
serviceProvicer => new RemotingProvider(
signingAccount => new RemotingViaNodeClient(
serviceProvicer.GetRequiredService<INodeClientProvider>(),
signingAccount)));

return services;
}
Expand Down
15 changes: 15 additions & 0 deletions net/src/Sails.Remoting/INodeClientProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Threading;
using System.Threading.Tasks;
using Substrate.Gear.Api.Generated;

namespace Sails.Remoting;

internal interface INodeClientProvider
{
/// <summary>
/// Returns connected node client.
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<SubstrateClientExt> GetNodeClientAsync(CancellationToken cancellationToken);
}
36 changes: 36 additions & 0 deletions net/src/Sails.Remoting/NodeClientProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using EnsureThat;
using Sails.Remoting.Options;
using Substrate.Gear.Api.Generated;
using Substrate.NetApi.Model.Extrinsics;

namespace Sails.Remoting;

internal sealed class NodeClientProvider : IDisposable, INodeClientProvider
{
public NodeClientProvider(NodeClientOptions options)
{
EnsureArg.IsNotNull(options, nameof(options));
EnsureArg.IsNotNull(options.GearNodeUri, nameof(options.GearNodeUri));

this.nodeClient = new SubstrateClientExt(options.GearNodeUri, ChargeTransactionPayment.Default());
}

private readonly SubstrateClientExt nodeClient;

/// <inheritdoc/>
public void Dispose()
{
this.nodeClient.Dispose();
GC.SuppressFinalize(this);
}

/// <inheritdoc/>
public async Task<SubstrateClientExt> GetNodeClientAsync(CancellationToken cancellationToken)
{
await this.nodeClient.ConnectAsync(cancellationToken).ConfigureAwait(false);
return this.nodeClient;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Sails.Remoting.Options;

public sealed record RemotingViaSubstrateClientOptions
public sealed record NodeClientOptions
{
public Uri? GearNodeUri { get; init; }
}
26 changes: 26 additions & 0 deletions net/src/Sails.Remoting/RemotingProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using EnsureThat;
using Sails.Remoting.Abstractions;
using Substrate.NetApi.Model.Types;

namespace Sails.Remoting;

internal sealed class RemotingProvider : IRemotingProvider
{
public RemotingProvider(Func<Account, IRemoting> remotingFactory)
{
EnsureArg.IsNotNull(remotingFactory, nameof(remotingFactory));

this.remotingFactory = remotingFactory;
}

private readonly Func<Account, IRemoting> remotingFactory;

/// <inheritdoc/>
public IRemoting CreateRemoting(Account signingAccount)
{
EnsureArg.IsNotNull(signingAccount, nameof(signingAccount));

return this.remotingFactory(signingAccount);
}
}
Loading

0 comments on commit ece5890

Please sign in to comment.