| | | 1 | | using Microsoft.Extensions.Logging; |
| | | 2 | | |
| | | 3 | | namespace NLightning.Application.Channels.Handlers; |
| | | 4 | | |
| | | 5 | | using Domain.Bitcoin.Interfaces; |
| | | 6 | | using Domain.Bitcoin.Transactions.Enums; |
| | | 7 | | using Domain.Bitcoin.Transactions.Interfaces; |
| | | 8 | | using Domain.Channels.Enums; |
| | | 9 | | using Domain.Channels.Interfaces; |
| | | 10 | | using Domain.Channels.Models; |
| | | 11 | | using Domain.Crypto.ValueObjects; |
| | | 12 | | using Domain.Exceptions; |
| | | 13 | | using Domain.Node.Options; |
| | | 14 | | using Domain.Persistence.Interfaces; |
| | | 15 | | using Domain.Protocol.Interfaces; |
| | | 16 | | using Domain.Protocol.Messages; |
| | | 17 | | using Infrastructure.Bitcoin.Builders.Interfaces; |
| | | 18 | | using Infrastructure.Bitcoin.Wallet.Interfaces; |
| | | 19 | | using Interfaces; |
| | | 20 | | |
| | | 21 | | public class FundingSignedMessageHandler : IChannelMessageHandler<FundingSignedMessage> |
| | | 22 | | { |
| | | 23 | | private readonly IBlockchainMonitor _blockchainMonitor; |
| | | 24 | | private readonly IChannelMemoryRepository _channelMemoryRepository; |
| | | 25 | | private readonly ICommitmentTransactionBuilder _commitmentTransactionBuilder; |
| | | 26 | | private readonly ICommitmentTransactionModelFactory _commitmentTransactionModelFactory; |
| | | 27 | | private readonly IFundingTransactionBuilder _fundingTransactionBuilder; |
| | | 28 | | private readonly IFundingTransactionModelFactory _fundingTransactionModelFactory; |
| | | 29 | | private readonly ILightningSigner _lightningSigner; |
| | | 30 | | private readonly ILogger<FundingSignedMessageHandler> _logger; |
| | | 31 | | private readonly IUnitOfWork _unitOfWork; |
| | | 32 | | private readonly IUtxoMemoryRepository _utxoMemoryRepository; |
| | | 33 | | |
| | 0 | 34 | | public FundingSignedMessageHandler(IBlockchainMonitor blockchainMonitor, |
| | 0 | 35 | | IChannelMemoryRepository channelMemoryRepository, |
| | 0 | 36 | | ICommitmentTransactionBuilder commitmentTransactionBuilder, |
| | 0 | 37 | | ICommitmentTransactionModelFactory commitmentTransactionModelFactory, |
| | 0 | 38 | | IFundingTransactionBuilder fundingTransactionBuilder, |
| | 0 | 39 | | IFundingTransactionModelFactory fundingTransactionModelFactory, |
| | 0 | 40 | | ILightningSigner lightningSigner, ILogger<FundingSignedMessageHandler> logger, |
| | 0 | 41 | | IUnitOfWork unitOfWork, IUtxoMemoryRepository utxoMemoryRepository) |
| | | 42 | | { |
| | 0 | 43 | | _blockchainMonitor = blockchainMonitor; |
| | 0 | 44 | | _channelMemoryRepository = channelMemoryRepository; |
| | 0 | 45 | | _commitmentTransactionBuilder = commitmentTransactionBuilder; |
| | 0 | 46 | | _commitmentTransactionModelFactory = commitmentTransactionModelFactory; |
| | 0 | 47 | | _fundingTransactionBuilder = fundingTransactionBuilder; |
| | 0 | 48 | | _fundingTransactionModelFactory = fundingTransactionModelFactory; |
| | 0 | 49 | | _lightningSigner = lightningSigner; |
| | 0 | 50 | | _logger = logger; |
| | 0 | 51 | | _unitOfWork = unitOfWork; |
| | 0 | 52 | | _utxoMemoryRepository = utxoMemoryRepository; |
| | 0 | 53 | | } |
| | | 54 | | |
| | | 55 | | public async Task<IChannelMessage?> HandleAsync(FundingSignedMessage message, ChannelState currentState, |
| | | 56 | | FeatureOptions negotiatedFeatures, CompactPubKey peerPubKey) |
| | | 57 | | { |
| | 0 | 58 | | if (_logger.IsEnabled(LogLevel.Trace)) |
| | 0 | 59 | | _logger.LogTrace("Processing FundingCreatedMessage with ChannelId: {ChannelId} from Peer: {PeerPubKey}", |
| | 0 | 60 | | message.Payload.ChannelId, peerPubKey); |
| | | 61 | | |
| | 0 | 62 | | var payload = message.Payload; |
| | | 63 | | |
| | 0 | 64 | | if (currentState != ChannelState.V1FundingCreated) |
| | 0 | 65 | | throw new ChannelErrorException( |
| | 0 | 66 | | $"Received funding signed, but the channel {payload.ChannelId} had the wrong state: {Enum.GetName(curren |
| | | 67 | | |
| | | 68 | | // Check if there's a temporary channel for this peer |
| | 0 | 69 | | if (!_channelMemoryRepository.TryGetChannel(payload.ChannelId, out var channel)) |
| | 0 | 70 | | throw new ChannelErrorException("This channel has never been negotiated", payload.ChannelId); |
| | | 71 | | |
| | | 72 | | // Generate the base commitment transactions |
| | 0 | 73 | | var localCommitmentTransaction = |
| | 0 | 74 | | _commitmentTransactionModelFactory.CreateCommitmentTransactionModel(channel, CommitmentSide.Local); |
| | | 75 | | |
| | | 76 | | // Build the output and the transactions |
| | 0 | 77 | | var localUnsignedCommitmentTransaction = _commitmentTransactionBuilder.Build(localCommitmentTransaction); |
| | | 78 | | |
| | | 79 | | // Validate remote signature for our local commitment transaction |
| | 0 | 80 | | _lightningSigner.ValidateSignature(channel.ChannelId, payload.Signature, localUnsignedCommitmentTransaction); |
| | | 81 | | |
| | | 82 | | // Update the channel with the new signature |
| | 0 | 83 | | channel.UpdateLastReceivedSignature(payload.Signature); |
| | | 84 | | |
| | | 85 | | // Get the locked utxos to create the funding transaction |
| | 0 | 86 | | var utxos = _utxoMemoryRepository.GetLockedUtxosForChannel(channel.ChannelId); |
| | | 87 | | |
| | | 88 | | // Get a change address in case we need one |
| | 0 | 89 | | var fundingTransactionModel = _fundingTransactionModelFactory.Create(channel, utxos, channel.ChangeAddress); |
| | 0 | 90 | | var unsignedFundingTransaction = _fundingTransactionBuilder.Build(fundingTransactionModel); |
| | | 91 | | |
| | | 92 | | // Sign the transaction |
| | 0 | 93 | | var allSigned = _lightningSigner.SignFundingTransaction(channel.ChannelId, unsignedFundingTransaction); |
| | 0 | 94 | | if (!allSigned) |
| | 0 | 95 | | throw new ChannelErrorException("Unable to sign all inputs for the funding transaction"); |
| | | 96 | | |
| | | 97 | | // Persist the channel to the database before publishing the transaction, so the watched transaction can point |
| | | 98 | | // to the channel |
| | 0 | 99 | | await PersistChannelAsync(channel); |
| | | 100 | | |
| | 0 | 101 | | await _blockchainMonitor.PublishAndWatchTransactionAsync(channel.ChannelId, unsignedFundingTransaction, |
| | 0 | 102 | | channel.ChannelConfig.MinimumDepth); |
| | | 103 | | |
| | | 104 | | // Now that we should remember the channel, we update its state |
| | 0 | 105 | | channel.UpdateState(ChannelState.V1FundingSigned); |
| | | 106 | | |
| | | 107 | | // Save to the database |
| | 0 | 108 | | await PersistChannelAsync(channel); |
| | | 109 | | |
| | 0 | 110 | | return null; |
| | 0 | 111 | | } |
| | | 112 | | |
| | | 113 | | /// <summary> |
| | | 114 | | /// Persists a channel to the database using a scoped Unit of Work |
| | | 115 | | /// </summary> |
| | | 116 | | private async Task PersistChannelAsync(ChannelModel channel) |
| | | 117 | | { |
| | | 118 | | try |
| | | 119 | | { |
| | | 120 | | // Update the channel in memory first |
| | 0 | 121 | | _channelMemoryRepository.UpdateChannel(channel); |
| | | 122 | | |
| | | 123 | | // Check if we are adding or if we need to update the channel |
| | 0 | 124 | | var existingChannel = await _unitOfWork.ChannelDbRepository.GetByIdAsync(channel.ChannelId); |
| | 0 | 125 | | if (existingChannel is not null) |
| | 0 | 126 | | await _unitOfWork.ChannelDbRepository.UpdateAsync(channel); |
| | | 127 | | else |
| | 0 | 128 | | await _unitOfWork.ChannelDbRepository.AddAsync(channel); |
| | | 129 | | |
| | 0 | 130 | | await _unitOfWork.SaveChangesAsync(); |
| | | 131 | | |
| | 0 | 132 | | if (_logger.IsEnabled(LogLevel.Debug)) |
| | 0 | 133 | | _logger.LogDebug("Successfully persisted channel {ChannelId} to database", channel.ChannelId); |
| | 0 | 134 | | } |
| | 0 | 135 | | catch (Exception ex) |
| | | 136 | | { |
| | 0 | 137 | | _logger.LogError(ex, "Failed to persist channel {ChannelId} to database", channel.ChannelId); |
| | 0 | 138 | | throw; |
| | | 139 | | } |
| | 0 | 140 | | } |
| | | 141 | | } |