Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Fix light timeouts and eclipse protection.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomusdrw committed May 7, 2018
1 parent d629b1b commit 06285ea
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
6 changes: 3 additions & 3 deletions ethcore/light/src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ mod packet {
mod timeout {
use std::time::Duration;

pub const HANDSHAKE: Duration = Duration::from_millis(2500);
pub const ACKNOWLEDGE_UPDATE: Duration = Duration::from_millis(5000);
pub const BASE: u64 = 1500; // base timeout for packet.
pub const HANDSHAKE: Duration = Duration::from_millis(4_000);
pub const ACKNOWLEDGE_UPDATE: Duration = Duration::from_millis(5_000);
pub const BASE: u64 = 2500; // base timeout for packet.

// timeouts per request within packet.
pub const HEADERS: u64 = 250; // per header?
Expand Down
26 changes: 20 additions & 6 deletions util/network-devp2p/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ impl<'s> NetworkContextTrait for NetworkContext<'s> {
}

fn disconnect_peer(&self, peer: PeerId) {
trace!(target: "network", "Peer disconnect requested: {}", peer);
self.io.message(NetworkIoMessage::Disconnect(peer))
.unwrap_or_else(|e| warn!("Error sending network IO message: {:?}", e));
}
Expand Down Expand Up @@ -687,7 +688,7 @@ impl Host {
Err(e) => {
let s = session.lock();
trace!(target: "network", "Session read error: {}:{:?} ({:?}) {:?}", token, s.id(), s.remote_addr(), e);
if let ErrorKind::Disconnect(DisconnectReason::IncompatibleProtocol) = *e.kind() {
if let ErrorKind::Disconnect(DisconnectReason::UselessPeer) = *e.kind() {
if let Some(id) = s.id() {
if !self.reserved_nodes.read().contains(id) {
self.nodes.write().mark_as_useless(id);
Expand Down Expand Up @@ -720,11 +721,24 @@ impl Host {
// Outgoing connections are allowed as long as their count is <= min_peers
// Incoming connections are allowed to take all of the max_peers reserve, or at most half of the slots.
let max_ingress = max(max_peers - min_peers, min_peers / 2);
if reserved_only ||
(s.info.originated && egress_count > min_peers) ||
(!s.info.originated && ingress_count > max_ingress) {
// only proceed if the connecting peer is reserved.
if !self.reserved_nodes.read().contains(&id) {
if reserved_only
|| (s.info.originated && egress_count > min_peers)
|| (!s.info.originated && ingress_count > max_ingress)
{
// We didn't start the connection, but the node is known to us
// So eventually we will attempt to connect to it as well.
let is_incoming_but_known = !s.info.originated && self.nodes.read().contains(&id);

if is_incoming_but_known {
warn!(target: "network", "Allowing incoming connection from a known node.");
}
// only proceed if the connecting peer is reserved or is known
if !is_incoming_but_known && !self.reserved_nodes.read().contains(&id) {
trace!(
target: "network",
"Rejected {} session: TooManyPeers",
if s.info.originated { "outbound" } else { "inbound" }
);
s.disconnect(io, DisconnectReason::TooManyPeers);
kill = true;
break;
Expand Down

0 comments on commit 06285ea

Please sign in to comment.