Upgrade rust-bitcoin to 0.31
[rust-lightning] / lightning / src / onion_message / functional_tests.rs
index eb0b17246c3f2eac9f8c93afd5857235adf0e973..9850e53642c705ac21b950db1e6f68cf4e7822db 100644 (file)
@@ -10,6 +10,7 @@
 //! Onion message testing and test utilities live here.
 
 use crate::blinded_path::{BlindedPath, EmptyNodeIdLookUp};
+use crate::blinded_path::message::ForwardNode;
 use crate::events::{Event, EventsProvider};
 use crate::ln::features::{ChannelFeatures, InitFeatures};
 use crate::ln::msgs::{self, DecodeError, OnionMessageHandler};
@@ -18,11 +19,11 @@ 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::messenger::{CustomOnionMessageHandler, DefaultMessageRouter, Destination, OnionMessagePath, OnionMessenger, PendingOnionMessage, SendError};
+use super::messenger::{CustomOnionMessageHandler, DefaultMessageRouter, Destination, OnionMessagePath, OnionMessenger, PendingOnionMessage, Responder, ResponseInstruction, SendError};
 use super::offers::{OffersMessage, OffersMessageHandler};
 use super::packet::{OnionMessageContents, Packet};
 
-use bitcoin::network::constants::Network;
+use bitcoin::network::Network;
 use bitcoin::hashes::hex::FromHex;
 use bitcoin::secp256k1::{All, PublicKey, Secp256k1, SecretKey};
 
