Skip to content

Commit

Permalink
Add channel funding txo to Channel Event::ChannelClosed
Browse files Browse the repository at this point in the history
  • Loading branch information
optout21 committed Dec 19, 2023
1 parent 4deb263 commit 11fe6ae
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 20 deletions.
10 changes: 8 additions & 2 deletions lightning/src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub use bump_transaction::BumpTransactionEvent;
use crate::sign::SpendableOutputDescriptor;
use crate::ln::channelmanager::{InterceptId, PaymentId, RecipientOnionFields};
use crate::ln::channel::FUNDING_CONF_DEADLINE_BLOCKS;
use crate::chain::transaction;
use crate::ln::features::ChannelTypeFeatures;
use crate::ln::msgs;
use crate::ln::{ChannelId, PaymentPreimage, PaymentHash, PaymentSecret};
Expand Down Expand Up @@ -886,6 +887,8 @@ pub enum Event {
///
/// This field will be `None` for objects serialized prior to LDK 0.0.117.
channel_capacity_sats: Option<u64>,
/// The original channel funding TXO; this helps checking for the existence and confirmation status of the closing tx
channel_funding_txo: Option<transaction::OutPoint>,
},
/// Used to indicate to the user that they can abandon the funding transaction and recycle the
/// inputs for another purpose.
Expand Down Expand Up @@ -1091,7 +1094,7 @@ impl Writeable for Event {
});
},
&Event::ChannelClosed { ref channel_id, ref user_channel_id, ref reason,
ref counterparty_node_id, ref channel_capacity_sats
ref counterparty_node_id, ref channel_capacity_sats, ref channel_funding_txo
} => {
9u8.write(writer)?;
// `user_channel_id` used to be a single u64 value. In order to remain backwards
Expand All @@ -1106,6 +1109,7 @@ impl Writeable for Event {
(3, user_channel_id_high, required),
(5, counterparty_node_id, option),
(7, channel_capacity_sats, option),
(9, channel_funding_txo, option),
});
},
&Event::DiscardFunding { ref channel_id, ref transaction } => {
Expand Down Expand Up @@ -1405,13 +1409,15 @@ impl MaybeReadable for Event {
let mut user_channel_id_high_opt: Option<u64> = None;
let mut counterparty_node_id = None;
let mut channel_capacity_sats = None;
let mut channel_funding_txo = None;
read_tlv_fields!(reader, {
(0, channel_id, required),
(1, user_channel_id_low_opt, option),
(2, reason, upgradable_required),
(3, user_channel_id_high_opt, option),
(5, counterparty_node_id, option),
(7, channel_capacity_sats, option),
(9, channel_funding_txo, option),
});

// `user_channel_id` used to be a single u64 value. In order to remain
Expand All @@ -1421,7 +1427,7 @@ impl MaybeReadable for Event {
((user_channel_id_high_opt.unwrap_or(0) as u128) << 64);

Ok(Some(Event::ChannelClosed { channel_id, user_channel_id, reason: _init_tlv_based_struct_field!(reason, upgradable_required),
counterparty_node_id, channel_capacity_sats }))
counterparty_node_id, channel_capacity_sats, channel_funding_txo }))
};
f()
},
Expand Down
32 changes: 19 additions & 13 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@ struct MsgHandleErrInternal {
chan_id: Option<(ChannelId, u128)>, // If Some a channel of ours has been closed
shutdown_finish: Option<(ShutdownResult, Option<msgs::ChannelUpdate>)>,
channel_capacity: Option<u64>,
channel_funding_txo: Option<OutPoint>,
}
impl MsgHandleErrInternal {
#[inline]
Expand All @@ -565,14 +566,17 @@ impl MsgHandleErrInternal {
chan_id: None,
shutdown_finish: None,
channel_capacity: None,
channel_funding_txo: None,
}
}
#[inline]
fn from_no_close(err: msgs::LightningError) -> Self {
Self { err, chan_id: None, shutdown_finish: None, channel_capacity: None }
Self { err, chan_id: None, shutdown_finish: None, channel_capacity: None, channel_funding_txo: None }
}
#[inline]
fn from_finish_shutdown(err: String, channel_id: ChannelId, user_channel_id: u128, shutdown_res: ShutdownResult, channel_update: Option<msgs::ChannelUpdate>, channel_capacity: u64) -> Self {
fn from_finish_shutdown<SP: Deref>(err: String, channel_id: ChannelId, shutdown_res: ShutdownResult, channel_update: Option<msgs::ChannelUpdate>, channel_context: &ChannelContext<SP>) -> Self
where SP::Target: SignerProvider
{
let err_msg = msgs::ErrorMessage { channel_id, data: err.clone() };
let action = if shutdown_res.monitor_update.is_some() {
// We have a closing `ChannelMonitorUpdate`, which means the channel was funded and we
Expand All @@ -584,9 +588,10 @@ impl MsgHandleErrInternal {
};
Self {
err: LightningError { err, action },
chan_id: Some((channel_id, user_channel_id)),
chan_id: Some((channel_id, channel_context.get_user_id())),
shutdown_finish: Some((shutdown_res, channel_update)),
channel_capacity: Some(channel_capacity)
channel_capacity: Some(channel_context.get_value_satoshis()),
channel_funding_txo: channel_context.get_funding_txo(),
}
}
#[inline]
Expand Down Expand Up @@ -620,6 +625,7 @@ impl MsgHandleErrInternal {
chan_id: None,
shutdown_finish: None,
channel_capacity: None,
channel_funding_txo: None,
}
}

Expand Down Expand Up @@ -1956,7 +1962,7 @@ macro_rules! handle_error {

match $internal {
Ok(msg) => Ok(msg),
Err(MsgHandleErrInternal { err, chan_id, shutdown_finish, channel_capacity }) => {
Err(MsgHandleErrInternal { err, chan_id, shutdown_finish, channel_capacity, channel_funding_txo }) => {
let mut msg_events = Vec::with_capacity(2);

if let Some((shutdown_res, update_option)) = shutdown_finish {
Expand All @@ -1972,6 +1978,7 @@ macro_rules! handle_error {
reason: ClosureReason::ProcessingError { err: err.err.clone() },
counterparty_node_id: Some($counterparty_node_id),
channel_capacity_sats: channel_capacity,
channel_funding_txo: channel_funding_txo,
}, None));
}
}
Expand Down Expand Up @@ -2040,11 +2047,9 @@ macro_rules! convert_chan_phase_err {
log_error!(logger, "Closing channel {} due to close-required error: {}", $channel_id, msg);
update_maps_on_chan_removal!($self, $channel.context);
let shutdown_res = $channel.context.force_shutdown(true);
let user_id = $channel.context.get_user_id();
let channel_capacity_satoshis = $channel.context.get_value_satoshis();

(true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, user_id,
shutdown_res, $channel_update, channel_capacity_satoshis))
(true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id,
shutdown_res, $channel_update, &$channel.context))
},
}
};
Expand Down Expand Up @@ -2718,6 +2723,7 @@ where
reason: closure_reason,
counterparty_node_id: Some(context.get_counterparty_node_id()),
channel_capacity_sats: Some(context.get_value_satoshis()),
channel_funding_txo: context.get_funding_txo(),
}, None));
}

