Skip to content

Commit 0cc0858

Browse files
authored
Merge pull request #2951 from jkczyz/2024-03-fix-sender-is-intro-node
Fix sender is the introduction node onion messages
2 parents a36b529 + 806fef5 commit 0cc0858

File tree

4 files changed

+76
-93
lines changed

4 files changed

+76
-93
lines changed

lightning/src/onion_message/functional_tests.rs

Lines changed: 68 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -11,60 +11,51 @@
1111
1212
use crate::blinded_path::BlindedPath;
1313
use crate::events::{Event, EventsProvider};
14-
use crate::ln::features::InitFeatures;
15-
use crate::ln::msgs::{self, DecodeError, OnionMessageHandler, SocketAddress};
14+
use crate::ln::features::{ChannelFeatures, InitFeatures};
15+
use crate::ln::msgs::{self, DecodeError, OnionMessageHandler};
16+
use crate::routing::gossip::{NetworkGraph, P2PGossipSync};
17+
use crate::routing::test_utils::{add_channel, add_or_update_node};
1618
use crate::sign::{NodeSigner, Recipient};
1719
use crate::util::ser::{FixedLengthReader, LengthReadable, Writeable, Writer};
1820
use crate::util::test_utils;
19-
use super::messenger::{CustomOnionMessageHandler, Destination, MessageRouter, OnionMessagePath, OnionMessenger, PendingOnionMessage, SendError};
21+
use super::messenger::{CustomOnionMessageHandler, DefaultMessageRouter, Destination, OnionMessagePath, OnionMessenger, PendingOnionMessage, SendError};
2022
use super::offers::{OffersMessage, OffersMessageHandler};
2123
use super::packet::{OnionMessageContents, Packet};
2224

2325
use bitcoin::network::constants::Network;
2426
use bitcoin::hashes::hex::FromHex;
25-
use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey, self};
27+
use bitcoin::secp256k1::{All, PublicKey, Secp256k1, SecretKey};
2628

2729
use crate::io;
2830
use crate::io_extras::read_to_end;
2931
use crate::sync::{Arc, Mutex};
3032

33+
use core::ops::Deref;
34+
3135
use crate::prelude::*;
3236

3337
struct MessengerNode {
3438
node_id: PublicKey,
39+
privkey: SecretKey,
3540
entropy_source: Arc<test_utils::TestKeysInterface>,
3641
messenger: OnionMessenger<
3742
Arc<test_utils::TestKeysInterface>,
3843
Arc<test_utils::TestNodeSigner>,
3944
Arc<test_utils::TestLogger>,
40-
Arc<TestMessageRouter>,
45+
Arc<DefaultMessageRouter<
46+
Arc<NetworkGraph<Arc<test_utils::TestLogger>>>,
47+
Arc<test_utils::TestLogger>,
48+
Arc<test_utils::TestKeysInterface>
49+
>>,
4150
Arc<TestOffersMessageHandler>,
4251
Arc<TestCustomMessageHandler>
4352
>,
4453
custom_message_handler: Arc<TestCustomMessageHandler>,
45-
}
46-
47-
struct TestMessageRouter {}
48-
49-
impl MessageRouter for TestMessageRouter {
50-
fn find_path(
51-
&self, _sender: PublicKey, _peers: Vec<PublicKey>, destination: Destination
52-
) -> Result<OnionMessagePath, ()> {
53-
Ok(OnionMessagePath {
54-
intermediate_nodes: vec![],
55-
destination,
56-
first_node_addresses:
57-
Some(vec![SocketAddress::TcpIpV4 { addr: [127, 0, 0, 1], port: 1000 }]),
58-
})
59-
}
60-
61-
fn create_blinded_paths<
62-
T: secp256k1::Signing + secp256k1::Verification
63-
>(
64-
&self, _recipient: PublicKey, _peers: Vec<PublicKey>, _secp_ctx: &Secp256k1<T>,
65-
) -> Result<Vec<BlindedPath>, ()> {
66-
unreachable!()
67-
}
54+
gossip_sync: Arc<P2PGossipSync<
55+
Arc<NetworkGraph<Arc<test_utils::TestLogger>>>,
56+
Arc<test_utils::TestChainSource>,
57+
Arc<test_utils::TestLogger>
58+
>>
6859
}
6960

