Skip to content

Commit 9a5c9b3

Browse files
committed
Include the user channel id counter in Event::ChannelClosed
This makes it more practical for users to track channels using their own IDs, especially across funding.
1 parent be82239 commit 9a5c9b3

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

lightning/src/ln/channelmanager.rs

+21-11
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ type ShutdownResult = (Option<(OutPoint, ChannelMonitorUpdate)>, Vec<(HTLCSource
242242
243243
struct MsgHandleErrInternal {
244244
err: msgs::LightningError,
245-
chan_id: Option<[u8; 32]>, // If Some a channel of ours has been closed
245+
chan_id: Option<([u8; 32], u64)>, // If Some a channel of ours has been closed
246246
shutdown_finish: Option<(ShutdownResult, Option<msgs::ChannelUpdate>)>,
247247
}
248248
impl MsgHandleErrInternal {
@@ -278,7 +278,7 @@ impl MsgHandleErrInternal {
278278
Self { err, chan_id: None, shutdown_finish: None }
279279
}
280280
#[inline]
281-
fn from_finish_shutdown(err: String, channel_id: [u8; 32], shutdown_res: ShutdownResult, channel_update: Option<msgs::ChannelUpdate>) -> Self {
281+
fn from_finish_shutdown(err: String, channel_id: [u8; 32], user_channel_id: u64, shutdown_res: ShutdownResult, channel_update: Option<msgs::ChannelUpdate>) -> Self {
282282
Self {
283283
err: LightningError {
284284
err: err.clone(),
@@ -289,7 +289,7 @@ impl MsgHandleErrInternal {
289289
},
290290
},
291291
},
292-
chan_id: Some(channel_id),
292+
chan_id: Some((channel_id, user_channel_id)),
293293
shutdown_finish: Some((shutdown_res, channel_update)),
294294
}
295295
}
@@ -894,8 +894,11 @@ macro_rules! handle_error {
894894
msg: update
895895
});
896896
}
897-
if let Some(channel_id) = chan_id {
898-
$self.pending_events.lock().unwrap().push(events::Event::ChannelClosed { channel_id, reason: ClosureReason::ProcessingError { err: err.err.clone() } });
897+
if let Some((channel_id, user_channel_id)) = chan_id {
898+
$self.pending_events.lock().unwrap().push(events::Event::ChannelClosed {
899+
channel_id, user_channel_id,
900+
reason: ClosureReason::ProcessingError { err: err.err.clone() }
901+
});
899902
}
900903
}
901904

@@ -937,15 +940,17 @@ macro_rules! convert_chan_err {
937940
$short_to_id.remove(&short_id);
938941
}
939942
let shutdown_res = $channel.force_shutdown(true);
940-
(true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, shutdown_res, $self.get_channel_update_for_broadcast(&$channel).ok()))
943+
(true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, $channel.get_user_id(),
944+
shutdown_res, $self.get_channel_update_for_broadcast(&$channel).ok()))
941945
},
942946
ChannelError::CloseDelayBroadcast(msg) => {
943947
log_error!($self.logger, "Channel {} need to be shutdown but closing transactions not broadcast due to {}", log_bytes!($channel_id[..]), msg);
944948
if let Some(short_id) = $channel.get_short_channel_id() {
945949
$short_to_id.remove(&short_id);
946950
}
947951
let shutdown_res = $channel.force_shutdown(false);
948-
(true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, shutdown_res, $self.get_channel_update_for_broadcast(&$channel).ok()))
952+
(true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, $channel.get_user_id(),
953+
shutdown_res, $self.get_channel_update_for_broadcast(&$channel).ok()))
949954
}
950955
}
951956
}
@@ -1013,7 +1018,7 @@ macro_rules! handle_monitor_err {
10131018
// splitting hairs we'd prefer to claim payments that were to us, but we haven't
10141019
// given up the preimage yet, so might as well just wait until the payment is
10151020
// retried, avoiding the on-chain fees.
1016-
let res: Result<(), _> = Err(MsgHandleErrInternal::from_finish_shutdown("ChannelMonitor storage failure".to_owned(), *$chan_id,
1021+
let res: Result<(), _> = Err(MsgHandleErrInternal::from_finish_shutdown("ChannelMonitor storage failure".to_owned(), *$chan_id, $chan.get_user_id(),
10171022
$chan.force_shutdown(true), $self.get_channel_update_for_broadcast(&$chan).ok() ));
10181023
(res, true)
10191024
},
@@ -1402,7 +1407,11 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
14021407
},
14031408
None => {},
14041409
}
1405-
pending_events_lock.push(events::Event::ChannelClosed { channel_id: channel.channel_id(), reason: closure_reason });
1410+
pending_events_lock.push(events::Event::ChannelClosed {
1411+
channel_id: channel.channel_id(),
1412+
user_channel_id: channel.get_user_id(),
1413+
reason: closure_reason
1414+
});
14061415
}
14071416

