Skip to content

Commit 728192e

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 8b729b6 commit 728192e

File tree

2 files changed

+81
-6
lines changed

2 files changed

+81
-6
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,21 @@ 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 by the holder.
617+
HolderForceClosed,
618+
/// The channel was force closed by the counterparty.
619+
CounterpartyForceClosed,
620+
/// The channel was cooperatively closed.
621+
CoopClose,
622+
/// This balance is the result of an HTLC.
623+
Htlc,
624+
}
625+
611626
/// Details about the balance(s) available for spending once the channel appears on chain.
612627
///
613628
/// See [`ChannelMonitor::get_claimable_balances`] for more details on when these will or will not
@@ -675,6 +690,8 @@ pub enum Balance {
675690
/// The height at which an [`Event::SpendableOutputs`] event will be generated for this
676691
/// amount.
677692
confirmation_height: u32,
693+
/// Whether this balance is a result of cooperative close, a force-close, or an HTLC.
694+
source: BalanceSource,
678695
},
679696
/// The channel has been closed, and the given balance should be ours but awaiting spending
680697
/// transaction confirmation. If the spending transaction does not confirm in time, it is
@@ -2110,6 +2127,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
21102127
return Some(Balance::ClaimableAwaitingConfirmations {
21112128
amount_satoshis: htlc.amount_msat / 1000,
21122129
confirmation_height: conf_thresh,
2130+
source: BalanceSource::Htlc,
21132131
});
21142132
} else if htlc_resolved.is_some() && !htlc_output_spend_pending {
21152133
// Funding transaction spends should be fully confirmed by the time any
@@ -2157,6 +2175,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
21572175
return Some(Balance::ClaimableAwaitingConfirmations {
21582176
amount_satoshis: htlc.amount_msat / 1000,
21592177
confirmation_height: conf_thresh,
2178+
source: BalanceSource::Htlc,
21602179
});
21612180
} else {
21622181
let outbound_payment = match source {
@@ -2185,6 +2204,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
21852204
return Some(Balance::ClaimableAwaitingConfirmations {
21862205
amount_satoshis: htlc.amount_msat / 1000,
21872206
confirmation_height: conf_thresh,
2207+
source: BalanceSource::Htlc,
21882208
});
21892209
} else {
21902210
return Some(Balance::ContentiousClaimable {
@@ -2272,6 +2292,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
22722292
res.push(Balance::ClaimableAwaitingConfirmations {
22732293
amount_satoshis: value.to_sat(),
22742294
confirmation_height: conf_thresh,
2295+
source: BalanceSource::CounterpartyForceClosed,
22752296
});
22762297
} else {
22772298
// If a counterparty commitment transaction is awaiting confirmation, we
@@ -2295,6 +2316,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
22952316
res.push(Balance::ClaimableAwaitingConfirmations {
22962317
amount_satoshis: output.value.to_sat(),
22972318
confirmation_height: event.confirmation_threshold(),
2319+
source: BalanceSource::CounterpartyForceClosed,
22982320
});
22992321
if let Some(confirmed_to_self_idx) = confirmed_counterparty_output.map(|(idx, _)| idx) {
23002322
if event.transaction.as_ref().map(|tx|
@@ -2327,6 +2349,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
23272349
res.push(Balance::ClaimableAwaitingConfirmations {
23282350
amount_satoshis: us.current_holder_commitment_tx.to_self_value_sat,
23292351
confirmation_height: conf_thresh,
2352+
source: BalanceSource::HolderForceClosed,
23302353
});
23312354
}
23322355
found_commitment_tx = true;
@@ -2337,6 +2360,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
23372360
res.push(Balance::ClaimableAwaitingConfirmations {
23382361
amount_satoshis: prev_commitment.to_self_value_sat,
23392362
confirmation_height: conf_thresh,
2363+
source: BalanceSource::HolderForceClosed,
23402364
});
23412365
}
23422366
found_commitment_tx = true;
@@ -2350,6 +2374,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
23502374
res.push(Balance::ClaimableAwaitingConfirmations {
23512375
amount_satoshis: us.current_holder_commitment_tx.to_self_value_sat,
23522376
confirmation_height: conf_thresh,
2377+
source: BalanceSource::CoopClose,
23532378
});
23542379
}
23552380
}

0 commit comments

Comments
 (0)