Skip to content

Commit

Permalink
Merge torrust#961: dev: remove some internal types in preference for …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
da2ce7 committed Aug 24, 2024
2 parents efc2a76 + bef5680 commit f79a019
Show file tree
Hide file tree
Showing 40 changed files with 461 additions and 549 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/media/flamegraph_generated_without_sudo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions packages/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ rust-version.workspace = true
version.workspace = true

[dependencies]
aquatic_udp_protocol = "0"
binascii = "0"
derive_more = "0"
serde = { version = "1", features = ["derive"] }
tdyne-peer-id = "1"
tdyne-peer-id-registry = "0"
thiserror = "1"
zerocopy = "0"
43 changes: 0 additions & 43 deletions packages/primitives/src/announce_event.rs

This file was deleted.

66 changes: 51 additions & 15 deletions packages/primitives/src/info_hash.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use std::hash::{DefaultHasher, Hash, Hasher};
use std::ops::{Deref, DerefMut};
use std::panic::Location;

use thiserror::Error;
use zerocopy::FromBytes;

/// `BitTorrent` Info Hash v1
#[derive(PartialEq, Eq, Hash, Clone, Copy, Default, Debug)]
pub struct InfoHash(pub [u8; 20]);
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
pub struct InfoHash {
data: aquatic_udp_protocol::InfoHash,
}

pub const INFO_HASH_BYTES_LEN: usize = 20;

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

Self { data }
}

/// Returns the `InfoHash` internal byte array.
Expand All @@ -36,13 +39,41 @@ impl InfoHash {
}
}

impl Default for InfoHash {
fn default() -> Self {
Self {
data: aquatic_udp_protocol::InfoHash(Default::default()),
}
}
}

impl From<aquatic_udp_protocol::InfoHash> for InfoHash {
fn from(data: aquatic_udp_protocol::InfoHash) -> Self {
Self { data }
}
}

impl Deref for InfoHash {
type Target = aquatic_udp_protocol::InfoHash;

fn deref(&self) -> &Self::Target {
&self.data
}
}

impl DerefMut for InfoHash {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.data
}
}

impl Ord for InfoHash {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.0.cmp(&other.0)
}
}

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

fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut i = Self([0u8; 20]);
let mut i = Self::default();
if s.len() != 40 {
return Err(binascii::ConvertError::InvalidInputLength);
}
Expand All @@ -72,7 +103,7 @@ impl std::str::FromStr for InfoHash {
impl std::convert::From<&[u8]> for InfoHash {
fn from(data: &[u8]) -> InfoHash {
assert_eq!(data.len(), 20);
let mut ret = InfoHash([0u8; 20]);
let mut ret = Self::default();
ret.0.clone_from_slice(data);
ret
}
Expand All @@ -82,23 +113,28 @@ impl std::convert::From<&[u8]> for InfoHash {
impl std::convert::From<&DefaultHasher> for InfoHash {
fn from(data: &DefaultHasher) -> InfoHash {
let n = data.finish().to_le_bytes();
InfoHash([
let bytes = [
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],
n[3],
])
];
let data = aquatic_udp_protocol::InfoHash(bytes);
Self { data }
}
}

impl std::convert::From<&i32> for InfoHash {
fn from(n: &i32) -> InfoHash {
let n = n.to_le_bytes();
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]])
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]];
let data = aquatic_udp_protocol::InfoHash(bytes);
Self { data }
}
}

impl std::convert::From<[u8; 20]> for InfoHash {
fn from(val: [u8; 20]) -> Self {
InfoHash(val)
fn from(bytes: [u8; 20]) -> Self {
let data = aquatic_udp_protocol::InfoHash(bytes);
Self { data }
}
}

Expand Down Expand Up @@ -171,7 +207,7 @@ impl<'v> serde::de::Visitor<'v> for InfoHashVisitor {
));
}

let mut res = InfoHash([0u8; 20]);
let mut res = InfoHash::default();

if binascii::hex2bin(v.as_bytes(), &mut res.0).is_err() {
return Err(serde::de::Error::invalid_value(
Expand Down
24 changes: 0 additions & 24 deletions packages/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ use std::collections::BTreeMap;
use std::time::Duration;

use info_hash::InfoHash;
use serde::{Deserialize, Serialize};

pub mod announce_event;
pub mod info_hash;
pub mod pagination;
pub mod peer;
Expand All @@ -20,26 +18,4 @@ pub mod torrent_metrics;
/// Duration since the Unix Epoch.
pub type DurationSinceUnixEpoch = Duration;

/// Serializes a `DurationSinceUnixEpoch` as a Unix timestamp in milliseconds.
/// # Errors
///
/// Will return `serde::Serializer::Error` if unable to serialize the `unix_time_value`.
pub fn ser_unix_time_value<S: serde::Serializer>(unix_time_value: &DurationSinceUnixEpoch, ser: S) -> Result<S::Ok, S::Error> {
#[allow(clippy::cast_possible_truncation)]
ser.serialize_u64(unix_time_value.as_millis() as u64)
}

/// IP version used by the peer to connect to the tracker: IPv4 or IPv6
#[derive(PartialEq, Eq, Debug)]
pub enum IPVersion {
/// <https://en.wikipedia.org/wiki/Internet_Protocol_version_4>
IPv4,
/// <https://en.wikipedia.org/wiki/IPv6>
IPv6,
}

/// Number of bytes downloaded, uploaded or pending to download (left) by the peer.
#[derive(Hash, Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct NumberOfBytes(pub i64);

pub type PersistentTorrents = BTreeMap<InfoHash, u32>;
Loading

0 comments on commit f79a019

Please sign in to comment.