| | 1 | | using Microsoft.Extensions.Logging; |
| | 2 | | using NLightning.Domain.Bitcoin.Transactions.Enums; |
| | 3 | | using NLightning.Domain.Bitcoin.Transactions.Interfaces; |
| | 4 | | using NLightning.Infrastructure.Bitcoin.Builders.Interfaces; |
| | 5 | | using NLightning.Infrastructure.Bitcoin.Wallet.Interfaces; |
| | 6 | |
|
| | 7 | | namespace NLightning.Application.Channels.Handlers; |
| | 8 | |
|
| | 9 | | using Domain.Bitcoin.Interfaces; |
| | 10 | | using Domain.Channels.Enums; |
| | 11 | | using Domain.Channels.Interfaces; |
| | 12 | | using Domain.Channels.Models; |
| | 13 | | using Domain.Crypto.ValueObjects; |
| | 14 | | using Domain.Exceptions; |
| | 15 | | using Domain.Node.Options; |
| | 16 | | using Domain.Persistence.Interfaces; |
| | 17 | | using Domain.Protocol.Interfaces; |
| | 18 | | using Domain.Protocol.Messages; |
| | 19 | | using Interfaces; |
| | 20 | |
|
| | 21 | | public class FundingCreatedMessageHandler : IChannelMessageHandler<FundingCreatedMessage> |
| | 22 | | { |
| | 23 | | private readonly IBlockchainMonitor _blockchainMonitor; |
| | 24 | | private readonly IChannelIdFactory _channelIdFactory; |
| | 25 | | private readonly IChannelMemoryRepository _channelMemoryRepository; |
| | 26 | | private readonly ICommitmentTransactionBuilder _commitmentTransactionBuilder; |
| | 27 | | private readonly ICommitmentTransactionModelFactory _commitmentTransactionModelFactory; |
| | 28 | | private readonly ILightningSigner _lightningSigner; |
| | 29 | | private readonly ILogger<FundingCreatedMessageHandler> _logger; |
| | 30 | | private readonly IMessageFactory _messageFactory; |
| | 31 | | private readonly IUnitOfWork _unitOfWork; |
| | 32 | |
|
| 24 | 33 | | public FundingCreatedMessageHandler(IBlockchainMonitor blockchainMonitor, IChannelIdFactory channelIdFactory, |
| 24 | 34 | | IChannelMemoryRepository channelMemoryRepository, |
| 24 | 35 | | ICommitmentTransactionBuilder commitmentTransactionBuilder, |
| 24 | 36 | | ICommitmentTransactionModelFactory commitmentTransactionModelFactory, |
| 24 | 37 | | ILightningSigner lightningSigner, ILogger<FundingCreatedMessageHandler> logger, |
| 24 | 38 | | IMessageFactory messageFactory, IUnitOfWork unitOfWork) |
| | 39 | | { |
| 24 | 40 | | _blockchainMonitor = blockchainMonitor; |
| 24 | 41 | | _channelIdFactory = channelIdFactory; |
| 24 | 42 | | _channelMemoryRepository = channelMemoryRepository; |
| 24 | 43 | | _commitmentTransactionBuilder = commitmentTransactionBuilder; |
| 24 | 44 | | _commitmentTransactionModelFactory = commitmentTransactionModelFactory; |
| 24 | 45 | | _lightningSigner = lightningSigner; |
| 24 | 46 | | _logger = logger; |
| 24 | 47 | | _messageFactory = messageFactory; |
| 24 | 48 | | _unitOfWork = unitOfWork; |
| 24 | 49 | | } |
| | 50 | |
|
| | 51 | | public async Task<IChannelMessage?> HandleAsync(FundingCreatedMessage message, ChannelState currentState, |
| | 52 | | FeatureOptions negotiatedFeatures, CompactPubKey peerPubKey) |
| | 53 | | { |
| 24 | 54 | | _logger.LogTrace("Processing FundingCreatedMessage with ChannelId: {ChannelId} from Peer: {PeerPubKey}", |
| 24 | 55 | | message.Payload.ChannelId, peerPubKey); |
| | 56 | |
|
| 24 | 57 | | var payload = message.Payload; |
| | 58 | |
|
| 24 | 59 | | if (currentState != ChannelState.None) |
| 4 | 60 | | throw new ChannelErrorException("A channel with this id already exists", payload.ChannelId); |
| | 61 | |
|
| | 62 | | // Check if there's a temporary channel for this peer |
| 20 | 63 | | if (!_channelMemoryRepository.TryGetTemporaryChannelState(peerPubKey, payload.ChannelId, out currentState)) |
| 4 | 64 | | throw new ChannelErrorException("This channel has never been negotiated", payload.ChannelId); |
| | 65 | |
|
| 16 | 66 | | if (currentState != ChannelState.V1Opening) |
| 4 | 67 | | throw new ChannelErrorException("Channel had the wrong state", payload.ChannelId, |
| 4 | 68 | | "This channel is already being negotiated with peer"); |
| | 69 | |
|
| | 70 | | // Get the channel and set missing props |
| 12 | 71 | | if (!_channelMemoryRepository.TryGetTemporaryChannel(peerPubKey, payload.ChannelId, out var channel)) |
| 4 | 72 | | throw new ChannelErrorException("Temporary channel not found", payload.ChannelId); |
| | 73 | |
|
| 8 | 74 | | channel.FundingOutput.TransactionId = payload.FundingTxId; |
| 8 | 75 | | channel.FundingOutput.Index = payload.FundingOutputIndex; |
| | 76 | |
|
| | 77 | | // Create a new channelId |
| 8 | 78 | | var oldChannelId = channel.ChannelId; |
| 8 | 79 | | channel.UpdateChannelId(_channelIdFactory.CreateV1(payload.FundingTxId, payload.FundingOutputIndex)); |
| | 80 | |
|
| | 81 | | // Register the channel with the signer |
| 8 | 82 | | _lightningSigner.RegisterChannel(channel.ChannelId, channel.GetSigningInfo()); |
| | 83 | |
|
| | 84 | | // Generate the base commitment transactions |
| 8 | 85 | | var localCommitmentTransaction = |
| 8 | 86 | | _commitmentTransactionModelFactory.CreateCommitmentTransactionModel(channel, CommitmentSide.Local); |
| 8 | 87 | | var remoteCommitmentTransaction = |
| 8 | 88 | | _commitmentTransactionModelFactory.CreateCommitmentTransactionModel(channel, CommitmentSide.Remote); |
| | 89 | |
|
| | 90 | | // Build the output and the transactions |
| 8 | 91 | | var localUnsignedCommitmentTransaction = _commitmentTransactionBuilder.Build(localCommitmentTransaction); |
| 8 | 92 | | var remoteUnsignedCommitmentTransaction = _commitmentTransactionBuilder.Build(remoteCommitmentTransaction); |
| | 93 | |
|
| | 94 | | // Validate remote signature for our local commitment transaction |
| 8 | 95 | | _lightningSigner.ValidateSignature(channel.ChannelId, payload.Signature, localUnsignedCommitmentTransaction); |
| | 96 | |
|
| | 97 | | // Sign our remote commitment transaction |
| 8 | 98 | | var ourSignature = _lightningSigner.SignTransaction(channel.ChannelId, remoteUnsignedCommitmentTransaction); |
| | 99 | |
|
| 8 | 100 | | channel.UpdateState(ChannelState.V1FundingSigned); |
| | 101 | | // Save to the database |
| 8 | 102 | | await PersistChannelAsync(channel); |
| | 103 | |
|
| | 104 | | // Create the funding signed message |
| 4 | 105 | | var fundingSignedMessage = |
| 4 | 106 | | _messageFactory.CreatedFundingSignedMessage(channel.ChannelId, ourSignature); |
| | 107 | |
|
| | 108 | | // Add the channel to the dictionary |
| 4 | 109 | | _channelMemoryRepository.AddChannel(channel); |
| | 110 | |
|
| | 111 | | // Remove the temporary channel |
| 4 | 112 | | _channelMemoryRepository.RemoveTemporaryChannel(peerPubKey, oldChannelId); |
| | 113 | |
|
| 4 | 114 | | await _blockchainMonitor.WatchTransactionAsync(channel.ChannelId, payload.FundingTxId, |
| 4 | 115 | | channel.ChannelConfig.MinimumDepth); |
| | 116 | |
|
| 4 | 117 | | return fundingSignedMessage; |
| 4 | 118 | | } |
| | 119 | |
|
| | 120 | | /// <summary> |
| | 121 | | /// Persists a channel to the database using the scoped Unit of Work |
| | 122 | | /// </summary> |
| | 123 | | private async Task PersistChannelAsync(ChannelModel channel) |
| | 124 | | { |
| | 125 | | try |
| | 126 | | { |
| | 127 | | // Check if the channel already exists |
| 8 | 128 | | var existingChannel = await _unitOfWork.ChannelDbRepository.GetByIdAsync(channel.ChannelId); |
| 8 | 129 | | if (existingChannel is not null) |
| 4 | 130 | | throw new ChannelWarningException("Channel already exists", channel.ChannelId, |
| 4 | 131 | | "This channel is already in our database"); |
| | 132 | |
|
| 4 | 133 | | await _unitOfWork.ChannelDbRepository.AddAsync(channel); |
| 4 | 134 | | await _unitOfWork.SaveChangesAsync(); |
| | 135 | |
|
| 4 | 136 | | _logger.LogDebug("Successfully persisted channel {ChannelId} to database", channel.ChannelId); |
| 4 | 137 | | } |
| 4 | 138 | | catch (Exception ex) |
| | 139 | | { |
| 4 | 140 | | _logger.LogError(ex, "Failed to persist channel {ChannelId} to database", channel.ChannelId); |
| 4 | 141 | | throw; |
| | 142 | | } |
| 4 | 143 | | } |
| | 144 | | } |