Skip to content

Commit 4389597

Browse files
committed
Test onion message replies
1 parent 87f44c1 commit 4389597

File tree

1 file changed

+41
-21
lines changed

1 file changed

+41
-21
lines changed

lightning/src/onion_message/functional_tests.rs

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ use crate::util::test_utils;
2020
use bitcoin::network::constants::Network;
2121
use bitcoin::secp256k1::{PublicKey, Secp256k1};
2222

23-
use core::sync::atomic::{AtomicU16, Ordering};
2423
use crate::io;
2524
use crate::io_extras::read_to_end;
26-
use crate::sync::Arc;
25+
use crate::sync::{Arc, Mutex};
26+
27+
use crate::prelude::*;
2728

2829
struct MessengerNode {
2930
keys_manager: Arc<test_utils::TestKeysInterface>,
@@ -36,7 +37,6 @@ struct MessengerNode {
3637
Arc<TestCustomMessageHandler>
3738
>,
3839
custom_message_handler: Arc<TestCustomMessageHandler>,
39-
logger: Arc<test_utils::TestLogger>,
4040
}
4141

4242
impl MessengerNode {
@@ -49,9 +49,12 @@ struct TestMessageRouter {}
4949

5050
impl MessageRouter for TestMessageRouter {
5151
fn find_path(
52-
&self, _sender: &PublicKey, _destination: Destination
52+
&self, _sender: &PublicKey, destination: Destination
5353
) -> Result<OnionMessagePath, ()> {
54-
todo!()
54+
Ok(OnionMessagePath {
55+
intermediate_nodes: vec![],
56+
destination,
57+
})
5558
}
5659
}
5760

@@ -69,7 +72,7 @@ impl ResponseErrorHandler for TestOffersMessageHandler {
6972
}
7073
}
7174

72-
#[derive(Clone)]
75+
#[derive(Clone, Debug, PartialEq)]
7376
enum TestCustomMessage {
7477
Request,
7578
Response,
@@ -99,12 +102,16 @@ impl Writeable for TestCustomMessage {
99102
}
100103

101104
struct TestCustomMessageHandler {
102-
num_messages_expected: AtomicU16,
105+
expected_messages: Mutex<VecDeque<TestCustomMessage>>,
103106
}
104107

105108
impl TestCustomMessageHandler {
106109
fn new() -> Self {
107-
Self { num_messages_expected: AtomicU16::new(0) }
110+
Self { expected_messages: Mutex::new(VecDeque::new()) }
111+
}
112+
113+
fn expect_message(&self, message: TestCustomMessage) {
114+
self.expected_messages.lock().unwrap().push_back(message);
108115
}
109116
}
110117

@@ -115,14 +122,18 @@ impl Drop for TestCustomMessageHandler {
115122
return;
116123
}
117124
}
118-
assert_eq!(self.num_messages_expected.load(Ordering::SeqCst), 0);
125+
assert!(self.expected_messages.lock().unwrap().is_empty());
119126
}
120127
}
121128

122129
impl CustomOnionMessageHandler for TestCustomMessageHandler {
123130
type CustomMessage = TestCustomMessage;
124131
fn handle_custom_message(&self, msg: Self::CustomMessage) -> Option<Self::CustomMessage> {
125-
self.num_messages_expected.fetch_sub(1, Ordering::SeqCst);
132+
match self.expected_messages.lock().unwrap().pop_front() {
133+
Some(expected_msg) => assert_eq!(expected_msg, msg),
134+
None => panic!("Unexpected message: {:?}", msg),
135+
}
136+
126137
match msg {
127138
TestCustomMessage::Request => Some(TestCustomMessage::Response),
128139
TestCustomMessage::Response => None,
@@ -167,7 +178,6 @@ fn create_nodes(num_messengers: u8) -> Vec<MessengerNode> {
167178
offers_message_handler, custom_message_handler.clone()
168179
),
169180
custom_message_handler,
170-
logger,
171181
});
172182
}
173183
for idx in 0..num_messengers - 1 {
@@ -182,7 +192,6 @@ fn create_nodes(num_messengers: u8) -> Vec<MessengerNode> {
182192
}
183193

184194
fn pass_along_path(path: &Vec<MessengerNode>) {
185-
path[path.len() - 1].custom_message_handler.num_messages_expected.fetch_add(1, Ordering::SeqCst);
186195
let mut prev_node = &path[0];
187196
for node in path.into_iter().skip(1) {
188197
let events = prev_node.messenger.release_pending_msgs();
@@ -206,6 +215,7 @@ fn one_hop() {
206215
destination: Destination::Node(nodes[1].get_node_pk()),
207216
};
208217
nodes[0].messenger.send_onion_message(path, test_msg, None).unwrap();
218+
nodes[1].custom_message_handler.expect_message(TestCustomMessage::Response);
209219
pass_along_path(&nodes);
210220
}
211221

@@ -219,6 +229,7 @@ fn two_unblinded_hops() {
219229
destination: Destination::Node(nodes[2].get_node_pk()),
220230
};
221231
nodes[0].messenger.send_onion_message(path, test_msg, None).unwrap();
232+
nodes[2].custom_message_handler.expect_message(TestCustomMessage::Response);
222233
pass_along_path(&nodes);
223234
}
224235

@@ -235,6 +246,7 @@ fn two_unblinded_two_blinded() {
235246
};
236247

237248
nodes[0].messenger.send_onion_message(path, test_msg, None).unwrap();
249+
nodes[4].custom_message_handler.expect_message(TestCustomMessage::Response);
238250
pass_along_path(&nodes);
239251
}
240252

