< Summary - Combined Code Coverage

Information
Class: NLightning.Application.Channels.Handlers.OpenChannel1MessageHandler
Assembly: NLightning.Application
File(s): /home/runner/work/NLightning/NLightning/src/NLightning.Application/Channels/Handlers/OpenChannel1MessageHandler.cs
Tag: 57_24045730253
Line coverage
0%
Covered lines: 0
Uncovered lines: 49
Coverable lines: 49
Total lines: 101
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 16
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%
HandleAsync()0%272160%

File(s)

/home/runner/work/NLightning/NLightning/src/NLightning.Application/Channels/Handlers/OpenChannel1MessageHandler.cs

#LineLine coverage
 1using Microsoft.Extensions.Logging;
 2
 3namespace NLightning.Application.Channels.Handlers;
 4
 5using Domain.Channels.Enums;
 6using Domain.Channels.Interfaces;
 7using Domain.Crypto.ValueObjects;
 8using Domain.Enums;
 9using Domain.Exceptions;
 10using Domain.Node;
 11using Domain.Node.Options;
 12using Domain.Protocol.Interfaces;
 13using Domain.Protocol.Messages;
 14using Domain.Protocol.Tlv;
 15using Interfaces;
 16
 17public class OpenChannel1MessageHandler : IChannelMessageHandler<OpenChannel1Message>
 18{
 19    private readonly IChannelFactory _channelFactory;
 20    private readonly IChannelMemoryRepository _channelMemoryRepository;
 21    private readonly ILogger<OpenChannel1MessageHandler> _logger;
 22    private readonly IMessageFactory _messageFactory;
 23
 024    public OpenChannel1MessageHandler(IChannelFactory channelFactory, IChannelMemoryRepository channelMemoryRepository,
 025                                      ILogger<OpenChannel1MessageHandler> logger, IMessageFactory messageFactory)
 26    {
 027        _channelFactory = channelFactory;
 028        _channelMemoryRepository = channelMemoryRepository;
 029        _logger = logger;
 030        _messageFactory = messageFactory;
 031    }
 32
 33    public async Task<IChannelMessage?> HandleAsync(OpenChannel1Message message, ChannelState currentState,
 34                                                    FeatureOptions negotiatedFeatures, CompactPubKey peerPubKey)
 35    {
 036        _logger.LogTrace("Processing OpenChannel1Message with ChannelId: {ChannelId} from Peer: {PeerPubKey}",
 037                         message.Payload.ChannelId, peerPubKey);
 38
 039        var payload = message.Payload;
 40
 041        if (currentState != ChannelState.None)
 042            throw new ChannelErrorException("A channel with this id already exists", payload.ChannelId);
 43
 44        // Check if there's a temporary channel for this peer
 045        if (_channelMemoryRepository.TryGetTemporaryChannelState(peerPubKey, payload.ChannelId, out currentState))
 46        {
 047            if (currentState != ChannelState.V1Opening)
 48            {
 049                throw new ChannelErrorException("Channel had the wrong state", payload.ChannelId,
 050                                                "This channel is already being negotiated with peer");
 51            }
 52        }
 53
 54        // Create the channel
 055        var channel = await _channelFactory.CreateChannelV1AsNonInitiatorAsync(message, negotiatedFeatures, peerPubKey);
 56
 057        _logger.LogTrace("Created Channel with fundingPubKey: {fundingPubKey}",
 058                         channel.LocalKeySet.FundingCompactPubKey);
 59
 60        // Add the channel to dictionaries
 061        _channelMemoryRepository.AddTemporaryChannel(peerPubKey, channel);
 62
 63        // Create UpfrontShutdownScriptTlv if needed
 064        UpfrontShutdownScriptTlv? upfrontShutdownScriptTlv = null;
 065        if (channel.LocalUpfrontShutdownScript is not null)
 066            upfrontShutdownScriptTlv = new UpfrontShutdownScriptTlv(channel.LocalUpfrontShutdownScript.Value);
 67        else
 068            upfrontShutdownScriptTlv = new UpfrontShutdownScriptTlv(Array.Empty<byte>());
 69
 070        var channelTypeFeatureSet = FeatureSet.NewBasicChannelType();
 071        if (negotiatedFeatures.OptionAnchors >= FeatureSupport.Optional)
 072            channelTypeFeatureSet.SetFeature(Feature.OptionAnchors,
 073                                             negotiatedFeatures.OptionAnchors == FeatureSupport.Compulsory);
 74
 075        if (channel.ChannelConfig.UseScidAlias >= FeatureSupport.Optional)
 076            channelTypeFeatureSet.SetFeature(Feature.OptionScidAlias,
 077                                             channel.ChannelConfig.UseScidAlias == FeatureSupport.Compulsory);
 78
 079        if (channel.ChannelConfig.MinimumDepth == 0)
 080            channelTypeFeatureSet.SetFeature(Feature.OptionZeroconf, true);
 81
 082        var featureSetBytes = channelTypeFeatureSet.GetBytes() ?? throw new ChannelErrorException("The channel type is n
 083                                            "Sorry, we had an internal error");
 084        var channelTypeTlv = new ChannelTypeTlv(featureSetBytes);
 85
 86        // Create the reply message
 087        var acceptChannel1ReplyMessage = _messageFactory
 088           .CreateAcceptChannel1Message(channel.ChannelConfig.ChannelReserveAmount, channelTypeTlv,
 089                                        channel.LocalKeySet.DelayedPaymentCompactBasepoint,
 090                                        channel.LocalKeySet.CurrentPerCommitmentCompactPoint,
 091                                        channel.LocalKeySet.FundingCompactPubKey,
 092                                        channel.LocalKeySet.HtlcCompactBasepoint,
 093                                        channel.ChannelConfig.MaxAcceptedHtlcs,
 094                                        channel.ChannelConfig.MaxHtlcAmountInFlight, channel.ChannelConfig.MinimumDepth,
 095                                        channel.LocalKeySet.PaymentCompactBasepoint,
 096                                        channel.LocalKeySet.RevocationCompactBasepoint, channel.ChannelId,
 097                                        channel.ChannelConfig.ToSelfDelay, upfrontShutdownScriptTlv);
 98
 099        return acceptChannel1ReplyMessage;
 0100    }
 101}