Skip to content

Commit d665748

Browse files
authored
Merge pull request #1098 from 1nF0rmed/2021-09-adds-discard-funding-event
Add Event::DiscardFunding generation
2 parents 843d25d + 1955008 commit d665748

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
@@ -1903,6 +1903,15 @@ impl<Signer: Sign> Channel<Signer> {
19031903
Ok(())
19041904
}
19051905

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

lightning/src/ln/channelmanager.rs

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

1387+
/// Helper function that issues the channel close events
1388+
fn issue_channel_close_events(&self, channel: &Channel<Signer>, closure_reason: ClosureReason) {
1389+
let mut pending_events_lock = self.pending_events.lock().unwrap();
1390+
match channel.unbroadcasted_funding() {
1391+
Some(transaction) => {
1392+
pending_events_lock.push(events::Event::DiscardFunding { channel_id: channel.channel_id(), transaction })
1393+
},
1394+
None => {},
1395+
}
1396+
pending_events_lock.push(events::Event::ChannelClosed { channel_id: channel.channel_id(), reason: closure_reason });
1397+
}
1398+
13871399
fn close_channel_internal(&self, channel_id: &[u8; 32], target_feerate_sats_per_1000_weight: Option<u32>) -> Result<(), APIError> {
13881400
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
13891401

@@ -1430,12 +1442,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
14301442
msg: channel_update
14311443
});
14321444
}
1433-
if let Ok(mut pending_events_lock) = self.pending_events.lock() {
1434-
pending_events_lock.push(events::Event::ChannelClosed {
1435-
channel_id: *channel_id,
1436-
reason: ClosureReason::HolderForceClosed
1437-
});
1438-
}
1445+
self.issue_channel_close_events(&channel, ClosureReason::HolderForceClosed);
14391446
}
14401447
break Ok(());
14411448
},
@@ -1526,13 +1533,12 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
15261533
if let Some(short_id) = chan.get().get_short_channel_id() {
15271534
channel_state.short_to_id.remove(&short_id);
15281535
}
1529-
let mut pending_events_lock = self.pending_events.lock().unwrap();
15301536
if peer_node_id.is_some() {
15311537
if let Some(peer_msg) = peer_msg {
1532-
pending_events_lock.push(events::Event::ChannelClosed { channel_id: *channel_id, reason: ClosureReason::CounterpartyForceClosed { peer_msg: peer_msg.to_string() } });
1538+
self.issue_channel_close_events(chan.get(),ClosureReason::CounterpartyForceClosed { peer_msg: peer_msg.to_string() });
15331539
}
15341540
} else {
1535-
pending_events_lock.push(events::Event::ChannelClosed { channel_id: *channel_id, reason: ClosureReason::HolderForceClosed });
1541+
self.issue_channel_close_events(chan.get(),ClosureReason::HolderForceClosed);
15361542
}
15371543
chan.remove_entry().1
15381544
} else {
@@ -3705,7 +3711,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
37053711
msg: update
37063712
});
37073713
}
3708-
self.pending_events.lock().unwrap().push(events::Event::ChannelClosed { channel_id: msg.channel_id, reason: ClosureReason::CooperativeClosure });
3714+
self.issue_channel_close_events(&chan, ClosureReason::CooperativeClosure);
37093715
}
37103716
Ok(())
37113717
}
@@ -4117,7 +4123,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
41174123
msg: update
41184124
});
41194125
}
4120-
self.pending_events.lock().unwrap().push(events::Event::ChannelClosed { channel_id: chan.channel_id(), reason: ClosureReason::CommitmentTxConfirmed });
4126+
self.issue_channel_close_events(&chan, ClosureReason::CommitmentTxConfirmed);
41214127
pending_msg_events.push(events::MessageSendEvent::HandleError {
41224128
node_id: chan.get_counterparty_node_id(),
41234129
action: msgs::ErrorAction::SendErrorMessage {
@@ -4233,12 +4239,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
42334239
});
42344240
}
42354241

4236-
if let Ok(mut pending_events_lock) = self.pending_events.lock() {
4237-
pending_events_lock.push(events::Event::ChannelClosed {
4238-
channel_id: *channel_id,
4239-
reason: ClosureReason::CooperativeClosure
4240-
});
4241-
}
4242+
self.issue_channel_close_events(chan, ClosureReason::CooperativeClosure);
42424243

