Skip to content

Commit d0ee7de

Browse files
liveseedcoderm73
authored and
coderm73
committed
Merge pull request #1403 from jurvis/jurvis/add-paymentforwardingfailed-event
Add HTLCHandlingFailed event
2 parents a0556ae + b7ebfd1 commit d0ee7de

16 files changed

+400
-181
lines changed

fuzz/src/chanmon_consistency.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ impl chain::Watch<EnforcingSigner> for TestChainMonitor {
148148
self.chain_monitor.update_channel(funding_txo, update)
149149
}
150150

151-
fn release_pending_monitor_events(&self) -> Vec<(OutPoint, Vec<MonitorEvent>)> {
151+
fn release_pending_monitor_events(&self) -> Vec<(OutPoint, Vec<MonitorEvent>, Option<PublicKey>)> {
152152
return self.chain_monitor.release_pending_monitor_events();
153153
}
154154
}
@@ -860,6 +860,7 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out) {
860860
events::Event::PendingHTLCsForwardable { .. } => {
861861
nodes[$node].process_pending_htlc_forwards();
862862
},
863+
events::Event::HTLCHandlingFailed { .. } => {},
863864
_ => if out.may_fail.load(atomic::Ordering::Acquire) {
864865
return;
865866
} else {

lightning/src/chain/chainmonitor.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ use prelude::*;
4343
use sync::{RwLock, RwLockReadGuard, Mutex, MutexGuard};
4444
use core::ops::Deref;
4545
use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
46+
use bitcoin::secp256k1::PublicKey;
4647

4748
#[derive(Clone, Copy, Hash, PartialEq, Eq)]
4849
/// A specific update's ID stored in a `MonitorUpdateId`, separated out to make the contents
@@ -235,7 +236,7 @@ pub struct ChainMonitor<ChannelSigner: Sign, C: Deref, T: Deref, F: Deref, L: De
235236
persister: P,
236237
/// "User-provided" (ie persistence-completion/-failed) [`MonitorEvent`]s. These came directly
237238
/// from the user and not from a [`ChannelMonitor`].
238-
pending_monitor_events: Mutex<Vec<(OutPoint, Vec<MonitorEvent>)>>,
239+
pending_monitor_events: Mutex<Vec<(OutPoint, Vec<MonitorEvent>, Option<PublicKey>)>>,
239240
/// The best block height seen, used as a proxy for the passage of time.
240241
highest_chain_height: AtomicUsize,
241242
}
@@ -299,7 +300,7 @@ where C::Target: chain::Filter,
299300
log_trace!(self.logger, "Finished syncing Channel Monitor for channel {}", log_funding_info!(monitor)),
300301
Err(ChannelMonitorUpdateErr::PermanentFailure) => {
301302
monitor_state.channel_perm_failed.store(true, Ordering::Release);
302-
self.pending_monitor_events.lock().unwrap().push((*funding_outpoint, vec![MonitorEvent::UpdateFailed(*funding_outpoint)]));
303+
self.pending_monitor_events.lock().unwrap().push((*funding_outpoint, vec![MonitorEvent::UpdateFailed(*funding_outpoint)], monitor.get_counterparty_node_id()));
303304
},
304305
Err(ChannelMonitorUpdateErr::TemporaryFailure) => {
305306
log_debug!(self.logger, "Channel Monitor sync for channel {} in progress, holding events until completion!", log_funding_info!(monitor));
@@ -458,7 +459,7 @@ where C::Target: chain::Filter,
458459
self.pending_monitor_events.lock().unwrap().push((funding_txo, vec![MonitorEvent::UpdateCompleted {
459460
funding_txo,
460461
monitor_update_id: monitor_data.monitor.get_latest_update_id(),
461-
}]));
462+
}], monitor_data.monitor.get_counterparty_node_id()));
462463
},
463464
MonitorUpdateId { contents: UpdateOrigin::ChainSync(_) } => {
464465
if !monitor_data.has_pending_chainsync_updates(&pending_monitor_updates) {
@@ -476,10 +477,12 @@ where C::Target: chain::Filter,
476477
/// channel_monitor_updated once with the highest ID.
477478
#[cfg(any(test, fuzzing))]
478479
pub fn force_channel_monitor_updated(&self, funding_txo: OutPoint, monitor_update_id: u64) {
480+
let monitors = self.monitors.read().unwrap();
481+
let counterparty_node_id = monitors.get(&funding_txo).and_then(|m| m.monitor.get_counterparty_node_id());
479482
self.pending_monitor_events.lock().unwrap().push((funding_txo, vec![MonitorEvent::UpdateCompleted {
480483
funding_txo,
481484
monitor_update_id,
482-
}]));
485+
}], counterparty_node_id));
483486
}
484487

485488
#[cfg(any(test, fuzzing, feature = "_test_utils"))]
@@ -666,7 +669,7 @@ where C::Target: chain::Filter,
666669
}
667670
}
668671

