|
| 1 | +//! Defines the `TxBuilder` trait, and the `SpecTxBuilder` type |
| 2 | +#![allow(dead_code)] |
| 3 | +#![allow(unused_variables)] |
| 4 | + |
| 5 | +use bitcoin::secp256k1::{self, PublicKey, Secp256k1}; |
| 6 | + |
| 7 | +use crate::ln::chan_utils::{ |
| 8 | + self, ChannelTransactionParameters, CommitmentTransaction, HTLCOutputInCommitment, |
| 9 | + TxCreationKeys, |
| 10 | +}; |
| 11 | +use crate::ln::channel::CommitmentStats; |
| 12 | +use crate::ln::channelmanager::HTLCSource; |
| 13 | +use crate::prelude::*; |
| 14 | + |
| 15 | +/// A trait for types that can build commitment transactions, both for the holder, and the counterparty. |
| 16 | +pub(crate) trait TxBuilder { |
| 17 | + /// Build a commitment transaction, and populate the elements of `htlcs` with their output indices. |
| 18 | + fn build_commitment_transaction<'a>( |
| 19 | + &self, local: bool, commitment_number: u64, per_commitment_point: &PublicKey, |
| 20 | + channel_parameters: &ChannelTransactionParameters, secp_ctx: &Secp256k1<secp256k1::All>, |
| 21 | + channel_value_satoshis: u64, value_to_self_with_offset_msat: u64, |
| 22 | + htlcs_included: Vec<(HTLCOutputInCommitment, Option<&'a HTLCSource>)>, feerate_per_kw: u32, |
| 23 | + broadcaster_dust_limit_satoshis: u64, |
| 24 | + ) -> CommitmentStats<'a>; |
| 25 | +} |
| 26 | + |
| 27 | +/// A type that builds commitment transactions according to the Lightning Specification. |
| 28 | +#[derive(Clone, Debug, Default)] |
| 29 | +pub struct SpecTxBuilder {} |
| 30 | + |
| 31 | +impl TxBuilder for SpecTxBuilder { |
| 32 | + fn build_commitment_transaction<'a>( |
| 33 | + &self, local: bool, commitment_number: u64, per_commitment_point: &PublicKey, |
| 34 | + channel_parameters: &ChannelTransactionParameters, secp_ctx: &Secp256k1<secp256k1::All>, |
| 35 | + channel_value_satoshis: u64, value_to_self_with_offset_msat: u64, |
| 36 | + htlcs_included: Vec<(HTLCOutputInCommitment, Option<&'a HTLCSource>)>, feerate_per_kw: u32, |
| 37 | + broadcaster_dust_limit_satoshis: u64, |
| 38 | + ) -> CommitmentStats<'a> { |
| 39 | + let mut local_htlc_total_msat = 0; |
| 40 | + let mut remote_htlc_total_msat = 0; |
| 41 | + let mut included_dust_htlcs = Vec::new(); |
| 42 | + let mut included_non_dust_htlcs = Vec::with_capacity(htlcs_included.len()); |
| 43 | + let mut value_to_self_msat = value_to_self_with_offset_msat; |
| 44 | + |
| 45 | + let params = if local { |
| 46 | + channel_parameters.as_holder_broadcastable() |
| 47 | + } else { |
| 48 | + channel_parameters.as_counterparty_broadcastable() |
| 49 | + }; |
| 50 | + |
| 51 | + // Sort dust vs non-dust HTLCs |
| 52 | + for (htlc, source_opt) in htlcs_included { |
| 53 | + if htlc.offered { |
| 54 | + if local { |
| 55 | + local_htlc_total_msat += htlc.amount_msat; |
| 56 | + } else { |
| 57 | + remote_htlc_total_msat += htlc.amount_msat |
| 58 | + } |
| 59 | + let htlc_tx_fee = |
| 60 | + if params.channel_type_features().supports_anchors_zero_fee_htlc_tx() { |
| 61 | + 0 |
| 62 | + } else { |
| 63 | + feerate_per_kw as u64 |
| 64 | + * chan_utils::htlc_timeout_tx_weight(params.channel_type_features()) |
| 65 | + / 1000 |
| 66 | + }; |
| 67 | + if htlc.amount_msat / 1000 >= broadcaster_dust_limit_satoshis + htlc_tx_fee { |
| 68 | + // log_trace!(logger, " ...including {} {} HTLC {} (hash {}) with value {}", if $outbound { "outbound" } else { "inbound" }, $state_name, $htlc.htlc_id, &$htlc.payment_hash, $htlc.amount_msat); |
| 69 | + included_non_dust_htlcs.push((htlc, source_opt)); |
| 70 | + } else { |
| 71 | + // log_trace!(logger, " ...including {} {} dust HTLC {} (hash {}) with value {} due to dust limit", if $outbound { "outbound" } else { "inbound" }, $state_name, $htlc.htlc_id, &$htlc.payment_hash, $htlc.amount_msat); |
| 72 | + included_dust_htlcs.push((htlc, source_opt)); |
| 73 | + } |
| 74 | + } else { |
| 75 | + if local { |
| 76 | + remote_htlc_total_msat += htlc.amount_msat |
| 77 | + } else { |
| 78 | + local_htlc_total_msat += htlc.amount_msat; |
| 79 | + } |
| 80 | + let htlc_tx_fee = |
| 81 | + if params.channel_type_features().supports_anchors_zero_fee_htlc_tx() { |
| 82 | + 0 |
| 83 | + } else { |
| 84 | + feerate_per_kw as u64 |
| 85 | + * chan_utils::htlc_success_tx_weight(params.channel_type_features()) |
| 86 | + / 1000 |
| 87 | + }; |
| 88 | + if htlc.amount_msat / 1000 >= broadcaster_dust_limit_satoshis + htlc_tx_fee { |
| 89 | + // log_trace!(logger, " ...including {} {} HTLC {} (hash {}) with value {}", if $outbound { "outbound" } else { "inbound" }, $state_name, $htlc.htlc_id, &$htlc.payment_hash, $htlc.amount_msat); |
| 90 | + included_non_dust_htlcs.push((htlc, source_opt)); |
| 91 | + } else { |
| 92 | + // log_trace!(logger, " ...including {} {} dust HTLC {} (hash {}) with value {}", if $outbound { "outbound" } else { "inbound" }, $state_name, $htlc.htlc_id, &$htlc.payment_hash, $htlc.amount_msat); |
| 93 | + included_dust_htlcs.push((htlc, source_opt)); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // Subtract the HTLC amounts from the to_self, to_remote balances |
| 99 | + // Note that in case they have several just-awaiting-last-RAA fulfills in-progress (ie |
| 100 | + // AwaitingRemoteRevokeToRemove or AwaitingRemovedRemoteRevoke) we may have allowed them to |
| 101 | + // "violate" their reserve value by counting those against it. Thus we do checked subtraction, |
| 102 | + // as otherwise we can overflow. |
| 103 | + let mut value_to_remote_msat = |
| 104 | + u64::checked_sub(channel_value_satoshis * 1000, value_to_self_msat).unwrap(); |
| 105 | + value_to_self_msat = u64::checked_sub(value_to_self_msat, local_htlc_total_msat).unwrap(); |
| 106 | + value_to_remote_msat = |
| 107 | + u64::checked_sub(value_to_remote_msat, remote_htlc_total_msat).unwrap(); |
| 108 | + |
| 109 | + // Subtract the fees, and the anchor amounts from the channel funder |
| 110 | + let total_fee_sat = chan_utils::commit_tx_fee_sat( |
| 111 | + feerate_per_kw, |
| 112 | + included_non_dust_htlcs.len(), |
| 113 | + params.channel_type_features(), |
| 114 | + ); |
| 115 | + let anchors_val = if params.channel_type_features().supports_anchors_zero_fee_htlc_tx() { |
| 116 | + crate::ln::channel::ANCHOR_OUTPUT_VALUE_SATOSHI * 2 |
| 117 | + } else { |
| 118 | + 0 |
| 119 | + }; |
| 120 | + let (value_to_self, value_to_remote) = if channel_parameters.is_outbound_from_holder { |
| 121 | + ( |
| 122 | + (value_to_self_msat / 1000) |
| 123 | + .saturating_sub(anchors_val) |
| 124 | + .saturating_sub(total_fee_sat), |
| 125 | + value_to_remote_msat / 1000, |
| 126 | + ) |
| 127 | + } else { |
| 128 | + ( |
| 129 | + value_to_self_msat / 1000, |
| 130 | + (value_to_remote_msat / 1000) |
| 131 | + .saturating_sub(anchors_val) |
| 132 | + .saturating_sub(total_fee_sat), |
| 133 | + ) |
| 134 | + }; |
| 135 | + let mut value_to_a = if local { value_to_self } else { value_to_remote }; |
| 136 | + let mut value_to_b = if local { value_to_remote } else { value_to_self }; |
| 137 | + |
| 138 | + // Trim the to_local, to_remote outputs if any are below the dust limit |
| 139 | + if value_to_a >= broadcaster_dust_limit_satoshis { |
| 140 | + // log_trace!(logger, " ...including {} output with value {}", if local { "to_local" } else { "to_remote" }, value_to_a); |
| 141 | + } else { |
| 142 | + value_to_a = 0; |
| 143 | + } |
| 144 | + if value_to_b >= broadcaster_dust_limit_satoshis { |
| 145 | + // log_trace!(logger, " ...including {} output with value {}", if local { "to_remote" } else { "to_local" }, value_to_b); |
| 146 | + } else { |
| 147 | + value_to_b = 0; |
| 148 | + } |
| 149 | + |
| 150 | + let (funding_pubkey_a, funding_pubkey_b) = ( |
| 151 | + params.broadcaster_pubkeys().funding_pubkey, |
| 152 | + params.countersignatory_pubkeys().funding_pubkey, |
| 153 | + ); |
| 154 | + let keys = TxCreationKeys::from_channel_static_keys( |
| 155 | + per_commitment_point, |
| 156 | + params.broadcaster_pubkeys(), |
| 157 | + params.countersignatory_pubkeys(), |
| 158 | + &secp_ctx, |
| 159 | + ); |
| 160 | + |
| 161 | + let tx = CommitmentTransaction::new_with_auxiliary_htlc_data( |
| 162 | + commitment_number, |
| 163 | + value_to_a, |
| 164 | + value_to_b, |
| 165 | + funding_pubkey_a, |
| 166 | + funding_pubkey_b, |
| 167 | + keys, |
| 168 | + feerate_per_kw, |
| 169 | + &mut included_non_dust_htlcs, |
| 170 | + ¶ms, |
| 171 | + ); |
| 172 | + |
| 173 | + // Sort the non-dust HTLCs by their output index, then append the dust htlcs |
| 174 | + let mut htlcs_included = included_non_dust_htlcs; |
| 175 | + // The unwrap is safe, because all non-dust HTLCs have been assigned an output index |
| 176 | + htlcs_included.sort_unstable_by_key(|h| h.0.transaction_output_index.unwrap()); |
| 177 | + htlcs_included.append(&mut included_dust_htlcs); |
| 178 | + |
| 179 | + todo!(); |
| 180 | + } |
| 181 | +} |
0 commit comments