Skip to content

Commit

Permalink
Use the newly introduced variants to allow respond_with_reply_path test
Browse files Browse the repository at this point in the history
1. Add a new test for ResponseInstruction::WithReplyPath, verifying
   both successful and unsuccessful reply_path creation.
  • Loading branch information
shaavan committed May 21, 2024
1 parent b4d2c77 commit 5e99d68
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 10 deletions.
1 change: 0 additions & 1 deletion lightning/src/ln/peer_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ impl CustomMessageHandler for IgnoringMessageHandler {
// Since we always return `None` in the read the handle method should never be called.
unreachable!();
}

fn get_and_clear_pending_msg(&self) -> Vec<(PublicKey, Self::CustomMessage)> { Vec::new() }

fn provided_node_features(&self) -> NodeFeatures { NodeFeatures::empty() }
Expand Down
84 changes: 75 additions & 9 deletions lightning/src/onion_message/functional_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,21 @@ impl CustomOnionMessageHandler for TestCustomMessageHandler {
Some(expected_msg) => assert_eq!(expected_msg, msg),
None => panic!("Unexpected message: {:?}", msg),
}
let response_option = match msg {
TestCustomMessage::Request => Some(TestCustomMessage::Response),
TestCustomMessage::Response => None,
TestCustomMessage::ResponseA => Some(TestCustomMessage::ResponseB),
TestCustomMessage::ResponseB => Some(TestCustomMessage::ResponseA),
let (response_option, add_reply_path) = match msg {
TestCustomMessage::Request => (Some(TestCustomMessage::Response), false),
TestCustomMessage::Response => (None, false),
TestCustomMessage::ResponseA => (Some(TestCustomMessage::ResponseB), true),
TestCustomMessage::ResponseB => (Some(TestCustomMessage::ResponseA), true),
};
if let (Some(response), Some(responder)) = (response_option, responder) {
responder.respond(response)
} else {
ResponseInstruction::NoResponse
match (response_option, responder) {
(Some(response), Some(responder)) => {
if add_reply_path {
responder.respond_with_reply_path(response)
} else {
responder.respond(response)
}
}
_ => ResponseInstruction::NoResponse,
}
}
fn read_custom_message<R: io::Read>(&self, message_type: u64, buffer: &mut R) -> Result<Option<Self::CustomMessage>, DecodeError> where Self: Sized {
Expand Down Expand Up @@ -456,6 +461,67 @@ fn async_response_over_one_blinded_hop() {
pass_along_path(&nodes);
}

fn do_test_async_response_with_reply_path_over_one_blinded_hop(reply_path_succeed: bool) {
// Simulate an asynchronous interaction between two nodes, Alice and Bob.

// 1. Set up the network with two nodes: Alice and Bob.
let mut nodes = create_nodes(2);
let alice = &nodes[0];
let bob = &nodes[1];

// 2. Define the message sent from Bob to Alice.
let message = TestCustomMessage::ResponseA;
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(&[bob.node_id], &*bob.entropy_source, &secp_ctx).unwrap();

if reply_path_succeed {
// Add a channel so that nodes are announced to each other.
// This will allow creating the reply path.
add_channel_to_graph(alice, bob, &secp_ctx, 24);
}

// 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 = alice.custom_message_handler.handle_custom_message(message, responder);

if !reply_path_succeed {
// 6. Simulate Alice attempting to asynchronously respond back to Bob
// but failing to create a reply path.
assert_eq!(
alice.messenger.handle_onion_message_response(response_instruction),
Err(SendError::PathNotFound),
);
return;
}

// 6. Simulate Alice asynchronously responding back to Bob with a response.
assert_eq!(
alice.messenger.handle_onion_message_response(response_instruction),
Ok(Some(SendSuccess::Buffered)),
);

// 7. Expect Bob to receive the response and handle it accordingly.
bob.custom_message_handler.expect_message(TestCustomMessage::ResponseB);
pass_along_path(&nodes);

// 8. Expect Alice to receive Bob's response through the created reply path and handle it.
alice.custom_message_handler.expect_message(TestCustomMessage::ResponseA);
nodes.reverse();
pass_along_path(&nodes);
}

#[test]
fn async_response_with_reply_path_over_one_blinded_hop() {
do_test_async_response_with_reply_path_over_one_blinded_hop(true);
do_test_async_response_with_reply_path_over_one_blinded_hop(false);
}

#[test]
fn too_big_packet_error() {
// Make sure we error as expected if a packet is too big to send.
Expand Down

0 comments on commit 5e99d68

Please sign in to comment.