Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 7d07614

Browse files
committed
Merge remote-tracking branch 'origin/master' into v0.2
2 parents 82ca00c + 3269611 commit 7d07614

File tree

22 files changed

+354
-234
lines changed

22 files changed

+354
-234
lines changed

Cargo.lock

Lines changed: 40 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

polkadot/cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ env_logger = "0.4"
1212
error-chain = "0.12"
1313
log = "0.3"
1414
atty = "0.2"
15-
regex = "0.2"
15+
regex = "1"
1616
time = "0.1"
1717
slog = "^2"
1818
ansi_term = "0.10"

polkadot/cli/src/lib.rs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ use codec::{Decode, Encode};
8686
use client::BlockOrigin;
8787
use runtime_primitives::generic::SignedBlock;
8888
use names::{Generator, Name};
89+
use regex::Regex;
8990

9091
use futures::Future;
9192
use tokio::runtime::Runtime;
@@ -155,6 +156,29 @@ pub trait Worker {
155156
fn work<C: ServiceComponents>(self, service: &Service<C>) -> Self::Work;
156157
}
157158

159+
/// Check whether a node name is considered as valid
160+
fn is_node_name_valid(_name: &str) -> Result<(), &str> {
161+
const MAX_NODE_NAME_LENGTH: usize = 32;
162+
let name = _name.to_string();
163+
if name.chars().count() >= MAX_NODE_NAME_LENGTH {
164+
return Err("Node name too long");
165+
}
166+
167+
let invalid_chars = r"[\\.@]";
168+
let re = Regex::new(invalid_chars).unwrap();
169+
if re.is_match(&name) {
170+
return Err("Node name should not contain invalid chars such as '.' and '@'");
171+
}
172+
173+
let invalid_patterns = r"(https?:\\/+)?(www)+";
174+
let re = Regex::new(invalid_patterns).unwrap();
175+
if re.is_match(&name) {
176+
return Err("Node name should not contain urls");
177+
}
178+
179+
Ok(())
180+
}
181+
158182
/// Parse command line arguments and start the node.
159183
///
160184
/// IANA unassigned port ranges that we could use:
@@ -206,7 +230,11 @@ pub fn run<I, T, W>(args: I, worker: W) -> error::Result<()> where
206230
None => Generator::with_naming(Name::Numbered).next().unwrap(),
207231
Some(name) => name.into(),
208232
};
209-
info!("Node name: {}", config.name);
233+
match is_node_name_valid(&config.name) {
234+
Ok(_) => info!("Node name: {}", config.name),
235+
Err(msg) => return Err(error::ErrorKind::Input(
236+
format!("Invalid node name '{}'. Reason: {}. If unsure, use none.", config.name, msg)).into())
237+
}
210238

211239
let base_path = base_path(&matches);
212240

@@ -613,7 +641,27 @@ fn init_logger(pattern: &str) {
613641

614642
fn kill_color(s: &str) -> String {
615643
lazy_static! {
616-
static ref RE: regex::Regex = regex::Regex::new("\x1b\\[[^m]+m").expect("Error initializing color regex");
644+
static ref RE: Regex = Regex::new("\x1b\\[[^m]+m").expect("Error initializing color regex");
617645
}
618646
RE.replace_all(s, "").to_string()
619647
}
648+
649+
#[cfg(test)]
650+
mod tests {
651+
use super::*;
652+
653+
#[test]
654+
fn tests_node_name_good() {
655+
assert!(is_node_name_valid("short name").is_ok());
656+
}
657+
658+
#[test]
659+
fn tests_node_name_bad() {
660+
assert!(is_node_name_valid("long names are not very cool for the ui").is_err());
661+
assert!(is_node_name_valid("Dots.not.Ok").is_err());
662+
assert!(is_node_name_valid("http://visit.me").is_err());
663+
assert!(is_node_name_valid("https://visit.me").is_err());
664+
assert!(is_node_name_valid("www.visit.me").is_err());
665+
assert!(is_node_name_valid("email@domain").is_err());
666+
}
667+
}

polkadot/network/src/lib.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ use parking_lot::Mutex;
4949
use polkadot_consensus::{Statement, SignedStatement, GenericStatement};
5050
use polkadot_primitives::{AccountId, Block, SessionKey, Hash, Header};
5151
use polkadot_primitives::parachain::{Id as ParaId, BlockData, Extrinsic, CandidateReceipt, Collation};
52-
use substrate_network::{PeerId, RequestId, Context};
52+
use substrate_network::{PeerId, RequestId, Context, Severity};
5353
use substrate_network::consensus_gossip::ConsensusGossip;
5454
use substrate_network::{message, generic_message};
5555
use substrate_network::specialization::Specialization;
@@ -403,7 +403,7 @@ impl PolkadotProtocol {
403403
};
404404

405405
if !info.claimed_validator {
406-
ctx.disable_peer(peer_id, "Session key broadcasted without setting authority role");
406+
ctx.report_peer(peer_id, Severity::Bad("Session key broadcasted without setting authority role"));
407407
return;
408408
}
409409

@@ -438,7 +438,7 @@ impl PolkadotProtocol {
438438
self.pending.push(req);
439439
self.dispatch_pending_requests(ctx);
440440
}
441-
None => ctx.disable_peer(peer_id, "Unexpected block data response"),
441+
None => ctx.report_peer(peer_id, Severity::Bad("Unexpected block data response")),
442442
}
443443
}
444444

