| | | 1 | | using NLightning.Domain.Bitcoin.Interfaces; |
| | | 2 | | using NLightning.Domain.Bitcoin.Transactions.Constants; |
| | | 3 | | using NLightning.Domain.Bitcoin.Transactions.Enums; |
| | | 4 | | using NLightning.Domain.Bitcoin.Transactions.Interfaces; |
| | | 5 | | using NLightning.Domain.Bitcoin.Transactions.Models; |
| | | 6 | | using NLightning.Domain.Bitcoin.Transactions.Outputs; |
| | | 7 | | using NLightning.Domain.Channels.Enums; |
| | | 8 | | using NLightning.Domain.Channels.Models; |
| | | 9 | | using NLightning.Domain.Channels.ValueObjects; |
| | | 10 | | using NLightning.Domain.Exceptions; |
| | | 11 | | using NLightning.Domain.Money; |
| | | 12 | | using NLightning.Domain.Protocol.Interfaces; |
| | | 13 | | |
| | | 14 | | namespace NLightning.Domain.Bitcoin.Transactions.Factories; |
| | | 15 | | |
| | | 16 | | public class CommitmentTransactionModelFactory : ICommitmentTransactionModelFactory |
| | | 17 | | { |
| | | 18 | | private readonly ICommitmentKeyDerivationService _commitmentKeyDerivationService; |
| | | 19 | | private readonly ILightningSigner _lightningSigner; |
| | | 20 | | |
| | 68 | 21 | | public CommitmentTransactionModelFactory(ICommitmentKeyDerivationService commitmentKeyDerivationService, |
| | 68 | 22 | | ILightningSigner lightningSigner) |
| | | 23 | | { |
| | 68 | 24 | | _commitmentKeyDerivationService = commitmentKeyDerivationService; |
| | 68 | 25 | | _lightningSigner = lightningSigner; |
| | 68 | 26 | | } |
| | | 27 | | |
| | | 28 | | public CommitmentTransactionModel CreateCommitmentTransactionModel(ChannelModel channel, CommitmentSide side) |
| | | 29 | | { |
| | | 30 | | // Guarantee we have a RemoteKeySet |
| | 68 | 31 | | if (channel.RemoteKeySet is null) |
| | 0 | 32 | | throw new InvalidOperationException( |
| | 0 | 33 | | "Channel must have a RemoteKeySet to create a commitment transaction model"); |
| | 0 | 34 | | |
| | 0 | 35 | | // Guarantee we have a CommitmentNumber |
| | 68 | 36 | | if (channel.CommitmentNumber is null) |
| | 0 | 37 | | throw new InvalidOperationException( |
| | 0 | 38 | | "Channel must have a CommitmentNumber to create a commitment transaction model"); |
| | 0 | 39 | | |
| | 0 | 40 | | // Guarantee we have a FundingOutput |
| | 68 | 41 | | if (channel.FundingOutput is null) |
| | 0 | 42 | | throw new InvalidOperationException( |
| | 0 | 43 | | "Channel must have a FundingOutput to create a commitment transaction model"); |
| | 0 | 44 | | |
| | 0 | 45 | | // Create base output information |
| | 68 | 46 | | ToLocalOutputInfo? toLocalOutput = null; |
| | 68 | 47 | | ToRemoteOutputInfo? toRemoteOutput = null; |
| | 68 | 48 | | AnchorOutputInfo? localAnchorOutput = null; |
| | 68 | 49 | | AnchorOutputInfo? remoteAnchorOutput = null; |
| | 68 | 50 | | var offeredHtlcOutputs = new List<OfferedHtlcOutputInfo>(); |
| | 68 | 51 | | var receivedHtlcOutputs = new List<ReceivedHtlcOutputInfo>(); |
| | 0 | 52 | | |
| | 0 | 53 | | // Get the HTLCs based on the commitment side |
| | 68 | 54 | | var htlcs = new List<Htlc>(); |
| | 68 | 55 | | htlcs.AddRange(channel.LocalOfferedHtlcs?.ToList() ?? []); |
| | 68 | 56 | | htlcs.AddRange(channel.RemoteOfferedHtlcs?.ToList() ?? []); |
| | 0 | 57 | | |
| | 0 | 58 | | // Get basepoints from the signer instead of the old key set model |
| | 68 | 59 | | var localBasepoints = _lightningSigner.GetChannelBasepoints(channel.LocalKeySet.KeyIndex); |
| | 68 | 60 | | var remoteBasepoints = new ChannelBasepoints(channel.RemoteKeySet!.FundingCompactPubKey, |
| | 68 | 61 | | channel.RemoteKeySet.RevocationCompactBasepoint, |
| | 68 | 62 | | channel.RemoteKeySet.PaymentCompactBasepoint, |
| | 68 | 63 | | channel.RemoteKeySet.DelayedPaymentCompactBasepoint, |
| | 68 | 64 | | channel.RemoteKeySet.HtlcCompactBasepoint); |
| | | 65 | | |
| | | 66 | | // Derive the commitment keys from the appropriate perspective |
| | 68 | 67 | | var commitmentKeys = side switch |
| | 68 | 68 | | { |
| | 68 | 69 | | CommitmentSide.Local => _commitmentKeyDerivationService.DeriveLocalCommitmentKeys( |
| | 68 | 70 | | channel.LocalKeySet.KeyIndex, localBasepoints, remoteBasepoints, |
| | 68 | 71 | | channel.LocalKeySet.CurrentPerCommitmentIndex), |
| | 68 | 72 | | |
| | 0 | 73 | | CommitmentSide.Remote => _commitmentKeyDerivationService.DeriveRemoteCommitmentKeys( |
| | 0 | 74 | | localBasepoints, remoteBasepoints, channel.RemoteKeySet.CurrentPerCommitmentCompactPoint), |
| | 68 | 75 | | |
| | 0 | 76 | | _ => throw new ArgumentOutOfRangeException(nameof(side), side, |
| | 0 | 77 | | "You should use either Local or Remote commitment side.") |
| | 68 | 78 | | }; |
| | 0 | 79 | | |
| | | 80 | | // Calculate base weight |
| | 68 | 81 | | var weight = WeightConstants.TransactionBaseWeight |
| | 68 | 82 | | + TransactionConstants.CommitmentTransactionInputWeight |
| | 68 | 83 | | // + htlcs.Count * WeightConstants.HtlcOutputWeight |
| | 68 | 84 | | + WeightConstants.P2WshOutputWeight; // To Local Output |
| | 0 | 85 | | |
| | 0 | 86 | | // Set initial amounts for to_local and to_remote outputs |
| | 68 | 87 | | var toLocalAmount = side == CommitmentSide.Local |
| | 68 | 88 | | ? channel.LocalBalance |
| | 68 | 89 | | : channel.RemoteBalance; |
| | | 90 | | |
| | 68 | 91 | | var toRemoteAmount = side == CommitmentSide.Local |
| | 68 | 92 | | ? channel.RemoteBalance |
| | 68 | 93 | | : channel.LocalBalance; |
| | 0 | 94 | | |
| | 68 | 95 | | var localDustLimitAmount = side == CommitmentSide.Local |
| | 68 | 96 | | ? channel.ChannelConfig.LocalDustLimitAmount |
| | 68 | 97 | | : channel.ChannelConfig.RemoteDustLimitAmount; |
| | 0 | 98 | | |
| | 68 | 99 | | var remoteDustLimitAmount = side == CommitmentSide.Local |
| | 68 | 100 | | ? channel.ChannelConfig.RemoteDustLimitAmount |
| | 68 | 101 | | : channel.ChannelConfig.LocalDustLimitAmount; |
| | 0 | 102 | | |
| | 68 | 103 | | if (htlcs is { Count: > 0 }) |
| | 0 | 104 | | { |
| | | 105 | | // Calculate htlc weight and fee |
| | 60 | 106 | | var offeredHtlcWeight = channel.ChannelConfig.OptionAnchorOutputs |
| | 60 | 107 | | ? WeightConstants.HtlcTimeoutWeightAnchors |
| | 60 | 108 | | : WeightConstants.HtlcTimeoutWeightNoAnchors; |
| | 60 | 109 | | var offeredHtlcFee = |
| | 60 | 110 | | LightningMoney.MilliSatoshis(offeredHtlcWeight * channel.ChannelConfig.FeeRateAmountPerKw.Satoshi); |
| | | 111 | | |
| | 60 | 112 | | var receivedHtlcWeight = channel.ChannelConfig.OptionAnchorOutputs |
| | 60 | 113 | | ? WeightConstants.HtlcSuccessWeightAnchors |
| | 60 | 114 | | : WeightConstants.HtlcSuccessWeightNoAnchors; |
| | 60 | 115 | | var receivedHtlcFee = |
| | 60 | 116 | | LightningMoney.MilliSatoshis(receivedHtlcWeight * channel.ChannelConfig.FeeRateAmountPerKw.Satoshi); |
| | | 117 | | |
| | 704 | 118 | | foreach (var htlc in htlcs) |
| | 0 | 119 | | { |
| | 0 | 120 | | // Determine if this is an offered or received HTLC from the perspective of the commitment holder |
| | 292 | 121 | | var isOffered = side == CommitmentSide.Local |
| | 292 | 122 | | ? htlc.Direction == HtlcDirection.Outgoing |
| | 292 | 123 | | : htlc.Direction == HtlcDirection.Incoming; |
| | | 124 | | |
| | | 125 | | // Calculate the amounts after subtracting fees |
| | 292 | 126 | | var htlcFee = isOffered ? offeredHtlcFee : receivedHtlcFee; |
| | 292 | 127 | | var htlcAmount = htlc.Amount.Satoshi > htlcFee.Satoshi |
| | 292 | 128 | | ? LightningMoney.Satoshis(htlc.Amount.Satoshi - htlcFee.Satoshi) |
| | 292 | 129 | | : LightningMoney.Zero; |
| | 0 | 130 | | |
| | 0 | 131 | | // Always subtract the full HTLC amount from to_local |
| | 292 | 132 | | toLocalAmount = toLocalAmount > htlc.Amount |
| | 292 | 133 | | ? toLocalAmount - htlc.Amount |
| | 292 | 134 | | : LightningMoney.Zero; // If not enough, set to zero |
| | | 135 | | |
| | | 136 | | // Offered or received depends on dust check |
| | 292 | 137 | | if (htlcAmount.Satoshi < localDustLimitAmount.Satoshi) |
| | 0 | 138 | | continue; |
| | 0 | 139 | | |
| | 132 | 140 | | weight += WeightConstants.HtlcOutputWeight; |
| | 132 | 141 | | if (isOffered) |
| | | 142 | | { |
| | 64 | 143 | | offeredHtlcOutputs.Add(new OfferedHtlcOutputInfo( |
| | 64 | 144 | | htlc, |
| | 64 | 145 | | commitmentKeys.LocalHtlcPubKey, |
| | 64 | 146 | | commitmentKeys.RemoteHtlcPubKey, |
| | 64 | 147 | | commitmentKeys.RevocationPubKey)); |
| | 0 | 148 | | } |
| | | 149 | | else |
| | 0 | 150 | | { |
| | 68 | 151 | | receivedHtlcOutputs.Add(new ReceivedHtlcOutputInfo( |
| | 68 | 152 | | htlc, |
| | 68 | 153 | | commitmentKeys.LocalHtlcPubKey, |
| | 68 | 154 | | commitmentKeys.RemoteHtlcPubKey, |
| | 68 | 155 | | commitmentKeys.RevocationPubKey)); |
| | | 156 | | } |
| | 0 | 157 | | } |
| | 0 | 158 | | } |
| | 0 | 159 | | |
| | | 160 | | LightningMoney fee; |
| | | 161 | | // Create anchor outputs if option_anchors is negotiated |
| | 68 | 162 | | if (channel.ChannelConfig.OptionAnchorOutputs) |
| | 0 | 163 | | { |
| | 0 | 164 | | localAnchorOutput = new AnchorOutputInfo(channel.LocalKeySet.FundingCompactPubKey, true); |
| | 0 | 165 | | remoteAnchorOutput = new AnchorOutputInfo(channel.RemoteKeySet.FundingCompactPubKey, false); |
| | 0 | 166 | | |
| | 0 | 167 | | weight += WeightConstants.AnchorOutputWeight * 2 |
| | 0 | 168 | | + WeightConstants.P2WshOutputWeight; // Add ToRemote Output weight |
| | 0 | 169 | | fee = LightningMoney.MilliSatoshis(weight * channel.ChannelConfig.FeeRateAmountPerKw.Satoshi); |
| | 0 | 170 | | |
| | 0 | 171 | | ref var feePayerAmount = |
| | 0 | 172 | | ref GetFeePayerAmount(side, channel.IsInitiator, ref toLocalAmount, ref toRemoteAmount); |
| | 0 | 173 | | AdjustForAnchorOutputs(ref feePayerAmount, fee, TransactionConstants.AnchorOutputAmount); |
| | | 174 | | } |
| | | 175 | | else |
| | 0 | 176 | | { |
| | 68 | 177 | | weight += WeightConstants.P2WpkhOutputWeight; // Add ToRemote Output weight |
| | 68 | 178 | | fee = LightningMoney.MilliSatoshis(weight * channel.ChannelConfig.FeeRateAmountPerKw.Satoshi); |
| | 0 | 179 | | |
| | 68 | 180 | | ref var feePayerAmount = |
| | 68 | 181 | | ref GetFeePayerAmount(side, channel.IsInitiator, ref toLocalAmount, ref toRemoteAmount); |
| | 0 | 182 | | |
| | | 183 | | // Simple fee deduction when no anchors |
| | 68 | 184 | | feePayerAmount = feePayerAmount.Satoshi > fee.Satoshi |
| | 68 | 185 | | ? LightningMoney.Satoshis(feePayerAmount.Satoshi - fee.Satoshi) |
| | 68 | 186 | | : LightningMoney.Zero; |
| | | 187 | | } |
| | | 188 | | |
| | 0 | 189 | | // Fail if both amounts are below ChannelReserve |
| | 68 | 190 | | if (channel.ChannelConfig.ChannelReserveAmount is not null |
| | 68 | 191 | | && toLocalAmount.Satoshi < channel.ChannelConfig.ChannelReserveAmount.Satoshi |
| | 68 | 192 | | && toRemoteAmount.Satoshi < channel.ChannelConfig.ChannelReserveAmount.Satoshi) |
| | 0 | 193 | | throw new ChannelErrorException("Both to_local and to_remote amounts are below the reserve limits."); |
| | | 194 | | |
| | 0 | 195 | | // Only create output if the amount is above the dust limit |
| | 68 | 196 | | if (toLocalAmount.Satoshi >= localDustLimitAmount.Satoshi) |
| | | 197 | | { |
| | 60 | 198 | | toLocalOutput = new ToLocalOutputInfo(toLocalAmount, commitmentKeys.LocalDelayedPubKey, |
| | 60 | 199 | | commitmentKeys.RevocationPubKey, |
| | 60 | 200 | | channel.ChannelConfig.ToSelfDelay); |
| | | 201 | | } |
| | 0 | 202 | | |
| | 68 | 203 | | if (toRemoteAmount.Satoshi >= remoteDustLimitAmount.Satoshi) |
| | | 204 | | { |
| | 68 | 205 | | var remotePubKey = side == CommitmentSide.Local |
| | 68 | 206 | | ? channel.RemoteKeySet.PaymentCompactBasepoint |
| | 68 | 207 | | : channel.LocalKeySet.PaymentCompactBasepoint; |
| | | 208 | | |
| | 68 | 209 | | toRemoteOutput = |
| | 68 | 210 | | new ToRemoteOutputInfo(toRemoteAmount, remotePubKey, channel.ChannelConfig.OptionAnchorOutputs); |
| | 0 | 211 | | } |
| | 0 | 212 | | |
| | 68 | 213 | | if (offeredHtlcOutputs.Count == 0 && receivedHtlcOutputs.Count == 0) |
| | | 214 | | { |
| | | 215 | | // If no HTLCs and no to_local, we can remove our anchor output |
| | 24 | 216 | | if (toLocalOutput is null) |
| | 8 | 217 | | localAnchorOutput = null; |
| | | 218 | | |
| | | 219 | | // If no HTLCs and no to_remote, we can remove their anchor output |
| | 24 | 220 | | if (toRemoteOutput is null) |
| | 0 | 221 | | remoteAnchorOutput = null; |
| | | 222 | | } |
| | 0 | 223 | | |
| | 0 | 224 | | // Create and return the commitment transaction model |
| | 68 | 225 | | return new CommitmentTransactionModel(channel.CommitmentNumber!, fee, channel.FundingOutput!, |
| | 68 | 226 | | localAnchorOutput, remoteAnchorOutput, toLocalOutput, toRemoteOutput, |
| | 68 | 227 | | offeredHtlcOutputs, receivedHtlcOutputs); |
| | | 228 | | } |
| | | 229 | | |
| | | 230 | | private static ref LightningMoney GetFeePayerAmount(CommitmentSide side, bool isInitiator, |
| | | 231 | | ref LightningMoney toLocal, |
| | 0 | 232 | | ref LightningMoney toRemote) |
| | | 233 | | { |
| | 0 | 234 | | // If we're the initiator, and it's our tx, deduct from toLocal |
| | 0 | 235 | | // If not initiator and our tx, deduct from toRemote |
| | 0 | 236 | | // For remote tx, logic is reversed |
| | 68 | 237 | | if ((side == CommitmentSide.Local && isInitiator) || (side == CommitmentSide.Remote && !isInitiator)) |
| | 68 | 238 | | return ref toLocal; |
| | | 239 | | |
| | 0 | 240 | | return ref toRemote; |
| | 0 | 241 | | } |
| | | 242 | | |
| | | 243 | | private static void AdjustForAnchorOutputs(ref LightningMoney amount, LightningMoney fee, |
| | | 244 | | LightningMoney anchorAmount) |
| | | 245 | | { |
| | 0 | 246 | | if (amount > fee) |
| | | 247 | | { |
| | 0 | 248 | | amount -= fee; |
| | 0 | 249 | | amount = amount > anchorAmount |
| | 0 | 250 | | ? amount - anchorAmount |
| | 0 | 251 | | : LightningMoney.Zero; |
| | | 252 | | } |
| | | 253 | | else |
| | 0 | 254 | | amount = LightningMoney.Zero; |
| | 0 | 255 | | } |
| | | 256 | | } |