Skip to content

Commit 642913c

Browse files
committed
Advance self blinded payment paths
DefaultRouter will ignore blinded paths where the sender is the introduction node. Similar to message paths, advance such paths by one so that payments may be sent to them.
1 parent 55ba2aa commit 642913c

File tree

4 files changed

+66
-10
lines changed

4 files changed

+66
-10
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4031,8 +4031,8 @@ where
40314031
self.pending_outbound_payments
40324032
.send_payment_for_bolt12_invoice(
40334033
invoice, payment_id, &self.router, self.list_usable_channels(),
4034-
|| self.compute_inflight_htlcs(), &self.entropy_source, &self.node_signer,
4035-
best_block_height, &self.logger, &self.pending_events,
4034+
|| self.compute_inflight_htlcs(), &self.entropy_source, &self.node_signer, &self,
4035+
&self.secp_ctx, best_block_height, &self.logger, &self.pending_events,
40364036
|args| self.send_payment_along_path(args)
40374037
)
40384038
}

lightning/src/ln/offers_tests.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,17 +986,32 @@ fn creates_offer_with_blinded_path_using_unannounced_introduction_node() {
986986
alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
987987

988988
let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
989+
let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
990+
offer_id: offer.id(),
991+
invoice_request: InvoiceRequestFields {
992+
payer_id: invoice_request.payer_id(),
993+
quantity: None,
994+
payer_note_truncated: None,
995+
},
996+
});
989997
assert_ne!(invoice_request.payer_id(), bob_id);
990998
assert_eq!(reply_path.introduction_node, IntroductionNode::NodeId(alice_id));
991999

9921000
let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
1001+
bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
9931002

9941003
let invoice = extract_invoice(bob, &onion_message);
9951004
assert_ne!(invoice.signing_pubkey(), alice_id);
9961005
assert!(!invoice.payment_paths().is_empty());
9971006
for (_, path) in invoice.payment_paths() {
9981007
assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
9991008
}
1009+
1010+
route_bolt12_payment(bob, &[alice], &invoice);
1011+
expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
1012+
1013+
claim_bolt12_payment(bob, &[alice], payment_context);
1014+
expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
10001015
}
10011016

10021017
/// Checks that a refund can be created using an unannounced node as a blinded path's introduction

lightning/src/ln/outbound_payment.rs

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ use bitcoin::hashes::Hash;
1313
use bitcoin::hashes::sha256::Hash as Sha256;
1414
use bitcoin::secp256k1::{self, Secp256k1, SecretKey};
1515

16+
use crate::blinded_path::{IntroductionNode, NodeIdLookUp};
17+
use crate::blinded_path::payment::advance_path_by_one;
1618
use crate::events::{self, PaymentFailureReason};
1719
use crate::ln::types::{PaymentHash, PaymentPreimage, PaymentSecret};
1820
use crate::ln::channel_state::ChannelDetails;
@@ -775,17 +777,21 @@ impl OutboundPayments {
775777
}
776778
}
777779

