Skip to content

Commit ddc5f53

Browse files
committed
add info logs
1 parent 3164f21 commit ddc5f53

File tree

4 files changed

+38
-10
lines changed

4 files changed

+38
-10
lines changed

src/DCLPulse/Messaging/HandshakeHandler.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ public class HandshakeHandler(MessagePipe messagePipe,
1212
PeerStateFactory peerStateFactory,
1313
SnapshotBoard snapshotBoard,
1414
IdentityBoard identityBoard,
15-
ITransport transport) : IMessageHandler
15+
ITransport transport,
16+
ILogger<HandshakeHandler> logger) : IMessageHandler
1617
{
1718
public void Handle(Dictionary<PeerIndex, PeerState> peers, PeerIndex from, ClientMessage message)
1819
{
@@ -31,6 +32,8 @@ public void Handle(Dictionary<PeerIndex, PeerState> peers, PeerIndex from, Clien
3132
},
3233
}, ITransport.PacketMode.RELIABLE));
3334

35+
logger.LogInformation("Handshake validation failed: cannot parse auth-chain");
36+
3437
return;
3538
}
3639

@@ -60,8 +63,13 @@ public void Handle(Dictionary<PeerIndex, PeerState> peers, PeerIndex from, Clien
6063
peers[from] = peer;
6164

6265
if (identityBoard.TryGetPeerIndexByWallet(peer.WalletId, out PeerIndex duplicatedPeer))
66+
{
6367
if (duplicatedPeer != from)
68+
{
6469
transport.Disconnect(duplicatedPeer, ITransport.DisconnectReason.DuplicateSession);
70+
logger.LogInformation("Duplicated peer found {Wallet}, disconnecting peer {Peer}", result.UserAddress, duplicatedPeer);
71+
}
72+
}
6573

6674
identityBoard.Set(from, result.UserAddress);
6775
snapshotBoard.SetActive(from);
@@ -73,6 +81,8 @@ public void Handle(Dictionary<PeerIndex, PeerState> peers, PeerIndex from, Clien
7381
Success = true,
7482
},
7583
}, ITransport.PacketMode.RELIABLE));
84+
85+
logger.LogInformation("Peer handshake accepted with wallet {Wallet} - peerId {Peer}", result.UserAddress, from);
7686
}
7787
catch (Exception e)
7888
{
@@ -84,6 +94,8 @@ public void Handle(Dictionary<PeerIndex, PeerState> peers, PeerIndex from, Clien
8494
Error = e.Message,
8595
},
8696
}, ITransport.PacketMode.RELIABLE));
97+
98+
logger.LogInformation("Handshake validation failed: {Error}", e.Message);
8799
}
88100
}
89101
}

src/DCLPulse/Peers/PeerIndex.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,9 @@ namespace Pulse.Peers;
55
/// It's up to the Transport Service implementation to ensure Indexing (instead of non-sequential IDs).
66
/// Always in the range [0, maxPeers). Safe to use as a direct array index.
77
/// </summary>
8-
public readonly struct PeerIndex : IEquatable<PeerIndex>
8+
public readonly struct PeerIndex(uint value) : IEquatable<PeerIndex>
99
{
10-
public readonly uint Value;
11-
12-
public PeerIndex(uint value)
13-
{
14-
Value = value;
15-
}
10+
public readonly uint Value = value;
1611

1712
public static implicit operator uint(PeerIndex id) =>
1813
id.Value;
@@ -25,4 +20,13 @@ public override bool Equals(object? obj) =>
2520

2621
public override int GetHashCode() =>
2722
(int)Value;
23+
24+
public override string ToString() =>
25+
Value.ToString();
26+
27+
public static bool operator ==(PeerIndex left, PeerIndex right) =>
28+
left.Equals(right);
29+
30+
public static bool operator !=(PeerIndex left, PeerIndex right) =>
31+
!(left == right);
2832
}

