From 74b84f0e659683723668d3c49a11bc3214cac6f5 Mon Sep 17 00:00:00 2001 From: Revertron Date: Tue, 6 Sep 2022 19:17:04 +0200 Subject: [PATCH] Network optimizations. --- Cargo.toml | 2 +- src/p2p/network.rs | 8 ++++++-- src/p2p/peers.rs | 6 ++++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 11121f0..c2b8e08 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "alfis" -version = "0.8.0" +version = "0.8.1" authors = ["Revertron "] edition = "2021" build = "build.rs" diff --git a/src/p2p/network.rs b/src/p2p/network.rs index 6dacaee..544be2a 100644 --- a/src/p2p/network.rs +++ b/src/p2p/network.rs @@ -227,11 +227,15 @@ impl Network { } if event.is_readable() { - return self.process_readable(registry, event, seen_blocks); + if !self.process_readable(registry, event, seen_blocks) { + return false; + } } if event.is_writable() { - return self.process_writable(registry, event); + if !self.process_writable(registry, event) { + return false; + } } true diff --git a/src/p2p/peers.rs b/src/p2p/peers.rs index 1f9e890..f3a0b58 100644 --- a/src/p2p/peers.rs +++ b/src/p2p/peers.rs @@ -9,6 +9,7 @@ use chrono::Utc; use log::{debug, error, info, trace, warn}; use mio::net::TcpStream; use mio::{Interest, Registry, Token}; +use rand::prelude::SliceRandom; use rand::random; use rand::seq::IteratorRandom; @@ -362,10 +363,11 @@ impl Peers { fn ask_blocks_from_peers(&mut self, registry: &Registry, height: u64, max_height: u64, have_blocks: HashSet) { let mut rng = rand::thread_rng(); - let peers = self.peers + let mut peers = self.peers .iter_mut() .filter_map(|(token, peer)| if peer.has_more_blocks(height) { Some((token, peer)) } else { None }) .choose_multiple(&mut rng, (max_height - height) as usize); + peers.shuffle(&mut rng); let mut index = height + 1; for (token, peer) in peers { if have_blocks.contains(&index) { @@ -374,7 +376,7 @@ impl Peers { continue; } debug!("Peer {} is higher than we are, requesting block {}", &peer.get_addr().ip(), index); - registry.reregister(peer.get_stream(), *token, Interest::WRITABLE | Interest::READABLE).unwrap(); + registry.reregister(peer.get_stream(), *token, Interest::WRITABLE).unwrap(); peer.set_state(State::message(Message::GetBlock { index })); index += 1; if index > max_height {