< Summary - Combined Code Coverage

Information
Class: NLightning.Infrastructure.Node.Factories.PeerServiceFactory
Assembly: NLightning.Infrastructure
File(s): /home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure/Node/Factories/PeerServiceFactory.cs
Tag: 36_15743069263
Line coverage
0%
Covered lines: 0
Uncovered lines: 50
Coverable lines: 50
Total lines: 125
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/PeerServiceFactory.cs

#LineLine coverage
 1using System.Net;
 2using System.Net.Sockets;
 3using Microsoft.Extensions.DependencyInjection;
 4using Microsoft.Extensions.Logging;
 5using Microsoft.Extensions.Options;
 6
 7namespace NLightning.Infrastructure.Node.Factories;
 8
 9using Domain.Crypto.ValueObjects;
 10using Domain.Exceptions;
 11using Domain.Node.Interfaces;
 12using Domain.Node.Options;
 13using Domain.Protocol.Interfaces;
 14using Services;
 15
 16/// <summary>
 17/// Factory for creating peer services.
 18/// </summary>
 19public class PeerServiceFactory : IPeerServiceFactory
 20{
 21    private readonly ILoggerFactory _loggerFactory;
 22    private readonly IMessageFactory _messageFactory;
 23    private readonly IMessageServiceFactory _messageServiceFactory;
 24    private readonly ISecureKeyManager _secureKeyManager;
 25    private readonly ITransportServiceFactory _transportServiceFactory;
 26    private readonly IServiceProvider _serviceProvider;
 27    private readonly NodeOptions _nodeOptions;
 28
 029    public PeerServiceFactory(ILoggerFactory loggerFactory, IMessageFactory messageFactory,
 030                              IMessageServiceFactory messageServiceFactory, ISecureKeyManager secureKeyManager,
 031                              ITransportServiceFactory transportServiceFactory, IOptions<NodeOptions> nodeOptions,
 032                              IServiceProvider serviceProvider)
 33    {
 034        _loggerFactory = loggerFactory;
 035        _messageFactory = messageFactory;
 036        _messageServiceFactory = messageServiceFactory;
 037        _secureKeyManager = secureKeyManager;
 038        _transportServiceFactory = transportServiceFactory;
 039        _serviceProvider = serviceProvider;
 040        _nodeOptions = nodeOptions.Value;
 041    }
 42
 43    /// <inheritdoc />
 44    /// <exception cref="ConnectionException">Thrown when the connection to the peer fails.</exception>
 45    public async Task<IPeerService> CreateConnectedPeerAsync(CompactPubKey peerPubKey, TcpClient tcpClient)
 46    {
 47        // Create a specific logger for the communication service
 048        var commLogger = _loggerFactory.CreateLogger<PeerCommunicationService>();
 049        var appLogger = _loggerFactory.CreateLogger<PeerService>();
 50
 51        // Create and Initialize the transport service
 052        var key = _secureKeyManager.GetNodeKeyPair();
 053        var transportService =
 054            _transportServiceFactory.CreateTransportService(true, key.PrivKey, peerPubKey, tcpClient);
 55
 56        try
 57        {
 058            await transportService.InitializeAsync();
 059        }
 060        catch (Exception ex)
 61        {
 062            throw new ConnectionException($"Error connecting to peer {peerPubKey}", ex);
 63        }
 64
 65        // Create the message service
 066        var messageService = _messageServiceFactory.CreateMessageService(transportService);
 67
 68        // Create the ping pong service
 069        var pingPongService = _serviceProvider.GetRequiredService<IPingPongService>();
 70
 71        // Create the communication service
 072        var communicationService =
 073            new PeerCommunicationService(commLogger, messageService, _messageFactory, peerPubKey, pingPongService,
 074                                         _serviceProvider);
 75
 76        // Create the service
 077        return new PeerService(communicationService, _nodeOptions.Features, appLogger, _nodeOptions.NetworkTimeout);
 078    }
 79
 80    /// <inheritdoc />
 81    /// <exception cref="ConnectionException">Thrown when the connection to the peer fails.</exception>
 82    public async Task<IPeerService> CreateConnectingPeerAsync(TcpClient tcpClient)
 83    {
 84        // Create loggers
 085        var commLogger = _loggerFactory.CreateLogger<PeerCommunicationService>();
 086        var appLogger = _loggerFactory.CreateLogger<PeerService>();
 87
 088        var remoteEndPoint =
 089            (IPEndPoint)(tcpClient.Client.RemoteEndPoint ?? throw new Exception("Failed to get remote endpoint"));
 090        var ipAddress = remoteEndPoint.Address.ToString();
 091        var port = remoteEndPoint.Port;
 92
 93        // Create and Initialize the transport service
 094        var key = _secureKeyManager.GetNodeKeyPair();
 095        var transportService =
 096            _transportServiceFactory.CreateTransportService(false, key.PrivKey, key.CompactPubKey, tcpClient);
 97        try
 98        {
 099            await transportService.InitializeAsync();
 0100        }
 0101        catch (Exception ex)
 102        {
 0103            throw new ConnectionException($"Error establishing connection to peer {ipAddress}:{port}", ex);
 104        }
 105
 0106        if (transportService.RemoteStaticPublicKey is null)
 107        {
 0108            throw new ErrorException("Failed to get remote static public key");
 109        }
 110
 111        // Create the message service
 0112        var messageService = _messageServiceFactory.CreateMessageService(transportService);
 113
 114        // Create the ping pong service
 0115        var pingPongService = _serviceProvider.GetRequiredService<IPingPongService>();
 116
 117        // Create the communication service (infrastructure layer)
 0118        var communicationService = new PeerCommunicationService(commLogger, messageService, _messageFactory,
 0119                                                                transportService.RemoteStaticPublicKey.Value,
 0120                                                                pingPongService, _serviceProvider);
 121
 122        // Create the application service (application layer)
 0123        return new PeerService(communicationService, _nodeOptions.Features, appLogger, _nodeOptions.NetworkTimeout);
 0124    }
 125}