< Summary - Combined Code Coverage

Information
Class: NLightning.Infrastructure.Transport.Services.HandshakeService
Assembly: NLightning.Infrastructure
File(s): /home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure/Transport/Services/HandshakeService.cs
Tag: 30_15166811759
Line coverage
82%
Covered lines: 28
Uncovered lines: 6
Coverable lines: 34
Total lines: 131
Line coverage: 82.3%
Branch coverage
75%
Covered branches: 9
Total branches: 12
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)50%22100%
get_IsInitiator()100%11100%
get_RemoteStaticPublicKey()100%210%
PerformStep(...)100%88100%
InitiatorWriteActOne(...)100%11100%
InitiatorReadActTwoAndWriteActThree(...)100%11100%
ResponderReadActOneAndWriteActTwo(...)100%11100%
ResponderReadActThree(...)100%11100%
Dispose()0%620%

File(s)

/home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure/Transport/Services/HandshakeService.cs

#LineLine coverage
 1namespace NLightning.Infrastructure.Transport.Services;
 2
 3using Common.Utils;
 4using Crypto.Functions;
 5using Domain.Transport;
 6using Handshake.States;
 7using Interfaces;
 8using Protocol.Constants;
 9
 10/// <summary>
 11/// Initializes a new instance of the <see cref="HandshakeService"/> class.
 12/// </summary>
 13/// <param name="isInitiator">If we are initiating the connection</param>
 14/// <param name="localStaticPrivateKey">Our local Private Key</param>
 15/// <param name="staticPublicKey">If we are initiating, the remote Public Key, else our local Public Key</param>
 2416internal sealed class HandshakeService(bool isInitiator, ReadOnlySpan<byte> localStaticPrivateKey,
 2417                                       ReadOnlySpan<byte> staticPublicKey, IHandshakeState? handshakeState = null)
 18    : IHandshakeService
 19{
 2420    private readonly IHandshakeState _handshakeState = handshakeState
 2421                                                       ?? new HandshakeState(isInitiator, localStaticPrivateKey,
 2422                                                                             staticPublicKey, new Ecdh());
 23
 2424    private byte _steps = 2;
 25    private bool _disposed;
 26
 27    /// <inheritdoc/>
 4028    public bool IsInitiator => isInitiator;
 29
 030    public NBitcoin.PubKey? RemoteStaticPublicKey => _handshakeState.RemoteStaticPublicKey;
 31
 32    /// <inheritdoc/>
 33    /// <exception cref="InvalidOperationException">Thrown when there's no more steps to complete</exception>
 34    public int PerformStep(ReadOnlySpan<byte> inMessage, Span<byte> outMessage, out ITransport? transport)
 35    {
 4836        ExceptionUtils.ThrowIfDisposed(_disposed, nameof(HandshakeService));
 37
 4838        switch (_steps)
 39        {
 40            case 2:
 2441                _steps--;
 2442                transport = null;
 2443                return IsInitiator
 2444                    ? InitiatorWriteActOne(outMessage)
 2445                    : ResponderReadActOneAndWriteActTwo(inMessage, outMessage);
 46            case 1:
 1647                _steps--;
 1648                return IsInitiator
 1649                    ? InitiatorReadActTwoAndWriteActThree(inMessage, outMessage, out transport)
 1650                    : ResponderReadActThree(inMessage, out transport);
 51            default:
 852                throw new InvalidOperationException("There's no more steps to complete");
 53        }
 54    }
 55
 56    #region Initiator Methods
 57    /// <summary>
 58    /// Initiator writes act one
 59    /// </summary>
 60    /// <param name="outMessage">The buffer to write the message to</param>
 61    /// <returns>Number of bytes written to outMessage</returns>
 62    private int InitiatorWriteActOne(Span<byte> outMessage)
 63    {
 64        // Write act one
 1265        return _handshakeState.WriteMessage(ProtocolConstants.EMPTY_MESSAGE, outMessage).Item1;
 66    }
 67
 68    /// <summary>
 69    /// Initiator reads act two and writes act three
 70    /// </summary>
 71    /// <param name="actTwoMessage">Byte[] representation of Act Two Message</param>
 72    /// <param name="outMessage">The buffer to write the message to</param>
 73    /// <param name="transport"> The Transport that is going to be returned by the handshake after all steps has been co
 74    /// <returns>Number of bytes written to outMessage</returns>
 75    private int InitiatorReadActTwoAndWriteActThree(ReadOnlySpan<byte> actTwoMessage, Span<byte> outMessage,
 76                                                    out ITransport? transport)
 77    {
 78        // Read act two
 879        _ = _handshakeState.ReadMessage(actTwoMessage, outMessage);
 80
 81        // Write act three
 882        (var messageSize, _, transport) = _handshakeState.WriteMessage(ProtocolConstants.EMPTY_MESSAGE, outMessage);
 83
 884        return messageSize;
 85    }
 86    #endregion
 87
 88    #region Responder Methods
 89    /// <summary>
 90    /// Responder reads act one and writes act two
 91    /// </summary>
 92    /// <param name="actOneMessage">Byte[] representation of Act One Message</param>
 93    /// <param name="outMessage">The buffer to write the message to</param>
 94    /// <returns>Number of bytes written to outMessage</returns>
 95    private int ResponderReadActOneAndWriteActTwo(ReadOnlySpan<byte> actOneMessage, Span<byte> outMessage)
 96    {
 97        // Read act one
 1298        var (responderMessageSize, _, _) = _handshakeState.ReadMessage(actOneMessage, outMessage);
 99
 100        // Write act two
 12101        return _handshakeState.WriteMessage(outMessage[..responderMessageSize], outMessage).Item1;
 102    }
 103
 104    /// <summary>
 105    /// Responder reads act three
 106    /// </summary>
 107    /// <param name="actThreeMessage">Byte[] representation of Act Three Message</param>
 108    /// <param name="transport"> The Transport that is going to be returned by the handshake after all steps has been co
 109    /// <returns>Number of bytes read from actThreeMessage</returns>
 110    private int ResponderReadActThree(ReadOnlySpan<byte> actThreeMessage, out ITransport? transport)
 111    {
 112        // Read act three
 8113        var messageBuffer = new byte[ProtocolConstants.MAX_MESSAGE_LENGTH];
 8114        (var messageSize, _, transport) = _handshakeState.ReadMessage(actThreeMessage, messageBuffer);
 115
 8116        return messageSize;
 117    }
 118    #endregion
 119
 120    public void Dispose()
 121    {
 0122        if (_disposed)
 123        {
 0124            return;
 125        }
 126
 0127        _handshakeState.Dispose();
 128
 0129        _disposed = true;
 0130    }
 131}