Skip to content

Commit f79a019

Browse files
committed
Merge torrust#961: dev: remove some internal types in preference for aquatics
bef5680 dev: vairous fixups (Cameron Garnham) 9362fa5 dev: use aquatic PeerId instead of local one (Cameron Garnham) 03e88d0 dev: use aquatic number_of_bytes (Cameron Garnham) 00af70f dev: remove announce event wrapper (Cameron Garnham) 325df70 dev: use aquatic_udp_protocol InfoHash inside our type (Cameron Garnham) 7779fa3 dev: remove announce_request wrapper (Cameron Garnham) Pull request description: Prefer the protocol types in https://github.com/greatest-ape/aquatic/tree/master - Removed `AnnounceEvent` for aquatic type. - `InfoHash` uses aquatic type internally. - Use aquatic `PeerId` and `peer::Id` uses aquatic type internally. ACKs for top commit: da2ce7: ACK bef5680 Tree-SHA512: 275c2f4261a4c84930dea7e0b5252d76f134a458e9e9a2b2c7d5fb615ac30798b59f454ef335f331ae8f0eb822d2b0b93ee2fa4737cc9131e02bb89ccc20e413
2 parents efc2a76 + bef5680 commit f79a019

File tree

40 files changed

+461
-549
lines changed

40 files changed

+461
-549
lines changed

Cargo.lock

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

docs/media/flamegraph_generated_without_sudo.svg

+1-1
Loading

packages/primitives/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ rust-version.workspace = true
1515
version.workspace = true
1616

1717
[dependencies]
18+
aquatic_udp_protocol = "0"
1819
binascii = "0"
1920
derive_more = "0"
2021
serde = { version = "1", features = ["derive"] }
2122
tdyne-peer-id = "1"
2223
tdyne-peer-id-registry = "0"
2324
thiserror = "1"
25+
zerocopy = "0"

packages/primitives/src/announce_event.rs

-43
This file was deleted.

packages/primitives/src/info_hash.rs

+51-15
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
use std::hash::{DefaultHasher, Hash, Hasher};
2+
use std::ops::{Deref, DerefMut};
23
use std::panic::Location;
34

45
use thiserror::Error;
6+
use zerocopy::FromBytes;
57

68
/// `BitTorrent` Info Hash v1
7-
#[derive(PartialEq, Eq, Hash, Clone, Copy, Default, Debug)]
8-
pub struct InfoHash(pub [u8; 20]);
9+
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
10+
pub struct InfoHash {
11+
data: aquatic_udp_protocol::InfoHash,
12+
}
913

1014
pub const INFO_HASH_BYTES_LEN: usize = 20;
1115

@@ -17,10 +21,9 @@ impl InfoHash {
1721
/// Will panic if byte slice does not contains the exact amount of bytes need for the `InfoHash`.
1822
#[must_use]
1923
pub fn from_bytes(bytes: &[u8]) -> Self {
20-
assert_eq!(bytes.len(), INFO_HASH_BYTES_LEN);
21-
let mut ret = Self([0u8; INFO_HASH_BYTES_LEN]);
22-
ret.0.clone_from_slice(bytes);
23-
ret
24+
let data = aquatic_udp_protocol::InfoHash::read_from(bytes).expect("it should have the exact amount of bytes");
25+
26+
Self { data }
2427
}
2528

2629
/// Returns the `InfoHash` internal byte array.
@@ -36,13 +39,41 @@ impl InfoHash {
3639
}
3740
}
3841

42+
impl Default for InfoHash {
43+
fn default() -> Self {
44+
Self {
45+
data: aquatic_udp_protocol::InfoHash(Default::default()),
46+
}
47+
}
48+
}
49+
50+
impl From<aquatic_udp_protocol::InfoHash> for InfoHash {
51+
fn from(data: aquatic_udp_protocol::InfoHash) -> Self {
52+
Self { data }
53+
}
54+
}
55+
56+
impl Deref for InfoHash {
57+
type Target = aquatic_udp_protocol::InfoHash;
58+
59+
fn deref(&self) -> &Self::Target {
60+
&self.data
61+
}
62+
}
63+
64+
impl DerefMut for InfoHash {
65+
fn deref_mut(&mut self) -> &mut Self::Target {
66+
&mut self.data
67+
}
68+
}
69+
3970
impl Ord for InfoHash {
4071
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
4172
self.0.cmp(&other.0)
4273
}
4374
}
4475

