From 148ec4df4e4923ec17d1a1f6933b1999b9def1f1 Mon Sep 17 00:00:00 2001 From: uink45 <79078981+uink45@users.noreply.github.com> Date: Thu, 3 Oct 2024 14:50:02 +1000 Subject: [PATCH] Update logs. --- src/Lantern.Beacon.Console/Program.cs | 1 - src/Lantern.Beacon/BeaconClientManager.cs | 12 ++++---- .../Discovery/CustomDiscoveryProtocol.cs | 28 ++----------------- .../Networking/Gossip/GossipSubManager.cs | 4 +-- .../CustomPubsub/CustomPubsubProtocol.cs | 14 ++++------ .../CustomPubsub/CustomPubsubRouter.cs | 4 +-- .../Libp2pProtocols/Mplex/MplexProtocol.cs | 2 +- .../ReqRespProtocols/StatusProtocol.cs | 2 +- .../BeaconClientManagerTests.cs | 8 ------ 9 files changed, 19 insertions(+), 56 deletions(-) diff --git a/src/Lantern.Beacon.Console/Program.cs b/src/Lantern.Beacon.Console/Program.cs index 6014095..4b6737c 100644 --- a/src/Lantern.Beacon.Console/Program.cs +++ b/src/Lantern.Beacon.Console/Program.cs @@ -40,7 +40,6 @@ public static async Task Main() .WithEntry(EnrEntryKey.Id, new EntryId("v4")) .WithEntry(EnrEntryKey.Secp256K1, new EntrySecp256K1(sessionOptions.Signer.PublicKey)); var discv5LoggerFactory = LoggerFactory.Create(builder => builder.SetMinimumLevel(LogLevel.None)); - // Beacon client options var syncProtocolOptions = new SyncProtocolOptions { diff --git a/src/Lantern.Beacon/BeaconClientManager.cs b/src/Lantern.Beacon/BeaconClientManager.cs index 8672f2f..c561a7d 100644 --- a/src/Lantern.Beacon/BeaconClientManager.cs +++ b/src/Lantern.Beacon/BeaconClientManager.cs @@ -87,7 +87,7 @@ public async Task InitAsync() customDiscoveryProtocol.OnAddPeer = HandleDiscoveredPeer; - _logger.LogInformation("Beacon client manager started with address {Address}", LocalPeer.Address); + _logger.LogInformation("Client started with address {Address}", LocalPeer.Address); } catch (Exception e) { @@ -165,7 +165,7 @@ private async Task DisplaySyncStatus(CancellationToken token) private async Task ProcessPeerDiscoveryAsync(CancellationToken token) { - _logger.LogInformation("Running peer discovery..."); + _logger.LogDebug("Running peer discovery..."); var semaphore = new SemaphoreSlim(clientOptions.MaxParallelDials); while (!token.IsCancellationRequested) @@ -310,7 +310,7 @@ private async Task DialPeer(Multiaddress peer, CancellationToken token = default if (completedTask != timeoutTask) { - _logger.LogInformation("Successfully dialed peer /ip4/{Ip4}/tcp/{TcpPort}/p2p/{PeerId}", ip4, tcpPort, + _logger.LogDebug("Successfully dialed peer /ip4/{Ip4}/tcp/{TcpPort}/p2p/{PeerId}", ip4, tcpPort, peerIdString); var remotePeerId = dialTask.Result.Address.GetPeerId(); @@ -318,7 +318,7 @@ private async Task DialPeer(Multiaddress peer, CancellationToken token = default if (!result) { - _logger.LogInformation("No protocols found for peer /ip4/{Ip4}/tcp/{TcpPort}/p2p/{PeerId}", ip4, + _logger.LogDebug("No protocols found for peer /ip4/{Ip4}/tcp/{TcpPort}/p2p/{PeerId}", ip4, tcpPort, peerIdString); await dialTask.Result.DisconnectAsync(); return; @@ -330,7 +330,7 @@ private async Task DialPeer(Multiaddress peer, CancellationToken token = default if (supportsLightClientProtocols) { _logger.LogInformation( - "Peer /ip4/{Ip4}/tcp/{TcpPort}/p2p/{PeerId} supports all light client protocols", ip4, tcpPort, + "Found peer /ip4/{Ip4}/tcp/{TcpPort}/p2p/{PeerId} that supports all light client protocols", ip4, tcpPort, peerIdString); discoveryProtocol.OnAddPeer?.Invoke([peer]); @@ -352,7 +352,7 @@ private async Task DialPeer(Multiaddress peer, CancellationToken token = default } else { - _logger.LogInformation( + _logger.LogDebug( "Peer /ip4/{Ip4}/tcp/{TcpPort}/p2p/{PeerId} does not support all light client protocols. Disconnecting...", ip4, tcpPort, peerIdString); await dialTask.Result.DisconnectAsync(); diff --git a/src/Lantern.Beacon/Networking/Discovery/CustomDiscoveryProtocol.cs b/src/Lantern.Beacon/Networking/Discovery/CustomDiscoveryProtocol.cs index e168484..ff452e1 100644 --- a/src/Lantern.Beacon/Networking/Discovery/CustomDiscoveryProtocol.cs +++ b/src/Lantern.Beacon/Networking/Discovery/CustomDiscoveryProtocol.cs @@ -40,7 +40,7 @@ public async Task InitAsync() public async Task GetDiscoveredNodesAsync(Multiaddress localPeerAddr, CancellationToken token = default) { - _logger.LogInformation("Starting discovery with local peer address: {LocalPeerAddr}", localPeerAddr); + _logger.LogDebug("Starting discovery with local peer address: {LocalPeerAddr}", localPeerAddr); try { @@ -58,7 +58,7 @@ public async Task GetDiscoveredNodesAsync(Multiaddress localPeerAddr, Cancellati break; } - _logger.LogInformation("Found {Count} peers to dial so far...", multiaddresses.Count); + _logger.LogInformation("Discovered {Count} peers so far...", multiaddresses.Count); var batchNodes = nodes.Skip(i).Take(beaconOptions.MaxParallelDials).ToList(); var tasks = batchNodes.Select(node => discv5Protocol.SendFindNodeAsync(node, randomNodeId)).ToList(); @@ -107,33 +107,11 @@ public async Task GetDiscoveredNodesAsync(Multiaddress localPeerAddr, Cancellati discoveredNodes.Clear(); } } - - // var multiaddresses = new List(); - // var bootNodes = new[] - // { - // "/ip4/162.55.138.93/tcp/9000/p2p/16Uiu2HAmTUhE91q42JaTWBSHAu8zoPcFiZdnvLD9ujc6dPNBWcoy", - // "/ip4/198.244.165.204/tcp/9000/p2p/16Uiu2HAmEP83jeVdeSiwrgqSaUX8SQgEqUAhe4tTZKm1w7y7vUeL", - // "/ip4/102.218.213.140/tcp/32000/p2p/16Uiu2HAmDdPDsuxkewTeNNqq1rL9A5PdKFD486P6BvqyyVppY89w", - // "/ip4/185.177.124.120/tcp/9000/p2p/16Uiu2HAmF59QpFcwjTPeU7iXMpkBqwUALy5rNW5j8i6AQVi8nG7e", - // "/ip4/140.228.71.48/tcp/9000/p2p/16Uiu2HAmP4jwhWtYRgR7Lkp4H75S7vXDV8NBN9gdZH6k2di124PB", - // "/ip4/121.134.209.218/tcp/9000/p2p/16Uiu2HAmMM3xTjNbYipQLjWXC7zHB6ygtDVMZJpQ6EeSG6jDiYSR", - // }; - // - // foreach (var bootNode in bootNodes) - // { - // var multiaddress = Multiaddress.Decode(bootNode); - // if (multiaddress == null) - // { - // continue; - // } - // - // multiaddresses.Add(multiaddress); - // } if (multiaddresses.Count != 0) { OnAddPeer?.Invoke(multiaddresses.ToArray()); - _logger.LogInformation("Found {Count} peers to dial", multiaddresses.Count); + _logger.LogInformation("Discovery completed. Found {Count} peers for dialing", multiaddresses.Count); } } catch (Exception ex) diff --git a/src/Lantern.Beacon/Networking/Gossip/GossipSubManager.cs b/src/Lantern.Beacon/Networking/Gossip/GossipSubManager.cs index 90c0826..299486f 100644 --- a/src/Lantern.Beacon/Networking/Gossip/GossipSubManager.cs +++ b/src/Lantern.Beacon/Networking/Gossip/GossipSubManager.cs @@ -57,7 +57,7 @@ public void Start(CancellationToken token = default) }; _ = Task.Run(() => router.RunAsync(beaconClientManager.LocalPeer, discoveryProtocol, settings, token), token); - _logger.LogInformation("Running GossipSub protocol"); + _logger.LogDebug("Running GossipSub protocol"); } public async Task StopAsync() @@ -185,7 +185,7 @@ private void CheckIfPeerExistsInGossip(PeerId peerId) if (!foundLivePeer) { - _logger?.LogInformation("Adding peer {PeerId} to 'GossipPeers'", peerId); + _logger?.LogInformation("Adding peer {PeerId} to GossipPeers", peerId); peerState.GossipPeers.TryAdd(peerId, true); } } diff --git a/src/Lantern.Beacon/Networking/Libp2pProtocols/CustomPubsub/CustomPubsubProtocol.cs b/src/Lantern.Beacon/Networking/Libp2pProtocols/CustomPubsub/CustomPubsubProtocol.cs index 9e5e3d1..8d12aaa 100644 --- a/src/Lantern.Beacon/Networking/Libp2pProtocols/CustomPubsub/CustomPubsubProtocol.cs +++ b/src/Lantern.Beacon/Networking/Libp2pProtocols/CustomPubsub/CustomPubsubProtocol.cs @@ -17,15 +17,11 @@ public abstract class CustomPubsubProtocol : IProtocol { private readonly ILogger? _logger; private readonly CustomPubsubRouter router; - private readonly IPeerState _peerState; - private readonly SyncProtocolOptions _syncProtocolOptions; public string Id { get; } - public CustomPubsubProtocol(SyncProtocolOptions syncProtocolOptions, IPeerState peerState, string protocolId, CustomPubsubRouter router, ILoggerFactory? loggerFactory = null) + public CustomPubsubProtocol(string protocolId, CustomPubsubRouter router, ILoggerFactory? loggerFactory = null) { - _syncProtocolOptions = syncProtocolOptions; - _peerState = peerState; _logger = loggerFactory?.CreateLogger(GetType()); Id = protocolId; this.router = router; @@ -98,10 +94,10 @@ public override string ToString() } } -public class FloodsubProtocol(SyncProtocolOptions syncProtocolOptions, IPeerState peerState, CustomPubsubRouter router, ILoggerFactory? loggerFactory = null) : CustomPubsubProtocol(syncProtocolOptions, peerState, CustomPubsubRouter.FloodsubProtocolVersion, router, loggerFactory); +public class FloodsubProtocol(CustomPubsubRouter router, ILoggerFactory? loggerFactory = null) : CustomPubsubProtocol(CustomPubsubRouter.FloodsubProtocolVersion, router, loggerFactory); -public class GossipsubProtocol(SyncProtocolOptions syncProtocolOptions, IPeerState peerState, CustomPubsubRouter router, ILoggerFactory? loggerFactory = null) : CustomPubsubProtocol(syncProtocolOptions, peerState, CustomPubsubRouter.GossipsubProtocolVersionV10, router, loggerFactory); +public class GossipsubProtocol(CustomPubsubRouter router, ILoggerFactory? loggerFactory = null) : CustomPubsubProtocol(CustomPubsubRouter.GossipsubProtocolVersionV10, router, loggerFactory); -public class GossipsubProtocolV11(SyncProtocolOptions syncProtocolOptions, IPeerState peerState, CustomPubsubRouter router, ILoggerFactory? loggerFactory = null) : CustomPubsubProtocol(syncProtocolOptions, peerState, CustomPubsubRouter.GossipsubProtocolVersionV11, router, loggerFactory); +public class GossipsubProtocolV11(CustomPubsubRouter router, ILoggerFactory? loggerFactory = null) : CustomPubsubProtocol(CustomPubsubRouter.GossipsubProtocolVersionV11, router, loggerFactory); -public class GossipsubProtocolV12(SyncProtocolOptions syncProtocolOptions, IPeerState peerState, CustomPubsubRouter router, ILoggerFactory? loggerFactory = null) : CustomPubsubProtocol(syncProtocolOptions, peerState, CustomPubsubRouter.GossipsubProtocolVersionV12, router, loggerFactory); +public class GossipsubProtocolV12(CustomPubsubRouter router, ILoggerFactory? loggerFactory = null) : CustomPubsubProtocol(CustomPubsubRouter.GossipsubProtocolVersionV12, router, loggerFactory); diff --git a/src/Lantern.Beacon/Networking/Libp2pProtocols/CustomPubsub/CustomPubsubRouter.cs b/src/Lantern.Beacon/Networking/Libp2pProtocols/CustomPubsub/CustomPubsubRouter.cs index 42019d3..f80dd6c 100644 --- a/src/Lantern.Beacon/Networking/Libp2pProtocols/CustomPubsub/CustomPubsubRouter.cs +++ b/src/Lantern.Beacon/Networking/Libp2pProtocols/CustomPubsub/CustomPubsubRouter.cs @@ -177,9 +177,7 @@ public async Task RunAsync(ILocalPeer localPeer, IDiscoveryProtocol discoveryPro _ = localPeer.ListenAsync(localPeer.Address, token); _ = StartDiscoveryAsync(discoveryProtocol, token); - logger?.LogInformation("Started"); - - // reconnection if needed + _ = Task.Run(async () => { while (!token.IsCancellationRequested) diff --git a/src/Lantern.Beacon/Networking/Libp2pProtocols/Mplex/MplexProtocol.cs b/src/Lantern.Beacon/Networking/Libp2pProtocols/Mplex/MplexProtocol.cs index 2bfd01f..19ebe33 100644 --- a/src/Lantern.Beacon/Networking/Libp2pProtocols/Mplex/MplexProtocol.cs +++ b/src/Lantern.Beacon/Networking/Libp2pProtocols/Mplex/MplexProtocol.cs @@ -163,7 +163,7 @@ protected override async Task ConnectAsync(IChannel downChannel, IChannelFactory peerState.InitiatorChannels.Clear(); peerState.ReceiverChannels.Clear(); - _logger?.LogInformation("Connection with peer {PeerId} has been closed", context.RemotePeer.Address.GetPeerId()!.ToString()); + _logger?.LogDebug("Connection with peer {PeerId} has been closed", context.RemotePeer.Address.GetPeerId()!.ToString()); } private void HandleSubDialRequests(IPeerContext context, IChannelFactory channelFactory, bool isListener, IChannel downChannel, PeerConnectionState peerState) diff --git a/src/Lantern.Beacon/Networking/ReqRespProtocols/StatusProtocol.cs b/src/Lantern.Beacon/Networking/ReqRespProtocols/StatusProtocol.cs index c00ee1e..1702909 100644 --- a/src/Lantern.Beacon/Networking/ReqRespProtocols/StatusProtocol.cs +++ b/src/Lantern.Beacon/Networking/ReqRespProtocols/StatusProtocol.cs @@ -133,7 +133,7 @@ public async Task ListenAsync(IChannel downChannel, IChannelFactory? upChannelFa await downChannel.WriteAsync(rawData, cts.Token); await downChannel.CloseAsync(); - _logger?.LogInformation("Sent status response to {PeerId} with forkDigest={forkDigest}, finalizedRoot={finalizedRoot}, finalizedEpoch={finalizedEpoch}, headRoot={headRoot}, headSlot={headSlot}", + _logger?.LogDebug("Sent status response to {PeerId} with forkDigest={forkDigest}, finalizedRoot={finalizedRoot}, finalizedEpoch={finalizedEpoch}, headRoot={headRoot}, headSlot={headSlot}", context.RemotePeer.Address.Get(), Convert.ToHexString(forkDigest), Convert.ToHexString(finalisedRoot), diff --git a/test/Lantern.Beacon.Tests/BeaconClientManagerTests.cs b/test/Lantern.Beacon.Tests/BeaconClientManagerTests.cs index c80f210..906d5b4 100644 --- a/test/Lantern.Beacon.Tests/BeaconClientManagerTests.cs +++ b/test/Lantern.Beacon.Tests/BeaconClientManagerTests.cs @@ -74,14 +74,6 @@ public async Task InitAsync_ShouldInitializeCorrectly() _beaconClientManager = new BeaconClientManager(new BeaconClientOptions(), _mockManualDiscoveryProtocol.Object, _mockLiteDbService.Object, _mockCustomDiscoveryProtocol.Object, _mockPeerState.Object, _mockSyncProtocol.Object,_mockPeerFactory.Object, _mockIdentityManager.Object, _mockLoggerFactory.Object); await _beaconClientManager.InitAsync(); - - _mockLogger.Verify(log => log.Log( - LogLevel.Information, - It.IsAny(), - It.Is((v, t) => v.ToString().Contains("Beacon client manager started with address")), - null, - It.IsAny>()), - Times.Once); } [Test]