< Summary - Combined Code Coverage

Information
Class: NLightning.Infrastructure.Repositories.Memory.ChannelMemoryRepository
Assembly: NLightning.Infrastructure.Repositories
File(s): /home/runner/work/NLightning/NLightning/src/NLightning.Infrastructure.Repositories/Memory/ChannelMemoryRepository.cs
Tag: 57_24045730253
Line coverage
0%
Covered lines: 0
Uncovered lines: 69
Coverable lines: 69
Total lines: 139
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 18
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/NLightning/NLightning/src/NLightning.Infrastructure.Repositories/Memory/ChannelMemoryRepository.cs

#LineLine coverage
 1using System.Collections.Concurrent;
 2using System.Diagnostics.CodeAnalysis;
 3using Microsoft.Extensions.Logging;
 4
 5namespace NLightning.Infrastructure.Repositories.Memory;
 6
 7using Domain.Channels.Enums;
 8using Domain.Channels.Events;
 9using Domain.Channels.Interfaces;
 10using Domain.Channels.Models;
 11using Domain.Channels.ValueObjects;
 12using Domain.Crypto.ValueObjects;
 13
 14public class ChannelMemoryRepository : IChannelMemoryRepository
 15{
 16    private readonly ILogger<ChannelMemoryRepository> _logger;
 017    private readonly ConcurrentDictionary<ChannelId, ChannelModel> _channels = [];
 018    private readonly ConcurrentDictionary<ChannelId, ChannelState> _channelStates = [];
 019    private readonly ConcurrentDictionary<(CompactPubKey, ChannelId), ChannelModel> _temporaryChannels = [];
 020    private readonly ConcurrentDictionary<(CompactPubKey, ChannelId), ChannelState> _temporaryChannelStates = [];
 21
 22    /// <inheritdoc/>
 23    public event EventHandler<ChannelUpgradedEventArgs>? OnChannelUpgraded;
 24
 25    /// <inheritdoc/>
 26    public event EventHandler<ChannelUpdatedEventArgs>? OnChannelUpdated;
 27
 028    public ChannelMemoryRepository(ILogger<ChannelMemoryRepository> logger)
 029    {
 030        _logger = logger;
 031    }
 32
 33    /// <inheritdoc/>
 34    public bool TryGetChannel(ChannelId channelId, [MaybeNullWhen(false)] out ChannelModel channel)
 035    {
 036        return _channels.TryGetValue(channelId, out channel);
 037    }
 38
 39    /// <inheritdoc/>
 40    public List<ChannelModel> FindChannels(Func<ChannelModel, bool> predicate)
 041    {
 042        return _channels
 043              .Values
 044              .Where(predicate)
 045              .ToList();
 046    }
 47
 48    /// <inheritdoc/>
 49    public bool TryGetChannelState(ChannelId channelId, out ChannelState channelState)
 050    {
 051        return _channelStates.TryGetValue(channelId, out channelState);
 052    }
 53
 54    /// <inheritdoc/>
 55    public void AddChannel(ChannelModel channel)
 056    {
 057        ArgumentNullException.ThrowIfNull(channel);
 58
 059        if (!_channels.TryAdd(channel.ChannelId, channel))
 060            throw new InvalidOperationException($"Channel with Id {channel.ChannelId} already exists.");
 61
 062        _channelStates[channel.ChannelId] = channel.State;
 063    }
 64
 65    /// <inheritdoc/>
 66    public void UpdateChannel(ChannelModel channel)
 067    {
 068        ArgumentNullException.ThrowIfNull(channel);
 69
 070        if (!_channels.ContainsKey(channel.ChannelId))
 071            throw new KeyNotFoundException($"Channel with Id {channel.ChannelId} does not exist.");
 72
 073        _channels[channel.ChannelId] = channel;
 074        _channelStates[channel.ChannelId] = channel.State;
 75
 076        OnChannelUpdated?.Invoke(this, new ChannelUpdatedEventArgs(channel));
 077    }
 78
 79    /// <inheritdoc/>
 80    public bool TryRemoveChannel(ChannelId channelId)
 081    {
 082        var removed = _channels.TryRemove(channelId, out _);
 083        return removed && _channelStates.TryRemove(channelId, out _);
 084    }
 85
 86    /// <inheritdoc/>
 87    public bool TryGetTemporaryChannel(CompactPubKey compactPubKey, ChannelId channelId,
 88                                       [MaybeNullWhen(false)] out ChannelModel channel)
 089    {
 090        return _temporaryChannels.TryGetValue((compactPubKey, channelId), out channel);
 091    }
 92
 93    /// <inheritdoc/>
 94    public bool TryGetTemporaryChannelState(CompactPubKey compactPubKey, ChannelId channelId,
 95                                            out ChannelState channelState)
 096    {
 097        return _temporaryChannelStates.TryGetValue((compactPubKey, channelId), out channelState);
 098    }
 99
 100    /// <inheritdoc/>
 101    public void AddTemporaryChannel(CompactPubKey compactPubKey, ChannelModel channel)
 0102    {
 0103        if (!_temporaryChannels.TryAdd((compactPubKey, channel.ChannelId), channel))
 0104            throw new InvalidOperationException(
 0105                $"Temporary channel with Id {channel.ChannelId} for CompactPubKey {compactPubKey} already exists.");
 106
 0107        _temporaryChannelStates[(compactPubKey, channel.ChannelId)] = channel.State;
 0108    }
 109
 110    /// <inheritdoc/>
 111    public void UpdateTemporaryChannel(CompactPubKey compactPubKey, ChannelModel channel)
 0112    {
 0113        if (!_temporaryChannels.ContainsKey((compactPubKey, channel.ChannelId)))
 0114            throw new KeyNotFoundException(
 0115                $"Temporary channel with Id {channel.ChannelId} for CompactPubKey {compactPubKey} does not exist.");
 116
 0117        _temporaryChannels[(compactPubKey, channel.ChannelId)] = channel;
 0118        _temporaryChannelStates[(compactPubKey, channel.ChannelId)] = channel.State;
 0119    }
 120
 121    /// <inheritdoc/>
 122    public bool TryRemoveTemporaryChannel(CompactPubKey compactPubKey, ChannelId channelId)
 0123    {
 0124        var removed = _temporaryChannels.TryRemove((compactPubKey, channelId), out _);
 0125        return removed && _temporaryChannelStates.TryRemove((compactPubKey, channelId), out _);
 0126    }
 127
 128    /// <inheritdoc/>
 129    public void UpgradeChannel(ChannelId oldChannelId, ChannelModel tempChannel)
 0130    {
 0131        AddChannel(tempChannel);
 0132        if (!TryRemoveTemporaryChannel(tempChannel.RemoteNodeId, oldChannelId))
 0133            _logger.LogWarning(
 0134                "Unable to remove Temporary Channel with Id {oldChannelId} while upgrading Channel {channelId}.",
 0135                oldChannelId, tempChannel.ChannelId);
 136
 0137        OnChannelUpgraded?.Invoke(this, new ChannelUpgradedEventArgs(oldChannelId, tempChannel.ChannelId));
 0138    }
 139}