669-
fn release_pending_monitor_events(&self) -> Vec<(OutPoint, Vec<MonitorEvent>)> {
672+
fn release_pending_monitor_events(&self) -> Vec<(OutPoint, Vec<MonitorEvent>, Option<PublicKey>)> {
670673
let mut pending_monitor_events = self.pending_monitor_events.lock().unwrap().split_off(0);
671674
for monitor_state in self.monitors.read().unwrap().values() {
672675
let is_pending_monitor_update = monitor_state.has_pending_chainsync_updates(&monitor_state.pending_monitor_updates.lock().unwrap());
@@ -695,7 +698,8 @@ where C::Target: chain::Filter,
695698
let monitor_events = monitor_state.monitor.get_and_clear_pending_monitor_events();
696699
if monitor_events.len() > 0 {
697700
let monitor_outpoint = monitor_state.monitor.get_funding_txo().0;
698-
pending_monitor_events.push((monitor_outpoint, monitor_events));
701+
let counterparty_node_id = monitor_state.monitor.get_counterparty_node_id();
702+
pending_monitor_events.push((monitor_outpoint, monitor_events, counterparty_node_id));
699703
}
700704
}
701705
}

lightning/src/chain/channelmonitor.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1214,6 +1214,10 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
12141214
self.inner.lock().unwrap().get_cur_holder_commitment_number()
12151215
}
12161216

1217+
pub(crate) fn get_counterparty_node_id(&self) -> Option<PublicKey> {
1218+
self.inner.lock().unwrap().counterparty_node_id
1219+
}
1220+
12171221
/// Used by ChannelManager deserialization to broadcast the latest holder state if its copy of
12181222
/// the Channel was out-of-date. You may use it to get a broadcastable holder toxic tx in case of
12191223
/// fallen-behind, i.e when receiving a channel_reestablish with a proof that our counterparty side knows

lightning/src/chain/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use bitcoin::blockdata::script::Script;
1515
use bitcoin::blockdata::transaction::{Transaction, TxOut};
1616
use bitcoin::hash_types::{BlockHash, Txid};
1717
use bitcoin::network::constants::Network;
18+
use bitcoin::secp256k1::PublicKey;
1819

1920
use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, MonitorEvent};
2021
use chain::keysinterface::Sign;
@@ -302,7 +303,7 @@ pub trait Watch<ChannelSigner: Sign> {
302303
///
303304
/// For details on asynchronous [`ChannelMonitor`] updating and returning
304305
/// [`MonitorEvent::UpdateCompleted`] here, see [`ChannelMonitorUpdateErr::TemporaryFailure`].
305-
fn release_pending_monitor_events(&self) -> Vec<(OutPoint, Vec<MonitorEvent>)>;
306+
fn release_pending_monitor_events(&self) -> Vec<(OutPoint, Vec<MonitorEvent>, Option<PublicKey>)>;
306307
}
307308

308309
/// The `Filter` trait defines behavior for indicating chain activity of interest pertaining to

lightning/src/ln/chanmon_update_fail_tests.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use ln::msgs;
2626
use ln::msgs::{ChannelMessageHandler, RoutingMessageHandler};
2727
use util::config::UserConfig;
2828
use util::enforcing_trait_impls::EnforcingSigner;
29-
use util::events::{Event, MessageSendEvent, MessageSendEventsProvider, PaymentPurpose, ClosureReason};
29+
use util::events::{Event, MessageSendEvent, MessageSendEventsProvider, PaymentPurpose, ClosureReason, HTLCDestination};
3030
use util::errors::APIError;
3131
use util::ser::{ReadableArgs, Writeable};
3232
use util::test_utils::TestBroadcaster;
@@ -832,7 +832,7 @@ fn do_test_monitor_update_fail_raa(test_ignore_second_cs: bool) {
832832

833833
// Fail the payment backwards, failing the monitor update on nodes[1]'s receipt of the RAA
834834
nodes[2].node.fail_htlc_backwards(&payment_hash_1);
835-
expect_pending_htlcs_forwardable!(nodes[2]);
835+
expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[2], vec![HTLCDestination::FailedPayment { payment_hash: payment_hash_1 }]);
836836
check_added_monitors!(nodes[2], 1);
837837

838838
let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
@@ -913,7 +913,7 @@ fn do_test_monitor_update_fail_raa(test_ignore_second_cs: bool) {
913913
let (outpoint, latest_update, _) = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&chan_2.2).unwrap().clone();
914914
nodes[1].chain_monitor.chain_monitor.force_channel_monitor_updated(outpoint, latest_update);
915915
check_added_monitors!(nodes[1], 0);
916-
expect_pending_htlcs_forwardable!(nodes[1]);
916+
expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::NextHopChannel { node_id: Some(nodes[2].node.get_our_node_id()), channel_id: chan_2.2 }]);
917917
check_added_monitors!(nodes[1], 1);
918918

919919
let mut events_3 = nodes[1].node.get_and_clear_pending_msg_events();
@@ -1690,14 +1690,14 @@ fn test_monitor_update_on_pending_forwards() {
16901690
let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
16911691
let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
16921692
let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
1693-
create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
1693+
let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
16941694

16951695
// Rebalance a bit so that we can send backwards from 3 to 1.
16961696
send_payment(&nodes[0], &[&nodes[1], &nodes[2]], 5000000);
16971697

16981698
let (_, payment_hash_1, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1000000);
16991699
nodes[2].node.fail_htlc_backwards(&payment_hash_1);
1700-
expect_pending_htlcs_forwardable!(nodes[2]);
1700+
expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[2], vec![HTLCDestination::FailedPayment { payment_hash: payment_hash_1 }]);
17011701
check_added_monitors!(nodes[2], 1);
17021702

