From: Valentine Wallace Date: Sat, 9 Sep 2023 23:36:18 +0000 (-0400) Subject: Test sending and receiving to 1-hop blinded paths X-Git-Tag: v0.0.117-alpha1~14^2~1 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=3e377a1d2f49e7b0f73b697afd039c2b660ca8b2;p=rust-lightning Test sending and receiving to 1-hop blinded paths --- diff --git a/lightning/src/ln/blinded_payment_tests.rs b/lightning/src/ln/blinded_payment_tests.rs new file mode 100644 index 000000000..dbc0420bf --- /dev/null +++ b/lightning/src/ln/blinded_payment_tests.rs @@ -0,0 +1,115 @@ +// This file is Copyright its original authors, visible in version control +// history. +// +// This file is licensed under the Apache License, Version 2.0 or the MIT license +// , at your option. +// You may not use this file except in accordance with one or both of these +// licenses. + +use bitcoin::secp256k1::Secp256k1; +use crate::blinded_path::BlindedPath; +use crate::blinded_path::payment::{PaymentConstraints, ReceiveTlvs}; +use crate::events::MessageSendEventsProvider; +use crate::ln::channelmanager; +use crate::ln::channelmanager::{PaymentId, RecipientOnionFields}; +use crate::ln::features::Bolt12InvoiceFeatures; +use crate::ln::functional_test_utils::*; +use crate::ln::outbound_payment::Retry; +use crate::prelude::*; +use crate::routing::router::{PaymentParameters, RouteParameters}; +use crate::util::config::UserConfig; + +#[test] +fn one_hop_blinded_path() { + do_one_hop_blinded_path(true); + do_one_hop_blinded_path(false); +} + +fn do_one_hop_blinded_path(success: bool) { + let chanmon_cfgs = create_chanmon_cfgs(2); + let node_cfgs = create_node_cfgs(2, &chanmon_cfgs); + let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]); + let nodes = create_network(2, &node_cfgs, &node_chanmgrs); + let chan_upd = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0).0.contents; + + let amt_msat = 5000; + let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[1], Some(amt_msat), None); + let payee_tlvs = ReceiveTlvs { + payment_secret, + payment_constraints: PaymentConstraints { + max_cltv_expiry: u32::max_value(), + htlc_minimum_msat: chan_upd.htlc_minimum_msat, + }, + }; + let mut secp_ctx = Secp256k1::new(); + let blinded_path = BlindedPath::new_for_payment( + &[], nodes[1].node.get_our_node_id(), payee_tlvs, chan_upd.htlc_maximum_msat, + &chanmon_cfgs[1].keys_manager, &secp_ctx + ).unwrap(); + + let route_params = RouteParameters { + payment_params: PaymentParameters::blinded(vec![blinded_path]), + final_value_msat: amt_msat + }; + nodes[0].node.send_payment(payment_hash, RecipientOnionFields::spontaneous_empty(), + PaymentId(payment_hash.0), route_params, Retry::Attempts(0)).unwrap(); + check_added_monitors(&nodes[0], 1); + pass_along_route(&nodes[0], &[&[&nodes[1]]], amt_msat, payment_hash, payment_secret); + if success { + claim_payment(&nodes[0], &[&nodes[1]], payment_preimage); + } else { + fail_payment(&nodes[0], &[&nodes[1]], payment_hash); + } +} + +#[test] +fn mpp_to_one_hop_blinded_path() { + let chanmon_cfgs = create_chanmon_cfgs(4); + let node_cfgs = create_node_cfgs(4, &chanmon_cfgs); + let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]); + let nodes = create_network(4, &node_cfgs, &node_chanmgrs); + let mut secp_ctx = Secp256k1::new(); + + create_announced_chan_between_nodes(&nodes, 0, 1); + create_announced_chan_between_nodes(&nodes, 0, 2); + let chan_upd_1_3 = create_announced_chan_between_nodes(&nodes, 1, 3).0.contents; + create_announced_chan_between_nodes(&nodes, 2, 3).0.contents; + + let amt_msat = 15_000_000; + let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[3], Some(amt_msat), None); + let payee_tlvs = ReceiveTlvs { + payment_secret, + payment_constraints: PaymentConstraints { + max_cltv_expiry: u32::max_value(), + htlc_minimum_msat: chan_upd_1_3.htlc_minimum_msat, + }, + }; + let blinded_path = BlindedPath::new_for_payment( + &[], nodes[3].node.get_our_node_id(), payee_tlvs, u64::max_value(), + &chanmon_cfgs[3].keys_manager, &secp_ctx + ).unwrap(); + + let bolt12_features: Bolt12InvoiceFeatures = + channelmanager::provided_invoice_features(&UserConfig::default()).to_context(); + let route_params = RouteParameters { + payment_params: PaymentParameters::blinded(vec![blinded_path]) + .with_bolt12_features(bolt12_features).unwrap(), + final_value_msat: amt_msat, + }; + nodes[0].node.send_payment(payment_hash, RecipientOnionFields::spontaneous_empty(), PaymentId(payment_hash.0), route_params, Retry::Attempts(0)).unwrap(); + check_added_monitors(&nodes[0], 2); + + let expected_route: &[&[&Node]] = &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]]; + let mut events = nodes[0].node.get_and_clear_pending_msg_events(); + assert_eq!(events.len(), 2); + + let ev = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events); + pass_along_path(&nodes[0], expected_route[0], amt_msat, payment_hash.clone(), + Some(payment_secret), ev.clone(), false, None); + + let ev = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut events); + pass_along_path(&nodes[0], expected_route[1], amt_msat, payment_hash.clone(), + Some(payment_secret), ev.clone(), true, None); + claim_payment_along_route(&nodes[0], expected_route, false, payment_preimage); +} diff --git a/lightning/src/ln/mod.rs b/lightning/src/ln/mod.rs index bacb9e86c..e03190a9d 100644 --- a/lightning/src/ln/mod.rs +++ b/lightning/src/ln/mod.rs @@ -43,6 +43,9 @@ pub mod wire; // without the node parameter being mut. This is incorrect, and thus newer rustcs will complain // about an unnecessary mut. Thus, we silence the unused_mut warning in two test modules below. +#[cfg(test)] +#[allow(unused_mut)] +mod blinded_payment_tests; #[cfg(test)] #[allow(unused_mut)] mod functional_tests;