Skip to content

Commit a5cefff

Browse files
committed
Test onion message replies
1 parent 6fb7857 commit a5cefff

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 {
@@ -49,7 +49,7 @@ struct TestMessageRouter {}
4949

5050
impl MessageRouter for TestMessageRouter {
5151
fn find_route(&self, _sender: &PublicKey, _destination: &Destination) -> Option<Vec<PublicKey>> {
52-
todo!()
52+
Some(vec![])
5353
}
5454
}
5555

@@ -67,7 +67,7 @@ impl ResponseErrorHandler for TestOffersMessageHandler {
6767
}
6868
}
6969

70-
#[derive(Clone)]
70+
#[derive(Clone, Debug, PartialEq)]
7171
enum TestCustomMessage {
7272
Request,
7373
Response,
@@ -97,12 +97,16 @@ impl Writeable for TestCustomMessage {
9797
}
9898

9999
struct TestCustomMessageHandler {
100-
num_messages_expected: AtomicU16,
100+
expected_messages: Mutex<VecDeque<TestCustomMessage>>,
101101
}
102102

103103
impl TestCustomMessageHandler {
104104
fn new() -> Self {
105-
Self { num_messages_expected: AtomicU16::new(0) }
105+
Self { expected_messages: Mutex::new(VecDeque::new()) }
106+
}
107+
108+
fn expect_message(&self, message: TestCustomMessage) {
109+
self.expected_messages.lock().unwrap().push_back(message);
106110
}
107111
}
108112

@@ -113,14 +117,18 @@ impl Drop for TestCustomMessageHandler {
113117
return;
114118
}
115119
}
116-
assert_eq!(self.num_messages_expected.load(Ordering::SeqCst), 0);
120+
assert!(self.expected_messages.lock().unwrap().is_empty());
117121
}
118122
}
119123