17031703
let cs_fail_update = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
@@ -1718,7 +1718,7 @@ fn test_monitor_update_on_pending_forwards() {
17181718
commitment_signed_dance!(nodes[1], nodes[2], payment_event.commitment_msg, false);
17191719

17201720
chanmon_cfgs[1].persister.set_update_ret(Err(ChannelMonitorUpdateErr::TemporaryFailure));
1721-
expect_pending_htlcs_forwardable!(nodes[1]);
1721+
expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::NextHopChannel { node_id: Some(nodes[2].node.get_our_node_id()), channel_id: chan_2.2 }]);
17221722
check_added_monitors!(nodes[1], 1);
17231723
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
17241724
nodes[1].logger.assert_log("lightning::ln::channelmanager".to_string(), "Failed to update ChannelMonitor".to_string(), 1);
@@ -2106,7 +2106,7 @@ fn test_fail_htlc_on_broadcast_after_claim() {
21062106
check_closed_broadcast!(nodes[1], true);
21072107
connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
21082108
check_added_monitors!(nodes[1], 1);
2109-
expect_pending_htlcs_forwardable!(nodes[1]);
2109+
expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::NextHopChannel { node_id: Some(nodes[2].node.get_our_node_id()), channel_id: chan_id_2 }]);
21102110

21112111
nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_fulfill_htlcs[0]);
21122112
expect_payment_sent_without_paths!(nodes[0], payment_preimage);
@@ -2469,7 +2469,7 @@ fn do_test_reconnect_dup_htlc_claims(htlc_status: HTLCStatusAtDupClaim, second_f
24692469
};
24702470
if second_fails {
24712471
nodes[2].node.fail_htlc_backwards(&payment_hash);
2472-
expect_pending_htlcs_forwardable!(nodes[2]);
2472+
expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[2], vec![HTLCDestination::FailedPayment { payment_hash }]);
24732473
check_added_monitors!(nodes[2], 1);
24742474
get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
24752475
} else {
@@ -2505,7 +2505,7 @@ fn do_test_reconnect_dup_htlc_claims(htlc_status: HTLCStatusAtDupClaim, second_f
25052505

25062506
if second_fails {
25072507
reconnect_nodes(&nodes[1], &nodes[2], (false, false), (0, 0), (0, 0), (1, 0), (0, 0), (0, 0), (false, false));
2508-
expect_pending_htlcs_forwardable!(nodes[1]);
2508+
expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::NextHopChannel { node_id: Some(nodes[2].node.get_our_node_id()), channel_id: chan_id_2 }]);
25092509
} else {
25102510
reconnect_nodes(&nodes[1], &nodes[2], (false, false), (0, 0), (1, 0), (0, 0), (0, 0), (0, 0), (false, false));
25112511
}

lightning/src/ln/channel.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -5765,7 +5765,7 @@ impl<Signer: Sign> Channel<Signer> {
57655765
/// those explicitly stated to be allowed after shutdown completes, eg some simple getters).
57665766
/// Also returns the list of payment_hashes for channels which we can safely fail backwards
57675767
/// immediately (others we will have to allow to time out).
5768-
pub fn force_shutdown(&mut self, should_broadcast: bool) -> (Option<(OutPoint, ChannelMonitorUpdate)>, Vec<(HTLCSource, PaymentHash)>) {
5768+
pub fn force_shutdown(&mut self, should_broadcast: bool) -> (Option<(OutPoint, ChannelMonitorUpdate)>, Vec<(HTLCSource, PaymentHash, PublicKey, [u8; 32])>) {
57695769
// Note that we MUST only generate a monitor update that indicates force-closure - we're
57705770
// called during initialization prior to the chain_monitor in the encompassing ChannelManager
57715771
// being fully configured in some cases. Thus, its likely any monitor events we generate will
@@ -5775,10 +5775,11 @@ impl<Signer: Sign> Channel<Signer> {
57755775
// We go ahead and "free" any holding cell HTLCs or HTLCs we haven't yet committed to and
57765776
// return them to fail the payment.
57775777
let mut dropped_outbound_htlcs = Vec::with_capacity(self.holding_cell_htlc_updates.len());
5778+
let counterparty_node_id = self.get_counterparty_node_id();
57785779
for htlc_update in self.holding_cell_htlc_updates.drain(..) {
57795780
match htlc_update {
57805781
HTLCUpdateAwaitingACK::AddHTLC { source, payment_hash, .. } => {
5781-
dropped_outbound_htlcs.push((source, payment_hash));
5782+
dropped_outbound_htlcs.push((source, payment_hash, counterparty_node_id, self.channel_id));
57825783
},
57835784
_ => {}
57845785
}

0 commit comments

Comments
 (0)