Skip to content

Commit 6e87fb3

Browse files
committed
Indicate source of balances
Introduce the `BalanceSource` enum to differentiate between force-close, coop-close, and HTLCs in `Balance::ClaimableAwaitingConfirmations`.
1 parent 109553e commit 6e87fb3

File tree

2 files changed

+79
-6
lines changed

2 files changed

+79
-6
lines changed

lightning/src/chain/channelmonitor.rs

+23
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,19 @@ impl_writeable_tlv_based_enum_upgradable!(ChannelMonitorUpdateStep,
608608
},
609609
);
610610

611+
/// Indicates whether the balance is derived from a cooperative close, a force-close
612+
/// (for holder or counterparty), or whether it is for an HTLC.
613+
#[derive(Clone, Debug, PartialEq, Eq)]
614+
#[cfg_attr(test, derive(PartialOrd, Ord))]
615+
pub enum BalanceSource {
616+
/// The channel was force closed.
617+
ForceClosed,
618+
/// The channel was cooperatively closed.
619+
CoopClose,
620+
/// This balance is the result of an HTLC.
621+
Htlc,
622+
}
623+
611624
/// Details about the balance(s) available for spending once the channel appears on chain.
612625
///
613626
/// See [`ChannelMonitor::get_claimable_balances`] for more details on when these will or will not
@@ -673,6 +686,8 @@ pub enum Balance {
673686
/// The height at which an [`Event::SpendableOutputs`] event will be generated for this
674687
/// amount.
675688
confirmation_height: u32,
689+
/// Whether this balance is a result of cooperative close, a force-close, or an HTLC.
690+
source: BalanceSource,
676691
},
677692
/// The channel has been closed, and the given balance should be ours but awaiting spending
678693
/// transaction confirmation. If the spending transaction does not confirm in time, it is
@@ -2102,6 +2117,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
21022117
return Some(Balance::ClaimableAwaitingConfirmations {
21032118
amount_satoshis: htlc.amount_msat / 1000,
21042119
confirmation_height: conf_thresh,
2120+
source: BalanceSource::Htlc,
21052121
});
21062122
} else if htlc_resolved.is_some() && !htlc_output_spend_pending {
21072123
// Funding transaction spends should be fully confirmed by the time any
@@ -2149,6 +2165,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
21492165
return Some(Balance::ClaimableAwaitingConfirmations {
21502166
amount_satoshis: htlc.amount_msat / 1000,
21512167
confirmation_height: conf_thresh,
2168+
source: BalanceSource::Htlc,
21522169
});
21532170
} else {
21542171
let outbound_payment = match source {
@@ -2177,6 +2194,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
21772194
return Some(Balance::ClaimableAwaitingConfirmations {
21782195
amount_satoshis: htlc.amount_msat / 1000,
21792196
confirmation_height: conf_thresh,
2197+
source: BalanceSource::Htlc,
21802198
});
21812199
} else {
21822200
return Some(Balance::ContentiousClaimable {
@@ -2264,6 +2282,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
22642282
res.push(Balance::ClaimableAwaitingConfirmations {
22652283
amount_satoshis: value.to_sat(),
22662284
confirmation_height: conf_thresh,
2285+
source: BalanceSource::ForceClosed,
22672286
});
22682287
} else {
22692288
// If a counterparty commitment transaction is awaiting confirmation, we
@@ -2287,6 +2306,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
22872306
res.push(Balance::ClaimableAwaitingConfirmations {
22882307
amount_satoshis: output.value.to_sat(),
22892308
confirmation_height: event.confirmation_threshold(),
2309+
source: BalanceSource::ForceClosed,
22902310
});
22912311
if let Some(confirmed_to_self_idx) = confirmed_counterparty_output.map(|(idx, _)| idx) {
22922312
if event.transaction.as_ref().map(|tx|
@@ -2319,6 +2339,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
23192339
res.push(Balance::ClaimableAwaitingConfirmations {
23202340
amount_satoshis: us.current_holder_commitment_tx.to_self_value_sat,
23212341
confirmation_height: conf_thresh,
2342+
source: BalanceSource::ForceClosed,
23222343
});
23232344
}
23242345
found_commitment_tx = true;
@@ -2329,6 +2350,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
23292350
res.push(Balance::ClaimableAwaitingConfirmations {
23302351
amount_satoshis: prev_commitment.to_self_value_sat,
23312352
confirmation_height: conf_thresh,
2353+
source: BalanceSource::ForceClosed,
23322354
});
23332355
}
23342356
found_commitment_tx = true;
@@ -2342,6 +2364,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
23422364
res.push(Balance::ClaimableAwaitingConfirmations {
23432365
amount_satoshis: us.current_holder_commitment_tx.to_self_value_sat,
23442366
confirmation_height: conf_thresh,
2367+
source: BalanceSource::CoopClose,
23452368
});
23462369
}
23472370
}

0 commit comments

Comments
 (0)