Skip to content

Commit 1955008

Browse files
committed
Adds DiscardFunding event
During the event of a channel close, if the funding transaction is yet to be broadcasted then a DiscardFunding event is issued along with the ChannelClose event.
1 parent 367a2cc commit 1955008

File tree

5 files changed

+66
-23
lines changed

5 files changed

+66
-23
lines changed

lightning/src/ln/channel.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1901,6 +1901,15 @@ impl<Signer: Sign> Channel<Signer> {
19011901
Ok(())
19021902
}
19031903

1904+
/// Returns transaction if there is pending funding transaction that is yet to broadcast
1905+
pub fn unbroadcasted_funding(&self) -> Option<Transaction> {
1906+
if self.channel_state & (ChannelState::FundingCreated as u32) != 0 {
1907+
self.funding_transaction.clone()
1908+
} else {
1909+
None
1910+
}
1911+
}
1912+
19041913
/// Returns a HTLCStats about inbound pending htlcs
19051914
fn get_inbound_pending_htlc_stats(&self) -> HTLCStats {
19061915
let mut stats = HTLCStats {

lightning/src/ln/channelmanager.rs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,6 +1326,18 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
13261326
self.list_channels_with_filter(|&(_, ref channel)| channel.is_live())
13271327
}
13281328

1329+
/// Helper function that issues the channel close events
1330+
fn issue_channel_close_events(&self, channel: &Channel<Signer>, closure_reason: ClosureReason) {
1331+
let mut pending_events_lock = self.pending_events.lock().unwrap();
1332+
match channel.unbroadcasted_funding() {
1333+
Some(transaction) => {
1334+
pending_events_lock.push(events::Event::DiscardFunding { channel_id: channel.channel_id(), transaction })
1335+
},
1336+
None => {},
1337+
}
1338+
pending_events_lock.push(events::Event::ChannelClosed { channel_id: channel.channel_id(), reason: closure_reason });
1339+
}
1340+
13291341
fn close_channel_internal(&self, channel_id: &[u8; 32], target_feerate_sats_per_1000_weight: Option<u32>) -> Result<(), APIError> {
13301342
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
13311343

@@ -1372,12 +1384,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
13721384
msg: channel_update
13731385
});
13741386
}
1375-
if let Ok(mut pending_events_lock) = self.pending_events.lock() {
1376-
pending_events_lock.push(events::Event::ChannelClosed {
1377-
channel_id: *channel_id,
1378-
reason: ClosureReason::HolderForceClosed
1379-
});
1380-
}
1387+
self.issue_channel_close_events(&channel, ClosureReason::HolderForceClosed);
13811388
}
13821389
break Ok(());
13831390
},
@@ -1468,13 +1475,12 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
14681475
if let Some(short_id) = chan.get().get_short_channel_id() {
14691476
channel_state.short_to_id.remove(&short_id);
14701477
}
1471-
let mut pending_events_lock = self.pending_events.lock().unwrap();
14721478
if peer_node_id.is_some() {
14731479
if let Some(peer_msg) = peer_msg {
1474-
pending_events_lock.push(events::Event::ChannelClosed { channel_id: *channel_id, reason: ClosureReason::CounterpartyForceClosed { peer_msg: peer_msg.to_string() } });
1480+
self.issue_channel_close_events(chan.get(),ClosureReason::CounterpartyForceClosed { peer_msg: peer_msg.to_string() });
14751481
}
14761482
} else {
1477-
pending_events_lock.push(events::Event::ChannelClosed { channel_id: *channel_id, reason: ClosureReason::HolderForceClosed });
1483+
self.issue_channel_close_events(chan.get(),ClosureReason::HolderForceClosed);
14781484
}
14791485
chan.remove_entry().1
14801486
} else {
@@ -3574,7 +3580,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
35743580
msg: update
35753581
});
35763582
}
3577-
self.pending_events.lock().unwrap().push(events::Event::ChannelClosed { channel_id: msg.channel_id, reason: ClosureReason::CooperativeClosure });
3583+
self.issue_channel_close_events(&chan, ClosureReason::CooperativeClosure);
35783584
}
35793585
Ok(())
35803586
}
@@ -3986,7 +3992,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
39863992
msg: update
39873993
});
39883994
}
3989-
self.pending_events.lock().unwrap().push(events::Event::ChannelClosed { channel_id: chan.channel_id(), reason: ClosureReason::CommitmentTxConfirmed });
3995+
self.issue_channel_close_events(&chan, ClosureReason::CommitmentTxConfirmed);
39903996
pending_msg_events.push(events::MessageSendEvent::HandleError {
39913997
node_id: chan.get_counterparty_node_id(),
39923998
action: msgs::ErrorAction::SendErrorMessage {
@@ -4102,12 +4108,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
41024108
});
41034109
}
41044110

