< Summary - Combined Code Coverage

Information
Class: NLightning.Infrastructure.Node.Factories.PeerFactory
Assembly: NLightning.Infrastructure
File(s): /home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure/Node/Factories/PeerFactory.cs
Tag: 30_15166811759
Line coverage
0%
Covered lines: 0
Uncovered lines: 45
Coverable lines: 45
Total lines: 114
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 4
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%210%
CreateConnectedPeerAsync()100%210%
CreateConnectingPeerAsync()0%2040%

File(s)

/home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure/Node/Factories/PeerFactory.cs

#LineLine coverage
 1using System.Net;
 2using System.Net.Sockets;
 3using Microsoft.Extensions.Logging;
 4using Microsoft.Extensions.Options;
 5
 6namespace NLightning.Infrastructure.Node.Factories;
 7
 8using Domain.Exceptions;
 9using Domain.Factories;
 10using Domain.Node.Options;
 11using Domain.Protocol.Factories;
 12using Domain.Protocol.Managers;
 13using Interfaces;
 14using Models;
 15
 16public class PeerFactory : IPeerFactory
 17{
 18    private readonly ILoggerFactory _loggerFactory;
 19    private readonly IMessageFactory _messageFactory;
 20    private readonly IMessageServiceFactory _messageServiceFactory;
 21    private readonly IPingPongServiceFactory _pingPongServiceFactory;
 22    private readonly ISecureKeyManager _secureKeyManager;
 23    private readonly ITransportServiceFactory _transportServiceFactory;
 24    private readonly NodeOptions _nodeOptions;
 25
 026    public PeerFactory(ILoggerFactory loggerFactory, IMessageFactory messageFactory,
 027                       IMessageServiceFactory messageServiceFactory, IPingPongServiceFactory pingPongServiceFactory,
 028                       ISecureKeyManager secureKeyManager, ITransportServiceFactory transportServiceFactory,
 029                       IOptions<NodeOptions> nodeOptions)
 30    {
 031        _loggerFactory = loggerFactory;
 032        _messageFactory = messageFactory;
 033        _messageServiceFactory = messageServiceFactory;
 034        _pingPongServiceFactory = pingPongServiceFactory;
 035        _secureKeyManager = secureKeyManager;
 036        _transportServiceFactory = transportServiceFactory;
 037        _nodeOptions = nodeOptions.Value;
 038    }
 39
 40    /// <summary>
 41    /// Creates a peer that we're connecting to.
 42    /// </summary>
 43    /// <param name="peerAddress">Peer address</param>
 44    /// <param name="tcpClient">TCP client</param>
 45    /// <returns>A task that represents the asynchronous operation. The task result contains the created peer.</returns>
 46    /// <exception cref="ConnectionException">Thrown when the connection to the peer fails.</exception>
 47    public async Task<Peer> CreateConnectedPeerAsync(Protocol.Models.PeerAddress peerAddress, TcpClient tcpClient)
 48    {
 49        // Create a specific logger for the Peer class
 050        var logger = _loggerFactory.CreateLogger<Peer>();
 51
 52        // Create and Initialize the transport service
 053        var keyBytes = _secureKeyManager.GetNodeKey().ToBytes();
 054        var transportService = _transportServiceFactory
 055            .CreateTransportService(true, keyBytes, peerAddress.PubKey.ToBytes(), tcpClient);
 56        try
 57        {
 058            await transportService.InitializeAsync();
 059        }
 060        catch (Exception ex)
 61        {
 062            throw new ConnectionException($"Error connecting to peer {peerAddress.Host}:{peerAddress.Port}", ex);
 63        }
 64
 65        // Create the message service
 066        var messageService = _messageServiceFactory.CreateMessageService(transportService);
 67
 68        // Create the ping pong service
 069        var pingPongService = _pingPongServiceFactory.CreatePingPongService();
 70
 071        return new Peer(_nodeOptions.Features, logger, _messageFactory, messageService, _nodeOptions.NetworkTimeout,
 072                        peerAddress, pingPongService);
 073    }
 74
 75    public async Task<Peer> CreateConnectingPeerAsync(TcpClient tcpClient)
 76    {
 77        // Create a specific logger for the Peer class
 078        var logger = _loggerFactory.CreateLogger<Peer>();
 79
 080        var remoteEndPoint = (IPEndPoint)(tcpClient.Client.RemoteEndPoint
 081                                          ?? throw new Exception("Failed to get remote endpoint"));
 082        var ipAddress = remoteEndPoint.Address.ToString();
 083        var port = remoteEndPoint.Port;
 84
 85        // Create and Initialize the transport service
 086        var key = _secureKeyManager.GetNodeKey();
 087        var transportService = _transportServiceFactory
 088            .CreateTransportService(false, key.ToBytes(), key.PubKey.ToBytes(), tcpClient);
 89        try
 90        {
 091            await transportService.InitializeAsync();
 092        }
 093        catch (Exception ex)
 94        {
 095            throw new ConnectionException($"Error establishing connection to peer {ipAddress}:{port}", ex);
 96        }
 97
 098        if (transportService.RemoteStaticPublicKey is null)
 99        {
 0100            throw new ErrorException("Failed to get remote static public key");
 101        }
 102
 103        // Create the message service
 0104        var messageService = _messageServiceFactory.CreateMessageService(transportService);
 105
 106        // Create the ping pong service
 0107        var pingPongService = _pingPongServiceFactory.CreatePingPongService();
 108
 0109        var peerAddress = new Protocol.Models.PeerAddress(transportService.RemoteStaticPublicKey, ipAddress, port);
 110
 0111        return new Peer(_nodeOptions.Features, logger, _messageFactory, messageService, _nodeOptions.NetworkTimeout,
 0112                        peerAddress, pingPongService);
 0113    }
 114}