Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow responding asynchronously to OnionMessage #2996

Merged
merged 5 commits into from
May 31, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 45 additions & 15 deletions lightning/src/onion_message/functional_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,39 @@ impl Writeable for TestCustomMessage {
}

struct TestCustomMessageHandler {
expected_messages: Mutex<VecDeque<TestCustomMessage>>,
expectations: Mutex<VecDeque<OnHandleCustomMessage>>,
}

struct OnHandleCustomMessage {
expect: TestCustomMessage,
include_reply_path: bool,
}

impl TestCustomMessageHandler {
fn new() -> Self {
Self { expected_messages: Mutex::new(VecDeque::new()) }
Self { expectations: Mutex::new(VecDeque::new()) }
}

fn expect_message(&self, message: TestCustomMessage) {
self.expected_messages.lock().unwrap().push_back(message);
self.expectations.lock().unwrap().push_back(
OnHandleCustomMessage {
expect: message,
include_reply_path: false,
}
);
}

fn expect_message_and_response(&self, message: TestCustomMessage) {
self.expectations.lock().unwrap().push_back(
OnHandleCustomMessage {
expect: message,
include_reply_path: true,
}
);
}

fn get_next_expectation(&self) -> OnHandleCustomMessage {
self.expectations.lock().unwrap().pop_front().expect("No expectations remaining")
}
}

Expand All @@ -132,25 +155,32 @@ impl Drop for TestCustomMessageHandler {
return;
}
}
assert!(self.expected_messages.lock().unwrap().is_empty());
assert!(self.expectations.lock().unwrap().is_empty());
}
}

impl CustomOnionMessageHandler for TestCustomMessageHandler {
type CustomMessage = TestCustomMessage;
fn handle_custom_message(&self, msg: Self::CustomMessage, responder: Option<Responder>) -> ResponseInstruction<Self::CustomMessage> {
match self.expected_messages.lock().unwrap().pop_front() {
Some(expected_msg) => assert_eq!(expected_msg, msg),
None => panic!("Unexpected message: {:?}", msg),
}
let response_option = match msg {
TestCustomMessage::Ping => Some(TestCustomMessage::Pong),
TestCustomMessage::Pong => None,
let expectation = self.get_next_expectation();
assert_eq!(msg, expectation.expect);

let response = match msg {
TestCustomMessage::Ping => TestCustomMessage::Pong,
TestCustomMessage::Pong => TestCustomMessage::Ping,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we responding to a pong with a ping? Shouldn't we fail if we are expected to reply to a Pong instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new test for this PR required multiple back-and-forth communication between the nodes. To allow doing so we settled on making Ping and Pong each other's response.

Here's a discussion thread related to it!

Thanks!

};
if let (Some(response), Some(responder)) = (response_option, responder) {
responder.respond(response)
} else {
ResponseInstruction::NoResponse

// Sanity check: expecting to include reply path when responder is absent should panic.
if expectation.include_reply_path && responder.is_none() {
panic!("Expected to include a reply_path, but the responder was absent.")
}

match responder {
Some(responder) if expectation.include_reply_path => {
responder.respond_with_reply_path(response)
},
Some(responder) => responder.respond(response),
None => 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