Skip to content

Commit 11fe6ae

Browse files
committed
Add channel funding txo to Channel Event::ChannelClosed
1 parent 4deb263 commit 11fe6ae

File tree

4 files changed

+45
-20
lines changed

4 files changed

+45
-20
lines changed

lightning/src/events/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub use bump_transaction::BumpTransactionEvent;
2121
use crate::sign::SpendableOutputDescriptor;
2222
use crate::ln::channelmanager::{InterceptId, PaymentId, RecipientOnionFields};
2323
use crate::ln::channel::FUNDING_CONF_DEADLINE_BLOCKS;
24+
use crate::chain::transaction;
2425
use crate::ln::features::ChannelTypeFeatures;
2526
use crate::ln::msgs;
2627
use crate::ln::{ChannelId, PaymentPreimage, PaymentHash, PaymentSecret};
@@ -886,6 +887,8 @@ pub enum Event {
886887
///
887888
/// This field will be `None` for objects serialized prior to LDK 0.0.117.
888889
channel_capacity_sats: Option<u64>,
890+
/// The original channel funding TXO; this helps checking for the existence and confirmation status of the closing tx
891+
channel_funding_txo: Option<transaction::OutPoint>,
889892
},
890893
/// Used to indicate to the user that they can abandon the funding transaction and recycle the
891894
/// inputs for another purpose.
@@ -1091,7 +1094,7 @@ impl Writeable for Event {
10911094
});
10921095
},
10931096
&Event::ChannelClosed { ref channel_id, ref user_channel_id, ref reason,
1094-
ref counterparty_node_id, ref channel_capacity_sats
1097+
ref counterparty_node_id, ref channel_capacity_sats, ref channel_funding_txo
10951098
} => {
10961099
9u8.write(writer)?;
10971100
// `user_channel_id` used to be a single u64 value. In order to remain backwards
@@ -1106,6 +1109,7 @@ impl Writeable for Event {
11061109
(3, user_channel_id_high, required),
11071110
(5, counterparty_node_id, option),
11081111
(7, channel_capacity_sats, option),
1112+
(9, channel_funding_txo, option),
11091113
});
11101114
},
11111115
&Event::DiscardFunding { ref channel_id, ref transaction } => {
@@ -1405,13 +1409,15 @@ impl MaybeReadable for Event {
14051409
let mut user_channel_id_high_opt: Option<u64> = None;
14061410
let mut counterparty_node_id = None;
14071411
let mut channel_capacity_sats = None;
1412+
let mut channel_funding_txo = None;
14081413
read_tlv_fields!(reader, {
14091414
(0, channel_id, required),
14101415
(1, user_channel_id_low_opt, option),
14111416
(2, reason, upgradable_required),
14121417
(3, user_channel_id_high_opt, option),
14131418
(5, counterparty_node_id, option),
14141419
(7, channel_capacity_sats, option),
1420+
(9, channel_funding_txo, option),
14151421
});
14161422

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

14231429
Ok(Some(Event::ChannelClosed { channel_id, user_channel_id, reason: _init_tlv_based_struct_field!(reason, upgradable_required),
1424-
counterparty_node_id, channel_capacity_sats }))
1430+
counterparty_node_id, channel_capacity_sats, channel_funding_txo }))
14251431
};
14261432
f()
14271433
},

