Skip to content

Commit

Permalink
chore: disallow unused
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuhvi committed Apr 22, 2024
1 parent 3ca17ef commit 402538e
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 43 deletions.
15 changes: 0 additions & 15 deletions pkarr/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,6 @@ impl PkarrCache {
}
}

/// Returns the number of key-value pairs that are currently in the cache.
pub fn len(&self) -> usize {
self.inner.lock().unwrap().len()
}

/// Returns true if the cache is empty and false otherwise.
pub fn is_empty(&self) -> bool {
self.inner.lock().unwrap().is_empty()
}

/// Puts a key-value pair into cache. If the key already exists in the cache,
/// then it updates the key's value.
pub fn put(&self, key: Id, value: SignedPacket) {
Expand All @@ -42,9 +32,4 @@ impl PkarrCache {
pub fn get(&self, key: &Id) -> Option<SignedPacket> {
self.inner.lock().unwrap().get(key).cloned()
}

/// Returns the lock over underlying LRU cache.
pub fn lock(&self) -> std::sync::MutexGuard<LruCache<Id, SignedPacket>> {
self.inner.lock().unwrap()
}
}
29 changes: 9 additions & 20 deletions pkarr/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
use flume::{Receiver, Sender};
use lru::LruCache;
use mainline::{
dht::DhtSettings,
rpc::{
messages::{self, ErrorSpecific},
messages::{self},
QueryResponse, QueryResponseSpecific, ReceivedFrom, ReceivedMessage, Response, Rpc,
RpcTickReport,
},
Id, MutableItem,
};
use std::{
collections::{HashMap, HashSet},
num::NonZeroUsize,
thread,
time::{Duration, Instant},
};
use std::{collections::HashMap, num::NonZeroUsize, thread};
use tracing::{debug, instrument};

use crate::{cache::PkarrCache, Error, PublicKey, Result, SignedPacket};
Expand All @@ -28,6 +21,7 @@ pub const DEFAULT_CACHE_SIZE: usize = 1000;
// TODO: examine errors (failed to publish, failed to bind socket, unused errors...)
// TODO: logs (info for binding, debug for steps)
// TODO: HTTP relay should return some caching headers.
// TODO: add server settings to mainline DhtSettings

#[derive(Debug, Clone)]
pub struct Settings {
Expand Down Expand Up @@ -163,7 +157,7 @@ impl PkarrClient {
.map_err(|_| Error::DhtIsShutdown)?;

match receiver.recv() {
Ok(Ok(id)) => Ok(()),
Ok(Ok(_)) => Ok(()),
Ok(Err(error)) => match error {
mainline::Error::PutQueryIsInflight(_) => Err(Error::PublishInflight),
// Should not be reachable unless all nodes responded with a QueryError,
Expand All @@ -174,7 +168,7 @@ impl PkarrClient {
},
// Since we pass this sender to `Rpc::put`, the only reason the sender,
// would be dropped, is if `Rpc` is dropped, which should only happeng on shutdown.
Err(error) => Err(Error::DhtIsShutdown),
Err(_) => Err(Error::DhtIsShutdown),
}
}

Expand Down Expand Up @@ -249,8 +243,6 @@ impl PkarrClient {

/// Shutdown the actor thread loop.
pub fn shutdown(&self) -> Result<()> {
let (sender, receiver) = flume::bounded::<()>(1);

self.sender
.send(ActorMessage::Shutdown)
.map_err(|_| Error::DhtIsShutdown)?;
Expand All @@ -259,7 +251,7 @@ impl PkarrClient {
}
}

fn run(mut rpc: Rpc, settings: DhtSettings, cache: PkarrCache, receiver: Receiver<ActorMessage>) {
fn run(mut rpc: Rpc, _settings: DhtSettings, cache: PkarrCache, receiver: Receiver<ActorMessage>) {
let mut server = mainline::server::Server::default();
let mut senders: HashMap<Id, Vec<Sender<ResolveResponse>>> = HashMap::new();

Expand Down Expand Up @@ -367,7 +359,7 @@ fn run(mut rpc: Rpc, settings: DhtSettings, cache: PkarrCache, receiver: Receive
// with it.
if let Some(set) = senders.get(target) {
for sender in set {
sender.send(ResolveResponse::NoMoreRecentValue(*seq));
let _ = sender.send(ResolveResponse::NoMoreRecentValue(*seq));
}
}

Expand Down Expand Up @@ -403,8 +395,6 @@ enum ResolveResponse {

#[cfg(test)]
mod tests {
use std::thread::sleep;

use mainline::Testnet;

use super::*;
Expand All @@ -414,7 +404,7 @@ mod tests {
fn publish_resolve() {
let testnet = Testnet::new(10);

let mut a = PkarrClient::builder()
let a = PkarrClient::builder()
.bootstrap(&testnet.bootstrap)
.server()
.build()
Expand All @@ -432,7 +422,7 @@ mod tests {

let signed_packet = SignedPacket::from_packet(&keypair, &packet).unwrap();

let x = a.publish(&signed_packet);
let _ = a.publish(&signed_packet);

let b = PkarrClient::builder()
.bootstrap(&testnet.bootstrap)
Expand All @@ -441,7 +431,6 @@ mod tests {

let resolved = b.resolve(&keypair.public_key()).unwrap();
assert_eq!(resolved.as_bytes(), signed_packet.as_bytes());
assert_eq!(b.cache.len(), 1);

let from_cache = b.resolve(&keypair.public_key()).unwrap();
assert_eq!(from_cache.as_bytes(), signed_packet.as_bytes());
Expand Down
2 changes: 0 additions & 2 deletions pkarr/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! Main Crate Error
use crate::keys::PublicKey;

#[derive(thiserror::Error, Debug)]
/// Pkarr crate error enum.
pub enum Error {
Expand Down
1 change: 0 additions & 1 deletion pkarr/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![doc = include_str!("../README.md")]
#![allow(unused)]

// TODO add support for wasm using relays.

Expand Down
10 changes: 5 additions & 5 deletions pkarr/src/signed_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{Error, Keypair, PublicKey, Result};
use bytes::{Bytes, BytesMut};
use ed25519_dalek::Signature;
#[cfg(feature = "dht")]
use mainline::{Id, MutableItem};
use mainline::MutableItem;
use self_cell::self_cell;
use simple_dns::{
rdata::{RData, A, AAAA},
Expand Down Expand Up @@ -744,9 +744,9 @@ mod tests {
#[test]
fn minimum_ttl_empty() {
let keypair = Keypair::random();
let mut packet = Packet::new_reply(0);
let packet = Packet::new_reply(0);

let mut signed = SignedPacket::from_packet(&keypair, &packet).unwrap();
let signed = SignedPacket::from_packet(&keypair, &packet).unwrap();

assert_eq!(signed.ttl(None, None), 30);
}
Expand All @@ -769,7 +769,7 @@ mod tests {
RData::TXT("world".try_into().unwrap()),
));

let mut signed = SignedPacket::from_packet(&keypair, &packet).unwrap();
let signed = SignedPacket::from_packet(&keypair, &packet).unwrap();

assert_eq!(signed.minimum_ttl(None, None), DEFAULT_MINIMUM_TTL);

Expand All @@ -795,7 +795,7 @@ mod tests {
RData::TXT("world".try_into().unwrap()),
));

let mut signed = SignedPacket::from_packet(&keypair, &packet).unwrap();
let signed = SignedPacket::from_packet(&keypair, &packet).unwrap();

assert_eq!(signed.minimum_ttl(None, None), DEFAULT_MAXIMUM_TTL);

Expand Down

0 comments on commit 402538e

Please sign in to comment.