Skip to content

Commit 0071eff

Browse files
committed
Replace HTLCOutputInCommitment::is_dust with a flag
At the moment we do not let the interface decide whether a HTLC is dust or not.
1 parent 2f19710 commit 0071eff

File tree

7 files changed

+62
-37
lines changed

7 files changed

+62
-37
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5312,7 +5312,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
53125312
let transaction_output_index: Option<u32> = Readable::read(reader)?;
53135313

53145314
HTLCOutputInCommitment {
5315-
offered, amount_msat, cltv_expiry, payment_hash, transaction_output_index
5315+
offered, amount_msat, cltv_expiry, payment_hash, transaction_output_index, is_dust: transaction_output_index.is_none(),
53165316
}
53175317
}
53185318
}
@@ -5812,6 +5812,7 @@ mod tests {
58125812
cltv_expiry: 0,
58135813
payment_hash: preimage.1.clone(),
58145814
transaction_output_index: Some(idx as u32),
5815+
is_dust: false,
58155816
});
58165817
}
58175818
res
@@ -5965,6 +5966,7 @@ mod tests {
59655966
cltv_expiry: 2 << 16,
59665967
payment_hash: PaymentHash([1; 32]),
59675968
transaction_output_index: Some($idx as u32),
5969+
is_dust: true,
59685970
};
59695971
let redeem_script = if *$weight == WEIGHT_REVOKED_OUTPUT { chan_utils::get_revokeable_redeemscript(&RevocationKey::from_basepoint(&secp_ctx, &RevocationBasepoint::from(pubkey), &pubkey), 256, &DelayedPaymentKey::from_basepoint(&secp_ctx, &DelayedPaymentBasepoint::from(pubkey), &pubkey)) } else { chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, $opt_anchors, &HtlcKey::from_basepoint(&secp_ctx, &HtlcBasepoint::from(pubkey), &pubkey), &HtlcKey::from_basepoint(&secp_ctx, &HtlcBasepoint::from(pubkey), &pubkey), &RevocationKey::from_basepoint(&secp_ctx, &RevocationBasepoint::from(pubkey), &pubkey)) };
59705972
let sighash = hash_to_message!(&$sighash_parts.p2wsh_signature_hash($idx, &redeem_script, $amount, EcdsaSighashType::All).unwrap()[..]);

lightning/src/chain/onchaintx.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,7 @@ mod tests {
13221322
cltv_expiry: i as u32,
13231323
payment_hash: hash,
13241324
transaction_output_index: Some(i as u32),
1325+
is_dust: false,
13251326
}
13261327
);
13271328
}

