| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | using System.Diagnostics.CodeAnalysis; |
| | | 3 | | using Microsoft.Extensions.Logging; |
| | | 4 | | |
| | | 5 | | namespace NLightning.Infrastructure.Repositories.Memory; |
| | | 6 | | |
| | | 7 | | using Domain.Channels.Enums; |
| | | 8 | | using Domain.Channels.Events; |
| | | 9 | | using Domain.Channels.Interfaces; |
| | | 10 | | using Domain.Channels.Models; |
| | | 11 | | using Domain.Channels.ValueObjects; |
| | | 12 | | using Domain.Crypto.ValueObjects; |
| | | 13 | | |
| | | 14 | | public class ChannelMemoryRepository : IChannelMemoryRepository |
| | | 15 | | { |
| | | 16 | | private readonly ILogger<ChannelMemoryRepository> _logger; |
| | 0 | 17 | | private readonly ConcurrentDictionary<ChannelId, ChannelModel> _channels = []; |
| | 0 | 18 | | private readonly ConcurrentDictionary<ChannelId, ChannelState> _channelStates = []; |
| | 0 | 19 | | private readonly ConcurrentDictionary<(CompactPubKey, ChannelId), ChannelModel> _temporaryChannels = []; |
| | 0 | 20 | | 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 | | |
| | 0 | 28 | | public ChannelMemoryRepository(ILogger<ChannelMemoryRepository> logger) |
| | 0 | 29 | | { |
| | 0 | 30 | | _logger = logger; |
| | 0 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <inheritdoc/> |
| | | 34 | | public bool TryGetChannel(ChannelId channelId, [MaybeNullWhen(false)] out ChannelModel channel) |
| | 0 | 35 | | { |
| | 0 | 36 | | return _channels.TryGetValue(channelId, out channel); |
| | 0 | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <inheritdoc/> |
| | | 40 | | public List<ChannelModel> FindChannels(Func<ChannelModel, bool> predicate) |
| | 0 | 41 | | { |
| | 0 | 42 | | return _channels |
| | 0 | 43 | | .Values |
| | 0 | 44 | | .Where(predicate) |
| | 0 | 45 | | .ToList(); |
| | 0 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <inheritdoc/> |
| | | 49 | | public bool TryGetChannelState(ChannelId channelId, out ChannelState channelState) |
| | 0 | 50 | | { |
| | 0 | 51 | | return _channelStates.TryGetValue(channelId, out channelState); |
| | 0 | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <inheritdoc/> |
| | | 55 | | public void AddChannel(ChannelModel channel) |
| | 0 | 56 | | { |
| | 0 | 57 | | ArgumentNullException.ThrowIfNull(channel); |
| | | 58 | | |
| | 0 | 59 | | if (!_channels.TryAdd(channel.ChannelId, channel)) |
| | 0 | 60 | | throw new InvalidOperationException($"Channel with Id {channel.ChannelId} already exists."); |
| | | 61 | | |
| | 0 | 62 | | _channelStates[channel.ChannelId] = channel.State; |
| | 0 | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <inheritdoc/> |
| | | 66 | | public void UpdateChannel(ChannelModel channel) |
| | 0 | 67 | | { |
| | 0 | 68 | | ArgumentNullException.ThrowIfNull(channel); |
| | | 69 | | |
| | 0 | 70 | | if (!_channels.ContainsKey(channel.ChannelId)) |
| | 0 | 71 | | throw new KeyNotFoundException($"Channel with Id {channel.ChannelId} does not exist."); |
| | | 72 | | |
| | 0 | 73 | | _channels[channel.ChannelId] = channel; |
| | 0 | 74 | | _channelStates[channel.ChannelId] = channel.State; |
| | | 75 | | |
| | 0 | 76 | | OnChannelUpdated?.Invoke(this, new ChannelUpdatedEventArgs(channel)); |
| | 0 | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <inheritdoc/> |
| | | 80 | | public bool TryRemoveChannel(ChannelId channelId) |
| | 0 | 81 | | { |
| | 0 | 82 | | var removed = _channels.TryRemove(channelId, out _); |
| | 0 | 83 | | return removed && _channelStates.TryRemove(channelId, out _); |
| | 0 | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <inheritdoc/> |
| | | 87 | | public bool TryGetTemporaryChannel(CompactPubKey compactPubKey, ChannelId channelId, |
| | | 88 | | [MaybeNullWhen(false)] out ChannelModel channel) |
| | 0 | 89 | | { |
| | 0 | 90 | | return _temporaryChannels.TryGetValue((compactPubKey, channelId), out channel); |
| | 0 | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <inheritdoc/> |
| | | 94 | | public bool TryGetTemporaryChannelState(CompactPubKey compactPubKey, ChannelId channelId, |
| | | 95 | | out ChannelState channelState) |
| | 0 | 96 | | { |
| | 0 | 97 | | return _temporaryChannelStates.TryGetValue((compactPubKey, channelId), out channelState); |
| | 0 | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <inheritdoc/> |
| | | 101 | | public void AddTemporaryChannel(CompactPubKey compactPubKey, ChannelModel channel) |
| | 0 | 102 | | { |
| | 0 | 103 | | if (!_temporaryChannels.TryAdd((compactPubKey, channel.ChannelId), channel)) |
| | 0 | 104 | | throw new InvalidOperationException( |
| | 0 | 105 | | $"Temporary channel with Id {channel.ChannelId} for CompactPubKey {compactPubKey} already exists."); |
| | | 106 | | |
| | 0 | 107 | | _temporaryChannelStates[(compactPubKey, channel.ChannelId)] = channel.State; |
| | 0 | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <inheritdoc/> |
| | | 111 | | public void UpdateTemporaryChannel(CompactPubKey compactPubKey, ChannelModel channel) |
| | 0 | 112 | | { |
| | 0 | 113 | | if (!_temporaryChannels.ContainsKey((compactPubKey, channel.ChannelId))) |
| | 0 | 114 | | throw new KeyNotFoundException( |
| | 0 | 115 | | $"Temporary channel with Id {channel.ChannelId} for CompactPubKey {compactPubKey} does not exist."); |
| | | 116 | | |
| | 0 | 117 | | _temporaryChannels[(compactPubKey, channel.ChannelId)] = channel; |
| | 0 | 118 | | _temporaryChannelStates[(compactPubKey, channel.ChannelId)] = channel.State; |
| | 0 | 119 | | } |
| | | 120 | | |
| | | 121 | | /// <inheritdoc/> |
| | | 122 | | public bool TryRemoveTemporaryChannel(CompactPubKey compactPubKey, ChannelId channelId) |
| | 0 | 123 | | { |
| | 0 | 124 | | var removed = _temporaryChannels.TryRemove((compactPubKey, channelId), out _); |
| | 0 | 125 | | return removed && _temporaryChannelStates.TryRemove((compactPubKey, channelId), out _); |
| | 0 | 126 | | } |
| | | 127 | | |
| | | 128 | | /// <inheritdoc/> |
| | | 129 | | public void UpgradeChannel(ChannelId oldChannelId, ChannelModel tempChannel) |
| | 0 | 130 | | { |
| | 0 | 131 | | AddChannel(tempChannel); |
| | 0 | 132 | | if (!TryRemoveTemporaryChannel(tempChannel.RemoteNodeId, oldChannelId)) |
| | 0 | 133 | | _logger.LogWarning( |
| | 0 | 134 | | "Unable to remove Temporary Channel with Id {oldChannelId} while upgrading Channel {channelId}.", |
| | 0 | 135 | | oldChannelId, tempChannel.ChannelId); |
| | | 136 | | |
| | 0 | 137 | | OnChannelUpgraded?.Invoke(this, new ChannelUpgradedEventArgs(oldChannelId, tempChannel.ChannelId)); |
| | 0 | 138 | | } |
| | | 139 | | } |