Skip to content

Commit

Permalink
Support sending blinded MPP payments
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinewallace committed Aug 29, 2023
1 parent ef57ab0 commit 223e2de
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
72 changes: 71 additions & 1 deletion lightning/src/ln/blinded_payment_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ use bitcoin::secp256k1::Secp256k1;
use crate::blinded_path::BlindedPath;
use crate::blinded_path::payment::{ForwardTlvs, PaymentConstraints, PaymentRelay, ReceiveTlvs};
use crate::events::MessageSendEventsProvider;
use crate::ln::channelmanager;
use crate::ln::channelmanager::{PaymentId, RecipientOnionFields, RetryableSendFailure};
use crate::ln::features::BlindedHopFeatures;
use crate::ln::features::{BlindedHopFeatures, Bolt12InvoiceFeatures};
use crate::ln::functional_test_utils::*;
use crate::ln::msgs::ChannelMessageHandler;
use crate::ln::outbound_payment::Retry;
use crate::prelude::*;
use crate::routing::router::{PaymentParameters, RouteParameters};
use crate::util::config::UserConfig;

#[test]
fn simple_blinded_payment() {
Expand Down Expand Up @@ -70,6 +72,74 @@ fn simple_blinded_payment() {
claim_payment(&nodes[0], &[&nodes[1], &nodes[2], &nodes[3]], payment_preimage);
}

#[test]
fn blinded_mpp() {
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;
let chan_upd_2_3 = 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 mut blinded_paths = Vec::new();
for (idx, chan_upd) in [chan_upd_1_3, chan_upd_2_3].iter().enumerate() {
let intermediate_nodes = vec![(nodes[idx + 1].node.get_our_node_id(), ForwardTlvs {
short_channel_id: chan_upd.short_channel_id,
payment_relay: PaymentRelay {
cltv_expiry_delta: chan_upd.cltv_expiry_delta,
fee_proportional_millionths: chan_upd.fee_proportional_millionths,
fee_base_msat: chan_upd.fee_base_msat,
},
payment_constraints: PaymentConstraints {
max_cltv_expiry: u32::max_value(),
htlc_minimum_msat: chan_upd.htlc_minimum_msat,
},
features: BlindedHopFeatures::empty(),
})];
let payee_tlvs = ReceiveTlvs {
payment_secret,
payment_constraints: PaymentConstraints {
max_cltv_expiry: u32::max_value(),
htlc_minimum_msat: chan_upd.htlc_minimum_msat,
},
};
let blinded_path = BlindedPath::new_for_payment(
&intermediate_nodes[..], nodes[3].node.get_our_node_id(), payee_tlvs,
chan_upd.htlc_maximum_msat, &chanmon_cfgs[3].keys_manager, &secp_ctx
).unwrap();
blinded_paths.push(blinded_path);
}

let bolt12_features: Bolt12InvoiceFeatures =
channelmanager::provided_invoice_features(&UserConfig::default()).to_context();
let route_params = RouteParameters {
payment_params: PaymentParameters::blinded(blinded_paths)
.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);
}

#[test]
fn blinded_intercept_payment() {
let chanmon_cfgs = create_chanmon_cfgs(3);
Expand Down
4 changes: 3 additions & 1 deletion lightning/src/ln/outbound_payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,9 @@ impl OutboundPayments {
if route.paths.len() < 1 {
return Err(PaymentSendFailure::ParameterError(APIError::InvalidRoute{err: "There must be at least one path to send over".to_owned()}));
}
if recipient_onion.payment_secret.is_none() && route.paths.len() > 1 {
if recipient_onion.payment_secret.is_none() && route.paths.len() > 1
&& !route.paths.iter().any(|p| p.blinded_tail.is_some())
{
return Err(PaymentSendFailure::ParameterError(APIError::APIMisuseError{err: "Payment secret is required for multi-path payments".to_owned()}));
}
let mut total_value = 0;
Expand Down

0 comments on commit 223e2de

Please sign in to comment.