Skip to content

Commit

Permalink
Address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisInSky committed Oct 28, 2024
1 parent afc30b8 commit 2520bff
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 55 deletions.
7 changes: 0 additions & 7 deletions net/src/Sails.Remoting.Abstractions/IRemoting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,13 @@
using System.Threading;
using System.Threading.Tasks;
using Substrate.Gear.Api.Generated.Model.gprimitives;
using Substrate.NetApi.Model.Types;
using GasUnit = Substrate.NetApi.Model.Types.Primitive.U64;
using ValueUnit = Substrate.NetApi.Model.Types.Primitive.U128;

namespace Sails.Remoting.Abstractions;

public interface IRemoting
{
/// <summary>
/// Sets account for signing transactions.
/// </summary>
/// <param name="signingAccount"></param>
void SetSigningAccount(Account signingAccount);

/// <summary>
/// Activates/creates a program from previously uploaded code.
/// </summary>
Expand Down
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<INodeClientProvider>(_ => new NodeClientProvider(options));

services.AddTransient<IRemotingProvider>(
_ => new RemotingProvider(
signingAccount => new RemotingViaSubstrateClient(options, signingAccount)));
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; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
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.frame_system;
using Substrate.Gear.Api.Generated.Model.gprimitives;
Expand All @@ -14,7 +13,6 @@
using Substrate.Gear.Client;
using Substrate.Gear.Client.Model.Types;
using Substrate.Gear.Client.Model.Types.Base;
using Substrate.NetApi.Model.Extrinsics;
using Substrate.NetApi.Model.Types;
using Substrate.NetApi.Model.Types.Base;
using Substrate.NetApi.Model.Types.Primitive;
Expand All @@ -30,24 +28,22 @@

namespace Sails.Remoting;

internal sealed class RemotingViaSubstrateClient : IDisposable, IRemoting
internal sealed class RemotingViaNodeClient : IRemoting
{
/// <summary>
/// Creates an instance implementing the <see cref="IRemoting"/> interface via <see cref="SubstrateClientExt"/>
/// with initial account for signing transactions.
/// </summary>
/// <param name="options"></param>
/// <param name="nodeClientProvider"></param>
/// <param name="signingAccount"></param>
public RemotingViaSubstrateClient(
RemotingViaSubstrateClientOptions options,
public RemotingViaNodeClient(
INodeClientProvider nodeClientProvider,
Account signingAccount)
{
EnsureArg.IsNotNull(options, nameof(options));
EnsureArg.IsNotNull(options.GearNodeUri, nameof(options.GearNodeUri));
EnsureArg.IsNotNull(nodeClientProvider, nameof(nodeClientProvider));
EnsureArg.IsNotNull(signingAccount, nameof(signingAccount));

this.nodeClient = new SubstrateClientExt(options.GearNodeUri, ChargeTransactionPayment.Default());
this.isNodeClientConnected = false;
this.nodeClientProvider = nodeClientProvider;
this.signingAccount = signingAccount;
}

Expand All @@ -56,23 +52,8 @@ public RemotingViaSubstrateClient(

private static readonly GasUnit BlockGasLimit = new GearGasConstants().BlockGasLimit();

private readonly SubstrateClientExt nodeClient;
private Account signingAccount;
private bool isNodeClientConnected;

public void Dispose()
{
this.nodeClient.Dispose();
GC.SuppressFinalize(this);
}

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

this.signingAccount = signingAccount;
}
private readonly INodeClientProvider nodeClientProvider;
private readonly Account signingAccount;

/// <inheritdoc/>
public async Task<Task<(ActorId ProgramId, byte[] EncodedReply)>> ActivateAsync(
Expand All @@ -87,7 +68,7 @@ public void SetSigningAccount(Account signingAccount)
EnsureArg.IsNotNull(salt, nameof(salt));
EnsureArg.IsNotNull(encodedPayload, nameof(encodedPayload));

var nodeClient = await this.GetConnectedNodeClientAsync(cancellationToken).ConfigureAwait(false);
var nodeClient = await this.nodeClientProvider.GetNodeClientAsync(cancellationToken).ConfigureAwait(false);

gasLimit ??= (await nodeClient.CalculateGasForCreateProgramAsync(
this.signingAccount.GetPublicKey(),
Expand All @@ -106,15 +87,15 @@ public void SetSigningAccount(Account signingAccount)
value,
keep_alive: new Bool(true));

var (blockHash, extrinsicHash, extrinsicIdx) = await this.nodeClient.ExecuteExtrinsicAsync(
var (blockHash, extrinsicHash, extrinsicIdx) = await nodeClient.ExecuteExtrinsicAsync(
this.signingAccount,
createProgram,
DefaultExtrinsicTtlInBlocks,
cancellationToken)
.ConfigureAwait(false);

// It can be moved inside the task to return.
var blockEvents = await this.nodeClient.ListBlockEventsAsync(
var blockEvents = await nodeClient.ListBlockEventsAsync(
blockHash,
cancellationToken)
.ConfigureAwait(false);
Expand Down Expand Up @@ -159,7 +140,7 @@ public async Task<Task<byte[]>> MessageAsync(
EnsureArg.IsNotNull(programId, nameof(programId));
EnsureArg.IsNotNull(encodedPayload, nameof(encodedPayload));

var nodeClient = await this.GetConnectedNodeClientAsync(cancellationToken).ConfigureAwait(false);
var nodeClient = await this.nodeClientProvider.GetNodeClientAsync(cancellationToken).ConfigureAwait(false);

gasLimit ??= (await nodeClient.CalculateGasForHandleAsync(
this.signingAccount.GetPublicKey(),
Expand All @@ -177,7 +158,7 @@ public async Task<Task<byte[]>> MessageAsync(
value,
keep_alive: new Bool(true));

var (blockHash, extrinsicHash, extrinsicIdx) = await this.nodeClient.ExecuteExtrinsicAsync(
var (blockHash, extrinsicHash, extrinsicIdx) = await nodeClient.ExecuteExtrinsicAsync(
this.signingAccount,
sendMessage,
DefaultExtrinsicTtlInBlocks,
Expand All @@ -204,7 +185,7 @@ public async Task<byte[]> QueryAsync(
EnsureArg.IsNotNull(programId, nameof(programId));
EnsureArg.IsNotNull(encodedPayload, nameof(encodedPayload));

var nodeClient = await this.GetConnectedNodeClientAsync(cancellationToken).ConfigureAwait(false);
var nodeClient = await this.nodeClientProvider.GetNodeClientAsync(cancellationToken).ConfigureAwait(false);

gasLimit ??= BlockGasLimit;

Expand All @@ -221,14 +202,4 @@ public async Task<byte[]> QueryAsync(

return replyInfo.EncodedPayload;
}

private async Task<SubstrateClientExt> GetConnectedNodeClientAsync(CancellationToken cancellationToken)
{
if (!this.isNodeClientConnected)
{
await this.nodeClient.ConnectAsync(cancellationToken).ConfigureAwait(false);
this.isNodeClientConnected = true;
}
return this.nodeClient;
}
}

0 comments on commit 2520bff

Please sign in to comment.