Skip to content

Commit 26fb470

Browse files
committed
Introduce new property to OutboundV1Channel
- This property keep track of if the OutboundV1Channel is accepted by the peer.
1 parent 74bc9e2 commit 26fb470

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

lightning/src/ln/channel.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,10 @@ pub(crate) const DISCONNECT_PEER_AWAITING_RESPONSE_TICKS: usize = 2;
633633
/// exceeding this age limit will be force-closed and purged from memory.
634634
pub(crate) const UNFUNDED_CHANNEL_AGE_LIMIT_TICKS: usize = 60;
635635

636+
/// The number of ticks that may elapse while we're waiting for an outbound channel to be accepted by peer.
637+
/// An unaccepted channel exceeding this age limit will be force-closed and purged from memory.
638+
pub(crate) const UNACCEPTED_CHANNEL_AGE_LIMIT_TICKS: usize = 2;
639+
636640
/// Number of blocks needed for an output from a coinbase transaction to be spendable.
637641
pub(crate) const COINBASE_MATURITY: u32 = 100;
638642

@@ -673,6 +677,34 @@ impl<'a, SP: Deref> ChannelPhase<SP> where
673677
}
674678
}
675679

680+
pub(super) struct UnacceptedChannelContext {
681+
/// A counter tracking how many ticks have elapsed since this unaccepted channel was
682+
/// created. If this unaccepted channel reaches peer has yet to respond after reaching
683+
/// `UNACCEPTED_CHANNEL_AGE_LIMIT_TICKS`, it will be force-closed and purged from memory.
684+
///
685+
/// This is so that we don't keep outbound request around which have not been accepted
686+
/// in a timely manner
687+
unaccepted_channel_age_ticks: usize,
688+
is_channel_accepted: bool,
689+
}
690+
691+
impl UnacceptedChannelContext {
692+
/// Determines whether we should force-close and purge this unfunded channel from memory due to it
693+
/// having reached the unfunded channel age limit.
694+
///
695+
/// This should be called on every [`super::channelmanager::ChannelManager::timer_tick_occurred`].
696+
pub fn should_expire_unaccepted_channel(&mut self) -> bool {
697+
if self.is_channel_accepted { return false; }
698+
self.unaccepted_channel_age_ticks += 1;
699+
self.unaccepted_channel_age_ticks >= UNACCEPTED_CHANNEL_AGE_LIMIT_TICKS
700+
}
701+
702+
/// Set channel status to accepted
703+
pub fn channel_accepted(&mut self) {
704+
self.is_channel_accepted = true;
705+
}
706+
}
707+
676708
/// Contains all state common to unfunded inbound/outbound channels.
677709
pub(super) struct UnfundedChannelContext {
678710
/// A counter tracking how many ticks have elapsed since this unfunded channel was
@@ -5948,6 +5980,7 @@ impl<SP: Deref> Channel<SP> where
59485980
pub(super) struct OutboundV1Channel<SP: Deref> where SP::Target: SignerProvider {
59495981
pub context: ChannelContext<SP>,
59505982
pub unfunded_context: UnfundedChannelContext,
5983+
pub unaccepted_context: UnacceptedChannelContext,
59515984
}
59525985

59535986
impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
@@ -6152,7 +6185,8 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
61526185

61536186
blocked_monitor_updates: Vec::new(),
61546187
},
6155-
unfunded_context: UnfundedChannelContext { unfunded_channel_age_ticks: 0 }
6188+
unfunded_context: UnfundedChannelContext { unfunded_channel_age_ticks: 0 },
6189+
unaccepted_context: UnacceptedChannelContext { unaccepted_channel_age_ticks: 0, is_channel_accepted: false }
61566190
})
61576191
}
61586192

0 commit comments

Comments
 (0)