Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions polkadot/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ use parking_lot::Mutex;
use polkadot_consensus::{Statement, SignedStatement, GenericStatement};
use polkadot_primitives::{AccountId, Block, SessionKey, Hash, Header};
use polkadot_primitives::parachain::{Id as ParaId, BlockData, Extrinsic, CandidateReceipt, Collation};
use substrate_network::{PeerId, RequestId, Context};
use substrate_network::{PeerId, RequestId, Context, Severity};
use substrate_network::consensus_gossip::ConsensusGossip;
use substrate_network::{message, generic_message};
use substrate_network::specialization::Specialization;
Expand Down Expand Up @@ -403,7 +403,7 @@ impl PolkadotProtocol {
};

if !info.claimed_validator {
ctx.disable_peer(peer_id, "Session key broadcasted without setting authority role");
ctx.report_peer(peer_id, Severity::Bad("Session key broadcasted without setting authority role"));
return;
}

Expand Down Expand Up @@ -438,7 +438,7 @@ impl PolkadotProtocol {
self.pending.push(req);
self.dispatch_pending_requests(ctx);
}
None => ctx.disable_peer(peer_id, "Unexpected block data response"),
None => ctx.report_peer(peer_id, Severity::Bad("Unexpected block data response")),
}
}

Expand All @@ -453,9 +453,9 @@ impl PolkadotProtocol {
};

