Skip to content

Commit 25f9adf

Browse files
track PendingChannelReady emission and surface readiness
1 parent 1e576b4 commit 25f9adf

File tree

2 files changed

+75
-20
lines changed

2 files changed

+75
-20
lines changed

lightning/src/ln/channel.rs

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ pub struct AvailableBalances {
7878
pub(crate) struct ChainActionUpdates {
7979
pub(crate) channel_ready_msg: Option<msgs::ChannelReady>,
8080
pub(crate) timed_out_htlcs: Vec<(HTLCSource, PaymentHash)>,
81-
pub(crate) announcement_sigs: Option<msgs::AnnouncementSignatures>
81+
pub(crate) announcement_sigs: Option<msgs::AnnouncementSignatures>,
82+
pub(crate) pending_channel_ready: bool
8283
}
8384

8485
impl ChainActionUpdates {
@@ -87,6 +88,7 @@ impl ChainActionUpdates {
8788
channel_ready_msg: None,
8889
timed_out_htlcs: Vec::new(),
8990
announcement_sigs: None,
91+
pending_channel_ready: false,
9092
}
9193
}
9294
}
@@ -755,6 +757,8 @@ pub(super) struct Channel<Signer: ChannelSigner> {
755757

756758
// We track whether we already emitted a `ChannelReady` event.
757759
channel_ready_event_emitted: bool,
760+
// We track whether we already emitted a `PendingChannelReady` event.
761+
pending_channel_ready_event_emitted: bool,
758762

759763
/// The unique identifier used to re-derive the private key material for the channel through
760764
/// [`SignerProvider::derive_channel_signer`].
@@ -1129,6 +1133,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
11291133
outbound_scid_alias,
11301134

11311135
channel_ready_event_emitted: false,
1136+
pending_channel_ready_event_emitted: false,
11321137

11331138
#[cfg(any(test, fuzzing))]
11341139
historical_inbound_htlc_fulfills: HashSet::new(),
@@ -1478,6 +1483,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
14781483
outbound_scid_alias,
14791484

14801485
channel_ready_event_emitted: false,
1486+
pending_channel_ready_event_emitted: false,
14811487

14821488
#[cfg(any(test, fuzzing))]
14831489
historical_inbound_htlc_fulfills: HashSet::new(),
@@ -2373,7 +2379,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
23732379

23742380
log_info!(logger, "Generated funding_signed for peer for channel {}", log_bytes!(self.channel_id()));
23752381

2376-
let need_channel_ready = self.check_get_channel_ready(0).is_some();
2382+
let need_channel_ready = self.check_get_channel_ready(0).0.is_some();
23772383
self.monitor_updating_paused(false, false, need_channel_ready, Vec::new(), Vec::new(), Vec::new());
23782384

23792385
Ok((msgs::FundingSigned {
@@ -2461,7 +2467,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
24612467

24622468
log_info!(logger, "Received funding_signed from peer for channel {}", log_bytes!(self.channel_id()));
24632469

2464-
let need_channel_ready = self.check_get_channel_ready(0).is_some();
2470+
let need_channel_ready = self.check_get_channel_ready(0).0.is_some();
24652471
self.monitor_updating_paused(false, false, need_channel_ready, Vec::new(), Vec::new(), Vec::new());
24662472
Ok(channel_monitor)
24672473
}
@@ -4730,6 +4736,16 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
47304736
self.channel_ready_event_emitted = true;
47314737
}
47324738

4739+
// Checks whether we should emit a `PendingChannelReady` event.
4740+
pub(crate) fn should_emit_pending_channel_ready_event(&self) -> bool {
4741+
self.requires_manual_readiness_signal && !self.pending_channel_ready_event_emitted
4742+
}
4743+
4744+
// Remembers that we already emitted a `PendingChannelReady` event.
4745+
pub(crate) fn set_pending_channel_ready_event_emitted(&mut self) {
4746+
self.pending_channel_ready_event_emitted = true;
4747+
}
4748+
47334749
/// Tracks the number of ticks elapsed since the previous [`ChannelConfig`] was updated. Once
47344750
/// [`EXPIRE_PREV_CONFIG_TICKS`] is reached, the previous config is considered expired and will
47354751
/// no longer be considered when forwarding HTLCs.
@@ -4994,12 +5010,12 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
49945010
self.channel_update_status = status;
49955011
}
49965012

4997-
fn check_get_channel_ready(&mut self, height: u32) -> Option<msgs::ChannelReady> {
5013+
fn check_get_channel_ready(&mut self, height: u32) -> (Option<msgs::ChannelReady>, bool) {
49985014
// Called:
49995015
// * always when a new block/transactions are confirmed with the new height
50005016
// * when funding is signed with a height of 0
50015017
if self.funding_tx_confirmation_height == 0 && self.minimum_depth != Some(0) {
5002-
return None;
5018+
return (None, false);
50035019
}
50045020

50055021
let funding_tx_confirmations = height as i64 - self.funding_tx_confirmation_height as i64 + 1;
@@ -5008,7 +5024,11 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
50085024
}
50095025

50105026
if funding_tx_confirmations < self.minimum_depth.unwrap_or(0) as i64 {
5011-
return None;
5027+
return (None, false);
5028+
}
5029+
5030+
if self.should_emit_pending_channel_ready_event() {
5031+
return (None, true);
50125032
}
50135033

50145034
let non_shutdown_state = self.channel_state & (!MULTI_STATE_FLAGS);
@@ -5042,17 +5062,17 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
50425062
if self.channel_state & (ChannelState::PeerDisconnected as u32) == 0 {
50435063
let next_per_commitment_point =
50445064
self.holder_signer.get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, &self.secp_ctx);
5045-
return Some(msgs::ChannelReady {
5065+
return (Some(msgs::ChannelReady {
50465066
channel_id: self.channel_id,
50475067
next_per_commitment_point,
50485068
short_channel_id_alias: Some(self.outbound_scid_alias),
5049-
});
5069+
}), false);
50505070
}
50515071
} else {
50525072
self.monitor_pending_channel_ready = true;
50535073
}
50545074
}
5055-
None
5075+
(None, false)
50565076
}
50575077

