Skip to content

Commit

Permalink
dev: vairous fixups
Browse files Browse the repository at this point in the history
  • Loading branch information
da2ce7 committed Jul 15, 2024
1 parent 330f72d commit 99c83af
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 36 deletions.
2 changes: 1 addition & 1 deletion packages/primitives/src/info_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl Ord for InfoHash {
}
}

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 Down
9 changes: 0 additions & 9 deletions packages/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,4 @@ pub mod torrent_metrics;
/// Duration since the Unix Epoch.
pub type DurationSinceUnixEpoch = Duration;

/// 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,
}

pub type PersistentTorrents = BTreeMap<InfoHash, u32>;
34 changes: 12 additions & 22 deletions packages/primitives/src/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes, PeerId};
use serde::Serialize;
use zerocopy::FromBytes as _;

use crate::{DurationSinceUnixEpoch, IPVersion};
use crate::DurationSinceUnixEpoch;

/// Peer struct used by the core `Tracker`.
///
Expand Down Expand Up @@ -208,15 +208,6 @@ impl Peer {
pub fn change_ip(&mut self, new_ip: &IpAddr) {
self.peer_addr = SocketAddr::new(*new_ip, self.peer_addr.port());
}

/// The IP version used by the peer: IPV4 or IPV6
#[must_use]
pub fn ip_version(&self) -> IPVersion {
if self.peer_addr.is_ipv4() {
return IPVersion::IPv4;
}
IPVersion::IPv6
}
}

use std::panic::Location;
Expand Down Expand Up @@ -264,22 +255,21 @@ impl DerefMut for Id {
}
}

impl From<[u8; 20]> for Id {
fn from(bytes: [u8; 20]) -> Self {
let data = PeerId(bytes);
Self { data }
}
}

impl From<i32> for Id {
fn from(number: i32) -> Self {
impl Id {
#[must_use]
pub fn new<T>(number: T) -> Self
where
T: Into<i128>,
{
let number: i128 = number.into();
let number = number.to_le_bytes();
let bytes = [
0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, number[0], number[1], number[2],
number[3],
0u8, 0u8, 0u8, 0u8, number[0], number[1], number[2], number[3], number[4], number[5], number[6], number[7],
number[8], number[9], number[10], number[11], number[12], number[13], number[14], number[15],
];

Id::from(bytes)
let data = PeerId(bytes);
Id { data }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl TorrentPeerBuilder {
/// has not announced it has stopped
#[must_use]
pub fn a_completed_peer(id: i32) -> peer::Peer {
let peer_id = peer::Id::from(id);
let peer_id = peer::Id::new(id);
TorrentPeerBuilder::new()
.with_number_of_bytes_left(0)
.with_event_completed()
Expand All @@ -81,7 +81,7 @@ pub fn a_completed_peer(id: i32) -> peer::Peer {
/// Leecher: left > 0 OR event = Stopped
#[must_use]
pub fn a_started_peer(id: i32) -> peer::Peer {
let peer_id = peer::Id::from(id);
let peer_id = peer::Id::new(id);
TorrentPeerBuilder::new()
.with_number_of_bytes_left(1)
.with_event_started()
Expand Down
2 changes: 1 addition & 1 deletion packages/torrent-repository/tests/entry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ async fn it_should_limit_the_number_of_peers_returned(
// We add one more peer than the scrape limit
for peer_number in 1..=74 + 1 {
let mut peer = a_started_peer(1);
peer.peer_id = *peer::Id::from(peer_number);
peer.peer_id = *peer::Id::new(peer_number);
torrent.upsert_peer(&peer).await;
}

Expand Down
2 changes: 1 addition & 1 deletion src/servers/apis/v1/context/torrent/resources/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct Peer {
/// The peer's left bytes (pending to download).
pub left: i64,
/// The peer's event: `started`, `stopped`, `completed`.
/// See [`AnnounceEvent`](torrust_tracker_primitives::announce_event::AnnounceEvent).
/// See [`AnnounceEvent`](aquatic_udp_protocol::AnnounceEvent).
pub event: String,
}

Expand Down

0 comments on commit 99c83af

Please sign in to comment.