Skip to content

Commit c345478

Browse files
add signal_channel_readiness to ChannelManager api
1 parent 07404e4 commit c345478

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

lightning/src/ln/channel.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4746,6 +4746,10 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
47464746
self.pending_channel_ready_event_emitted = true;
47474747
}
47484748

4749+
pub(crate) fn unset_requires_manual_readiness_signal(&mut self) {
4750+
self.requires_manual_readiness_signal = false;
4751+
}
4752+
47494753
/// Tracks the number of ticks elapsed since the previous [`ChannelConfig`] was updated. Once
47504754
/// [`EXPIRE_PREV_CONFIG_TICKS`] is reached, the previous config is considered expired and will
47514755
/// no longer be considered when forwarding HTLCs.
@@ -5010,7 +5014,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
50105014
self.channel_update_status = status;
50115015
}
50125016

5013-
fn check_get_channel_ready(&mut self, height: u32) -> (Option<msgs::ChannelReady>, bool) {
5017+
pub fn check_get_channel_ready(&mut self, height: u32) -> (Option<msgs::ChannelReady>, bool) {
50145018
// Called:
50155019
// * always when a new block/transactions are confirmed with the new height
50165020
// * when funding is signed with a height of 0

lightning/src/ln/channelmanager.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4406,6 +4406,42 @@ where
44064406
Ok(())
44074407
}
44084408

4409+
/// Signals channel readiness after a [`Event::PendingChannelReady`].
4410+
///
4411+
/// The `channel_id` parameter indicates which channel should signal readiness,
4412+
/// and the `counterparty_node_id` parameter is the id of the peer the channel is with.
4413+
///
4414+
/// [`Event::PendingChannelReady`]: events::Event::PendingChannelReady
4415+
pub fn signal_channel_readiness(&self, channel_id: &[u8; 32], counterparty_node_id: &PublicKey) -> Result<(), APIError> {
4416+
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
4417+
4418+
let per_peer_state = self.per_peer_state.read().unwrap();
4419+
let peer_state_mutex = per_peer_state.get(counterparty_node_id)
4420+
.ok_or_else(|| APIError::ChannelUnavailable { err: format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id) })?;
4421+
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
4422+
let peer_state = &mut *peer_state_lock;
4423+
let pending_msg_events = &mut peer_state.pending_msg_events;
4424+
match peer_state.channel_by_id.entry(channel_id.clone()) {
4425+
hash_map::Entry::Occupied(mut channel) => {
4426+
let best_block_height = self.best_block.read().unwrap().height();
4427+
let channel = channel.get_mut();
4428+
let (channel_ready_opt, _) = channel.check_get_channel_ready(best_block_height);
4429+
match channel_ready_opt {
4430+
Some(channel_ready) => {
4431+
channel.unset_requires_manual_readiness_signal();
4432+
send_channel_ready!(self, pending_msg_events, channel, channel_ready);
4433+
}
4434+
None => return Err(APIError::APIMisuseError { err: "The channel isn't currently in a state where we can signal readiness.".to_owned() })
4435+
}
4436+
}
4437+
hash_map::Entry::Vacant(_) => {
4438+
return Err(APIError::ChannelUnavailable { err: format!("Channel with id {} not found for the passed counterparty node_id {}", log_bytes!(*channel_id), counterparty_node_id) });
4439+
}
4440+
}
4441+
4442+
Ok(())
4443+
}
4444+
44094445
/// Gets the number of peers which match the given filter and do not have any funded, outbound,
44104446
/// or 0-conf channels.
44114447
///

0 commit comments

Comments
 (0)