Skip to content

Commit a635241

Browse files
committed
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.
1 parent 41567f4 commit a635241

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

lightning/src/onion_message/functional_tests.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,48 @@ fn three_blinded_hops() {
324324
pass_along_path(&nodes);
325325
}
326326

327+
#[test]
328+
fn async_response_over_one_blinded_hop() {
329+
// Simulate an asynchronous interaction between two nodes, Alice and Bob.
330+
331+
// 1. Set up the network with two nodes: Alice and Bob.
332+
let nodes = create_nodes(2);
333+
let alice = &nodes[0];
334+
let bob = &nodes[1];
335+
336+
// 2. Define the message sent from Bob to Alice.
337+
let message = TestCustomMessage::Request;
338+
let message_type = message.msg_type();
339+
let path_id = Some([2; 32]);
340+
341+
// 3. Simulate the creation of a Blinded Reply path provided by Bob.
342+
let secp_ctx = Secp256k1::new();
343+
let reply_path = BlindedPath::new_for_message(&[nodes[1].node_id], &*nodes[1].entropy_source, &secp_ctx).unwrap();
344+
345+
// 4. Create a responder using the reply path for Alice.
346+
let responder = Some(Responder::new(reply_path));
347+
348+
// 5. Expect Alice to receive the message and create a response instruction for it.
349+
alice.custom_message_handler.expect_message(message.clone());
350+
let response_instruction = nodes[0].custom_message_handler.handle_custom_message(message, responder);
351+
352+
// 6. Simulate Alice asynchronously responding back to Bob with a response.
353+
nodes[0].messenger.handle_onion_message_response(response_instruction, message_type, path_id);
354+
355+
// 7. Check the message that Alice intends to send to Bob.
356+
let events = alice.messenger.release_pending_msgs();
357+
let onion_msg = {
358+
let msgs = events.get(&bob.node_id).unwrap();
359+
assert_eq!(msgs.len(), 1);
360+
msgs[0].clone()
361+
};
362+
363+
// 8. Finally, Bob verifies that the response is of the expected type and handles it appropriately,
364+
// simulating correct response creation by Alice and acceptance by Bob.
365+
bob.custom_message_handler.expect_message(TestCustomMessage::Response);
366+
bob.messenger.handle_onion_message(&alice.node_id, &onion_msg);
367+
}
368+
327369
#[test]
328370
fn too_big_packet_error() {
329371
// Make sure we error as expected if a packet is too big to send.

lightning/src/onion_message/messenger.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,9 @@ where
887887
)
888888
}
889889

890-
fn handle_onion_message_response<T: OnionMessageContents>(
890+
/// Handles the response to an [`OnionMessage`] based on it's [`ResponseInstruction`]
891+
/// and enqueueing it for further processing.
892+
pub fn handle_onion_message_response<T: OnionMessageContents>(
891893
&self, response: ResponseInstruction<T>, message_type: &str, path_id: Option<[u8; 32]>
892894
) {
893895
if let ResponseInstruction::WithoutReplyPath(response) = response {

0 commit comments

Comments
 (0)