Skip to content

Commit 19e7aaf

Browse files
committed
Test onion message replies
1 parent 973394c commit 19e7aaf

File tree

1 file changed

+37
-20
lines changed

1 file changed

+37
-20
lines changed

lightning/src/onion_message/functional_tests.rs

Lines changed: 37 additions & 20 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 {
@@ -51,7 +51,7 @@ impl MessageRouter for TestMessageRouter {
5151
fn find_route(
5252
&self, _sender: &PublicKey, _destination: &Destination
5353
) -> Result<Vec<PublicKey>, ()> {
54-
todo!()
54+
Ok(vec![])
5555
}
5656
}
5757

@@ -69,7 +69,7 @@ impl ResponseErrorHandler for TestOffersMessageHandler {
6969
}
7070
}
7171

72-
#[derive(Clone)]
72+
#[derive(Clone, Debug, PartialEq)]
7373
enum TestCustomMessage {
7474
Request,
7575
Response,
@@ -99,12 +99,16 @@ impl Writeable for TestCustomMessage {
9999
}
100100

101101
struct TestCustomMessageHandler {
102-
num_messages_expected: AtomicU16,
102+
expected_messages: Mutex<VecDeque<TestCustomMessage>>,
103103
}
104104

105105
impl TestCustomMessageHandler {
106106
fn new() -> Self {
107-
Self { num_messages_expected: AtomicU16::new(0) }
107+
Self { expected_messages: Mutex::new(VecDeque::new()) }
108+
}
109+
110+
fn expect_message(&self, message: TestCustomMessage) {
111+
self.expected_messages.lock().unwrap().push_back(message);
108112
}
109113
}
110114

@@ -115,14 +119,18 @@ impl Drop for TestCustomMessageHandler {
115119
return;
116120
}
117121
}
118-
assert_eq!(self.num_messages_expected.load(Ordering::SeqCst), 0);
122+
assert!(self.expected_messages.lock().unwrap().is_empty());
119123
}
120124
}
121125

