Skip to content

Commit

Permalink
Refactor initiator abbreviation
Browse files Browse the repository at this point in the history
  • Loading branch information
emhane committed Jan 14, 2024
1 parent 25077d4 commit 41f0ef6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 34 deletions.
16 changes: 8 additions & 8 deletions src/handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,8 @@ impl Handler {
HandlerIn::WhoAreYou(wru_ref, enr) => self.send_challenge::<P>(wru_ref, enr).await,
HandlerIn::HolePunchEnr(tgt_enr, relay_init) => {
// Assemble the notification for the target
let (initr_enr, _tgt, timed_out_nonce) = relay_init.into();
let relay_msg_notif = RelayMsgNotification::new(initr_enr, timed_out_nonce);
let (inr_enr, _tgt, timed_out_nonce) = relay_init.into();
let relay_msg_notif = RelayMsgNotification::new(inr_enr, timed_out_nonce);
if let Err(e) = self.send_relay_msg_notif::<P>(tgt_enr, relay_msg_notif).await {
warn!("Failed to relay. Error: {}", e);
}
Expand Down Expand Up @@ -1094,9 +1094,9 @@ impl Handler {
match message {
Message::Response(response) => self.handle_response::<P>(node_address, response).await,
Message::RelayInitNotification(notif) => {
let initr_node_id = notif.initiator_enr().node_id();
if initr_node_id != node_address.node_id {
warn!("peer {node_address} tried to initiate hole punch attempt for another node {initr_node_id}, banning peer {node_address}");
let inr_node_id = notif.initiator_enr().node_id();
if inr_node_id != node_address.node_id {
warn!("peer {node_address} tried to initiate hole punch attempt for another node {inr_node_id}, banning peer {node_address}");
self.fail_session(&node_address, RequestError::MaliciousRelayInit, true)
.await;
let ban_timeout = self
Expand All @@ -1111,7 +1111,7 @@ impl Handler {
Message::RelayMsgNotification(notif) => {
match self.nat_hole_puncher.is_behind_nat {
Some(false) => {
// initr may not be malicious and initiated a hole punch attempt when
// inr may not be malicious and initiated a hole punch attempt when
// a request to this node timed out for another reason
debug!("peer {node_address} relayed a hole punch notification but we are not behind nat");
}
Expand Down Expand Up @@ -1590,9 +1590,9 @@ impl HolePunchNat for Handler {
&mut self,
relay_msg: RelayMsgNotification,
) -> Result<(), NatHolePunchError> {
let (initr_enr, timed_out_msg_nonce) = relay_msg.into();
let (inr_enr, timed_out_msg_nonce) = relay_msg.into();
let initiator_node_address =
match NodeContact::try_from_enr(initr_enr, self.nat_hole_puncher.ip_mode) {
match NodeContact::try_from_enr(inr_enr, self.nat_hole_puncher.ip_mode) {
Ok(contact) => contact.node_address(),
Err(e) => return Err(NatHolePunchError::Target(e.into())),
};
Expand Down
52 changes: 26 additions & 26 deletions src/handler/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,24 +497,24 @@ async fn nat_hole_punch_relay() {
let mut dummy_session = build_dummy_session();

// Initiator
let initr_enr = {
let inr_enr = {
let key = CombinedKey::generate_secp256k1();
EnrBuilder::new("v4")
.ip4(Ipv4Addr::LOCALHOST)
.udp4(9011)
.build(&key)
.unwrap()
};
let initr_addr = initr_enr.udp4_socket().unwrap().into();
let initr_node_id = initr_enr.node_id();
let inr_addr = inr_enr.udp4_socket().unwrap().into();
let inr_node_id = inr_enr.node_id();

let initr_node_address = NodeAddress::new(initr_addr, initr_enr.node_id());
let inr_node_address = NodeAddress::new(inr_addr, inr_enr.node_id());
handler
.sessions
.cache
.insert(initr_node_address, dummy_session.clone());
.insert(inr_node_address, dummy_session.clone());

let initr_socket = UdpSocket::bind(initr_addr)
let inr_socket = UdpSocket::bind(inr_addr)
.await
.expect("should bind to initiator socket");

Expand Down Expand Up @@ -559,16 +559,16 @@ async fn nat_hole_punch_relay() {

// Initiator handle
let relay_init_notif =
RelayInitNotification::new(initr_enr.clone(), tgt_node_id, MessageNonce::default());
RelayInitNotification::new(inr_enr.clone(), tgt_node_id, MessageNonce::default());

let initr_handle = tokio::spawn(async move {
let inr_handle = tokio::spawn(async move {
let mut session = build_dummy_session();
let packet = session
.encrypt_session_message::<DefaultProtocolId>(initr_node_id, &relay_init_notif.encode())
.encrypt_session_message::<DefaultProtocolId>(inr_node_id, &relay_init_notif.encode())
.expect("should encrypt notification");
let encoded_packet = packet.encode::<DefaultProtocolId>(&relay_node_id);

initr_socket
inr_socket
.send_to(&encoded_packet, relay_addr)
.await
.expect("should relay init notification to relay")
Expand All @@ -589,10 +589,10 @@ async fn nat_hole_punch_relay() {
});

// Join all handles
let (initr_res, relay_res, tgt_res, mock_service_res) =
tokio::join!(initr_handle, relay_handle, tgt_handle, mock_service_handle);
let (inr_res, relay_res, tgt_res, mock_service_res) =
tokio::join!(inr_handle, relay_handle, tgt_handle, mock_service_handle);

initr_res.unwrap();
inr_res.unwrap();
relay_res.unwrap();
mock_service_res.unwrap();

Expand Down Expand Up @@ -624,7 +624,7 @@ async fn nat_hole_punch_relay() {
match Message::decode(&decrypted_message).expect("should decode message") {
Message::RelayMsgNotification(relay_msg) => {
let (enr, _) = relay_msg.into();
assert_eq!(initr_enr, enr)
assert_eq!(inr_enr, enr)
}
_ => panic!("message should decode to a relay msg notification"),
}
Expand Down Expand Up @@ -666,27 +666,27 @@ async fn nat_hole_punch_target() {
.expect("should bind to target socket");

// Initiator
let initr_enr = {
let inr_enr = {
let key = CombinedKey::generate_secp256k1();
EnrBuilder::new("v4")
.ip4(Ipv4Addr::LOCALHOST)
.udp4(9021)
.build(&key)
.unwrap()
};
let initr_addr = initr_enr.udp4_socket().unwrap();
let initr_node_id = initr_enr.node_id();
let initr_nonce: MessageNonce = [1; MESSAGE_NONCE_LENGTH];
let inr_addr = inr_enr.udp4_socket().unwrap();
let inr_node_id = inr_enr.node_id();
let inr_nonce: MessageNonce = [1; MESSAGE_NONCE_LENGTH];

let initr_socket = UdpSocket::bind(initr_addr)
let inr_socket = UdpSocket::bind(inr_addr)
.await
.expect("should bind to initiator socket");

// Target handle
let tgt_handle = tokio::spawn(async move { handler.start::<DefaultProtocolId>().await });

// Relay handle
let relay_msg_notif = RelayMsgNotification::new(initr_enr.clone(), initr_nonce);
let relay_msg_notif = RelayMsgNotification::new(inr_enr.clone(), inr_nonce);

let relay_handle = tokio::spawn(async move {
let mut session = build_dummy_session();
Expand All @@ -703,9 +703,9 @@ async fn nat_hole_punch_target() {

// Initiator handle
let target_exit = mock_service.exit_tx;
let initr_handle = tokio::spawn(async move {
let inr_handle = tokio::spawn(async move {
let mut buffer = [0; MAX_PACKET_SIZE];
let res = initr_socket
let res = inr_socket
.recv_from(&mut buffer)
.await
.expect("should read bytes from socket");
Expand All @@ -716,16 +716,16 @@ async fn nat_hole_punch_target() {
});

// Join all handles
let (tgt_res, relay_res, initr_res) = tokio::join!(tgt_handle, relay_handle, initr_handle);
let (tgt_res, relay_res, inr_res) = tokio::join!(tgt_handle, relay_handle, inr_handle);

tgt_res.unwrap();
relay_res.unwrap();

let ((length, src), buffer) = initr_res.unwrap();
let ((length, src), buffer) = inr_res.unwrap();

assert_eq!(src, tgt_addr);

let (packet, _aad) = Packet::decode::<DefaultProtocolId>(&initr_node_id, &buffer[..length])
let (packet, _aad) = Packet::decode::<DefaultProtocolId>(&inr_node_id, &buffer[..length])
.expect("should decode packet");
let Packet { header, .. } = packet;
let PacketHeader {
Expand All @@ -735,5 +735,5 @@ async fn nat_hole_punch_target() {
} = header;

assert!(kind.is_whoareyou());
assert_eq!(message_nonce, initr_nonce)
assert_eq!(message_nonce, inr_nonce)
}

0 comments on commit 41f0ef6

Please sign in to comment.