@@ -73,8 +74,8 @@ impl Drop for MessengerNode {
 struct TestOffersMessageHandler {}
 
 impl OffersMessageHandler for TestOffersMessageHandler {
-       fn handle_message(&self, _message: OffersMessage) -> Option<OffersMessage> {
-               None
+       fn handle_message(&self, _message: OffersMessage, _responder: Option<Responder>) -> ResponseInstruction<OffersMessage> {
+               ResponseInstruction::NoResponse
        }
 }
 
@@ -96,6 +97,9 @@ impl OnionMessageContents for TestCustomMessage {
                        TestCustomMessage::Response => CUSTOM_RESPONSE_MESSAGE_TYPE,
                }
        }
+       fn msg_type(&self) -> &'static str {
+               "Custom Message"
+       }
 }
 
 impl Writeable for TestCustomMessage {
@@ -134,15 +138,19 @@ impl Drop for TestCustomMessageHandler {
 
 impl CustomOnionMessageHandler for TestCustomMessageHandler {
        type CustomMessage = TestCustomMessage;
-       fn handle_custom_message(&self, msg: Self::CustomMessage) -> Option<Self::CustomMessage> {
+       fn handle_custom_message(&self, msg: Self::CustomMessage, responder: Option<Responder>) -> ResponseInstruction<Self::CustomMessage> {
                match self.expected_messages.lock().unwrap().pop_front() {
                        Some(expected_msg) => assert_eq!(expected_msg, msg),
                        None => panic!("Unexpected message: {:?}", msg),
                }
-
-               match msg {
+               let response_option = match msg {
                        TestCustomMessage::Request => Some(TestCustomMessage::Response),
                        TestCustomMessage::Response => None,
+               };
+               if let (Some(response), Some(responder)) = (response_option, responder) {
+                       responder.respond(response)
+               } else {
+                       ResponseInstruction::NoResponse
                }
        }
        fn read_custom_message<R: io::Read>(&self, message_type: u64, buffer: &mut R) -> Result<Option<Self::CustomMessage>, DecodeError> where Self: Sized {
@@ -320,7 +328,7 @@ fn one_blinded_hop() {
        let test_msg = TestCustomMessage::Response;
 
        let secp_ctx = Secp256k1::new();
-       let blinded_path = BlindedPath::new_for_message(&[nodes[1].node_id], &*nodes[1].entropy_source, &secp_ctx).unwrap();
+       let blinded_path = BlindedPath::new_for_message(&[], nodes[1].node_id, &*nodes[1].entropy_source, &secp_ctx).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);
@@ -333,7 +341,8 @@ fn two_unblinded_two_blinded() {
        let test_msg = TestCustomMessage::Response;
 
        let secp_ctx = Secp256k1::new();
-       let blinded_path = BlindedPath::new_for_message(&[nodes[3].node_id, nodes[4].node_id], &*nodes[4].entropy_source, &secp_ctx).unwrap();
+       let intermediate_nodes = [ForwardNode { node_id: nodes[3].node_id, short_channel_id: None }];
+       let blinded_path = BlindedPath::new_for_message(&intermediate_nodes, nodes[4].node_id, &*nodes[4].entropy_source, &secp_ctx).unwrap();
        let path = OnionMessagePath {
                intermediate_nodes: vec![nodes[1].node_id, nodes[2].node_id],
                destination: Destination::BlindedPath(blinded_path),
@@ -351,7 +360,11 @@ fn three_blinded_hops() {
        let test_msg = TestCustomMessage::Response;
 
        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 intermediate_nodes = [
+               ForwardNode { node_id: nodes[1].node_id, short_channel_id: None },
+               ForwardNode { node_id: nodes[2].node_id, short_channel_id: None },
+       ];
+       let blinded_path = BlindedPath::new_for_message(&intermediate_nodes, nodes[3].node_id, &*nodes[3].entropy_source, &secp_ctx).unwrap();
        let destination = Destination::BlindedPath(blinded_path);
 
        nodes[0].messenger.send_onion_message(test_msg, destination, None).unwrap();
@@ -384,7 +397,11 @@ fn we_are_intro_node() {
        let test_msg = TestCustomMessage::Response;
 
        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 intermediate_nodes = [
+               ForwardNode { node_id: nodes[0].node_id, short_channel_id: None },
+               ForwardNode { node_id: nodes[1].node_id, short_channel_id: None },
+       ];
+       let blinded_path = BlindedPath::new_for_message(&intermediate_nodes, nodes[2].node_id, &*nodes[2].entropy_source, &secp_ctx).unwrap();
        let destination = Destination::BlindedPath(blinded_path);
 
        nodes[0].messenger.send_onion_message(test_msg.clone(), destination, None).unwrap();
@@ -392,7 +409,8 @@ fn we_are_intro_node() {
        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 intermediate_nodes = [ForwardNode { node_id: nodes[0].node_id, short_channel_id: None }];
+       let blinded_path = BlindedPath::new_for_message(&intermediate_nodes, nodes[1].node_id, &*nodes[1].entropy_source, &secp_ctx).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);
@@ -407,7 +425,8 @@ fn invalid_blinded_path_error() {
        let test_msg = TestCustomMessage::Response;
 
        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();
+       let intermediate_nodes = [ForwardNode { node_id: nodes[1].node_id, short_channel_id: None }];
+       let mut blinded_path = BlindedPath::new_for_message(&intermediate_nodes, nodes[2].node_id, &*nodes[2].entropy_source, &secp_ctx).unwrap();
        blinded_path.blinded_hops.clear();
        let destination = Destination::BlindedPath(blinded_path);
        let err = nodes[0].messenger.send_onion_message(test_msg, destination, None).unwrap_err();
@@ -426,7 +445,11 @@ fn reply_path() {
                destination: Destination::Node(nodes[3].node_id),
                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();
+       let intermediate_nodes = [
+               ForwardNode { node_id: nodes[2].node_id, short_channel_id: None },
+               ForwardNode { node_id: nodes[1].node_id, short_channel_id: None },
+       ];
+       let reply_path = BlindedPath::new_for_message(&intermediate_nodes, 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();
        nodes[3].custom_message_handler.expect_message(TestCustomMessage::Request);
        pass_along_path(&nodes);
@@ -436,9 +459,17 @@ fn reply_path() {
        pass_along_path(&nodes);
 
        // 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 intermediate_nodes = [
+               ForwardNode { node_id: nodes[1].node_id, short_channel_id: None },
+               ForwardNode { node_id: nodes[2].node_id, short_channel_id: None },
+       ];
+       let blinded_path = BlindedPath::new_for_message(&intermediate_nodes, nodes[3].node_id, &*nodes[3].entropy_source, &secp_ctx).unwrap();
        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();
+       let intermediate_nodes = [
+               ForwardNode { node_id: nodes[2].node_id, short_channel_id: None },
+               ForwardNode { node_id: nodes[1].node_id, short_channel_id: None },
+       ];
+       let reply_path = BlindedPath::new_for_message(&intermediate_nodes, nodes[0].node_id, &*nodes[0].entropy_source, &secp_ctx).unwrap();
 
        nodes[0].messenger.send_onion_message(test_msg, destination, Some(reply_path)).unwrap();
        nodes[3].custom_message_handler.expect_message(TestCustomMessage::Request);
@@ -461,6 +492,9 @@ fn invalid_custom_message_type() {
                        // Onion message contents must have a TLV >= 64.
                        63
                }
+               fn msg_type(&self) -> &'static str {
+                       "Invalid Message"
+               }
        }
 
        impl Writeable for InvalidCustomMessage {
@@ -515,8 +549,9 @@ fn requests_peer_connection_for_buffered_messages() {
        let secp_ctx = Secp256k1::new();
        add_channel_to_graph(&nodes[0], &nodes[1], &secp_ctx, 42);
 
+       let intermediate_nodes = [ForwardNode { node_id: nodes[1].node_id, short_channel_id: None }];
        let blinded_path = BlindedPath::new_for_message(
-               &[nodes[1].node_id, nodes[2].node_id], &*nodes[0].entropy_source, &secp_ctx
+               &intermediate_nodes, nodes[2].node_id, &*nodes[0].entropy_source, &secp_ctx
        ).unwrap();
        let destination = Destination::BlindedPath(blinded_path);
 
@@ -552,8 +587,9 @@ fn drops_buffered_messages_waiting_for_peer_connection() {
        let secp_ctx = Secp256k1::new();
        add_channel_to_graph(&nodes[0], &nodes[1], &secp_ctx, 42);
 
+       let intermediate_nodes = [ForwardNode { node_id: nodes[1].node_id, short_channel_id: None }];
        let blinded_path = BlindedPath::new_for_message(
-               &[nodes[1].node_id, nodes[2].node_id], &*nodes[0].entropy_source, &secp_ctx
+               &intermediate_nodes, nodes[2].node_id, &*nodes[0].entropy_source, &secp_ctx
        ).unwrap();
        let destination = Destination::BlindedPath(blinded_path);
 
@@ -601,8 +637,9 @@ fn intercept_offline_peer_oms() {
 
        let message = TestCustomMessage::Response;
        let secp_ctx = Secp256k1::new();
+       let intermediate_nodes = [ForwardNode { node_id: nodes[1].node_id, short_channel_id: None }];
        let blinded_path = BlindedPath::new_for_message(
-               &[nodes[1].node_id, nodes[2].node_id], &*nodes[2].entropy_source, &secp_ctx
+               &intermediate_nodes, nodes[2].node_id, &*nodes[2].entropy_source, &secp_ctx
        ).unwrap();
        let destination = Destination::BlindedPath(blinded_path);