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