From 2520bffa72369f6a0efb60803230b3cf771bdb68 Mon Sep 17 00:00:00 2001 From: Dennis Dyatlov Date: Mon, 28 Oct 2024 11:24:35 +0000 Subject: [PATCH] Address PR comments --- .../Sails.Remoting.Abstractions/IRemoting.cs | 7 --- .../IServiceCollectionExtensions.cs | 12 ++-- net/src/Sails.Remoting/INodeClientProvider.cs | 15 +++++ net/src/Sails.Remoting/NodeClientProvider.cs | 36 ++++++++++++ ...eClientOptions.cs => NodeClientOptions.cs} | 2 +- ...rateClient.cs => RemotingViaNodeClient.cs} | 57 +++++-------------- 6 files changed, 74 insertions(+), 55 deletions(-) create mode 100644 net/src/Sails.Remoting/INodeClientProvider.cs create mode 100644 net/src/Sails.Remoting/NodeClientProvider.cs rename net/src/Sails.Remoting/Options/{RemotingViaSubstrateClientOptions.cs => NodeClientOptions.cs} (64%) rename net/src/Sails.Remoting/{RemotingViaSubstrateClient.cs => RemotingViaNodeClient.cs} (77%) diff --git a/net/src/Sails.Remoting.Abstractions/IRemoting.cs b/net/src/Sails.Remoting.Abstractions/IRemoting.cs index 675ca579..a597cb14 100644 --- a/net/src/Sails.Remoting.Abstractions/IRemoting.cs +++ b/net/src/Sails.Remoting.Abstractions/IRemoting.cs @@ -2,7 +2,6 @@ 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; @@ -10,12 +9,6 @@ namespace Sails.Remoting.Abstractions; public interface IRemoting { - /// - /// Sets account for signing transactions. - /// - /// - void SetSigningAccount(Account signingAccount); - /// /// Activates/creates a program from previously uploaded code. /// diff --git a/net/src/Sails.Remoting/DependencyInjection/IServiceCollectionExtensions.cs b/net/src/Sails.Remoting/DependencyInjection/IServiceCollectionExtensions.cs index 4a1d6e39..0f398b65 100644 --- a/net/src/Sails.Remoting/DependencyInjection/IServiceCollectionExtensions.cs +++ b/net/src/Sails.Remoting/DependencyInjection/IServiceCollectionExtensions.cs @@ -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(_ => new NodeClientProvider(options)); + services.AddTransient( - _ => new RemotingProvider( - signingAccount => new RemotingViaSubstrateClient(options, signingAccount))); + serviceProvicer => new RemotingProvider( + signingAccount => new RemotingViaNodeClient( + serviceProvicer.GetRequiredService(), + signingAccount))); return services; } diff --git a/net/src/Sails.Remoting/INodeClientProvider.cs b/net/src/Sails.Remoting/INodeClientProvider.cs new file mode 100644 index 00000000..0ddc6ba8 --- /dev/null +++ b/net/src/Sails.Remoting/INodeClientProvider.cs @@ -0,0 +1,15 @@ +using System.Threading; +using System.Threading.Tasks; +using Substrate.Gear.Api.Generated; + +namespace Sails.Remoting; + +internal interface INodeClientProvider +{ + /// + /// Returns connected node client. + /// + /// + /// + Task GetNodeClientAsync(CancellationToken cancellationToken); +} diff --git a/net/src/Sails.Remoting/NodeClientProvider.cs b/net/src/Sails.Remoting/NodeClientProvider.cs new file mode 100644 index 00000000..30b2b410 --- /dev/null +++ b/net/src/Sails.Remoting/NodeClientProvider.cs @@ -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; + + /// + public void Dispose() + { + this.nodeClient.Dispose(); + GC.SuppressFinalize(this); + } + + /// + public async Task GetNodeClientAsync(CancellationToken cancellationToken) + { + await this.nodeClient.ConnectAsync(cancellationToken).ConfigureAwait(false); + return this.nodeClient; + } +} diff --git a/net/src/Sails.Remoting/Options/RemotingViaSubstrateClientOptions.cs b/net/src/Sails.Remoting/Options/NodeClientOptions.cs similarity index 64% rename from net/src/Sails.Remoting/Options/RemotingViaSubstrateClientOptions.cs rename to net/src/Sails.Remoting/Options/NodeClientOptions.cs index 27538a3b..f9bf7831 100644 --- a/net/src/Sails.Remoting/Options/RemotingViaSubstrateClientOptions.cs +++ b/net/src/Sails.Remoting/Options/NodeClientOptions.cs @@ -2,7 +2,7 @@ namespace Sails.Remoting.Options; -public sealed record RemotingViaSubstrateClientOptions +public sealed record NodeClientOptions { public Uri? GearNodeUri { get; init; } } diff --git a/net/src/Sails.Remoting/RemotingViaSubstrateClient.cs b/net/src/Sails.Remoting/RemotingViaNodeClient.cs similarity index 77% rename from net/src/Sails.Remoting/RemotingViaSubstrateClient.cs rename to net/src/Sails.Remoting/RemotingViaNodeClient.cs index d2609b1a..cbdc9b71 100644 --- a/net/src/Sails.Remoting/RemotingViaSubstrateClient.cs +++ b/net/src/Sails.Remoting/RemotingViaNodeClient.cs @@ -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; @@ -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; @@ -30,24 +28,22 @@ namespace Sails.Remoting; -internal sealed class RemotingViaSubstrateClient : IDisposable, IRemoting +internal sealed class RemotingViaNodeClient : IRemoting { /// /// Creates an instance implementing the interface via /// with initial account for signing transactions. /// - /// + /// /// - 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; } @@ -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); - } - - /// - public void SetSigningAccount(Account signingAccount) - { - EnsureArg.IsNotNull(signingAccount, nameof(signingAccount)); - - this.signingAccount = signingAccount; - } + private readonly INodeClientProvider nodeClientProvider; + private readonly Account signingAccount; /// public async Task> ActivateAsync( @@ -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(), @@ -106,7 +87,7 @@ 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, @@ -114,7 +95,7 @@ public void SetSigningAccount(Account signingAccount) .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); @@ -159,7 +140,7 @@ public async Task> 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(), @@ -177,7 +158,7 @@ public async Task> 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, @@ -204,7 +185,7 @@ public async Task 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; @@ -221,14 +202,4 @@ public async Task QueryAsync( return replyInfo.EncodedPayload; } - - private async Task GetConnectedNodeClientAsync(CancellationToken cancellationToken) - { - if (!this.isNodeClientConnected) - { - await this.nodeClient.ConnectAsync(cancellationToken).ConfigureAwait(false); - this.isNodeClientConnected = true; - } - return this.nodeClient; - } }