1 // This file is Copyright its original authors, visible in version control
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
10 //! Onion message testing and test utilities live here.
12 use chain::keysinterface::{KeysInterface, Recipient};
13 use super::{BlindedRoute, Destination, OnionMessenger};
14 use util::enforcing_trait_impls::EnforcingSigner;
17 use bitcoin::network::constants::Network;
18 use bitcoin::secp256k1::{PublicKey, Secp256k1};
22 struct MessengerNode {
23 keys_manager: Arc<test_utils::TestKeysInterface>,
24 messenger: OnionMessenger<EnforcingSigner, Arc<test_utils::TestKeysInterface>, Arc<test_utils::TestLogger>>,
25 logger: Arc<test_utils::TestLogger>,
29 fn get_node_pk(&self) -> PublicKey {
30 let secp_ctx = Secp256k1::new();
31 PublicKey::from_secret_key(&secp_ctx, &self.keys_manager.get_node_secret(Recipient::Node).unwrap())
35 fn create_nodes(num_messengers: u8) -> Vec<MessengerNode> {
36 let mut res = Vec::new();
37 for i in 0..num_messengers {
38 let logger = Arc::new(test_utils::TestLogger::with_id(format!("node {}", i)));
39 let seed = [i as u8; 32];
40 let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&seed, Network::Testnet));
41 res.push(MessengerNode {
42 keys_manager: keys_manager.clone(),
43 messenger: OnionMessenger::new(keys_manager, logger.clone()),
50 fn pass_along_path(mut path: Vec<MessengerNode>, expected_path_id: Option<[u8; 32]>) {
51 let mut prev_node = path.remove(0);
52 let num_nodes = path.len();
53 for (idx, node) in path.into_iter().enumerate() {
54 let events = prev_node.messenger.release_pending_msgs();
55 assert_eq!(events.len(), 1);
57 let msgs = events.get(&node.get_node_pk()).unwrap();
58 assert_eq!(msgs.len(), 1);
61 node.messenger.handle_onion_message(&prev_node.get_node_pk(), &onion_msg);
62 if idx == num_nodes - 1 {
63 node.logger.assert_log_contains(
64 "lightning::onion_message::messenger".to_string(),
65 format!("Received an onion message with path_id: {:02x?}", expected_path_id).to_string(), 1);
73 let nodes = create_nodes(2);
75 nodes[0].messenger.send_onion_message(&[], Destination::Node(nodes[1].get_node_pk())).unwrap();
76 pass_along_path(nodes, None);
80 fn two_unblinded_hops() {
81 let nodes = create_nodes(3);
83 nodes[0].messenger.send_onion_message(&[nodes[1].get_node_pk()], Destination::Node(nodes[2].get_node_pk())).unwrap();
84 pass_along_path(nodes, None);
88 fn two_unblinded_two_blinded() {
89 let nodes = create_nodes(5);
91 let secp_ctx = Secp256k1::new();
92 let blinded_route = BlindedRoute::new::<EnforcingSigner, _, _>(&[nodes[3].get_node_pk(), nodes[4].get_node_pk()], &*nodes[4].keys_manager, &secp_ctx).unwrap();
94 nodes[0].messenger.send_onion_message(&[nodes[1].get_node_pk(), nodes[2].get_node_pk()], Destination::BlindedRoute(blinded_route)).unwrap();
95 pass_along_path(nodes, None);
99 fn three_blinded_hops() {
100 let nodes = create_nodes(4);
102 let secp_ctx = Secp256k1::new();
103 let blinded_route = BlindedRoute::new::<EnforcingSigner, _, _>(&[nodes[1].get_node_pk(), nodes[2].get_node_pk(), nodes[3].get_node_pk()], &*nodes[3].keys_manager, &secp_ctx).unwrap();
105 nodes[0].messenger.send_onion_message(&[], Destination::BlindedRoute(blinded_route)).unwrap();
106 pass_along_path(nodes, None);