Skip to content

Commit ed54b32

Browse files
committed
fixup: Use the weight of the single input-output pair, not the full tx
1 parent c222c41 commit ed54b32

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

lightning/src/events/bump_transaction/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -970,10 +970,10 @@ where
970970
let expected_signed_tx_weight = unsigned_tx_weight + total_satisfaction_weight;
971971
if expected_signed_tx_weight >= max_weight {
972972
let extra_weight = expected_signed_tx_weight - max_weight;
973-
let htlcs_to_remove = (extra_weight / chan_utils::htlc_timeout_tx_weight(channel_type)
974-
// If we remove extra_weight / timeout_weight + 1 we sometimes still land above max_weight
975-
+ 2) as usize;
976-
batch_size = batch_size.checked_sub(htlcs_to_remove).ok_or(())?;
973+
let htlcs_to_remove = extra_weight
974+
/ chan_utils::htlc_timeout_input_output_pair_weight(channel_type)
975+
+ 1;
976+
batch_size = batch_size.checked_sub(htlcs_to_remove as usize).ok_or(())?;
977977
continue;
978978
}
979979
broadcasted_htlcs += batch_size;

lightning/src/ln/chan_utils.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//! largely of interest for those implementing the traits on [`crate::sign`] by hand.
1212
1313
use bitcoin::amount::Amount;
14+
use bitcoin::constants::WITNESS_SCALE_FACTOR;
1415
use bitcoin::opcodes;
1516
use bitcoin::script::{Builder, Script, ScriptBuf};
1617
use bitcoin::sighash;
@@ -96,7 +97,7 @@ pub const P2A_ANCHOR_INPUT_WITNESS_WEIGHT: u64 = 1;
9697
pub const P2A_MAX_VALUE: u64 = 240;
9798

9899
/// The maximum weight of a TRUC transaction.
99-
pub const TRUC_MAX_WEIGHT: u64 = 10_000 * bitcoin::constants::WITNESS_SCALE_FACTOR as u64;
100+
pub const TRUC_MAX_WEIGHT: u64 = 10_000 * WITNESS_SCALE_FACTOR as u64;
100101

101102
/// The upper bound weight of an HTLC timeout input from a commitment transaction with keyed anchor outputs.
102103
pub const HTLC_TIMEOUT_INPUT_KEYED_ANCHOR_WITNESS_WEIGHT: u64 = 288;
@@ -137,6 +138,15 @@ pub fn htlc_success_tx_weight(channel_type_features: &ChannelTypeFeatures) -> u6
137138
if channel_type_features.supports_anchors_zero_fee_htlc_tx() { HTLC_SUCCESS_ANCHOR_TX_WEIGHT } else { HTLC_SUCCESS_TX_WEIGHT }
138139
}
139140

141+
/// Gets the weight of a single input-output pair in HTLC-success transactions
142+
pub fn htlc_success_input_output_pair_weight(channel_type_features: &ChannelTypeFeatures) -> u64 {
143+
const TXIN_WEIGHT: u64 = 41 * WITNESS_SCALE_FACTOR as u64;
144+
const TXOUT_WEIGHT: u64 = 43 * WITNESS_SCALE_FACTOR as u64;
145+
let witness_weight =
146+
if channel_type_features.supports_anchors_zero_fee_htlc_tx() { 327 } else { 324 };
147+
TXIN_WEIGHT + TXOUT_WEIGHT + witness_weight
148+
}
149+
140150
/// Gets the weight for an HTLC-Timeout transaction.
141151
#[inline]
142152
#[rustfmt::skip]
@@ -146,6 +156,15 @@ pub fn htlc_timeout_tx_weight(channel_type_features: &ChannelTypeFeatures) -> u6
146156
if channel_type_features.supports_anchors_zero_fee_htlc_tx() { HTLC_TIMEOUT_ANCHOR_TX_WEIGHT } else { HTLC_TIMEOUT_TX_WEIGHT }
147157
}
148158

159+
/// Gets the weight of a single input-output pair in HTLC-timeout transactions
160+
pub fn htlc_timeout_input_output_pair_weight(channel_type_features: &ChannelTypeFeatures) -> u64 {
161+
const TXIN_WEIGHT: u64 = 41 * WITNESS_SCALE_FACTOR as u64;
162+
const TXOUT_WEIGHT: u64 = 43 * WITNESS_SCALE_FACTOR as u64;
163+
let witness_weight =
164+
if channel_type_features.supports_anchors_zero_fee_htlc_tx() { 288 } else { 285 };
165+
TXIN_WEIGHT + TXOUT_WEIGHT + witness_weight
166+
}
167+
149168
/// Describes the type of HTLC claim as determined by analyzing the witness.
150169
#[derive(PartialEq, Eq)]
151170
pub enum HTLCClaim {

lightning/src/ln/zero_fee_commitment_tests.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,10 @@ fn test_htlc_claim_chunking() {
163163
check_spends!(htlc_claims[0], node_1_commit_tx[0], coinbase_tx);
164164
check_spends!(htlc_claims[1], node_1_commit_tx[0], coinbase_tx);
165165

166-
assert_eq!(htlc_claims[0].input.len(), 60);
167-
assert_eq!(htlc_claims[0].output.len(), 60);
168-
assert_eq!(htlc_claims[1].input.len(), 17);
169-
assert_eq!(htlc_claims[1].output.len(), 17);
166+
assert_eq!(htlc_claims[0].input.len(), 59);
167+
assert_eq!(htlc_claims[0].output.len(), 59);
168+
assert_eq!(htlc_claims[1].input.len(), 18);
169+
assert_eq!(htlc_claims[1].output.len(), 18);
170170

171171
check_closed_broadcast!(nodes[0], true);
172172
check_added_monitors!(nodes[0], 1);
@@ -205,8 +205,8 @@ fn test_htlc_claim_chunking() {
205205
let fresh_htlc_claims = nodes[1].tx_broadcaster.txn_broadcast();
206206
assert_eq!(fresh_htlc_claims.len(), 1);
207207
check_spends!(fresh_htlc_claims[0], node_1_commit_tx[0], htlc_claims[0]);
208-
assert_eq!(fresh_htlc_claims[0].input.len(), 17);
209-
assert_eq!(fresh_htlc_claims[0].output.len(), 17);
208+
assert_eq!(fresh_htlc_claims[0].input.len(), 18);
209+
assert_eq!(fresh_htlc_claims[0].output.len(), 18);
210210

211211
// Assert when we handled the second `BumpTransaction::HTLCResolution` event, we
212212
// assigned the same UTXO id to that set of HTLCs

0 commit comments

Comments
 (0)