From: Tee8z Date: Wed, 12 Oct 2022 18:11:21 +0000 (-0400) Subject: adds 'receiver_node_id' to 'Event::Payment{Received,Claimed}' X-Git-Tag: v0.0.113~26^2 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=babde3a3c5a1d11aead782f6a1aba9485016a80f;p=rust-lightning adds 'receiver_node_id' to 'Event::Payment{Received,Claimed}' --- diff --git a/lightning-invoice/src/utils.rs b/lightning-invoice/src/utils.rs index 5d54e376b..9d96c6e61 100644 --- a/lightning-invoice/src/utils.rs +++ b/lightning-invoice/src/utils.rs @@ -1083,7 +1083,7 @@ mod test { nodes[fwd_idx].node.process_pending_htlc_forwards(); let payment_preimage_opt = if user_generated_pmt_hash { None } else { Some(payment_preimage) }; - expect_payment_received!(&nodes[fwd_idx], payment_hash, payment_secret, payment_amt, payment_preimage_opt); + expect_payment_received!(&nodes[fwd_idx], payment_hash, payment_secret, payment_amt, payment_preimage_opt, route.paths[0].last().unwrap().pubkey); do_claim_payment_along_route(&nodes[0], &vec!(&vec!(&nodes[fwd_idx])[..]), false, payment_preimage); let events = nodes[0].node.get_and_clear_pending_events(); assert_eq!(events.len(), 2); diff --git a/lightning/src/ln/chanmon_update_fail_tests.rs b/lightning/src/ln/chanmon_update_fail_tests.rs index e78b8999f..2ee55a6af 100644 --- a/lightning/src/ln/chanmon_update_fail_tests.rs +++ b/lightning/src/ln/chanmon_update_fail_tests.rs @@ -200,9 +200,10 @@ fn do_test_simple_monitor_temporary_update_fail(disconnect: bool) { let events_3 = nodes[1].node.get_and_clear_pending_events(); assert_eq!(events_3.len(), 1); match events_3[0] { - Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat } => { + Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat, receiver_node_id } => { assert_eq!(payment_hash_1, *payment_hash); assert_eq!(amount_msat, 1_000_000); + assert_eq!(receiver_node_id.unwrap(), nodes[1].node.get_our_node_id()); match &purpose { PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => { assert!(payment_preimage.is_none()); @@ -567,9 +568,10 @@ fn do_test_monitor_temporary_update_fail(disconnect_count: usize) { let events_5 = nodes[1].node.get_and_clear_pending_events(); assert_eq!(events_5.len(), 1); match events_5[0] { - Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat } => { + Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat, receiver_node_id } => { assert_eq!(payment_hash_2, *payment_hash); assert_eq!(amount_msat, 1_000_000); + assert_eq!(receiver_node_id.unwrap(), nodes[1].node.get_our_node_id()); match &purpose { PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => { assert!(payment_preimage.is_none()); @@ -682,9 +684,10 @@ fn test_monitor_update_fail_cs() { let events = nodes[1].node.get_and_clear_pending_events(); assert_eq!(events.len(), 1); match events[0] { - Event::PaymentReceived { payment_hash, ref purpose, amount_msat } => { + Event::PaymentReceived { payment_hash, ref purpose, amount_msat, receiver_node_id } => { assert_eq!(payment_hash, our_payment_hash); assert_eq!(amount_msat, 1_000_000); + assert_eq!(receiver_node_id.unwrap(), nodes[1].node.get_our_node_id()); match &purpose { PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => { assert!(payment_preimage.is_none()); @@ -1645,9 +1648,10 @@ fn test_monitor_update_fail_claim() { let events = nodes[0].node.get_and_clear_pending_events(); assert_eq!(events.len(), 2); match events[0] { - Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat } => { + Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat, receiver_node_id } => { assert_eq!(payment_hash_2, *payment_hash); assert_eq!(1_000_000, amount_msat); + assert_eq!(receiver_node_id.unwrap(), nodes[0].node.get_our_node_id()); match &purpose { PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => { assert!(payment_preimage.is_none()); @@ -1659,9 +1663,10 @@ fn test_monitor_update_fail_claim() { _ => panic!("Unexpected event"), } match events[1] { - Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat } => { + Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat, receiver_node_id } => { assert_eq!(payment_hash_3, *payment_hash); assert_eq!(1_000_000, amount_msat); + assert_eq!(receiver_node_id.unwrap(), nodes[0].node.get_our_node_id()); match &purpose { PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => { assert!(payment_preimage.is_none()); diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index b22dacdd6..cb790b144 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -3323,6 +3323,12 @@ impl ChannelManager {{ @@ -3365,6 +3371,7 @@ impl ChannelManager ChannelManager ChannelManager chan_id.clone(), @@ -4101,6 +4110,12 @@ impl ChannelManager ChannelManager if let Some((payment_purpose, claimable_htlcs)) = claimable_htlcs.remove(&payment_hash) { log_info!(args.logger, "Re-claiming HTLCs with payment hash {} as we've released the preimage to a ChannelMonitor!", log_bytes!(payment_hash.0)); let mut claimable_amt_msat = 0; + let mut receiver_node_id = Some(our_network_pubkey); + let phantom_shared_secret = claimable_htlcs[0].prev_hop.phantom_shared_secret; + if phantom_shared_secret.is_some() { + let phantom_pubkey = args.keys_manager.get_node_id(Recipient::PhantomNode) + .expect("Failed to get node_id for phantom node recipient"); + receiver_node_id = Some(phantom_pubkey) + } for claimable_htlc in claimable_htlcs { claimable_amt_msat += claimable_htlc.value; @@ -7474,6 +7497,7 @@ impl<'a, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> } } pending_events_read.push(events::Event::PaymentClaimed { + receiver_node_id, payment_hash, purpose: payment_purpose, amount_msat: claimable_amt_msat, diff --git a/lightning/src/ln/functional_test_utils.rs b/lightning/src/ln/functional_test_utils.rs index d81847e65..cd3041be8 100644 --- a/lightning/src/ln/functional_test_utils.rs +++ b/lightning/src/ln/functional_test_utils.rs @@ -1469,20 +1469,20 @@ macro_rules! expect_pending_htlcs_forwardable_from_events { } }} } - #[macro_export] #[cfg(any(test, feature = "_bench_unstable", feature = "_test_utils"))] macro_rules! expect_payment_received { ($node: expr, $expected_payment_hash: expr, $expected_payment_secret: expr, $expected_recv_value: expr) => { - expect_payment_received!($node, $expected_payment_hash, $expected_payment_secret, $expected_recv_value, None) + expect_payment_received!($node, $expected_payment_hash, $expected_payment_secret, $expected_recv_value, None, $node.node.get_our_node_id()) }; - ($node: expr, $expected_payment_hash: expr, $expected_payment_secret: expr, $expected_recv_value: expr, $expected_payment_preimage: expr) => { + ($node: expr, $expected_payment_hash: expr, $expected_payment_secret: expr, $expected_recv_value: expr, $expected_payment_preimage: expr, $expected_receiver_node_id: expr) => { let events = $node.node.get_and_clear_pending_events(); assert_eq!(events.len(), 1); match events[0] { - $crate::util::events::Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat } => { + $crate::util::events::Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat, receiver_node_id } => { assert_eq!($expected_payment_hash, *payment_hash); assert_eq!($expected_recv_value, amount_msat); + assert_eq!($expected_receiver_node_id, receiver_node_id.unwrap()); match purpose { $crate::util::events::PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => { assert_eq!(&$expected_payment_preimage, payment_preimage); @@ -1774,8 +1774,9 @@ pub fn do_pass_along_path<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_p if payment_received_expected { assert_eq!(events_2.len(), 1); match events_2[0] { - Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat } => { + Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat, receiver_node_id } => { assert_eq!(our_payment_hash, *payment_hash); + assert_eq!(node.node.get_our_node_id(), receiver_node_id.unwrap()); match &purpose { PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => { assert_eq!(expected_preimage, *payment_preimage); diff --git a/lightning/src/ln/functional_tests.rs b/lightning/src/ln/functional_tests.rs index 9a0d03d8c..232e4704a 100644 --- a/lightning/src/ln/functional_tests.rs +++ b/lightning/src/ln/functional_tests.rs @@ -1956,9 +1956,10 @@ fn test_channel_reserve_holding_cell_htlcs() { let events = nodes[2].node.get_and_clear_pending_events(); assert_eq!(events.len(), 2); match events[0] { - Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat } => { + Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat, receiver_node_id } => { assert_eq!(our_payment_hash_21, *payment_hash); assert_eq!(recv_value_21, amount_msat); + assert_eq!(nodes[2].node.get_our_node_id(), receiver_node_id.unwrap()); match &purpose { PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => { assert!(payment_preimage.is_none()); @@ -1970,9 +1971,10 @@ fn test_channel_reserve_holding_cell_htlcs() { _ => panic!("Unexpected event"), } match events[1] { - Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat } => { + Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat, receiver_node_id } => { assert_eq!(our_payment_hash_22, *payment_hash); assert_eq!(recv_value_22, amount_msat); + assert_eq!(nodes[2].node.get_our_node_id(), receiver_node_id.unwrap()); match &purpose { PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => { assert!(payment_preimage.is_none()); @@ -3734,9 +3736,10 @@ fn do_test_drop_messages_peer_disconnect(messages_delivered: u8, simulate_broken let events_2 = nodes[1].node.get_and_clear_pending_events(); assert_eq!(events_2.len(), 1); match events_2[0] { - Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat } => { + Event::PaymentReceived { ref payment_hash, ref purpose, amount_msat, receiver_node_id } => { assert_eq!(payment_hash_1, *payment_hash); assert_eq!(amount_msat, 1_000_000); + assert_eq!(receiver_node_id.unwrap(), nodes[1].node.get_our_node_id()); match &purpose { PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => { assert!(payment_preimage.is_none()); diff --git a/lightning/src/ln/onion_route_tests.rs b/lightning/src/ln/onion_route_tests.rs index c9422a6e0..2bf36ffba 100644 --- a/lightning/src/ln/onion_route_tests.rs +++ b/lightning/src/ln/onion_route_tests.rs @@ -1229,7 +1229,7 @@ fn test_phantom_failure_reject_payment() { nodes[1].node.process_pending_htlc_forwards(); expect_pending_htlcs_forwardable_ignore!(nodes[1]); nodes[1].node.process_pending_htlc_forwards(); - expect_payment_received!(nodes[1], payment_hash, payment_secret, recv_amt_msat); + expect_payment_received!(nodes[1], payment_hash, payment_secret, recv_amt_msat, None, route.paths[0].last().unwrap().pubkey); nodes[1].node.fail_htlc_backwards(&payment_hash); expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(nodes[1], vec![HTLCDestination::FailedPayment { payment_hash }]); nodes[1].node.process_pending_htlc_forwards(); diff --git a/lightning/src/util/events.rs b/lightning/src/util/events.rs index 3593cbf97..3032aec75 100644 --- a/lightning/src/util/events.rs +++ b/lightning/src/util/events.rs @@ -342,6 +342,13 @@ pub enum Event { /// [`ChannelManager::claim_funds`]: crate::ln::channelmanager::ChannelManager::claim_funds /// [`ChannelManager::fail_htlc_backwards`]: crate::ln::channelmanager::ChannelManager::fail_htlc_backwards PaymentReceived { + /// The node that received the payment. + /// This is useful to identify payments which were received via [phantom node payments]. + /// This field will always be filled in when the event was generated by LDK versions + /// 0.0.113 and above. + /// + /// [phantom node payments]: crate::chain::keysinterface::PhantomKeysManager + receiver_node_id: Option, /// The hash for which the preimage should be handed to the ChannelManager. Note that LDK will /// not stop you from registering duplicate payment hashes for inbound payments. payment_hash: PaymentHash, @@ -366,6 +373,13 @@ pub enum Event { /// /// [`ChannelManager::claim_funds`]: crate::ln::channelmanager::ChannelManager::claim_funds PaymentClaimed { + /// The node that received the payment. + /// This is useful to identify payments which were received via [phantom node payments]. + /// This field will always be filled in when the event was generated by LDK versions + /// 0.0.113 and above. + /// + /// [phantom node payments]: crate::chain::keysinterface::PhantomKeysManager + receiver_node_id: Option, /// The payment hash of the claimed payment. Note that LDK will not stop you from /// registering duplicate payment hashes for inbound payments. payment_hash: PaymentHash, @@ -739,7 +753,7 @@ impl Writeable for Event { // We never write out FundingGenerationReady events as, upon disconnection, peers // drop any channels which have not yet exchanged funding_signed. }, - &Event::PaymentReceived { ref payment_hash, ref amount_msat, ref purpose } => { + &Event::PaymentReceived { ref payment_hash, ref amount_msat, ref purpose, ref receiver_node_id } => { 1u8.write(writer)?; let mut payment_secret = None; let payment_preimage; @@ -754,6 +768,7 @@ impl Writeable for Event { } write_tlv_fields!(writer, { (0, payment_hash, required), + (1, receiver_node_id, option), (2, payment_secret, option), (4, amount_msat, required), (6, 0u64, required), // user_payment_id required for compatibility with 0.0.103 and earlier @@ -854,10 +869,11 @@ impl Writeable for Event { // We never write the OpenChannelRequest events as, upon disconnection, peers // drop any channels which have not yet exchanged funding_signed. }, - &Event::PaymentClaimed { ref payment_hash, ref amount_msat, ref purpose } => { + &Event::PaymentClaimed { ref payment_hash, ref amount_msat, ref purpose, ref receiver_node_id } => { 19u8.write(writer)?; write_tlv_fields!(writer, { (0, payment_hash, required), + (1, receiver_node_id, option), (2, purpose, required), (4, amount_msat, required), }); @@ -923,9 +939,11 @@ impl MaybeReadable for Event { let mut payment_preimage = None; let mut payment_secret = None; let mut amount_msat = 0; + let mut receiver_node_id = None; let mut _user_payment_id = None::; // For compatibility with 0.0.103 and earlier read_tlv_fields!(reader, { (0, payment_hash, required), + (1, receiver_node_id, option), (2, payment_secret, option), (4, amount_msat, required), (6, _user_payment_id, option), @@ -940,6 +958,7 @@ impl MaybeReadable for Event { None => return Err(msgs::DecodeError::InvalidValue), }; Ok(Some(Event::PaymentReceived { + receiver_node_id, payment_hash, amount_msat, purpose, @@ -1117,13 +1136,16 @@ impl MaybeReadable for Event { let mut payment_hash = PaymentHash([0; 32]); let mut purpose = None; let mut amount_msat = 0; + let mut receiver_node_id = None; read_tlv_fields!(reader, { (0, payment_hash, required), + (1, receiver_node_id, option), (2, purpose, ignorable), (4, amount_msat, required), }); if purpose.is_none() { return Ok(None); } Ok(Some(Event::PaymentClaimed { + receiver_node_id, payment_hash, purpose: purpose.unwrap(), amount_msat,