Skip to content

Commit 3164f21

Browse files
committed
fix duplicated sessions
1 parent 1f07ded commit 3164f21

File tree

4 files changed

+26
-23
lines changed

4 files changed

+26
-23
lines changed

src/DCLPulse/Messaging/HandshakeHandler.cs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ public void Handle(Dictionary<PeerIndex, PeerState> peers, PeerIndex from, Clien
5858
peer.ConnectionState = PeerConnectionState.AUTHENTICATED;
5959

6060
peers[from] = peer;
61+
62+
if (identityBoard.TryGetPeerIndexByWallet(peer.WalletId, out PeerIndex duplicatedPeer))
63+
if (duplicatedPeer != from)
64+
transport.Disconnect(duplicatedPeer, ITransport.DisconnectReason.DuplicateSession);
65+
6166
identityBoard.Set(from, result.UserAddress);
6267
snapshotBoard.SetActive(from);
6368

@@ -68,8 +73,6 @@ public void Handle(Dictionary<PeerIndex, PeerState> peers, PeerIndex from, Clien
6873
Success = true,
6974
},
7075
}, ITransport.PacketMode.RELIABLE));
71-
72-
DisconnectDuplicatedSessions(peers, from, result);
7376
}
7477
catch (Exception e)
7578
{
@@ -83,16 +86,4 @@ public void Handle(Dictionary<PeerIndex, PeerState> peers, PeerIndex from, Clien
8386
}, ITransport.PacketMode.RELIABLE));
8487
}
8588
}
86-
87-
private void DisconnectDuplicatedSessions(Dictionary<PeerIndex, PeerState> peers, PeerIndex from, AuthChainValidationResult result)
88-
{
89-
// TODO: could be improved if we index by walletId, although we would need to keep in sync both lists
90-
foreach ((PeerIndex pi, PeerState ps) in peers)
91-
{
92-
if (!string.Equals(ps.WalletId, result.UserAddress, StringComparison.OrdinalIgnoreCase)) continue;
93-
94-
if (pi != from)
95-
transport.Disconnect(pi, ITransport.DisconnectReason.DuplicateSession);
96-
}
97-
}
9889
}
Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Collections.Concurrent;
2+
13
namespace Pulse.Peers.Simulation;
24

35
/// <summary>
@@ -10,18 +12,28 @@ namespace Pulse.Peers.Simulation;
1012
/// </summary>
1113
public sealed class IdentityBoard(int maxPeers)
1214
{
13-
private readonly string?[] walletIds = new string?[maxPeers];
15+
private readonly string?[] walletsByPeerIds = new string?[maxPeers];
16+
private readonly ConcurrentDictionary<string, PeerIndex> peerIdsByWallets = new (StringComparer.OrdinalIgnoreCase);
1417

1518
public void Set(PeerIndex id, string walletId)
1619
{
17-
Volatile.Write(ref walletIds[(int)id.Value], walletId);
20+
Volatile.Write(ref walletsByPeerIds[(int)id.Value], walletId);
21+
peerIdsByWallets[walletId] = id;
1822
}
1923

20-
public string? Get(PeerIndex id) =>
21-
Volatile.Read(ref walletIds[(int)id.Value]);
24+
public string? GetWalletIdByPeerIndex(PeerIndex id) =>
25+
Volatile.Read(ref walletsByPeerIds[(int)id.Value]);
26+
27+
public bool TryGetPeerIndexByWallet(string walletId, out PeerIndex peerIndex) =>
28+
peerIdsByWallets.TryGetValue(walletId, out peerIndex);
2229

2330
public void Clear(PeerIndex id)
2431
{
25-
Volatile.Write(ref walletIds[(int)id.Value], null);
32+
string? walletId = GetWalletIdByPeerIndex(id);
33+
34+
if (walletId != null)
35+
peerIdsByWallets.TryRemove(walletId, out _);
36+
37+
Volatile.Write(ref walletsByPeerIds[(int)id.Value], null);
2638
}
2739
}

src/DCLPulse/Peers/Simulation/PeerSimulation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ private void ProcessVisibleSubjects(
203203
{
204204
PlayerJoined = new PlayerJoined
205205
{
206-
UserId = identityBoard.Get(entry.Subject),
206+
UserId = identityBoard.GetWalletIdByPeerIndex(entry.Subject),
207207
ProfileVersion = profileBoard.Get(entry.Subject),
208208
State = CreateFullState(entry.Subject, subjectSnapshot),
209209
},

src/DCLPulseTests/IdentityBoardTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void ConcurrentSetAndGet_ReadersNeverSeeTornValues()
4444
{
4545
for (var i = 0; i < MAX_PEERS; i++)
4646
{
47-
string? value = board.Get(new PeerIndex((uint)i));
47+
string? value = board.GetWalletIdByPeerIndex(new PeerIndex((uint)i));
4848

4949
// Must be either null (not yet written) or the exact expected value
5050
if (value != null && value != expectedWallets[i])
@@ -104,7 +104,7 @@ public void ConcurrentSetAndClear_ReadersNeverSeeTornValues()
104104
{
105105
for (var i = 0; i < MAX_PEERS; i++)
106106
{
107-
string? value = board.Get(new PeerIndex((uint)i));
107+
string? value = board.GetWalletIdByPeerIndex(new PeerIndex((uint)i));
108108

109109
if (value != null && value != expectedWallets[i])
110110
errors.Add($"Slot {i}: expected null or '{expectedWallets[i]}', got '{value}'");
@@ -141,7 +141,7 @@ public void ConcurrentReaders_AllSeeWrittenValueAfterWriterCompletes()
141141
{
142142
for (var i = 0; i < MAX_PEERS; i++)
143143
{
144-
string? value = board.Get(new PeerIndex((uint)i));
144+
string? value = board.GetWalletIdByPeerIndex(new PeerIndex((uint)i));
145145

146146
if (value != expectedWallets[i])
147147
errors.Add($"Slot {i}: expected '{expectedWallets[i]}', got '{value}'");

0 commit comments

Comments
 (0)