Skip to content

Commit bd6323e

Browse files
Add counterparty_node_id & channel_capacity to ChannelClosed
The current ChannelClosed event does not let you know the counterparty during a channel close event. This change adds the counterparty_node_id and the channel_capacity to the ChannelClosed event. This helps users to have more context during a channel close event. Solves #2343
1 parent 6f58072 commit bd6323e

File tree

2 files changed

+44
-14
lines changed

2 files changed

+44
-14
lines changed

lightning/src/events/mod.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,15 @@ pub enum Event {
755755
/// [`UserConfig::manually_accept_inbound_channels`]: crate::util::config::UserConfig::manually_accept_inbound_channels
756756
user_channel_id: u128,
757757
/// The reason the channel was closed.
758-
reason: ClosureReason
758+
reason: ClosureReason,
759+
/// Counterparty in the closed channel.
760+
///
761+
/// This field will be `None` for objects serialized prior to LDK 0.0.117.
762+
counterparty_node_id: Option<PublicKey>,
763+
/// Channel capacity of the closing channel (sats).
764+
///
765+
/// This field will be `None` for objects serialized prior to LDK 0.0.117.
766+
channel_capacity_sats: Option<u64>,
759767
},
760768
/// Used to indicate to the user that they can abandon the funding transaction and recycle the
761769
/// inputs for another purpose.
@@ -956,7 +964,9 @@ impl Writeable for Event {
956964
(5, outbound_amount_forwarded_msat, option),
957965
});
958966
},
959-
&Event::ChannelClosed { ref channel_id, ref user_channel_id, ref reason } => {
967+
&Event::ChannelClosed { ref channel_id, ref user_channel_id, ref reason,
968+
ref counterparty_node_id, ref channel_capacity_sats
969+
} => {
960970
9u8.write(writer)?;
961971
// `user_channel_id` used to be a single u64 value. In order to remain backwards
962972
// compatible with versions prior to 0.0.113, the u128 is serialized as two
@@ -968,6 +978,8 @@ impl Writeable for Event {
968978
(1, user_channel_id_low, required),
969979
(2, reason, required),
970980
(3, user_channel_id_high, required),
981+
(5, counterparty_node_id, option),
982+
(7, channel_capacity_sats, option),
971983
});
972984
},
973985
&Event::DiscardFunding { ref channel_id, ref transaction } => {
@@ -1252,11 +1264,15 @@ impl MaybeReadable for Event {
12521264
let mut reason = UpgradableRequired(None);
12531265
let mut user_channel_id_low_opt: Option<u64> = None;
12541266
let mut user_channel_id_high_opt: Option<u64> = None;
1267+
let mut counterparty_node_id = None;
1268+
let mut channel_capacity_sats = None;
12551269
read_tlv_fields!(reader, {
12561270
(0, channel_id, required),
12571271
(1, user_channel_id_low_opt, option),
12581272
(2, reason, upgradable_required),
12591273
(3, user_channel_id_high_opt, option),
1274+
(5, counterparty_node_id, option),
1275+
(7, channel_capacity_sats, option),
12601276
});
12611277

12621278
// `user_channel_id` used to be a single u64 value. In order to remain
@@ -1265,7 +1281,8 @@ impl MaybeReadable for Event {
12651281
let user_channel_id = (user_channel_id_low_opt.unwrap_or(0) as u128) +
12661282
((user_channel_id_high_opt.unwrap_or(0) as u128) << 64);
12671283

1268-
Ok(Some(Event::ChannelClosed { channel_id, user_channel_id, reason: _init_tlv_based_struct_field!(reason, upgradable_required) }))
1284+
Ok(Some(Event::ChannelClosed { channel_id, user_channel_id, reason: _init_tlv_based_struct_field!(reason, upgradable_required),
1285+
counterparty_node_id, channel_capacity_sats }))
12691286
};
12701287
f()
12711288
},

lightning/src/ln/channelmanager.rs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ struct MsgHandleErrInternal {
376376
err: msgs::LightningError,
377377
chan_id: Option<([u8; 32], u128)>, // If Some a channel of ours has been closed
378378
shutdown_finish: Option<(ShutdownResult, Option<msgs::ChannelUpdate>)>,
379+
channel_capacity: Option<u64>,
379380
}
380381
impl MsgHandleErrInternal {
381382
#[inline]
@@ -392,14 +393,15 @@ impl MsgHandleErrInternal {
392393
},
393394
chan_id: None,
394395
shutdown_finish: None,
396+
channel_capacity: None,
395397
}
396398
}
397399
#[inline]
398400
fn from_no_close(err: msgs::LightningError) -> Self {
399-
Self { err, chan_id: None, shutdown_finish: None }
401+
Self { err, chan_id: None, shutdown_finish: None, channel_capacity: None }
400402
}
401403
#[inline]
402-
fn from_finish_shutdown(err: String, channel_id: [u8; 32], user_channel_id: u128, shutdown_res: ShutdownResult, channel_update: Option<msgs::ChannelUpdate>) -> Self {
404+
fn from_finish_shutdown(err: String, channel_id: [u8; 32], user_channel_id: u128, shutdown_res: ShutdownResult, channel_update: Option<msgs::ChannelUpdate>, channel_capacity: u64) -> Self {
403405
Self {
404406
err: LightningError {
405407
err: err.clone(),
@@ -412,6 +414,7 @@ impl MsgHandleErrInternal {
412414
},
413415
chan_id: Some((channel_id, user_channel_id)),
414416
shutdown_finish: Some((shutdown_res, channel_update)),
417+
channel_capacity: Some(channel_capacity)
415418
}
416419
}
417420
#[inline]
@@ -444,6 +447,7 @@ impl MsgHandleErrInternal {
444447
},
445448
chan_id: None,
446449
shutdown_finish: None,
450+
channel_capacity: None,
447451
}
448452
}
449453
}
@@ -1659,7 +1663,7 @@ macro_rules! handle_error {
16591663

16601664
match $internal {
16611665
Ok(msg) => Ok(msg),
1662-
Err(MsgHandleErrInternal { err, chan_id, shutdown_finish }) => {
1666+
Err(MsgHandleErrInternal { err, chan_id, shutdown_finish, channel_capacity }) => {
16631667
let mut msg_events = Vec::with_capacity(2);
16641668

16651669
if let Some((shutdown_res, update_option)) = shutdown_finish {
@@ -1672,7 +1676,9 @@ macro_rules! handle_error {
16721676
if let Some((channel_id, user_channel_id)) = chan_id {
16731677
$self.pending_events.lock().unwrap().push_back((events::Event::ChannelClosed {
16741678
channel_id, user_channel_id,
1675-
reason: ClosureReason::ProcessingError { err: err.err.clone() }
1679+
reason: ClosureReason::ProcessingError { err: err.err.clone() },
1680+
counterparty_node_id: Some($counterparty_node_id),
1681+
channel_capacity_sats: channel_capacity,
16761682
}, None));
16771683
}
16781684
}
@@ -1745,7 +1751,7 @@ macro_rules! convert_chan_err {
17451751
update_maps_on_chan_removal!($self, &$channel.context);
17461752
let shutdown_res = $channel.context.force_shutdown(true);
17471753
(true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, $channel.context.get_user_id(),
1748-
shutdown_res, $self.get_channel_update_for_broadcast(&$channel).ok()))
1754+
shutdown_res, $self.get_channel_update_for_broadcast(&$channel).ok(), $channel.context.get_value_satoshis()))
17491755
},
17501756
}
17511757
};
@@ -1758,7 +1764,7 @@ macro_rules! convert_chan_err {
17581764
update_maps_on_chan_removal!($self, &$channel_context);
17591765
let shutdown_res = $channel_context.force_shutdown(false);
17601766
(true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, $channel_context.get_user_id(),
1761-
shutdown_res, None))
1767+
shutdown_res, None, $channel_context.get_value_satoshis()))
17621768
},
17631769
}
17641770
}
@@ -1937,7 +1943,7 @@ macro_rules! handle_new_monitor_update {
19371943
let res = Err(MsgHandleErrInternal::from_finish_shutdown(
19381944
"ChannelMonitor storage failure".to_owned(), $chan.context.channel_id(),
19391945
$chan.context.get_user_id(), $chan.context.force_shutdown(false),
1940-
$self.get_channel_update_for_broadcast(&$chan).ok()));
1946+
$self.get_channel_update_for_broadcast(&$chan).ok(), $chan.context.get_value_satoshis()));
19411947
$remove;
19421948
res
19431949
},
@@ -2371,7 +2377,9 @@ where
23712377
pending_events_lock.push_back((events::Event::ChannelClosed {
23722378
channel_id: context.channel_id(),
23732379
user_channel_id: context.get_user_id(),
2374-
reason: closure_reason
2380+
reason: closure_reason,
2381+
counterparty_node_id: Some(context.get_counterparty_node_id()),
2382+
channel_capacity_sats: Some(context.get_value_satoshis()),
23752383
}, None));
23762384
}
23772385

