Optimized p2p communication. Disabled connections from v0.6.*. Updated dependencies.

This commit is contained in:
Revertron
2022-07-11 14:44:24 +02:00
parent ef5cef290e
commit 20054e4be7
5 changed files with 34 additions and 12 deletions
+8 -1
View File
@@ -459,6 +459,9 @@ impl Network {
let my_id = self.peers.get_my_id().to_owned();
let answer = match message {
Message::Hand { app_version, origin, version, public, rand_id } => {
if app_version.starts_with("0.6") {
return State::Banned;
}
if self.peers.is_our_own_connect(&rand_id) {
warn!("Detected loop connect");
State::SendLoop
@@ -490,6 +493,9 @@ impl Network {
if self.peers.is_tween_connect(&rand_id) {
return State::Twin;
}
if app_version.starts_with("0.6") {
return State::Banned;
}
let nodes = self.peers.get_peers_active_count();
let peer = self.peers.get_mut_peer(token).unwrap();
// TODO check rand_id whether we have this peers connection already
@@ -610,6 +616,7 @@ impl Network {
let peers_count = self.peers.get_peers_active_count();
let peer = self.peers.get_mut_peer(token).unwrap();
peer.set_received_block(block.index);
trace!("New block from {}", &peer.get_addr());
let mut context = self.context.lock().unwrap();
let max_height = context.chain.get_max_height();
@@ -654,8 +661,8 @@ impl Network {
if height + 1 == block.index {
context.chain.update_max_height(height);
post(crate::event::Event::SyncFinished);
return State::Banned;
}
return State::Banned;
}
BlockQuality::Rewind => {
debug!("Got some orphan block, requesting its parent");
+14
View File
@@ -19,6 +19,7 @@ pub struct Peer {
active: bool,
reconnects: u32,
received_block: u64,
sent_height: u64,
cipher: Option<Chacha>,
fork: HashMap<u64, Block>
}
@@ -36,6 +37,7 @@ impl Peer {
active: false,
reconnects: 0,
received_block: 0,
sent_height: 0,
cipher: None,
fork: HashMap::new()
}
@@ -84,6 +86,18 @@ impl Peer {
self.height = height;
}
pub fn get_height(&self) -> u64 {
self.height
}
pub fn set_sent_height(&mut self, height: u64) {
self.sent_height = height;
}
pub fn get_sent_height(&self) -> u64 {
self.sent_height
}
pub fn is_higher(&self, height: u64) -> bool {
self.height > height
}
+4 -3
View File
@@ -284,13 +284,14 @@ impl Peers {
let mut rng = rand::thread_rng();
match self.peers
.iter_mut()
.filter_map(|(token, peer)| if peer.is_lower(height) && peer.get_state().is_idle() { Some((token, peer)) } else { None })
.filter_map(|(token, peer)| if peer.is_lower(height) && peer.get_state().is_idle() && peer.get_sent_height() < height { Some((token, peer)) } else { None })
.choose(&mut rng) {
None => {}
Some((token, peer)) => {
debug!("Peer {} is behind, sending ping", &peer.get_addr().ip());
debug!("Peer {} is behind ({}), sending ping", &peer.get_addr().ip(), peer.get_height());
registry.reregister(peer.get_stream(), *token, Interest::WRITABLE).unwrap();
peer.set_state(State::message(Message::Ping { height, hash }));
peer.set_sent_height(height);
self.update_behind_ping_time();
}
}
@@ -368,7 +369,7 @@ impl Peers {
if self.new_peers.is_empty() {
return;
}
self.new_peers.dedup();
self.new_peers.sort().dedup();
let addr = self.new_peers.remove(0);
match self.connect_peer(&addr, registry, unique_token, yggdrasil_only) {
Ok(_) => {}