Skip to content

Commit 4b70921

Browse files
authored
Merge pull request #2800 from optout21/channel-close-add-funding
Add channel funding txo to Channel Event::ChannelClosed
2 parents cd5f09b + efaba11 commit 4b70921

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
@@ -2415,6 +2416,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
24152416
channel_capacity_satoshis: self.channel_value_satoshis,
24162417
counterparty_node_id: self.counterparty_node_id,
24172418
unbroadcasted_funding_tx,
2419+
channel_funding_txo: self.get_funding_txo(),
24182420
}
24192421
}
24202422

@@ -4943,6 +4945,7 @@ impl<SP: Deref> Channel<SP> where
49434945
channel_capacity_satoshis: self.context.channel_value_satoshis,
49444946
counterparty_node_id: self.context.counterparty_node_id,
49454947
unbroadcasted_funding_tx: self.context.unbroadcasted_funding(),
4948+
channel_funding_txo: self.context.get_funding_txo(),
49464949
};
49474950
let tx = self.build_signed_closing_transaction(&mut closing_tx, &msg.signature, &sig);
49484951
self.context.channel_state = ChannelState::ShutdownComplete;
@@ -4977,6 +4980,7 @@ impl<SP: Deref> Channel<SP> where
49774980
channel_capacity_satoshis: self.context.channel_value_satoshis,
49784981
counterparty_node_id: self.context.counterparty_node_id,
49794982
unbroadcasted_funding_tx: self.context.unbroadcasted_funding(),
4983+
channel_funding_txo: self.context.get_funding_txo(),
49804984
};
49814985
self.context.channel_state = ChannelState::ShutdownComplete;
49824986
self.context.update_time_counter += 1;

lightning/src/ln/channelmanager.rs

+3
Original file line numberDiff line numberDiff line change
@@ -2884,6 +2884,7 @@ where
28842884
reason: shutdown_res.closure_reason,
28852885
counterparty_node_id: Some(shutdown_res.counterparty_node_id),
28862886
channel_capacity_sats: Some(shutdown_res.channel_capacity_satoshis),
2887+
channel_funding_txo: shutdown_res.channel_funding_txo,
28872888
}, None));
28882889

28892890
if let Some(transaction) = shutdown_res.unbroadcasted_funding_tx {
@@ -10327,6 +10328,7 @@ where
1032710328
reason: ClosureReason::OutdatedChannelManager,
1032810329
counterparty_node_id: Some(channel.context.get_counterparty_node_id()),
1032910330
channel_capacity_sats: Some(channel.context.get_value_satoshis()),
10331+
channel_funding_txo: channel.context.get_funding_txo(),
1033010332
}, None));
1033110333
for (channel_htlc_source, payment_hash) in channel.inflight_htlc_sources() {
1033210334
let mut found_htlc = false;
@@ -10380,6 +10382,7 @@ where
1038010382
reason: ClosureReason::DisconnectedPeer,
1038110383
counterparty_node_id: Some(channel.context.get_counterparty_node_id()),
1038210384
channel_capacity_sats: Some(channel.context.get_value_satoshis()),
10385+
channel_funding_txo: channel.context.get_funding_txo(),
1038310386
}, None));
1038410387
} else {
1038510388
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
@@ -10689,17 +10689,23 @@ fn test_disconnect_in_funding_batch() {
1068910689
nodes[0].node.peer_disconnected(&nodes[2].node.get_our_node_id());
1069010690

1069110691
// The channels in the batch will close immediately.
10692-
let channel_id_1 = OutPoint { txid: tx.txid(), index: 0 }.to_channel_id();
10693-
let channel_id_2 = OutPoint { txid: tx.txid(), index: 1 }.to_channel_id();
10692+
let funding_txo_1 = OutPoint { txid: tx.txid(), index: 0 };
10693+
let funding_txo_2 = OutPoint { txid: tx.txid(), index: 1 };
10694+
let channel_id_1 = funding_txo_1.to_channel_id();
10695+
let channel_id_2 = funding_txo_2.to_channel_id();
1069410696
check_closed_events(&nodes[0], &[
1069510697
ExpectedCloseEvent {
1069610698
channel_id: Some(channel_id_1),
1069710699
discard_funding: true,
10700+
channel_funding_txo: Some(funding_txo_1),
10701+
user_channel_id: Some(42),
1069810702
..Default::default()
1069910703
},
1070010704
ExpectedCloseEvent {
1070110705
channel_id: Some(channel_id_2),
1070210706
discard_funding: true,
10707+
channel_funding_txo: Some(funding_txo_2),
10708+
user_channel_id: Some(43),
1070310709
..Default::default()
1070410710
},
1070510711
]);
@@ -10757,8 +10763,10 @@ fn test_batch_funding_close_after_funding_signed() {
1075710763
assert_eq!(nodes[0].tx_broadcaster.txn_broadcast().len(), 0);
1075810764

1075910765
// Force-close the channel for which we've completed the initial monitor.
10760-
let channel_id_1 = OutPoint { txid: tx.txid(), index: 0 }.to_channel_id();
10761-
let channel_id_2 = OutPoint { txid: tx.txid(), index: 1 }.to_channel_id();
10766+
let funding_txo_1 = OutPoint { txid: tx.txid(), index: 0 };
10767+
let funding_txo_2 = OutPoint { txid: tx.txid(), index: 1 };
10768+
let channel_id_1 = funding_txo_1.to_channel_id();
10769+
let channel_id_2 = funding_txo_2.to_channel_id();
1076210770
nodes[0].node.force_close_broadcasting_latest_txn(&channel_id_1, &nodes[1].node.get_our_node_id()).unwrap();
1076310771
check_added_monitors(&nodes[0], 2);
1076410772
{
@@ -10790,11 +10798,15 @@ fn test_batch_funding_close_after_funding_signed() {
1079010798
ExpectedCloseEvent {
1079110799
channel_id: Some(channel_id_1),
1079210800
discard_funding: true,
10801+
channel_funding_txo: Some(funding_txo_1),
10802+
user_channel_id: Some(42),
1079310803
..Default::default()
1079410804
},
1079510805
ExpectedCloseEvent {
1079610806
channel_id: Some(channel_id_2),
1079710807
discard_funding: true,
10808+
channel_funding_txo: Some(funding_txo_2),
10809+
user_channel_id: Some(43),
1079810810
..Default::default()
1079910811
},
1080010812
]);

0 commit comments

Comments
 (0)