@@ -3371,7 +3379,8 @@ where
33713379
let channel_id = chan.context.channel_id();
33723380
let user_id = chan.context.get_user_id();
33733381
let shutdown_res = chan.context.force_shutdown(false);
3374-
(chan, MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, user_id, shutdown_res, None))
3382+
let channel_capacity = chan.context.get_value_satoshis();
3383+
(chan, MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, user_id, shutdown_res, None, channel_capacity))
33753384
} else { unreachable!(); });
33763385
match funding_res {
33773386
Ok((chan, funding_msg)) => (chan, funding_msg),
@@ -5408,7 +5417,7 @@ where
54085417
let user_id = inbound_chan.context.get_user_id();
54095418
let shutdown_res = inbound_chan.context.force_shutdown(false);
54105419
return Err(MsgHandleErrInternal::from_finish_shutdown(format!("{}", err),
5411-
msg.temporary_channel_id, user_id, shutdown_res, None));
5420+
msg.temporary_channel_id, user_id, shutdown_res, None, inbound_chan.context.get_value_satoshis()));
54125421
},
54135422
}
54145423
},
@@ -8356,7 +8365,9 @@ where
83568365
channel_closures.push_back((events::Event::ChannelClosed {
83578366
channel_id: channel.context.channel_id(),
83588367
user_channel_id: channel.context.get_user_id(),
8359-
reason: ClosureReason::OutdatedChannelManager
8368+
reason: ClosureReason::OutdatedChannelManager,
8369+
counterparty_node_id: Some(channel.context.get_counterparty_node_id()),
8370+
channel_capacity_sats: Some(channel.context.get_value_satoshis()),
83608371
}, None));
83618372
for (channel_htlc_source, payment_hash) in channel.inflight_htlc_sources() {
83628373
let mut found_htlc = false;
@@ -8408,6 +8419,8 @@ where
84088419
channel_id: channel.context.channel_id(),
84098420
user_channel_id: channel.context.get_user_id(),
84108421
reason: ClosureReason::DisconnectedPeer,
8422+
counterparty_node_id: Some(channel.context.get_counterparty_node_id()),
8423+
channel_capacity_sats: Some(channel.context.get_value_satoshis()),
84118424
}, None));
84128425
} else {
84138426
log_error!(args.logger, "Missing ChannelMonitor for channel {} needed by ChannelManager.", log_bytes!(channel.context.channel_id()));

0 commit comments

Comments
 (0)