Skip to content

Commit 63b2004

Browse files
committed
Account for coinbase tx in ChannelContext::minimum_depth
Now that ChannelContext::minimum_depth is not overridden with COINBASE_MATURITY when the funding transaction is the coinbase transaction, so do instead in the identically named method. Also, add a minimum_depth to Channel. The one on ChannelContext can become private once FudningScope doesn't need to be accessed directly from a ChannelManager macro. This fixes ChannelDetails showing an incorrect minimum depth when the coinbase transaction is used to funded the channel.
1 parent 8739bd4 commit 63b2004

File tree

3 files changed

+26
-24
lines changed

3 files changed

+26
-24
lines changed

lightning/src/ln/channel.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,6 +1702,10 @@ impl<SP: Deref> Channel<SP> where
17021702
ChannelPhase::UnfundedV2(chan) => chan.context.get_available_balances_for_scope(&chan.funding, fee_estimator),
17031703
}
17041704
}
1705+
1706+
pub fn minimum_depth(&self) -> Option<u32> {
1707+
self.context().minimum_depth(self.funding())
1708+
}
17051709
}
17061710

17071711
impl<SP: Deref> From<OutboundV1Channel<SP>> for Channel<SP>
@@ -3485,8 +3489,22 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
34853489
self.temporary_channel_id
34863490
}
34873491

3488-
pub fn minimum_depth(&self) -> Option<u32> {
3489-
self.minimum_depth
3492+
pub(super) fn minimum_depth(&self, funding: &FundingScope) -> Option<u32> {
3493+
let minimum_depth = self.minimum_depth?;
3494+
3495+
let is_coinbase = funding
3496+
.funding_transaction
3497+
.as_ref()
3498+
.map(|tx| tx.is_coinbase())
3499+
.unwrap_or(false);
3500+
3501+
// If the funding transaction is a coinbase transaction, we need 100 confirmations unless
3502+
// the channel is 0-conf.
3503+
if is_coinbase && minimum_depth > 0 && minimum_depth < COINBASE_MATURITY {
3504+
Some(COINBASE_MATURITY)
3505+
} else {
3506+
Some(minimum_depth)
3507+
}
34903508
}
34913509

34923510
/// Gets the "user_id" value passed into the construction of this channel. It has no special
@@ -8578,30 +8596,14 @@ impl<SP: Deref> FundedChannel<SP> where
85788596
}
85798597

85808598
fn check_funding_meets_minimum_depth(&self, funding: &FundingScope, height: u32) -> bool {
8581-
let minimum_depth = self.context.minimum_depth
8599+
let minimum_depth = self.context.minimum_depth(funding)
85828600
.expect("ChannelContext::minimum_depth should be set for FundedChannel");
85838601

85848602
// Zero-conf channels always meet the minimum depth.
85858603
if minimum_depth == 0 {
85868604
return true;
85878605
}
85888606

8589-
let is_coinbase = funding
8590-
.funding_transaction
8591-
.as_ref()
8592-
.map(|tx| tx.is_coinbase())
8593-
.unwrap_or(false);
8594-
8595-
let minimum_depth = {
8596-
// If the funding transaction is a coinbase transaction, we need to set the minimum
8597-
// depth to 100.
8598-
if is_coinbase && minimum_depth < COINBASE_MATURITY {
8599-
COINBASE_MATURITY
8600-
} else {
8601-
minimum_depth
8602-
}
8603-
};
8604-
86058607
if funding.funding_tx_confirmation_height == 0 {
86068608
return false;
86078609
}

lightning/src/ln/channel_state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ impl ChannelDetails {
531531
next_outbound_htlc_limit_msat: balance.next_outbound_htlc_limit_msat,
532532
next_outbound_htlc_minimum_msat: balance.next_outbound_htlc_minimum_msat,
533533
user_channel_id: context.get_user_id(),
534-
confirmations_required: context.minimum_depth(),
534+
confirmations_required: channel.minimum_depth(),
535535
confirmations: Some(funding.get_funding_tx_confirmations(best_block_height)),
536536
force_close_spend_delay: funding.get_counterparty_selected_contest_delay(),
537537
is_outbound: funding.is_outbound(),

lightning/src/ln/channelmanager.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3052,7 +3052,7 @@ macro_rules! locked_close_channel {
30523052
// into the map (which prevents the `PeerState` from being cleaned up) for channels that
30533053
// never even got confirmations (which would open us up to DoS attacks).
30543054
let update_id = $channel_context.get_latest_monitor_update_id();
3055-
if $channel_funding.get_funding_tx_confirmation_height().is_some() || $channel_context.minimum_depth() == Some(0) || update_id > 1 {
3055+
if $channel_funding.get_funding_tx_confirmation_height().is_some() || $channel_context.minimum_depth($channel_funding) == Some(0) || update_id > 1 {
30563056
let chan_id = $channel_context.channel_id();
30573057
$peer_state.closed_channel_monitor_update_ids.insert(chan_id, update_id);
30583058
}
@@ -7980,7 +7980,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
79807980

79817981
if accept_0conf {
79827982
// This should have been correctly configured by the call to Inbound(V1/V2)Channel::new.
7983-
debug_assert!(channel.context().minimum_depth().unwrap() == 0);
7983+
debug_assert!(channel.minimum_depth().unwrap() == 0);
79847984
} else if channel.funding().get_channel_type().requires_zero_conf() {
79857985
let send_msg_err_event = MessageSendEvent::HandleError {
79867986
node_id: channel.context().get_counterparty_node_id(),
@@ -8056,7 +8056,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
80568056
Some(funded_chan) => {
80578057
// This covers non-zero-conf inbound `Channel`s that we are currently monitoring, but those
80588058
// which have not yet had any confirmations on-chain.
8059-
if !funded_chan.funding.is_outbound() && funded_chan.context.minimum_depth().unwrap_or(1) != 0 &&
8059+
if !funded_chan.funding.is_outbound() && chan.minimum_depth().unwrap_or(1) != 0 &&
80608060
funded_chan.funding.get_funding_tx_confirmations(best_block_height) == 0
80618061
{
80628062
num_unfunded_channels += 1;
@@ -8069,7 +8069,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
80698069
}
80708070

80718071
// 0conf channels are not considered unfunded.
8072-
if chan.context().minimum_depth().unwrap_or(1) == 0 {
8072+
if chan.minimum_depth().unwrap_or(1) == 0 {
80738073
continue;
80748074
}
80758075

0 commit comments

Comments
 (0)