@@ -251,6 +263,7 @@ fn three_blinded_hops() {
251263
};
252264

253265
nodes[0].messenger.send_onion_message(path, test_msg, None).unwrap();
266+
nodes[3].custom_message_handler.expect_message(TestCustomMessage::Response);
254267
pass_along_path(&nodes);
255268
}
256269

@@ -285,6 +298,7 @@ fn we_are_intro_node() {
285298
};
286299

287300
nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg.clone()), None).unwrap();
301+
nodes[2].custom_message_handler.expect_message(TestCustomMessage::Response);
288302
pass_along_path(&nodes);
289303

290304
// Try with a two-hop blinded path where we are the introduction node.
@@ -294,6 +308,7 @@ fn we_are_intro_node() {
294308
destination: Destination::BlindedPath(blinded_path),
295309
};
296310
nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg), None).unwrap();
311+
nodes[1].custom_message_handler.expect_message(TestCustomMessage::Response);
297312
nodes.remove(2);
298313
pass_along_path(&nodes);
299314
}
@@ -329,8 +344,8 @@ fn invalid_blinded_path_error() {
329344

330345
#[test]
331346
fn reply_path() {
332-
let nodes = create_nodes(4);
333-
let test_msg = TestCustomMessage::Response;
347+
let mut nodes = create_nodes(4);
348+
let test_msg = TestCustomMessage::Request;
334349
let secp_ctx = Secp256k1::new();
335350

336351
// Destination::Node
@@ -340,11 +355,12 @@ fn reply_path() {
340355
};
341356
let reply_path = BlindedPath::new_for_message(&[nodes[2].get_node_pk(), nodes[1].get_node_pk(), nodes[0].get_node_pk()], &*nodes[0].keys_manager, &secp_ctx).unwrap();
342357
nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg.clone()), Some(reply_path)).unwrap();
358+
nodes[3].custom_message_handler.expect_message(TestCustomMessage::Request);
343359
pass_along_path(&nodes);
344360
// Make sure the last node successfully decoded the reply path.
345-
nodes[3].logger.assert_log_contains(
346-
"lightning::onion_message::messenger",
347-
&format!("Received an onion message with path_id None and a reply_path"), 1);
361+
nodes[0].custom_message_handler.expect_message(TestCustomMessage::Response);
362+
nodes.reverse();
363+
pass_along_path(&nodes);
348364

349365
// Destination::BlindedPath
350366
let blinded_path = BlindedPath::new_for_message(&[nodes[1].get_node_pk(), nodes[2].get_node_pk(), nodes[3].get_node_pk()], &*nodes[3].keys_manager, &secp_ctx).unwrap();
@@ -355,10 +371,13 @@ fn reply_path() {
355371
let reply_path = BlindedPath::new_for_message(&[nodes[2].get_node_pk(), nodes[1].get_node_pk(), nodes[0].get_node_pk()], &*nodes[0].keys_manager, &secp_ctx).unwrap();
356372

357373
nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg), Some(reply_path)).unwrap();
374+
nodes[3].custom_message_handler.expect_message(TestCustomMessage::Request);
375+
pass_along_path(&nodes);
376+
377+
// Make sure the last node successfully decoded the reply path.
378+
nodes[0].custom_message_handler.expect_message(TestCustomMessage::Response);
379+
nodes.reverse();
358380
pass_along_path(&nodes);
359-
nodes[3].logger.assert_log_contains(
360-
"lightning::onion_message::messenger",
361-
&format!("Received an onion message with path_id None and a reply_path"), 2);
362381
}
363382

364383
#[test]
@@ -389,7 +408,7 @@ fn invalid_custom_message_type() {
389408
#[test]
390409
fn peer_buffer_full() {
391410
let nodes = create_nodes(2);
392-
let test_msg = TestCustomMessage::Response;
411+
let test_msg = TestCustomMessage::Request;
393412
let path = OnionMessagePath {
394413
intermediate_nodes: vec![],
395414
destination: Destination::Node(nodes[1].get_node_pk()),
@@ -419,5 +438,6 @@ fn many_hops() {
419438
destination: Destination::Node(nodes[num_nodes-1].get_node_pk()),
420439
};
421440
nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg), None).unwrap();
441+
nodes[num_nodes-1].custom_message_handler.expect_message(TestCustomMessage::Response);
422442
pass_along_path(&nodes);
423443
}

0 commit comments

Comments
 (0)