Skip to content

Commit 7cdaba7

Browse files
Get rid of BigUInt
1 parent 965711d commit 7cdaba7

File tree

4 files changed

+36
-27
lines changed

4 files changed

+36
-27
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ path = "src/parser_tester.rs"
2020
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
2121

2222
[dependencies]
23-
num = { version = "^0.4", features = ["alloc"] }
23+
num = { version = "^0.4" }
2424
bendy = { version = "^0.3" }
2525
juicy_bencode = "^0.1"
2626
rand = { version = "^0.8", features = ["small_rng"] }

src/dht_service.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ mod tests {
193193
dht_service::DhtV4,
194194
domain_knowledge::{InfoHash, NodeId},
195195
};
196-
use num::{BigUint, Num};
197196
use opentelemetry::global;
198197
use rand::RngCore;
199198
use std::{net::SocketAddrV4, str::FromStr, sync::Once};
@@ -291,13 +290,7 @@ mod tests {
291290
)
292291
.await?;
293292

294-
let info_hash = BigUint::from_str_radix("233b78ca585fe0a8c9e8eb4bda03f52e8b6f554b", 16).unwrap();
295-
let info_hash = info_hash.to_bytes_be();
296-
297-
let mut stupid = [0u8; 20];
298-
stupid.copy_from_slice(&info_hash[0..20]);
299-
300-
let info_hash = InfoHash(stupid);
293+
let info_hash = InfoHash::from_hex_str("233b78ca585fe0a8c9e8eb4bda03f52e8b6f554b");
301294

302295
let client = dht.client();
303296
let (token, peers) = client.get_peers(info_hash).await?;

src/dht_service/dht_client.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ use crate::{
55
our_error::{naur, OurError},
66
utils::ParSpawnAndAwait,
77
};
8-
use num::BigUint;
9-
use std::{collections::HashSet, net::SocketAddrV4, ops::BitXor, sync::Arc, time::Duration};
8+
use std::{collections::HashSet, net::SocketAddrV4, sync::Arc, time::Duration};
109
use tokio::time::timeout;
1110
use tracing::warn;
1211
use tracing::{instrument, trace};
@@ -118,10 +117,7 @@ impl DhtHandle {
118117
let mut sorted_by_distance: Vec<_> = returned_nodes
119118
.into_iter()
120119
.map(|node| {
121-
let node_id = BigUint::from_bytes_be(node.id().as_bytes());
122-
let our_id = BigUint::from_bytes_be(self.our_id.as_bytes());
123-
let distance = our_id.bitxor(node_id);
124-
120+
let distance = node.id().dist(&self.our_id);
125121
(node, distance)
126122
})
127123
.collect();

src/domain_knowledge.rs

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const NODE_ID_LEN: usize = 20;
77
pub const ZERO_DIST: [u8; NODE_ID_LEN] = [0; NODE_ID_LEN];
88

99
#[derive(PartialEq, Eq, Hash, Clone, Copy, PartialOrd, Ord)]
10-
pub struct NodeId(pub [u8; 20]);
10+
pub struct NodeId(pub [u8; NODE_ID_LEN]);
1111

1212
impl Debug for NodeId {
1313
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@@ -19,13 +19,13 @@ impl Debug for NodeId {
1919
}
2020

2121
impl NodeId {
22-
/// Panics if the length is not exactly 20
22+
/// Panics if the length is not exactly NODE_ID_LEN
2323
pub fn from_bytes_unchecked(bytes: &[u8]) -> Self {
24-
if bytes.len() != 20 {
25-
panic!("Node id must be exactly 20 bytes");
24+
if bytes.len() != NODE_ID_LEN {
25+
panic!("Node id must be exactly {NODE_ID_LEN} bytes");
2626
}
2727

28-
let mut arr = [0u8; 20];
28+
let mut arr = [0u8; NODE_ID_LEN];
2929
arr.copy_from_slice(bytes);
3030
NodeId(arr)
3131
}
@@ -40,9 +40,9 @@ impl NodeId {
4040
our_id ^ node_id
4141
}
4242

43-
pub fn dist(&self, rhs: &Self) -> [u8; 20] {
44-
let mut dist = [0u8; 20];
45-
for i in 0..20 {
43+
pub fn dist(&self, rhs: &Self) -> [u8; NODE_ID_LEN] {
44+
let mut dist = [0u8; NODE_ID_LEN];
45+
for i in 0..NODE_ID_LEN {
4646
dist[i] = self.0[i] ^ rhs.0[i]
4747
}
4848
dist
@@ -77,22 +77,42 @@ impl ToBencode for NodeId {
7777
// }
7878

7979
#[derive(PartialEq, Eq, Hash, Clone, Copy)]
80-
pub struct InfoHash(pub [u8; 20]);
80+
pub struct InfoHash(pub [u8; NODE_ID_LEN]);
8181

8282
impl InfoHash {
83+
/// Panics if `bytes` is not 20 bytes in length
8384
pub fn from_bytes_unchecked(bytes: &[u8]) -> Self {
84-
if bytes.len() != 20 {
85-
panic!("Info hash must be exactly 20 bytes");
85+
if bytes.len() != NODE_ID_LEN {
86+
panic!("Info hash must be exactly {NODE_ID_LEN} bytes");
8687
}
8788

88-
let mut arr = [0u8; 20];
89+
let mut arr = [0u8; NODE_ID_LEN];
8990
arr.copy_from_slice(bytes);
9091
InfoHash(arr)
9192
}
9293

9394
pub fn as_bytes(&self) -> &[u8] {
9495
&self.0
9596
}
97+
98+
pub fn from_hex_str(str: &str) -> Self {
99+
if str.len() != NODE_ID_LEN * 2 {
100+
panic!("Info hash must be exactly {NODE_ID_LEN} bytes");
101+
}
102+
if !str.is_ascii() {
103+
panic!("input has non ascii characters");
104+
}
105+
106+
let mut arr = [0u8; NODE_ID_LEN];
107+
let mut i = 0;
108+
while i != str.len() {
109+
let bytes = &str[i..i + 2];
110+
arr[i >> 1] = u8::from_str_radix(bytes, 16).expect("input string must be consist of soley hex digits");
111+
i <<= 1;
112+
}
113+
114+
Self(arr)
115+
}
96116
}
97117

98118
impl Debug for InfoHash {

0 commit comments

Comments
 (0)