778-
pub(super) fn send_payment_for_bolt12_invoice<R: Deref, ES: Deref, NS: Deref, IH, SP, L: Deref>(
780+
pub(super) fn send_payment_for_bolt12_invoice<
781+
R: Deref, ES: Deref, NS: Deref, NL: Deref, IH, SP, L: Deref
782+
>(
779783
&self, invoice: &Bolt12Invoice, payment_id: PaymentId, router: &R,
780784
first_hops: Vec<ChannelDetails>, inflight_htlcs: IH, entropy_source: &ES, node_signer: &NS,
781-
best_block_height: u32, logger: &L,
785+
node_id_lookup: &NL, secp_ctx: &Secp256k1<secp256k1::All>, best_block_height: u32,
786+
logger: &L,
782787
pending_events: &Mutex<VecDeque<(events::Event, Option<EventCompletionAction>)>>,
783788
send_payment_along_path: SP,
784789
) -> Result<(), Bolt12PaymentError>
785790
where
786791
R::Target: Router,
787792
ES::Target: EntropySource,
788793
NS::Target: NodeSigner,
794+
NL::Target: NodeIdLookUp,
789795
L::Target: Logger,
790796
IH: Fn() -> InFlightHtlcs,
791797
SP: Fn(SendAlongPathArgs) -> Result<(), APIError>,
@@ -807,7 +813,26 @@ impl OutboundPayments {
807813
hash_map::Entry::Vacant(_) => return Err(Bolt12PaymentError::UnexpectedInvoice),
808814
};
809815

810-
let payment_params = PaymentParameters::from_bolt12_invoice(&invoice);
816+
let mut payment_params = PaymentParameters::from_bolt12_invoice(&invoice);
817+
818+
// Advance any blinded path where the introduction node is our node.
819+
if let Ok(our_node_id) = node_signer.get_node_id(Recipient::Node) {
820+
for (_, path) in payment_params.payee.blinded_route_hints_mut().iter_mut() {
821+
let introduction_node_id = match path.introduction_node {
822+
IntroductionNode::NodeId(pubkey) => pubkey,
823+
IntroductionNode::DirectedShortChannelId(direction, scid) => {
824+
match node_id_lookup.next_node_id(scid) {
825+
Some(next_node_id) => *direction.select_pubkey(&our_node_id, &next_node_id),
826+
None => continue,
827+
}
828+
},
829+
};
830+
if introduction_node_id == our_node_id {
831+
let _ = advance_path_by_one(path, node_signer, node_id_lookup, secp_ctx);
832+
}
833+
}
834+
}
835+
811836
let amount_msat = invoice.amount_msats();
812837
let mut route_params = RouteParameters::from_payment_params_and_value(
813838
payment_params, amount_msat
@@ -1858,6 +1883,7 @@ mod tests {
18581883

18591884
use core::time::Duration;
18601885

1886+
use crate::blinded_path::EmptyNodeIdLookUp;
18611887
use crate::events::{Event, PathFailure, PaymentFailureReason};
18621888
use crate::ln::types::PaymentHash;
18631889
use crate::ln::channelmanager::{PaymentId, RecipientOnionFields};
@@ -2201,6 +2227,7 @@ mod tests {
22012227
let network_graph = Arc::new(NetworkGraph::new(Network::Testnet, &logger));
22022228
let scorer = RwLock::new(test_utils::TestScorer::new());
22032229
let router = test_utils::TestRouter::new(network_graph, &logger, &scorer);
2230+
let secp_ctx = Secp256k1::new();
22042231
let keys_manager = test_utils::TestKeysInterface::new(&[0; 32], Network::Testnet);
22052232

22062233
let pending_events = Mutex::new(VecDeque::new());
@@ -2229,7 +2256,8 @@ mod tests {
22292256
assert_eq!(
22302257
outbound_payments.send_payment_for_bolt12_invoice(
22312258
&invoice, payment_id, &&router, vec![], || InFlightHtlcs::new(), &&keys_manager,
2232-
&&keys_manager, 0, &&logger, &pending_events, |_| panic!()
2259+
&&keys_manager, &EmptyNodeIdLookUp {}, &secp_ctx, 0, &&logger, &pending_events,
2260+
|_| panic!()
22332261
),
22342262
Ok(()),
22352263
);
@@ -2252,6 +2280,7 @@ mod tests {
22522280
let network_graph = Arc::new(NetworkGraph::new(Network::Testnet, &logger));
22532281
let scorer = RwLock::new(test_utils::TestScorer::new());
22542282
let router = test_utils::TestRouter::new(network_graph, &logger, &scorer);
2283+
let secp_ctx = Secp256k1::new();
22552284
let keys_manager = test_utils::TestKeysInterface::new(&[0; 32], Network::Testnet);
22562285

22572286
let pending_events = Mutex::new(VecDeque::new());
@@ -2288,7 +2317,8 @@ mod tests {
22882317
assert_eq!(
22892318
outbound_payments.send_payment_for_bolt12_invoice(
22902319
&invoice, payment_id, &&router, vec![], || InFlightHtlcs::new(), &&keys_manager,
2291-
&&keys_manager, 0, &&logger, &pending_events, |_| panic!()
2320+
&&keys_manager, &EmptyNodeIdLookUp {}, &secp_ctx, 0, &&logger, &pending_events,
2321+
|_| panic!()
22922322
),
22932323
Ok(()),
22942324
);
@@ -2311,6 +2341,7 @@ mod tests {
23112341
let network_graph = Arc::new(NetworkGraph::new(Network::Testnet, &logger));
23122342
let scorer = RwLock::new(test_utils::TestScorer::new());
23132343
let router = test_utils::TestRouter::new(network_graph, &logger, &scorer);
2344+
let secp_ctx = Secp256k1::new();
23142345
let keys_manager = test_utils::TestKeysInterface::new(&[0; 32], Network::Testnet);
23152346

23162347
let pending_events = Mutex::new(VecDeque::new());
@@ -2360,7 +2391,8 @@ mod tests {
23602391
assert_eq!(
23612392
outbound_payments.send_payment_for_bolt12_invoice(
23622393
&invoice, payment_id, &&router, vec![], || InFlightHtlcs::new(), &&keys_manager,
2363-
&&keys_manager, 0, &&logger, &pending_events, |_| panic!()
2394+
&&keys_manager, &EmptyNodeIdLookUp {}, &secp_ctx, 0, &&logger, &pending_events,
2395+
|_| panic!()
23642396
),
23652397
Err(Bolt12PaymentError::UnexpectedInvoice),
23662398
);
@@ -2377,7 +2409,8 @@ mod tests {
23772409
assert_eq!(
23782410
outbound_payments.send_payment_for_bolt12_invoice(
23792411
&invoice, payment_id, &&router, vec![], || InFlightHtlcs::new(), &&keys_manager,
2380-
&&keys_manager, 0, &&logger, &pending_events, |_| Ok(())
2412+
&&keys_manager, &EmptyNodeIdLookUp {}, &secp_ctx, 0, &&logger, &pending_events,
2413+
|_| Ok(())
23812414
),
23822415
Ok(()),
23832416
);
@@ -2387,7 +2420,8 @@ mod tests {
23872420
assert_eq!(
23882421
outbound_payments.send_payment_for_bolt12_invoice(
23892422
&invoice, payment_id, &&router, vec![], || InFlightHtlcs::new(), &&keys_manager,
2390-
&&keys_manager, 0, &&logger, &pending_events, |_| panic!()
2423+
&&keys_manager, &EmptyNodeIdLookUp {}, &secp_ctx, 0, &&logger, &pending_events,
2424+
|_| panic!()
23912425
),
23922426
Err(Bolt12PaymentError::DuplicateInvoice),
23932427
);

lightning/src/routing/router.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,13 @@ impl Payee {
10451045
}
10461046
}
10471047

1048+
pub(crate) fn blinded_route_hints_mut(&mut self) -> &mut [(BlindedPayInfo, BlindedPath)] {
1049+
match self {
1050+
Self::Blinded { route_hints, .. } => &mut route_hints[..],
1051+
Self::Clear { .. } => &mut []
1052+
}
1053+
}
1054+
10481055
fn unblinded_route_hints(&self) -> &[RouteHint] {
10491056
match self {
10501057
Self::Blinded { .. } => &[],

0 commit comments

Comments
 (0)