45-
impl std::cmp::PartialOrd<InfoHash> for InfoHash {
76+
impl PartialOrd<InfoHash> for InfoHash {
4677
fn partial_cmp(&self, other: &InfoHash) -> Option<std::cmp::Ordering> {
4778
Some(self.cmp(other))
4879
}
@@ -60,7 +91,7 @@ impl std::str::FromStr for InfoHash {
6091
type Err = binascii::ConvertError;
6192

6293
fn from_str(s: &str) -> Result<Self, Self::Err> {
63-
let mut i = Self([0u8; 20]);
94+
let mut i = Self::default();
6495
if s.len() != 40 {
6596
return Err(binascii::ConvertError::InvalidInputLength);
6697
}
@@ -72,7 +103,7 @@ impl std::str::FromStr for InfoHash {
72103
impl std::convert::From<&[u8]> for InfoHash {
73104
fn from(data: &[u8]) -> InfoHash {
74105
assert_eq!(data.len(), 20);
75-
let mut ret = InfoHash([0u8; 20]);
106+
let mut ret = Self::default();
76107
ret.0.clone_from_slice(data);
77108
ret
78109
}
@@ -82,23 +113,28 @@ impl std::convert::From<&[u8]> for InfoHash {
82113
impl std::convert::From<&DefaultHasher> for InfoHash {
83114
fn from(data: &DefaultHasher) -> InfoHash {
84115
let n = data.finish().to_le_bytes();
85-
InfoHash([
116+
let bytes = [
86117
n[0], n[1], n[2], n[3], n[4], n[5], n[6], n[7], n[0], n[1], n[2], n[3], n[4], n[5], n[6], n[7], n[0], n[1], n[2],
87118
n[3],
88-
])
119+
];
120+
let data = aquatic_udp_protocol::InfoHash(bytes);
121+
Self { data }
89122
}
90123
}
91124

92125
impl std::convert::From<&i32> for InfoHash {
93126
fn from(n: &i32) -> InfoHash {
94127
let n = n.to_le_bytes();
95-
InfoHash([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, n[0], n[1], n[2], n[3]])
128+
let bytes = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, n[0], n[1], n[2], n[3]];
129+
let data = aquatic_udp_protocol::InfoHash(bytes);
130+
Self { data }
96131
}
97132
}
98133

99134
impl std::convert::From<[u8; 20]> for InfoHash {
100-
fn from(val: [u8; 20]) -> Self {
101-
InfoHash(val)
135+
fn from(bytes: [u8; 20]) -> Self {
136+
let data = aquatic_udp_protocol::InfoHash(bytes);
137+
Self { data }
102138
}
103139
}
104140

@@ -171,7 +207,7 @@ impl<'v> serde::de::Visitor<'v> for InfoHashVisitor {
171207
));
172208
}
173209

174-
let mut res = InfoHash([0u8; 20]);
210+
let mut res = InfoHash::default();
175211

176212
if binascii::hex2bin(v.as_bytes(), &mut res.0).is_err() {
177213
return Err(serde::de::Error::invalid_value(

packages/primitives/src/lib.rs

-24
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ use std::collections::BTreeMap;
88
use std::time::Duration;
99

1010
use info_hash::InfoHash;
11-
use serde::{Deserialize, Serialize};
1211

13-
pub mod announce_event;
1412
pub mod info_hash;
1513
pub mod pagination;
1614
pub mod peer;
@@ -20,26 +18,4 @@ pub mod torrent_metrics;
2018
/// Duration since the Unix Epoch.
2119
pub type DurationSinceUnixEpoch = Duration;
2220

23-
/// Serializes a `DurationSinceUnixEpoch` as a Unix timestamp in milliseconds.
24-
/// # Errors
25-
///
26-
/// Will return `serde::Serializer::Error` if unable to serialize the `unix_time_value`.
27-
pub fn ser_unix_time_value<S: serde::Serializer>(unix_time_value: &DurationSinceUnixEpoch, ser: S) -> Result<S::Ok, S::Error> {
28-
#[allow(clippy::cast_possible_truncation)]
29-
ser.serialize_u64(unix_time_value.as_millis() as u64)
30-
}
31-
32-
/// IP version used by the peer to connect to the tracker: IPv4 or IPv6
33-
#[derive(PartialEq, Eq, Debug)]
34-
pub enum IPVersion {
35-
/// <https://en.wikipedia.org/wiki/Internet_Protocol_version_4>
36-
IPv4,
37-
/// <https://en.wikipedia.org/wiki/IPv6>
38-
IPv6,
39-
}
40-
41-
/// Number of bytes downloaded, uploaded or pending to download (left) by the peer.
42-
#[derive(Hash, Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
43-
pub struct NumberOfBytes(pub i64);
44-
4521
pub type PersistentTorrents = BTreeMap<InfoHash, u32>;

0 commit comments

Comments
 (0)