Skip to content

Commit 5178f43

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 38690bf commit 5178f43

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

lightning/src/onion_message/functional_tests.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,37 @@ fn three_blinded_hops() {
326326
pass_along_path(&nodes);
327327
}
328328

329+
#[test]
330+
fn async_response_over_one_blinded_hop() {
331+
// Simulate an asynchronous interaction between two nodes, Alice and Bob.
332+
333+
// 1. Set up the network with two nodes: Alice and Bob.
334+
let nodes = create_nodes(2);
335+
let alice = &nodes[0];
336+
let bob = &nodes[1];
337+
338+
// 2. Define the message sent from Bob to Alice.
339+
let message = TestCustomMessage::Request;
340+
let path_id = Some([2; 32]);
341+
342+
// 3. Simulate the creation of a Blinded Reply path provided by Bob.
343+
let secp_ctx = Secp256k1::new();
344+
let reply_path = BlindedPath::new_for_message(&[nodes[1].node_id], &*nodes[1].entropy_source, &secp_ctx).unwrap();
345+
346+
// 4. Create a responder using the reply path for Alice.
347+
let responder = Some(Responder::new(reply_path, path_id));
348+
349+
// 5. Expect Alice to receive the message and create a response instruction for it.
350+
alice.custom_message_handler.expect_message(message.clone());
351+
let response_instruction = nodes[0].custom_message_handler.handle_custom_message(message, responder);
352+
353+
// 6. Simulate Alice asynchronously responding back to Bob with a response.
354+
nodes[0].messenger.handle_onion_message_response(response_instruction);
355+
bob.custom_message_handler.expect_message(TestCustomMessage::Response);
356+
357+
pass_along_path(&nodes);
358+
}
359+
329360
#[test]
330361
fn too_big_packet_error() {
331362
// Make sure we error as expected if a packet is too big to send.

lightning/src/onion_message/messenger.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ pub struct Responder {
258258

259259
impl Responder {
260260
/// Creates a new [`Responder`] instance with the provided reply path.
261-
fn new(reply_path: BlindedPath, path_id: Option<[u8; 32]>) -> Self {
261+
pub(super) fn new(reply_path: BlindedPath, path_id: Option<[u8; 32]>) -> Self {
262262
Responder {
263263
reply_path,
264264
path_id,
@@ -977,7 +977,9 @@ where
977977
)
978978
}
979979

980-
fn handle_onion_message_response<T: OnionMessageContents>(
980+
/// Handles the response to an [`OnionMessage`] based on its [`ResponseInstruction`],
981+
/// enqueueing any response for sending.
982+
pub fn handle_onion_message_response<T: OnionMessageContents>(
981983
&self, response: ResponseInstruction<T>
982984
) {
983985
if let ResponseInstruction::WithoutReplyPath(response) = response {

0 commit comments

Comments
 (0)