Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improve errors #202

Merged
merged 3 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Microsoft.IdentityModel.Tokens" Version="6.32.3" />
<PackageVersion Include="Microsoft.IdentityModel.Tokens" Version="8.0.1" />
<PackageVersion Include="Nethereum.HdWallet" Version="4.14.0" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="Portable.BouncyCastle" Version="1.9.0" />
<PackageVersion Include="Websocket.Client" Version="4.6.1" />
<PackageVersion Include="xunit" Version="2.4.2" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.6.3" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.0" />
<PackageVersion Include="coverlet.collector" Version="3.1.0" />
<PackageVersion Include="Websocket.Client" Version="4.7.0" />
<PackageVersion Include="xunit" Version="2.9.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageVersion>
<PackageVersion Include="Nethereum.Signer" Version="4.14.0" />
<PackageVersion Include="coverlet.collector" Version="6.0.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
<PackageReference Include="Microsoft.NET.Test.Sdk"/>
<PackageReference Include="xunit"/>
<PackageReference Include="xunit.runner.visualstudio"/>
<PackageReference Include="coverlet.collector"/>
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
10 changes: 9 additions & 1 deletion WalletConnectSharp.Core/Controllers/HeartBeat.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using WalletConnectSharp.Common.Logging;
using WalletConnectSharp.Core.Interfaces;

namespace WalletConnectSharp.Core.Controllers
Expand Down Expand Up @@ -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);
}
Expand Down
6 changes: 5 additions & 1 deletion WalletConnectSharp.Core/Controllers/Relayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,11 @@ protected virtual async void OnProviderPayload(string payloadJson)

protected virtual async Task<bool> 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;
Expand Down
48 changes: 22 additions & 26 deletions WalletConnectSharp.Core/Controllers/Subscriber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> RpcSubscribe(string topic, ProtocolOptions relay)
{
var api = RelayProtocols.GetRelayProtocol(relay.Protocol);
Expand Down Expand Up @@ -522,31 +506,43 @@ public async Task Unsubscribe(string topic, UnsubscribeOptions opts = null)
/// Determines whether the given topic is subscribed or not
/// </summary>
/// <param name="topic">The topic to check</param>
/// <param name="cancellationToken">A token to cancel the operation.</param>
/// <returns>Return true if the topic is subscribed, false otherwise</returns>
public Task<bool> IsSubscribed(string topic)
public async Task<bool> 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<string[]> RpcBatchSubscribe(string[] topics, ProtocolOptions relay)
Expand Down
3 changes: 2 additions & 1 deletion WalletConnectSharp.Core/Interfaces/ISubscriber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public interface ISubscriber : IModule
/// Determines whether the given topic is subscribed or not
/// </summary>
/// <param name="topic">The topic to check</param>
/// /// <param name="cancellationToken">A token to cancel the operation.</param>
/// <returns>Return true if the topic is subscribed, false otherwise</returns>
public Task<bool> IsSubscribed(string topic);
public Task<bool> IsSubscribed(string topic, CancellationToken cancellationToken = default);
}
}
Loading