| | 1 | | using NBitcoin; |
| | 2 | | using NBitcoin.Crypto; |
| | 3 | |
|
| | 4 | | namespace NLightning.Infrastructure.Bitcoin.Transactions; |
| | 5 | |
|
| | 6 | | using Domain.Money; |
| | 7 | | using Domain.Protocol.Constants; |
| | 8 | | using Domain.ValueObjects; |
| | 9 | | using Outputs; |
| | 10 | | using Protocol.Models; |
| | 11 | |
|
| | 12 | | /// <summary> |
| | 13 | | /// Represents a commitment transaction. |
| | 14 | | /// </summary> |
| | 15 | | public class CommitmentTransaction : BaseTransaction |
| | 16 | | { |
| | 17 | | #region Private Fields |
| | 18 | | private readonly LightningMoney _anchorAmount; |
| | 19 | | private readonly LightningMoney _dustLimitAmount; |
| | 20 | | private readonly bool _isChannelFunder; |
| | 21 | | private readonly bool _mustTrimHtlcOutputs; |
| | 22 | |
|
| | 23 | | private LightningMoney _toFunderAmount; |
| | 24 | | #endregion |
| | 25 | |
|
| | 26 | | #region Public Properties |
| 528 | 27 | | public ToLocalOutput ToLocalOutput { get; } |
| 760 | 28 | | public ToRemoteOutput ToRemoteOutput { get; } |
| 284 | 29 | | public ToAnchorOutput? LocalAnchorOutput { get; private set; } |
| 284 | 30 | | public ToAnchorOutput? RemoteAnchorOutput { get; private set; } |
| 8 | 31 | | public CommitmentNumber CommitmentNumber { get; } |
| 360 | 32 | | public IList<OfferedHtlcOutput> OfferedHtlcOutputs { get; } = []; |
| 368 | 33 | | public IList<ReceivedHtlcOutput> ReceivedHtlcOutputs { get; } = []; |
| | 34 | |
|
| | 35 | | #endregion |
| | 36 | |
|
| | 37 | | #region Constructors |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// Initializes a new instance of the <see cref="CommitmentTransaction"/> class. |
| | 41 | | /// </summary> |
| | 42 | | /// <param name="anchorAmount">The anchor amount.</param> |
| | 43 | | /// <param name="network">The network type.</param> |
| | 44 | | /// <param name="mustTrimHtlcOutputs">Indicates if HTLC outputs must be trimmed.</param> |
| | 45 | | /// <param name="dustLimitAmount"></param> |
| | 46 | | /// <param name="fundingOutput">The funding coin.</param> |
| | 47 | | /// <param name="localPaymentBasepoint">The local public key.</param> |
| | 48 | | /// <param name="remotePaymentBasepoint">The remote public key.</param> |
| | 49 | | /// <param name="localDelayedPubKey">The local delayed public key.</param> |
| | 50 | | /// <param name="revocationPubKey">The revocation public key.</param> |
| | 51 | | /// <param name="toLocalAmount">The amount for the to_local output in satoshis.</param> |
| | 52 | | /// <param name="toRemoteAmount">The amount for the to_remote output in satoshis.</param> |
| | 53 | | /// <param name="toSelfDelay">The to_self_delay in blocks.</param> |
| | 54 | | /// <param name="commitmentNumber">The commitment number object.</param> |
| | 55 | | /// <param name="isChannelFunder">Indicates if the local node is the channel funder.</param> |
| | 56 | | internal CommitmentTransaction(LightningMoney anchorAmount, LightningMoney dustLimitAmount, |
| | 57 | | bool mustTrimHtlcOutputs, Network network, FundingOutput fundingOutput, |
| | 58 | | PubKey localPaymentBasepoint, PubKey remotePaymentBasepoint, |
| | 59 | | PubKey localDelayedPubKey, PubKey revocationPubKey, LightningMoney toLocalAmount, |
| | 60 | | LightningMoney toRemoteAmount, uint toSelfDelay, CommitmentNumber commitmentNumber, |
| | 61 | | bool isChannelFunder) |
| 140 | 62 | | : base(!anchorAmount.IsZero, network, TransactionConstants.COMMITMENT_TRANSACTION_VERSION, SigHash.All, |
| 140 | 63 | | (fundingOutput.ToCoin(), commitmentNumber.CalculateSequence())) |
| | 64 | | { |
| 140 | 65 | | ArgumentNullException.ThrowIfNull(localPaymentBasepoint); |
| 140 | 66 | | ArgumentNullException.ThrowIfNull(remotePaymentBasepoint); |
| 140 | 67 | | ArgumentNullException.ThrowIfNull(localDelayedPubKey); |
| 140 | 68 | | ArgumentNullException.ThrowIfNull(revocationPubKey); |
| | 69 | |
|
| 140 | 70 | | if (toLocalAmount.IsZero && toRemoteAmount.IsZero) |
| | 71 | | { |
| 4 | 72 | | throw new ArgumentException("Both toLocalAmount and toRemoteAmount cannot be zero."); |
| | 73 | | } |
| | 74 | |
|
| 136 | 75 | | _anchorAmount = anchorAmount; |
| 136 | 76 | | _dustLimitAmount = dustLimitAmount; |
| 136 | 77 | | _isChannelFunder = isChannelFunder; |
| 136 | 78 | | _mustTrimHtlcOutputs = mustTrimHtlcOutputs; |
| 136 | 79 | | CommitmentNumber = commitmentNumber; |
| | 80 | |
|
| | 81 | | // Set locktime |
| 136 | 82 | | SetLockTime(commitmentNumber.CalculateLockTime()); |
| | 83 | |
|
| | 84 | | // Set funder amount |
| 136 | 85 | | var localAmount = LightningMoney.Zero; |
| 136 | 86 | | var remoteAmount = LightningMoney.Zero; |
| 136 | 87 | | if (_isChannelFunder) |
| | 88 | | { |
| | 89 | | // localAmount will be calculated later |
| 128 | 90 | | _toFunderAmount = toLocalAmount; |
| 128 | 91 | | remoteAmount = toRemoteAmount; |
| | 92 | | } |
| | 93 | | else |
| | 94 | | { |
| | 95 | | // remoteAmount will be calculated later |
| 8 | 96 | | _toFunderAmount = toRemoteAmount; |
| 8 | 97 | | localAmount = toLocalAmount; |
| | 98 | | } |
| | 99 | |
|
| | 100 | | // to_local output |
| 136 | 101 | | ToLocalOutput = new ToLocalOutput(localDelayedPubKey, revocationPubKey, toSelfDelay, localAmount); |
| 136 | 102 | | AddOutput(ToLocalOutput); |
| | 103 | |
|
| | 104 | | // to_remote output |
| 136 | 105 | | ToRemoteOutput = new ToRemoteOutput(!anchorAmount.IsZero, remotePaymentBasepoint, remoteAmount); |
| 136 | 106 | | AddOutput(ToRemoteOutput); |
| | 107 | |
|
| 136 | 108 | | if (anchorAmount == LightningMoney.Zero) |
| | 109 | | { |
| 84 | 110 | | return; |
| | 111 | | } |
| | 112 | |
|
| | 113 | | // Local anchor output |
| 52 | 114 | | LocalAnchorOutput = new ToAnchorOutput(fundingOutput.LocalPubKey, anchorAmount); |
| 52 | 115 | | AddOutput(LocalAnchorOutput); |
| | 116 | |
|
| | 117 | | // Remote anchor output |
| 52 | 118 | | RemoteAnchorOutput = new ToAnchorOutput(fundingOutput.RemotePubKey, anchorAmount); |
| 52 | 119 | | AddOutput(RemoteAnchorOutput); |
| 52 | 120 | | } |
| | 121 | | #endregion |
| | 122 | |
|
| | 123 | | #region Public Methods |
| | 124 | | public void AddOfferedHtlcOutput(OfferedHtlcOutput offeredHtlcOutput) |
| | 125 | | { |
| 96 | 126 | | if (Finalized) |
| | 127 | | { |
| 0 | 128 | | throw new InvalidOperationException("You can't add outputs to an already finalized transaction."); |
| | 129 | | } |
| | 130 | |
|
| | 131 | | // Add output |
| 96 | 132 | | OfferedHtlcOutputs.Add(offeredHtlcOutput); |
| 96 | 133 | | AddOutput(offeredHtlcOutput); |
| 96 | 134 | | } |
| | 135 | |
|
| | 136 | | public void AddReceivedHtlcOutput(ReceivedHtlcOutput receivedHtlcOutput) |
| | 137 | | { |
| 104 | 138 | | if (Finalized) |
| | 139 | | { |
| 0 | 140 | | throw new InvalidOperationException("You can't add outputs to an already finalized transaction."); |
| | 141 | | } |
| | 142 | |
|
| 104 | 143 | | ReceivedHtlcOutputs.Add(receivedHtlcOutput); |
| 104 | 144 | | AddOutput(receivedHtlcOutput); |
| 104 | 145 | | } |
| | 146 | |
|
| | 147 | | public void AppendRemoteSignatureAndSign(ECDSASignature remoteSignature, PubKey remotePubKey) |
| | 148 | | { |
| 104 | 149 | | AppendRemoteSignatureToTransaction(new TransactionSignature(remoteSignature), remotePubKey); |
| 104 | 150 | | SignTransactionWithExistingKeys(); |
| 104 | 151 | | } |
| | 152 | |
|
| | 153 | | public Transaction GetSignedTransaction() |
| | 154 | | { |
| 124 | 155 | | if (Finalized) |
| | 156 | | { |
| 120 | 157 | | return FinalizedTransaction; |
| | 158 | | } |
| | 159 | |
|
| 4 | 160 | | throw new InvalidOperationException("You have to sign and finalize the transaction first."); |
| | 161 | | } |
| | 162 | | #endregion |
| | 163 | |
|
| | 164 | | #region Internal Methods |
| | 165 | | internal override void ConstructTransaction(LightningMoney currentFeePerKw) |
| | 166 | | { |
| | 167 | | // Calculate base fee |
| 116 | 168 | | var outputWeight = CalculateOutputWeight(); |
| 116 | 169 | | var calculatedFee = (outputWeight + TransactionConstants.COMMITMENT_TRANSACTION_INPUT_WEIGHT) |
| 116 | 170 | | * currentFeePerKw.Satoshi / 1000L; |
| 116 | 171 | | if (CalculatedFee.Satoshi != calculatedFee) |
| | 172 | | { |
| 112 | 173 | | CalculatedFee.Satoshi = calculatedFee; |
| | 174 | | } |
| | 175 | |
|
| | 176 | | // Deduct base fee from the funder amount |
| 116 | 177 | | if (CalculatedFee > _toFunderAmount) |
| | 178 | | { |
| 4 | 179 | | _toFunderAmount = LightningMoney.Zero; |
| | 180 | | } |
| | 181 | | else |
| | 182 | | { |
| 112 | 183 | | _toFunderAmount -= CalculatedFee; |
| | 184 | | } |
| | 185 | |
|
| | 186 | | // Deduct anchor fee from the funder amount |
| 116 | 187 | | if (!_anchorAmount.IsZero && !_toFunderAmount.IsZero) |
| | 188 | | { |
| 40 | 189 | | _toFunderAmount -= _anchorAmount; |
| 40 | 190 | | _toFunderAmount -= _anchorAmount; |
| | 191 | | } |
| | 192 | |
|
| | 193 | | // Trim Local and Remote outputs |
| 116 | 194 | | if (_isChannelFunder) |
| | 195 | | { |
| 112 | 196 | | SetLocalAndRemoteAmounts(ToLocalOutput, ToRemoteOutput); |
| | 197 | | } |
| | 198 | | else |
| | 199 | | { |
| 4 | 200 | | SetLocalAndRemoteAmounts(ToRemoteOutput, ToLocalOutput); |
| | 201 | | } |
| | 202 | |
|
| | 203 | | // Trim HTLCs |
| 116 | 204 | | if (_mustTrimHtlcOutputs) |
| | 205 | | { |
| 0 | 206 | | var offeredHtlcWeight = _anchorAmount.IsZero |
| 0 | 207 | | ? WeightConstants.HTLC_TIMEOUT_WEIGHT_NO_ANCHORS |
| 0 | 208 | | : WeightConstants.HTLC_TIMEOUT_WEIGHT_ANCHORS; |
| 0 | 209 | | var offeredHtlcFee = offeredHtlcWeight * currentFeePerKw.Satoshi / 1000L; |
| 0 | 210 | | foreach (var offeredHtlcOutput in OfferedHtlcOutputs) |
| | 211 | | { |
| 0 | 212 | | var htlcAmount = offeredHtlcOutput.Amount - offeredHtlcFee; |
| 0 | 213 | | if (htlcAmount < _dustLimitAmount) |
| | 214 | | { |
| 0 | 215 | | RemoveOutput(offeredHtlcOutput); |
| | 216 | | } |
| | 217 | | } |
| | 218 | |
|
| 0 | 219 | | var receivedHtlcWeight = _anchorAmount.IsZero |
| 0 | 220 | | ? WeightConstants.HTLC_SUCCESS_WEIGHT_NO_ANCHORS |
| 0 | 221 | | : WeightConstants.HTLC_SUCCESS_WEIGHT_ANCHORS; |
| 0 | 222 | | var receivedHtlcFee = receivedHtlcWeight * currentFeePerKw.Satoshi / 1000L; |
| 0 | 223 | | foreach (var receivedHtlcOutput in ReceivedHtlcOutputs) |
| | 224 | | { |
| 0 | 225 | | var htlcAmount = receivedHtlcOutput.Amount - receivedHtlcFee; |
| 0 | 226 | | if (htlcAmount < _dustLimitAmount) |
| | 227 | | { |
| 0 | 228 | | RemoveOutput(receivedHtlcOutput); |
| | 229 | | } |
| | 230 | | } |
| | 231 | | } |
| | 232 | |
|
| | 233 | | // Anchors are always needed, except when one of the outputs is zero and there's no htlc output |
| 248 | 234 | | if (!_anchorAmount.IsZero && !Outputs.Any(o => o is BaseHtlcOutput)) |
| | 235 | | { |
| 20 | 236 | | if (ToLocalOutput.Amount.IsZero) |
| | 237 | | { |
| 4 | 238 | | RemoveOutput(LocalAnchorOutput); |
| | 239 | | } |
| | 240 | |
|
| 20 | 241 | | if (ToRemoteOutput.Amount.IsZero) |
| | 242 | | { |
| 4 | 243 | | RemoveOutput(RemoteAnchorOutput); |
| | 244 | | } |
| | 245 | | } |
| | 246 | |
|
| | 247 | | // Order Outputs |
| 116 | 248 | | AddOrderedOutputsToTransaction(); |
| 116 | 249 | | } |
| | 250 | |
|
| | 251 | | internal new void SignTransaction(params BitcoinSecret[] secrets) |
| | 252 | | { |
| 116 | 253 | | base.SignTransaction(secrets); |
| | 254 | |
|
| 116 | 255 | | SetTxIdAndIndexes(); |
| 116 | 256 | | } |
| | 257 | | #endregion |
| | 258 | |
|
| | 259 | | #region Private Methods |
| | 260 | | private void SetLocalAndRemoteAmounts(BaseOutput funderOutput, BaseOutput otherOutput) |
| | 261 | | { |
| 116 | 262 | | if (_toFunderAmount >= _dustLimitAmount) |
| | 263 | | { |
| 96 | 264 | | if (_toFunderAmount != funderOutput.Amount) |
| | 265 | | { |
| | 266 | | // Remove old output |
| 96 | 267 | | RemoveOutput(funderOutput); |
| | 268 | |
|
| | 269 | | // Set amount |
| 96 | 270 | | funderOutput.Amount = _toFunderAmount; |
| | 271 | |
|
| | 272 | | // Add new output |
| 96 | 273 | | AddOutput(funderOutput); |
| | 274 | | } |
| | 275 | | } |
| | 276 | | else |
| | 277 | | { |
| 20 | 278 | | RemoveOutput(funderOutput); |
| 20 | 279 | | funderOutput.Amount = LightningMoney.Zero; |
| | 280 | | } |
| | 281 | |
|
| 116 | 282 | | RemoveOutput(otherOutput); |
| 116 | 283 | | if (otherOutput.Amount >= _dustLimitAmount) |
| | 284 | | { |
| 112 | 285 | | AddOutput(otherOutput); |
| | 286 | | } |
| | 287 | | else |
| | 288 | | { |
| 4 | 289 | | otherOutput.Amount = LightningMoney.Zero; |
| | 290 | | } |
| 4 | 291 | | } |
| | 292 | |
|
| | 293 | | private void SetTxIdAndIndexes() |
| | 294 | | { |
| 116 | 295 | | ToRemoteOutput.TxId = TxId; |
| 116 | 296 | | ToRemoteOutput.Index = Outputs.IndexOf(ToRemoteOutput); |
| | 297 | |
|
| 116 | 298 | | ToLocalOutput.TxId = TxId; |
| 116 | 299 | | ToRemoteOutput.Index = Outputs.IndexOf(ToLocalOutput); |
| | 300 | |
|
| 424 | 301 | | foreach (var offeredHtlcOutput in OfferedHtlcOutputs) |
| | 302 | | { |
| 96 | 303 | | offeredHtlcOutput.TxId = TxId; |
| 96 | 304 | | offeredHtlcOutput.Index = Outputs.IndexOf(offeredHtlcOutput); |
| | 305 | | } |
| | 306 | |
|
| 440 | 307 | | foreach (var receivedHtlcOutput in ReceivedHtlcOutputs) |
| | 308 | | { |
| 104 | 309 | | receivedHtlcOutput.TxId = TxId; |
| 104 | 310 | | receivedHtlcOutput.Index = Outputs.IndexOf(receivedHtlcOutput); |
| | 311 | | } |
| | 312 | |
|
| 116 | 313 | | if (!_anchorAmount.IsZero) |
| | 314 | | { |
| 40 | 315 | | if (LocalAnchorOutput is not null) |
| | 316 | | { |
| 40 | 317 | | LocalAnchorOutput.TxId = TxId; |
| 40 | 318 | | LocalAnchorOutput.Index = Outputs.IndexOf(LocalAnchorOutput); |
| | 319 | | } |
| | 320 | |
|
| 40 | 321 | | if (RemoteAnchorOutput is not null) |
| | 322 | | { |
| 40 | 323 | | RemoteAnchorOutput.TxId = TxId; |
| 40 | 324 | | RemoteAnchorOutput.Index = Outputs.IndexOf(RemoteAnchorOutput); |
| | 325 | | } |
| | 326 | | } |
| 116 | 327 | | } |
| | 328 | | #endregion |
| | 329 | | } |