50585078
/// When a transaction is confirmed, we check whether it is or spends the funding transaction
@@ -5109,13 +5129,23 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
51095129
// If we allow 1-conf funding, we may need to check for channel_ready here and
51105130
// send it immediately instead of waiting for a best_block_updated call (which
51115131
// may have already happened for this block).
5112-
if let Some(channel_ready) = self.check_get_channel_ready(height) {
5132+
let (channel_ready_opt, generate_pending_channel_ready) = self.check_get_channel_ready(height);
5133+
5134+
if let Some(channel_ready) = channel_ready_opt {
51135135
log_info!(logger, "Sending a channel_ready to our peer for channel {}", log_bytes!(self.channel_id));
51145136
let announcement_sigs = self.get_announcement_sigs(node_signer, genesis_block_hash, user_config, height, logger);
51155137
return Ok(ChainActionUpdates {
51165138
channel_ready_msg: Some(channel_ready),
51175139
announcement_sigs,
5118-
timed_out_htlcs: vec![]
5140+
timed_out_htlcs: vec![],
5141+
pending_channel_ready: false
5142+
});
5143+
} else if generate_pending_channel_ready {
5144+
return Ok(ChainActionUpdates {
5145+
channel_ready_msg: None,
5146+
announcement_sigs: None,
5147+
timed_out_htlcs: vec![],
5148+
pending_channel_ready: true
51195149
});
51205150
}
51215151
}
@@ -5127,11 +5157,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
51275157
}
51285158
}
51295159
}
5130-
Ok(ChainActionUpdates {
5131-
channel_ready_msg: None,
5132-
announcement_sigs: None,
5133-
timed_out_htlcs: vec![]
5134-
})
5160+
Ok(ChainActionUpdates::no_updates())
51355161
}
51365162

51375163
/// When a new block is connected, we check the height of the block against outbound holding
@@ -5183,15 +5209,17 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
51835209

51845210
self.update_time_counter = cmp::max(self.update_time_counter, highest_header_time);
51855211

5186-
if let Some(channel_ready) = self.check_get_channel_ready(height) {
5212+
let (channel_ready_opt, generate_pending_channel_ready) = self.check_get_channel_ready(height);
5213+
if let Some(channel_ready) = channel_ready_opt {
51875214
let announcement_sigs = if let Some((genesis_block_hash, node_signer, user_config)) = genesis_node_signer {
51885215
self.get_announcement_sigs(node_signer, genesis_block_hash, user_config, height, logger)
51895216
} else { None };
51905217
log_info!(logger, "Sending a channel_ready to our peer for channel {}", log_bytes!(self.channel_id));
51915218
return Ok(ChainActionUpdates {
51925219
channel_ready_msg: Some(channel_ready),
51935220
timed_out_htlcs,
5194-
announcement_sigs
5221+
announcement_sigs,
5222+
pending_channel_ready: false
51955223
});
51965224
}
51975225

@@ -5235,7 +5263,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
52355263
Ok(ChainActionUpdates {
52365264
channel_ready_msg: None,
52375265
timed_out_htlcs,
5238-
announcement_sigs
5266+
announcement_sigs,
5267+
pending_channel_ready: generate_pending_channel_ready
52395268
})
52405269
}
52415270

