X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;ds=sidebyside;f=lightning%2Fsrc%2Fonion_message%2Ffunctional_tests.rs;h=acf34a3a8c8a112067bd22cd905b328166398321;hb=774b048d8f71e1e55f85a318366cd398f7941b7e;hp=82a08504fe4c7418ea7f9140f92f3bd329a381d8;hpb=210407e1bb31020be47f4a0b1eac57bb814f6d69;p=rust-lightning diff --git a/lightning/src/onion_message/functional_tests.rs b/lightning/src/onion_message/functional_tests.rs index 82a08504..acf34a3a 100644 --- a/lightning/src/onion_message/functional_tests.rs +++ b/lightning/src/onion_message/functional_tests.rs @@ -9,50 +9,54 @@ //! Onion message testing and test utilities live here. -use crate::blinded_path::BlindedPath; -use crate::ln::features::InitFeatures; +use crate::blinded_path::{BlindedPath, EmptyNodeIdLookUp}; +use crate::events::{Event, EventsProvider}; +use crate::ln::features::{ChannelFeatures, InitFeatures}; use crate::ln::msgs::{self, DecodeError, OnionMessageHandler}; +use crate::routing::gossip::{NetworkGraph, P2PGossipSync}; +use crate::routing::test_utils::{add_channel, add_or_update_node}; use crate::sign::{NodeSigner, Recipient}; use crate::util::ser::{FixedLengthReader, LengthReadable, Writeable, Writer}; use crate::util::test_utils; -use super::{CustomOnionMessageHandler, Destination, MessageRouter, OffersMessage, OffersMessageHandler, OnionMessageContents, OnionMessagePath, OnionMessenger, PendingOnionMessage, SendError}; +use super::messenger::{CustomOnionMessageHandler, DefaultMessageRouter, Destination, OnionMessagePath, OnionMessenger, PendingOnionMessage, SendError}; +use super::offers::{OffersMessage, OffersMessageHandler}; +use super::packet::{OnionMessageContents, Packet}; use bitcoin::network::constants::Network; use bitcoin::hashes::hex::FromHex; -use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey}; +use bitcoin::secp256k1::{All, PublicKey, Secp256k1, SecretKey}; use crate::io; use crate::io_extras::read_to_end; use crate::sync::{Arc, Mutex}; +use core::ops::Deref; + use crate::prelude::*; struct MessengerNode { node_id: PublicKey, + privkey: SecretKey, entropy_source: Arc, messenger: OnionMessenger< Arc, Arc, Arc, - Arc, + Arc, + Arc>>, + Arc, + Arc + >>, Arc, Arc >, custom_message_handler: Arc, -} - -struct TestMessageRouter {} - -impl MessageRouter for TestMessageRouter { - fn find_path( - &self, _sender: PublicKey, _peers: Vec, destination: Destination - ) -> Result { - Ok(OnionMessagePath { - intermediate_nodes: vec![], - destination, - addresses: None, - }) - } + gossip_sync: Arc>>, + Arc, + Arc + >> } struct TestOffersMessageHandler {} @@ -159,6 +163,12 @@ fn create_nodes(num_messengers: u8) -> Vec { } fn create_nodes_using_secrets(secrets: Vec) -> Vec { + let gossip_logger = Arc::new(test_utils::TestLogger::with_id("gossip".to_string())); + let network_graph = Arc::new(NetworkGraph::new(Network::Testnet, gossip_logger.clone())); + let gossip_sync = Arc::new( + P2PGossipSync::new(network_graph.clone(), None, gossip_logger) + ); + let mut nodes = Vec::new(); for (i, secret_key) in secrets.into_iter().enumerate() { let logger = Arc::new(test_utils::TestLogger::with_id(format!("node {}", i))); @@ -166,29 +176,63 @@ fn create_nodes_using_secrets(secrets: Vec) -> Vec { let entropy_source = Arc::new(test_utils::TestKeysInterface::new(&seed, Network::Testnet)); let node_signer = Arc::new(test_utils::TestNodeSigner::new(secret_key)); - let message_router = Arc::new(TestMessageRouter {}); + let node_id_lookup = Arc::new(EmptyNodeIdLookUp {}); + let message_router = Arc::new( + DefaultMessageRouter::new(network_graph.clone(), entropy_source.clone()) + ); let offers_message_handler = Arc::new(TestOffersMessageHandler {}); let custom_message_handler = Arc::new(TestCustomMessageHandler::new()); nodes.push(MessengerNode { + privkey: secret_key, node_id: node_signer.get_node_id(Recipient::Node).unwrap(), entropy_source: entropy_source.clone(), messenger: OnionMessenger::new( - entropy_source, node_signer, logger.clone(), message_router, + entropy_source, node_signer, logger.clone(), node_id_lookup, message_router, offers_message_handler, custom_message_handler.clone() ), custom_message_handler, + gossip_sync: gossip_sync.clone(), }); } for i in 0..nodes.len() - 1 { - let mut features = InitFeatures::empty(); - features.set_onion_messages_optional(); - let init_msg = msgs::Init { features, networks: None, remote_network_address: None }; - nodes[i].messenger.peer_connected(&nodes[i + 1].node_id, &init_msg.clone(), true).unwrap(); - nodes[i + 1].messenger.peer_connected(&nodes[i].node_id, &init_msg.clone(), false).unwrap(); + connect_peers(&nodes[i], &nodes[i + 1]); } nodes } +fn connect_peers(node_a: &MessengerNode, node_b: &MessengerNode) { + let mut features = InitFeatures::empty(); + features.set_onion_messages_optional(); + let init_msg = msgs::Init { features, networks: None, remote_network_address: None }; + node_a.messenger.peer_connected(&node_b.node_id, &init_msg.clone(), true).unwrap(); + node_b.messenger.peer_connected(&node_a.node_id, &init_msg.clone(), false).unwrap(); +} + +fn disconnect_peers(node_a: &MessengerNode, node_b: &MessengerNode) { + node_a.messenger.peer_disconnected(&node_b.node_id); + node_b.messenger.peer_disconnected(&node_a.node_id); +} + +fn release_events(node: &MessengerNode) -> Vec { + let events = core::cell::RefCell::new(Vec::new()); + node.messenger.process_pending_events(&|e| events.borrow_mut().push(e)); + events.into_inner() +} + +fn add_channel_to_graph( + node_a: &MessengerNode, node_b: &MessengerNode, secp_ctx: &Secp256k1, short_channel_id: u64 +) { + let gossip_sync = node_a.gossip_sync.deref(); + let privkey_a = &node_a.privkey; + let privkey_b = &node_b.privkey; + let channel_features = ChannelFeatures::empty(); + let node_features_a = node_a.messenger.provided_node_features(); + let node_features_b = node_b.messenger.provided_node_features(); + add_channel(gossip_sync, secp_ctx, privkey_a, privkey_b, channel_features, short_channel_id); + add_or_update_node(gossip_sync, secp_ctx, privkey_a, node_features_a, 1); + add_or_update_node(gossip_sync, secp_ctx, privkey_b, node_features_b, 1); +} + fn pass_along_path(path: &Vec) { let mut prev_node = &path[0]; for node in path.into_iter().skip(1) { @@ -208,12 +252,8 @@ fn one_unblinded_hop() { let nodes = create_nodes(2); let test_msg = TestCustomMessage::Response; - let path = OnionMessagePath { - intermediate_nodes: vec![], - destination: Destination::Node(nodes[1].node_id), - addresses: None, - }; - nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap(); + let destination = Destination::Node(nodes[1].node_id); + nodes[0].messenger.send_onion_message(test_msg, destination, None).unwrap(); nodes[1].custom_message_handler.expect_message(TestCustomMessage::Response); pass_along_path(&nodes); } @@ -226,8 +266,9 @@ fn two_unblinded_hops() { let path = OnionMessagePath { intermediate_nodes: vec![nodes[1].node_id], destination: Destination::Node(nodes[2].node_id), - addresses: None, + first_node_addresses: None, }; + nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap(); nodes[2].custom_message_handler.expect_message(TestCustomMessage::Response); pass_along_path(&nodes); @@ -240,12 +281,8 @@ fn one_blinded_hop() { let secp_ctx = Secp256k1::new(); let blinded_path = BlindedPath::new_for_message(&[nodes[1].node_id], &*nodes[1].entropy_source, &secp_ctx).unwrap(); - let path = OnionMessagePath { - intermediate_nodes: vec![], - destination: Destination::BlindedPath(blinded_path), - addresses: None, - }; - nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap(); + let destination = Destination::BlindedPath(blinded_path); + nodes[0].messenger.send_onion_message(test_msg, destination, None).unwrap(); nodes[1].custom_message_handler.expect_message(TestCustomMessage::Response); pass_along_path(&nodes); } @@ -260,7 +297,7 @@ fn two_unblinded_two_blinded() { let path = OnionMessagePath { intermediate_nodes: vec![nodes[1].node_id, nodes[2].node_id], destination: Destination::BlindedPath(blinded_path), - addresses: None, + first_node_addresses: None, }; nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap(); @@ -275,13 +312,9 @@ fn three_blinded_hops() { let secp_ctx = Secp256k1::new(); let blinded_path = BlindedPath::new_for_message(&[nodes[1].node_id, nodes[2].node_id, nodes[3].node_id], &*nodes[3].entropy_source, &secp_ctx).unwrap(); - let path = OnionMessagePath { - intermediate_nodes: vec![], - destination: Destination::BlindedPath(blinded_path), - addresses: None, - }; + let destination = Destination::BlindedPath(blinded_path); - nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap(); + nodes[0].messenger.send_onion_message(test_msg, destination, None).unwrap(); nodes[3].custom_message_handler.expect_message(TestCustomMessage::Response); pass_along_path(&nodes); } @@ -297,7 +330,7 @@ fn too_big_packet_error() { let path = OnionMessagePath { intermediate_nodes: hops, destination: Destination::Node(hop_node_id), - addresses: None, + first_node_addresses: None, }; let err = nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap_err(); assert_eq!(err, SendError::TooBigPacket); @@ -312,24 +345,16 @@ fn we_are_intro_node() { let secp_ctx = Secp256k1::new(); let blinded_path = BlindedPath::new_for_message(&[nodes[0].node_id, nodes[1].node_id, nodes[2].node_id], &*nodes[2].entropy_source, &secp_ctx).unwrap(); - let path = OnionMessagePath { - intermediate_nodes: vec![], - destination: Destination::BlindedPath(blinded_path), - addresses: None, - }; + let destination = Destination::BlindedPath(blinded_path); - nodes[0].messenger.send_onion_message_using_path(path, test_msg.clone(), None).unwrap(); + nodes[0].messenger.send_onion_message(test_msg.clone(), destination, None).unwrap(); nodes[2].custom_message_handler.expect_message(TestCustomMessage::Response); pass_along_path(&nodes); // Try with a two-hop blinded path where we are the introduction node. let blinded_path = BlindedPath::new_for_message(&[nodes[0].node_id, nodes[1].node_id], &*nodes[1].entropy_source, &secp_ctx).unwrap(); - let path = OnionMessagePath { - intermediate_nodes: vec![], - destination: Destination::BlindedPath(blinded_path), - addresses: None, - }; - nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap(); + let destination = Destination::BlindedPath(blinded_path); + nodes[0].messenger.send_onion_message(test_msg, destination, None).unwrap(); nodes[1].custom_message_handler.expect_message(TestCustomMessage::Response); nodes.remove(2); pass_along_path(&nodes); @@ -345,12 +370,8 @@ fn invalid_blinded_path_error() { let secp_ctx = Secp256k1::new(); let mut blinded_path = BlindedPath::new_for_message(&[nodes[1].node_id, nodes[2].node_id], &*nodes[2].entropy_source, &secp_ctx).unwrap(); blinded_path.blinded_hops.clear(); - let path = OnionMessagePath { - intermediate_nodes: vec![], - destination: Destination::BlindedPath(blinded_path), - addresses: None, - }; - let err = nodes[0].messenger.send_onion_message_using_path(path, test_msg.clone(), None).unwrap_err(); + let destination = Destination::BlindedPath(blinded_path); + let err = nodes[0].messenger.send_onion_message(test_msg, destination, None).unwrap_err(); assert_eq!(err, SendError::TooFewBlindedHops); } @@ -364,7 +385,7 @@ fn reply_path() { let path = OnionMessagePath { intermediate_nodes: vec![nodes[1].node_id, nodes[2].node_id], destination: Destination::Node(nodes[3].node_id), - addresses: None, + first_node_addresses: None, }; let reply_path = BlindedPath::new_for_message(&[nodes[2].node_id, nodes[1].node_id, nodes[0].node_id], &*nodes[0].entropy_source, &secp_ctx).unwrap(); nodes[0].messenger.send_onion_message_using_path(path, test_msg.clone(), Some(reply_path)).unwrap(); @@ -377,14 +398,10 @@ fn reply_path() { // Destination::BlindedPath let blinded_path = BlindedPath::new_for_message(&[nodes[1].node_id, nodes[2].node_id, nodes[3].node_id], &*nodes[3].entropy_source, &secp_ctx).unwrap(); - let path = OnionMessagePath { - intermediate_nodes: vec![], - destination: Destination::BlindedPath(blinded_path), - addresses: None, - }; + let destination = Destination::BlindedPath(blinded_path); let reply_path = BlindedPath::new_for_message(&[nodes[2].node_id, nodes[1].node_id, nodes[0].node_id], &*nodes[0].entropy_source, &secp_ctx).unwrap(); - nodes[0].messenger.send_onion_message_using_path(path, test_msg, Some(reply_path)).unwrap(); + nodes[0].messenger.send_onion_message(test_msg, destination, Some(reply_path)).unwrap(); nodes[3].custom_message_handler.expect_message(TestCustomMessage::Request); pass_along_path(&nodes); @@ -412,12 +429,8 @@ fn invalid_custom_message_type() { } let test_msg = InvalidCustomMessage {}; - let path = OnionMessagePath { - intermediate_nodes: vec![], - destination: Destination::Node(nodes[1].node_id), - addresses: None, - }; - let err = nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap_err(); + let destination = Destination::Node(nodes[1].node_id); + let err = nodes[0].messenger.send_onion_message(test_msg, destination, None).unwrap_err(); assert_eq!(err, SendError::InvalidMessage); } @@ -425,15 +438,11 @@ fn invalid_custom_message_type() { fn peer_buffer_full() { let nodes = create_nodes(2); let test_msg = TestCustomMessage::Request; - let path = OnionMessagePath { - intermediate_nodes: vec![], - destination: Destination::Node(nodes[1].node_id), - addresses: None, - }; + let destination = Destination::Node(nodes[1].node_id); for _ in 0..188 { // Based on MAX_PER_PEER_BUFFER_SIZE in OnionMessenger - nodes[0].messenger.send_onion_message_using_path(path.clone(), test_msg.clone(), None).unwrap(); + nodes[0].messenger.send_onion_message(test_msg.clone(), destination.clone(), None).unwrap(); } - let err = nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap_err(); + let err = nodes[0].messenger.send_onion_message(test_msg, destination, None).unwrap_err(); assert_eq!(err, SendError::BufferFull); } @@ -453,13 +462,83 @@ fn many_hops() { let path = OnionMessagePath { intermediate_nodes, destination: Destination::Node(nodes[num_nodes-1].node_id), - addresses: None, + first_node_addresses: None, }; nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap(); nodes[num_nodes-1].custom_message_handler.expect_message(TestCustomMessage::Response); pass_along_path(&nodes); } +#[test] +fn requests_peer_connection_for_buffered_messages() { + let nodes = create_nodes(3); + let message = TestCustomMessage::Request; + let secp_ctx = Secp256k1::new(); + add_channel_to_graph(&nodes[0], &nodes[1], &secp_ctx, 42); + + let blinded_path = BlindedPath::new_for_message( + &[nodes[1].node_id, nodes[2].node_id], &*nodes[0].entropy_source, &secp_ctx + ).unwrap(); + let destination = Destination::BlindedPath(blinded_path); + + // Buffer an onion message for a connected peer + nodes[0].messenger.send_onion_message(message.clone(), destination.clone(), None).unwrap(); + assert!(release_events(&nodes[0]).is_empty()); + assert!(nodes[0].messenger.next_onion_message_for_peer(nodes[1].node_id).is_some()); + assert!(nodes[0].messenger.next_onion_message_for_peer(nodes[1].node_id).is_none()); + + // Buffer an onion message for a disconnected peer + disconnect_peers(&nodes[0], &nodes[1]); + assert!(nodes[0].messenger.next_onion_message_for_peer(nodes[1].node_id).is_none()); + nodes[0].messenger.send_onion_message(message, destination, None).unwrap(); + + // Check that a ConnectionNeeded event for the peer is provided + let events = release_events(&nodes[0]); + assert_eq!(events.len(), 1); + match &events[0] { + Event::ConnectionNeeded { node_id, .. } => assert_eq!(*node_id, nodes[1].node_id), + e => panic!("Unexpected event: {:?}", e), + } + + // Release the buffered onion message when reconnected + connect_peers(&nodes[0], &nodes[1]); + assert!(nodes[0].messenger.next_onion_message_for_peer(nodes[1].node_id).is_some()); + assert!(nodes[0].messenger.next_onion_message_for_peer(nodes[1].node_id).is_none()); +} + +#[test] +fn drops_buffered_messages_waiting_for_peer_connection() { + let nodes = create_nodes(3); + let message = TestCustomMessage::Request; + let secp_ctx = Secp256k1::new(); + add_channel_to_graph(&nodes[0], &nodes[1], &secp_ctx, 42); + + let blinded_path = BlindedPath::new_for_message( + &[nodes[1].node_id, nodes[2].node_id], &*nodes[0].entropy_source, &secp_ctx + ).unwrap(); + let destination = Destination::BlindedPath(blinded_path); + + // Buffer an onion message for a disconnected peer + disconnect_peers(&nodes[0], &nodes[1]); + nodes[0].messenger.send_onion_message(message, destination, None).unwrap(); + + // Release the event so the timer can start ticking + let events = release_events(&nodes[0]); + assert_eq!(events.len(), 1); + match &events[0] { + Event::ConnectionNeeded { node_id, .. } => assert_eq!(*node_id, nodes[1].node_id), + e => panic!("Unexpected event: {:?}", e), + } + + // Drop buffered messages for a disconnected peer after some timer ticks + use crate::onion_message::messenger::MAX_TIMER_TICKS; + for _ in 0..=MAX_TIMER_TICKS { + nodes[0].messenger.timer_tick_occurred(); + } + connect_peers(&nodes[0], &nodes[1]); + assert!(nodes[0].messenger.next_onion_message_for_peer(nodes[1].node_id).is_none()); +} + #[test] fn spec_test_vector() { let secret_keys = [ @@ -479,8 +558,8 @@ fn spec_test_vector() { let sender_to_alice_packet_bytes_len = sender_to_alice_packet_bytes.len() as u64; let mut reader = io::Cursor::new(sender_to_alice_packet_bytes); let mut packet_reader = FixedLengthReader::new(&mut reader, sender_to_alice_packet_bytes_len); - let sender_to_alice_packet: super::Packet = - ::read(&mut packet_reader).unwrap(); + let sender_to_alice_packet: Packet = + ::read(&mut packet_reader).unwrap(); let secp_ctx = Secp256k1::new(); let sender_to_alice_om = msgs::OnionMessage { blinding_point: PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&>::from_hex("6363636363636363636363636363636363636363636363636363636363636363").unwrap()).unwrap()),