From 51584f520244ac50a53f0708394abc5270fa886c Mon Sep 17 00:00:00 2001 From: arkpar Date: Sun, 10 Jan 2016 14:11:23 +0100 Subject: [PATCH] Networking fixes --- src/sync/chain.rs | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/src/sync/chain.rs b/src/sync/chain.rs index 276b1993c..95505ff81 100644 --- a/src/sync/chain.rs +++ b/src/sync/chain.rs @@ -13,20 +13,11 @@ /// 4. Maintain sync by handling NewBlocks/NewHashes messages /// -use std::collections::{HashSet, HashMap}; -use std::cmp::{min, max}; +use util::*; use std::mem::{replace}; -use util::network::{PeerId, PacketId}; -use util::hash::{H256, FixedHash}; -use util::bytes::{Bytes}; -use util::uint::{U256}; -use util::rlp::{Rlp, UntrustedRlp, RlpStream, self}; -use util::rlp::rlptraits::{Stream, View}; -use util::rlp::rlperrors::DecoderError; -use util::sha3::Hashable; -use client::{BlockNumber, BlockChainClient, BlockStatus, QueueStatus, ImportResult}; use views::{HeaderView}; use header::{Header as BlockHeader}; +use client::{BlockNumber, BlockChainClient, BlockStatus, QueueStatus, ImportResult}; use sync::range_collection::{RangeCollection, ToUsize, FromUsize}; use sync::io::SyncIo; @@ -66,6 +57,8 @@ const NODE_DATA_PACKET: u8 = 0x0e; const GET_RECEIPTS_PACKET: u8 = 0x0f; const RECEIPTS_PACKET: u8 = 0x10; +const NETWORK_ID: U256 = ONE_U256; //TODO: get this from parent + struct Header { /// Header data data: Bytes, @@ -241,7 +234,19 @@ impl ChainSync { asking_blocks: Vec::new(), }; - trace!(target: "sync", "New peer (protocol: {}, network: {:?}, difficulty: {:?}, latest:{}, genesis:{})", peer.protocol_version, peer.network_id, peer.difficulty, peer.latest, peer.genesis); + trace!(target: "sync", "New peer {} (protocol: {}, network: {:?}, difficulty: {:?}, latest:{}, genesis:{})", peer_id, peer.protocol_version, peer.network_id, peer.difficulty, peer.latest, peer.genesis); + + let chain_info = io.chain().chain_info(); + if peer.genesis != chain_info.genesis_hash { + io.disable_peer(peer_id); + trace!(target: "sync", "Peer {} genesis hash not matched", peer_id); + return Ok(()); + } + if peer.network_id != NETWORK_ID { + io.disable_peer(peer_id); + trace!(target: "sync", "Peer {} network id not matched", peer_id); + return Ok(()); + } let old = self.peers.insert(peer_id.clone(), peer); if old.is_some() { @@ -449,8 +454,10 @@ impl ChainSync { /// Called by peer when it is disconnecting pub fn on_peer_aborting(&mut self, io: &mut SyncIo, peer: &PeerId) { trace!(target: "sync", "== Disconnected {}", peer); - self.clear_peer_download(peer); - self.continue_sync(io); + if self.peers.contains_key(&peer) { + self.clear_peer_download(peer); + self.continue_sync(io); + } } /// Called when a new peer is connected @@ -769,7 +776,7 @@ impl ChainSync { let mut packet = RlpStream::new_list(5); let chain = io.chain().chain_info(); packet.append(&(PROTOCOL_VERSION as u32)); - packet.append(&0u32); //TODO: network id + packet.append(&NETWORK_ID); //TODO: network id packet.append(&chain.total_difficulty); packet.append(&chain.best_block_hash); packet.append(&chain.genesis_hash);