match info.validator_key {
None => ctx.disable_peer(
None => ctx.report_peer(
peer_id,
"Sent collator role without registering first as validator",
Severity::Bad("Sent collator role without registering first as validator"),
),
Some(key) => for (relay_parent, collation) in self.local_collations.note_validator_role(key, role) {
send_polkadot_message(
Expand Down Expand Up @@ -483,7 +483,7 @@ impl Specialization<Block> for PolkadotProtocol {

if let Some((ref acc_id, ref para_id)) = local_status.collating_for {
if self.collator_peer_id(acc_id.clone()).is_some() {
ctx.disconnect_peer(peer_id);
ctx.report_peer(peer_id, Severity::Useless("Unknown Polkadot-specific reason"));
return
}

Expand Down Expand Up @@ -571,7 +571,7 @@ impl Specialization<Block> for PolkadotProtocol {
Some(msg) => self.on_polkadot_message(ctx, peer_id, raw, msg),
None => {
trace!(target: "p_net", "Bad message from {}", peer_id);
ctx.disable_peer(peer_id, "Invalid polkadot protocol message format");
ctx.report_peer(peer_id, Severity::Bad("Invalid polkadot protocol message format"));
}
}
}
Expand Down Expand Up @@ -616,15 +616,15 @@ impl PolkadotProtocol {
let collated_acc = collation.receipt.collator;

match self.peers.get(&from) {
None => ctx.disconnect_peer(from),
None => ctx.report_peer(from, Severity::Useless("Unknown Polkadot specific reason")),
Some(peer_info) => match peer_info.collating_for {
None => ctx.disable_peer(from, "Sent collation without registering collator intent"),
None => ctx.report_peer(from, Severity::Bad("Sent collation without registering collator intent")),
Some((ref acc_id, ref para_id)) => {
let structurally_valid = para_id == &collation_para && acc_id == &collated_acc;
if structurally_valid && collation.receipt.check_signature().is_ok() {
self.collators.on_collation(acc_id.clone(), relay_parent, collation)
} else {
ctx.disable_peer(from, "Sent malformed collation")
ctx.report_peer(from, Severity::Bad("Sent malformed collation"))
};
}
},
Expand Down Expand Up @@ -654,7 +654,7 @@ impl PolkadotProtocol {
// disconnect a collator by account-id.
fn disconnect_bad_collator(&self, ctx: &mut Context<Block>, account_id: AccountId) {
if let Some(peer_id) = self.collator_peer_id(account_id) {
ctx.disable_peer(peer_id, "Consensus layer determined the given collator misbehaved")
ctx.report_peer(peer_id, Severity::Bad("Consensus layer determined the given collator misbehaved"))
}
}
}
Expand Down
13 changes: 6 additions & 7 deletions polkadot/network/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use polkadot_primitives::{Block, Hash, SessionKey};
use polkadot_primitives::parachain::{CandidateReceipt, HeadData, BlockData};
use substrate_primitives::H512;
use codec::Encode;
use substrate_network::{PeerId, PeerInfo, ClientHandle, Context, Roles, message::Message as SubstrateMessage, specialization::Specialization, generic_message::Message as GenericMessage};
use substrate_network::{Severity, PeerId, PeerInfo, ClientHandle, Context, Roles, message::Message as SubstrateMessage, specialization::Specialization, generic_message::Message as GenericMessage};

use std::sync::Arc;
use futures::Future;
Expand All @@ -41,12 +41,11 @@ impl Context<Block> for TestContext {
unimplemented!()
}

fn disable_peer(&mut self, peer: PeerId, _reason: &str) {
self.disabled.push(peer);
}

fn disconnect_peer(&mut self, peer: PeerId) {
self.disconnected.push(peer);
fn report_peer(&mut self, peer: PeerId, reason: Severity) {
match reason {
Severity::Bad(_) => self.disabled.push(peer),
_ => self.disconnected.push(peer),
}
}

fn peer_info(&self, _peer: PeerId) -> Option<PeerInfo<Block>> {
Expand Down
8 changes: 4 additions & 4 deletions substrate/network-libp2p/src/connection_filter.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
// This file is part of Parity.
// This file is part of Substrate.

// Parity is free software: you can redistribute it and/or modify
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity is distributed in the hope that it will be useful,
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

//! Connection filter trait.

Expand Down
9 changes: 5 additions & 4 deletions substrate/network-libp2p/src/custom_proto.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// Copyright 2018 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// This file is part of Substrate.

// Polkadot is free software: you can redistribute it and/or modify
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Polkadot is distributed in the hope that it will be useful,
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.?
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.?

use bytes::{Bytes, BytesMut};
use ProtocolId;
Expand Down Expand Up @@ -122,6 +122,7 @@ where C: AsyncRead + AsyncWrite + 'static, // TODO: 'static :-/
type MultiaddrFuture = Maf;
type Future = future::FutureResult<(Self::Output, Self::MultiaddrFuture), IoError>;

#[allow(deprecated)]
fn upgrade(
self,
socket: C,
Expand Down
8 changes: 4 additions & 4 deletions substrate/network-libp2p/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
// This file is part of Parity.
// This file is part of Substrate.

// Parity is free software: you can redistribute it and/or modify
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity is distributed in the hope that it will be useful,
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

use std::{io, net, fmt};
use libc::{ENFILE, EMFILE};
Expand Down
8 changes: 4 additions & 4 deletions substrate/network-libp2p/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// Copyright 2018 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// This file is part of Substrate.

// Polkadot is free software: you can redistribute it and/or modify
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Polkadot is distributed in the hope that it will be useful,
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.?
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.?

#![recursion_limit="128"]
#![type_length_limit = "268435456"]
Expand Down
72 changes: 62 additions & 10 deletions substrate/network-libp2p/src/network_state.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// Copyright 2018 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// This file is part of Substrate.

// Polkadot is free software: you can redistribute it and/or modify
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Polkadot is distributed in the hope that it will be useful,
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.?
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.?

use bytes::Bytes;
use fnv::{FnvHashMap, FnvHashSet};
Expand Down Expand Up @@ -126,6 +126,45 @@ struct PeerConnectionInfo {
local_address: Option<Multiaddr>,
}

/// Simplified, POD version of PeerConnectionInfo.
#[derive(Debug, Clone)]
pub struct PeerInfo {
/// Id of the peer.
pub id: PeerstorePeerId,

/// True if this connection was initiated by us.
/// Note that it is theoretically possible that we dial the remote at the
/// same time they dial us, in which case the protocols may be dispatched
/// between both connections, and in which case the value here will be racy.
pub originated: bool,

/// Latest known ping duration.
pub ping: Option<Duration>,

/// The client version of the remote, or `None` if not known.
pub client_version: Option<String>,

/// The multiaddress of the remote, or `None` if not known.
pub remote_address: Option<Multiaddr>,

/// The local multiaddress used to communicate with the remote, or `None`
/// if not known.
pub local_address: Option<Multiaddr>,
}

impl<'a> From<&'a PeerConnectionInfo> for PeerInfo {
fn from(i: &'a PeerConnectionInfo) -> PeerInfo {
PeerInfo {
id: i.id.clone(),
originated: i.originated,
ping: i.ping.lock().clone(),
client_version: i.client_version.clone(),
remote_address: i.remote_address.clone(),
local_address: i.local_address.clone(),
}
}
}

impl NetworkState {
pub fn new(config: &NetworkConfiguration) -> Result<NetworkState, Error> {
// Private and public keys configuration.
Expand Down Expand Up @@ -581,24 +620,37 @@ impl NetworkState {
} else {
// We are connected to this peer, but not with the current
// protocol.
debug!(target: "sub-libp2p", "Tried to send message to peer {} \
for which we aren't connected with the requested protocol",
peer_id);
debug!(target: "sub-libp2p",
"Tried to send message to peer {} for which we aren't connected with the requested protocol",
peer_id
);
return Err(ErrorKind::PeerNotFound.into())
}
} else {
debug!(target: "sub-libp2p", "Tried to send message to invalid \
peer ID {}", peer_id);
debug!(target: "sub-libp2p", "Tried to send message to invalid peer ID {}", peer_id);
return Err(ErrorKind::PeerNotFound.into())
}
}

/// Get the info on a peer, if there's an active connection.
pub fn peer_info(&self, who: PeerId) -> Option<PeerInfo> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this should be merged with session_info, but I haven't looked at how the higher-level layers use session_info().

self.connections.read().info_by_peer.get(&who).map(Into::into)
}

/// Disconnects a peer, if a connection exists (ie. drops the Kademlia
/// controller, and the senders that were stored in the `UniqueConnec` of
/// `custom_proto`).
pub fn disconnect_peer(&self, peer_id: PeerId) {
pub fn drop_peer(&self, peer_id: PeerId, reason: Option<&str>) {
let mut connections = self.connections.write();
if let Some(peer_info) = connections.info_by_peer.remove(&peer_id) {
if let Some(reason) = reason {
if let (&Some(ref client_version), &Some(ref remote_address)) = (&peer_info.client_version, &peer_info.remote_address) {
debug!(target: "sub-libp2p", "Disconnected peer {} (version: {}, address: {}). {}", peer_id, client_version, remote_address, reason);
} else {
debug!(target: "sub-libp2p", "Disconnected peer {}. {}", peer_id, reason);
}
}

trace!(target: "sub-libp2p", "Destroying peer #{} {:?} ; \
kademlia = {:?} ; num_protos = {:?}", peer_id, peer_info.id,
peer_info.kad_connec.is_alive(),
Expand Down
Loading