Skip to content

Commit a338289

Browse files
refactor: unify hex encoding with data-encoding (#3047)
## Description We already use `data-encoding` for `base32` and `hex` encoding, this replaces our usage of the `hex` crate with this. ## Breaking Changes <!-- Optional, if there are any breaking changes document them, including how to migrate older code. --> ## Notes & open questions <!-- Any notes, remarks or open questions you have to make about the PR. --> ## Change checklist - [x] Self-review. - [x] Documentation updates following the [style guide](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text), if relevant. - [x] Tests if relevant. - [x] All breaking changes documented.
1 parent 218aad3 commit a338289

File tree

8 files changed

+37
-30
lines changed

8 files changed

+37
-30
lines changed

Cargo.lock

+1-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

iroh-relay/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ futures-lite = "2.5"
3030
futures-sink = "0.3"
3131
futures-util = "0.3"
3232
governor = "0.7.0"
33-
hex = "0.4.3"
3433
hickory-proto = { version = "=0.25.0-alpha.4" }
3534
hickory-resolver = "=0.25.0-alpha.4"
3635
hostname = "0.4"
@@ -96,6 +95,7 @@ tracing-subscriber = { version = "0.3", features = [
9695
url = { version = "2.5", features = ["serde"] }
9796
webpki = { package = "rustls-webpki", version = "0.102" }
9897
webpki-roots = "0.26"
98+
data-encoding = "2.6.0"
9999

100100
[dev-dependencies]
101101
clap = { version = "4", features = ["derive"] }

iroh-relay/src/client.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,11 @@ impl PingTracker {
179179
///
180180
/// If there is no [`oneshot::Sender`] in the tracker, return `None`.
181181
fn unregister(&mut self, data: [u8; 8], why: &'static str) -> Option<oneshot::Sender<()>> {
182-
trace!("removing ping {}: {}", hex::encode(data), why);
182+
trace!(
183+
"removing ping {}: {}",
184+
data_encoding::HEXLOWER.encode(&data),
185+
why
186+
);
183187
self.0.remove(&data)
184188
}
185189
}
@@ -759,7 +763,7 @@ impl Actor {
759763
async fn ping(&mut self, s: oneshot::Sender<Result<Duration, ClientError>>) {
760764
let connect_res = self.connect("ping").await.map(|(c, _)| c);
761765
let (ping, recv) = self.pings.register();
762-
trace!("ping: {}", hex::encode(ping));
766+
trace!("ping: {}", data_encoding::HEXLOWER.encode(&ping));
763767

764768
self.ping_tasks.spawn(async move {
765769
let res = match connect_res {

iroh-relay/src/protos/stun.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ mod tests {
313313
},
314314
ResponseTestCase {
315315
name: "no-4in6",
316-
data: hex::decode("010100182112a4424fd5d202dcb37d31fc773306002000140002cd3d2112a4424fd5d202dcb382ce2dc3fcc7").unwrap(),
316+
data: data_encoding::HEXLOWER.decode(b"010100182112a4424fd5d202dcb37d31fc773306002000140002cd3d2112a4424fd5d202dcb382ce2dc3fcc7").unwrap(),
317317
want_tid: vec![79, 213, 210, 2, 220, 179, 125, 49, 252, 119, 51, 6],
318318
want_addr: IpAddr::V4(Ipv4Addr::from([209, 180, 207, 193])),
319319
want_port: 60463,

iroh/Cargo.toml

+2-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ axum = { version = "0.7", optional = true }
2222
backoff = "0.4.0"
2323
base64 = "0.22.1"
2424
bytes = "1.7"
25+
data-encoding = "2.2"
2526
der = { version = "0.7", features = ["alloc", "derive"] }
2627
derive_more = { version = "1.0.0", features = [
2728
"debug",
@@ -36,7 +37,6 @@ futures-lite = "2.5"
3637
futures-sink = "0.3"
3738
futures-util = "0.3"
3839
governor = "0.7.0"
39-
hex = "0.4.3"
4040
hickory-resolver = { version = "=0.25.0-alpha.4" }
4141
hostname = "0.4"
4242
http = "1"
@@ -112,7 +112,6 @@ net-report = { package = "iroh-net-report", path = "../iroh-net-report", version
112112
iroh-metrics = { version = "0.29", default-features = false }
113113

114114
# local-swarm-discovery
115-
data-encoding = { version = "2.2", optional = true }
116115
swarm-discovery = { version = "0.3.0-alpha.1", optional = true }
117116

118117
# dht_discovery
@@ -178,7 +177,7 @@ harness = false
178177
default = ["metrics", "discovery-pkarr-dht"]
179178
metrics = ["iroh-metrics/metrics", "iroh-relay/metrics", "net-report/metrics", "portmapper/metrics"]
180179
test-utils = ["iroh-relay/test-utils", "iroh-relay/server", "dep:axum"]
181-
discovery-local-network = ["dep:data-encoding", "dep:swarm-discovery"]
180+
discovery-local-network = ["dep:swarm-discovery"]
182181
discovery-pkarr-dht = ["pkarr/dht", "dep:genawaiter"]
183182
examples = [
184183
"dep:clap",

iroh/src/disco.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use std::{
2424
};
2525

2626
use anyhow::{anyhow, bail, ensure, Context, Result};
27+
use data_encoding::HEXLOWER;
2728
use iroh_base::{PublicKey, RelayUrl};
2829
use serde::{Deserialize, Serialize};
2930
use url::Url;
@@ -383,10 +384,10 @@ impl Display for Message {
383384
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
384385
match self {
385386
Message::Ping(ping) => {
386-
write!(f, "Ping(tx={})", hex::encode(ping.tx_id))
387+
write!(f, "Ping(tx={})", HEXLOWER.encode(&ping.tx_id))
387388
}
388389
Message::Pong(pong) => {
389-
write!(f, "Pong(tx={})", hex::encode(pong.tx_id))
390+
write!(f, "Pong(tx={})", HEXLOWER.encode(&pong.tx_id))
390391
}
391392
Message::CallMeMaybe(_) => {
392393
write!(f, "CallMeMaybe")
@@ -460,7 +461,9 @@ mod tests {
460461
let got = test.m.as_bytes();
461462
assert_eq!(
462463
got,
463-
hex::decode(test.want.replace(' ', "")).unwrap(),
464+
data_encoding::HEXLOWER
465+
.decode(test.want.replace(' ', "").as_bytes())
466+
.unwrap(),
464467
"wrong as_bytes"
465468
);
466469

iroh/src/magicsock.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use std::{
3232
use anyhow::{anyhow, Context as _, Result};
3333
use bytes::Bytes;
3434
use concurrent_queue::ConcurrentQueue;
35+
use data_encoding::HEXLOWER;
3536
use futures_lite::{FutureExt, StreamExt};
3637
use futures_util::{stream::BoxStream, task::AtomicWaker};
3738
use iroh_base::{NodeAddr, NodeId, PublicKey, RelayUrl, SecretKey, SharedSecret};
@@ -1070,20 +1071,20 @@ impl MagicSock {
10701071
let handled = self.node_map.handle_ping(sender, addr.clone(), dm.tx_id);
10711072
match handled.role {
10721073
PingRole::Duplicate => {
1073-
debug!(%src, tx = %hex::encode(dm.tx_id), "received ping: path already confirmed, skip");
1074+
debug!(%src, tx = %HEXLOWER.encode(&dm.tx_id), "received ping: path already confirmed, skip");
10741075
return;
10751076
}
10761077
PingRole::LikelyHeartbeat => {}
10771078
PingRole::NewPath => {
1078-
debug!(%src, tx = %hex::encode(dm.tx_id), "received ping: new path");
1079+
debug!(%src, tx = %HEXLOWER.encode(&dm.tx_id), "received ping: new path");
10791080
}
10801081
PingRole::Activate => {
1081-
debug!(%src, tx = %hex::encode(dm.tx_id), "received ping: path active");
1082+
debug!(%src, tx = %HEXLOWER.encode(&dm.tx_id), "received ping: path active");
10821083
}
10831084
}
10841085

10851086
// Send a pong.
1086-
debug!(tx = %hex::encode(dm.tx_id), %addr, dstkey = %sender.fmt_short(),
1087+
debug!(tx = %HEXLOWER.encode(&dm.tx_id), %addr, dstkey = %sender.fmt_short(),
10871088
"sending pong");
10881089
let pong = disco::Message::Pong(disco::Pong {
10891090
tx_id: dm.tx_id,
@@ -1137,11 +1138,11 @@ impl MagicSock {
11371138
};
11381139
if sent {
11391140
let msg_sender = self.actor_sender.clone();
1140-
trace!(%dst, tx = %hex::encode(tx_id), ?purpose, "ping sent (queued)");
1141+
trace!(%dst, tx = %HEXLOWER.encode(&tx_id), ?purpose, "ping sent (queued)");
11411142
self.node_map
11421143
.notify_ping_sent(id, dst, tx_id, purpose, msg_sender);
11431144
} else {
1144-
warn!(dst = ?dst, tx = %hex::encode(tx_id), ?purpose, "failed to send ping: queues full");
1145+
warn!(dst = ?dst, tx = %HEXLOWER.encode(&tx_id), ?purpose, "failed to send ping: queues full");
11451146
}
11461147
}
11471148

@@ -1321,7 +1322,7 @@ impl MagicSock {
13211322
node_key: self.public_key(),
13221323
});
13231324
self.try_send_disco_message(dst.clone(), dst_node, msg)?;
1324-
debug!(%dst, tx = %hex::encode(tx_id), ?purpose, "ping sent (polled)");
1325+
debug!(%dst, tx = %HEXLOWER.encode(&tx_id), ?purpose, "ping sent (polled)");
13251326
let msg_sender = self.actor_sender.clone();
13261327
self.node_map
13271328
.notify_ping_sent(id, dst.clone(), tx_id, purpose, msg_sender);
@@ -3059,8 +3060,8 @@ mod tests {
30593060
val,
30603061
msg,
30613062
"[sender] expected {}, got {}",
3062-
hex::encode(msg),
3063-
hex::encode(&val)
3063+
HEXLOWER.encode(msg),
3064+
HEXLOWER.encode(&val)
30643065
);
30653066

30663067
let stats = conn.stats();
@@ -3465,8 +3466,8 @@ mod tests {
34653466
anyhow::ensure!(
34663467
val == $msg,
34673468
"expected {}, got {}",
3468-
hex::encode($msg),
3469-
hex::encode(val)
3469+
HEXLOWER.encode(&$msg[..]),
3470+
HEXLOWER.encode(&val)
34703471
);
34713472
};
34723473
}
@@ -3618,8 +3619,8 @@ mod tests {
36183619
anyhow::ensure!(
36193620
val == $msg,
36203621
"expected {}, got {}",
3621-
hex::encode($msg),
3622-
hex::encode(val)
3622+
HEXLOWER.encode(&$msg[..]),
3623+
HEXLOWER.encode(&val)
36233624
);
36243625
};
36253626
}

iroh/src/magicsock/node_map/node_state.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::{
55
time::{Duration, Instant},
66
};
77

8+
use data_encoding::HEXLOWER;
89
use iroh_base::{NodeAddr, NodeId, PublicKey, RelayUrl};
910
use iroh_metrics::inc;
1011
use iroh_relay::protos::stun;
@@ -410,7 +411,7 @@ impl NodeState {
410411
#[instrument("disco", skip_all, fields(node = %self.node_id.fmt_short()))]
411412
pub(super) fn ping_timeout(&mut self, txid: stun::TransactionId) {
412413
if let Some(sp) = self.sent_pings.remove(&txid) {
413-
debug!(tx = %hex::encode(txid), addr = %sp.to, "pong not received in timeout");
414+
debug!(tx = %HEXLOWER.encode(&txid), addr = %sp.to, "pong not received in timeout");
414415
match sp.to {
415416
SendAddr::Udp(addr) => {
416417
if let Some(path_state) = self.udp_paths.paths.get_mut(&addr.into()) {
@@ -461,7 +462,7 @@ impl NodeState {
461462
return None;
462463
}
463464
let tx_id = stun::TransactionId::default();
464-
trace!(tx = %hex::encode(tx_id), %dst, ?purpose,
465+
trace!(tx = %HEXLOWER.encode(&tx_id), %dst, ?purpose,
465466
dst = %self.node_id.fmt_short(), "start ping");
466467
event!(
467468
target: "iroh::_events::ping::sent",
@@ -488,7 +489,7 @@ impl NodeState {
488489
purpose: DiscoPingPurpose,
489490
sender: mpsc::Sender<ActorMessage>,
490491
) {
491-
trace!(%to, tx = %hex::encode(tx_id), ?purpose, "record ping sent");
492+
trace!(%to, tx = %HEXLOWER.encode(&tx_id), ?purpose, "record ping sent");
492493

493494
let now = Instant::now();
494495
let mut path_found = false;
@@ -872,7 +873,7 @@ impl NodeState {
872873
match self.sent_pings.remove(&m.tx_id) {
873874
None => {
874875
// This is not a pong for a ping we sent.
875-
warn!(tx = %hex::encode(m.tx_id), "received pong with unknown transaction id");
876+
warn!(tx = %HEXLOWER.encode(&m.tx_id), "received pong with unknown transaction id");
876877
None
877878
}
878879
Some(sp) => {
@@ -884,7 +885,7 @@ impl NodeState {
884885
let latency = now - sp.at;
885886

886887
debug!(
887-
tx = %hex::encode(m.tx_id),
888+
tx = %HEXLOWER.encode(&m.tx_id),
888889
src = %src,
889890
reported_ping_src = %m.ping_observed_addr,
890891
ping_dst = %sp.to,

0 commit comments

Comments
 (0)