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.
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,
)
}
- fn handle_onion_message_response<T: OnionMessageContents>(
+ /// 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<T: OnionMessageContents>(
&self, response: ResponseInstruction<T>
) {
if let ResponseInstruction::WithoutReplyPath(response) = response {