122126
impl CustomOnionMessageHandler for TestCustomMessageHandler {
123127
type CustomMessage = TestCustomMessage;
124128
fn handle_custom_message(&self, msg: Self::CustomMessage) -> Option<Self::CustomMessage> {
125-
self.num_messages_expected.fetch_sub(1, Ordering::SeqCst);
129+
match self.expected_messages.lock().unwrap().pop_front() {
130+
Some(expected_msg) => assert_eq!(expected_msg, msg),
131+
None => panic!("Unexpected message: {:?}", msg),
132+
}
133+
126134
match msg {
127135
TestCustomMessage::Request => Some(TestCustomMessage::Response),
128136
TestCustomMessage::Response => None,
@@ -167,7 +175,6 @@ fn create_nodes(num_messengers: u8) -> Vec<MessengerNode> {
167175
offers_message_handler, custom_message_handler.clone()
168176
),
169177
custom_message_handler,
170-
logger,
171178
});
172179
}
173180
for idx in 0..num_messengers - 1 {
@@ -182,7 +189,6 @@ fn create_nodes(num_messengers: u8) -> Vec<MessengerNode> {
182189
}
183190

184191
fn pass_along_path(path: &Vec<MessengerNode>) {
185-
path[path.len() - 1].custom_message_handler.num_messages_expected.fetch_add(1, Ordering::SeqCst);
186192
let mut prev_node = &path[0];
187193
for node in path.into_iter().skip(1) {
188194
let events = prev_node.messenger.release_pending_msgs();
@@ -202,6 +208,7 @@ fn one_hop() {
202208
let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
203209

204210
nodes[0].messenger.send_onion_message(&[], Destination::Node(nodes[1].get_node_pk()), test_msg, None).unwrap();
211+
nodes[1].custom_message_handler.expect_message(TestCustomMessage::Response);
205212
pass_along_path(&nodes);
206213
}
207214

@@ -211,6 +218,7 @@ fn two_unblinded_hops() {
211218
let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
212219

213220
nodes[0].messenger.send_onion_message(&[nodes[1].get_node_pk()], Destination::Node(nodes[2].get_node_pk()), test_msg, None).unwrap();
221+
nodes[2].custom_message_handler.expect_message(TestCustomMessage::Response);
214222
pass_along_path(&nodes);
215223
}
216224

@@ -223,6 +231,7 @@ fn two_unblinded_two_blinded() {
223231
let blinded_path = BlindedPath::new_for_message(&[nodes[3].get_node_pk(), nodes[4].get_node_pk()], &*nodes[4].keys_manager, &secp_ctx).unwrap();
224232

225233
nodes[0].messenger.send_onion_message(&[nodes[1].get_node_pk(), nodes[2].get_node_pk()], Destination::BlindedPath(blinded_path), test_msg, None).unwrap();
234+
nodes[4].custom_message_handler.expect_message(TestCustomMessage::Response);
226235
pass_along_path(&nodes);
227236
}
228237

@@ -235,6 +244,7 @@ fn three_blinded_hops() {
235244
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();
236245

237246
nodes[0].messenger.send_onion_message(&[], Destination::BlindedPath(blinded_path), test_msg, None).unwrap();
247+
nodes[3].custom_message_handler.expect_message(TestCustomMessage::Response);
238248
pass_along_path(&nodes);
239249
}
240250

@@ -261,11 +271,13 @@ fn we_are_intro_node() {
261271
let blinded_path = BlindedPath::new_for_message(&[nodes[0].get_node_pk(), nodes[1].get_node_pk(), nodes[2].get_node_pk()], &*nodes[2].keys_manager, &secp_ctx).unwrap();
262272

263273
nodes[0].messenger.send_onion_message(&[], Destination::BlindedPath(blinded_path), OnionMessageContents::Custom(test_msg.clone()), None).unwrap();
274+
nodes[2].custom_message_handler.expect_message(TestCustomMessage::Response);
264275
pass_along_path(&nodes);
265276

266277
// Try with a two-hop blinded path where we are the introduction node.
267278
let blinded_path = BlindedPath::new_for_message(&[nodes[0].get_node_pk(), nodes[1].get_node_pk()], &*nodes[1].keys_manager, &secp_ctx).unwrap();
268279
nodes[0].messenger.send_onion_message(&[], Destination::BlindedPath(blinded_path), OnionMessageContents::Custom(test_msg), None).unwrap();
280+
nodes[1].custom_message_handler.expect_message(TestCustomMessage::Response);
269281
nodes.remove(2);
270282
pass_along_path(&nodes);
271283
}
@@ -293,28 +305,32 @@ fn invalid_blinded_path_error() {
293305

294306
#[test]
295307
fn reply_path() {
296-
let nodes = create_nodes(4);
297-
let test_msg = TestCustomMessage::Response;
308+
let mut nodes = create_nodes(4);
309+
let test_msg = TestCustomMessage::Request;
298310
let secp_ctx = Secp256k1::new();
299311

300312
// Destination::Node
301313
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();
302314
nodes[0].messenger.send_onion_message(&[nodes[1].get_node_pk(), nodes[2].get_node_pk()], Destination::Node(nodes[3].get_node_pk()), OnionMessageContents::Custom(test_msg.clone()), Some(reply_path)).unwrap();
315+
nodes[3].custom_message_handler.expect_message(TestCustomMessage::Request);
303316
pass_along_path(&nodes);
304317
// Make sure the last node successfully decoded the reply path.
305-
nodes[3].logger.assert_log_contains(
306-
"lightning::onion_message::messenger",
307-
&format!("Received an onion message with path_id None and a reply_path"), 1);
318+
nodes[0].custom_message_handler.expect_message(TestCustomMessage::Response);
319+
nodes.reverse();
320+
pass_along_path(&nodes);
308321

309322
// Destination::BlindedPath
310323
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();
311324
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();
312325

313326
nodes[0].messenger.send_onion_message(&[], Destination::BlindedPath(blinded_path), OnionMessageContents::Custom(test_msg), Some(reply_path)).unwrap();
327+
nodes[3].custom_message_handler.expect_message(TestCustomMessage::Request);
328+
pass_along_path(&nodes);
329+
330+
// Make sure the last node successfully decoded the reply path.
331+
nodes[0].custom_message_handler.expect_message(TestCustomMessage::Response);
332+
nodes.reverse();
314333
pass_along_path(&nodes);
315-
nodes[3].logger.assert_log_contains(
316-
"lightning::onion_message::messenger",
317-
&format!("Received an onion message with path_id None and a reply_path"), 2);
318334
}
319335

320336
#[test]
@@ -341,7 +357,7 @@ fn invalid_custom_message_type() {
341357
#[test]
342358
fn peer_buffer_full() {
343359
let nodes = create_nodes(2);
344-
let test_msg = TestCustomMessage::Response;
360+
let test_msg = TestCustomMessage::Request;
345361
for _ in 0..188 { // Based on MAX_PER_PEER_BUFFER_SIZE in OnionMessenger
346362
nodes[0].messenger.send_onion_message(&[], Destination::Node(nodes[1].get_node_pk()), OnionMessageContents::Custom(test_msg.clone()), None).unwrap();
347363
}
@@ -363,5 +379,6 @@ fn many_hops() {
363379
}
364380

365381
nodes[0].messenger.send_onion_message(&intermediates, Destination::Node(nodes[num_nodes-1].get_node_pk()), OnionMessageContents::Custom(test_msg), None).unwrap();
382+
nodes[num_nodes-1].custom_message_handler.expect_message(TestCustomMessage::Response);
366383
pass_along_path(&nodes);
367384
}

0 commit comments

Comments
 (0)