Skip to content

Commit

Permalink
[sync]clippy.
Browse files Browse the repository at this point in the history
  • Loading branch information
tiangong3624749 committed Feb 1, 2021
1 parent d536f57 commit ae85fc3
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 92 deletions.
19 changes: 8 additions & 11 deletions network/api/src/peer_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl PeerSelector {
.lock()
.iter()
.find(|peer| &peer.peer_id() == peer_id)
.and_then(|peer| Some(peer.peer_info.clone()))
.map(|peer| peer.peer_info.clone())
}

/// Get top N peers sorted by total_difficulty
Expand Down Expand Up @@ -231,7 +231,7 @@ impl PeerSelector {
.collect()
}

pub fn retain(&self, peers: &Vec<PeerId>) {
pub fn retain(&self, peers: &[PeerId]) {
let mut score: u64 = 0;
self.details.lock().retain(|peer| -> bool {
let flag = peers.contains(&peer.peer_id());
Expand Down Expand Up @@ -302,11 +302,7 @@ impl PeerSelector {
}

if self.len() == 1 {
return self
.details
.lock()
.get(0)
.and_then(|peer| Some(peer.peer_id()));
return self.details.lock().get(0).map(|peer| peer.peer_id());
}

let mut random = rand::thread_rng();
Expand All @@ -327,14 +323,14 @@ impl PeerSelector {
.lock()
.iter()
.choose(&mut rand::thread_rng())
.and_then(|peer| Some(peer.peer_info.clone()))
.map(|peer| peer.peer_info.clone())
}

pub fn first_peer(&self) -> Option<PeerInfo> {
self.details
.lock()
.get(0)
.and_then(|peer| Some(peer.peer_info.clone()))
.map(|peer| peer.peer_info.clone())
}

pub fn is_empty(&self) -> bool {
Expand Down Expand Up @@ -362,6 +358,7 @@ impl PeerSelector {
#[cfg(test)]
mod tests {
use crate::peer_provider::PeerSelector;
use crate::PeerStrategy;
use starcoin_crypto::HashValue;
use starcoin_types::block::BlockHeader;
use starcoin_types::peer_info::{PeerId, PeerInfo};
Expand Down Expand Up @@ -404,8 +401,8 @@ mod tests {
),
];

let peer_selector = PeerSelector::new(peers);
let beat_selector = peer_selector.bests();
let peer_selector = PeerSelector::new(peers, PeerStrategy::default());
let beat_selector = peer_selector.bests().unwrap();
assert_eq!(2, beat_selector.len());

let top_selector = peer_selector.top(3);
Expand Down
2 changes: 1 addition & 1 deletion sync/src/tasks/inner_sync_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ where
let peer_id = target_peers
.iter()
.choose(&mut rand::thread_rng())
.map(|peer| peer.clone())
.cloned()
.ok_or_else(|| format_err!("Random peer is none when create sub target"))?;
let info = self
.fetcher
Expand Down
21 changes: 4 additions & 17 deletions sync/src/tasks/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use futures::channel::mpsc::UnboundedReceiver;
use futures::future::BoxFuture;
use futures::{FutureExt, StreamExt};
use futures_timer::Delay;
use network_api::NetworkService;
use rand::Rng;
use starcoin_accumulator::{Accumulator, MerkleAccumulator};
use starcoin_chain_api::ChainReader;
Expand All @@ -19,7 +18,6 @@ use starcoin_types::peer_info::{PeerId, PeerInfo};
use starcoin_vm_types::genesis_config::ChainNetwork;
use std::sync::Arc;
use std::time::Duration;
use test_helper::DummyNetworkService;

#[derive(Clone)]
pub struct MockBlockIdFetcher {
Expand Down Expand Up @@ -48,11 +46,11 @@ impl MockBlockIdFetcher {
}

impl PeerOperator for MockBlockIdFetcher {
fn filter(&self, peers: &Vec<PeerId>) {
fn filter(&self, _peers: &[PeerId]) {
unimplemented!()
}

fn new_peer(&self, peer_info: PeerInfo) {
fn new_peer(&self, _peer_info: PeerInfo) {
unimplemented!()
}

Expand Down Expand Up @@ -95,17 +93,6 @@ impl BlockIdFetcher for MockBlockIdFetcher {
}
}

pub struct SyncNodeMockerFactory {
network: DummyNetworkService,
fetch: Arc<SyncNodeMocker>,
}

impl SyncNodeMockerFactory {
pub fn new(network: DummyNetworkService, fetch: Arc<SyncNodeMocker>) -> Self {
Self { network, fetch }
}
}

pub struct SyncNodeMocker {
pub chain_mocker: MockChain,
pub delay_milliseconds: u64,
Expand Down Expand Up @@ -169,11 +156,11 @@ impl SyncNodeMocker {
}

impl PeerOperator for SyncNodeMocker {
fn filter(&self, peers: &Vec<PeerId>) {
fn filter(&self, _peers: &[PeerId]) {
unimplemented!()
}

fn new_peer(&self, peer_info: PeerInfo) {
fn new_peer(&self, _peer_info: PeerInfo) {
unimplemented!()
}

Expand Down
6 changes: 3 additions & 3 deletions sync/src/tasks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use stream_task::{
};

pub trait PeerOperator: Send + Sync {
fn filter(&self, peers: &Vec<PeerId>);
fn filter(&self, peers: &[PeerId]);

fn new_peer(&self, beer_info: PeerInfo);

Expand Down Expand Up @@ -55,7 +55,7 @@ pub trait BlockIdFetcher: Send + Sync {
}

impl PeerOperator for VerifiedRpcClient {
fn filter(&self, peers: &Vec<PeerId>) {
fn filter(&self, peers: &[PeerId]) {
self.selector().retain(peers)
}

Expand Down Expand Up @@ -110,7 +110,7 @@ impl<T> PeerOperator for Arc<T>
where
T: PeerOperator,
{
fn filter(&self, peers: &Vec<PeerId>) {
fn filter(&self, peers: &[PeerId]) {
PeerOperator::filter(self.as_ref(), peers)
}

Expand Down
83 changes: 26 additions & 57 deletions sync/src/tasks/tests.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
// Copyright (c) The Starcoin Core Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::tasks::mock::{SyncNodeMocker, SyncNodeMockerFactory};
use crate::tasks::mock::SyncNodeMocker;
use crate::tasks::{full_sync_task, BlockCollector};
use anyhow::{format_err, Result};
use futures::channel::mpsc::unbounded;
use futures_timer::Delay;
use logger::prelude::*;
use network_api::PeerStrategy;
use pin_utils::core_reexport::time::Duration;
use starcoin_chain_api::ChainReader;
use starcoin_chain_mock::BlockChain;
use starcoin_genesis::Genesis;
use starcoin_storage::block_info::BlockInfoStore;
use starcoin_storage::BlockStore;
use starcoin_types::block::{Block, BlockBody, BlockHeader};
use starcoin_types::peer_info::PeerInfo;
use starcoin_vm_types::genesis_config::{BuiltinNetworkID, ChainNetwork};
use std::sync::Arc;
use test_helper::DummyNetworkService;
Expand All @@ -38,21 +38,17 @@ pub async fn test_full_sync_new_node() -> Result<()> {
let storage = node2.chain().get_storage();
let (sender_1, receiver_1) = unbounded();
let (sender_2, _receiver_2) = unbounded();
let peers: Vec<PeerInfo> = Vec::new();
let fetcher_factory_1 = Arc::new(SyncNodeMockerFactory::new(
DummyNetworkService,
arc_node1.clone(),
));
let (sync_task, _task_handle, task_event_counter, _) = full_sync_task(
current_block_header.id(),
target.clone(),
false,
net2.time_service(),
storage.clone(),
sender_1,
peers.clone(),
arc_node1.clone(),
sender_2,
fetcher_factory_1,
DummyNetworkService,
PeerStrategy::default(),
)?;
let join_handle = node2.process_block_connect_event(receiver_1).await;
let branch = sync_task.await?;
Expand All @@ -69,10 +65,6 @@ pub async fn test_full_sync_new_node() -> Result<()> {

let (sender_1, receiver_1) = unbounded();
let (sender_2, _receiver_2) = unbounded();
let fetcher_factory_2 = Arc::new(SyncNodeMockerFactory::new(
DummyNetworkService,
arc_node1.clone(),
));
//sync again
let target = arc_node1.chain().get_block_info(None)?.unwrap();
let (sync_task, _task_handle, task_event_counter, _) = full_sync_task(
Expand All @@ -82,9 +74,10 @@ pub async fn test_full_sync_new_node() -> Result<()> {
net2.time_service(),
storage.clone(),
sender_1,
peers.clone(),
arc_node1.clone(),
sender_2,
fetcher_factory_2,
DummyNetworkService,
PeerStrategy::default(),
)?;
let join_handle = node2.process_block_connect_event(receiver_1).await;
let branch = sync_task.await?;
Expand Down Expand Up @@ -143,21 +136,17 @@ pub async fn test_full_sync_fork() -> Result<()> {
let storage = node2.chain().get_storage();
let (sender, receiver) = unbounded();
let (sender_2, _receiver_2) = unbounded();
let peers: Vec<PeerInfo> = Vec::new();
let fetcher_factory_1 = Arc::new(SyncNodeMockerFactory::new(
DummyNetworkService,
arc_node1.clone(),
));
let (sync_task, _task_handle, task_event_counter, _) = full_sync_task(
current_block_header.id(),
target.clone(),
false,
net2.time_service(),
storage.clone(),
sender,
peers.clone(),
arc_node1.clone(),
sender_2,
fetcher_factory_1,
DummyNetworkService,
PeerStrategy::default(),
)?;
let join_handle = node2.process_block_connect_event(receiver).await;
let branch = sync_task.await?;
Expand All @@ -178,21 +167,17 @@ pub async fn test_full_sync_fork() -> Result<()> {
let (sender, receiver) = unbounded();
let target = arc_node1.chain().get_block_info(None)?.unwrap();
let (sender_2, _receiver_2) = unbounded();
let peers: Vec<PeerInfo> = Vec::new();
let fetcher_factory_1 = Arc::new(SyncNodeMockerFactory::new(
DummyNetworkService,
arc_node1.clone(),
));
let (sync_task, _task_handle, task_event_counter, _) = full_sync_task(
current_block_header.id(),
target.clone(),
false,
net2.time_service(),
storage,
sender,
peers.clone(),
arc_node1.clone(),
sender_2,
fetcher_factory_1,
DummyNetworkService,
PeerStrategy::default(),
)?;
let join_handle = node2.process_block_connect_event(receiver).await;
let branch = sync_task.await?;
Expand Down Expand Up @@ -229,21 +214,17 @@ pub async fn test_full_sync_fork_from_genesis() -> Result<()> {
let storage = node2.chain().get_storage();
let (sender, receiver) = unbounded();
let (sender_2, _receiver_2) = unbounded();
let peers: Vec<PeerInfo> = Vec::new();
let fetcher_factory_1 = Arc::new(SyncNodeMockerFactory::new(
DummyNetworkService,
arc_node1.clone(),
));
let (sync_task, _task_handle, task_event_counter, _) = full_sync_task(
current_block_header.id(),
target.clone(),
false,
net2.time_service(),
storage.clone(),
sender,
peers.clone(),
arc_node1.clone(),
sender_2,
fetcher_factory_1,
DummyNetworkService,
PeerStrategy::default(),
)?;
let join_handle = node2.process_block_connect_event(receiver).await;
let branch = sync_task.await?;
Expand Down Expand Up @@ -289,21 +270,17 @@ pub async fn test_full_sync_continue() -> Result<()> {
let storage = node2.chain().get_storage();
let (sender, receiver) = unbounded();
let (sender_2, _receiver_2) = unbounded();
let peers: Vec<PeerInfo> = Vec::new();
let fetcher_factory_1 = Arc::new(SyncNodeMockerFactory::new(
DummyNetworkService,
arc_node1.clone(),
));
let (sync_task, _task_handle, task_event_counter, _) = full_sync_task(
current_block_header.id(),
target.clone(),
false,
net2.time_service(),
storage.clone(),
sender,
peers.clone(),
arc_node1.clone(),
sender_2,
fetcher_factory_1,
DummyNetworkService,
PeerStrategy::default(),
)?;
let join_handle = node2.process_block_connect_event(receiver).await;
let branch = sync_task.await?;
Expand All @@ -326,21 +303,17 @@ pub async fn test_full_sync_continue() -> Result<()> {
//continue sync
//TODO find a way to verify continue sync will reuse previous task local block.
let (sender_2, _receiver_2) = unbounded();
let peers: Vec<PeerInfo> = Vec::new();
let fetcher_factory_1 = Arc::new(SyncNodeMockerFactory::new(
DummyNetworkService,
arc_node1.clone(),
));
let (sync_task, _task_handle, task_event_counter, _) = full_sync_task(
current_block_header.id(),
target.clone(),
false,
net2.time_service(),
storage.clone(),
sender,
peers.clone(),
arc_node1.clone(),
sender_2,
fetcher_factory_1,
DummyNetworkService,
PeerStrategy::default(),
)?;

let join_handle = node2.process_block_connect_event(receiver).await;
Expand Down Expand Up @@ -380,21 +353,17 @@ pub async fn test_full_sync_cancel() -> Result<()> {
let storage = node2.chain().get_storage();
let (sender, receiver) = unbounded();
let (sender_2, _receiver_2) = unbounded();
let peers: Vec<PeerInfo> = Vec::new();
let fetcher_factory_1 = Arc::new(SyncNodeMockerFactory::new(
DummyNetworkService,
arc_node1.clone(),
));
let (sync_task, task_handle, task_event_counter, _) = full_sync_task(
current_block_header.id(),
target.clone(),
false,
net2.time_service(),
storage.clone(),
sender,
peers.clone(),
arc_node1.clone(),
sender_2,
fetcher_factory_1,
DummyNetworkService,
PeerStrategy::default(),
)?;
let join_handle = node2.process_block_connect_event(receiver).await;
let sync_join_handle = tokio::task::spawn(sync_task);
Expand Down
4 changes: 1 addition & 3 deletions sync/src/verified_rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,7 @@ impl VerifiedRpcClient {

pub fn best_peer(&self) -> Option<PeerInfo> {
if let Some(peers) = self.peer_selector.bests() {
peers
.choose(&mut rand::thread_rng())
.and_then(|peer| Some(peer.clone()))
peers.choose(&mut rand::thread_rng()).cloned()
} else {
None
}
Expand Down

0 comments on commit ae85fc3

Please sign in to comment.