src/DCLPulse/Peers/PeersManager.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public sealed class PeersManager : BackgroundService
2626
{
2727
private readonly MessagePipe messagePipe;
2828
private readonly ILogger<PeersManager> logger;
29+
private readonly ILogger<PeerSimulation> peerSimulationLogger;
2930
private readonly ITimeProvider timeProvider;
3031
private readonly PeerStateFactory peerStateFactory;
3132
private readonly IAreaOfInterest areaOfInterest;
@@ -55,13 +56,15 @@ public PeersManager(
5556
IdentityBoard identityBoard,
5657
PeerOptions peerOptions,
5758
ILogger<PeersManager> logger,
59+
ILogger<PeerSimulation> peerSimulationLogger,
5860
ITimeProvider timeProvider,
5961
Dictionary<ClientMessage.MessageOneofCase, IMessageHandler> messageHandlers,
6062
ITransport transport,
6163
ProfileBoard profileBoard)
6264
{
6365
this.messagePipe = messagePipe;
6466
this.logger = logger;
67+
this.peerSimulationLogger = peerSimulationLogger;
6568
this.timeProvider = timeProvider;
6669
this.messageHandlers = messageHandlers;
6770
this.transport = transport;
@@ -100,7 +103,7 @@ protected override Task ExecuteAsync(CancellationToken stoppingToken)
100103
{
101104
var simulation = new PeerSimulation(
102105
areaOfInterest, snapshotBoard, spatialGrid, identityBoard,
103-
messagePipe, peerOptions.SimulationSteps, timeProvider, transport, profileBoard);
106+
messagePipe, peerOptions.SimulationSteps, timeProvider, transport, profileBoard, peerSimulationLogger);
104107

105108
tasks[i + 1] = WorkerAsync(i, messageChannels[i].Reader,
106109
peerLifeCycleChannels[i].Reader, simulation, stoppingToken);
@@ -197,6 +200,8 @@ internal void DrainPeerLifeCycleEvents(ChannelReader<PeerLifeCycleEvent> reader,
197200
peerState.ConnectionState = PeerConnectionState.PENDING_AUTH;
198201
peerState.TransportState = peerState.TransportState with { ConnectionTime = timeProvider.MonotonicTime };
199202
peers[from] = peerState;
203+
204+
logger.LogInformation("Peer connected {Peer}", from);
200205
}
201206
else if (evt.Type == PeerEventType.Disconnected)
202207
{
@@ -211,6 +216,8 @@ internal void DrainPeerLifeCycleEvents(ChannelReader<PeerLifeCycleEvent> reader,
211216
};
212217

213218
peers[from] = peerState;
219+
220+
logger.LogInformation("Peer disconnected {Peer}", from);
214221
}
215222
}
216223
catch (Exception ex)

src/DCLPulse/Peers/Simulation/PeerSimulation.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public sealed class PeerSimulation : IPeerSimulation
3030
private readonly ITimeProvider timeProvider;
3131
private readonly ITransport transport;
3232
private readonly ProfileBoard profileBoard;
33+
private readonly ILogger<PeerSimulation> logger;
3334

3435
/// <summary>
3536
/// Per-observer views: observer PeerIndex → (subject PeerIndex → view).
@@ -66,7 +67,8 @@ public PeerSimulation(
6667
uint[] simulationSteps,
6768
ITimeProvider timeProvider,
6869
ITransport transport,
69-
ProfileBoard profileBoard)
70+
ProfileBoard profileBoard,
71+
ILogger<PeerSimulation> logger)
7072
{
7173
this.areaOfInterest = areaOfInterest;
7274
this.snapshotBoard = snapshotBoard;
@@ -77,6 +79,7 @@ public PeerSimulation(
7779
this.timeProvider = timeProvider;
7880
this.transport = transport;
7981
this.profileBoard = profileBoard;
82+
this.logger = logger;
8083

8184
BaseTickMs = simulationSteps[0];
8285
tierDivisors = new uint[simulationSteps.Length];
@@ -98,6 +101,7 @@ public void SimulateTick(Dictionary<PeerIndex, PeerState> peers, uint tickCounte
98101
{
99102
// Trigger disconnection flow which will mark the peer as DISCONNECTING and eventually removed
100103
transport.Disconnect(observerId, ITransport.DisconnectReason.AuthTimeout);
104+
logger.LogInformation("Peer {Peer} disconnected due to authentication timed out", observerId);
101105
continue;
102106
}
103107
}
@@ -112,6 +116,7 @@ public void SimulateTick(Dictionary<PeerIndex, PeerState> peers, uint tickCounte
112116
identityBoard.Clear(observerId);
113117
observerViews.Remove(observerId);
114118
peersToBeRemoved.Add(observerId);
119+
logger.LogInformation("Peer {Peer} removed after disconnected", observerId);
115120
continue;
116121
}
117122
}

0 commit comments

Comments
 (0)