lightning/src/ln/channelmanager.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,7 @@ struct MsgHandleErrInternal {
548548
chan_id: Option<(ChannelId, u128)>, // If Some a channel of ours has been closed
549549
shutdown_finish: Option<(ShutdownResult, Option<msgs::ChannelUpdate>)>,
550550
channel_capacity: Option<u64>,
551+
channel_funding_txo: Option<OutPoint>,
551552
}
552553
impl MsgHandleErrInternal {
553554
#[inline]
@@ -565,14 +566,17 @@ impl MsgHandleErrInternal {
565566
chan_id: None,
566567
shutdown_finish: None,
567568
channel_capacity: None,
569+
channel_funding_txo: None,
568570
}
569571
}
570572
#[inline]
571573
fn from_no_close(err: msgs::LightningError) -> Self {
572-
Self { err, chan_id: None, shutdown_finish: None, channel_capacity: None }
574+
Self { err, chan_id: None, shutdown_finish: None, channel_capacity: None, channel_funding_txo: None }
573575
}
574576
#[inline]
575-
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 {
577+
fn from_finish_shutdown<SP: Deref>(err: String, channel_id: ChannelId, shutdown_res: ShutdownResult, channel_update: Option<msgs::ChannelUpdate>, channel_context: &ChannelContext<SP>) -> Self
578+
where SP::Target: SignerProvider
579+
{
576580
let err_msg = msgs::ErrorMessage { channel_id, data: err.clone() };
577581
let action = if shutdown_res.monitor_update.is_some() {
578582
// We have a closing `ChannelMonitorUpdate`, which means the channel was funded and we
@@ -584,9 +588,10 @@ impl MsgHandleErrInternal {
584588
};
585589
Self {
586590
err: LightningError { err, action },
587-
chan_id: Some((channel_id, user_channel_id)),
591+
chan_id: Some((channel_id, channel_context.get_user_id())),
588592
shutdown_finish: Some((shutdown_res, channel_update)),
589-
channel_capacity: Some(channel_capacity)
593+
channel_capacity: Some(channel_context.get_value_satoshis()),
594+
channel_funding_txo: channel_context.get_funding_txo(),
590595
}
591596
}
592597
#[inline]
@@ -620,6 +625,7 @@ impl MsgHandleErrInternal {
620625
chan_id: None,
621626
shutdown_finish: None,
622627
channel_capacity: None,
628+
channel_funding_txo: None,
623629
}
624630
}
625631

@@ -1956,7 +1962,7 @@ macro_rules! handle_error {
19561962

19571963
match $internal {
19581964
Ok(msg) => Ok(msg),
1959-
Err(MsgHandleErrInternal { err, chan_id, shutdown_finish, channel_capacity }) => {
1965+
Err(MsgHandleErrInternal { err, chan_id, shutdown_finish, channel_capacity, channel_funding_txo }) => {
19601966
let mut msg_events = Vec::with_capacity(2);
19611967

19621968
if let Some((shutdown_res, update_option)) = shutdown_finish {
@@ -1972,6 +1978,7 @@ macro_rules! handle_error {
19721978
reason: ClosureReason::ProcessingError { err: err.err.clone() },
19731979
counterparty_node_id: Some($counterparty_node_id),
19741980
channel_capacity_sats: channel_capacity,
1981+
channel_funding_txo: channel_funding_txo,
19751982
}, None));
19761983
}
19771984
}
@@ -2040,11 +2047,9 @@ macro_rules! convert_chan_phase_err {
20402047
log_error!(logger, "Closing channel {} due to close-required error: {}", $channel_id, msg);
20412048
update_maps_on_chan_removal!($self, $channel.context);
20422049
let shutdown_res = $channel.context.force_shutdown(true);
2043-
let user_id = $channel.context.get_user_id();
2044-
let channel_capacity_satoshis = $channel.context.get_value_satoshis();
20452050

2046-
(true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, user_id,
2047-
shutdown_res, $channel_update, channel_capacity_satoshis))
2051+
(true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id,
2052+
shutdown_res, $channel_update, &$channel.context))
20482053
},
20492054
}
20502055
};
@@ -2718,6 +2723,7 @@ where
27182723
reason: closure_reason,
27192724
counterparty_node_id: Some(context.get_counterparty_node_id()),
27202725
channel_capacity_sats: Some(context.get_value_satoshis()),
2726+
channel_funding_txo: context.get_funding_txo(),
27212727
}, None));
27222728
}
27232729

@@ -3757,11 +3763,9 @@ where
37573763
let logger = WithChannelContext::from(&self.logger, &chan.context);
37583764
let funding_res = chan.get_funding_created(funding_transaction, funding_txo, is_batch_funding, &&logger)
37593765
.map_err(|(mut chan, e)| if let ChannelError::Close(msg) = e {
3760-
let channel_id = chan.context.channel_id();
3761-
let user_id = chan.context.get_user_id();
37623766
let shutdown_res = chan.context.force_shutdown(false);
3763-
let channel_capacity = chan.context.get_value_satoshis();
3764-
(chan, MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, user_id, shutdown_res, None, channel_capacity))
3767+
let err_msg = MsgHandleErrInternal::from_finish_shutdown(msg, chan.context.channel_id(), shutdown_res, None, &chan.context);
3768+
(chan, err_msg)
37653769
} else { unreachable!(); });
37663770
match funding_res {
37673771
Ok(funding_msg) => (chan, funding_msg),
@@ -10308,6 +10312,7 @@ where
1030810312
reason: ClosureReason::OutdatedChannelManager,
1030910313
counterparty_node_id: Some(channel.context.get_counterparty_node_id()),
1031010314
channel_capacity_sats: Some(channel.context.get_value_satoshis()),
10315+
channel_funding_txo: channel.context.get_funding_txo(),
1031110316
}, None));
1031210317
for (channel_htlc_source, payment_hash) in channel.inflight_htlc_sources() {
1031310318
let mut found_htlc = false;
@@ -10361,6 +10366,7 @@ where
1036110366
reason: ClosureReason::DisconnectedPeer,
1036210367
counterparty_node_id: Some(channel.context.get_counterparty_node_id()),
1036310368
channel_capacity_sats: Some(channel.context.get_value_satoshis()),
10369+
channel_funding_txo: channel.context.get_funding_txo(),
1036410370
}, None));
1036510371
} else {
1036610372
log_error!(logger, "Missing ChannelMonitor for channel {} needed by ChannelManager.", &channel.context.channel_id());

lightning/src/ln/functional_test_utils.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1534,6 +1534,7 @@ pub struct ExpectedCloseEvent {
15341534
pub counterparty_node_id: Option<PublicKey>,
15351535
pub discard_funding: bool,
15361536
pub reason: Option<ClosureReason>,
1537+
pub channel_funding_txo: Option<OutPoint>,
15371538
}
15381539

15391540
impl ExpectedCloseEvent {
@@ -1544,6 +1545,7 @@ impl ExpectedCloseEvent {
15441545
counterparty_node_id: None,
15451546
discard_funding,
15461547
reason: Some(reason),
1548+
channel_funding_txo: None,
15471549
}
15481550
}
15491551
}
@@ -1562,12 +1564,14 @@ pub fn check_closed_events(node: &Node, expected_close_events: &[ExpectedCloseEv
15621564
reason,
15631565
counterparty_node_id,
15641566
channel_capacity_sats,
1567+
channel_funding_txo,
15651568
..
15661569
} if (
15671570
expected_event.channel_id.map(|expected| *channel_id == expected).unwrap_or(true) &&
15681571
expected_event.reason.as_ref().map(|expected| reason == expected).unwrap_or(true) &&
15691572
expected_event.counterparty_node_id.map(|expected| *counterparty_node_id == Some(expected)).unwrap_or(true) &&
1570-
expected_event.channel_capacity_sats.map(|expected| *channel_capacity_sats == Some(expected)).unwrap_or(true)
1573+
expected_event.channel_capacity_sats.map(|expected| *channel_capacity_sats == Some(expected)).unwrap_or(true) &&
1574+
expected_event.channel_funding_txo.map(|expected| *channel_funding_txo == Some(expected)).unwrap_or(true)
15711575
)
15721576
)));
15731577
}
@@ -1592,6 +1596,7 @@ pub fn check_closed_event(node: &Node, events_count: usize, expected_reason: Clo
15921596
counterparty_node_id: Some(*node_id),
15931597
discard_funding: is_check_discard_funding,
15941598
reason: Some(expected_reason.clone()),
1599+
channel_funding_txo: None,
15951600
}).collect::<Vec<_>>();
15961601
check_closed_events(node, expected_close_events.as_slice());
15971602
}