14081417
fn close_channel_internal(&self, channel_id: &[u8; 32], target_feerate_sats_per_1000_weight: Option<u32>) -> Result<(), APIError> {
@@ -2237,7 +2246,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
22372246

22382247
(chan.get_outbound_funding_created(funding_transaction, funding_txo, &self.logger)
22392248
.map_err(|e| if let ChannelError::Close(msg) = e {
2240-
MsgHandleErrInternal::from_finish_shutdown(msg, chan.channel_id(), chan.force_shutdown(true), None)
2249+
MsgHandleErrInternal::from_finish_shutdown(msg, chan.channel_id(), chan.get_user_id(), chan.force_shutdown(true), None)
22412250
} else { unreachable!(); })
22422251
, chan)
22432252
},
@@ -2579,7 +2588,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
25792588
channel_state.short_to_id.remove(&short_id);
25802589
}
25812590
// ChannelClosed event is generated by handle_error for us.
2582-
Err(MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, channel.force_shutdown(true), self.get_channel_update_for_broadcast(&channel).ok()))
2591+
Err(MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, channel.get_user_id(), channel.force_shutdown(true), self.get_channel_update_for_broadcast(&channel).ok()))
25832592
},
25842593
ChannelError::CloseDelayBroadcast(_) => { panic!("Wait is only generated on receipt of channel_reestablish, which is handled by try_chan_entry, we don't bother to support it here"); }
25852594
};
@@ -5618,6 +5627,7 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
56185627
monitor.broadcast_latest_holder_commitment_txn(&args.tx_broadcaster, &args.logger);
56195628
channel_closures.push(events::Event::ChannelClosed {
56205629
channel_id: channel.channel_id(),
5630+
user_channel_id: channel.get_user_id(),
56215631
reason: ClosureReason::OutdatedChannelManager
56225632
});
56235633
} else {

lightning/src/util/events.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,10 @@ pub enum Event {
146146
channel_value_satoshis: u64,
147147
/// The script which should be used in the transaction output.
148148
output_script: Script,
149-
/// The value passed in to ChannelManager::create_channel
149+
/// The `user_id` value passed in to [`ChannelManager::create_channel`], or 0 for an
150+
/// inbound channel.
151+
///
152+
/// [`ChannelManager::create_channel`]: crate::ln::channelmanager::ChannelManager::create_channel
150153
user_channel_id: u64,
151154
},
152155
/// Indicates we've received money! Just gotta dig out that payment preimage and feed it to
@@ -262,6 +265,12 @@ pub enum Event {
262265
/// The channel_id of the channel which has been closed. Note that on-chain transactions
263266
/// resolving the channel are likely still awaiting confirmation.
264267
channel_id: [u8; 32],
268+
/// The `user_id` value passed in to [`ChannelManager::create_channel`], or 0 for an
269+
/// inbound channel. This will always be zero for objects serialized with LDK versions
270+
/// prior to 0.0.102.
271+
///
272+
/// [`ChannelManager::create_channel`]: crate::ln::channelmanager::ChannelManager::create_channel
273+
user_channel_id: u64,
265274
/// The reason the channel was closed.
266275
reason: ClosureReason
267276
},
@@ -352,10 +361,11 @@ impl Writeable for Event {
352361
(2, claim_from_onchain_tx, required),
353362
});
354363
},
355-
&Event::ChannelClosed { ref channel_id, ref reason } => {
364+
&Event::ChannelClosed { ref channel_id, ref user_channel_id, ref reason } => {
356365
9u8.write(writer)?;
357366
write_tlv_fields!(writer, {
358367
(0, channel_id, required),
368+
(1, user_channel_id, required),
359369
(2, reason, required)
360370
});
361371
},
@@ -492,12 +502,15 @@ impl MaybeReadable for Event {
492502
let f = || {
493503
let mut channel_id = [0; 32];
494504
let mut reason = None;
505+
let mut user_channel_id_opt = None;
495506
read_tlv_fields!(reader, {
496507
(0, channel_id, required),
508+
(1, user_channel_id_opt, option),
497509
(2, reason, ignorable),
498510
});
499511
if reason.is_none() { return Ok(None); }
500-
Ok(Some(Event::ChannelClosed { channel_id, reason: reason.unwrap() }))
512+
let user_channel_id = if let Some(id) = user_channel_id_opt { id } else { 0 };
513+
Ok(Some(Event::ChannelClosed { channel_id, user_channel_id, reason: reason.unwrap() }))
501514
};
502515
f()
503516
},

0 commit comments

Comments
 (0)