Skip to content

Commit efaba11

Browse files
committed
Add channel funding txo to Channel Event::ChannelClosed
1 parent 7bc2a14 commit efaba11

File tree

5 files changed

+50
-9
lines changed

5 files changed

+50
-9
lines changed

lightning/src/events/mod.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use crate::ln::channel::FUNDING_CONF_DEADLINE_BLOCKS;
2424
use crate::ln::features::ChannelTypeFeatures;
2525
use crate::ln::msgs;
2626
use crate::ln::{ChannelId, PaymentPreimage, PaymentHash, PaymentSecret};
27+
use crate::chain::transaction;
2728
use crate::routing::gossip::NetworkUpdate;
2829
use crate::util::errors::APIError;
2930
use crate::util::ser::{BigSize, FixedLengthReader, Writeable, Writer, MaybeReadable, Readable, RequiredWrapper, UpgradableRequired, WithoutLength};
@@ -861,7 +862,7 @@ pub enum Event {
861862
///
862863
/// [`ChannelManager::accept_inbound_channel`]: crate::ln::channelmanager::ChannelManager::accept_inbound_channel
863864
/// [`UserConfig::manually_accept_inbound_channels`]: crate::util::config::UserConfig::manually_accept_inbound_channels
864-
ChannelClosed {
865+
ChannelClosed {
865866
/// The `channel_id` of the channel which has been closed. Note that on-chain transactions
866867
/// resolving the channel are likely still awaiting confirmation.
867868
channel_id: ChannelId,
@@ -886,6 +887,10 @@ 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
891+
/// status of the closing tx.
892+
/// Note that for instances serialized in v0.0.119 or prior this will be missing (None).
893+
channel_funding_txo: Option<transaction::OutPoint>,
889894
},
890895
/// Used to indicate to the user that they can abandon the funding transaction and recycle the
891896
/// inputs for another purpose.
@@ -1091,7 +1096,7 @@ impl Writeable for Event {
10911096
});
10921097
},
10931098
&Event::ChannelClosed { ref channel_id, ref user_channel_id, ref reason,
1094-
ref counterparty_node_id, ref channel_capacity_sats
1099+
ref counterparty_node_id, ref channel_capacity_sats, ref channel_funding_txo
10951100
} => {
10961101
9u8.write(writer)?;
10971102
// `user_channel_id` used to be a single u64 value. In order to remain backwards
@@ -1106,6 +1111,7 @@ impl Writeable for Event {
11061111
(3, user_channel_id_high, required),
11071112
(5, counterparty_node_id, option),
11081113
(7, channel_capacity_sats, option),
1114+
(9, channel_funding_txo, option),
11091115
});
11101116
},
11111117
&Event::DiscardFunding { ref channel_id, ref transaction } => {
@@ -1405,13 +1411,15 @@ impl MaybeReadable for Event {
14051411
let mut user_channel_id_high_opt: Option<u64> = None;
14061412
let mut counterparty_node_id = None;
14071413
let mut channel_capacity_sats = None;
1414+
let mut channel_funding_txo = None;
14081415
read_tlv_fields!(reader, {
14091416
(0, channel_id, required),
14101417
(1, user_channel_id_low_opt, option),
14111418
(2, reason, upgradable_required),
14121419
(3, user_channel_id_high_opt, option),
14131420
(5, counterparty_node_id, option),
14141421
(7, channel_capacity_sats, option),
1422+
(9, channel_funding_txo, option),
14151423
});
14161424

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

14231431
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 }))
1432+
counterparty_node_id, channel_capacity_sats, channel_funding_txo }))
14251433
};
14261434
f()
14271435
},

lightning/src/ln/channel.rs

+4
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,7 @@ pub(crate) struct ShutdownResult {
827827
pub(crate) channel_capacity_satoshis: u64,
828828
pub(crate) counterparty_node_id: PublicKey,
829829
pub(crate) unbroadcasted_funding_tx: Option<Transaction>,
830+
pub(crate) channel_funding_txo: Option<OutPoint>,
830831
}
831832

832833
/// If the majority of the channels funds are to the fundee and the initiator holds only just
@@ -2420,6 +2421,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
24202421
channel_capacity_satoshis: self.channel_value_satoshis,
24212422
counterparty_node_id: self.counterparty_node_id,
24222423
unbroadcasted_funding_tx,
2424+
channel_funding_txo: self.get_funding_txo(),
24232425
}
24242426
}
24252427

