From eec0e5d8b20e6c387ff48a9b64a2f7be1f6ef92b Mon Sep 17 00:00:00 2001 From: skibitsky Date: Mon, 19 Aug 2024 13:07:43 +0300 Subject: [PATCH 1/3] Update dependencies --- Directory.Packages.props | 15 ++++++++------- .../WalletConnectSharp.Auth.Tests.csproj | 4 ---- .../WalletConnectSharp.Crypto.Tests.csproj | 4 ---- .../WalletConnectSharp.Network.Tests.csproj | 4 ---- .../WalletConnectSharp.Sign.Test.csproj | 4 ---- .../WalletConnectSharp.Storage.Test.csproj | 1 - .../WalletConnectSharp.Web3Wallet.Tests.csproj | 4 ---- 7 files changed, 8 insertions(+), 28 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index f8f3d8e..fc3e048 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -3,16 +3,17 @@ true - + - - - - - + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + - \ No newline at end of file diff --git a/Tests/WalletConnectSharp.Auth.Tests/WalletConnectSharp.Auth.Tests.csproj b/Tests/WalletConnectSharp.Auth.Tests/WalletConnectSharp.Auth.Tests.csproj index 9f3749c..d612457 100644 --- a/Tests/WalletConnectSharp.Auth.Tests/WalletConnectSharp.Auth.Tests.csproj +++ b/Tests/WalletConnectSharp.Auth.Tests/WalletConnectSharp.Auth.Tests.csproj @@ -14,10 +14,6 @@ runtime; build; native; contentfiles; analyzers; buildtransitive all - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - diff --git a/Tests/WalletConnectSharp.Crypto.Tests/WalletConnectSharp.Crypto.Tests.csproj b/Tests/WalletConnectSharp.Crypto.Tests/WalletConnectSharp.Crypto.Tests.csproj index ae51b03..4183b5f 100644 --- a/Tests/WalletConnectSharp.Crypto.Tests/WalletConnectSharp.Crypto.Tests.csproj +++ b/Tests/WalletConnectSharp.Crypto.Tests/WalletConnectSharp.Crypto.Tests.csproj @@ -14,10 +14,6 @@ runtime; build; native; contentfiles; analyzers; buildtransitive all - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - diff --git a/Tests/WalletConnectSharp.Network.Tests/WalletConnectSharp.Network.Tests.csproj b/Tests/WalletConnectSharp.Network.Tests/WalletConnectSharp.Network.Tests.csproj index 8c72a4b..4fb375d 100644 --- a/Tests/WalletConnectSharp.Network.Tests/WalletConnectSharp.Network.Tests.csproj +++ b/Tests/WalletConnectSharp.Network.Tests/WalletConnectSharp.Network.Tests.csproj @@ -14,10 +14,6 @@ runtime; build; native; contentfiles; analyzers; buildtransitive all - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - diff --git a/Tests/WalletConnectSharp.Sign.Test/WalletConnectSharp.Sign.Test.csproj b/Tests/WalletConnectSharp.Sign.Test/WalletConnectSharp.Sign.Test.csproj index eefa15c..7cbc5f4 100644 --- a/Tests/WalletConnectSharp.Sign.Test/WalletConnectSharp.Sign.Test.csproj +++ b/Tests/WalletConnectSharp.Sign.Test/WalletConnectSharp.Sign.Test.csproj @@ -14,10 +14,6 @@ runtime; build; native; contentfiles; analyzers; buildtransitive all - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - diff --git a/Tests/WalletConnectSharp.Storage.Test/WalletConnectSharp.Storage.Test.csproj b/Tests/WalletConnectSharp.Storage.Test/WalletConnectSharp.Storage.Test.csproj index be8fbda..2aaba23 100644 --- a/Tests/WalletConnectSharp.Storage.Test/WalletConnectSharp.Storage.Test.csproj +++ b/Tests/WalletConnectSharp.Storage.Test/WalletConnectSharp.Storage.Test.csproj @@ -17,7 +17,6 @@ - diff --git a/Tests/WalletConnectSharp.Web3Wallet.Tests/WalletConnectSharp.Web3Wallet.Tests.csproj b/Tests/WalletConnectSharp.Web3Wallet.Tests/WalletConnectSharp.Web3Wallet.Tests.csproj index ed02976..f07c3ac 100644 --- a/Tests/WalletConnectSharp.Web3Wallet.Tests/WalletConnectSharp.Web3Wallet.Tests.csproj +++ b/Tests/WalletConnectSharp.Web3Wallet.Tests/WalletConnectSharp.Web3Wallet.Tests.csproj @@ -15,10 +15,6 @@ runtime; build; native; contentfiles; analyzers; buildtransitive all - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - From 27f1fa6dc852042e2178926526ae6f9bac4be71c Mon Sep 17 00:00:00 2001 From: skibitsky Date: Mon, 19 Aug 2024 13:08:47 +0300 Subject: [PATCH 2/3] Improve exception catching in the background tasks --- .../Controllers/HeartBeat.cs | 10 +++++- .../Controllers/Relayer.cs | 6 +++- .../Controllers/Subscriber.cs | 32 +++++++++++++------ .../Interfaces/ISubscriber.cs | 3 +- 4 files changed, 38 insertions(+), 13 deletions(-) diff --git a/WalletConnectSharp.Core/Controllers/HeartBeat.cs b/WalletConnectSharp.Core/Controllers/HeartBeat.cs index e327945..e55fc1e 100644 --- a/WalletConnectSharp.Core/Controllers/HeartBeat.cs +++ b/WalletConnectSharp.Core/Controllers/HeartBeat.cs @@ -1,3 +1,4 @@ +using WalletConnectSharp.Common.Logging; using WalletConnectSharp.Core.Interfaces; namespace WalletConnectSharp.Core.Controllers @@ -76,7 +77,14 @@ public Task InitAsync(CancellationToken cancellationToken = default) { while (!token.IsCancellationRequested) { - Pulse(); + try + { + Pulse(); + } + catch (Exception ex) + { + WCLogger.LogError(ex); + } await Task.Delay(Interval, token); } diff --git a/WalletConnectSharp.Core/Controllers/Relayer.cs b/WalletConnectSharp.Core/Controllers/Relayer.cs index 696cf34..f82b6b2 100644 --- a/WalletConnectSharp.Core/Controllers/Relayer.cs +++ b/WalletConnectSharp.Core/Controllers/Relayer.cs @@ -277,7 +277,11 @@ protected virtual async void OnProviderPayload(string payloadJson) protected virtual async Task ShouldIgnoreMessageEvent(MessageEvent messageEvent) { - if (!(await Subscriber.IsSubscribed(messageEvent.Topic))) return true; + var isSubscribed = await Subscriber.IsSubscribed(messageEvent.Topic); + if (!isSubscribed) + { + return true; + } var exists = Messages.Has(messageEvent.Topic, messageEvent.Message); return exists; diff --git a/WalletConnectSharp.Core/Controllers/Subscriber.cs b/WalletConnectSharp.Core/Controllers/Subscriber.cs index 0ee973e..82cb81a 100644 --- a/WalletConnectSharp.Core/Controllers/Subscriber.cs +++ b/WalletConnectSharp.Core/Controllers/Subscriber.cs @@ -522,31 +522,43 @@ public async Task Unsubscribe(string topic, UnsubscribeOptions opts = null) /// Determines whether the given topic is subscribed or not /// /// The topic to check + /// A token to cancel the operation. /// Return true if the topic is subscribed, false otherwise - public Task IsSubscribed(string topic) + public async Task IsSubscribed(string topic, CancellationToken cancellationToken = default) { - if (Topics.Contains(topic)) return Task.FromResult(true); - - return Task.Run(async delegate() + if (Topics.Contains(topic)) { - var startTime = DateTimeOffset.Now.ToUnixTimeSeconds(); + return true; + } + + var startTime = DateTimeOffset.Now.ToUnixTimeMilliseconds(); + const int timeoutMilliseconds = 5_000; + const int delayMilliseconds = 20; - while (true) + try + { + while (!cancellationToken.IsCancellationRequested) { if (!pending.ContainsKey(topic) && Topics.Contains(topic)) { return true; } - var currentTime = DateTimeOffset.Now.ToUnixTimeSeconds(); - if (currentTime - startTime >= 5) + var elapsedMilliseconds = DateTimeOffset.Now.ToUnixTimeMilliseconds() - startTime; + if (elapsedMilliseconds >= timeoutMilliseconds) { return false; } - await Task.Delay(20); + await Task.Delay(delayMilliseconds, cancellationToken); } - }); + } + catch (OperationCanceledException) + { + return false; + } + + return false; } protected virtual async Task RpcBatchSubscribe(string[] topics, ProtocolOptions relay) diff --git a/WalletConnectSharp.Core/Interfaces/ISubscriber.cs b/WalletConnectSharp.Core/Interfaces/ISubscriber.cs index b2837c4..6062ebc 100644 --- a/WalletConnectSharp.Core/Interfaces/ISubscriber.cs +++ b/WalletConnectSharp.Core/Interfaces/ISubscriber.cs @@ -71,7 +71,8 @@ public interface ISubscriber : IModule /// Determines whether the given topic is subscribed or not /// /// The topic to check + /// /// A token to cancel the operation. /// Return true if the topic is subscribed, false otherwise - public Task IsSubscribed(string topic); + public Task IsSubscribed(string topic, CancellationToken cancellationToken = default); } } From dd412833c5e7b441fdad25e4a24d030ddef8d256 Mon Sep 17 00:00:00 2001 From: skibitsky Date: Mon, 19 Aug 2024 13:25:29 +0300 Subject: [PATCH 3/3] Remove unused Subscriber.Resubscribe method --- .../Controllers/Subscriber.cs | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/WalletConnectSharp.Core/Controllers/Subscriber.cs b/WalletConnectSharp.Core/Controllers/Subscriber.cs index 82cb81a..9c1c38c 100644 --- a/WalletConnectSharp.Core/Controllers/Subscriber.cs +++ b/WalletConnectSharp.Core/Controllers/Subscriber.cs @@ -276,22 +276,6 @@ protected virtual async Task Reset() this.Resubscribed?.Invoke(this, EventArgs.Empty); } - protected virtual async Task Resubscribe(ActiveSubscription subscription) - { - if (!Ids.Contains(subscription.Id)) - { - var @params = new PendingSubscription() { Relay = subscription.Relay, Topic = subscription.Topic }; - - if (pending.ContainsKey(@params.Topic)) - pending.Remove(@params.Topic); - - pending.Add(@params.Topic, @params); - - var id = await RpcSubscribe(@params.Topic, @params.Relay); - OnResubscribe(id, @params); - } - } - protected virtual async Task RpcSubscribe(string topic, ProtocolOptions relay) { var api = RelayProtocols.GetRelayProtocol(relay.Protocol);