42434244
log_info!(self.logger, "Broadcasting {}", log_tx!(tx));
42444245
self.tx_broadcaster.broadcast_transaction(&tx);
@@ -4675,7 +4676,7 @@ where
46754676
msg: update
46764677
});
46774678
}
4678-
self.pending_events.lock().unwrap().push(events::Event::ChannelClosed { channel_id: channel.channel_id(), reason: ClosureReason::CommitmentTxConfirmed });
4679+
self.issue_channel_close_events(channel, ClosureReason::CommitmentTxConfirmed);
46794680
pending_msg_events.push(events::MessageSendEvent::HandleError {
46804681
node_id: channel.get_counterparty_node_id(),
46814682
action: msgs::ErrorAction::SendErrorMessage { msg: e },
@@ -4866,7 +4867,7 @@ impl<Signer: Sign, M: Deref , T: Deref , K: Deref , F: Deref , L: Deref >
48664867
msg: update
48674868
});
48684869
}
4869-
self.pending_events.lock().unwrap().push(events::Event::ChannelClosed { channel_id: chan.channel_id(), reason: ClosureReason::DisconnectedPeer });
4870+
self.issue_channel_close_events(chan, ClosureReason::DisconnectedPeer);
48704871
false
48714872
} else {
48724873
true
@@ -4881,7 +4882,7 @@ impl<Signer: Sign, M: Deref , T: Deref , K: Deref , F: Deref , L: Deref >
48814882
if let Some(short_id) = chan.get_short_channel_id() {
48824883
short_to_id.remove(&short_id);
48834884
}
4884-
self.pending_events.lock().unwrap().push(events::Event::ChannelClosed { channel_id: chan.channel_id(), reason: ClosureReason::DisconnectedPeer });
4885+
self.issue_channel_close_events(chan, ClosureReason::DisconnectedPeer);
48854886
return false;
48864887
} else {
48874888
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
@@ -8640,7 +8640,7 @@ fn test_pre_lockin_no_chan_closed_update() {
86408640
let channel_id = ::chain::transaction::OutPoint { txid: funding_created_msg.funding_txid, index: funding_created_msg.funding_output_index }.to_channel_id();
86418641
nodes[0].node.handle_error(&nodes[1].node.get_our_node_id(), &msgs::ErrorMessage { channel_id, data: "Hi".to_owned() });
86428642
assert!(nodes[0].chain_monitor.added_monitors.lock().unwrap().is_empty());
8643-
check_closed_event!(nodes[0], 1, ClosureReason::CounterpartyForceClosed { peer_msg: "Hi".to_string() });
8643+
check_closed_event!(nodes[0], 2, ClosureReason::CounterpartyForceClosed { peer_msg: "Hi".to_string() }, true);
86448644
}
86458645

86468646
#[test]

lightning/src/util/events.rs

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

3536
/// Some information provided on receipt of payment depends on whether the payment received is a
3637
/// spontaneous payment or a "conventional" lightning payment that's paying an invoice.
@@ -258,6 +259,14 @@ pub enum Event {
258259
channel_id: [u8; 32],
259260
/// The reason the channel was closed.
260261
reason: ClosureReason
262+
},
263+
/// Used to indicate to the user that they can abandon the funding transaction and recycle the
264+
/// inputs for another purpose.
265+
DiscardFunding {
266+
/// The channel_id of the channel which has been closed.
267+
channel_id: [u8; 32],
268+
/// The full transaction received from the user
269+
transaction: Transaction
261270
}
262271
}
263272

@@ -344,6 +353,13 @@ impl Writeable for Event {
344353
(2, reason, required)
345354
});
346355
},
356+
&Event::DiscardFunding { ref channel_id, ref transaction } => {
357+
11u8.write(writer)?;
358+
write_tlv_fields!(writer, {
359+
(0, channel_id, required),
360+
(2, transaction, required)
361+
})
362+
},
347363
// Note that, going forward, all new events must only write data inside of
348364
// `write_tlv_fields`. Versions 0.0.101+ will ignore odd-numbered events that write
349365
// data via `write_tlv_fields`.
@@ -473,6 +489,15 @@ impl MaybeReadable for Event {
473489
if reason.is_none() { return Ok(None); }
474490
Ok(Some(Event::ChannelClosed { channel_id, reason: reason.unwrap() }))
475491
},
492+
11u8 => {
493+
let mut channel_id = [0; 32];
494+
let mut transaction = Transaction{ version: 2, lock_time: 0, input: Vec::new(), output: Vec::new() };
495+
read_tlv_fields!(reader, {
496+
(0, channel_id, required),
497+
(2, transaction, required),
498+
});
499+
Ok(Some(Event::DiscardFunding { channel_id, transaction } ))
500+
},
476501
// Versions prior to 0.0.100 did not ignore odd types, instead returning InvalidValue.
477502
// Version 0.0.100 failed to properly ignore odd types, possibly resulting in corrupt
478503
// reads.

0 commit comments

Comments
 (0)