@@ -4956,6 +4958,7 @@ impl<SP: Deref> Channel<SP> where
49564958
channel_capacity_satoshis: self.context.channel_value_satoshis,
49574959
counterparty_node_id: self.context.counterparty_node_id,
49584960
unbroadcasted_funding_tx: self.context.unbroadcasted_funding(),
4961+
channel_funding_txo: self.context.get_funding_txo(),
49594962
};
49604963
let tx = self.build_signed_closing_transaction(&mut closing_tx, &msg.signature, &sig);
49614964
self.context.channel_state = ChannelState::ShutdownComplete;
@@ -4990,6 +4993,7 @@ impl<SP: Deref> Channel<SP> where
49904993
channel_capacity_satoshis: self.context.channel_value_satoshis,
49914994
counterparty_node_id: self.context.counterparty_node_id,
49924995
unbroadcasted_funding_tx: self.context.unbroadcasted_funding(),
4996+
channel_funding_txo: self.context.get_funding_txo(),
49934997
};
49944998
self.context.channel_state = ChannelState::ShutdownComplete;
49954999
self.context.update_time_counter += 1;

lightning/src/ln/channelmanager.rs

+3
Original file line numberDiff line numberDiff line change
@@ -2878,6 +2878,7 @@ where
28782878
reason: shutdown_res.closure_reason,
28792879
counterparty_node_id: Some(shutdown_res.counterparty_node_id),
28802880
channel_capacity_sats: Some(shutdown_res.channel_capacity_satoshis),
2881+
channel_funding_txo: shutdown_res.channel_funding_txo,
28812882
}, None));
28822883

28832884
if let Some(transaction) = shutdown_res.unbroadcasted_funding_tx {
@@ -10304,6 +10305,7 @@ where
1030410305
reason: ClosureReason::OutdatedChannelManager,
1030510306
counterparty_node_id: Some(channel.context.get_counterparty_node_id()),
1030610307
channel_capacity_sats: Some(channel.context.get_value_satoshis()),
10308+
channel_funding_txo: channel.context.get_funding_txo(),
1030710309
}, None));
1030810310
for (channel_htlc_source, payment_hash) in channel.inflight_htlc_sources() {
1030910311
let mut found_htlc = false;
@@ -10357,6 +10359,7 @@ where
1035710359
reason: ClosureReason::DisconnectedPeer,
1035810360
counterparty_node_id: Some(channel.context.get_counterparty_node_id()),
1035910361
channel_capacity_sats: Some(channel.context.get_value_satoshis()),
10362+
channel_funding_txo: channel.context.get_funding_txo(),
1036010363
}, None));
1036110364
} else {
1036210365
log_error!(logger, "Missing ChannelMonitor for channel {} needed by ChannelManager.", &channel.context.channel_id());

lightning/src/ln/functional_test_utils.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -1539,6 +1539,8 @@ pub struct ExpectedCloseEvent {
15391539
pub counterparty_node_id: Option<PublicKey>,
15401540
pub discard_funding: bool,
15411541
pub reason: Option<ClosureReason>,
1542+
pub channel_funding_txo: Option<OutPoint>,
1543+
pub user_channel_id: Option<u128>,
15421544
}
15431545

15441546
impl ExpectedCloseEvent {
@@ -1549,6 +1551,8 @@ impl ExpectedCloseEvent {
15491551
counterparty_node_id: None,
15501552
discard_funding,
15511553
reason: Some(reason),
1554+
channel_funding_txo: None,
1555+
user_channel_id: None,
15521556
}
15531557
}
15541558
}
@@ -1567,12 +1571,20 @@ pub fn check_closed_events(node: &Node, expected_close_events: &[ExpectedCloseEv
15671571
reason,
15681572
counterparty_node_id,
15691573
channel_capacity_sats,
1574+
channel_funding_txo,
1575+
user_channel_id,
15701576
..
15711577
} if (
15721578
expected_event.channel_id.map(|expected| *channel_id == expected).unwrap_or(true) &&
15731579
expected_event.reason.as_ref().map(|expected| reason == expected).unwrap_or(true) &&
1574-
expected_event.counterparty_node_id.map(|expected| *counterparty_node_id == Some(expected)).unwrap_or(true) &&
1575-
expected_event.channel_capacity_sats.map(|expected| *channel_capacity_sats == Some(expected)).unwrap_or(true)
1580+
expected_event.
1581+
counterparty_node_id.map(|expected| *counterparty_node_id == Some(expected)).unwrap_or(true) &&
1582+
expected_event.channel_capacity_sats
1583+
.map(|expected| *channel_capacity_sats == Some(expected)).unwrap_or(true) &&
1584+
expected_event.channel_funding_txo
1585+
.map(|expected| *channel_funding_txo == Some(expected)).unwrap_or(true) &&
1586+
expected_event.user_channel_id
1587+
.map(|expected| *user_channel_id == expected).unwrap_or(true)
15761588
)
15771589
)));
15781590
}
@@ -1597,6 +1609,8 @@ pub fn check_closed_event(node: &Node, events_count: usize, expected_reason: Clo
15971609
counterparty_node_id: Some(*node_id),
15981610
discard_funding: is_check_discard_funding,
15991611
reason: Some(expected_reason.clone()),
1612+
channel_funding_txo: None,
1613+
user_channel_id: None,
16001614
}).collect::<Vec<_>>();
16011615
check_closed_events(node, expected_close_events.as_slice());
16021616
}

