Skip to content

Commit f73f8af

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 d3cd080 commit f73f8af

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
@@ -366,6 +366,37 @@ fn three_blinded_hops() {
366366
pass_along_path(&nodes);
367367
}
368368

369+
#[test]
370+
fn async_response_over_one_blinded_hop() {
371+
// Simulate an asynchronous interaction between two nodes, Alice and Bob.
372+
373+
// 1. Set up the network with two nodes: Alice and Bob.
374+
let nodes = create_nodes(2);
375+
let alice = &nodes[0];
376+
let bob = &nodes[1];
377+
378+
// 2. Define the message sent from Bob to Alice.
379+
let message = TestCustomMessage::Request;
380+
let path_id = Some([2; 32]);
381+
382+
// 3. Simulate the creation of a Blinded Reply path provided by Bob.
383+
let secp_ctx = Secp256k1::new();
384+
let reply_path = BlindedPath::new_for_message(&[nodes[1].node_id], &*nodes[1].entropy_source, &secp_ctx).unwrap();
385+
386+
// 4. Create a responder using the reply path for Alice.
387+
let responder = Some(Responder::new(reply_path, path_id));
388+
389+
// 5. Expect Alice to receive the message and create a response instruction for it.
390+
alice.custom_message_handler.expect_message(message.clone());
391+
let response_instruction = nodes[0].custom_message_handler.handle_custom_message(message, responder);
392+
393+
// 6. Simulate Alice asynchronously responding back to Bob with a response.
394+
nodes[0].messenger.handle_onion_message_response(response_instruction);
395+
bob.custom_message_handler.expect_message(TestCustomMessage::Response);
396+
397+
pass_along_path(&nodes);
398+
}
399+
369400
#[test]
370401
fn too_big_packet_error() {
371402
// 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
@@ -260,7 +260,7 @@ pub struct Responder {
260260

261261
impl Responder {
262262
/// Creates a new [`Responder`] instance with the provided reply path.
263-
fn new(reply_path: BlindedPath, path_id: Option<[u8; 32]>) -> Self {
263+
pub(super) fn new(reply_path: BlindedPath, path_id: Option<[u8; 32]>) -> Self {
264264
Responder {
265265
reply_path,
266266
path_id,
@@ -1043,7 +1043,9 @@ where
10431043
)
10441044
}
10451045

1046-
fn handle_onion_message_response<T: OnionMessageContents>(
1046+
/// Handles the response to an [`OnionMessage`] based on its [`ResponseInstruction`],
1047+
/// enqueueing any response for sending.
1048+
pub fn handle_onion_message_response<T: OnionMessageContents>(
10471049
&self, response: ResponseInstruction<T>
10481050
) {
10491051
if let ResponseInstruction::WithoutReplyPath(response) = response {

0 commit comments

Comments
 (0)