| | | 1 | | using System.Security.Cryptography; |
| | | 2 | | using Microsoft.Extensions.Logging; |
| | | 3 | | |
| | | 4 | | namespace NLightning.Application.Channels.Handlers; |
| | | 5 | | |
| | | 6 | | using Domain.Bitcoin.Interfaces; |
| | | 7 | | using Domain.Channels.Enums; |
| | | 8 | | using Domain.Channels.Interfaces; |
| | | 9 | | using Domain.Channels.Models; |
| | | 10 | | using Domain.Channels.ValueObjects; |
| | | 11 | | using Domain.Enums; |
| | | 12 | | using Domain.Persistence.Interfaces; |
| | | 13 | | using Domain.Protocol.Interfaces; |
| | | 14 | | |
| | | 15 | | public class FundingConfirmedMessageHandler |
| | | 16 | | { |
| | | 17 | | private readonly IChannelMemoryRepository _channelMemoryRepository; |
| | | 18 | | private readonly ILightningSigner _lightningSigner; |
| | | 19 | | private readonly ILogger<FundingConfirmedMessageHandler> _logger; |
| | | 20 | | private readonly IMessageFactory _messageFactory; |
| | | 21 | | private readonly IUnitOfWork _uow; |
| | | 22 | | |
| | | 23 | | public event EventHandler<IChannelMessage>? OnMessageReady; |
| | | 24 | | |
| | 0 | 25 | | public FundingConfirmedMessageHandler(IChannelMemoryRepository channelMemoryRepository, |
| | 0 | 26 | | ILightningSigner lightningSigner, |
| | 0 | 27 | | ILogger<FundingConfirmedMessageHandler> logger, |
| | 0 | 28 | | IMessageFactory messageFactory, |
| | 0 | 29 | | IUnitOfWork uow) |
| | | 30 | | { |
| | 0 | 31 | | _channelMemoryRepository = channelMemoryRepository; |
| | 0 | 32 | | _lightningSigner = lightningSigner; |
| | 0 | 33 | | _logger = logger; |
| | 0 | 34 | | _messageFactory = messageFactory; |
| | 0 | 35 | | _uow = uow; |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | public async Task HandleAsync(ChannelModel channel) |
| | | 39 | | { |
| | | 40 | | try |
| | | 41 | | { |
| | | 42 | | // Check if the channel is in the right state |
| | 0 | 43 | | if (channel.State is not (ChannelState.V1FundingSigned |
| | 0 | 44 | | or ChannelState.ReadyForThem)) |
| | 0 | 45 | | _logger.LogError( |
| | 0 | 46 | | "Received funding confirmation, but the channel {ChannelId} had a wrong state: {State}", |
| | 0 | 47 | | channel.ChannelId, Enum.GetName(channel.State)); |
| | | 48 | | |
| | 0 | 49 | | var mustUseScidAlias = channel.ChannelConfig.UseScidAlias > FeatureSupport.No; |
| | | 50 | | |
| | | 51 | | // Create our new per-commitment point |
| | 0 | 52 | | channel.CommitmentNumber.Increment(); |
| | 0 | 53 | | var newPerCommitmentPoint = |
| | 0 | 54 | | _lightningSigner.GetPerCommitmentPoint(channel.ChannelId, channel.CommitmentNumber.Value); |
| | 0 | 55 | | channel.LocalKeySet.UpdatePerCommitmentPoint(newPerCommitmentPoint); |
| | | 56 | | |
| | | 57 | | // Handle ScidAlias |
| | 0 | 58 | | if (mustUseScidAlias) |
| | | 59 | | { |
| | | 60 | | // Decide how many SCID aliases we need |
| | 0 | 61 | | var scidAliasesCount = RandomNumberGenerator.GetInt32(2, 6); // Randomly choose between 2 and 5 |
| | 0 | 62 | | channel.LocalAliases = new List<ShortChannelId>(); |
| | 0 | 63 | | for (var i = 0; i < scidAliasesCount; i++) |
| | | 64 | | { |
| | | 65 | | // Generate a random SCID alias |
| | 0 | 66 | | var scidAlias = new ShortChannelId(RandomNumberGenerator.GetBytes(ShortChannelId.Length)); |
| | 0 | 67 | | channel.LocalAliases.Add(scidAlias); |
| | | 68 | | } |
| | | 69 | | } |
| | | 70 | | |
| | 0 | 71 | | if (channel.State == ChannelState.ReadyForThem) |
| | | 72 | | { |
| | | 73 | | // Valid transition: ReadyForThem -> Open |
| | 0 | 74 | | channel.UpdateState(ChannelState.Open); |
| | 0 | 75 | | await PersistChannelAsync(channel); |
| | | 76 | | |
| | 0 | 77 | | _logger.LogInformation("Channel {ChannelId} is now open", channel.ChannelId); |
| | | 78 | | |
| | | 79 | | // TODO: Notify application layer that channel is fully open |
| | | 80 | | // TODO: Update routing tables |
| | | 81 | | } |
| | 0 | 82 | | else if (channel.State == ChannelState.V1FundingSigned) |
| | | 83 | | { |
| | | 84 | | // Valid transition: V1FundingSigned -> ReadyForUs |
| | 0 | 85 | | channel.UpdateState(ChannelState.ReadyForUs); |
| | 0 | 86 | | await PersistChannelAsync(channel); |
| | | 87 | | |
| | 0 | 88 | | _logger.LogInformation("Funding confirmed for us for channel {ChannelId}", |
| | 0 | 89 | | channel.ChannelId); |
| | | 90 | | } |
| | | 91 | | |
| | 0 | 92 | | if (channel.LocalAliases is { Count: > 0 }) |
| | | 93 | | { |
| | | 94 | | // Create a ChannelReady message with the SCID aliases |
| | 0 | 95 | | foreach (var alias in channel.LocalAliases) |
| | | 96 | | { |
| | 0 | 97 | | var channelReadyMessage = |
| | 0 | 98 | | _messageFactory.CreateChannelReadyMessage(channel.ChannelId, newPerCommitmentPoint, alias); |
| | | 99 | | |
| | | 100 | | // Raise the event with the message |
| | 0 | 101 | | OnMessageReady?.Invoke(this, channelReadyMessage); |
| | | 102 | | } |
| | | 103 | | } |
| | | 104 | | else |
| | | 105 | | { |
| | 0 | 106 | | var channelReadyMessage = |
| | 0 | 107 | | _messageFactory.CreateChannelReadyMessage(channel.ChannelId, newPerCommitmentPoint, |
| | 0 | 108 | | channel.ShortChannelId); |
| | | 109 | | |
| | | 110 | | // Raise the event with the message |
| | 0 | 111 | | OnMessageReady?.Invoke(this, channelReadyMessage); |
| | | 112 | | } |
| | | 113 | | |
| | 0 | 114 | | _logger.LogInformation("Channel {ChannelId} funding transaction confirmed", channel.ChannelId); |
| | 0 | 115 | | } |
| | 0 | 116 | | catch (Exception ex) |
| | | 117 | | { |
| | 0 | 118 | | _logger.LogError(ex, "Error handling funding confirmation for channel {ChannelId}", channel.ChannelId); |
| | 0 | 119 | | throw; |
| | | 120 | | } |
| | 0 | 121 | | } |
| | | 122 | | |
| | | 123 | | private async Task PersistChannelAsync(ChannelModel channel) |
| | | 124 | | { |
| | 0 | 125 | | _channelMemoryRepository.UpdateChannel(channel); |
| | 0 | 126 | | await _uow.ChannelDbRepository.UpdateAsync(channel); |
| | | 127 | | |
| | 0 | 128 | | await _uow.SaveChangesAsync(); |
| | 0 | 129 | | } |
| | | 130 | | } |