Skip to content

Commit 17231f7

Browse files
committed
BlindedPath with unannounced introduction node
When creating blinded paths for receiving onion messages, allow using the recipient's only peer as the introduction node when the recipient is unannounced. This allows for sending messages without going through an intermediary, which is useful for testing or when only connected to an LSP with an empty NetworkGraph.
1 parent a666401 commit 17231f7

File tree

4 files changed

+60
-23
lines changed

4 files changed

+60
-23
lines changed

lightning/src/blinded_path/message.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use core::mem;
3030
use core::ops::Deref;
3131

3232
/// An intermediate node, and possibly a short channel id leading to the next node.
33-
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
33+
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
3434
pub struct ForwardNode {
3535
/// This node's pubkey.
3636
pub node_id: PublicKey,

lightning/src/ln/offers_tests.rs

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -865,43 +865,75 @@ fn pays_for_refund_without_blinded_paths() {
865865
expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
866866
}
867867

868-
/// Fails creating an offer when a blinded path cannot be created without exposing the node's id.
868+
/// Checks that an offer can be created using an unannounced node as a blinded path's introduction
869+
/// node. This is only preferred if there are no other options which may indicated either the offer
870+
/// is intended for the unannounced node or that the node is actually announced (e.g., an LSP) but
871+
/// the recipient doesn't have a network graph.
869872
#[test]
870-
fn fails_creating_offer_without_blinded_paths() {
873+
fn creates_offer_with_blinded_path_using_unannounced_introduction_node() {
871874
let chanmon_cfgs = create_chanmon_cfgs(2);
872875
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
873876
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
874877
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
875878

876879
create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
877880

878-
match nodes[0].node.create_offer_builder(None) {
879-
Ok(_) => panic!("Expected error"),
880-
Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
881+
let alice = &nodes[0];
882+
let alice_id = alice.node.get_our_node_id();
883+
let bob = &nodes[1];
884+
let bob_id = bob.node.get_our_node_id();
885+
886+
let offer = alice.node
887+
.create_offer_builder(None).unwrap()
888+
.amount_msats(10_000_000)
889+
.build().unwrap();
890+
assert_ne!(offer.signing_pubkey(), Some(alice_id));
891+
assert!(!offer.paths().is_empty());
892+
for path in offer.paths() {
893+
assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
881894
}
895+
896+
let payment_id = PaymentId([1; 32]);
897+
bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
898+
expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
899+
900+
let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
901+
alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
902+
903+
let (_, reply_path) = extract_invoice_request(alice, &onion_message);
904+
assert_eq!(reply_path.introduction_node, IntroductionNode::NodeId(alice_id));
882905
}
883906

884-
/// Fails creating a refund when a blinded path cannot be created without exposing the node's id.
907+
/// Checks that a refund can be created using an unannounced node as a blinded path's introduction
908+
/// node. This is only preferred if there are no other options which may indicated either the refund
909+
/// is intended for the unannounced node or that the node is actually announced (e.g., an LSP) but
910+
/// the sender doesn't have a network graph.
885911
#[test]
886-
fn fails_creating_refund_without_blinded_paths() {
912+
fn creates_refund_with_blinded_path_using_unannounced_introduction_node() {
887913
let chanmon_cfgs = create_chanmon_cfgs(2);
888914
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
889915
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
890916
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
891917

892918
create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
893919

920+
let alice = &nodes[0];
921+
let alice_id = alice.node.get_our_node_id();
922+
let bob = &nodes[1];
923+
let bob_id = bob.node.get_our_node_id();
924+
894925
let absolute_expiry = Duration::from_secs(u64::MAX);
895926
let payment_id = PaymentId([1; 32]);
896-
897-
match nodes[0].node.create_refund_builder(
898-
10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
899-
) {
900-
Ok(_) => panic!("Expected error"),
901-
Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
927+
let refund = bob.node
928+
.create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
929+
.unwrap()
930+
.build().unwrap();
931+
assert_ne!(refund.payer_id(), bob_id);
932+
assert!(!refund.paths().is_empty());
933+
for path in refund.paths() {
934+
assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
902935
}
903-
904-
assert!(nodes[0].node.list_recent_payments().is_empty());
936+
expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
905937
}
906938

907939
/// Fails creating or paying an offer when a blinded path cannot be created because no peers are
@@ -1080,8 +1112,7 @@ fn fails_sending_invoice_with_unsupported_chain_for_refund() {
10801112
}
10811113
}
10821114

1083-
/// Fails creating an invoice request when a blinded reply path cannot be created without exposing
1084-
/// the node's id.
1115+
/// Fails creating an invoice request when a blinded reply path cannot be created.
10851116
#[test]
10861117
fn fails_creating_invoice_request_without_blinded_reply_path() {
10871118
let chanmon_cfgs = create_chanmon_cfgs(6);
@@ -1098,7 +1129,7 @@ fn fails_creating_invoice_request_without_blinded_reply_path() {
10981129
let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
10991130

11001131
disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
1101-
disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
1132+
disconnect_peers(david, &[bob, charlie, &nodes[4], &nodes[5]]);
11021133

11031134
let offer = alice.node
11041135
.create_offer_builder(None).unwrap()

lightning/src/onion_message/functional_tests.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,8 +492,9 @@ fn async_response_with_reply_path_fails() {
492492
let path_id = Some([2; 32]);
493493
let reply_path = BlindedPath::new_for_message(&[], bob.node_id, &*bob.entropy_source, &secp_ctx).unwrap();
494494

495-
// Alice tries to asynchronously respond to Bob, but fails because the nodes are unannounced.
496-
// Therefore, the reply_path cannot be used for the response.
495+
// Alice tries to asynchronously respond to Bob, but fails because the nodes are unannounced and
496+
// disconnected. Thus, a reply path could no be created for the response.
497+
disconnect_peers(alice, bob);
497498
let responder = Responder::new(reply_path, path_id);
498499
alice.custom_message_handler.expect_message_and_response(message.clone());
499500
let response_instruction = alice.custom_message_handler.handle_custom_message(message, Some(responder));

lightning/src/onion_message/messenger.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ where
483483
}
484484

485485
fn create_blinded_paths_from_iter<
486-
I: Iterator<Item = ForwardNode>,
486+
I: ExactSizeIterator<Item = ForwardNode>,
487487
T: secp256k1::Signing + secp256k1::Verification
488488
>(
489489
&self, recipient: PublicKey, peers: I, secp_ctx: &Secp256k1<T>, compact_paths: bool
@@ -499,13 +499,18 @@ where
499499
let is_recipient_announced =
500500
network_graph.nodes().contains_key(&NodeId::from_pubkey(&recipient));
501501

502+
let has_one_peer = peers.len() == 1;
502503
let mut peer_info = peers
503-
// Limit to peers with announced channels
504+
// Limit to peers with announced channels unless the recipient is unannounced.
504505
.filter_map(|peer|
505506
network_graph
506507
.node(&NodeId::from_pubkey(&peer.node_id))
507508
.filter(|info| info.channels.len() >= MIN_PEER_CHANNELS)
508509
.map(|info| (peer, info.is_tor_only(), info.channels.len()))
510+
// Allow messages directly with the only peer when unannounced.
511+
.or_else(|| (!is_recipient_announced && has_one_peer)
512+
.then(|| (peer, false, 0))
513+
)
509514
)
510515
// Exclude Tor-only nodes when the recipient is announced.
511516
.filter(|(_, is_tor_only, _)| !(*is_tor_only && is_recipient_announced))

0 commit comments

Comments
 (0)