| | 1 | | using Microsoft.Extensions.Logging; |
| | 2 | |
|
| | 3 | | namespace NLightning.Application.Channels.Handlers; |
| | 4 | |
|
| | 5 | | using Domain.Channels.Enums; |
| | 6 | | using Domain.Channels.Interfaces; |
| | 7 | | using Domain.Crypto.ValueObjects; |
| | 8 | | using Domain.Exceptions; |
| | 9 | | using Domain.Node.Options; |
| | 10 | | using Domain.Protocol.Interfaces; |
| | 11 | | using Domain.Protocol.Messages; |
| | 12 | | using Domain.Protocol.Tlv; |
| | 13 | | using Interfaces; |
| | 14 | |
|
| | 15 | | public class OpenChannel1MessageHandler : IChannelMessageHandler<OpenChannel1Message> |
| | 16 | | { |
| | 17 | | private readonly IChannelFactory _channelFactory; |
| | 18 | | private readonly IChannelMemoryRepository _channelMemoryRepository; |
| | 19 | | private readonly ILogger<OpenChannel1MessageHandler> _logger; |
| | 20 | | private readonly IMessageFactory _messageFactory; |
| | 21 | |
|
| 20 | 22 | | public OpenChannel1MessageHandler(IChannelFactory channelFactory, IChannelMemoryRepository channelMemoryRepository, |
| 20 | 23 | | ILogger<OpenChannel1MessageHandler> logger, IMessageFactory messageFactory) |
| | 24 | | { |
| 20 | 25 | | _channelFactory = channelFactory; |
| 20 | 26 | | _channelMemoryRepository = channelMemoryRepository; |
| 20 | 27 | | _logger = logger; |
| 20 | 28 | | _messageFactory = messageFactory; |
| 20 | 29 | | } |
| | 30 | |
|
| | 31 | | public async Task<IChannelMessage?> HandleAsync(OpenChannel1Message message, ChannelState currentState, |
| | 32 | | FeatureOptions negotiatedFeatures, CompactPubKey peerPubKey) |
| | 33 | | { |
| 20 | 34 | | _logger.LogTrace("Processing OpenChannel1Message with ChannelId: {ChannelId} from Peer: {PeerPubKey}", |
| 20 | 35 | | message.Payload.ChannelId, peerPubKey); |
| | 36 | |
|
| 20 | 37 | | var payload = message.Payload; |
| | 38 | |
|
| 20 | 39 | | if (currentState != ChannelState.None) |
| 4 | 40 | | throw new ChannelErrorException("A channel with this id already exists", payload.ChannelId); |
| | 41 | |
|
| | 42 | | // Check if there's a temporary channel for this peer |
| 16 | 43 | | if (_channelMemoryRepository.TryGetTemporaryChannelState(peerPubKey, payload.ChannelId, out currentState)) |
| | 44 | | { |
| 8 | 45 | | if (currentState != ChannelState.V1Opening) |
| | 46 | | { |
| 4 | 47 | | throw new ChannelErrorException("Channel had the wrong state", payload.ChannelId, |
| 4 | 48 | | "This channel is already being negotiated with peer"); |
| | 49 | | } |
| | 50 | | } |
| | 51 | |
|
| | 52 | | // Create the channel |
| 12 | 53 | | var channel = await _channelFactory.CreateChannelV1AsNonInitiatorAsync(message, negotiatedFeatures, peerPubKey); |
| | 54 | |
|
| 12 | 55 | | _logger.LogTrace("Created Channel with fundingPubKey: {fundingPubKey}", |
| 12 | 56 | | channel.LocalKeySet.FundingCompactPubKey); |
| | 57 | |
|
| | 58 | | // Add the channel to dictionaries |
| 12 | 59 | | _channelMemoryRepository.AddTemporaryChannel(peerPubKey, channel); |
| | 60 | |
|
| | 61 | | // Create UpfrontShutdownScriptTlv if needed |
| 12 | 62 | | UpfrontShutdownScriptTlv? upfrontShutdownScriptTlv = null; |
| 12 | 63 | | if (channel.LocalUpfrontShutdownScript is not null) |
| 4 | 64 | | upfrontShutdownScriptTlv = new UpfrontShutdownScriptTlv(channel.LocalUpfrontShutdownScript.Value); |
| | 65 | |
|
| | 66 | | // TODO: Create the ChannelTypeTlv |
| | 67 | |
|
| | 68 | | // Create the reply message |
| 12 | 69 | | var acceptChannel1ReplyMessage = _messageFactory |
| 12 | 70 | | .CreateAcceptChannel1Message(channel.ChannelConfig.ChannelReserveAmount!, null, |
| 12 | 71 | | channel.LocalKeySet.DelayedPaymentCompactBasepoint, |
| 12 | 72 | | channel.LocalKeySet.CurrentPerCommitmentCompactPoint, |
| 12 | 73 | | channel.LocalKeySet.FundingCompactPubKey, |
| 12 | 74 | | channel.LocalKeySet.HtlcCompactBasepoint, |
| 12 | 75 | | channel.ChannelConfig.MaxAcceptedHtlcs, |
| 12 | 76 | | channel.ChannelConfig.MaxHtlcAmountInFlight, channel.ChannelConfig.MinimumDepth, |
| 12 | 77 | | channel.LocalKeySet.PaymentCompactBasepoint, |
| 12 | 78 | | channel.LocalKeySet.RevocationCompactBasepoint, channel.ChannelId, |
| 12 | 79 | | channel.ChannelConfig.ToSelfDelay, upfrontShutdownScriptTlv); |
| | 80 | |
|
| 12 | 81 | | return acceptChannel1ReplyMessage; |
| 12 | 82 | | } |
| | 83 | | } |