08be1b2c5027d64bdf599faca92f0b2ed510ef5c
[rust-lightning] / lightning / src / onion_message / functional_tests.rs
1 // This file is Copyright its original authors, visible in version control
2 // history.
3 //
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
8 // licenses.
9
10 //! Onion message testing and test utilities live here.
11
12 use crate::blinded_path::{BlindedPath, EmptyNodeIdLookUp};
13 use crate::blinded_path::message::ForwardNode;
14 use crate::events::{Event, EventsProvider};
15 use crate::ln::features::{ChannelFeatures, InitFeatures};
16 use crate::ln::msgs::{self, DecodeError, OnionMessageHandler};
17 use crate::routing::gossip::{NetworkGraph, P2PGossipSync};
18 use crate::routing::test_utils::{add_channel, add_or_update_node};
19 use crate::sign::{NodeSigner, Recipient};
20 use crate::util::ser::{FixedLengthReader, LengthReadable, Writeable, Writer};
21 use crate::util::test_utils;
22 use super::messenger::{CustomOnionMessageHandler, DefaultMessageRouter, Destination, OnionMessagePath, OnionMessenger, PendingOnionMessage, Responder, ResponseInstruction, SendError, SendSuccess};
23 use super::offers::{OffersMessage, OffersMessageHandler};
24 use super::packet::{OnionMessageContents, Packet};
25
26 use bitcoin::network::Network;
27 use bitcoin::hashes::hex::FromHex;
28 use bitcoin::secp256k1::{All, PublicKey, Secp256k1, SecretKey};
29
30 use crate::io;
31 use crate::io_extras::read_to_end;
32 use crate::sync::{Arc, Mutex};
33
34 use core::ops::Deref;
35
36 use crate::prelude::*;
37
38 struct MessengerNode {
39         node_id: PublicKey,
40         privkey: SecretKey,
41         entropy_source: Arc<test_utils::TestKeysInterface>,
42         messenger: OnionMessenger<
43                 Arc<test_utils::TestKeysInterface>,
44                 Arc<test_utils::TestNodeSigner>,
45                 Arc<test_utils::TestLogger>,
46                 Arc<EmptyNodeIdLookUp>,
47                 Arc<DefaultMessageRouter<
48                         Arc<NetworkGraph<Arc<test_utils::TestLogger>>>,
49                         Arc<test_utils::TestLogger>,
50                         Arc<test_utils::TestKeysInterface>
51                 >>,
52                 Arc<TestOffersMessageHandler>,
53                 Arc<TestCustomMessageHandler>
54         >,
55         custom_message_handler: Arc<TestCustomMessageHandler>,
56         gossip_sync: Arc<P2PGossipSync<
57                 Arc<NetworkGraph<Arc<test_utils::TestLogger>>>,
58                 Arc<test_utils::TestChainSource>,
59                 Arc<test_utils::TestLogger>
60         >>
61 }
62
63 impl Drop for MessengerNode {
64         fn drop(&mut self) {
65                 #[cfg(feature = "std")] {
66                         if std::thread::panicking() {
67                                 return;
68                         }
69                 }
70                 assert!(release_events(self).is_empty());
71         }
72 }
73
74 struct TestOffersMessageHandler {}
75
76 impl OffersMessageHandler for TestOffersMessageHandler {
77         fn handle_message(&self, _message: OffersMessage, _responder: Option<Responder>) -> ResponseInstruction<OffersMessage> {
78                 ResponseInstruction::NoResponse
79         }
80 }
81
82 #[derive(Clone, Debug, PartialEq)]
83 enum TestCustomMessage {
84         Ping,
85         Pong,
86 }
87
88 const CUSTOM_PING_MESSAGE_TYPE: u64 = 4242;
89 const CUSTOM_PONG_MESSAGE_TYPE: u64 = 4343;
90 const CUSTOM_PING_MESSAGE_CONTENTS: [u8; 32] = [42; 32];
91 const CUSTOM_PONG_MESSAGE_CONTENTS: [u8; 32] = [43; 32];
92
93 impl OnionMessageContents for TestCustomMessage {
94         fn tlv_type(&self) -> u64 {
95                 match self {
96                         TestCustomMessage::Ping => CUSTOM_PING_MESSAGE_TYPE,
97                         TestCustomMessage::Pong => CUSTOM_PONG_MESSAGE_TYPE,
98                 }
99         }
100         fn msg_type(&self) -> &'static str {
101                 "Custom Message"
102         }
103 }
104
105 impl Writeable for TestCustomMessage {
106         fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
107                 match self {
108                         TestCustomMessage::Ping => Ok(CUSTOM_PING_MESSAGE_CONTENTS.write(w)?),
109                         TestCustomMessage::Pong => Ok(CUSTOM_PONG_MESSAGE_CONTENTS.write(w)?),
110                 }
111         }
112 }
113
114 struct TestCustomMessageHandler {
115         expectations: Mutex<VecDeque<OnHandleCustomMessage>>,
116 }
117
118 struct OnHandleCustomMessage {
119         expect: TestCustomMessage,
120         include_reply_path: bool,
121 }
122
123 impl TestCustomMessageHandler {
124         fn new() -> Self {
125                 Self { expectations: Mutex::new(VecDeque::new()) }
126         }
127
128         fn expect_message(&self, message: TestCustomMessage) {
129                 self.expectations.lock().unwrap().push_back(
130                         OnHandleCustomMessage {
131                                 expect: message,
132                                 include_reply_path: false,
133                         }
134                 );
135         }
136
137         fn expect_message_and_response(&self, message: TestCustomMessage) {
138                 self.expectations.lock().unwrap().push_back(
139                         OnHandleCustomMessage {
140                                 expect: message,
141                                 include_reply_path: true,
142                         }
143                 );
144         }
145
146         fn get_next_expectation(&self) -> OnHandleCustomMessage {
147                 self.expectations.lock().unwrap().pop_front().expect("No expectations remaining")
148         }
149 }
150
151 impl Drop for TestCustomMessageHandler {
152         fn drop(&mut self) {
153                 #[cfg(feature = "std")] {
154                         if std::thread::panicking() {
155                                 return;
156                         }
157                 }
158                 assert!(self.expectations.lock().unwrap().is_empty());
159         }
160 }
161
162 impl CustomOnionMessageHandler for TestCustomMessageHandler {
163         type CustomMessage = TestCustomMessage;
164         fn handle_custom_message(&self, msg: Self::CustomMessage, responder: Option<Responder>) -> ResponseInstruction<Self::CustomMessage> {
165                 let expectation = self.get_next_expectation();
166                 assert_eq!(msg, expectation.expect);
167
168                 let response = match msg {
169                         TestCustomMessage::Ping => TestCustomMessage::Pong,
170                         TestCustomMessage::Pong => TestCustomMessage::Ping,
171                 };
172
173                 // Sanity check: expecting to include reply path when responder is absent should panic.
174                 if expectation.include_reply_path && responder.is_none() {
175                         panic!("Expected to include a reply_path, but the responder was absent.")
176                 }
177
178                 match responder {
179                         Some(responder) if expectation.include_reply_path => {
180                                 responder.respond_with_reply_path(response)
181                         },
182                         Some(responder) => responder.respond(response),
183                         None => ResponseInstruction::NoResponse,
184                 }
185         }
186         fn read_custom_message<R: io::Read>(&self, message_type: u64, buffer: &mut R) -> Result<Option<Self::CustomMessage>, DecodeError> where Self: Sized {
187                 match message_type {
188                         CUSTOM_PING_MESSAGE_TYPE => {
189                                 let buf = read_to_end(buffer)?;
190                                 assert_eq!(buf, CUSTOM_PING_MESSAGE_CONTENTS);
191                                 Ok(Some(TestCustomMessage::Ping))
192                         },
193                         CUSTOM_PONG_MESSAGE_TYPE => {
194                                 let buf = read_to_end(buffer)?;
195                                 assert_eq!(buf, CUSTOM_PONG_MESSAGE_CONTENTS);
196                                 Ok(Some(TestCustomMessage::Pong))
197                         },
198                         _ => Ok(None),
199                 }
200         }
201         fn release_pending_custom_messages(&self) -> Vec<PendingOnionMessage<Self::CustomMessage>> {
202                 vec![]
203         }
204 }
205
206 fn create_nodes(num_messengers: u8) -> Vec<MessengerNode> {
207         let cfgs = (1..=num_messengers)
208                 .into_iter()
209                 .map(|_| MessengerCfg::new())
210                 .collect();
211         create_nodes_using_cfgs(cfgs)
212 }
213
214 struct MessengerCfg {
215         secret_override: Option<SecretKey>,
216         intercept_offline_peer_oms: bool,
217 }
218 impl MessengerCfg {
219         fn new() -> Self {
220                 Self { secret_override: None, intercept_offline_peer_oms: false }
221         }
222         fn with_node_secret(mut self, secret: SecretKey) -> Self {
223                 self.secret_override = Some(secret);
224                 self
225         }
226         fn with_offline_peer_interception(mut self) -> Self {
227                 self.intercept_offline_peer_oms = true;
228                 self
229         }
230 }
231
232 fn create_nodes_using_cfgs(cfgs: Vec<MessengerCfg>) -> Vec<MessengerNode> {
233         let gossip_logger = Arc::new(test_utils::TestLogger::with_id("gossip".to_string()));
234         let network_graph = Arc::new(NetworkGraph::new(Network::Testnet, gossip_logger.clone()));
235         let gossip_sync = Arc::new(
236                 P2PGossipSync::new(network_graph.clone(), None, gossip_logger)
237         );
238
239         let mut nodes = Vec::new();
240         for (i, cfg) in cfgs.into_iter().enumerate() {
241                 let secret_key = cfg.secret_override.unwrap_or(SecretKey::from_slice(&[(i + 1) as u8; 32]).unwrap());
242                 let logger = Arc::new(test_utils::TestLogger::with_id(format!("node {}", i)));
243                 let seed = [i as u8; 32];
244                 let entropy_source = Arc::new(test_utils::TestKeysInterface::new(&seed, Network::Testnet));
245                 let node_signer = Arc::new(test_utils::TestNodeSigner::new(secret_key));
246
247                 let node_id_lookup = Arc::new(EmptyNodeIdLookUp {});
248                 let message_router = Arc::new(
249                         DefaultMessageRouter::new(network_graph.clone(), entropy_source.clone())
250                 );
251                 let offers_message_handler = Arc::new(TestOffersMessageHandler {});
252                 let custom_message_handler = Arc::new(TestCustomMessageHandler::new());
253                 let messenger = if cfg.intercept_offline_peer_oms {
254                         OnionMessenger::new_with_offline_peer_interception(
255                                 entropy_source.clone(), node_signer.clone(), logger.clone(),
256                                 node_id_lookup, message_router, offers_message_handler,
257                                 custom_message_handler.clone()
258                         )
259                 } else {
260                         OnionMessenger::new(
261                                 entropy_source.clone(), node_signer.clone(), logger.clone(),
262                                 node_id_lookup, message_router, offers_message_handler,
263                                 custom_message_handler.clone()
264                         )
265                 };
266                 nodes.push(MessengerNode {
267                         privkey: secret_key,
268                         node_id: node_signer.get_node_id(Recipient::Node).unwrap(),
269                         entropy_source,
270                         messenger,
271                         custom_message_handler,
272                         gossip_sync: gossip_sync.clone(),
273                 });
274         }
275         for i in 0..nodes.len() - 1 {
276                 connect_peers(&nodes[i], &nodes[i + 1]);
277         }
278         nodes
279 }
280
281 fn connect_peers(node_a: &MessengerNode, node_b: &MessengerNode) {
282         let mut features = InitFeatures::empty();
283         features.set_onion_messages_optional();
284         let init_msg = msgs::Init { features, networks: None, remote_network_address: None };
285         node_a.messenger.peer_connected(&node_b.node_id, &init_msg.clone(), true).unwrap();
286         node_b.messenger.peer_connected(&node_a.node_id, &init_msg.clone(), false).unwrap();
287 }
288
289 fn disconnect_peers(node_a: &MessengerNode, node_b: &MessengerNode) {
290         node_a.messenger.peer_disconnected(&node_b.node_id);
291         node_b.messenger.peer_disconnected(&node_a.node_id);
292 }
293
294 fn release_events(node: &MessengerNode) -> Vec<Event> {
295         let events = core::cell::RefCell::new(Vec::new());
296         node.messenger.process_pending_events(&|e| events.borrow_mut().push(e));
297         events.into_inner()
298 }
299
300 fn add_channel_to_graph(
301         node_a: &MessengerNode, node_b: &MessengerNode, secp_ctx: &Secp256k1<All>, short_channel_id: u64
302 ) {
303         let gossip_sync = node_a.gossip_sync.deref();
304         let privkey_a = &node_a.privkey;
305         let privkey_b = &node_b.privkey;
306         let channel_features = ChannelFeatures::empty();
307         let node_features_a = node_a.messenger.provided_node_features();
308         let node_features_b = node_b.messenger.provided_node_features();
309         add_channel(gossip_sync, secp_ctx, privkey_a, privkey_b, channel_features, short_channel_id);
310         add_or_update_node(gossip_sync, secp_ctx, privkey_a, node_features_a, 1);
311         add_or_update_node(gossip_sync, secp_ctx, privkey_b, node_features_b, 1);
312 }
313
314 fn pass_along_path(path: &Vec<MessengerNode>) {
315         let mut prev_node = &path[0];
316         for node in path.into_iter().skip(1) {
317                 let events = prev_node.messenger.release_pending_msgs();
318                 let onion_msg =  {
319                         let msgs = events.get(&node.node_id).unwrap();
320                         assert_eq!(msgs.len(), 1);
321                         msgs[0].clone()
322                 };
323                 node.messenger.handle_onion_message(&prev_node.node_id, &onion_msg);
324                 prev_node = node;
325         }
326 }
327
328 #[test]
329 fn one_unblinded_hop() {
330         let nodes = create_nodes(2);
331         let test_msg = TestCustomMessage::Pong;
332
333         let destination = Destination::Node(nodes[1].node_id);
334         nodes[0].messenger.send_onion_message(test_msg, destination, None).unwrap();
335         nodes[1].custom_message_handler.expect_message(TestCustomMessage::Pong);
336         pass_along_path(&nodes);
337 }
338
339 #[test]
340 fn two_unblinded_hops() {
341         let nodes = create_nodes(3);
342         let test_msg = TestCustomMessage::Pong;
343
344         let path = OnionMessagePath {
345                 intermediate_nodes: vec![nodes[1].node_id],
346                 destination: Destination::Node(nodes[2].node_id),
347                 first_node_addresses: None,
348         };
349
350         nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap();
351         nodes[2].custom_message_handler.expect_message(TestCustomMessage::Pong);
352         pass_along_path(&nodes);
353 }
354
355 #[test]
356 fn one_blinded_hop() {
357         let nodes = create_nodes(2);
358         let test_msg = TestCustomMessage::Pong;
359
360         let secp_ctx = Secp256k1::new();
361         let blinded_path = BlindedPath::new_for_message(&[], nodes[1].node_id, &*nodes[1].entropy_source, &secp_ctx).unwrap();
362         let destination = Destination::BlindedPath(blinded_path);
363         nodes[0].messenger.send_onion_message(test_msg, destination, None).unwrap();
364         nodes[1].custom_message_handler.expect_message(TestCustomMessage::Pong);
365         pass_along_path(&nodes);
366 }
367
368 #[test]
369 fn two_unblinded_two_blinded() {
370         let nodes = create_nodes(5);
371         let test_msg = TestCustomMessage::Pong;
372
373         let secp_ctx = Secp256k1::new();
374         let intermediate_nodes = [ForwardNode { node_id: nodes[3].node_id, short_channel_id: None }];
375         let blinded_path = BlindedPath::new_for_message(&intermediate_nodes, nodes[4].node_id, &*nodes[4].entropy_source, &secp_ctx).unwrap();
376         let path = OnionMessagePath {
377                 intermediate_nodes: vec![nodes[1].node_id, nodes[2].node_id],
378                 destination: Destination::BlindedPath(blinded_path),
379                 first_node_addresses: None,
380         };
381
382         nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap();
383         nodes[4].custom_message_handler.expect_message(TestCustomMessage::Pong);
384         pass_along_path(&nodes);
385 }
386
387 #[test]
388 fn three_blinded_hops() {
389         let nodes = create_nodes(4);
390         let test_msg = TestCustomMessage::Pong;
391
392         let secp_ctx = Secp256k1::new();
393         let intermediate_nodes = [
394                 ForwardNode { node_id: nodes[1].node_id, short_channel_id: None },
395                 ForwardNode { node_id: nodes[2].node_id, short_channel_id: None },
396         ];
397         let blinded_path = BlindedPath::new_for_message(&intermediate_nodes, nodes[3].node_id, &*nodes[3].entropy_source, &secp_ctx).unwrap();
398         let destination = Destination::BlindedPath(blinded_path);
399
400         nodes[0].messenger.send_onion_message(test_msg, destination, None).unwrap();
401         nodes[3].custom_message_handler.expect_message(TestCustomMessage::Pong);
402         pass_along_path(&nodes);
403 }
404
405 #[test]
406 fn async_response_over_one_blinded_hop() {
407         // Simulate an asynchronous interaction between two nodes, Alice and Bob.
408
409         // 1. Set up the network with two nodes: Alice and Bob.
410         let nodes = create_nodes(2);
411         let alice = &nodes[0];
412         let bob = &nodes[1];
413
414         // 2. Define the message sent from Bob to Alice.
415         let message = TestCustomMessage::Ping;
416         let path_id = Some([2; 32]);
417
418         // 3. Simulate the creation of a Blinded Reply path provided by Bob.
419         let secp_ctx = Secp256k1::new();
420         let reply_path = BlindedPath::new_for_message(&[], nodes[1].node_id, &*nodes[1].entropy_source, &secp_ctx).unwrap();
421
422         // 4. Create a responder using the reply path for Alice.
423         let responder = Some(Responder::new(reply_path, path_id));
424
425         // 5. Expect Alice to receive the message and create a response instruction for it.
426         alice.custom_message_handler.expect_message(message.clone());
427         let response_instruction = nodes[0].custom_message_handler.handle_custom_message(message, responder);
428
429         // 6. Simulate Alice asynchronously responding back to Bob with a response.
430         assert_eq!(
431                 nodes[0].messenger.handle_onion_message_response(response_instruction),
432                 Ok(Some(SendSuccess::Buffered)),
433         );
434
435         bob.custom_message_handler.expect_message(TestCustomMessage::Pong);
436
437         pass_along_path(&nodes);
438 }
439
440 #[test]
441 fn async_response_with_reply_path_succeeds() {
442         // Simulate an asynchronous interaction between two nodes, Alice and Bob.
443         // Create a channel between the two nodes to establish them as announced nodes,
444         // which allows the creation of the reply_path for successful communication.
445
446         let mut nodes = create_nodes(2);
447         let alice = &nodes[0];
448         let bob = &nodes[1];
449         let secp_ctx = Secp256k1::new();
450
451         add_channel_to_graph(alice, bob, &secp_ctx, 24);
452
453         // Alice receives a message from Bob with an added reply_path for responding back.
454         let message = TestCustomMessage::Ping;
455         let path_id = Some([2; 32]);
456         let reply_path = BlindedPath::new_for_message(&[], bob.node_id, &*bob.entropy_source, &secp_ctx).unwrap();
457
458         // Alice asynchronously responds to Bob, expecting a response back from him.
459         let responder = Responder::new(reply_path, path_id);
460         alice.custom_message_handler.expect_message_and_response(message.clone());
461         let response_instruction = alice.custom_message_handler.handle_custom_message(message, Some(responder));
462
463         assert_eq!(
464                 alice.messenger.handle_onion_message_response(response_instruction),
465                 Ok(Some(SendSuccess::Buffered)),
466         );
467
468         // Set Bob's expectation and pass the Onion Message along the path.
469         bob.custom_message_handler.expect_message(TestCustomMessage::Pong);
470         pass_along_path(&nodes);
471
472         // Bob responds back to Alice using the reply_path she included with the OnionMessage.
473         // Set Alice's expectation and reverse the path for the response.
474         alice.custom_message_handler.expect_message(TestCustomMessage::Ping);
475         nodes.reverse();
476         pass_along_path(&nodes);
477 }
478
479 #[test]
480 fn async_response_with_reply_path_fails() {
481         // Simulate an asynchronous interaction between two unannounced nodes, Alice and Bob.
482         // Since the nodes are unannounced, attempting to respond using a reply_path
483         // will fail, leading to an expected failure in communication.
484
485         let nodes = create_nodes(2);
486         let alice = &nodes[0];
487         let bob = &nodes[1];
488         let secp_ctx = Secp256k1::new();
489
490         // Alice receives a message from Bob with an added reply_path for responding back.
491         let message = TestCustomMessage::Ping;
492         let path_id = Some([2; 32]);
493         let reply_path = BlindedPath::new_for_message(&[], bob.node_id, &*bob.entropy_source, &secp_ctx).unwrap();
494
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.
497         let responder = Responder::new(reply_path, path_id);
498         alice.custom_message_handler.expect_message_and_response(message.clone());
499         let response_instruction = alice.custom_message_handler.handle_custom_message(message, Some(responder));
500
501         assert_eq!(
502                 alice.messenger.handle_onion_message_response(response_instruction),
503                 Err(SendError::PathNotFound),
504         );
505 }
506
507 #[test]
508 fn too_big_packet_error() {
509         // Make sure we error as expected if a packet is too big to send.
510         let nodes = create_nodes(2);
511         let test_msg = TestCustomMessage::Pong;
512
513         let hop_node_id = nodes[1].node_id;
514         let hops = vec![hop_node_id; 400];
515         let path = OnionMessagePath {
516                 intermediate_nodes: hops,
517                 destination: Destination::Node(hop_node_id),
518                 first_node_addresses: None,
519         };
520         let err = nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap_err();
521         assert_eq!(err, SendError::TooBigPacket);
522 }
523
524 #[test]
525 fn we_are_intro_node() {
526         // If we are sending straight to a blinded path and we are the introduction node, we need to
527         // advance the blinded path by 1 hop so the second hop is the new introduction node.
528         let mut nodes = create_nodes(3);
529         let test_msg = TestCustomMessage::Pong;
530
531         let secp_ctx = Secp256k1::new();
532         let intermediate_nodes = [
533                 ForwardNode { node_id: nodes[0].node_id, short_channel_id: None },
534                 ForwardNode { node_id: nodes[1].node_id, short_channel_id: None },
535         ];
536         let blinded_path = BlindedPath::new_for_message(&intermediate_nodes, nodes[2].node_id, &*nodes[2].entropy_source, &secp_ctx).unwrap();
537         let destination = Destination::BlindedPath(blinded_path);
538
539         nodes[0].messenger.send_onion_message(test_msg.clone(), destination, None).unwrap();
540         nodes[2].custom_message_handler.expect_message(TestCustomMessage::Pong);
541         pass_along_path(&nodes);
542
543         // Try with a two-hop blinded path where we are the introduction node.
544         let intermediate_nodes = [ForwardNode { node_id: nodes[0].node_id, short_channel_id: None }];
545         let blinded_path = BlindedPath::new_for_message(&intermediate_nodes, nodes[1].node_id, &*nodes[1].entropy_source, &secp_ctx).unwrap();
546         let destination = Destination::BlindedPath(blinded_path);
547         nodes[0].messenger.send_onion_message(test_msg, destination, None).unwrap();
548         nodes[1].custom_message_handler.expect_message(TestCustomMessage::Pong);
549         nodes.remove(2);
550         pass_along_path(&nodes);
551 }
552
553 #[test]
554 fn invalid_blinded_path_error() {
555         // Make sure we error as expected if a provided blinded path has 0 hops.
556         let nodes = create_nodes(3);
557         let test_msg = TestCustomMessage::Pong;
558
559         let secp_ctx = Secp256k1::new();
560         let intermediate_nodes = [ForwardNode { node_id: nodes[1].node_id, short_channel_id: None }];
561         let mut blinded_path = BlindedPath::new_for_message(&intermediate_nodes, nodes[2].node_id, &*nodes[2].entropy_source, &secp_ctx).unwrap();
562         blinded_path.blinded_hops.clear();
563         let destination = Destination::BlindedPath(blinded_path);
564         let err = nodes[0].messenger.send_onion_message(test_msg, destination, None).unwrap_err();
565         assert_eq!(err, SendError::TooFewBlindedHops);
566 }
567
568 #[test]
569 fn reply_path() {
570         let mut nodes = create_nodes(4);
571         let test_msg = TestCustomMessage::Ping;
572         let secp_ctx = Secp256k1::new();
573
574         // Destination::Node
575         let path = OnionMessagePath {
576                 intermediate_nodes: vec![nodes[1].node_id, nodes[2].node_id],
577                 destination: Destination::Node(nodes[3].node_id),
578                 first_node_addresses: None,
579         };
580         let intermediate_nodes = [
581                 ForwardNode { node_id: nodes[2].node_id, short_channel_id: None },
582                 ForwardNode { node_id: nodes[1].node_id, short_channel_id: None },
583         ];
584         let reply_path = BlindedPath::new_for_message(&intermediate_nodes, nodes[0].node_id, &*nodes[0].entropy_source, &secp_ctx).unwrap();
585         nodes[0].messenger.send_onion_message_using_path(path, test_msg.clone(), Some(reply_path)).unwrap();
586         nodes[3].custom_message_handler.expect_message(TestCustomMessage::Ping);
587         pass_along_path(&nodes);
588         // Make sure the last node successfully decoded the reply path.
589         nodes[0].custom_message_handler.expect_message(TestCustomMessage::Pong);
590         nodes.reverse();
591         pass_along_path(&nodes);
592
593         // Destination::BlindedPath
594         let intermediate_nodes = [
595                 ForwardNode { node_id: nodes[1].node_id, short_channel_id: None },
596                 ForwardNode { node_id: nodes[2].node_id, short_channel_id: None },
597         ];
598         let blinded_path = BlindedPath::new_for_message(&intermediate_nodes, nodes[3].node_id, &*nodes[3].entropy_source, &secp_ctx).unwrap();
599         let destination = Destination::BlindedPath(blinded_path);
600         let intermediate_nodes = [
601                 ForwardNode { node_id: nodes[2].node_id, short_channel_id: None },
602                 ForwardNode { node_id: nodes[1].node_id, short_channel_id: None },
603         ];
604         let reply_path = BlindedPath::new_for_message(&intermediate_nodes, nodes[0].node_id, &*nodes[0].entropy_source, &secp_ctx).unwrap();
605
606         nodes[0].messenger.send_onion_message(test_msg, destination, Some(reply_path)).unwrap();
607         nodes[3].custom_message_handler.expect_message(TestCustomMessage::Ping);
608         pass_along_path(&nodes);
609
610         // Make sure the last node successfully decoded the reply path.
611         nodes[0].custom_message_handler.expect_message(TestCustomMessage::Pong);
612         nodes.reverse();
613         pass_along_path(&nodes);
614 }
615
616 #[test]
617 fn invalid_custom_message_type() {
618         let nodes = create_nodes(2);
619
620         #[derive(Debug)]
621         struct InvalidCustomMessage{}
622         impl OnionMessageContents for InvalidCustomMessage {
623                 fn tlv_type(&self) -> u64 {
624                         // Onion message contents must have a TLV >= 64.
625                         63
626                 }
627                 fn msg_type(&self) -> &'static str {
628                         "Invalid Message"
629                 }
630         }
631
632         impl Writeable for InvalidCustomMessage {
633                 fn write<W: Writer>(&self, _w: &mut W) -> Result<(), io::Error> { unreachable!() }
634         }
635
636         let test_msg = InvalidCustomMessage {};
637         let destination = Destination::Node(nodes[1].node_id);
638         let err = nodes[0].messenger.send_onion_message(test_msg, destination, None).unwrap_err();
639         assert_eq!(err, SendError::InvalidMessage);
640 }
641
642 #[test]
643 fn peer_buffer_full() {
644         let nodes = create_nodes(2);
645         let test_msg = TestCustomMessage::Ping;
646         let destination = Destination::Node(nodes[1].node_id);
647         for _ in 0..188 { // Based on MAX_PER_PEER_BUFFER_SIZE in OnionMessenger
648                 nodes[0].messenger.send_onion_message(test_msg.clone(), destination.clone(), None).unwrap();
649         }
650         let err = nodes[0].messenger.send_onion_message(test_msg, destination, None).unwrap_err();
651         assert_eq!(err, SendError::BufferFull);
652 }
653
654 #[test]
655 fn many_hops() {
656         // Check we can send over a route with many hops. This will exercise our logic for onion messages
657         // of size [`crate::onion_message::packet::BIG_PACKET_HOP_DATA_LEN`].
658         let num_nodes: usize = 25;
659         let nodes = create_nodes(num_nodes as u8);
660         let test_msg = TestCustomMessage::Pong;
661
662         let mut intermediate_nodes = vec![];
663         for i in 1..(num_nodes-1) {
664                 intermediate_nodes.push(nodes[i].node_id);
665         }
666
667         let path = OnionMessagePath {
668                 intermediate_nodes,
669                 destination: Destination::Node(nodes[num_nodes-1].node_id),
670                 first_node_addresses: None,
671         };
672         nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap();
673         nodes[num_nodes-1].custom_message_handler.expect_message(TestCustomMessage::Pong);
674         pass_along_path(&nodes);
675 }
676
677 #[test]
678 fn requests_peer_connection_for_buffered_messages() {
679         let nodes = create_nodes(3);
680         let message = TestCustomMessage::Ping;
681         let secp_ctx = Secp256k1::new();
682         add_channel_to_graph(&nodes[0], &nodes[1], &secp_ctx, 42);
683
684         let intermediate_nodes = [ForwardNode { node_id: nodes[1].node_id, short_channel_id: None }];
685         let blinded_path = BlindedPath::new_for_message(
686                 &intermediate_nodes, nodes[2].node_id, &*nodes[0].entropy_source, &secp_ctx
687         ).unwrap();
688         let destination = Destination::BlindedPath(blinded_path);
689
690         // Buffer an onion message for a connected peer
691         nodes[0].messenger.send_onion_message(message.clone(), destination.clone(), None).unwrap();
692         assert!(release_events(&nodes[0]).is_empty());
693         assert!(nodes[0].messenger.next_onion_message_for_peer(nodes[1].node_id).is_some());
694         assert!(nodes[0].messenger.next_onion_message_for_peer(nodes[1].node_id).is_none());
695
696         // Buffer an onion message for a disconnected peer
697         disconnect_peers(&nodes[0], &nodes[1]);
698         assert!(nodes[0].messenger.next_onion_message_for_peer(nodes[1].node_id).is_none());
699         nodes[0].messenger.send_onion_message(message, destination, None).unwrap();
700
701         // Check that a ConnectionNeeded event for the peer is provided
702         let events = release_events(&nodes[0]);
703         assert_eq!(events.len(), 1);
704         match &events[0] {
705                 Event::ConnectionNeeded { node_id, .. } => assert_eq!(*node_id, nodes[1].node_id),
706                 e => panic!("Unexpected event: {:?}", e),
707         }
708
709         // Release the buffered onion message when reconnected
710         connect_peers(&nodes[0], &nodes[1]);
711         assert!(nodes[0].messenger.next_onion_message_for_peer(nodes[1].node_id).is_some());
712         assert!(nodes[0].messenger.next_onion_message_for_peer(nodes[1].node_id).is_none());
713 }
714
715 #[test]
716 fn drops_buffered_messages_waiting_for_peer_connection() {
717         let nodes = create_nodes(3);
718         let message = TestCustomMessage::Ping;
719         let secp_ctx = Secp256k1::new();
720         add_channel_to_graph(&nodes[0], &nodes[1], &secp_ctx, 42);
721
722         let intermediate_nodes = [ForwardNode { node_id: nodes[1].node_id, short_channel_id: None }];
723         let blinded_path = BlindedPath::new_for_message(
724                 &intermediate_nodes, nodes[2].node_id, &*nodes[0].entropy_source, &secp_ctx
725         ).unwrap();
726         let destination = Destination::BlindedPath(blinded_path);
727
728         // Buffer an onion message for a disconnected peer
729         disconnect_peers(&nodes[0], &nodes[1]);
730         nodes[0].messenger.send_onion_message(message, destination, None).unwrap();
731
732         // Release the event so the timer can start ticking
733         let events = release_events(&nodes[0]);
734         assert_eq!(events.len(), 1);
735         match &events[0] {
736                 Event::ConnectionNeeded { node_id, .. } => assert_eq!(*node_id, nodes[1].node_id),
737                 e => panic!("Unexpected event: {:?}", e),
738         }
739
740         // Drop buffered messages for a disconnected peer after some timer ticks
741         use crate::onion_message::messenger::MAX_TIMER_TICKS;
742         for _ in 0..=MAX_TIMER_TICKS {
743                 nodes[0].messenger.timer_tick_occurred();
744         }
745         connect_peers(&nodes[0], &nodes[1]);
746         assert!(nodes[0].messenger.next_onion_message_for_peer(nodes[1].node_id).is_none());
747 }
748
749 #[test]
750 fn intercept_offline_peer_oms() {
751         // Ensure that if OnionMessenger is initialized with
752         // new_with_offline_peer_interception, we will intercept OMs for offline
753         // peers, generate the right events, and forward OMs when they are re-injected
754         // by the user.
755         let node_cfgs = vec![MessengerCfg::new(), MessengerCfg::new().with_offline_peer_interception(), MessengerCfg::new()];
756         let mut nodes = create_nodes_using_cfgs(node_cfgs);
757
758         let peer_conn_evs = release_events(&nodes[1]);
759         assert_eq!(peer_conn_evs.len(), 2);
760         for (i, ev) in peer_conn_evs.iter().enumerate() {
761                 match ev {
762                         Event::OnionMessagePeerConnected { peer_node_id } => {
763                                 let node_idx = if i == 0 { 0 } else { 2 };
764                                 assert_eq!(peer_node_id, &nodes[node_idx].node_id);
765                         },
766                         _ => panic!()
767                 }
768         }
769
770         let message = TestCustomMessage::Pong;
771         let secp_ctx = Secp256k1::new();
772         let intermediate_nodes = [ForwardNode { node_id: nodes[1].node_id, short_channel_id: None }];
773         let blinded_path = BlindedPath::new_for_message(
774                 &intermediate_nodes, nodes[2].node_id, &*nodes[2].entropy_source, &secp_ctx
775         ).unwrap();
776         let destination = Destination::BlindedPath(blinded_path);
777
778         // Disconnect the peers to ensure we intercept the OM.
779         disconnect_peers(&nodes[1], &nodes[2]);
780         nodes[0].messenger.send_onion_message(message, destination, None).unwrap();
781         let mut final_node_vec = nodes.split_off(2);
782         pass_along_path(&nodes);
783
784         let mut events = release_events(&nodes[1]);
785         assert_eq!(events.len(), 1);
786         let onion_message = match events.remove(0) {
787                 Event::OnionMessageIntercepted { peer_node_id, message } => {
788                         assert_eq!(peer_node_id, final_node_vec[0].node_id);
789                         message
790                 },
791                 _ => panic!()
792         };
793
794         // Ensure that we'll refuse to forward the re-injected OM until after the
795         // outbound peer comes back online.
796         let err = nodes[1].messenger.forward_onion_message(onion_message.clone(), &final_node_vec[0].node_id).unwrap_err();
797         assert_eq!(err, SendError::InvalidFirstHop(final_node_vec[0].node_id));
798
799         connect_peers(&nodes[1], &final_node_vec[0]);
800         let peer_conn_ev = release_events(&nodes[1]);
801         assert_eq!(peer_conn_ev.len(), 1);
802         match peer_conn_ev[0] {
803                 Event::OnionMessagePeerConnected { peer_node_id } => {
804                         assert_eq!(peer_node_id, final_node_vec[0].node_id);
805                 },
806                 _ => panic!()
807         }
808
809         nodes[1].messenger.forward_onion_message(onion_message, &final_node_vec[0].node_id).unwrap();
810         final_node_vec[0].custom_message_handler.expect_message(TestCustomMessage::Pong);
811         pass_along_path(&vec![nodes.remove(1), final_node_vec.remove(0)]);
812 }
813
814 #[test]
815 fn spec_test_vector() {
816         let node_cfgs = [
817                 "4141414141414141414141414141414141414141414141414141414141414141", // Alice
818                 "4242424242424242424242424242424242424242424242424242424242424242", // Bob
819                 "4343434343434343434343434343434343434343434343434343434343434343", // Carol
820                 "4444444444444444444444444444444444444444444444444444444444444444", // Dave
821         ]
822                 .iter()
823                 .map(|secret_hex| SecretKey::from_slice(&<Vec<u8>>::from_hex(secret_hex).unwrap()).unwrap())
824                 .map(|secret| MessengerCfg::new().with_node_secret(secret))
825                 .collect();
826         let nodes = create_nodes_using_cfgs(node_cfgs);
827
828         // Hardcode the sender->Alice onion message, because it includes an unknown TLV of type 1, which
829         // LDK doesn't support constructing.
830         let sender_to_alice_packet_bytes = <Vec<u8>>::from_hex("0002531fe6068134503d2723133227c867ac8fa6c83c537e9a44c3c5bdbdcb1fe33793b828776d70aabbd8cef1a5b52d5a397ae1a20f20435ff6057cd8be339d5aee226660ef73b64afa45dbf2e6e8e26eb96a259b2db5aeecda1ce2e768bbc35d389d7f320ca3d2bd14e2689bef2f5ac0307eaaabc1924eb972c1563d4646ae131accd39da766257ed35ea36e4222527d1db4fa7b2000aab9eafcceed45e28b5560312d4e2299bd8d1e7fe27d10925966c28d497aec400b4630485e82efbabc00550996bdad5d6a9a8c75952f126d14ad2cff91e16198691a7ef2937de83209285f1fb90944b4e46bca7c856a9ce3da10cdf2a7d00dc2bf4f114bc4d3ed67b91cbde558ce9af86dc81fbdc37f8e301b29e23c1466659c62bdbf8cff5d4c20f0fb0851ec72f5e9385dd40fdd2e3ed67ca4517117825665e50a3e26f73c66998daf18e418e8aef9ce2d20da33c3629db2933640e03e7b44c2edf49e9b482db7b475cfd4c617ae1d46d5c24d697846f9f08561eac2b065f9b382501f6eabf07343ed6c602f61eab99cdb52adf63fd44a8db2d3016387ea708fc1c08591e19b4d9984ebe31edbd684c2ea86526dd8c7732b1d8d9117511dc1b643976d356258fce8313b1cb92682f41ab72dedd766f06de375f9edacbcd0ca8c99b865ea2b7952318ea1fd20775a28028b5cf59dece5de14f615b8df254eee63493a5111ea987224bea006d8f1b60d565eef06ac0da194dba2a6d02e79b2f2f34e9ca6e1984a507319d86e9d4fcaeea41b4b9144e0b1826304d4cc1da61cfc5f8b9850697df8adc5e9d6f3acb3219b02764b4909f2b2b22e799fd66c383414a84a7d791b899d4aa663770009eb122f90282c8cb9cda16aba6897edcf9b32951d0080c0f52be3ca011fbec3fb16423deb47744645c3b05fdbd932edf54ba6efd26e65340a8e9b1d1216582e1b30d64524f8ca2d6c5ba63a38f7120a3ed71bed8960bcac2feee2dd41c90be48e3c11ec518eb3d872779e4765a6cc28c6b0fa71ab57ced73ae963cc630edae4258cba2bf25821a6ae049fec2fca28b5dd1bb004d92924b65701b06dcf37f0ccd147a13a03f9bc0f98b7d78fe9058089756931e2cd0e0ed92ec6759d07b248069526c67e9e6ce095118fd3501ba0f858ef030b76c6f6beb11a09317b5ad25343f4b31aef02bc555951bc7791c2c289ecf94d5544dcd6ad3021ed8e8e3db34b2a73e1eedb57b578b068a5401836d6e382110b73690a94328c404af25e85a8d6b808893d1b71af6a31fadd8a8cc6e31ecc0d9ff7e6b91fd03c274a5c1f1ccd25b61150220a3fddb04c91012f5f7a83a5c90deb2470089d6e38cd5914b9c946eca6e9d31bbf8667d36cf87effc3f3ff283c21dd4137bd569fe7cf758feac94053e4baf7338bb592c8b7c291667fadf4a9bf9a2a154a18f612cbc7f851b3f8f2070e0a9d180622ee4f8e81b0ab250d504cef24116a3ff188cc829fcd8610b56343569e8dc997629410d1967ca9dd1d27eec5e01e4375aad16c46faba268524b154850d0d6fe3a76af2c6aa3e97647c51036049ac565370028d6a439a2672b6face56e1b171496c0722cfa22d9da631be359661617c5d5a2d286c5e19db9452c1e21a0107b6400debda2decb0c838f342dd017cdb2dccdf1fe97e3df3f881856b546997a3fed9e279c720145101567dd56be21688fed66bf9759e432a9aa89cbbd225d13cdea4ca05f7a45cfb6a682a3d5b1e18f7e6cf934fae5098108bae9058d05c3387a01d8d02a656d2bfff67e9f46b2d8a6aac28129e52efddf6e552214c3f8a45bc7a912cca9a7fec1d7d06412c6972cb9e3dc518983f56530b8bffe7f92c4b6eb47d4aef59fb513c4653a42de61bc17ad7728e7fc7590ff05a9e991de03f023d0aaf8688ed6170def5091c66576a424ac1cb").unwrap();
831         let sender_to_alice_packet_bytes_len = sender_to_alice_packet_bytes.len() as u64;
832         let mut reader = io::Cursor::new(sender_to_alice_packet_bytes);
833         let mut packet_reader = FixedLengthReader::new(&mut reader, sender_to_alice_packet_bytes_len);
834         let sender_to_alice_packet: Packet =
835                 <Packet as LengthReadable>::read(&mut packet_reader).unwrap();
836         let secp_ctx = Secp256k1::new();
837         let sender_to_alice_om = msgs::OnionMessage {
838                 blinding_point: PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&<Vec<u8>>::from_hex("6363636363636363636363636363636363636363636363636363636363636363").unwrap()).unwrap()),
839                 onion_routing_packet: sender_to_alice_packet,
840         };
841         // The spec test vectors prepend the OM message type (513) to the encoded onion message strings,
842         // which is why the asserted strings differ slightly from the spec.
843         assert_eq!(sender_to_alice_om.encode(), <Vec<u8>>::from_hex("031195a8046dcbb8e17034bca630065e7a0982e4e36f6f7e5a8d4554e4846fcd9905560002531fe6068134503d2723133227c867ac8fa6c83c537e9a44c3c5bdbdcb1fe33793b828776d70aabbd8cef1a5b52d5a397ae1a20f20435ff6057cd8be339d5aee226660ef73b64afa45dbf2e6e8e26eb96a259b2db5aeecda1ce2e768bbc35d389d7f320ca3d2bd14e2689bef2f5ac0307eaaabc1924eb972c1563d4646ae131accd39da766257ed35ea36e4222527d1db4fa7b2000aab9eafcceed45e28b5560312d4e2299bd8d1e7fe27d10925966c28d497aec400b4630485e82efbabc00550996bdad5d6a9a8c75952f126d14ad2cff91e16198691a7ef2937de83209285f1fb90944b4e46bca7c856a9ce3da10cdf2a7d00dc2bf4f114bc4d3ed67b91cbde558ce9af86dc81fbdc37f8e301b29e23c1466659c62bdbf8cff5d4c20f0fb0851ec72f5e9385dd40fdd2e3ed67ca4517117825665e50a3e26f73c66998daf18e418e8aef9ce2d20da33c3629db2933640e03e7b44c2edf49e9b482db7b475cfd4c617ae1d46d5c24d697846f9f08561eac2b065f9b382501f6eabf07343ed6c602f61eab99cdb52adf63fd44a8db2d3016387ea708fc1c08591e19b4d9984ebe31edbd684c2ea86526dd8c7732b1d8d9117511dc1b643976d356258fce8313b1cb92682f41ab72dedd766f06de375f9edacbcd0ca8c99b865ea2b7952318ea1fd20775a28028b5cf59dece5de14f615b8df254eee63493a5111ea987224bea006d8f1b60d565eef06ac0da194dba2a6d02e79b2f2f34e9ca6e1984a507319d86e9d4fcaeea41b4b9144e0b1826304d4cc1da61cfc5f8b9850697df8adc5e9d6f3acb3219b02764b4909f2b2b22e799fd66c383414a84a7d791b899d4aa663770009eb122f90282c8cb9cda16aba6897edcf9b32951d0080c0f52be3ca011fbec3fb16423deb47744645c3b05fdbd932edf54ba6efd26e65340a8e9b1d1216582e1b30d64524f8ca2d6c5ba63a38f7120a3ed71bed8960bcac2feee2dd41c90be48e3c11ec518eb3d872779e4765a6cc28c6b0fa71ab57ced73ae963cc630edae4258cba2bf25821a6ae049fec2fca28b5dd1bb004d92924b65701b06dcf37f0ccd147a13a03f9bc0f98b7d78fe9058089756931e2cd0e0ed92ec6759d07b248069526c67e9e6ce095118fd3501ba0f858ef030b76c6f6beb11a09317b5ad25343f4b31aef02bc555951bc7791c2c289ecf94d5544dcd6ad3021ed8e8e3db34b2a73e1eedb57b578b068a5401836d6e382110b73690a94328c404af25e85a8d6b808893d1b71af6a31fadd8a8cc6e31ecc0d9ff7e6b91fd03c274a5c1f1ccd25b61150220a3fddb04c91012f5f7a83a5c90deb2470089d6e38cd5914b9c946eca6e9d31bbf8667d36cf87effc3f3ff283c21dd4137bd569fe7cf758feac94053e4baf7338bb592c8b7c291667fadf4a9bf9a2a154a18f612cbc7f851b3f8f2070e0a9d180622ee4f8e81b0ab250d504cef24116a3ff188cc829fcd8610b56343569e8dc997629410d1967ca9dd1d27eec5e01e4375aad16c46faba268524b154850d0d6fe3a76af2c6aa3e97647c51036049ac565370028d6a439a2672b6face56e1b171496c0722cfa22d9da631be359661617c5d5a2d286c5e19db9452c1e21a0107b6400debda2decb0c838f342dd017cdb2dccdf1fe97e3df3f881856b546997a3fed9e279c720145101567dd56be21688fed66bf9759e432a9aa89cbbd225d13cdea4ca05f7a45cfb6a682a3d5b1e18f7e6cf934fae5098108bae9058d05c3387a01d8d02a656d2bfff67e9f46b2d8a6aac28129e52efddf6e552214c3f8a45bc7a912cca9a7fec1d7d06412c6972cb9e3dc518983f56530b8bffe7f92c4b6eb47d4aef59fb513c4653a42de61bc17ad7728e7fc7590ff05a9e991de03f023d0aaf8688ed6170def5091c66576a424ac1cb").unwrap());
844         let sender_dummy_node_id = PublicKey::from_slice(&[2; 33]).unwrap();
845         nodes[0].messenger.handle_onion_message(&sender_dummy_node_id, &sender_to_alice_om);
846         let alice_to_bob_om = nodes[0].messenger.next_onion_message_for_peer(nodes[1].node_id).unwrap();
847         assert_eq!(alice_to_bob_om.encode(), <Vec<u8>>::from_hex("031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f05560002536d53f93796cad550b6c68662dca41f7e8c221c31022c64dd1a627b2df3982b25eac261e88369cfc66e1e3b6d9829cb3dcd707046e68a7796065202a7904811bf2608c5611cf74c9eb5371c7eb1a4428bb39a041493e2a568ddb0b2482a6cc6711bc6116cef144ebf988073cb18d9dd4ce2d3aa9de91a7dc6d7c6f11a852024626e66b41ba1158055505dff9cb15aa51099f315564d9ee3ed6349665dc3e209eedf9b5805ee4f69d315df44c80e63d0e2efbdab60ec96f44a3447c6a6ddb1efb6aa4e072bde1dab974081646bfddf3b02daa2b83847d74dd336465e76e9b8fecc2b0414045eeedfc39939088a76820177dd1103c99939e659beb07197bab9f714b30ba8dc83738e9a6553a57888aaeda156c68933a2f4ff35e3f81135076b944ed9856acbfee9c61299a5d1763eadd14bf5eaf71304c8e165e590d7ecbcd25f1650bf5b6c2ad1823b2dc9145e168974ecf6a2273c94decff76d94bc6708007a17f22262d63033c184d0166c14f41b225a956271947aae6ce65890ed8f0d09c6ffe05ec02ee8b9de69d7077a0c5adeb813aabcc1ba8975b73ab06ddea5f4db3c23a1de831602de2b83f990d4133871a1a81e53f86393e6a7c3a7b73f0c099fa72afe26c3027bb9412338a19303bd6e6591c04fb4cde9b832b5f41ae199301ea8c303b5cef3aca599454273565de40e1148156d1f97c1aa9e58459ab318304075e034f5b7899c12587b86776a18a1da96b7bcdc22864fccc4c41538ebce92a6f054d53bf46770273a70e75fe0155cd6d2f2e937465b0825ce3123b8c206fac4c30478fa0f08a97ade7216dce11626401374993213636e93545a31f500562130f2feb04089661ad8c34d5a4cbd2e4e426f37cb094c786198a220a2646ecadc38c04c29ee67b19d662c209a7b30bfecc7fe8bf7d274de0605ee5df4db490f6d32234f6af639d3fce38a2801bcf8d51e9c090a6c6932355a83848129a378095b34e71cb8f51152dc035a4fe8e802fec8de221a02ba5afd6765ce570bef912f87357936ea0b90cb2990f56035e89539ec66e8dbd6ed50835158614096990e019c3eba3d7dd6a77147641c6145e8b17552cd5cf7cd163dd40b9eaeba8c78e03a2cd8c0b7997d6f56d35f38983a202b4eb8a54e14945c4de1a6dde46167e11708b7a5ff5cb9c0f7fc12fae49a012aa90bb1995c038130b749c48e6f1ffb732e92086def42af10fbc460d94abeb7b2fa744a5e9a491d62a08452be8cf2fdef573deedc1fe97098bce889f98200b26f9bb99da9aceddda6d793d8e0e44a2601ef4590cfbb5c3d0197aac691e3d31c20fd8e38764962ca34dabeb85df28feabaf6255d4d0df3d814455186a84423182caa87f9673df770432ad8fdfe78d4888632d460d36d2719e8fa8e4b4ca10d817c5d6bc44a8b2affab8c2ba53b8bf4994d63286c2fad6be04c28661162fa1a67065ecda8ba8c13aee4a8039f4f0110e0c0da2366f178d8903e19136dad6df9d8693ce71f3a270f9941de2a93d9b67bc516207ac1687bf6e00b29723c42c7d9c90df9d5e599dbeb7b73add0a6a2b7aba82f98ac93cb6e60494040445229f983a81c34f7f686d166dfc98ec23a6318d4a02a311ac28d655ea4e0f9c3014984f31e621ef003e98c373561d9040893feece2e0fa6cd2dd565e6fbb2773a2407cb2c3273c306cf71f427f2e551c4092e067cf9869f31ac7c6c80dd52d4f85be57a891a41e34be0d564e39b4af6f46b85339254a58b205fb7e10e7d0470ee73622493f28c08962118c23a1198467e72c4ae1cd482144b419247a5895975ea90d135e2a46ef7e5794a1551a447ff0a0d299b66a7f565cd86531f5e7af5408d85d877ce95b1df12b88b7d5954903a5296325ba478ba1e1a9d1f30a2d5052b2e2889bbd64f72c72bc71d8817288a2").unwrap());
848         nodes[1].messenger.handle_onion_message(&nodes[0].node_id, &alice_to_bob_om);
849         let bob_to_carol_om = nodes[1].messenger.next_onion_message_for_peer(nodes[2].node_id).unwrap();
850         assert_eq!(bob_to_carol_om.encode(), <Vec<u8>>::from_hex("02b684babfd400c8dd48b367e9754b8021a3594a34dc94d7101776c7f6a86d0582055600029a77e8523162efa1f4208f4f2050cd5c386ddb6ce6d36235ea569d217ec52209fb85fdf7dbc4786c373eebdba0ddc184cfbe6da624f610e93f62c70f2c56be1090b926359969f040f932c03f53974db5656233bd60af375517d4323002937d784c2c88a564bcefe5c33d3fc21c26d94dfacab85e2e19685fd2ff4c543650958524439b6da68779459aee5ffc9dc543339acec73ff43be4c44ddcbe1c11d50e2411a67056ba9db7939d780f5a86123fdd3abd6f075f7a1d78ab7daf3a82798b7ec1e9f1345bc0d1e935098497067e2ae5a51ece396fcb3bb30871ad73aee51b2418b39f00c8e8e22be4a24f4b624e09cb0414dd46239de31c7be035f71e8da4f5a94d15b44061f46414d3f355069b5c5b874ba56704eb126148a22ec873407fe118972127e63ff80e682e410f297f23841777cec0517e933eaf49d7e34bd203266b42081b3a5193b51ccd34b41342bc67cf73523b741f5c012ba2572e9dda15fbe131a6ac2ff24dc2a7622d58b9f3553092cfae7fae3c8864d95f97aa49ec8edeff5d9f5782471160ee412d82ff6767030fc63eec6a93219a108cd41433834b26676a39846a944998796c79cd1cc460531b8ded659cedfd8aecefd91944f00476f1496daafb4ea6af3feacac1390ea510709783c2aa81a29de27f8959f6284f4684102b17815667cbb0645396ac7d542b878d90c42a1f7f00c4c4eedb2a22a219f38afadb4f1f562b6e000a94e75cc38f535b43a3c0384ccef127fde254a9033a317701c710b2b881065723486e3f4d3eea5e12f374a41565fe43fa137c1a252c2153dde055bb343344c65ad0529010ece29bbd405effbebfe3ba21382b94a60ac1a5ffa03f521792a67b30773cb42e862a8a02a8bbd41b842e115969c87d1ff1f8c7b5726b9f20772dd57fe6e4ea41f959a2a673ffad8e2f2a472c4c8564f3a5a47568dd75294b1c7180c500f7392a7da231b1fe9e525ea2d7251afe9ca52a17fe54a116cb57baca4f55b9b6de915924d644cba9dade4ccc01939d7935749c008bafc6d3ad01cd72341ce5ddf7a5d7d21cf0465ab7a3233433aef21f9acf2bfcdc5a8cc003adc4d82ac9d72b36eb74e05c9aa6ccf439ac92e6b84a3191f0764dd2a2e0b4cc3baa08782b232ad6ecd3ca6029bc08cc094aef3aebddcaddc30070cb6023a689641de86cfc6341c8817215a4650f844cd2ca60f2f10c6e44cfc5f23912684d4457bf4f599879d30b79bf12ef1ab8d34dddc15672b82e56169d4c770f0a2a7a960b1e8790773f5ff7fce92219808f16d061cc85e053971213676d28fb48925e9232b66533dbd938458eb2cc8358159df7a2a2e4cf87500ede2afb8ce963a845b98978edf26a6948d4932a6b95d022004556d25515fe158092ce9a913b4b4a493281393ca731e8d8e5a3449b9d888fc4e73ffcbb9c6d6d66e88e03cf6e81a0496ede6e4e4172b08c000601993af38f80c7f68c9d5fff9e0e215cff088285bf039ca731744efcb7825a272ca724517736b4890f47e306b200aa2543c363e2c9090bcf3cf56b5b86868a62471c7123a41740392fc1d5ab28da18dca66618e9af7b42b62b23aba907779e73ca03ec60e6ab9e0484b9cae6578e0fddb6386cb3468506bf6420298bf4a690947ab582255551d82487f271101c72e19e54872ab47eae144db66bc2f8194a666a5daec08d12822cb83a61946234f2dfdbd6ca7d8763e6818adee7b401fcdb1ac42f9df1ac5cc5ac131f2869013c8d6cd29d4c4e3d05bccd34ca83366d616296acf854fa05149bfd763a25b9938e96826a037fdcb85545439c76df6beed3bdbd01458f9cf984997cc4f0a7ac3cc3f5e1eeb59c09cadcf5a537f16e444149c8f17d4bdaef16c9fbabc5ef06eb0f0bf3a07a1beddfeacdaf1df5582d6dbd6bb808d6ab31bc22e5d7").unwrap());
851         nodes[2].messenger.handle_onion_message(&nodes[1].node_id, &bob_to_carol_om);
852         let carol_to_dave_om = nodes[2].messenger.next_onion_message_for_peer(nodes[3].node_id).unwrap();
853         assert_eq!(carol_to_dave_om.encode(), <Vec<u8>>::from_hex("025aaca62db7ce6b46386206ef9930daa32e979a35cb185a41cb951aa7d254b03c055600025550b2910294fa73bda99b9de9c851be9cbb481e23194a1743033630efba546b86e7d838d0f6e9cc0ed088dbf6889f0dceca3bfc745bd77d013a31311fa932a8bf1d28387d9ff521eabc651dee8f861fed609a68551145a451f017ec44978addeee97a423c08445531da488fd1ddc998e9cdbfcea59517b53fbf1833f0bbe6188dba6ca773a247220ec934010daca9cc185e1ceb136803469baac799e27a0d82abe53dc48a06a55d1f643885cc7894677dd20a4e4152577d1ba74b870b9279f065f9b340cedb3ca13b7df218e853e10ccd1b59c42a2acf93f489e170ee4373d30ab158b60fc20d3ba73a1f8c750951d69fb5b9321b968ddc8114936412346aff802df65516e1c09c51ef19849ff36c0199fd88c8bec301a30fef0c7cb497901c038611303f64e4174b5daf42832aa5586b84d2c9b95f382f4269a5d1bd4be898618dc78dfd451170f72ca16decac5b03e60702112e439cadd104fb3bbb3d5023c9b80823fdcd0a212a7e1aaa6eeb027adc7f8b3723031d135a09a979a4802788bb7861c6cc85501fb91137768b70aeab309b27b885686604ffc387004ac4f8c44b101c39bc0597ef7fd957f53fc5051f534b10eb3852100962b5e58254e5558689913c26ad6072ea41f5c5db10077cfc91101d4ae393be274c74297da5cc381cd88d54753aaa7df74b2f9da8d88a72bc9218fcd1f19e4ff4aace182312b9509c5175b6988f044c5756d232af02a451a02ca752f3c52747773acff6fd07d2032e6ce562a2c42105d106eba02d0b1904182cdc8c74875b082d4989d3a7e9f0e73de7c75d357f4af976c28c0b206c5e8123fc2391d078592d0d5ff686fd245c0a2de2e535b7cca99c0a37d432a8657393a9e3ca53eec1692159046ba52cb9bc97107349d8673f74cbc97e231f1108005c8d03e24ca813cea2294b39a7a493bcc062708f1f6cf0074e387e7d50e0666ce784ef4d31cb860f6cad767438d9ea5156ff0ae86e029e0247bf94df75ee0cda4f2006061455cb2eaff513d558863ae334cef7a3d45f55e7cc13153c6719e9901c1d4db6c03f643b69ea4860690305651794284d9e61eb848ccdf5a77794d376f0af62e46d4835acce6fd9eef5df73ebb8ea3bb48629766967f446e744ecc57ff3642c4aa1ccee9a2f72d5caa75fa05787d08b79408fce792485fdecdc25df34820fb061275d70b84ece540b0fc47b2453612be34f2b78133a64e812598fbe225fd85415f8ffe5340ce955b5fd9d67dd88c1c531dde298ed25f96df271558c812c26fa386966c76f03a6ebccbca49ac955916929bd42e134f982dde03f924c464be5fd1ba44f8dc4c3cbc8162755fd1d8f7dc044b15b1a796c53df7d8769bb167b2045b49cc71e08908796c92c16a235717cabc4bb9f60f8f66ff4fff1f9836388a99583acebdff4a7fb20f48eedcd1f4bdcc06ec8b48e35307df51d9bc81d38a94992dd135b30079e1f592da6e98dff496cb1a7776460a26b06395b176f585636ebdf7eab692b227a31d6979f5a6141292698e91346b6c806b90c7c6971e481559cae92ee8f4136f2226861f5c39ddd29bbdb118a35dece03f49a96804caea79a3dacfbf09d65f2611b5622de51d98e18151acb3bb84c09caaa0cc80edfa743a4679f37d6167618ce99e73362fa6f213409931762618a61f1738c071bba5afc1db24fe94afb70c40d731908ab9a505f76f57a7d40e708fd3df0efc5b7cbb2a7b75cd23449e09684a2f0e2bfa0d6176c35f96fe94d92fc9fa4103972781f81cb6e8df7dbeb0fc529c600d768bed3f08828b773d284f69e9a203459d88c12d6df7a75be2455fec128f07a497a2b2bf626cc6272d0419ca663e9dc66b8224227eb796f0246dcae9c5b0b6cfdbbd40c3245a610481c92047c968c9fc92c04b89cc41a0c15355a8f").unwrap());
854         // Dave handles the onion message but he'll log that he errored while decoding the hop data
855         // because he sees it as an empty onion message (the only contents of the sender's OM is "hello"
856         // with TLV type 1, which Dave ignores because (1) it's odd and he can't understand it and (2) LDK
857         // only attempts to parse custom OM TLVs with type > 64).
858         nodes[3].messenger.handle_onion_message(&nodes[2].node_id, &carol_to_dave_om);
859 }