lightning/src/ln/functional_tests.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10596,17 +10596,21 @@ fn test_disconnect_in_funding_batch() {
1059610596
nodes[0].node.peer_disconnected(&nodes[2].node.get_our_node_id());
1059710597

1059810598
// The channels in the batch will close immediately.
10599-
let channel_id_1 = OutPoint { txid: tx.txid(), index: 0 }.to_channel_id();
10600-
let channel_id_2 = OutPoint { txid: tx.txid(), index: 1 }.to_channel_id();
10599+
let funding_txo_1 = OutPoint { txid: tx.txid(), index: 0 };
10600+
let funding_txo_2 = OutPoint { txid: tx.txid(), index: 1 };
10601+
let channel_id_1 = funding_txo_1.to_channel_id();
10602+
let channel_id_2 = funding_txo_2.to_channel_id();
1060110603
check_closed_events(&nodes[0], &[
1060210604
ExpectedCloseEvent {
1060310605
channel_id: Some(channel_id_1),
1060410606
discard_funding: true,
10607+
channel_funding_txo: Some(funding_txo_1),
1060510608
..Default::default()
1060610609
},
1060710610
ExpectedCloseEvent {
1060810611
channel_id: Some(channel_id_2),
1060910612
discard_funding: true,
10613+
channel_funding_txo: Some(funding_txo_2),
1061010614
..Default::default()
1061110615
},
1061210616
]);
@@ -10664,8 +10668,10 @@ fn test_batch_funding_close_after_funding_signed() {
1066410668
assert_eq!(nodes[0].tx_broadcaster.txn_broadcast().len(), 0);
1066510669

1066610670
// Force-close the channel for which we've completed the initial monitor.
10667-
let channel_id_1 = OutPoint { txid: tx.txid(), index: 0 }.to_channel_id();
10668-
let channel_id_2 = OutPoint { txid: tx.txid(), index: 1 }.to_channel_id();
10671+
let funding_txo_1 = OutPoint { txid: tx.txid(), index: 0 };
10672+
let funding_txo_2 = OutPoint { txid: tx.txid(), index: 1 };
10673+
let channel_id_1 = funding_txo_1.to_channel_id();
10674+
let channel_id_2 = funding_txo_2.to_channel_id();
1066910675
nodes[0].node.force_close_broadcasting_latest_txn(&channel_id_1, &nodes[1].node.get_our_node_id()).unwrap();
1067010676
check_added_monitors(&nodes[0], 2);
1067110677
{
@@ -10697,11 +10703,13 @@ fn test_batch_funding_close_after_funding_signed() {
1069710703
ExpectedCloseEvent {
1069810704
channel_id: Some(channel_id_1),
1069910705
discard_funding: true,
10706+
channel_funding_txo: Some(funding_txo_1),
1070010707
..Default::default()
1070110708
},
1070210709
ExpectedCloseEvent {
1070310710
channel_id: Some(channel_id_2),
1070410711
discard_funding: true,
10712+
channel_funding_txo: Some(funding_txo_2),
1070510713
..Default::default()
1070610714
},
1070710715
]);

0 commit comments

Comments
 (0)