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