@@ -6467,6 +6496,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for Channel<Signer> {
64676496
{ Some(self.holder_max_htlc_value_in_flight_msat) } else { None };
64686497

64696498
let channel_ready_event_emitted = Some(self.channel_ready_event_emitted);
6499+
let pending_channel_ready_event_emitted = Some(self.pending_channel_ready_event_emitted);
64706500

64716501
// `user_id` used to be a single u64 value. In order to remain backwards compatible with
64726502
// versions prior to 0.0.113, the u128 is serialized as two separate u64 values. Therefore,
@@ -6500,7 +6530,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for Channel<Signer> {
65006530
(23, channel_ready_event_emitted, option),
65016531
(25, user_id_high_opt, option),
65026532
(27, self.channel_keys_id, required),
6503-
(29, requires_manual_readiness_signal, option)
6533+
(29, requires_manual_readiness_signal, option),
6534+
(31, pending_channel_ready_event_emitted, option),
65046535
});
65056536

65066537
Ok(())
@@ -6773,6 +6804,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
67736804
let mut user_id_high_opt: Option<u64> = None;
67746805
let mut channel_keys_id: Option<[u8; 32]> = None;
67756806
let mut requires_manual_readiness_signal: Option<bool> = Some(false);
6807+
let mut pending_channel_ready_event_emitted = None;
67766808

67776809
read_tlv_fields!(reader, {
67786810
(0, announcement_sigs, option),
@@ -6794,6 +6826,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
67946826
(25, user_id_high_opt, option),
67956827
(27, channel_keys_id, option),
67966828
(29, requires_manual_readiness_signal, option),
6829+
(31, pending_channel_ready_event_emitted, option),
67976830
});
67986831

67996832
let (channel_keys_id, holder_signer) = if let Some(channel_keys_id) = channel_keys_id {
@@ -6952,6 +6985,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
69526985
outbound_scid_alias: outbound_scid_alias.unwrap_or(0),
69536986

69546987
channel_ready_event_emitted: channel_ready_event_emitted.unwrap_or(true),
6988+
pending_channel_ready_event_emitted: pending_channel_ready_event_emitted.unwrap_or(true),
69556989

69566990
#[cfg(any(test, fuzzing))]
69576991
historical_inbound_htlc_fulfills,

lightning/src/ln/channelmanager.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,6 +1504,23 @@ macro_rules! emit_channel_ready_event {
15041504
}
15051505
}
15061506

1507+
macro_rules! emit_pending_channel_ready_event {
1508+
($self: expr, $channel: expr) => {
1509+
if $channel.should_emit_channel_ready_event() {
1510+
{
1511+
let mut pending_events = $self.pending_events.lock().unwrap();
1512+
pending_events.push(events::Event::PendingChannelReady {
1513+
channel_id: $channel.channel_id(),
1514+
user_channel_id: $channel.get_user_id(),
1515+
counterparty_node_id: $channel.get_counterparty_node_id(),
1516+
funding_outpoint: $channel.get_funding_txo().unwrap(),
1517+
});
1518+
}
1519+
$channel.set_pending_channel_ready_event_emitted();
1520+
}
1521+
}
1522+
}
1523+
15071524
macro_rules! handle_monitor_update_completion {
15081525
($self: ident, $update_id: expr, $peer_state_lock: expr, $peer_state: expr, $per_peer_state_lock: expr, $chan: expr) => { {
15091526
let mut updates = $chan.monitor_updating_restored(&$self.logger,
@@ -6001,6 +6018,10 @@ where
60016018
}
60026019
}
60036020

6021+
if chain_action_updates.pending_channel_ready {
6022+
emit_pending_channel_ready_event!(self, channel);
6023+
}
6024+
60046025
emit_channel_ready_event!(self, channel);
60056026

60066027
if let Some(announcement_sigs) = chain_action_updates.announcement_sigs {

0 commit comments

Comments
 (0)