Skip to content

Commit

Permalink
fix clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
oblique committed Jan 16, 2024
1 parent e4d35dd commit 76c3d07
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 64 deletions.
2 changes: 2 additions & 0 deletions src/cid_prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub(crate) struct CidPrefix {
}

impl CidPrefix {
#[allow(dead_code)]
pub(crate) fn from_cid<const S: usize>(cid: &CidGeneric<S>) -> CidPrefix {
CidPrefix {
version: cid.version(),
Expand Down Expand Up @@ -51,6 +52,7 @@ impl CidPrefix {
})
}

#[allow(dead_code)]
pub(crate) fn to_bytes(&self) -> Vec<u8> {
match self.version {
// CIDv0 is a naked multihash that with fixed code and size
Expand Down
42 changes: 19 additions & 23 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ pub(crate) struct ClientBehaviour<const S: usize, B>
where
B: Blockstore + Send + Sync,
{
config: Arc<ClientConfig>,
store: Arc<B>,
protocol: StreamProtocol,
queue: VecDeque<ToSwarm<BitswapEvent, ToHandlerEvent>>,
Expand Down Expand Up @@ -104,7 +103,6 @@ where
let set_send_dont_have = config.set_send_dont_have;

Ok(ClientBehaviour {
config: Arc::new(config),
store,
protocol,
queue: VecDeque::new(),
Expand Down Expand Up @@ -542,15 +540,9 @@ impl<const S: usize> Drop for ClientConnectionHandler<S> {
mod tests {
use super::*;
use crate::proto::message::mod_Message::{mod_Wantlist::WantType, Block, BlockPresence};
use crate::test_utils::{cid_of_data, poll_fn_once, select_poll_fn, RAW_CODEC};
use crate::test_utils::{cid_of_data, poll_fn_once};
use blockstore::{Blockstore, InMemoryBlockstore};
use cid::Cid;
use futures::future::{select, Either};
use multihash::Multihash;
use multihash_codetable::{Code, MultihashDigest};
use std::future::poll_fn;
use std::time::Duration;
use tokio::time::timeout;

async fn blockstore() -> Arc<InMemoryBlockstore<64>> {
let store = Arc::new(InMemoryBlockstore::<64>::new());
Expand Down Expand Up @@ -602,13 +594,13 @@ mod tests {
let mut client = new_client().await;

let peer1 = PeerId::random();
let mut conn1 = client.new_connection_handler(peer1.clone());
let mut _conn1 = client.new_connection_handler(peer1);

let peer2 = PeerId::random();
let mut conn2 = client.new_connection_handler(peer2.clone());
let mut _conn2 = client.new_connection_handler(peer2);

let cid1 = cid_of_data(b"x1");
let query_id1 = client.get(&cid1);
let _query_id1 = client.get(&cid1);

// Wantlist will be generated for both peers
for _ in 0..2 {
Expand Down Expand Up @@ -671,20 +663,23 @@ mod tests {
assert!(!entry.cancel);
assert_eq!(entry.wantType, WantType::Block);
assert!(entry.sendDontHave);

// Mark send state as ready
*send_state.lock().unwrap() = SendingState::Ready;
}

#[tokio::test]
async fn get_unknown_cid_responds_with_dont_have() {
let mut client = new_client().await;

let peer1 = PeerId::random();
let mut conn1 = client.new_connection_handler(peer1.clone());
let mut _conn1 = client.new_connection_handler(peer1);

let peer2 = PeerId::random();
let mut conn2 = client.new_connection_handler(peer2.clone());
let mut _conn2 = client.new_connection_handler(peer2);

let cid1 = cid_of_data(b"x1");
let query_id1 = client.get(&cid1);
let _query_id1 = client.get(&cid1);

// Wantlist will be generated for both peers
for _ in 0..2 {
Expand Down Expand Up @@ -765,7 +760,7 @@ mod tests {
let mut client = new_client().await;

let peer1 = PeerId::random();
let mut conn1 = client.new_connection_handler(peer1.clone());
let mut _conn1 = client.new_connection_handler(peer1);

let cid1 = cid_of_data(b"x1");
let query_id1 = client.get(&cid1);
Expand Down Expand Up @@ -828,19 +823,18 @@ mod tests {
#[tokio::test]
async fn full_wantlist_then_update() {
let mut client = new_client().await;
let mut conn = client.new_connection_handler(PeerId::random());
let mut _conn = client.new_connection_handler(PeerId::random());

let cid1 = cid_of_data(b"x1");
let query_id1 = client.get(&cid1);
let _query_id1 = client.get(&cid1);

let cid2 = cid_of_data(b"x2");
let query_id2 = client.get(&cid2);
let _query_id2 = client.get(&cid2);

let ev = poll_fn(|cx| client.poll(cx)).await;

let (wantlist, send_state) = match ev {
ToSwarm::NotifyHandler {
peer_id,
event: ToHandlerEvent::SendWantlist(wantlist, send_state),
..
} => (wantlist, send_state),
Expand Down Expand Up @@ -872,13 +866,12 @@ mod tests {
*send_state.lock().unwrap() = SendingState::Ready;

let cid3 = cid_of_data(b"x3");
let query_id3 = client.get(&cid3);
let _query_id3 = client.get(&cid3);

let ev = poll_fn(|cx| client.poll(cx)).await;

let wantlist = match ev {
ToSwarm::NotifyHandler {
peer_id,
event: ToHandlerEvent::SendWantlist(wantlist, _),
..
} => wantlist,
Expand All @@ -900,7 +893,7 @@ mod tests {
let mut client = new_client().await;

let peer1 = PeerId::random();
let mut conn1 = client.new_connection_handler(peer1.clone());
let mut _conn1 = client.new_connection_handler(peer1);

let cid1 = cid_of_data(b"x1");
let query_id1 = client.get(&cid1);
Expand Down Expand Up @@ -958,5 +951,8 @@ mod tests {
let entry = &wantlist.entries[0];
assert_eq!(entry.block, cid1.to_bytes());
assert!(entry.cancel);

// Mark send state as ready
*send_state.lock().unwrap() = SendingState::Ready;
}
}
16 changes: 0 additions & 16 deletions src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::future::poll_fn;
use std::task::{Context, Poll};

use cid::Cid;
use futures::future::Either;
use multihash_codetable::{Code, MultihashDigest};

pub const RAW_CODEC: u64 = 0x55;
Expand All @@ -12,21 +11,6 @@ pub fn cid_of_data(data: &[u8]) -> Cid {
Cid::new_v1(RAW_CODEC, hash)
}

pub(crate) async fn select_poll_fn<T1, F1, T2, F2>(mut f1: F1, mut f2: F2) -> Either<T1, T2>
where
F1: FnMut(&mut Context<'_>) -> Poll<T1>,
F2: FnMut(&mut Context<'_>) -> Poll<T2>,
{
poll_fn(|cx| {
if let Poll::Ready(val) = f1(cx).map(Either::Left) {
return Poll::Ready(val);
}

f2(cx).map(Either::Right)
})
.await
}

pub(crate) async fn poll_fn_once<T, F>(mut f: F) -> Option<T>
where
F: FnMut(&mut Context<'_>) -> Poll<T>,
Expand Down
36 changes: 11 additions & 25 deletions src/wantlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ mod tests {
proto::message::mod_Message::mod_Wantlist::{Entry, WantType},
test_utils::cid_of_data,
};
use cid::Cid;

#[test]
fn insert() {
Expand All @@ -214,7 +213,7 @@ mod tests {
);

let cid = cid_of_data(b"1");
wantlist.insert(cid.clone());
wantlist.insert(cid);
assert!(!state.is_updated(&wantlist));

assert_eq!(
Expand All @@ -228,7 +227,6 @@ mod tests {
sendDontHave: true,
}],
full: false,
..ProtoWantlist::default()
}
);
assert!(state.is_updated(&wantlist));
Expand All @@ -242,8 +240,8 @@ mod tests {
let cid1 = cid_of_data(b"1");
let cid2 = cid_of_data(b"2");

wantlist.insert(cid1.clone());
wantlist.insert(cid2.clone());
wantlist.insert(cid1);
wantlist.insert(cid2);

assert_eq!(
state.generate_proto_update(&wantlist),
Expand All @@ -265,7 +263,6 @@ mod tests {
}
],
full: false,
..ProtoWantlist::default()
}
);
assert!(state.is_updated(&wantlist));
Expand All @@ -284,7 +281,6 @@ mod tests {
sendDontHave: true,
}],
full: false,
..ProtoWantlist::default()
}
);
assert!(state.is_updated(&wantlist));
Expand All @@ -309,7 +305,6 @@ mod tests {
}
],
full: true,
..ProtoWantlist::default()
}
);
}
Expand All @@ -322,8 +317,8 @@ mod tests {
let cid1 = cid_of_data(b"1");
let cid2 = cid_of_data(b"2");

wantlist.insert(cid1.clone());
wantlist.insert(cid2.clone());
wantlist.insert(cid1);
wantlist.insert(cid2);

assert_eq!(
state.generate_proto_update(&wantlist),
Expand All @@ -345,7 +340,6 @@ mod tests {
}
],
full: false,
..ProtoWantlist::default()
}
);
assert!(state.is_updated(&wantlist));
Expand All @@ -372,7 +366,6 @@ mod tests {
}
],
full: true,
..ProtoWantlist::default()
}
);
}
Expand All @@ -385,8 +378,8 @@ mod tests {
let cid1 = cid_of_data(b"1");
let cid2 = cid_of_data(b"2");

wantlist.insert(cid1.clone());
wantlist.insert(cid2.clone());
wantlist.insert(cid1);
wantlist.insert(cid2);

assert_eq!(
state.generate_proto_update(&wantlist),
Expand All @@ -408,7 +401,6 @@ mod tests {
}
],
full: false,
..ProtoWantlist::default()
}
);
assert!(state.is_updated(&wantlist));
Expand All @@ -427,7 +419,6 @@ mod tests {
sendDontHave: true,
}],
full: true,
..ProtoWantlist::default()
}
);
}
Expand All @@ -440,8 +431,8 @@ mod tests {
let cid1 = cid_of_data(b"1");
let cid2 = cid_of_data(b"2");

wantlist.insert(cid1.clone());
wantlist.insert(cid2.clone());
wantlist.insert(cid1);
wantlist.insert(cid2);

assert_eq!(
state.generate_proto_update(&wantlist),
Expand All @@ -463,7 +454,6 @@ mod tests {
}
],
full: false,
..ProtoWantlist::default()
}
);
assert!(state.is_updated(&wantlist));
Expand All @@ -482,7 +472,6 @@ mod tests {
sendDontHave: true,
}],
full: true,
..ProtoWantlist::default()
}
);
}
Expand All @@ -495,8 +484,8 @@ mod tests {
let cid1 = cid_of_data(b"1");
let cid2 = cid_of_data(b"2");

wantlist.insert(cid1.clone());
wantlist.insert(cid2.clone());
wantlist.insert(cid1);
wantlist.insert(cid2);

assert_eq!(
state.generate_proto_update(&wantlist),
Expand All @@ -518,7 +507,6 @@ mod tests {
}
],
full: false,
..ProtoWantlist::default()
}
);
assert!(state.is_updated(&wantlist));
Expand All @@ -535,7 +523,6 @@ mod tests {
..Default::default()
}],
full: false,
..ProtoWantlist::default()
}
);

Expand All @@ -550,7 +537,6 @@ mod tests {
sendDontHave: true,
}],
full: true,
..ProtoWantlist::default()
}
);
}
Expand Down

0 comments on commit 76c3d07

Please sign in to comment.