#[derive(Clone, Debug, PartialEq)]
enum TestCustomMessage {
- Request,
- Response,
+ Ping,
+ Pong,
}
-const CUSTOM_REQUEST_MESSAGE_TYPE: u64 = 4242;
-const CUSTOM_RESPONSE_MESSAGE_TYPE: u64 = 4343;
-const CUSTOM_REQUEST_MESSAGE_CONTENTS: [u8; 32] = [42; 32];
-const CUSTOM_RESPONSE_MESSAGE_CONTENTS: [u8; 32] = [43; 32];
+const CUSTOM_PING_MESSAGE_TYPE: u64 = 4242;
+const CUSTOM_PONG_MESSAGE_TYPE: u64 = 4343;
+const CUSTOM_PING_MESSAGE_CONTENTS: [u8; 32] = [42; 32];
+const CUSTOM_PONG_MESSAGE_CONTENTS: [u8; 32] = [43; 32];
impl OnionMessageContents for TestCustomMessage {
fn tlv_type(&self) -> u64 {
match self {
- TestCustomMessage::Request => CUSTOM_REQUEST_MESSAGE_TYPE,
- TestCustomMessage::Response => CUSTOM_RESPONSE_MESSAGE_TYPE,
+ TestCustomMessage::Ping => CUSTOM_PING_MESSAGE_TYPE,
+ TestCustomMessage::Pong => CUSTOM_PONG_MESSAGE_TYPE,
}
}
fn msg_type(&self) -> &'static str {
impl Writeable for TestCustomMessage {
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
match self {
- TestCustomMessage::Request => Ok(CUSTOM_REQUEST_MESSAGE_CONTENTS.write(w)?),
- TestCustomMessage::Response => Ok(CUSTOM_RESPONSE_MESSAGE_CONTENTS.write(w)?),
+ TestCustomMessage::Ping => Ok(CUSTOM_PING_MESSAGE_CONTENTS.write(w)?),
+ TestCustomMessage::Pong => Ok(CUSTOM_PONG_MESSAGE_CONTENTS.write(w)?),
}
}
}
None => panic!("Unexpected message: {:?}", msg),
}
let response_option = match msg {
- TestCustomMessage::Request => Some(TestCustomMessage::Response),
- TestCustomMessage::Response => None,
+ TestCustomMessage::Ping => Some(TestCustomMessage::Pong),
+ TestCustomMessage::Pong => None,
};
if let (Some(response), Some(responder)) = (response_option, responder) {
responder.respond(response)
}
fn read_custom_message<R: io::Read>(&self, message_type: u64, buffer: &mut R) -> Result<Option<Self::CustomMessage>, DecodeError> where Self: Sized {
match message_type {
- CUSTOM_REQUEST_MESSAGE_TYPE => {
+ CUSTOM_PING_MESSAGE_TYPE => {
let buf = read_to_end(buffer)?;
- assert_eq!(buf, CUSTOM_REQUEST_MESSAGE_CONTENTS);
- Ok(Some(TestCustomMessage::Request))
+ assert_eq!(buf, CUSTOM_PING_MESSAGE_CONTENTS);
+ Ok(Some(TestCustomMessage::Ping))
},
- CUSTOM_RESPONSE_MESSAGE_TYPE => {
+ CUSTOM_PONG_MESSAGE_TYPE => {
let buf = read_to_end(buffer)?;
- assert_eq!(buf, CUSTOM_RESPONSE_MESSAGE_CONTENTS);
- Ok(Some(TestCustomMessage::Response))
+ assert_eq!(buf, CUSTOM_PONG_MESSAGE_CONTENTS);
+ Ok(Some(TestCustomMessage::Pong))
},
_ => Ok(None),
}
#[test]
fn one_unblinded_hop() {
let nodes = create_nodes(2);
- let test_msg = TestCustomMessage::Response;
+ let test_msg = TestCustomMessage::Pong;
let destination = Destination::Node(nodes[1].node_id);
nodes[0].messenger.send_onion_message(test_msg, destination, None).unwrap();
- nodes[1].custom_message_handler.expect_message(TestCustomMessage::Response);
+ nodes[1].custom_message_handler.expect_message(TestCustomMessage::Pong);
pass_along_path(&nodes);
}
#[test]
fn two_unblinded_hops() {
let nodes = create_nodes(3);
- let test_msg = TestCustomMessage::Response;
+ let test_msg = TestCustomMessage::Pong;
let path = OnionMessagePath {
intermediate_nodes: vec![nodes[1].node_id],
};
nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap();
- nodes[2].custom_message_handler.expect_message(TestCustomMessage::Response);
+ nodes[2].custom_message_handler.expect_message(TestCustomMessage::Pong);
pass_along_path(&nodes);
}
#[test]
fn one_blinded_hop() {
let nodes = create_nodes(2);
- let test_msg = TestCustomMessage::Response;
+ let test_msg = TestCustomMessage::Pong;
let secp_ctx = Secp256k1::new();
let blinded_path = BlindedPath::new_for_message(&[], nodes[1].node_id, &*nodes[1].entropy_source, &secp_ctx).unwrap();
let destination = Destination::BlindedPath(blinded_path);
nodes[0].messenger.send_onion_message(test_msg, destination, None).unwrap();
- nodes[1].custom_message_handler.expect_message(TestCustomMessage::Response);
+ nodes[1].custom_message_handler.expect_message(TestCustomMessage::Pong);
pass_along_path(&nodes);
}
#[test]
fn two_unblinded_two_blinded() {
let nodes = create_nodes(5);
- let test_msg = TestCustomMessage::Response;
+ let test_msg = TestCustomMessage::Pong;
let secp_ctx = Secp256k1::new();
let intermediate_nodes = [ForwardNode { node_id: nodes[3].node_id, short_channel_id: None }];
};
nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap();
- nodes[4].custom_message_handler.expect_message(TestCustomMessage::Response);
+ nodes[4].custom_message_handler.expect_message(TestCustomMessage::Pong);
pass_along_path(&nodes);
}
#[test]
fn three_blinded_hops() {
let nodes = create_nodes(4);
- let test_msg = TestCustomMessage::Response;
+ let test_msg = TestCustomMessage::Pong;
let secp_ctx = Secp256k1::new();
let intermediate_nodes = [
let destination = Destination::BlindedPath(blinded_path);
nodes[0].messenger.send_onion_message(test_msg, destination, None).unwrap();
- nodes[3].custom_message_handler.expect_message(TestCustomMessage::Response);
+ nodes[3].custom_message_handler.expect_message(TestCustomMessage::Pong);
pass_along_path(&nodes);
}
let bob = &nodes[1];
// 2. Define the message sent from Bob to Alice.
- let message = TestCustomMessage::Request;
+ let message = TestCustomMessage::Ping;
let path_id = Some([2; 32]);
// 3. Simulate the creation of a Blinded Reply path provided by Bob.
Ok(Some(SendSuccess::Buffered)),
);
- bob.custom_message_handler.expect_message(TestCustomMessage::Response);
+ bob.custom_message_handler.expect_message(TestCustomMessage::Pong);
pass_along_path(&nodes);
}
fn too_big_packet_error() {
// Make sure we error as expected if a packet is too big to send.
let nodes = create_nodes(2);
- let test_msg = TestCustomMessage::Response;
+ let test_msg = TestCustomMessage::Pong;
let hop_node_id = nodes[1].node_id;
let hops = vec![hop_node_id; 400];
// If we are sending straight to a blinded path and we are the introduction node, we need to
// advance the blinded path by 1 hop so the second hop is the new introduction node.
let mut nodes = create_nodes(3);
- let test_msg = TestCustomMessage::Response;
+ let test_msg = TestCustomMessage::Pong;
let secp_ctx = Secp256k1::new();
let intermediate_nodes = [
let destination = Destination::BlindedPath(blinded_path);
nodes[0].messenger.send_onion_message(test_msg.clone(), destination, None).unwrap();
- nodes[2].custom_message_handler.expect_message(TestCustomMessage::Response);
+ nodes[2].custom_message_handler.expect_message(TestCustomMessage::Pong);
pass_along_path(&nodes);
// Try with a two-hop blinded path where we are the introduction node.
let blinded_path = BlindedPath::new_for_message(&intermediate_nodes, nodes[1].node_id, &*nodes[1].entropy_source, &secp_ctx).unwrap();
let destination = Destination::BlindedPath(blinded_path);
nodes[0].messenger.send_onion_message(test_msg, destination, None).unwrap();
- nodes[1].custom_message_handler.expect_message(TestCustomMessage::Response);
+ nodes[1].custom_message_handler.expect_message(TestCustomMessage::Pong);
nodes.remove(2);
pass_along_path(&nodes);
}
fn invalid_blinded_path_error() {
// Make sure we error as expected if a provided blinded path has 0 hops.
let nodes = create_nodes(3);
- let test_msg = TestCustomMessage::Response;
+ let test_msg = TestCustomMessage::Pong;
let secp_ctx = Secp256k1::new();
let intermediate_nodes = [ForwardNode { node_id: nodes[1].node_id, short_channel_id: None }];
#[test]
fn reply_path() {
let mut nodes = create_nodes(4);
- let test_msg = TestCustomMessage::Request;
+ let test_msg = TestCustomMessage::Ping;
let secp_ctx = Secp256k1::new();
// Destination::Node
];
let reply_path = BlindedPath::new_for_message(&intermediate_nodes, nodes[0].node_id, &*nodes[0].entropy_source, &secp_ctx).unwrap();
nodes[0].messenger.send_onion_message_using_path(path, test_msg.clone(), Some(reply_path)).unwrap();
- nodes[3].custom_message_handler.expect_message(TestCustomMessage::Request);
+ nodes[3].custom_message_handler.expect_message(TestCustomMessage::Ping);
pass_along_path(&nodes);
// Make sure the last node successfully decoded the reply path.
- nodes[0].custom_message_handler.expect_message(TestCustomMessage::Response);
+ nodes[0].custom_message_handler.expect_message(TestCustomMessage::Pong);
nodes.reverse();
pass_along_path(&nodes);
let reply_path = BlindedPath::new_for_message(&intermediate_nodes, nodes[0].node_id, &*nodes[0].entropy_source, &secp_ctx).unwrap();
nodes[0].messenger.send_onion_message(test_msg, destination, Some(reply_path)).unwrap();
- nodes[3].custom_message_handler.expect_message(TestCustomMessage::Request);
+ nodes[3].custom_message_handler.expect_message(TestCustomMessage::Ping);
pass_along_path(&nodes);
// Make sure the last node successfully decoded the reply path.
- nodes[0].custom_message_handler.expect_message(TestCustomMessage::Response);
+ nodes[0].custom_message_handler.expect_message(TestCustomMessage::Pong);
nodes.reverse();
pass_along_path(&nodes);
}
#[test]
fn peer_buffer_full() {
let nodes = create_nodes(2);
- let test_msg = TestCustomMessage::Request;
+ let test_msg = TestCustomMessage::Ping;
let destination = Destination::Node(nodes[1].node_id);
for _ in 0..188 { // Based on MAX_PER_PEER_BUFFER_SIZE in OnionMessenger
nodes[0].messenger.send_onion_message(test_msg.clone(), destination.clone(), None).unwrap();
// of size [`crate::onion_message::packet::BIG_PACKET_HOP_DATA_LEN`].
let num_nodes: usize = 25;
let nodes = create_nodes(num_nodes as u8);
- let test_msg = TestCustomMessage::Response;
+ let test_msg = TestCustomMessage::Pong;
let mut intermediate_nodes = vec![];
for i in 1..(num_nodes-1) {
first_node_addresses: None,
};
nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap();
- nodes[num_nodes-1].custom_message_handler.expect_message(TestCustomMessage::Response);
+ nodes[num_nodes-1].custom_message_handler.expect_message(TestCustomMessage::Pong);
pass_along_path(&nodes);
}
#[test]
fn requests_peer_connection_for_buffered_messages() {
let nodes = create_nodes(3);
- let message = TestCustomMessage::Request;
+ let message = TestCustomMessage::Ping;
let secp_ctx = Secp256k1::new();
add_channel_to_graph(&nodes[0], &nodes[1], &secp_ctx, 42);
#[test]
fn drops_buffered_messages_waiting_for_peer_connection() {
let nodes = create_nodes(3);
- let message = TestCustomMessage::Request;
+ let message = TestCustomMessage::Ping;
let secp_ctx = Secp256k1::new();
add_channel_to_graph(&nodes[0], &nodes[1], &secp_ctx, 42);
}
}
- let message = TestCustomMessage::Response;
+ let message = TestCustomMessage::Pong;
let secp_ctx = Secp256k1::new();
let intermediate_nodes = [ForwardNode { node_id: nodes[1].node_id, short_channel_id: None }];
let blinded_path = BlindedPath::new_for_message(
}
nodes[1].messenger.forward_onion_message(onion_message, &final_node_vec[0].node_id).unwrap();
- final_node_vec[0].custom_message_handler.expect_message(TestCustomMessage::Response);
+ final_node_vec[0].custom_message_handler.expect_message(TestCustomMessage::Pong);
pass_along_path(&vec![nodes.remove(1), final_node_vec.remove(0)]);
}