Skip to content

Commit c49f632

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 5238bdb commit c49f632

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
@@ -1668,6 +1668,10 @@ impl<SP: Deref> Channel<SP> where
16681668
ChannelPhase::UnfundedV2(chan) => chan.context.get_available_balances_for_scope(&chan.funding, fee_estimator),
16691669
}
16701670
}
1671+
1672+
pub fn minimum_depth(&self) -> Option<u32> {
1673+
self.context().minimum_depth(self.funding())
1674+
}
16711675
}
16721676

16731677
impl<SP: Deref> From<OutboundV1Channel<SP>> for Channel<SP>
@@ -3450,8 +3454,22 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
34503454
self.temporary_channel_id
34513455
}
34523456

3453-
pub fn minimum_depth(&self) -> Option<u32> {
3454-
self.minimum_depth
3457+
pub(super) fn minimum_depth(&self, funding: &FundingScope) -> Option<u32> {
3458+
let minimum_depth = self.minimum_depth?;
3459+
3460+
let is_coinbase = funding
3461+
.funding_transaction
3462+
.as_ref()
3463+
.map(|tx| tx.is_coinbase())
3464+
.unwrap_or(false);
3465+
3466+
// If the funding transaction is a coinbase transaction, we need 100 confirmations unless
3467+
// the channel is 0-conf.
3468+
if is_coinbase && minimum_depth > 0 && minimum_depth < COINBASE_MATURITY {
3469+
Some(COINBASE_MATURITY)
3470+
} else {
3471+
Some(minimum_depth)
3472+
}
34553473
}
34563474

34573475
/// Gets the "user_id" value passed into the construction of this channel. It has no special
@@ -8423,30 +8441,14 @@ impl<SP: Deref> FundedChannel<SP> where
84238441
}
84248442

84258443
fn check_funding_meets_minimum_depth(&self, funding: &FundingScope, height: u32) -> bool {
8426-
let minimum_depth = self.context.minimum_depth
8444+
let minimum_depth = self.context.minimum_depth(funding)
84278445
.expect("ChannelContext::minimum_depth should be set for FundedChannel");
84288446

84298447
// Zero-conf channels always meet the minimum depth.
84308448
if minimum_depth == 0 {
84318449
return true;
84328450
}
84338451

8434-
let is_coinbase = funding
8435-
.funding_transaction
8436-
.as_ref()
8437-
.map(|tx| tx.is_coinbase())
8438-
.unwrap_or(false);
8439-
8440-
let minimum_depth = {
8441-
// If the funding transaction is a coinbase transaction, we need to set the minimum
8442-
// depth to 100.
8443-
if is_coinbase && minimum_depth < COINBASE_MATURITY {
8444-
COINBASE_MATURITY
8445-
} else {
8446-
minimum_depth
8447-
}
8448-
};
8449-
84508452
if funding.funding_tx_confirmation_height == 0 {
84518453
return false;
84528454
}

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
@@ -3050,7 +3050,7 @@ macro_rules! locked_close_channel {
30503050
// into the map (which prevents the `PeerState` from being cleaned up) for channels that
30513051
// never even got confirmations (which would open us up to DoS attacks).
30523052
let update_id = $channel_context.get_latest_monitor_update_id();
3053-
if $channel_funding.get_funding_tx_confirmation_height().is_some() || $channel_context.minimum_depth() == Some(0) || update_id > 1 {
3053+
if $channel_funding.get_funding_tx_confirmation_height().is_some() || $channel_context.minimum_depth($channel_funding) == Some(0) || update_id > 1 {
30543054
let chan_id = $channel_context.channel_id();
30553055
$peer_state.closed_channel_monitor_update_ids.insert(chan_id, update_id);
30563056
}
@@ -7971,7 +7971,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
79717971

79727972
if accept_0conf {
79737973
// This should have been correctly configured by the call to Inbound(V1/V2)Channel::new.
7974-
debug_assert!(channel.context().minimum_depth().unwrap() == 0);
7974+
debug_assert!(channel.minimum_depth().unwrap() == 0);
79757975
} else if channel.funding().get_channel_type().requires_zero_conf() {
79767976
let send_msg_err_event = MessageSendEvent::HandleError {
79777977
node_id: channel.context().get_counterparty_node_id(),
@@ -8047,7 +8047,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
80478047
Some(funded_chan) => {
80488048
// This covers non-zero-conf inbound `Channel`s that we are currently monitoring, but those
80498049
// which have not yet had any confirmations on-chain.
8050-
if !funded_chan.funding.is_outbound() && funded_chan.context.minimum_depth().unwrap_or(1) != 0 &&
8050+
if !funded_chan.funding.is_outbound() && chan.minimum_depth().unwrap_or(1) != 0 &&
80518051
funded_chan.funding.get_funding_tx_confirmations(best_block_height) == 0
80528052
{
80538053
num_unfunded_channels += 1;
@@ -8060,7 +8060,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
80608060
}
80618061

80628062
// 0conf channels are not considered unfunded.
8063-
if chan.context().minimum_depth().unwrap_or(1) == 0 {
8063+
if chan.minimum_depth().unwrap_or(1) == 0 {
80648064
continue;
80658065
}
80668066

0 commit comments

Comments
 (0)