120124
impl CustomOnionMessageHandler for TestCustomMessageHandler {
121125
type CustomMessage = TestCustomMessage;
122126
fn handle_custom_message(&self, msg: Self::CustomMessage) -> Option<Self::CustomMessage> {
123-
self.num_messages_expected.fetch_sub(1, Ordering::SeqCst);
127+
match self.expected_messages.lock().unwrap().pop_front() {
128+
Some(expected_msg) => assert_eq!(expected_msg, msg),
129+
None => panic!("Unexpected message: {:?}", msg),
130+
}
131+
124132
match msg {
125133
TestCustomMessage::Request => Some(TestCustomMessage::Response),
126134
TestCustomMessage::Response => None,
@@ -165,7 +173,6 @@ fn create_nodes(num_messengers: u8) -> Vec<MessengerNode> {
165173
offers_message_handler, custom_message_handler.clone()
166174
),
167175
custom_message_handler,
168-
logger,
169176
});
170177
}
171178
for idx in 0..num_messengers - 1 {
@@ -180,7 +187,6 @@ fn create_nodes(num_messengers: u8) -> Vec<MessengerNode> {
180187
}
181188

182189
fn pass_along_path(path: &Vec<MessengerNode>) {
183-
path[path.len() - 1].custom_message_handler.num_messages_expected.fetch_add(1, Ordering::SeqCst);
184190
let mut prev_node = &path[0];
185191
for node in path.into_iter().skip(1) {
186192
let events = prev_node.messenger.release_pending_msgs();
@@ -200,6 +206,7 @@ fn one_hop() {
200206
let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
201207

202208
nodes[0].messenger.send_onion_message(&[], Destination::Node(nodes[1].get_node_pk()), test_msg, None).unwrap();
209+
nodes[1].custom_message_handler.expect_message(TestCustomMessage::Response);
203210
pass_along_path(&nodes);
204211
}
205212

@@ -209,6 +216,7 @@ fn two_unblinded_hops() {
209216
let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
210217

211218
nodes[0].messenger.send_onion_message(&[nodes[1].get_node_pk()], Destination::Node(nodes[2].get_node_pk()), test_msg, None).unwrap();
219+
nodes[2].custom_message_handler.expect_message(TestCustomMessage::Response);
212220
pass_along_path(&nodes);
213221
}
214222

@@ -221,6 +229,7 @@ fn two_unblinded_two_blinded() {
221229
let blinded_path = BlindedPath::new_for_message(&[nodes[3].get_node_pk(), nodes[4].get_node_pk()], &*nodes[4].keys_manager, &secp_ctx).unwrap();
222230

223231
nodes[0].messenger.send_onion_message(&[nodes[1].get_node_pk(), nodes[2].get_node_pk()], Destination::BlindedPath(blinded_path), test_msg, None).unwrap();
232+
nodes[4].custom_message_handler.expect_message(TestCustomMessage::Response);
224233
pass_along_path(&nodes);
225234
}
226235

@@ -233,6 +242,7 @@ fn three_blinded_hops() {
233242
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();
234243

235244
nodes[0].messenger.send_onion_message(&[], Destination::BlindedPath(blinded_path), test_msg, None).unwrap();
245+
nodes[3].custom_message_handler.expect_message(TestCustomMessage::Response);
236246
pass_along_path(&nodes);
237247
}
238248

@@ -259,11 +269,13 @@ fn we_are_intro_node() {
259269
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();
260270

261271
nodes[0].messenger.send_onion_message(&[], Destination::BlindedPath(blinded_path), OnionMessageContents::Custom(test_msg.clone()), None).unwrap();
272+
nodes[2].custom_message_handler.expect_message(TestCustomMessage::Response);
262273
pass_along_path(&nodes);
263274

264275
// Try with a two-hop blinded path where we are the introduction node.
265276
let blinded_path = BlindedPath::new_for_message(&[nodes[0].get_node_pk(), nodes[1].get_node_pk()], &*nodes[1].keys_manager, &secp_ctx).unwrap();
266277
nodes[0].messenger.send_onion_message(&[], Destination::BlindedPath(blinded_path), OnionMessageContents::Custom(test_msg), None).unwrap();
278+
nodes[1].custom_message_handler.expect_message(TestCustomMessage::Response);
267279
nodes.remove(2);
268280
pass_along_path(&nodes);
269281
}
@@ -291,28 +303,32 @@ fn invalid_blinded_path_error() {
291303

292304
#[test]
293305
fn reply_path() {
294-
let nodes = create_nodes(4);
295-
let test_msg = TestCustomMessage::Response;
306+
let mut nodes = create_nodes(4);
307+
let test_msg = TestCustomMessage::Request;
296308
let secp_ctx = Secp256k1::new();
297309

298310
// Destination::Node
299311
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();
300312
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();
313+
nodes[3].custom_message_handler.expect_message(TestCustomMessage::Request);
301314
pass_along_path(&nodes);
302315
// Make sure the last node successfully decoded the reply path.
303-
nodes[3].logger.assert_log_contains(
304-
"lightning::onion_message::messenger",
305-
&format!("Received an onion message with path_id None and a reply_path"), 1);
316+
nodes[0].custom_message_handler.expect_message(TestCustomMessage::Response);
317+
nodes.reverse();
318+
pass_along_path(&nodes);
306319

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

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

318334
#[test]
@@ -339,7 +355,7 @@ fn invalid_custom_message_type() {
339355
#[test]
340356
fn peer_buffer_full() {
341357
let nodes = create_nodes(2);
342-
let test_msg = TestCustomMessage::Response;
358+
let test_msg = TestCustomMessage::Request;
343359
for _ in 0..188 { // Based on MAX_PER_PEER_BUFFER_SIZE in OnionMessenger
344360
nodes[0].messenger.send_onion_message(&[], Destination::Node(nodes[1].get_node_pk()), OnionMessageContents::Custom(test_msg.clone()), None).unwrap();
345361
}
@@ -361,5 +377,6 @@ fn many_hops() {
361377
}
362378

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

0 commit comments

Comments
 (0)