lightning/src/ln/functional_tests.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -10687,17 +10687,23 @@ fn test_disconnect_in_funding_batch() {
1068710687
nodes[0].node.peer_disconnected(&nodes[2].node.get_our_node_id());
1068810688

1068910689
// The channels in the batch will close immediately.
10690-
let channel_id_1 = OutPoint { txid: tx.txid(), index: 0 }.to_channel_id();
10691-
let channel_id_2 = OutPoint { txid: tx.txid(), index: 1 }.to_channel_id();
10690+
let funding_txo_1 = OutPoint { txid: tx.txid(), index: 0 };
10691+
let funding_txo_2 = OutPoint { txid: tx.txid(), index: 1 };
10692+
let channel_id_1 = funding_txo_1.to_channel_id();
10693+
let channel_id_2 = funding_txo_2.to_channel_id();
1069210694
check_closed_events(&nodes[0], &[
1069310695
ExpectedCloseEvent {
1069410696
channel_id: Some(channel_id_1),
1069510697
discard_funding: true,
10698+
channel_funding_txo: Some(funding_txo_1),
10699+
user_channel_id: Some(42),
1069610700
..Default::default()
1069710701
},
1069810702
ExpectedCloseEvent {
1069910703
channel_id: Some(channel_id_2),
1070010704
discard_funding: true,
10705+
channel_funding_txo: Some(funding_txo_2),
10706+
user_channel_id: Some(43),
1070110707
..Default::default()
1070210708
},
1070310709
]);
@@ -10755,8 +10761,10 @@ fn test_batch_funding_close_after_funding_signed() {
1075510761
assert_eq!(nodes[0].tx_broadcaster.txn_broadcast().len(), 0);
1075610762

1075710763
// Force-close the channel for which we've completed the initial monitor.
10758-
let channel_id_1 = OutPoint { txid: tx.txid(), index: 0 }.to_channel_id();
10759-
let channel_id_2 = OutPoint { txid: tx.txid(), index: 1 }.to_channel_id();
10764+
let funding_txo_1 = OutPoint { txid: tx.txid(), index: 0 };
10765+
let funding_txo_2 = OutPoint { txid: tx.txid(), index: 1 };
10766+
let channel_id_1 = funding_txo_1.to_channel_id();
10767+
let channel_id_2 = funding_txo_2.to_channel_id();
1076010768
nodes[0].node.force_close_broadcasting_latest_txn(&channel_id_1, &nodes[1].node.get_our_node_id()).unwrap();
1076110769
check_added_monitors(&nodes[0], 2);
1076210770
{
@@ -10788,11 +10796,15 @@ fn test_batch_funding_close_after_funding_signed() {
1078810796
ExpectedCloseEvent {
1078910797
channel_id: Some(channel_id_1),
1079010798
discard_funding: true,
10799+
channel_funding_txo: Some(funding_txo_1),
10800+
user_channel_id: Some(42),
1079110801
..Default::default()
1079210802
},
1079310803
ExpectedCloseEvent {
1079410804
channel_id: Some(channel_id_2),
1079510805
discard_funding: true,
10806+
channel_funding_txo: Some(funding_txo_2),
10807+
user_channel_id: Some(43),
1079610808
..Default::default()
1079710809
},
1079810810
]);

0 commit comments

Comments
 (0)