From 67ffac1df932db92f416c83b0d81d9bb176159a0 Mon Sep 17 00:00:00 2001 From: arkpar Date: Fri, 17 Jun 2016 16:01:33 +0200 Subject: [PATCH] Check for session expiration on peer registration --- sync/src/chain.rs | 4 ++++ sync/src/io.rs | 6 ++++++ sync/src/tests/chain.rs | 10 ++++++++++ util/src/network/host.rs | 7 ++++++- 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/sync/src/chain.rs b/sync/src/chain.rs index 90e18d8e8..7e23601e4 100644 --- a/sync/src/chain.rs +++ b/sync/src/chain.rs @@ -356,6 +356,10 @@ impl ChainSync { }; trace!(target: "sync", "New peer {} (protocol: {}, network: {:?}, difficulty: {:?}, latest:{}, genesis:{})", peer_id, peer.protocol_version, peer.network_id, peer.difficulty, peer.latest_hash, peer.genesis); + if io.is_expired() { + info!("Status packet from expired session {}:{}", peer_id, io.peer_info(peer_id)); + return Ok(()); + } if self.peers.contains_key(&peer_id) { warn!("Unexpected status packet from {}:{}", peer_id, io.peer_info(peer_id)); diff --git a/sync/src/io.rs b/sync/src/io.rs index 53a546e1c..5d4b32464 100644 --- a/sync/src/io.rs +++ b/sync/src/io.rs @@ -41,6 +41,8 @@ pub trait SyncIo { fn is_chain_queue_empty(&self) -> bool { self.chain().queue_info().is_empty() } + /// Check if the session is expired + fn is_expired(&self) -> bool; } /// Wraps `NetworkContext` and the blockchain client @@ -83,6 +85,10 @@ impl<'s, 'h> SyncIo for NetSyncIo<'s, 'h> { fn peer_info(&self, peer_id: PeerId) -> String { self.network.peer_info(peer_id) } + + fn is_expired(&self) -> bool { + self.network.is_expired() + } } diff --git a/sync/src/tests/chain.rs b/sync/src/tests/chain.rs index 09e83e358..7c7d70dde 100644 --- a/sync/src/tests/chain.rs +++ b/sync/src/tests/chain.rs @@ -192,3 +192,13 @@ fn restart_on_broken_chain() { assert_eq!(net.peer(0).chain.chain_info().best_block_number, 5); } + +#[test] +fn high_td_attach() { + let mut net = TestNet::new(2); + net.peer_mut(1).chain.add_blocks(10, EachBlockWith::Uncle); + net.peer_mut(1).chain.corrupt_block_parent(6); + net.sync_steps(20); + + assert_eq!(net.peer(0).chain.chain_info().best_block_number, 5); +} diff --git a/util/src/network/host.rs b/util/src/network/host.rs index 6001769fe..e6e76c472 100644 --- a/util/src/network/host.rs +++ b/util/src/network/host.rs @@ -18,7 +18,7 @@ use std::net::{SocketAddr}; use std::collections::{HashMap}; use std::str::{FromStr}; use std::sync::*; -use std::sync::atomic::{AtomicUsize, Ordering as AtomicOrdering}; +use std::sync::atomic::{AtomicUsize, AtomicBool, Ordering as AtomicOrdering}; use std::ops::*; use std::cmp::min; use std::path::{Path, PathBuf}; @@ -249,6 +249,11 @@ impl<'s, Message> NetworkContext<'s, Message> where Message: Send + Sync + Clone self.io.message(NetworkIoMessage::Disconnect(peer)); } + /// Sheck if the session is till active. + pub fn is_expired(&self) -> bool { + self.session.as_ref().map(|s| s.lock().unwrap().expired()).unwrap_or(false) + } + /// Register a new IO timer. 'IoHandler::timeout' will be called with the token. pub fn register_timer(&self, token: TimerToken, ms: u64) -> Result<(), UtilError> { self.io.message(NetworkIoMessage::AddTimer {