From: shaavan Date: Mon, 15 Apr 2024 09:52:25 +0000 (+0530) Subject: Convert handle_onion_message_response to a public function and add test coverage X-Git-Tag: v0.0.124-beta~107^2~4 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=ca5b6b51d16662d968bb7b49a28ae5c65ed8493f;p=rust-lightning Convert handle_onion_message_response to a public function and add test coverage This commit modifies handle_onion_message_response to be accessible publicly as handle_onion_message, enabling users to respond asynchronously. Additionally, a new test is introduced to validate this functionality. --- diff --git a/lightning/src/onion_message/functional_tests.rs b/lightning/src/onion_message/functional_tests.rs index 029038a9f..a777dc419 100644 --- a/lightning/src/onion_message/functional_tests.rs +++ b/lightning/src/onion_message/functional_tests.rs @@ -372,6 +372,37 @@ fn three_blinded_hops() { pass_along_path(&nodes); } +#[test] +fn async_response_over_one_blinded_hop() { + // Simulate an asynchronous interaction between two nodes, Alice and Bob. + + // 1. Set up the network with two nodes: Alice and Bob. + let nodes = create_nodes(2); + let alice = &nodes[0]; + let bob = &nodes[1]; + + // 2. Define the message sent from Bob to Alice. + let message = TestCustomMessage::Request; + let path_id = Some([2; 32]); + + // 3. Simulate the creation of a Blinded Reply path provided by Bob. + let secp_ctx = Secp256k1::new(); + let reply_path = BlindedPath::new_for_message(&[], nodes[1].node_id, &*nodes[1].entropy_source, &secp_ctx).unwrap(); + + // 4. Create a responder using the reply path for Alice. + let responder = Some(Responder::new(reply_path, path_id)); + + // 5. Expect Alice to receive the message and create a response instruction for it. + alice.custom_message_handler.expect_message(message.clone()); + let response_instruction = nodes[0].custom_message_handler.handle_custom_message(message, responder); + + // 6. Simulate Alice asynchronously responding back to Bob with a response. + nodes[0].messenger.handle_onion_message_response(response_instruction); + bob.custom_message_handler.expect_message(TestCustomMessage::Response); + + pass_along_path(&nodes); +} + #[test] fn too_big_packet_error() { // Make sure we error as expected if a packet is too big to send. diff --git a/lightning/src/onion_message/messenger.rs b/lightning/src/onion_message/messenger.rs index fc3eead1e..8ab92ba64 100644 --- a/lightning/src/onion_message/messenger.rs +++ b/lightning/src/onion_message/messenger.rs @@ -264,7 +264,7 @@ pub struct Responder { impl Responder { /// Creates a new [`Responder`] instance with the provided reply path. - fn new(reply_path: BlindedPath, path_id: Option<[u8; 32]>) -> Self { + pub(super) fn new(reply_path: BlindedPath, path_id: Option<[u8; 32]>) -> Self { Responder { reply_path, path_id, @@ -1053,7 +1053,16 @@ where ) } - fn handle_onion_message_response( + /// Handles the response to an [`OnionMessage`] based on its [`ResponseInstruction`], + /// enqueueing any response for sending. + /// + /// This function is useful for asynchronous handling of [`OnionMessage`]s. + /// Handlers have the option to return [`ResponseInstruction::NoResponse`], indicating that + /// no immediate response should be sent. Then, they can transfer the associated [`Responder`] + /// to another task responsible for generating the response asynchronously. Subsequently, when + /// the response is prepared and ready for sending, that task can invoke this method to enqueue + /// the response for delivery. + pub fn handle_onion_message_response( &self, response: ResponseInstruction ) { if let ResponseInstruction::WithoutReplyPath(response) = response {