Methods/Properties

.ctor(Microsoft.Extensions.Logging.ILogger`1<NLightning.Infrastructure.Repositories.Memory.ChannelMemoryRepository>)
TryGetChannel(NLightning.Domain.Channels.ValueObjects.ChannelId,NLightning.Domain.Channels.Models.ChannelModel&)
TryGetChannel(NLightning.Domain.Channels.ValueObjects.ChannelId,NLightning.Domain.Channels.Models.ChannelModel&)
FindChannels(System.Func`2<NLightning.Domain.Channels.Models.ChannelModel,System.Boolean>)
FindChannels(System.Func`2<NLightning.Domain.Channels.Models.ChannelModel,System.Boolean>)
TryGetChannelState(NLightning.Domain.Channels.ValueObjects.ChannelId,NLightning.Domain.Channels.Enums.ChannelState&)
TryGetChannelState(NLightning.Domain.Channels.ValueObjects.ChannelId,NLightning.Domain.Channels.Enums.ChannelState&)
AddChannel(NLightning.Domain.Channels.Models.ChannelModel)
AddChannel(NLightning.Domain.Channels.Models.ChannelModel)
UpdateChannel(NLightning.Domain.Channels.Models.ChannelModel)
UpdateChannel(NLightning.Domain.Channels.Models.ChannelModel)
TryRemoveChannel(NLightning.Domain.Channels.ValueObjects.ChannelId)
TryRemoveChannel(NLightning.Domain.Channels.ValueObjects.ChannelId)
TryGetTemporaryChannel(NLightning.Domain.Crypto.ValueObjects.CompactPubKey,NLightning.Domain.Channels.ValueObjects.ChannelId,NLightning.Domain.Channels.Models.ChannelModel&)
TryGetTemporaryChannel(NLightning.Domain.Crypto.ValueObjects.CompactPubKey,NLightning.Domain.Channels.ValueObjects.ChannelId,NLightning.Domain.Channels.Models.ChannelModel&)
TryGetTemporaryChannelState(NLightning.Domain.Crypto.ValueObjects.CompactPubKey,NLightning.Domain.Channels.ValueObjects.ChannelId,NLightning.Domain.Channels.Enums.ChannelState&)
TryGetTemporaryChannelState(NLightning.Domain.Crypto.ValueObjects.CompactPubKey,NLightning.Domain.Channels.ValueObjects.ChannelId,NLightning.Domain.Channels.Enums.ChannelState&)
AddTemporaryChannel(NLightning.Domain.Crypto.ValueObjects.CompactPubKey,NLightning.Domain.Channels.Models.ChannelModel)
AddTemporaryChannel(NLightning.Domain.Crypto.ValueObjects.CompactPubKey,NLightning.Domain.Channels.Models.ChannelModel)
UpdateTemporaryChannel(NLightning.Domain.Crypto.ValueObjects.CompactPubKey,NLightning.Domain.Channels.Models.ChannelModel)
UpdateTemporaryChannel(NLightning.Domain.Crypto.ValueObjects.CompactPubKey,NLightning.Domain.Channels.Models.ChannelModel)
TryRemoveTemporaryChannel(NLightning.Domain.Crypto.ValueObjects.CompactPubKey,NLightning.Domain.Channels.ValueObjects.ChannelId)
TryRemoveTemporaryChannel(NLightning.Domain.Crypto.ValueObjects.CompactPubKey,NLightning.Domain.Channels.ValueObjects.ChannelId)
UpgradeChannel(NLightning.Domain.Channels.ValueObjects.ChannelId,NLightning.Domain.Channels.Models.ChannelModel)
UpgradeChannel(NLightning.Domain.Channels.ValueObjects.ChannelId,NLightning.Domain.Channels.Models.ChannelModel)