Cfg-gate async payments module and public enum variants.
[rust-lightning] / lightning / src / onion_message / functional_tests.rs
index fe2b3799e72032524e348776d7d07d28a2f147e0..9a2dc36f87324ea4447bcf221b2a4d410dd3a444 100644 (file)
@@ -19,11 +19,12 @@ 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::async_payments::{AsyncPaymentsMessageHandler, HeldHtlcAvailable, ReleaseHeldHtlc};
 use super::messenger::{CustomOnionMessageHandler, DefaultMessageRouter, Destination, OnionMessagePath, OnionMessenger, PendingOnionMessage, Responder, ResponseInstruction, SendError, SendSuccess};
 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};
 
@@ -50,6 +51,7 @@ struct MessengerNode {
                        Arc<test_utils::TestKeysInterface>
                >>,
                Arc<TestOffersMessageHandler>,
+               Arc<TestAsyncPaymentsMessageHandler>,
                Arc<TestCustomMessageHandler>
        >,
        custom_message_handler: Arc<TestCustomMessageHandler>,
@@ -79,6 +81,17 @@ impl OffersMessageHandler for TestOffersMessageHandler {
        }
 }
 
+struct TestAsyncPaymentsMessageHandler {}
+
+impl AsyncPaymentsMessageHandler for TestAsyncPaymentsMessageHandler {
+       fn held_htlc_available(
+               &self, _message: HeldHtlcAvailable, _responder: Option<Responder>,
+       ) -> ResponseInstruction<ReleaseHeldHtlc> {
+               ResponseInstruction::NoResponse
+       }
+       fn release_held_htlc(&self, _message: ReleaseHeldHtlc) {}
+}
+
 #[derive(Clone, Debug, PartialEq)]
 enum TestCustomMessage {
        Ping,
@@ -112,16 +125,39 @@ impl Writeable for TestCustomMessage {
 }
 
 struct TestCustomMessageHandler {
-       expected_messages: Mutex<VecDeque<TestCustomMessage>>,
+       expectations: Mutex<VecDeque<OnHandleCustomMessage>>,
+}
+
+struct OnHandleCustomMessage {
+       expect: TestCustomMessage,
+       include_reply_path: bool,
 }
 
 impl TestCustomMessageHandler {
        fn new() -> Self {
-               Self { expected_messages: Mutex::new(VecDeque::new()) }
+               Self { expectations: Mutex::new(VecDeque::new()) }
        }
 
        fn expect_message(&self, message: TestCustomMessage) {
-               self.expected_messages.lock().unwrap().push_back(message);
+               self.expectations.lock().unwrap().push_back(
+                       OnHandleCustomMessage {
+                               expect: message,
+                               include_reply_path: false,
+                       }
+               );
+       }
+
+       fn expect_message_and_response(&self, message: TestCustomMessage) {
+               self.expectations.lock().unwrap().push_back(
+                       OnHandleCustomMessage {
+                               expect: message,
+                               include_reply_path: true,
+                       }
+               );
+       }
+
+       fn get_next_expectation(&self) -> OnHandleCustomMessage {
+               self.expectations.lock().unwrap().pop_front().expect("No expectations remaining")
        }
 }
 
@@ -132,25 +168,32 @@ impl Drop for TestCustomMessageHandler {
                                return;
                        }
                }
-               assert!(self.expected_messages.lock().unwrap().is_empty());
+               assert!(self.expectations.lock().unwrap().is_empty());
        }
 }
 
 impl CustomOnionMessageHandler for TestCustomMessageHandler {
        type CustomMessage = TestCustomMessage;
        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),
