Skip to content

Commit fdca6f0

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 df01208 commit fdca6f0

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

lightning/src/onion_message/functional_tests.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,37 @@ fn three_blinded_hops() {
372372
pass_along_path(&nodes);
373373
}
374374

375+
#[test]
376+
fn async_response_over_one_blinded_hop() {
377+
// Simulate an asynchronous interaction between two nodes, Alice and Bob.
378+
379+
// 1. Set up the network with two nodes: Alice and Bob.
380+
let nodes = create_nodes(2);
381+
let alice = &nodes[0];
382+
let bob = &nodes[1];
383+
384+
// 2. Define the message sent from Bob to Alice.
385+
let message = TestCustomMessage::Request;
386+
let path_id = Some([2; 32]);
387+
388+
// 3. Simulate the creation of a Blinded Reply path provided by Bob.
389+
let secp_ctx = Secp256k1::new();
390+
let reply_path = BlindedPath::new_for_message(&[], nodes[1].node_id, &*nodes[1].entropy_source, &secp_ctx).unwrap();
391+
392+
// 4. Create a responder using the reply path for Alice.
393+
let responder = Some(Responder::new(reply_path, path_id));
394+
395+
// 5. Expect Alice to receive the message and create a response instruction for it.
396+
alice.custom_message_handler.expect_message(message.clone());
397+
let response_instruction = nodes[0].custom_message_handler.handle_custom_message(message, responder);
398+
399+
// 6. Simulate Alice asynchronously responding back to Bob with a response.
400+
nodes[0].messenger.handle_onion_message_response(response_instruction);
401+
bob.custom_message_handler.expect_message(TestCustomMessage::Response);
402+
403+
pass_along_path(&nodes);
404+
}
405+
375406
#[test]
376407
fn too_big_packet_error() {
377408
// Make sure we error as expected if a packet is too big to send.

lightning/src/onion_message/messenger.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ pub struct Responder {
264264

265265
impl Responder {
266266
/// Creates a new [`Responder`] instance with the provided reply path.
267-
fn new(reply_path: BlindedPath, path_id: Option<[u8; 32]>) -> Self {
267+
pub(super) fn new(reply_path: BlindedPath, path_id: Option<[u8; 32]>) -> Self {
268268
Responder {
269269
reply_path,
270270
path_id,
@@ -1053,7 +1053,17 @@ where
10531053
)
10541054
}
10551055

1056-
fn handle_onion_message_response<T: OnionMessageContents>(
1056+
/// Handles the response to an [`OnionMessage`] based on its [`ResponseInstruction`],
1057+
/// enqueueing any response for sending.
1058+
///
1059+
/// This function is useful for asynchronous handling of [`OnionMessage`]s.
1060+
///
1061+
/// Handlers have the option to return [`ResponseInstruction::NoResponse`], indicating that
1062+
/// no immediate response should be sent. Instead, they can transfer the associated [`Responder`]
1063+
/// to another task responsible for generating the response asynchronously. Subsequently, when
1064+
/// the response is prepared and ready for sending, that task can invoke this method to enqueue
1065+
/// the response for delivery.
1066+
pub fn handle_onion_message_response<T: OnionMessageContents>(
10571067
&self, response: ResponseInstruction<T>
10581068
) {
10591069
if let ResponseInstruction::WithoutReplyPath(response) = response {

0 commit comments

Comments
 (0)