Skip to content

Commit 0ef9416

Browse files
committed
Introduce new property to OutboundV1Channel
- This keeps track of the timer ticks passed since the peer is disconnected corresponding to this OutboundV1Channel
1 parent 0c67753 commit 0ef9416

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

lightning/src/ln/channel.rs

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

882+
/// The number of ticks that may elapse while we're waiting for a disconnected peer to reconnect,
883+
/// before we try to close the associated outbound channel with them.
884+
pub(crate) const DISCONNECTED_UNCONFIRMED_CHANNEL_AGE_LIMIT_TICKS: usize = 2;
885+
882886
/// Number of blocks needed for an output from a coinbase transaction to be spendable.
883887
pub(crate) const COINBASE_MATURITY: u32 = 100;
884888

@@ -919,6 +923,39 @@ impl<'a, SP: Deref> ChannelPhase<SP> where
919923
}
920924
}
921925

926+
pub(super) struct UnconfirmedChannelContext {
927+
/// A counter tracking how many ticks have elapsed since this unaccepted channel was
928+
/// created. If this unaccepted channel reaches peer has yet to respond after reaching
929+
/// `UNACCEPTED_CHANNEL_AGE_LIMIT_TICKS`, it will be force-closed and purged from memory.
930+
///
931+
/// This is so that we don't keep outbound request around which have not been accepted
932+
/// in a timely manner
933+
///
934+
/// A counter tracking how many ticks have elapsed since this the peer associated to this
935+
/// unconfimed outbound channel has been disconnected. If this unconfirmed channel associated
936+
/// peer is still disconnected after reaching `DISCONNECTED_UNCONFIRMED_CHANNEL_AGE_LIMIT_TICKS`,
937+
/// it will be force-closed and purged from memory.
938+
///
939+
/// This is so that we don't keep the outbound request around for long whose associated peer
940+
/// has disconnected in middle of channel creation handshake, and has not connected back yet.
941+
unconfirmed_channel_age_ticks: usize,
942+
}
943+
944+
impl UnconfirmedChannelContext {
945+
/// Determines whether we should force-close and purge this unfunded channel from memory due to it
946+
/// having reached the unfunded channel age limit.
947+
///
948+
/// This should be called on every [`super::channelmanager::ChannelManager::timer_tick_occurred`].
949+
pub fn should_expire_unconfirmed_channel(&mut self, peer_connected: bool) -> bool {
950+
if peer_connected {
951+
self.unconfirmed_channel_age_ticks = 0;
952+
return false;
953+
}
954+
self.unconfirmed_channel_age_ticks += 1;
955+
self.unconfirmed_channel_age_ticks >= DISCONNECTED_UNCONFIRMED_CHANNEL_AGE_LIMIT_TICKS
956+
}
957+
}
958+
922959
/// Contains all state common to unfunded inbound/outbound channels.
923960
pub(super) struct UnfundedChannelContext {
924961
/// A counter tracking how many ticks have elapsed since this unfunded channel was
@@ -6075,6 +6112,7 @@ impl<SP: Deref> Channel<SP> where
60756112
pub(super) struct OutboundV1Channel<SP: Deref> where SP::Target: SignerProvider {
60766113
pub context: ChannelContext<SP>,
60776114
pub unfunded_context: UnfundedChannelContext,
6115+
pub unconfirmed_context: UnconfirmedChannelContext,
60786116
}
60796117

60806118
impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
@@ -6279,7 +6317,8 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
62796317

62806318
blocked_monitor_updates: Vec::new(),
62816319
},
6282-
unfunded_context: UnfundedChannelContext { unfunded_channel_age_ticks: 0 }
6320+
unfunded_context: UnfundedChannelContext { unfunded_channel_age_ticks: 0 },
6321+
unconfirmed_context: UnconfirmedChannelContext { unconfirmed_channel_age_ticks: 0 }
62836322
})
62846323
}
62856324

0 commit comments

Comments
 (0)