4105-
if let Ok(mut pending_events_lock) = self.pending_events.lock() {
4106-
pending_events_lock.push(events::Event::ChannelClosed {
4107-
channel_id: *channel_id,
4108-
reason: ClosureReason::CooperativeClosure
4109-
});
4110-
}
4111+
self.issue_channel_close_events(chan, ClosureReason::CooperativeClosure);
41114112

41124113
log_info!(self.logger, "Broadcasting {}", log_tx!(tx));
41134114
self.tx_broadcaster.broadcast_transaction(&tx);
@@ -4529,7 +4530,7 @@ where
45294530
msg: update
45304531
});
45314532
}
4532-
self.pending_events.lock().unwrap().push(events::Event::ChannelClosed { channel_id: channel.channel_id(), reason: ClosureReason::CommitmentTxConfirmed });
4533+
self.issue_channel_close_events(channel, ClosureReason::CommitmentTxConfirmed);
45334534
pending_msg_events.push(events::MessageSendEvent::HandleError {
45344535
node_id: channel.get_counterparty_node_id(),
45354536
action: msgs::ErrorAction::SendErrorMessage { msg: e },
@@ -4720,7 +4721,7 @@ impl<Signer: Sign, M: Deref , T: Deref , K: Deref , F: Deref , L: Deref >
47204721
msg: update
47214722
});
47224723
}
4723-
self.pending_events.lock().unwrap().push(events::Event::ChannelClosed { channel_id: chan.channel_id(), reason: ClosureReason::DisconnectedPeer });
4724+
self.issue_channel_close_events(chan, ClosureReason::DisconnectedPeer);
47244725
false
47254726
} else {
47264727
true
@@ -4735,7 +4736,7 @@ impl<Signer: Sign, M: Deref , T: Deref , K: Deref , F: Deref , L: Deref >
47354736
if let Some(short_id) = chan.get_short_channel_id() {
47364737
short_to_id.remove(&short_id);
47374738
}
4738-
self.pending_events.lock().unwrap().push(events::Event::ChannelClosed { channel_id: chan.channel_id(), reason: ClosureReason::DisconnectedPeer });
4739+
self.issue_channel_close_events(chan, ClosureReason::DisconnectedPeer);
47394740
return false;
47404741
} else {
47414742
no_channels_remain = false;

lightning/src/ln/functional_test_utils.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -763,21 +763,29 @@ macro_rules! check_closed_broadcast {
763763
}}
764764
}
765765

