Skip to content

Commit

Permalink
feat: use 2021 edition
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuhvi committed Feb 11, 2025
1 parent c6bb302 commit f444463
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 24 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "mainline"
version = "5.0.0"
authors = ["nuh.dev"]
edition = "2018"
edition = "2021"
description = "Simple, robust, BitTorrent's Mainline DHT implementation"
homepage = "https://github.com/pubky/mainline"
license = "MIT"
Expand All @@ -12,7 +12,7 @@ repository = "https://github.com/pubky/mainline"
exclude = ["/docs/*", "/examples/*"]

[dependencies]
rand = "0.8.5"
rand = "0.9.0"
serde_bencode = "^0.2.4"
serde = { version = "1.0.217", features = ["derive"] }
serde_bytes = "0.11.15"
Expand All @@ -21,7 +21,7 @@ crc = "3.2.1"
sha1_smol = "1.0.1"
ed25519-dalek = "2.1.1"
tracing = "0.1"
lru = { version = "0.12.5", default-features = false }
lru = { version = "0.13.0", default-features = false }
dyn-clone = "1.0.18"

document-features = "0.2.10"
Expand All @@ -41,7 +41,7 @@ histo = "1.0.0"
rayon = "1.10"
dashmap = "6.1"
flume = "0.11.1"
colored = "2.0.0"
colored = "2.2.0"
chrono = "0.4"

[features]
Expand Down
15 changes: 6 additions & 9 deletions src/common/id.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Kademlia node Id or a lookup target
use crc::{Crc, CRC_32_ISCSI};
use rand::Rng;
use rand::random;
use serde::{Deserialize, Serialize};
use std::convert::TryInto;
use std::{
Expand All @@ -23,8 +23,7 @@ pub struct Id([u8; ID_SIZE]);
impl Id {
/// Generate a random Id
pub fn random() -> Id {
let mut rng = rand::thread_rng();
let bytes: [u8; 20] = rng.gen();
let bytes: [u8; 20] = random();

Id(bytes)
}
Expand Down Expand Up @@ -90,10 +89,9 @@ impl Id {

/// Create a new Id from an Ipv4 address according to [BEP_0042](http://bittorrent.org/beps/bep_0042.html).
pub fn from_ip(ip: IpAddr) -> Id {
let mut rng = rand::thread_rng();
let r: u8 = rng.gen();
let r: u8 = random();

let bytes: [u8; 20] = rng.gen();
let bytes: [u8; 20] = random();

match ip {
IpAddr::V4(addr) => from_ipv4_and_r(bytes, addr, r),
Expand All @@ -103,10 +101,9 @@ impl Id {

/// Create a new Id from an Ipv4 address according to [BEP_0042](http://bittorrent.org/beps/bep_0042.html).
pub fn from_ipv4(ipv4: Ipv4Addr) -> Id {
let mut rng = rand::thread_rng();
let r: u8 = rng.gen();
let r: u8 = random();

let bytes: [u8; 20] = rng.gen();
let bytes: [u8; 20] = random();

from_ipv4_and_r(bytes, ipv4, r)
}
Expand Down
6 changes: 2 additions & 4 deletions src/rpc/server/peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::{net::SocketAddrV4, num::NonZeroUsize};

use rand::{thread_rng, Rng};
use rand::random_bool;

use crate::common::Id;

Expand Down Expand Up @@ -57,16 +57,14 @@ impl PeersStore {

let mut results = Vec::with_capacity(20);

let mut rng = thread_rng();

for (index, (_, addr)) in info_hash_lru.iter().enumerate() {
// Calculate the chance of adding the current item based on remaining items and slots
let remaining_slots = target_size - results.len();
let remaining_items = info_hash_lru.len() - index;
let current_chance = remaining_slots as f64 / remaining_items as f64;

// Randomly decide to add the item based on the current chance
if rng.gen_bool(current_chance) {
if random_bool(current_chance) {
results.push(*addr);
if results.len() == target_size {
break;
Expand Down
11 changes: 4 additions & 7 deletions src/rpc/server/tokens.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Manage tokens for remote client IPs.
use crc::{Crc, CRC_32_ISCSI};
use rand::{thread_rng, Rng};
use rand::random;
use std::{
fmt::{self, Debug, Formatter},
net::SocketAddrV4,
Expand Down Expand Up @@ -33,11 +33,9 @@ impl Debug for Tokens {
impl Tokens {
/// Create a Tokens generator.
pub fn new() -> Self {
let mut rng = thread_rng();

Tokens {
prev_secret: rng.gen(),
curr_secret: rng.gen(),
prev_secret: random(),
curr_secret: random(),
last_updated: Instant::now(),
}
}
Expand All @@ -60,10 +58,9 @@ impl Tokens {
/// Rotate the tokens secret.
pub fn rotate(&mut self) {
trace!("Rotating secrets");
let mut rng = thread_rng();

self.prev_secret = self.curr_secret;
self.curr_secret = rng.gen();
self.curr_secret = random();

self.last_updated = Instant::now();
}
Expand Down

0 comments on commit f444463

Please # to comment.