@@ -453,9 +453,9 @@ impl PolkadotProtocol {
453453
};
454454

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

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

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

618618
match self.peers.get(&from) {
619-
None => ctx.disconnect_peer(from),
619+
None => ctx.report_peer(from, Severity::Useless("Unknown Polkadot specific reason")),
620620
Some(peer_info) => match peer_info.collating_for {
621-
None => ctx.disable_peer(from, "Sent collation without registering collator intent"),
621+
None => ctx.report_peer(from, Severity::Bad("Sent collation without registering collator intent")),
622622
Some((ref acc_id, ref para_id)) => {
623623
let structurally_valid = para_id == &collation_para && acc_id == &collated_acc;
624624
if structurally_valid && collation.receipt.check_signature().is_ok() {
625625
self.collators.on_collation(acc_id.clone(), relay_parent, collation)
626626
} else {
627-
ctx.disable_peer(from, "Sent malformed collation")
627+
ctx.report_peer(from, Severity::Bad("Sent malformed collation"))
628628
};
629629
}
630630
},
@@ -654,7 +654,7 @@ impl PolkadotProtocol {
654654
// disconnect a collator by account-id.
655655
fn disconnect_bad_collator(&self, ctx: &mut Context<Block>, account_id: AccountId) {
656656
if let Some(peer_id) = self.collator_peer_id(account_id) {
657-
ctx.disable_peer(peer_id, "Consensus layer determined the given collator misbehaved")
657+
ctx.report_peer(peer_id, Severity::Bad("Consensus layer determined the given collator misbehaved"))
658658
}
659659
}
660660
}

polkadot/network/src/tests.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use polkadot_primitives::{Block, Hash, SessionKey};
2424
use polkadot_primitives::parachain::{CandidateReceipt, HeadData, BlockData};
2525
use substrate_primitives::H512;
2626
use codec::Encode;
27-
use substrate_network::{PeerId, PeerInfo, ClientHandle, Context, Roles, message::Message as SubstrateMessage, specialization::Specialization, generic_message::Message as GenericMessage};
27+
use substrate_network::{Severity, PeerId, PeerInfo, ClientHandle, Context, Roles, message::Message as SubstrateMessage, specialization::Specialization, generic_message::Message as GenericMessage};
2828

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

44-
fn disable_peer(&mut self, peer: PeerId, _reason: &str) {
45-
self.disabled.push(peer);
46-
}
47-
48-
fn disconnect_peer(&mut self, peer: PeerId) {
49-
self.disconnected.push(peer);
44+
fn report_peer(&mut self, peer: PeerId, reason: Severity) {
45+
match reason {
46+
Severity::Bad(_) => self.disabled.push(peer),
47+
_ => self.disconnected.push(peer),
48+
}
5049
}
5150

5251
fn peer_info(&self, _peer: PeerId) -> Option<PeerInfo<Block>> {

substrate/network-libp2p/src/connection_filter.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
2-
// This file is part of Parity.
2+
// This file is part of Substrate.
33

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

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

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

1717
//! Connection filter trait.
1818

0 commit comments

Comments
 (0)