Skip to content

Commit

Permalink
Merge #1259: Overhaul core Tracker: review public methods for `tracke…
Browse files Browse the repository at this point in the history
…r-core` package

6639c98 refactor: [#1258] make things private or pub(crate) when possible. (Jose Celano)

Pull request description:

  Overhaul core Tracker: review public methods for `tracker-core` package.

ACKs for top commit:
  josecelano:
    ACK 6639c98

Tree-SHA512: 0f45460320a3bd587e0e51eb92f2bfb73d5c889f1279d48668680b886c612d9af96fe1710a1526c6fd67ec540d85110f5031eaf5fe078980c043024caa943d47
  • Loading branch information
josecelano committed Feb 12, 2025
2 parents ea2d73d + 6639c98 commit efe7e98
Show file tree
Hide file tree
Showing 30 changed files with 357 additions and 320 deletions.
8 changes: 4 additions & 4 deletions packages/tracker-core/src/announce_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ mod tests {
use torrust_tracker_test_helpers::configuration;

use crate::announce_handler::AnnounceHandler;
use crate::core_tests::initialize_handlers;
use crate::scrape_handler::ScrapeHandler;
use crate::test_helpers::tests::initialize_handlers;

fn public_tracker() -> (Arc<AnnounceHandler>, Arc<ScrapeHandler>) {
let config = configuration::ephemeral_public();
Expand Down Expand Up @@ -244,7 +244,7 @@ mod tests {
peer_ip, public_tracker, sample_peer_1, sample_peer_2, sample_peer_3,
};
use crate::announce_handler::PeersWanted;
use crate::core_tests::{sample_info_hash, sample_peer};
use crate::test_helpers::tests::{sample_info_hash, sample_peer};

mod should_assign_the_ip_to_the_peer {

Expand Down Expand Up @@ -411,7 +411,7 @@ mod tests {

use crate::announce_handler::tests::the_announce_handler::{peer_ip, public_tracker};
use crate::announce_handler::PeersWanted;
use crate::core_tests::{completed_peer, leecher, sample_info_hash, seeder, started_peer};
use crate::test_helpers::tests::{completed_peer, leecher, sample_info_hash, seeder, started_peer};

#[tokio::test]
async fn when_the_peer_is_a_seeder() {
Expand Down Expand Up @@ -474,8 +474,8 @@ mod tests {

use crate::announce_handler::tests::the_announce_handler::peer_ip;
use crate::announce_handler::{AnnounceHandler, PeersWanted};
use crate::core_tests::{sample_info_hash, sample_peer};
use crate::databases::setup::initialize_database;
use crate::test_helpers::tests::{sample_info_hash, sample_peer};
use crate::torrent::manager::TorrentsManager;
use crate::torrent::repository::in_memory::InMemoryTorrentRepository;
use crate::torrent::repository::persisted::DatabasePersistentTorrentRepository;
Expand Down
8 changes: 4 additions & 4 deletions packages/tracker-core/src/authentication/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl KeysHandler {
/// # Errors
///
/// Will return a `database::Error` if unable to add the `auth_key` to the database.
pub async fn generate_permanent_peer_key(&self) -> Result<PeerKey, databases::error::Error> {
pub(crate) async fn generate_permanent_peer_key(&self) -> Result<PeerKey, databases::error::Error> {
self.generate_expiring_peer_key(None).await
}

Expand Down Expand Up @@ -170,7 +170,7 @@ impl KeysHandler {
/// # Arguments
///
/// * `key` - The pre-generated key.
pub async fn add_permanent_peer_key(&self, key: Key) -> Result<PeerKey, databases::error::Error> {
pub(crate) async fn add_permanent_peer_key(&self, key: Key) -> Result<PeerKey, databases::error::Error> {
self.add_expiring_peer_key(key, None).await
}

Expand All @@ -188,7 +188,7 @@ impl KeysHandler {
/// * `key` - The pre-generated key.
/// * `lifetime` - The duration in seconds for the new key. The key will be
/// no longer valid after `lifetime` seconds.
pub async fn add_expiring_peer_key(
pub(crate) async fn add_expiring_peer_key(
&self,
key: Key,
valid_until: Option<DurationSinceUnixEpoch>,
Expand Down Expand Up @@ -219,7 +219,7 @@ impl KeysHandler {
}

/// It removes an authentication key from memory.
pub async fn remove_in_memory_auth_key(&self, key: &Key) {
pub(crate) async fn remove_in_memory_auth_key(&self, key: &Key) {
self.in_memory_key_repository.remove(key).await;
}

Expand Down
8 changes: 5 additions & 3 deletions packages/tracker-core/src/authentication/key/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,19 @@ pub type ParseKeyError = peer_key::ParseKeyError;
///
/// For more information see function [`generate_key`](crate::authentication::key::generate_key) to generate the
/// [`PeerKey`](crate::authentication::PeerKey).
pub const AUTH_KEY_LENGTH: usize = 32;
pub(crate) const AUTH_KEY_LENGTH: usize = 32;

/// It generates a new permanent random key [`PeerKey`].
#[cfg(test)]
#[must_use]
pub fn generate_permanent_key() -> PeerKey {
pub(crate) fn generate_permanent_key() -> PeerKey {
generate_key(None)
}

/// It generates a new expiring random key [`PeerKey`].
#[cfg(test)]
#[must_use]
pub fn generate_expiring_key(lifetime: Duration) -> PeerKey {
pub(crate) fn generate_expiring_key(lifetime: Duration) -> PeerKey {
generate_key(Some(lifetime))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,22 @@ pub struct InMemoryKeyRepository {

impl InMemoryKeyRepository {
/// It adds a new authentication key.
pub async fn insert(&self, auth_key: &PeerKey) {
pub(crate) async fn insert(&self, auth_key: &PeerKey) {
self.keys.write().await.insert(auth_key.key.clone(), auth_key.clone());
}

/// It removes an authentication key.
pub async fn remove(&self, key: &Key) {
pub(crate) async fn remove(&self, key: &Key) {
self.keys.write().await.remove(key);
}

pub async fn get(&self, key: &Key) -> Option<PeerKey> {
pub(crate) async fn get(&self, key: &Key) -> Option<PeerKey> {
self.keys.read().await.get(key).cloned()
}

/// It clears all the authentication keys.
pub async fn clear(&self) {
#[allow(dead_code)]
pub(crate) async fn clear(&self) {
let mut keys = self.keys.write().await;
keys.clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl DatabaseKeyRepository {
/// # Errors
///
/// Will return a `databases::error::Error` if unable to add the `auth_key` to the database.
pub fn add(&self, peer_key: &PeerKey) -> Result<(), databases::error::Error> {
pub(crate) fn add(&self, peer_key: &PeerKey) -> Result<(), databases::error::Error> {
self.database.add_key_to_keys(peer_key)?;
Ok(())
}
Expand All @@ -31,7 +31,7 @@ impl DatabaseKeyRepository {
/// # Errors
///
/// Will return a `database::Error` if unable to remove the `key` from the database.
pub fn remove(&self, key: &Key) -> Result<(), databases::error::Error> {
pub(crate) fn remove(&self, key: &Key) -> Result<(), databases::error::Error> {
self.database.remove_key_from_keys(key)?;
Ok(())
}
Expand All @@ -41,7 +41,7 @@ impl DatabaseKeyRepository {
/// # Errors
///
/// Will return a `database::Error` if unable to load the keys from the database.
pub fn load_keys(&self) -> Result<Vec<PeerKey>, databases::error::Error> {
pub(crate) fn load_keys(&self) -> Result<Vec<PeerKey>, databases::error::Error> {
let keys = self.database.load_keys()?;
Ok(keys)
}
Expand Down
215 changes: 0 additions & 215 deletions packages/tracker-core/src/core_tests.rs

This file was deleted.

Loading

0 comments on commit efe7e98

Please sign in to comment.