Skip to content

Commit

Permalink
Network optimizations.
Browse files Browse the repository at this point in the history
  • Loading branch information
Revertron committed Sep 6, 2022
1 parent a36860e commit 74b84f0
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "alfis"
version = "0.8.0"
version = "0.8.1"
authors = ["Revertron <[email protected]>"]
edition = "2021"
build = "build.rs"
Expand Down
8 changes: 6 additions & 2 deletions src/p2p/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/p2p/peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -362,10 +363,11 @@ impl Peers {

fn ask_blocks_from_peers(&mut self, registry: &Registry, height: u64, max_height: u64, have_blocks: HashSet<u64>) {
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) {
Expand All @@ -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 {
Expand Down

0 comments on commit 74b84f0

Please sign in to comment.