7061
struct TestOffersMessageHandler {}
@@ -171,24 +162,34 @@ fn create_nodes(num_messengers: u8) -> Vec<MessengerNode> {
171162
}
172163

173164
fn create_nodes_using_secrets(secrets: Vec<SecretKey>) -> Vec<MessengerNode> {
165+
let gossip_logger = Arc::new(test_utils::TestLogger::with_id("gossip".to_string()));
166+
let network_graph = Arc::new(NetworkGraph::new(Network::Testnet, gossip_logger.clone()));
167+
let gossip_sync = Arc::new(
168+
P2PGossipSync::new(network_graph.clone(), None, gossip_logger)
169+
);
170+
174171
let mut nodes = Vec::new();
175172
for (i, secret_key) in secrets.into_iter().enumerate() {
176173
let logger = Arc::new(test_utils::TestLogger::with_id(format!("node {}", i)));
177174
let seed = [i as u8; 32];
178175
let entropy_source = Arc::new(test_utils::TestKeysInterface::new(&seed, Network::Testnet));
179176
let node_signer = Arc::new(test_utils::TestNodeSigner::new(secret_key));
180177

181-
let message_router = Arc::new(TestMessageRouter {});
178+
let message_router = Arc::new(
179+
DefaultMessageRouter::new(network_graph.clone(), entropy_source.clone())
180+
);
182181
let offers_message_handler = Arc::new(TestOffersMessageHandler {});
183182
let custom_message_handler = Arc::new(TestCustomMessageHandler::new());
184183
nodes.push(MessengerNode {
184+
privkey: secret_key,
185185
node_id: node_signer.get_node_id(Recipient::Node).unwrap(),
186186
entropy_source: entropy_source.clone(),
187187
messenger: OnionMessenger::new(
188188
entropy_source, node_signer, logger.clone(), message_router,
189189
offers_message_handler, custom_message_handler.clone()
190190
),
191191
custom_message_handler,
192+
gossip_sync: gossip_sync.clone(),
192193
});
193194
}
194195
for i in 0..nodes.len() - 1 {
@@ -216,6 +217,20 @@ fn release_events(node: &MessengerNode) -> Vec<Event> {
216217
events.into_inner()
217218
}
218219

220+
fn add_channel_to_graph(
221+
node_a: &MessengerNode, node_b: &MessengerNode, secp_ctx: &Secp256k1<All>, short_channel_id: u64
222+
) {
223+
let gossip_sync = node_a.gossip_sync.deref();
224+
let privkey_a = &node_a.privkey;
225+
let privkey_b = &node_b.privkey;
226+
let channel_features = ChannelFeatures::empty();
227+
let node_features_a = node_a.messenger.provided_node_features();
228+
let node_features_b = node_b.messenger.provided_node_features();
229+
add_channel(gossip_sync, secp_ctx, privkey_a, privkey_b, channel_features, short_channel_id);
230+
add_or_update_node(gossip_sync, secp_ctx, privkey_a, node_features_a, 1);
231+
add_or_update_node(gossip_sync, secp_ctx, privkey_b, node_features_b, 1);
232+
}
233+
219234
fn pass_along_path(path: &Vec<MessengerNode>) {
220235
let mut prev_node = &path[0];
221236
for node in path.into_iter().skip(1) {
@@ -235,12 +250,8 @@ fn one_unblinded_hop() {
235250
let nodes = create_nodes(2);
236251
let test_msg = TestCustomMessage::Response;
237252

238-
let path = OnionMessagePath {
239-
intermediate_nodes: vec![],
240-
destination: Destination::Node(nodes[1].node_id),
241-
first_node_addresses: None,
242-
};
243-
nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap();
253+
let destination = Destination::Node(nodes[1].node_id);
254+
nodes[0].messenger.send_onion_message(test_msg, destination, None).unwrap();
244255
nodes[1].custom_message_handler.expect_message(TestCustomMessage::Response);
245256
pass_along_path(&nodes);
246257
}
@@ -255,6 +266,7 @@ fn two_unblinded_hops() {
255266
destination: Destination::Node(nodes[2].node_id),
256267
first_node_addresses: None,
257268
};
269+
258270
nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap();
259271
nodes[2].custom_message_handler.expect_message(TestCustomMessage::Response);
260272
pass_along_path(&nodes);
@@ -267,12 +279,8 @@ fn one_blinded_hop() {
267279

268280
let secp_ctx = Secp256k1::new();
269281
let blinded_path = BlindedPath::new_for_message(&[nodes[1].node_id], &*nodes[1].entropy_source, &secp_ctx).unwrap();
270-
let path = OnionMessagePath {
271-
intermediate_nodes: vec![],
272-
destination: Destination::BlindedPath(blinded_path),
273-
first_node_addresses: None,
274-
};
275-
nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap();
282+
let destination = Destination::BlindedPath(blinded_path);
283+
nodes[0].messenger.send_onion_message(test_msg, destination, None).unwrap();
276284
nodes[1].custom_message_handler.expect_message(TestCustomMessage::Response);
277285
pass_along_path(&nodes);
278286
}
@@ -302,13 +310,9 @@ fn three_blinded_hops() {
302310

303311
let secp_ctx = Secp256k1::new();
304312
let blinded_path = BlindedPath::new_for_message(&[nodes[1].node_id, nodes[2].node_id, nodes[3].node_id], &*nodes[3].entropy_source, &secp_ctx).unwrap();
305-
let path = OnionMessagePath {
306-
intermediate_nodes: vec![],
307-
destination: Destination::BlindedPath(blinded_path),
308-
first_node_addresses: None,
309-
};
313+
let destination = Destination::BlindedPath(blinded_path);
310314

311-
nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap();
315+
nodes[0].messenger.send_onion_message(test_msg, destination, None).unwrap();
312316
nodes[3].custom_message_handler.expect_message(TestCustomMessage::Response);
313317
pass_along_path(&nodes);
314318
}
@@ -339,24 +343,16 @@ fn we_are_intro_node() {
339343

340344
let secp_ctx = Secp256k1::new();
341345
let blinded_path = BlindedPath::new_for_message(&[nodes[0].node_id, nodes[1].node_id, nodes[2].node_id], &*nodes[2].entropy_source, &secp_ctx).unwrap();
342-
let path = OnionMessagePath {
343-
intermediate_nodes: vec![],
344-
destination: Destination::BlindedPath(blinded_path),
345-
first_node_addresses: None,
346-
};
346+
let destination = Destination::BlindedPath(blinded_path);
347347

348-
nodes[0].messenger.send_onion_message_using_path(path, test_msg.clone(), None).unwrap();
348+
nodes[0].messenger.send_onion_message(test_msg.clone(), destination, None).unwrap();
349349
nodes[2].custom_message_handler.expect_message(TestCustomMessage::Response);
350350
pass_along_path(&nodes);
351351

352352
// Try with a two-hop blinded path where we are the introduction node.
353353
let blinded_path = BlindedPath::new_for_message(&[nodes[0].node_id, nodes[1].node_id], &*nodes[1].entropy_source, &secp_ctx).unwrap();
354-
let path = OnionMessagePath {
355-
intermediate_nodes: vec![],
356-
destination: Destination::BlindedPath(blinded_path),
357-
first_node_addresses: None,
358-
};
359-
nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap();
354+
let destination = Destination::BlindedPath(blinded_path);
355+
nodes[0].messenger.send_onion_message(test_msg, destination, None).unwrap();
360356
nodes[1].custom_message_handler.expect_message(TestCustomMessage::Response);
361357
nodes.remove(2);
362358
pass_along_path(&nodes);
@@ -372,12 +368,8 @@ fn invalid_blinded_path_error() {
372368
let secp_ctx = Secp256k1::new();
373369
let mut blinded_path = BlindedPath::new_for_message(&[nodes[1].node_id, nodes[2].node_id], &*nodes[2].entropy_source, &secp_ctx).unwrap();
374370
blinded_path.blinded_hops.clear();
375-
let path = OnionMessagePath {
376-
intermediate_nodes: vec![],
377-
destination: Destination::BlindedPath(blinded_path),
378-
first_node_addresses: None,
379-
};
380-
let err = nodes[0].messenger.send_onion_message_using_path(path, test_msg.clone(), None).unwrap_err();
371+
let destination = Destination::BlindedPath(blinded_path);
372+
let err = nodes[0].messenger.send_onion_message(test_msg, destination, None).unwrap_err();
381373
assert_eq!(err, SendError::TooFewBlindedHops);
382374
}
383375

@@ -404,14 +396,10 @@ fn reply_path() {
404396

405397
// Destination::BlindedPath
406398
let blinded_path = BlindedPath::new_for_message(&[nodes[1].node_id, nodes[2].node_id, nodes[3].node_id], &*nodes[3].entropy_source, &secp_ctx).unwrap();
407-
let path = OnionMessagePath {
408-
intermediate_nodes: vec![],
409-
destination: Destination::BlindedPath(blinded_path),
410-
first_node_addresses: None,
411-
};
399+
let destination = Destination::BlindedPath(blinded_path);
412400
let reply_path = BlindedPath::new_for_message(&[nodes[2].node_id, nodes[1].node_id, nodes[0].node_id], &*nodes[0].entropy_source, &secp_ctx).unwrap();
413401

414-
nodes[0].messenger.send_onion_message_using_path(path, test_msg, Some(reply_path)).unwrap();
402+
nodes[0].messenger.send_onion_message(test_msg, destination, Some(reply_path)).unwrap();
415403
nodes[3].custom_message_handler.expect_message(TestCustomMessage::Request);
416404
pass_along_path(&nodes);
417405

@@ -439,28 +427,20 @@ fn invalid_custom_message_type() {
439427
}
440428

441429
let test_msg = InvalidCustomMessage {};
442-
let path = OnionMessagePath {
443-
intermediate_nodes: vec![],
444-
destination: Destination::Node(nodes[1].node_id),
445-
first_node_addresses: None,
446-
};
447-
let err = nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap_err();
430+
let destination = Destination::Node(nodes[1].node_id);
431+
let err = nodes[0].messenger.send_onion_message(test_msg, destination, None).unwrap_err();
448432
assert_eq!(err, SendError::InvalidMessage);
449433
}
450434

451435
#[test]
452436
fn peer_buffer_full() {
453437
let nodes = create_nodes(2);
454438
let test_msg = TestCustomMessage::Request;
455-
let path = OnionMessagePath {
456-
intermediate_nodes: vec![],
457-
destination: Destination::Node(nodes[1].node_id),
458-
first_node_addresses: None,
459-
};
439+
let destination = Destination::Node(nodes[1].node_id);
460440
for _ in 0..188 { // Based on MAX_PER_PEER_BUFFER_SIZE in OnionMessenger
461-
nodes[0].messenger.send_onion_message_using_path(path.clone(), test_msg.clone(), None).unwrap();
441+
nodes[0].messenger.send_onion_message(test_msg.clone(), destination.clone(), None).unwrap();
462442
}
463-
let err = nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap_err();
443+
let err = nodes[0].messenger.send_onion_message(test_msg, destination, None).unwrap_err();
464444
assert_eq!(err, SendError::BufferFull);
465445
}
466446

@@ -492,6 +472,8 @@ fn requests_peer_connection_for_buffered_messages() {
492472
let nodes = create_nodes(3);
493473
let message = TestCustomMessage::Request;
494474
let secp_ctx = Secp256k1::new();
475+
add_channel_to_graph(&nodes[0], &nodes[1], &secp_ctx, 42);
476+
495477
let blinded_path = BlindedPath::new_for_message(
496478
&[nodes[1].node_id, nodes[2].node_id], &*nodes[0].entropy_source, &secp_ctx
497479
).unwrap();
@@ -527,6 +509,8 @@ fn drops_buffered_messages_waiting_for_peer_connection() {
527509
let nodes = create_nodes(3);
528510
let message = TestCustomMessage::Request;
529511
let secp_ctx = Secp256k1::new();
512+
add_channel_to_graph(&nodes[0], &nodes[1], &secp_ctx, 42);
513+
530514
let blinded_path = BlindedPath::new_for_message(
531515
&[nodes[1].node_id, nodes[2].node_id], &*nodes[0].entropy_source, &secp_ctx
532516
).unwrap();

lightning/src/onion_message/messenger.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,10 +318,10 @@ where
318318
ES::Target: EntropySource,
319319
{
320320
fn find_path(
321-
&self, _sender: PublicKey, peers: Vec<PublicKey>, destination: Destination
321+
&self, sender: PublicKey, peers: Vec<PublicKey>, destination: Destination
322322
) -> Result<OnionMessagePath, ()> {
323323
let first_node = destination.first_node();
324-
if peers.contains(&first_node) {
324+
if peers.contains(&first_node) || sender == first_node {
325325
Ok(OnionMessagePath {
326326
intermediate_nodes: vec![], destination, first_node_addresses: None
327327
})

lightning/src/routing/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ pub mod gossip;
1414
pub mod router;
1515
pub mod scoring;
1616
#[cfg(test)]
17-
mod test_utils;
17+
pub(crate) mod test_utils;

lightning/src/routing/test_utils.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99

1010
use crate::routing::gossip::{NetworkGraph, NodeAlias, P2PGossipSync};
1111
use crate::ln::features::{ChannelFeatures, NodeFeatures};
12-
use crate::ln::msgs::{UnsignedChannelAnnouncement, ChannelAnnouncement, RoutingMessageHandler,
13-
NodeAnnouncement, UnsignedNodeAnnouncement, ChannelUpdate, UnsignedChannelUpdate, MAX_VALUE_MSAT};
12+
use crate::ln::msgs::{ChannelAnnouncement, ChannelUpdate, MAX_VALUE_MSAT, NodeAnnouncement, RoutingMessageHandler, SocketAddress, UnsignedChannelAnnouncement, UnsignedChannelUpdate, UnsignedNodeAnnouncement};
1413
use crate::util::test_utils;
1514
use crate::util::ser::Writeable;
1615

@@ -28,7 +27,7 @@ use crate::sync::{self, Arc};
2827
use crate::routing::gossip::NodeId;
2928

3029
// Using the same keys for LN and BTC ids
31-
pub(super) fn add_channel(
30+
pub(crate) fn add_channel(
3231
gossip_sync: &P2PGossipSync<Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>,
3332
secp_ctx: &Secp256k1<All>, node_1_privkey: &SecretKey, node_2_privkey: &SecretKey, features: ChannelFeatures, short_channel_id: u64
3433
) {
@@ -60,7 +59,7 @@ pub(super) fn add_channel(
6059
};
6160
}
6261

63-
pub(super) fn add_or_update_node(
62+
pub(crate) fn add_or_update_node(
6463
gossip_sync: &P2PGossipSync<Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>,
6564
secp_ctx: &Secp256k1<All>, node_privkey: &SecretKey, features: NodeFeatures, timestamp: u32
6665
) {
@@ -71,7 +70,7 @@ pub(super) fn add_or_update_node(
7170
node_id,
7271
rgb: [0; 3],
7372
alias: NodeAlias([0; 32]),
74-
addresses: Vec::new(),
73+
addresses: vec![SocketAddress::TcpIpV4 { addr: [127, 0, 0, 1], port: 1000 }],
7574
excess_address_data: Vec::new(),
7675
excess_data: Vec::new(),
7776
};
@@ -87,7 +86,7 @@ pub(super) fn add_or_update_node(
8786
};
8887
}
8988

90-
pub(super) fn update_channel(
89+
pub(crate) fn update_channel(
9190
gossip_sync: &P2PGossipSync<Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>,
9291
secp_ctx: &Secp256k1<All>, node_privkey: &SecretKey, update: UnsignedChannelUpdate
9392
) {

0 commit comments

Comments
 (0)