Skip to content

Commit 043e35a

Browse files
authored
Add Peer collection dropping after corruption (#67)
1 parent c2a0ee7 commit 043e35a

File tree

5 files changed

+54
-10
lines changed

5 files changed

+54
-10
lines changed

Beacon.Sdk/BeaconClients/Abstract/IWalletBeaconClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Beacon.Sdk.BeaconClients.Abstract
77

88
public interface IWalletBeaconClient : IBaseBeaconClient
99
{
10-
Task AddPeerAsync(P2PPairingRequest pairingRequest, bool sendPairingResponse = true);
10+
Task<bool> AddPeerAsync(P2PPairingRequest pairingRequest, bool sendPairingResponse = true);
1111
Task<PermissionInfo?> TryReadPermissionInfo(string sourceAddress, string senderId, Network network);
1212
P2PPairingRequest GetPairingRequest(string pairingData);
1313
}

Beacon.Sdk/BeaconClients/WalletBeaconClient.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ public WalletBeaconClient(
5353
_peerFactory = peerFactory;
5454
}
5555

56-
public async Task AddPeerAsync(P2PPairingRequest pairingRequest, bool sendPairingResponse = true)
56+
public async Task<bool> AddPeerAsync(P2PPairingRequest pairingRequest, bool sendPairingResponse = true)
5757
{
5858
if (!HexString.TryParse(pairingRequest.PublicKey, out var peerHexPublicKey))
5959
{
6060
_logger.LogError("Can not parse receiver public key");
61-
return;
61+
return false;
6262
}
6363

6464
var peer = _peerFactory.Create(
@@ -68,11 +68,28 @@ public async Task AddPeerAsync(P2PPairingRequest pairingRequest, bool sendPairin
6868
pairingRequest.RelayServer
6969
);
7070

71-
peer = PeerRepository.CreateAsync(peer).Result;
71+
var createdPeer = await PeerRepository.CreateAsync(peer);
72+
73+
if (createdPeer == null)
74+
{
75+
_logger.LogWarning("Peers collection corrupted. Trying drop and create again");
76+
77+
await PeerRepository.DropAsync();
78+
79+
createdPeer = await PeerRepository.CreateAsync(peer);
80+
81+
if (createdPeer == null)
82+
{
83+
_logger?.LogError("Can't create peer");
84+
return false;
85+
}
86+
}
7287

7388
if (sendPairingResponse)
7489
await P2PCommunicationService
75-
.SendChannelOpeningMessageAsync(peer, pairingRequest.Id, AppName, AppUrl, IconUrl);
90+
.SendChannelOpeningMessageAsync(createdPeer, pairingRequest.Id, AppName, AppUrl, IconUrl);
91+
92+
return true;
7693
}
7794

7895
protected override async Task OnP2PMessagesReceived(object? sender, P2PMessageEventArgs e)

Beacon.Sdk/Core/Domain/Interfaces/Data/IPeerRepository.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ public interface IPeerRepository
1313
Task<List<Peer>> GetAll();
1414
Task Delete(Peer peer);
1515
Task MarkAllInactive();
16+
Task DropAsync();
1617
}
1718
}

Beacon.Sdk/Core/Infrastructure/Repositories/BaseLiteDbRepository.cs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ namespace Beacon.Sdk.Core.Infrastructure.Repositories
33
using System;
44
using System.Collections.Generic;
55
using System.Threading.Tasks;
6+
67
using LiteDB;
78
using Microsoft.Extensions.Logging;
89

@@ -55,9 +56,29 @@ protected Task<T> InConnection(string collectionName, Func<LiteCollection<T>, Ta
5556
_logger.LogError(e, "error in repository");
5657
}
5758

58-
return new Task<T>(() => default!);
59+
return Task.FromResult<T>(default!);
60+
}
61+
62+
protected Task InConnection(string collectionName, Action<LiteDatabase, LiteCollection<T>> func)
63+
{
64+
try
65+
{
66+
lock (_syncRoot)
67+
{
68+
using var db = new LiteDatabase(_connectionString);
69+
LiteCollection<T> col = db.GetCollection<T>(collectionName);
70+
71+
func(db, col);
72+
}
73+
}
74+
catch (Exception e)
75+
{
76+
_logger.LogError(e, "error in repository");
77+
}
78+
79+
return Task.CompletedTask;
5980
}
60-
81+
6182
protected Task<T?> InConnectionNullable(string collectionName, Func<LiteCollection<T>, Task<T?>> func)
6283
{
6384
try
@@ -75,7 +96,7 @@ protected Task<T> InConnection(string collectionName, Func<LiteCollection<T>, Ta
7596
_logger.LogError(e, "error in repository");
7697
}
7798

78-
return new Task<T?>(() => default);
99+
return Task.FromResult<T?>(default);
79100
}
80101

81102
protected Task<T[]?> InConnectionNullable(string collectionName, Func<LiteCollection<T>, Task<T[]?>> func)
@@ -95,7 +116,7 @@ protected Task<T> InConnection(string collectionName, Func<LiteCollection<T>, Ta
95116
_logger.LogError(e, "error in repository");
96117
}
97118

98-
return new Task<T[]?>(() => default);
119+
return Task.FromResult<T[]?>(default);
99120
}
100121

101122
protected Task<List<T>> InConnection(string collectionName, Func<LiteCollection<T>, Task<List<T>>> func)
@@ -115,7 +136,7 @@ protected Task<List<T>> InConnection(string collectionName, Func<LiteCollection<
115136
_logger.LogError(e, "error in repository");
116137
}
117138

118-
return new Task<List<T>>(() => default);
139+
return Task.FromResult<List<T>>(default!);
119140
}
120141
}
121142
}

Beacon.Sdk/Core/Infrastructure/Repositories/LiteDbPeerRepository.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,10 @@ public Task MarkAllInactive() => InConnectionAction(CollectionName, col =>
6060
col.Update(peer);
6161
}
6262
});
63+
64+
public Task DropAsync() => InConnection(CollectionName, (db, col) =>
65+
{
66+
db.DropCollection(col.Name);
67+
});
6368
}
6469
}

0 commit comments

Comments
 (0)