766-
/// Check that a channel's closing channel event has been issued
766+
/// Check that a channel's closing channel events has been issued
767767
#[macro_export]
768768
macro_rules! check_closed_event {
769-
($node: expr, $events: expr, $reason: expr) => {{
769+
($node: expr, $events: expr, $reason: expr) => {
770+
check_closed_event!($node, $events, $reason, false);
771+
};
772+
($node: expr, $events: expr, $reason: expr, $is_check_discard_funding: expr) => {{
770773
let events = $node.node.get_and_clear_pending_events();
771774
assert_eq!(events.len(), $events);
772775
let expected_reason = $reason;
776+
let mut issues_discard_funding = false;
773777
for event in events {
774778
match event {
775779
Event::ChannelClosed { ref reason, .. } => {
776780
assert_eq!(*reason, expected_reason);
777781
},
782+
Event::DiscardFunding { .. } => {
783+
issues_discard_funding = true;
784+
}
778785
_ => panic!("Unexpected event"),
779786
}
780787
}
788+
assert_eq!($is_check_discard_funding, issues_discard_funding);
781789
}}
782790
}
783791

lightning/src/ln/functional_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8632,7 +8632,7 @@ fn test_pre_lockin_no_chan_closed_update() {
86328632
let channel_id = ::chain::transaction::OutPoint { txid: funding_created_msg.funding_txid, index: funding_created_msg.funding_output_index }.to_channel_id();
86338633
nodes[0].node.handle_error(&nodes[1].node.get_our_node_id(), &msgs::ErrorMessage { channel_id, data: "Hi".to_owned() });
86348634
assert!(nodes[0].chain_monitor.added_monitors.lock().unwrap().is_empty());
8635-
check_closed_event!(nodes[0], 1, ClosureReason::CounterpartyForceClosed { peer_msg: "Hi".to_string() });
8635+
check_closed_event!(nodes[0], 2, ClosureReason::CounterpartyForceClosed { peer_msg: "Hi".to_string() }, true);
86368636
}
86378637

86388638
#[test]

lightning/src/util/events.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use io;
3030
use prelude::*;
3131
use core::time::Duration;
3232
use core::ops::Deref;
33+
use bitcoin::Transaction;
3334

3435
/// Some information provided on receipt of payment depends on whether the payment received is a
3536
/// spontaneous payment or a "conventional" lightning payment that's paying an invoice.
@@ -254,6 +255,14 @@ pub enum Event {
254255
channel_id: [u8; 32],
255256
/// The reason the channel was closed.
256257
reason: ClosureReason
258+
},
259+
/// Used to indicate to the user that they can abandon the funding transaction and recycle the
260+
/// inputs for another purpose.
261+
DiscardFunding {
262+
/// The channel_id of the channel which has been closed.
263+
channel_id: [u8; 32],
264+
/// The full transaction received from the user
265+
transaction: Transaction
257266
}
258267
}
259268

@@ -340,6 +349,13 @@ impl Writeable for Event {
340349
(2, reason, required)
341350
});
342351
},
352+
&Event::DiscardFunding { ref channel_id, ref transaction } => {
353+
11u8.write(writer)?;
354+
write_tlv_fields!(writer, {
355+
(0, channel_id, required),
356+
(2, transaction, required)
357+
})
358+
},
343359
// Note that, going forward, all new events must only write data inside of
344360
// `write_tlv_fields`. Versions 0.0.101+ will ignore odd-numbered events that write
345361
// data via `write_tlv_fields`.
@@ -471,6 +487,15 @@ impl MaybeReadable for Event {
471487
if reason.is_none() { return Ok(None); }
472488
Ok(Some(Event::ChannelClosed { channel_id, reason: reason.unwrap() }))
473489
},
490+
11u8 => {
491+
let mut channel_id = [0; 32];
492+
let mut transaction = Transaction{ version: 2, lock_time: 0, input: Vec::new(), output: Vec::new() };
493+
read_tlv_fields!(reader, {
494+
(0, channel_id, required),
495+
(2, transaction, required),
496+
});
497+
Ok(Some(Event::DiscardFunding { channel_id, transaction } ))
498+
},
474499
// Versions prior to 0.0.100 did not ignore odd types, instead returning InvalidValue.
475500
// Version 0.0.100 failed to properly ignore odd types, possibly resulting in corrupt
476501
// reads.

0 commit comments

Comments
 (0)