Expand Down Expand Up @@ -3757,11 +3763,9 @@ where
let logger = WithChannelContext::from(&self.logger, &chan.context);
let funding_res = chan.get_funding_created(funding_transaction, funding_txo, is_batch_funding, &&logger)
.map_err(|(mut chan, e)| if let ChannelError::Close(msg) = e {
let channel_id = chan.context.channel_id();
let user_id = chan.context.get_user_id();
let shutdown_res = chan.context.force_shutdown(false);
let channel_capacity = chan.context.get_value_satoshis();
(chan, MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, user_id, shutdown_res, None, channel_capacity))
let err_msg = MsgHandleErrInternal::from_finish_shutdown(msg, chan.context.channel_id(), shutdown_res, None, &chan.context);
(chan, err_msg)
} else { unreachable!(); });
match funding_res {
Ok(funding_msg) => (chan, funding_msg),
Expand Down Expand Up @@ -10308,6 +10312,7 @@ where
reason: ClosureReason::OutdatedChannelManager,
counterparty_node_id: Some(channel.context.get_counterparty_node_id()),
channel_capacity_sats: Some(channel.context.get_value_satoshis()),
channel_funding_txo: channel.context.get_funding_txo(),
}, None));
for (channel_htlc_source, payment_hash) in channel.inflight_htlc_sources() {
let mut found_htlc = false;
Expand Down Expand Up @@ -10361,6 +10366,7 @@ where
reason: ClosureReason::DisconnectedPeer,
counterparty_node_id: Some(channel.context.get_counterparty_node_id()),
channel_capacity_sats: Some(channel.context.get_value_satoshis()),
channel_funding_txo: channel.context.get_funding_txo(),
}, None));
} else {
log_error!(logger, "Missing ChannelMonitor for channel {} needed by ChannelManager.", &channel.context.channel_id());
Expand Down
7 changes: 6 additions & 1 deletion lightning/src/ln/functional_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1534,6 +1534,7 @@ pub struct ExpectedCloseEvent {
pub counterparty_node_id: Option<PublicKey>,
pub discard_funding: bool,
pub reason: Option<ClosureReason>,
pub channel_funding_txo: Option<OutPoint>,
}

impl ExpectedCloseEvent {
Expand All @@ -1544,6 +1545,7 @@ impl ExpectedCloseEvent {
counterparty_node_id: None,
discard_funding,
reason: Some(reason),
channel_funding_txo: None,
}
}
}
Expand All @@ -1562,12 +1564,14 @@ pub fn check_closed_events(node: &Node, expected_close_events: &[ExpectedCloseEv
reason,
counterparty_node_id,
channel_capacity_sats,
channel_funding_txo,
..
} if (
expected_event.channel_id.map(|expected| *channel_id == expected).unwrap_or(true) &&
expected_event.reason.as_ref().map(|expected| reason == expected).unwrap_or(true) &&
expected_event.counterparty_node_id.map(|expected| *counterparty_node_id == Some(expected)).unwrap_or(true) &&
expected_event.channel_capacity_sats.map(|expected| *channel_capacity_sats == Some(expected)).unwrap_or(true)
expected_event.channel_capacity_sats.map(|expected| *channel_capacity_sats == Some(expected)).unwrap_or(true) &&
expected_event.channel_funding_txo.map(|expected| *channel_funding_txo == Some(expected)).unwrap_or(true)
)
)));
}
Expand All @@ -1592,6 +1596,7 @@ pub fn check_closed_event(node: &Node, events_count: usize, expected_reason: Clo
counterparty_node_id: Some(*node_id),
discard_funding: is_check_discard_funding,
reason: Some(expected_reason.clone()),
channel_funding_txo: None,
}).collect::<Vec<_>>();
check_closed_events(node, expected_close_events.as_slice());
}
Expand Down
16 changes: 12 additions & 4 deletions lightning/src/ln/functional_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10596,17 +10596,21 @@ fn test_disconnect_in_funding_batch() {
nodes[0].node.peer_disconnected(&nodes[2].node.get_our_node_id());

// The channels in the batch will close immediately.
let channel_id_1 = OutPoint { txid: tx.txid(), index: 0 }.to_channel_id();
let channel_id_2 = OutPoint { txid: tx.txid(), index: 1 }.to_channel_id();
let funding_txo_1 = OutPoint { txid: tx.txid(), index: 0 };
let funding_txo_2 = OutPoint { txid: tx.txid(), index: 1 };
let channel_id_1 = funding_txo_1.to_channel_id();
let channel_id_2 = funding_txo_2.to_channel_id();
check_closed_events(&nodes[0], &[
ExpectedCloseEvent {
channel_id: Some(channel_id_1),
discard_funding: true,
channel_funding_txo: Some(funding_txo_1),
..Default::default()
},
ExpectedCloseEvent {
channel_id: Some(channel_id_2),
discard_funding: true,
channel_funding_txo: Some(funding_txo_2),
..Default::default()
},
]);
Expand Down Expand Up @@ -10664,8 +10668,10 @@ fn test_batch_funding_close_after_funding_signed() {
assert_eq!(nodes[0].tx_broadcaster.txn_broadcast().len(), 0);

// Force-close the channel for which we've completed the initial monitor.
let channel_id_1 = OutPoint { txid: tx.txid(), index: 0 }.to_channel_id();
let channel_id_2 = OutPoint { txid: tx.txid(), index: 1 }.to_channel_id();
let funding_txo_1 = OutPoint { txid: tx.txid(), index: 0 };
let funding_txo_2 = OutPoint { txid: tx.txid(), index: 1 };
let channel_id_1 = funding_txo_1.to_channel_id();
let channel_id_2 = funding_txo_2.to_channel_id();
nodes[0].node.force_close_broadcasting_latest_txn(&channel_id_1, &nodes[1].node.get_our_node_id()).unwrap();
check_added_monitors(&nodes[0], 2);
{
Expand Down Expand Up @@ -10697,11 +10703,13 @@ fn test_batch_funding_close_after_funding_signed() {
ExpectedCloseEvent {
channel_id: Some(channel_id_1),
discard_funding: true,
channel_funding_txo: Some(funding_txo_1),
..Default::default()
},
ExpectedCloseEvent {
channel_id: Some(channel_id_2),
discard_funding: true,
channel_funding_txo: Some(funding_txo_2),
..Default::default()
},
]);
Expand Down

0 comments on commit 11fe6ae

Please sign in to comment.