Skip to content

Commit 04952ca

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 c5214c2 commit 04952ca

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

lightning/src/events/mod.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,12 @@ pub enum Event {
733733
/// [`UserConfig::manually_accept_inbound_channels`]: crate::util::config::UserConfig::manually_accept_inbound_channels
734734
user_channel_id: u128,
735735
/// The reason the channel was closed.
736-
reason: ClosureReason
736+
reason: ClosureReason,
737+
/// Counterparty in the closed channel
738+
counterparty_node_id: Option<PublicKey>,
739+
/// Value of the closing channel
740+
channel_capacity: Option<u64>,
741+
737742
},
738743
/// Used to indicate to the user that they can abandon the funding transaction and recycle the
739744
/// inputs for another purpose.
@@ -929,7 +934,7 @@ impl Writeable for Event {
929934
(5, outbound_amount_forwarded_msat, option),
930935
});
931936
},
932-
&Event::ChannelClosed { ref channel_id, ref user_channel_id, ref reason } => {
937+
&Event::ChannelClosed { ref channel_id, ref user_channel_id, ref reason, ref counterparty_node_id, ref channel_capacity } => {
933938
9u8.write(writer)?;
934939
// `user_channel_id` used to be a single u64 value. In order to remain backwards
935940
// compatible with versions prior to 0.0.113, the u128 is serialized as two
@@ -941,6 +946,8 @@ impl Writeable for Event {
941946
(1, user_channel_id_low, required),
942947
(2, reason, required),
943948
(3, user_channel_id_high, required),
949+
(4, counterparty_node_id, option),
950+
(5, channel_capacity, option),
944951
});
945952
},
946953
&Event::DiscardFunding { ref channel_id, ref transaction } => {
@@ -1221,11 +1228,15 @@ impl MaybeReadable for Event {
12211228
let mut reason = UpgradableRequired(None);
12221229
let mut user_channel_id_low_opt: Option<u64> = None;
12231230
let mut user_channel_id_high_opt: Option<u64> = None;
1231+
let mut counterparty_node_id = None;
1232+
let mut channel_capacity = None;
12241233
read_tlv_fields!(reader, {
12251234
(0, channel_id, required),
12261235
(1, user_channel_id_low_opt, option),
12271236
(2, reason, upgradable_required),
12281237
(3, user_channel_id_high_opt, option),
1238+
(4, counterparty_node_id, option),
1239+
(5, channel_capacity, option),
12291240
});
12301241

12311242
// `user_channel_id` used to be a single u64 value. In order to remain
@@ -1234,7 +1245,7 @@ impl MaybeReadable for Event {
12341245
let user_channel_id = (user_channel_id_low_opt.unwrap_or(0) as u128) +
12351246
((user_channel_id_high_opt.unwrap_or(0) as u128) << 64);
12361247

1237-
Ok(Some(Event::ChannelClosed { channel_id, user_channel_id, reason: _init_tlv_based_struct_field!(reason, upgradable_required) }))
1248+
Ok(Some(Event::ChannelClosed { channel_id, user_channel_id, reason: _init_tlv_based_struct_field!(reason, upgradable_required), counterparty_node_id, channel_capacity }))
12381249
};
12391250
f()
12401251
},

lightning/src/ln/channelmanager.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,10 +1606,19 @@ macro_rules! handle_error {
16061606
});
16071607
}
16081608
if let Some((channel_id, user_channel_id)) = chan_id {
1609-
$self.pending_events.lock().unwrap().push_back((events::Event::ChannelClosed {
1610-
channel_id, user_channel_id,
1611-
reason: ClosureReason::ProcessingError { err: err.err.clone() }
1612-
}, None));
1609+
let per_peer_state = $self.per_peer_state.read().unwrap();
1610+
if let Some(peer_state_mutex) = per_peer_state.get(&$counterparty_node_id){
1611+
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
1612+
let peer_state = &mut *peer_state_lock;
1613+
let channel = peer_state.channel_by_id.get(&channel_id).unwrap();
1614+
$self.pending_events.lock().unwrap().push_back((events::Event::ChannelClosed {
1615+
channel_id, user_channel_id,
1616+
reason: ClosureReason::ProcessingError { err: err.err.clone() },
1617+
counterparty_node_id: Some($counterparty_node_id),
1618+
channel_capacity: Some(channel.context.get_value_satoshis()),
1619+
}, None));
1620+
}
1621+
16131622
}
16141623
}
16151624

@@ -2270,7 +2279,9 @@ where
22702279
pending_events_lock.push_back((events::Event::ChannelClosed {
22712280
channel_id: context.channel_id(),
22722281
user_channel_id: context.get_user_id(),
2273-
reason: closure_reason
2282+
reason: closure_reason,
2283+
counterparty_node_id: Some(context.get_counterparty_node_id()),
2284+
channel_capacity: Some(context.get_value_satoshis()),
22742285
}, None));
22752286
}
22762287

@@ -8056,7 +8067,9 @@ where
80568067
channel_closures.push_back((events::Event::ChannelClosed {
80578068
channel_id: channel.context.channel_id(),
80588069
user_channel_id: channel.context.get_user_id(),
8059-
reason: ClosureReason::OutdatedChannelManager
8070+
reason: ClosureReason::OutdatedChannelManager,
8071+
counterparty_node_id: Some(channel.context.get_counterparty_node_id()),
8072+
channel_capacity: Some(channel.context.get_value_satoshis()),
80608073
}, None));
80618074
for (channel_htlc_source, payment_hash) in channel.inflight_htlc_sources() {
80628075
let mut found_htlc = false;
@@ -8109,6 +8122,9 @@ where
81098122
channel_id: channel.context.channel_id(),
81108123
user_channel_id: channel.context.get_user_id(),
81118124
reason: ClosureReason::DisconnectedPeer,
8125+
counterparty_node_id: Some(channel.context.get_counterparty_node_id()),
8126+
channel_capacity: Some(channel.context.get_value_satoshis()),
8127+
81128128
}, None));
81138129
} else {
81148130
log_error!(args.logger, "Missing ChannelMonitor for channel {} needed by ChannelManager.", log_bytes!(channel.context.channel_id()));

0 commit comments

Comments
 (0)