< Summary - Combined Code Coverage

Information
Class: NLightning.Infrastructure.Node.Managers.PeerManager
Assembly: NLightning.Infrastructure
File(s): /home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure/Node/Managers/PeerManager.cs
Tag: 30_15166811759
Line coverage
71%
Covered lines: 25
Uncovered lines: 10
Coverable lines: 35
Total lines: 94
Line coverage: 71.4%
Branch coverage
0%
Covered branches: 0
Total branches: 2
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
ConnectToPeerAsync()100%1.02175%
AcceptPeerAsync()100%1.02175%
DisconnectPeer(...)0%620%

File(s)

/home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure/Node/Managers/PeerManager.cs

#LineLine coverage
 1using System.Net.Sockets;
 2using Microsoft.Extensions.Logging;
 3using Microsoft.Extensions.Options;
 4using NBitcoin;
 5
 6namespace NLightning.Infrastructure.Node.Managers;
 7
 8using Domain.Exceptions;
 9using Domain.Node.Options;
 10using Domain.ValueObjects;
 11using Infrastructure.Protocol.Models;
 12using Interfaces;
 13using Models;
 14
 15/// <summary>
 16/// Service for managing peers.
 17/// </summary>
 18/// <remarks>
 19/// This class is used to manage peers in the network.
 20/// </remarks>
 21/// <seealso cref="IPeerManager" />
 22public sealed class PeerManager : IPeerManager
 23{
 1224    private readonly Dictionary<ChannelId, PubKey> _channels = [];
 25    private readonly ILogger<PeerManager> _logger;
 26    private readonly IOptions<NodeOptions> _nodeOptions;
 27    private readonly IPeerFactory _peerFactory;
 1228    private readonly Dictionary<PubKey, Peer> _peers = [];
 29
 1230    public PeerManager(ILogger<PeerManager> logger, IOptions<NodeOptions> nodeOptions, IPeerFactory peerFactory)
 31    {
 1232        _logger = logger;
 1233        _nodeOptions = nodeOptions;
 1234        _peerFactory = peerFactory;
 1235    }
 36
 37    /// <inheritdoc />
 38    /// <exception cref="ConnectionException">Thrown when the connection to the peer fails.</exception>
 39    public async Task ConnectToPeerAsync(PeerAddress peerAddress)
 40    {
 41        // Connect to the peer
 842        var tcpClient = new TcpClient();
 43        try
 44        {
 845            await tcpClient.ConnectAsync(peerAddress.Host, peerAddress.Port,
 846                                         new CancellationTokenSource(_nodeOptions.Value.NetworkTimeout).Token);
 447        }
 048        catch (OperationCanceledException)
 49        {
 050            throw new ConnectionException($"Timeout connecting to peer {peerAddress.Host}:{peerAddress.Port}");
 51        }
 452        catch (Exception e)
 53        {
 454            throw new ConnectionException($"Failed to connect to peer {peerAddress.Host}:{peerAddress.Port}", e);
 55        }
 56
 457        var peer = await _peerFactory.CreateConnectedPeerAsync(peerAddress, tcpClient);
 458        peer.DisconnectEvent += (_, _) =>
 459        {
 060            _peers.Remove(peerAddress.PubKey);
 061            _logger.LogInformation("Peer {Peer} disconnected", peerAddress.PubKey);
 462        };
 63
 464        _peers.Add(peerAddress.PubKey, peer);
 465    }
 66
 67    /// <inheritdoc />
 68    /// <exception cref="ErrorException">Thrown when the connection to the peer fails.</exception>
 69    public async Task AcceptPeerAsync(TcpClient tcpClient)
 70    {
 71        // Create the peer
 472        var peer = await _peerFactory.CreateConnectingPeerAsync(tcpClient);
 473        peer.DisconnectEvent += (_, _) =>
 474        {
 075            _peers.Remove(peer.PeerAddress.PubKey);
 076            _logger.LogError("{Peer} disconnected", peer.PeerAddress.PubKey);
 477        };
 78
 479        _peers.Add(peer.PeerAddress.PubKey, peer);
 480    }
 81
 82    /// <inheritdoc />
 83    public void DisconnectPeer(PubKey pubKey)
 84    {
 085        if (_peers.TryGetValue(pubKey, out var peer))
 86        {
 087            peer.Disconnect();
 88        }
 89        else
 90        {
 091            _logger.LogWarning("Peer {Peer} not found", pubKey);
 92        }
 093    }
 94}