Skip to content

Commit

Permalink
refactor(iroh-net): Clean up peer_map, node_map and endpoint names (#…
Browse files Browse the repository at this point in the history
…2060)

## Description

This cleans up a bunch of naming:

- It documents how we decided to use NodeId in #1388.

- It fixes up a few more of those PublicKey uses to use NodeId.

- It renames the peer_map module to  node_map, the primary data
  structure in there is the NodeMap as we migrated away from naming
  things peer a while ago as well.

- It tries to carve out the "Endpoint" naming for a synonym of a
  "Node".

- As part of that it uses "Path" as meaning "one way of connecting to
  a node".  Either via a direct IP address or via a derper.


## Notes & open questions

Contains no functional changes.

## Change checklist

- [x] Self-review.
- [x] Documentation updates if relevant.
  • Loading branch information
flub authored Mar 7, 2024
1 parent a42c1b2 commit 6578d2c
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 85 deletions.
10 changes: 10 additions & 0 deletions iroh-base/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ fn get_or_create_crypto_keys<T>(
#[derive(Clone, Copy, PartialEq, Eq, Ord, PartialOrd)]
pub struct PublicKey([u8; 32]);

/// The identifier for a node in the (iroh) network.
///
/// This is equivalent to [`PublicKey`]. By convention we will (or should) use `PublicKey`
/// as type name when performing cryptographic operations, but use `NodeId` when referencing
/// a node. E.g.:
///
/// - `encrypt(key: PublicKey)`
/// - `send_to(node: NodeId)`
pub type NodeId = PublicKey;

impl Hash for PublicKey {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.0.hash(state);
Expand Down
3 changes: 2 additions & 1 deletion iroh-net/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ pub mod util;
pub use magic_endpoint::{AddrInfo, MagicEndpoint, NodeAddr};

pub use iroh_base::key;
pub use iroh_base::key::PublicKey as NodeId;

pub use iroh_base::key::NodeId;

#[cfg(test)]
pub(crate) mod test_utils;
26 changes: 14 additions & 12 deletions iroh-net/src/magicsock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,27 @@ use crate::{
dns::DNS_RESOLVER,
key::{PublicKey, SecretKey, SharedSecret},
magic_endpoint::NodeAddr,
magicsock::peer_map::PingRole,
net::{ip::LocalAddresses, netmon, IpFamily},
netcheck, portmapper, stun, AddrInfo,
};

use self::{
derp_actor::{DerpActor, DerpActorMessage, DerpReadResult},
metrics::Metrics as MagicsockMetrics,
peer_map::{NodeMap, PingAction, SendPing},
node_map::{NodeMap, PingAction, PingRole, SendPing},
rebinding_conn::RebindingUdpConn,
};

mod derp_actor;
mod metrics;
mod peer_map;
mod node_map;
mod rebinding_conn;
mod timer;

pub use crate::net::UdpSocket;

pub use self::metrics::Metrics;
pub use self::peer_map::{ConnectionType, ControlMsg, DirectAddrInfo, EndpointInfo};
pub use self::node_map::{ConnectionType, ControlMsg, DirectAddrInfo, EndpointInfo};
pub use self::timer::Timer;

/// How long we consider a STUN-derived endpoint valid for. UDP NAT mappings typically
Expand Down Expand Up @@ -737,7 +736,7 @@ impl Inner {
let SendPing {
id,
dst,
dst_key,
dst_node,
tx_id,
purpose,
} = ping;
Expand All @@ -746,8 +745,11 @@ impl Inner {
node_key: self.public_key(),
});
let sent = match dst {
SendAddr::Udp(addr) => self.udp_disco_sender.try_send((addr, dst_key, msg)).is_ok(),
SendAddr::Derp(ref url) => self.send_disco_message_derp(url, dst_key, msg),
SendAddr::Udp(addr) => self
.udp_disco_sender
.try_send((addr, dst_node, msg))
.is_ok(),
SendAddr::Derp(ref url) => self.send_disco_message_derp(url, dst_node, msg),
};
if sent {
let msg_sender = self.actor_sender.clone();
Expand All @@ -763,15 +765,15 @@ impl Inner {
let SendPing {
id,
dst,
dst_key,
dst_node,
tx_id,
purpose,
} = ping;
let msg = disco::Message::Ping(disco::Ping {
tx_id: *tx_id,
node_key: self.public_key(),
});
ready!(self.poll_send_disco_message(dst.clone(), *dst_key, msg, cx))?;
ready!(self.poll_send_disco_message(dst.clone(), *dst_node, msg, cx))?;
let msg_sender = self.actor_sender.clone();
debug!(%dst, tx = %hex::encode(tx_id), ?purpose, "ping sent (polled)");
self.node_map
Expand Down Expand Up @@ -921,9 +923,9 @@ impl Inner {
match *msg {
PingAction::SendCallMeMaybe {
ref derp_url,
dst_key,
dst_node,
} => {
self.send_or_queue_call_me_maybe(derp_url, dst_key);
self.send_or_queue_call_me_maybe(derp_url, dst_node);
}
PingAction::SendPing(ref ping) => {
ready!(self.poll_send_ping(ping, cx))?;
Expand Down Expand Up @@ -2708,7 +2710,7 @@ pub(crate) mod tests {
.magic_sock()
.tracked_endpoints()
.into_iter()
.map(|ep| ep.public_key)
.map(|ep| ep.node_id)
.collect()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ const MAX_INACTIVE_NODES: usize = 30;

/// Map of the [`Endpoint`] information for all the known nodes.
///
/// Each endpoint is also known as a "Node" in the "(iroh) network", but this is a bit of a
/// looser term. It is where "NodeMap" comes from however.
///
///
/// The nodes can be looked up by:
///
/// - The node's ID in this map, only useful if you know the ID from an insert or lookup.
Expand Down
Loading

0 comments on commit 6578d2c

Please sign in to comment.