lightning/src/chain/package.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,7 +1704,7 @@ mod tests {
17041704
let dumb_scalar = SecretKey::from_slice(&<Vec<u8>>::from_hex("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap();
17051705
let dumb_point = PublicKey::from_secret_key(&secp_ctx, &dumb_scalar);
17061706
let hash = PaymentHash([1; 32]);
1707-
let htlc = HTLCOutputInCommitment { offered: false, amount_msat: 1_000_000, cltv_expiry: 0, payment_hash: hash, transaction_output_index: None };
1707+
let htlc = HTLCOutputInCommitment { offered: false, amount_msat: 1_000_000, cltv_expiry: 0, payment_hash: hash, transaction_output_index: None, is_dust: true, };
17081708
let mut channel_parameters = ChannelTransactionParameters::test_dummy(0);
17091709
channel_parameters.channel_type_features =
17101710
ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies();
@@ -1723,7 +1723,7 @@ mod tests {
17231723
let dumb_scalar = SecretKey::from_slice(&<Vec<u8>>::from_hex("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap();
17241724
let dumb_point = PublicKey::from_secret_key(&secp_ctx, &dumb_scalar);
17251725
let hash = PaymentHash([1; 32]);
1726-
let htlc = HTLCOutputInCommitment { offered: true, amount_msat: $amt, cltv_expiry: $expiry, payment_hash: hash, transaction_output_index: None };
1726+
let htlc = HTLCOutputInCommitment { offered: true, amount_msat: $amt, cltv_expiry: $expiry, payment_hash: hash, transaction_output_index: None, is_dust: true };
17271727
let mut channel_parameters = ChannelTransactionParameters::test_dummy(0);
17281728
channel_parameters.channel_type_features = $features;
17291729
PackageSolvingData::CounterpartyReceivedHTLCOutput(
@@ -1742,7 +1742,7 @@ mod tests {
17421742
let dumb_point = PublicKey::from_secret_key(&secp_ctx, &dumb_scalar);
17431743
let hash = PaymentHash([1; 32]);
17441744
let preimage = PaymentPreimage([2;32]);
1745-
let htlc = HTLCOutputInCommitment { offered: false, amount_msat: $amt, cltv_expiry: 0, payment_hash: hash, transaction_output_index: None };
1745+
let htlc = HTLCOutputInCommitment { offered: false, amount_msat: $amt, cltv_expiry: 0, payment_hash: hash, transaction_output_index: None, is_dust: true };
17461746
let mut channel_parameters = ChannelTransactionParameters::test_dummy(0);
17471747
channel_parameters.channel_type_features = $features;
17481748
PackageSolvingData::CounterpartyOfferedHTLCOutput(
@@ -1765,6 +1765,7 @@ mod tests {
17651765
cltv_expiry: 420,
17661766
payment_hash: PaymentHash::from(preimage),
17671767
transaction_output_index: None,
1768+
is_dust: true,
17681769
};
17691770
let commitment_tx = HolderCommitmentTransaction::dummy(0, vec![htlc.clone()]);
17701771
let trusted_tx = commitment_tx.trust();
@@ -1800,6 +1801,7 @@ mod tests {
18001801
cltv_expiry: $cltv_expiry,
18011802
payment_hash: PaymentHash::from(PaymentPreimage([2;32])),
18021803
transaction_output_index: None,
1804+
is_dust: true,
18031805
};
18041806
let commitment_tx = HolderCommitmentTransaction::dummy(0, vec![htlc.clone()]);
18051807
let trusted_tx = commitment_tx.trust();

lightning/src/ln/chan_utils.rs

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,8 @@ pub struct HTLCOutputInCommitment {
636636
/// below the dust limit (in which case no output appears in the commitment transaction and the
637637
/// value is spent to additional transaction fees).
638638
pub transaction_output_index: Option<u32>,
639+
/// Whether the HTLC is dust - when set, the HTLC MUST be assigned a transaction output index
640+
pub is_dust: bool,
639641
}
640642

641643
impl HTLCOutputInCommitment {
@@ -654,32 +656,47 @@ impl HTLCOutputInCommitment {
654656
&& self.cltv_expiry == other.cltv_expiry
655657
&& self.payment_hash == other.payment_hash
656658
}
659+
}
657660

658-
pub(crate) fn is_dust(
659-
&self, feerate_per_kw: u32, broadcaster_dust_limit_sat: u64, features: &ChannelTypeFeatures,
660-
) -> bool {
661-
let htlc_tx_fee_sat = if features.supports_anchors_zero_fee_htlc_tx() {
662-
0
663-
} else {
664-
let htlc_tx_weight = if self.offered {
665-
htlc_timeout_tx_weight(features)
666-
} else {
667-
htlc_success_tx_weight(features)
668-
};
669-
// As required by the spec, round down
670-
feerate_per_kw as u64 * htlc_tx_weight / 1000
671-
};
672-
self.amount_msat / 1000 < broadcaster_dust_limit_sat + htlc_tx_fee_sat
661+
impl Writeable for HTLCOutputInCommitment {
662+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
663+
write_tlv_fields!(writer, {
664+
(0, self.offered, required),
665+
(2, self.amount_msat, required),
666+
(4, self.cltv_expiry, required),
667+
(6, self.payment_hash, required),
668+
(8, self.transaction_output_index, option),
669+
});
670+
Ok(())
673671
}
674672
}
675673

676-
impl_writeable_tlv_based!(HTLCOutputInCommitment, {
677-
(0, offered, required),
678-
(2, amount_msat, required),
679-
(4, cltv_expiry, required),
680-
(6, payment_hash, required),
681-
(8, transaction_output_index, option),
682-
});
674+
impl Readable for HTLCOutputInCommitment {
675+
fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
676+
let mut offered = RequiredWrapper(None);
677+
let mut amount_msat = RequiredWrapper(None);
678+
let mut cltv_expiry = RequiredWrapper(None);
679+
let mut payment_hash = RequiredWrapper(None);
680+
let mut transaction_output_index: Option<u32> = None;
681+
682+
read_tlv_fields!(reader, {
683+
(0, offered, required),
684+
(2, amount_msat, required),
685+
(4, cltv_expiry, required),
686+
(6, payment_hash, required),
687+
(8, transaction_output_index, option),
688+
});
689+
690+
Ok(Self {
691+
offered: offered.0.unwrap(),
692+
amount_msat: amount_msat.0.unwrap(),
693+
cltv_expiry: cltv_expiry.0.unwrap(),
694+
payment_hash: payment_hash.0.unwrap(),
695+
transaction_output_index,
696+
is_dust: transaction_output_index.is_none(),
697+
})
698+
}
699+
}
683700

684701
#[inline]
685702
#[rustfmt::skip]
@@ -2259,6 +2276,7 @@ mod tests {
22592276
cltv_expiry: 100,
22602277
payment_hash: PaymentHash([42; 32]),
22612278
transaction_output_index: None,
2279+
is_dust: true,
22622280
};
22632281

22642282
let offered_htlc = HTLCOutputInCommitment {
@@ -2267,6 +2285,7 @@ mod tests {
22672285
cltv_expiry: 100,
22682286
payment_hash: PaymentHash([43; 32]),
22692287
transaction_output_index: None,
2288+
is_dust: true,
22702289
};
22712290

22722291
// Generate broadcaster output and received and offered HTLC outputs, w/o anchors
@@ -2811,6 +2830,7 @@ mod tests {
28112830
cltv_expiry: 123,
28122831
payment_hash: PaymentHash([0xbb; 32]),
28132832
transaction_output_index: Some(0),
2833+
is_dust: false,
28142834
};
28152835

28162836
// Check amount sorting

lightning/src/ln/channel.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4582,20 +4582,21 @@ where
45824582
if local { "us" } else { "remote" }, if generated_by_local { "us" } else { "remote" }, feerate_per_kw);
45834583

45844584
macro_rules! get_htlc_in_commitment {
4585-
($htlc: expr, $offered: expr) => {
4585+
($htlc: expr, $offered: expr, $is_dust: expr) => {
45864586
HTLCOutputInCommitment {
45874587
offered: $offered,
45884588
amount_msat: $htlc.amount_msat,
45894589
cltv_expiry: $htlc.cltv_expiry,
45904590
payment_hash: $htlc.payment_hash,
4591-
transaction_output_index: None
4591+
transaction_output_index: None,
4592+
is_dust: $is_dust,
45924593
}
45934594
}
45944595
}
45954596

45964597
macro_rules! add_htlc_output {
4597-
($htlc: expr, $outbound: expr, $source: expr) => {
4598-
let htlc_in_tx = get_htlc_in_commitment!($htlc, $outbound == local);
4598+
($htlc: expr, $outbound: expr, $source: expr, $is_dust: expr) => {
4599+
let htlc_in_tx = get_htlc_in_commitment!($htlc, $outbound == local, $is_dust);
45994600
htlcs_included.push((htlc_in_tx, $source));
46004601
}
46014602
}
@@ -4606,7 +4607,8 @@ where
46064607
for htlc in self.pending_inbound_htlcs.iter() {
46074608
if htlc.state.included_in_commitment(generated_by_local) {
46084609
log_trace!(logger, " ...including inbound {} HTLC {} (hash {}) with value {}", htlc.state, htlc.htlc_id, htlc.payment_hash, htlc.amount_msat);
4609-
add_htlc_output!(htlc, false, None);
4610+
let is_dust = htlc.is_dust(local, feerate_per_kw, broadcaster_dust_limit_sat, funding.get_channel_type());
4611+
add_htlc_output!(htlc, false, None, is_dust);
46104612
} else {
46114613
log_trace!(logger, " ...not including inbound HTLC {} (hash {}) with value {} due to state ({})", htlc.htlc_id, htlc.payment_hash, htlc.amount_msat, htlc.state);
46124614
if let Some(preimage) = htlc.state.preimage() {
@@ -4622,7 +4624,8 @@ where
46224624
}
46234625
if htlc.state.included_in_commitment(generated_by_local) {
46244626
log_trace!(logger, " ...including outbound {} HTLC {} (hash {}) with value {}", htlc.state, htlc.htlc_id, htlc.payment_hash, htlc.amount_msat);
4625-
add_htlc_output!(htlc, true, Some(&htlc.source));
4627+
let is_dust = htlc.is_dust(local, feerate_per_kw, broadcaster_dust_limit_sat, funding.get_channel_type());
4628+
add_htlc_output!(htlc, true, Some(&htlc.source), is_dust);
46264629
} else {
46274630
log_trace!(logger, " ...not including outbound HTLC {} (hash {}) with value {} due to state ({})", htlc.htlc_id, htlc.payment_hash, htlc.amount_msat, htlc.state);
46284631
if htlc.state.preimage().is_some() {

lightning/src/ln/htlc_reserve_unit_tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,7 @@ pub fn test_fee_spike_violation_fails_htlc() {
866866
cltv_expiry: htlc_cltv,
867867
payment_hash,
868868
transaction_output_index: Some(1),
869+
is_dust: false,
869870
};
870871

871872
let commitment_number = INITIAL_COMMITMENT_NUMBER - 1;

lightning/src/sign/tx_builder.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,7 @@ impl TxBuilder for SpecTxBuilder {
8989
} else {
9090
remote_htlc_total_msat += htlc.amount_msat;
9191
}
92-
if htlc.is_dust(
93-
feerate_per_kw,
94-
broadcaster_dust_limit_satoshis,
95-
&channel_parameters.channel_type_features,
96-
) {
92+
if htlc.is_dust {
9793
log_trace!(
9894
logger,
9995
" ...trimming {} HTLC with value {}sat, hash {}, due to dust limit {}",

0 commit comments

Comments
 (0)