< 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: 38_17925369700
Line coverage
0%
Covered lines: 0
Uncovered lines: 52
Coverable lines: 52
Total lines: 127
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            transportService.Dispose();
 063            throw new ConnectionException($"Error connecting to peer {peerPubKey}", ex);
 64        }
 65
 66        // Create the message service
 067        var messageService = _messageServiceFactory.CreateMessageService(transportService);
 68
 69        // Create the ping pong service
 070        var pingPongService = _serviceProvider.GetRequiredService<IPingPongService>();
 71
 72        // Create the communication service
 073        var communicationService =
 074            new PeerCommunicationService(commLogger, messageService, _messageFactory, peerPubKey, pingPongService,
 075                                         _serviceProvider);
 76
 77        // Create the service
 078        return new PeerService(communicationService, _nodeOptions.Features, appLogger, _nodeOptions.NetworkTimeout);
 079    }
 80
 81    /// <inheritdoc />
 82    /// <exception cref="ConnectionException">Thrown when the connection to the peer fails.</exception>
 83    public async Task<IPeerService> CreateConnectingPeerAsync(TcpClient tcpClient)
 84    {
 85        // Create loggers
 086        var commLogger = _loggerFactory.CreateLogger<PeerCommunicationService>();
 087        var appLogger = _loggerFactory.CreateLogger<PeerService>();
 88
 089        var remoteEndPoint =
 090            (IPEndPoint)(tcpClient.Client.RemoteEndPoint ?? throw new Exception("Failed to get remote endpoint"));
 091        var ipAddress = remoteEndPoint.Address.ToString();
 092        var port = remoteEndPoint.Port;
 93
 94        // Create and Initialize the transport service
 095        var key = _secureKeyManager.GetNodeKeyPair();
 096        var transportService =
 097            _transportServiceFactory.CreateTransportService(false, key.PrivKey, key.CompactPubKey, tcpClient);
 98        try
 99        {
 0100            await transportService.InitializeAsync();
 0101        }
 0102        catch (Exception ex)
 103        {
 0104            transportService.Dispose();
 0105            throw new ConnectionException($"Error establishing connection to peer {ipAddress}:{port}", ex);
 106        }
 107
 0108        if (transportService.RemoteStaticPublicKey is null)
 109        {
 0110            throw new ErrorException("Failed to get remote static public key");
 111        }
 112
 113        // Create the message service
 0114        var messageService = _messageServiceFactory.CreateMessageService(transportService);
 115
 116        // Create the ping pong service
 0117        var pingPongService = _serviceProvider.GetRequiredService<IPingPongService>();
 118
 119        // Create the communication service (infrastructure layer)
 0120        var communicationService = new PeerCommunicationService(commLogger, messageService, _messageFactory,
 0121                                                                transportService.RemoteStaticPublicKey.Value,
 0122                                                                pingPongService, _serviceProvider);
 123
 124        // Create the application service (application layer)
 0125        return new PeerService(communicationService, _nodeOptions.Features, appLogger, _nodeOptions.NetworkTimeout);
 0126    }
 127}