Skip to content

Commit

Permalink
first pass
Browse files Browse the repository at this point in the history
  • Loading branch information
tankyleo committed Dec 19, 2024
1 parent d414ba9 commit 2e32578
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions lightning/src/ln/chan_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,17 @@ pub const HTLC_SUCCESS_INPUT_ANCHOR_WITNESS_WEIGHT: u64 = 327;
pub fn htlc_success_tx_weight(channel_type_features: &ChannelTypeFeatures) -> u64 {
const HTLC_SUCCESS_TX_WEIGHT: u64 = 703;
const HTLC_SUCCESS_ANCHOR_TX_WEIGHT: u64 = 706;
if channel_type_features.supports_anchors_zero_fee_htlc_tx() { HTLC_SUCCESS_ANCHOR_TX_WEIGHT } else { HTLC_SUCCESS_TX_WEIGHT }
assert!(!channel_type_features.supports_anchors_zero_fee_htlc_tx());
if channel_type_features.supports_anchors_nonzero_fee_htlc_tx() { HTLC_SUCCESS_ANCHOR_TX_WEIGHT } else { HTLC_SUCCESS_TX_WEIGHT }
}

/// Gets the weight for an HTLC-Timeout transaction.
#[inline]
pub fn htlc_timeout_tx_weight(channel_type_features: &ChannelTypeFeatures) -> u64 {
const HTLC_TIMEOUT_TX_WEIGHT: u64 = 663;
const HTLC_TIMEOUT_ANCHOR_TX_WEIGHT: u64 = 666;
if channel_type_features.supports_anchors_zero_fee_htlc_tx() { HTLC_TIMEOUT_ANCHOR_TX_WEIGHT } else { HTLC_TIMEOUT_TX_WEIGHT }
assert!(!channel_type_features.supports_anchors_zero_fee_htlc_tx());
if channel_type_features.supports_anchors_nonzero_fee_htlc_tx() { HTLC_TIMEOUT_ANCHOR_TX_WEIGHT } else { HTLC_TIMEOUT_TX_WEIGHT }
}

/// Describes the type of HTLC claim as determined by analyzing the witness.
Expand Down Expand Up @@ -200,7 +202,7 @@ pub(crate) fn per_outbound_htlc_counterparty_commit_tx_fee_msat(feerate_per_kw:
// Note that we need to divide before multiplying to round properly,
// since the lowest denomination of bitcoin on-chain is the satoshi.
let commitment_tx_fee = COMMITMENT_TX_WEIGHT_PER_HTLC * feerate_per_kw as u64 / 1000 * 1000;
if channel_type_features.supports_anchors_zero_fee_htlc_tx() {
if channel_type_features.supports_anchors_nonzero_fee_htlc_tx() {
commitment_tx_fee + htlc_success_tx_weight(channel_type_features) * feerate_per_kw as u64 / 1000
} else {
commitment_tx_fee
Expand Down Expand Up @@ -737,14 +739,15 @@ pub(crate) fn build_htlc_input(commitment_txid: &Txid, htlc: &HTLCOutputInCommit
pub(crate) fn build_htlc_output(
feerate_per_kw: u32, contest_delay: u16, htlc: &HTLCOutputInCommitment, channel_type_features: &ChannelTypeFeatures, broadcaster_delayed_payment_key: &DelayedPaymentKey, revocation_key: &RevocationKey
) -> TxOut {
let weight = if htlc.offered {
htlc_timeout_tx_weight(channel_type_features)
} else {
htlc_success_tx_weight(channel_type_features)
};
let output_value = if channel_type_features.supports_anchors_zero_fee_htlc_tx() && !channel_type_features.supports_anchors_nonzero_fee_htlc_tx() {
let output_value = if channel_type_features.supports_anchors_zero_fee_htlc_tx() {
assert!(!channel_type_features.supports_anchors_nonzero_fee_htlc_tx());
htlc.to_bitcoin_amount()
} else {
let weight = if htlc.offered {
htlc_timeout_tx_weight(channel_type_features)
} else {
htlc_success_tx_weight(channel_type_features)
};
let total_fee = Amount::from_sat(feerate_per_kw as u64 * weight / 1000);
htlc.to_bitcoin_amount() - total_fee
};
Expand Down

0 comments on commit 2e32578

Please sign in to comment.