-               }
-               let response_option = match msg {
-                       TestCustomMessage::Ping => Some(TestCustomMessage::Pong),
-                       TestCustomMessage::Pong => None,
+               let expectation = self.get_next_expectation();
+               assert_eq!(msg, expectation.expect);
+
+               let response = match msg {
+                       TestCustomMessage::Ping => TestCustomMessage::Pong,
+                       TestCustomMessage::Pong => TestCustomMessage::Ping,
                };
-               if let (Some(response), Some(responder)) = (response_option, responder) {
-                       responder.respond(response)
-               } else {
-                       ResponseInstruction::NoResponse
+
+               // Sanity check: expecting to include reply path when responder is absent should panic.
+               if expectation.include_reply_path && responder.is_none() {
+                       panic!("Expected to include a reply_path, but the responder was absent.")
+               }
+
+               match responder {
+                       Some(responder) if expectation.include_reply_path => {
+                               responder.respond_with_reply_path(response)
+                       },
+                       Some(responder) => responder.respond(response),
+                       None => ResponseInstruction::NoResponse,
                }
        }
        fn read_custom_message<R: io::Read>(&self, message_type: u64, buffer: &mut R) -> Result<Option<Self::CustomMessage>, DecodeError> where Self: Sized {
@@ -219,18 +262,19 @@ fn create_nodes_using_cfgs(cfgs: Vec<MessengerCfg>) -> Vec<MessengerNode> {
                        DefaultMessageRouter::new(network_graph.clone(), entropy_source.clone())
                );
                let offers_message_handler = Arc::new(TestOffersMessageHandler {});
+               let async_payments_message_handler = Arc::new(TestAsyncPaymentsMessageHandler {});
                let custom_message_handler = Arc::new(TestCustomMessageHandler::new());
                let messenger = if cfg.intercept_offline_peer_oms {
                        OnionMessenger::new_with_offline_peer_interception(
                                entropy_source.clone(), node_signer.clone(), logger.clone(),
                                node_id_lookup, message_router, offers_message_handler,
-                               custom_message_handler.clone()
+                               async_payments_message_handler, custom_message_handler.clone()
                        )
                } else {
                        OnionMessenger::new(
                                entropy_source.clone(), node_signer.clone(), logger.clone(),
                                node_id_lookup, message_router, offers_message_handler,
-                               custom_message_handler.clone()
+                               async_payments_message_handler, custom_message_handler.clone()
                        )
                };
                nodes.push(MessengerNode {
@@ -407,6 +451,73 @@ fn async_response_over_one_blinded_hop() {
        pass_along_path(&nodes);
 }
 
+#[test]
+fn async_response_with_reply_path_succeeds() {
+       // Simulate an asynchronous interaction between two nodes, Alice and Bob.
+       // Create a channel between the two nodes to establish them as announced nodes,
+       // which allows the creation of the reply_path for successful communication.
+
+       let mut nodes = create_nodes(2);
+       let alice = &nodes[0];
+       let bob = &nodes[1];
+       let secp_ctx = Secp256k1::new();
+
+       add_channel_to_graph(alice, bob, &secp_ctx, 24);
+
+       // Alice receives a message from Bob with an added reply_path for responding back.
+       let message = TestCustomMessage::Ping;
+       let path_id = Some([2; 32]);
+       let reply_path = BlindedPath::new_for_message(&[], bob.node_id, &*bob.entropy_source, &secp_ctx).unwrap();
+
+       // Alice asynchronously responds to Bob, expecting a response back from him.
+       let responder = Responder::new(reply_path, path_id);
+       alice.custom_message_handler.expect_message_and_response(message.clone());
+       let response_instruction = alice.custom_message_handler.handle_custom_message(message, Some(responder));
+
+       assert_eq!(
+               alice.messenger.handle_onion_message_response(response_instruction),
+               Ok(Some(SendSuccess::Buffered)),
+       );
+
+       // Set Bob's expectation and pass the Onion Message along the path.
+       bob.custom_message_handler.expect_message(TestCustomMessage::Pong);
+       pass_along_path(&nodes);
+
+       // Bob responds back to Alice using the reply_path she included with the OnionMessage.
+       // Set Alice's expectation and reverse the path for the response.
+       alice.custom_message_handler.expect_message(TestCustomMessage::Ping);
+       nodes.reverse();
+       pass_along_path(&nodes);
+}
+
+#[test]
+fn async_response_with_reply_path_fails() {
+       // Simulate an asynchronous interaction between two unannounced nodes, Alice and Bob.
+       // Since the nodes are unannounced, attempting to respond using a reply_path
+       // will fail, leading to an expected failure in communication.
+
+       let nodes = create_nodes(2);
+       let alice = &nodes[0];
+       let bob = &nodes[1];
+       let secp_ctx = Secp256k1::new();
+
+       // Alice receives a message from Bob with an added reply_path for responding back.
+       let message = TestCustomMessage::Ping;
+       let path_id = Some([2; 32]);
+       let reply_path = BlindedPath::new_for_message(&[], bob.node_id, &*bob.entropy_source, &secp_ctx).unwrap();
+
+       // Alice tries to asynchronously respond to Bob, but fails because the nodes are unannounced.
+       // Therefore, the reply_path cannot be used for the response.
+       let responder = Responder::new(reply_path, path_id);
+       alice.custom_message_handler.expect_message_and_response(message.clone());
+       let response_instruction = alice.custom_message_handler.handle_custom_message(message, Some(responder));
+
+       assert_eq!(
+               alice.messenger.handle_onion_message_response(response_instruction),
+               Err(SendError::PathNotFound),
+       );
+}
+
 #[test]
 fn too_big_packet_error() {
        // Make sure we error as expected if a packet is too big to send.