2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2016-02-12 09:52:32 +01:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2016-02-12 09:52:32 +01:00
|
|
|
// 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.
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
2016-02-12 09:52:32 +01:00
|
|
|
// 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
|
2019-01-07 11:33:07 +01:00
|
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2016-02-12 09:52:32 +01:00
|
|
|
|
2018-05-09 15:58:02 +02:00
|
|
|
use discovery::{NodeEntry, TableUpdates};
|
|
|
|
use ethereum_types::H512;
|
|
|
|
use ip_utils::*;
|
|
|
|
use network::{AllowIP, Error, ErrorKind, IpFilter};
|
2018-05-07 13:11:12 +02:00
|
|
|
use rand::{self, Rng};
|
2018-05-09 15:58:02 +02:00
|
|
|
use rlp::{DecoderError, Rlp, RlpStream};
|
|
|
|
use serde_json;
|
2018-01-31 09:50:01 +01:00
|
|
|
use std::{
|
2016-05-02 14:48:30 +02:00
|
|
|
collections::{HashMap, HashSet},
|
2018-01-31 09:50:01 +01:00
|
|
|
fmt::{self, Display, Formatter},
|
2020-08-05 06:08:03 +02:00
|
|
|
fs,
|
2018-01-31 09:50:01 +01:00
|
|
|
hash::{Hash, Hasher},
|
|
|
|
net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6, ToSocketAddrs},
|
|
|
|
path::PathBuf,
|
2020-08-05 06:08:03 +02:00
|
|
|
slice,
|
2018-01-31 09:50:01 +01:00
|
|
|
str::FromStr,
|
2018-05-07 13:11:12 +02:00
|
|
|
time::{self, Duration, SystemTime},
|
2020-08-05 06:08:03 +02:00
|
|
|
};
|
2016-02-12 09:52:32 +01:00
|
|
|
|
2016-02-14 01:03:48 +01:00
|
|
|
/// Node public key
|
|
|
|
pub type NodeId = H512;
|
|
|
|
|
2018-07-11 21:22:06 +02:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2016-02-14 01:03:48 +01:00
|
|
|
/// Node address info
|
|
|
|
pub struct NodeEndpoint {
|
|
|
|
/// IP(V4 or V6) address
|
|
|
|
pub address: SocketAddr,
|
2018-08-17 16:04:03 +02:00
|
|
|
/// Connection port.
|
2016-02-14 01:03:48 +01:00
|
|
|
pub udp_port: u16,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NodeEndpoint {
|
|
|
|
pub fn udp_address(&self) -> SocketAddr {
|
|
|
|
match self.address {
|
2018-05-31 13:38:46 +02:00
|
|
|
SocketAddr::V4(a) => SocketAddr::V4(SocketAddrV4::new(*a.ip(), self.udp_port)),
|
|
|
|
SocketAddr::V6(a) => SocketAddr::V6(SocketAddrV6::new(
|
|
|
|
*a.ip(),
|
|
|
|
self.udp_port,
|
|
|
|
a.flowinfo(),
|
|
|
|
a.scope_id(),
|
|
|
|
)),
|
2016-02-14 01:03:48 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
|
|
|
|
2017-07-28 19:06:39 +02:00
|
|
|
pub fn is_allowed(&self, filter: &IpFilter) -> bool {
|
|
|
|
(self.is_allowed_by_predefined(&filter.predefined)
|
|
|
|
|| filter
|
|
|
|
.custom_allow
|
|
|
|
.iter()
|
|
|
|
.any(|ipnet| self.address.ip().is_within(ipnet)))
|
|
|
|
&& !filter
|
|
|
|
.custom_block
|
|
|
|
.iter()
|
|
|
|
.any(|ipnet| self.address.ip().is_within(ipnet))
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-07-28 19:06:39 +02:00
|
|
|
pub fn is_allowed_by_predefined(&self, filter: &AllowIP) -> bool {
|
2016-10-24 18:25:27 +02:00
|
|
|
match filter {
|
2018-05-31 13:38:46 +02:00
|
|
|
AllowIP::All => true,
|
|
|
|
AllowIP::Private => self.address.ip().is_usable_private(),
|
|
|
|
AllowIP::Public => self.address.ip().is_usable_public(),
|
|
|
|
AllowIP::None => false,
|
2016-10-24 18:25:27 +02:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
|
|
|
|
2018-04-16 15:52:12 +02:00
|
|
|
pub fn from_rlp(rlp: &Rlp) -> Result<Self, DecoderError> {
|
2016-12-27 12:53:56 +01:00
|
|
|
let tcp_port = rlp.val_at::<u16>(2)?;
|
|
|
|
let udp_port = rlp.val_at::<u16>(1)?;
|
|
|
|
let addr_bytes = rlp.at(0)?.data()?;
|
|
|
|
let address = match addr_bytes.len() {
|
2016-02-14 01:03:48 +01:00
|
|
|
4 => Ok(SocketAddr::V4(SocketAddrV4::new(
|
|
|
|
Ipv4Addr::new(addr_bytes[0], addr_bytes[1], addr_bytes[2], addr_bytes[3]),
|
|
|
|
tcp_port,
|
|
|
|
))),
|
|
|
|
16 => unsafe {
|
2018-05-31 13:38:46 +02:00
|
|
|
let o: *const u16 = addr_bytes.as_ptr() as *const u16;
|
2018-01-31 09:50:01 +01:00
|
|
|
let o = slice::from_raw_parts(o, 8);
|
2016-02-14 01:03:48 +01:00
|
|
|
Ok(SocketAddr::V6(SocketAddrV6::new(
|
|
|
|
Ipv6Addr::new(o[0], o[1], o[2], o[3], o[4], o[5], o[6], o[7]),
|
|
|
|
tcp_port,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
)))
|
|
|
|
},
|
|
|
|
_ => Err(DecoderError::RlpInconsistentLengthAndData),
|
2016-12-27 12:53:56 +01:00
|
|
|
}?;
|
2018-05-31 13:38:46 +02:00
|
|
|
Ok(NodeEndpoint { address, udp_port })
|
2016-02-14 01:03:48 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-02-14 01:03:48 +01:00
|
|
|
pub fn to_rlp(&self, rlp: &mut RlpStream) {
|
|
|
|
match self.address {
|
|
|
|
SocketAddr::V4(a) => {
|
|
|
|
rlp.append(&(&a.ip().octets()[..]));
|
|
|
|
}
|
|
|
|
SocketAddr::V6(a) => unsafe {
|
2018-05-31 13:38:46 +02:00
|
|
|
let o: *const u8 = a.ip().segments().as_ptr() as *const u8;
|
2018-01-31 09:50:01 +01:00
|
|
|
rlp.append(&slice::from_raw_parts(o, 16));
|
2016-02-14 01:03:48 +01:00
|
|
|
},
|
|
|
|
};
|
|
|
|
rlp.append(&self.udp_port);
|
|
|
|
rlp.append(&self.address.port());
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-02-14 01:03:48 +01:00
|
|
|
pub fn to_rlp_list(&self, rlp: &mut RlpStream) {
|
|
|
|
rlp.begin_list(3);
|
|
|
|
self.to_rlp(rlp);
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2019-06-25 15:38:29 +02:00
|
|
|
/// Validates that the tcp port is not 0 and that the node is a valid discovery node (i.e. `is_valid_discovery_node()` is true).
|
|
|
|
/// Sync happens over tcp.
|
|
|
|
pub fn is_valid_sync_node(&self) -> bool {
|
|
|
|
self.is_valid_discovery_node() && self.address.port() != 0
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2019-06-25 15:38:29 +02:00
|
|
|
/// Validates that the udp port is not 0 and address IP is specified.
|
|
|
|
/// Peer discovery happens over udp.
|
|
|
|
pub fn is_valid_discovery_node(&self) -> bool {
|
|
|
|
self.udp_port != 0
|
|
|
|
&& match self.address {
|
2017-07-28 19:06:39 +02:00
|
|
|
SocketAddr::V4(a) => !a.ip().is_unspecified(),
|
|
|
|
SocketAddr::V6(a) => !a.ip().is_unspecified(),
|
2016-02-14 01:03:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for NodeEndpoint {
|
2017-11-13 14:37:08 +01:00
|
|
|
type Err = Error;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-02-14 01:03:48 +01:00
|
|
|
/// Create endpoint from string. Performs name resolution if given a host name.
|
2017-11-13 14:37:08 +01:00
|
|
|
fn from_str(s: &str) -> Result<NodeEndpoint, Error> {
|
2016-02-14 01:03:48 +01:00
|
|
|
let address = s.to_socket_addrs().map(|mut i| i.next());
|
|
|
|
match address {
|
|
|
|
Ok(Some(a)) => Ok(NodeEndpoint {
|
|
|
|
address: a,
|
|
|
|
udp_port: a.port(),
|
|
|
|
}),
|
2018-05-09 15:58:02 +02:00
|
|
|
Ok(None) => bail!(ErrorKind::AddressResolve(None)),
|
|
|
|
Err(_) => Err(ErrorKind::AddressParse.into()), // always an io::Error of InvalidInput kind
|
2016-02-14 01:03:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-07 13:11:12 +02:00
|
|
|
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
|
2016-02-14 01:03:48 +01:00
|
|
|
pub enum PeerType {
|
|
|
|
_Required,
|
|
|
|
Optional,
|
|
|
|
}
|
|
|
|
|
2018-05-07 13:11:12 +02:00
|
|
|
/// A type for representing an interaction (contact) with a node at a given time
|
|
|
|
/// that was either a success or a failure.
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
pub enum NodeContact {
|
|
|
|
Success(SystemTime),
|
|
|
|
Failure(SystemTime),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NodeContact {
|
|
|
|
fn success() -> NodeContact {
|
|
|
|
NodeContact::Success(SystemTime::now())
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-05-07 13:11:12 +02:00
|
|
|
fn failure() -> NodeContact {
|
|
|
|
NodeContact::Failure(SystemTime::now())
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-05-07 13:11:12 +02:00
|
|
|
fn time(&self) -> SystemTime {
|
|
|
|
match *self {
|
|
|
|
NodeContact::Success(t) | NodeContact::Failure(t) => t,
|
|
|
|
}
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-05-07 13:11:12 +02:00
|
|
|
/// Filters and old contact, returning `None` if it happened longer than a
|
|
|
|
/// week ago.
|
|
|
|
fn recent(&self) -> Option<&NodeContact> {
|
|
|
|
let t = self.time();
|
|
|
|
if let Ok(d) = t.elapsed() {
|
|
|
|
if d < Duration::from_secs(60 * 60 * 24 * 7) {
|
|
|
|
return Some(self);
|
|
|
|
}
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-05-07 13:11:12 +02:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2016-02-14 01:03:48 +01:00
|
|
|
pub struct Node {
|
|
|
|
pub id: NodeId,
|
|
|
|
pub endpoint: NodeEndpoint,
|
|
|
|
pub peer_type: PeerType,
|
2018-05-07 13:11:12 +02:00
|
|
|
pub last_contact: Option<NodeContact>,
|
2016-02-14 01:03:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Node {
|
|
|
|
pub fn new(id: NodeId, endpoint: NodeEndpoint) -> Node {
|
|
|
|
Node {
|
2018-05-31 13:38:46 +02:00
|
|
|
id,
|
|
|
|
endpoint,
|
2016-02-14 01:03:48 +01:00
|
|
|
peer_type: PeerType::Optional,
|
2018-05-07 13:11:12 +02:00
|
|
|
last_contact: None,
|
2016-02-14 01:03:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-15 11:54:38 +01:00
|
|
|
impl Display for Node {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
|
|
if self.endpoint.udp_port != self.endpoint.address.port() {
|
2018-04-02 13:12:52 +02:00
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"enode://{:x}@{}+{}",
|
|
|
|
self.id, self.endpoint.address, self.endpoint.udp_port
|
|
|
|
)?;
|
2016-02-15 11:54:38 +01:00
|
|
|
} else {
|
2018-04-02 13:12:52 +02:00
|
|
|
write!(f, "enode://{:x}@{}", self.id, self.endpoint.address)?;
|
2016-02-15 11:54:38 +01:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2016-02-19 17:11:24 +01:00
|
|
|
|
2016-02-14 01:03:48 +01:00
|
|
|
impl FromStr for Node {
|
2017-11-13 14:37:08 +01:00
|
|
|
type Err = Error;
|
2016-02-14 01:03:48 +01:00
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
2016-02-22 13:59:25 +01:00
|
|
|
let (id, endpoint) = if s.len() > 136 && &s[0..8] == "enode://" && &s[136..137] == "@" {
|
2017-11-13 14:37:08 +01:00
|
|
|
(
|
|
|
|
s[8..136].parse().map_err(|_| ErrorKind::InvalidNodeId)?,
|
|
|
|
NodeEndpoint::from_str(&s[137..])?,
|
|
|
|
)
|
2016-02-14 01:03:48 +01:00
|
|
|
} else {
|
2016-12-27 12:53:56 +01:00
|
|
|
(NodeId::new(), NodeEndpoint::from_str(s)?)
|
2016-02-14 01:03:48 +01:00
|
|
|
};
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-02-14 01:03:48 +01:00
|
|
|
Ok(Node {
|
2018-05-31 13:38:46 +02:00
|
|
|
id,
|
|
|
|
endpoint,
|
2016-02-14 01:03:48 +01:00
|
|
|
peer_type: PeerType::Optional,
|
2018-05-07 13:11:12 +02:00
|
|
|
last_contact: None,
|
2016-02-14 01:03:48 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq for Node {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.id == other.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Eq for Node {}
|
|
|
|
|
|
|
|
impl Hash for Node {
|
|
|
|
fn hash<H>(&self, state: &mut H)
|
|
|
|
where
|
|
|
|
H: Hasher,
|
|
|
|
{
|
|
|
|
self.id.hash(state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-31 09:50:01 +01:00
|
|
|
const MAX_NODES: usize = 1024;
|
|
|
|
const NODES_FILE: &str = "nodes.json";
|
|
|
|
|
2016-02-14 01:03:48 +01:00
|
|
|
/// Node table backed by disk file.
|
2016-02-12 09:52:32 +01:00
|
|
|
pub struct NodeTable {
|
2016-02-15 11:54:38 +01:00
|
|
|
nodes: HashMap<NodeId, Node>,
|
2016-05-02 14:48:30 +02:00
|
|
|
useless_nodes: HashSet<NodeId>,
|
2016-02-15 11:54:38 +01:00
|
|
|
path: Option<String>,
|
2016-02-12 09:52:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl NodeTable {
|
2016-02-15 11:54:38 +01:00
|
|
|
pub fn new(path: Option<String>) -> NodeTable {
|
2016-02-12 09:52:32 +01:00
|
|
|
NodeTable {
|
2016-02-15 11:54:38 +01:00
|
|
|
path: path.clone(),
|
|
|
|
nodes: NodeTable::load(path),
|
2016-05-02 14:48:30 +02:00
|
|
|
useless_nodes: HashSet::new(),
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
2016-02-12 09:52:32 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-02-14 01:03:48 +01:00
|
|
|
/// Add a node to table
|
2016-02-15 19:54:27 +01:00
|
|
|
pub fn add_node(&mut self, mut node: Node) {
|
2018-05-07 13:11:12 +02:00
|
|
|
// preserve node last_contact
|
|
|
|
node.last_contact = self.nodes.get(&node.id).and_then(|n| n.last_contact);
|
2018-05-31 13:38:46 +02:00
|
|
|
self.nodes.insert(node.id, node);
|
2016-02-12 09:52:32 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-05-07 13:11:12 +02:00
|
|
|
/// Returns a list of ordered nodes according to their most recent contact
|
|
|
|
/// and filtering useless nodes. The algorithm for creating the sorted nodes
|
|
|
|
/// is:
|
|
|
|
/// - Contacts that aren't recent (older than 1 week) are discarded
|
|
|
|
/// - (1) Nodes with a successful contact are ordered (most recent success first)
|
|
|
|
/// - (2) Nodes with unknown contact (older than 1 week or new nodes) are randomly shuffled
|
|
|
|
/// - (3) Nodes with a failed contact are ordered (oldest failure first)
|
|
|
|
/// - The final result is the concatenation of (1), (2) and (3)
|
2018-03-16 22:39:23 +01:00
|
|
|
fn ordered_entries(&self) -> Vec<&Node> {
|
2018-05-07 13:11:12 +02:00
|
|
|
let mut success = Vec::new();
|
|
|
|
let mut failures = Vec::new();
|
|
|
|
let mut unknown = Vec::new();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-05-07 13:11:12 +02:00
|
|
|
let nodes = self
|
|
|
|
.nodes
|
|
|
|
.values()
|
|
|
|
.filter(|n| !self.useless_nodes.contains(&n.id));
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-05-07 13:11:12 +02:00
|
|
|
for node in nodes {
|
|
|
|
// discard contact points older that aren't recent
|
|
|
|
match node.last_contact.as_ref().and_then(|c| c.recent()) {
|
|
|
|
Some(&NodeContact::Success(_)) => {
|
|
|
|
success.push(node);
|
|
|
|
}
|
|
|
|
Some(&NodeContact::Failure(_)) => {
|
|
|
|
failures.push(node);
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
unknown.push(node);
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
2018-05-07 13:11:12 +02:00
|
|
|
}
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-05-07 13:11:12 +02:00
|
|
|
success.sort_by(|a, b| {
|
|
|
|
let a = a
|
|
|
|
.last_contact
|
|
|
|
.expect("vector only contains values with defined last_contact; qed");
|
|
|
|
let b = b
|
|
|
|
.last_contact
|
|
|
|
.expect("vector only contains values with defined last_contact; qed");
|
|
|
|
// inverse ordering, most recent successes come first
|
|
|
|
b.time().cmp(&a.time())
|
2018-01-31 09:50:01 +01:00
|
|
|
});
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-05-07 13:11:12 +02:00
|
|
|
failures.sort_by(|a, b| {
|
|
|
|
let a = a
|
|
|
|
.last_contact
|
|
|
|
.expect("vector only contains values with defined last_contact; qed");
|
|
|
|
let b = b
|
|
|
|
.last_contact
|
|
|
|
.expect("vector only contains values with defined last_contact; qed");
|
|
|
|
// normal ordering, most distant failures come first
|
|
|
|
a.time().cmp(&b.time())
|
|
|
|
});
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-05-07 13:11:12 +02:00
|
|
|
rand::thread_rng().shuffle(&mut unknown);
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-05-07 13:11:12 +02:00
|
|
|
success.append(&mut unknown);
|
|
|
|
success.append(&mut failures);
|
|
|
|
success
|
2018-03-16 22:39:23 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-03-16 22:39:23 +01:00
|
|
|
/// Returns node ids sorted by failure percentage, for nodes with the same failure percentage the absolute number of
|
|
|
|
/// failures is considered.
|
2018-05-31 13:38:46 +02:00
|
|
|
pub fn nodes(&self, filter: &IpFilter) -> Vec<NodeId> {
|
2018-03-16 22:39:23 +01:00
|
|
|
self.ordered_entries()
|
|
|
|
.iter()
|
|
|
|
.filter(|n| n.endpoint.is_allowed(&filter))
|
|
|
|
.map(|n| n.id)
|
|
|
|
.collect()
|
2016-02-12 09:52:32 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-03-16 22:39:23 +01:00
|
|
|
/// Ordered list of all entries by failure percentage, for nodes with the same failure percentage the absolute
|
|
|
|
/// number of failures is considered.
|
|
|
|
pub fn entries(&self) -> Vec<NodeEntry> {
|
|
|
|
self.ordered_entries()
|
|
|
|
.iter()
|
|
|
|
.map(|n| NodeEntry {
|
2018-01-31 09:50:01 +01:00
|
|
|
endpoint: n.endpoint.clone(),
|
2018-05-31 13:38:46 +02:00
|
|
|
id: n.id,
|
2018-01-31 09:50:01 +01:00
|
|
|
})
|
|
|
|
.collect()
|
2016-02-15 20:28:27 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-02-14 01:03:48 +01:00
|
|
|
/// Get particular node
|
2016-02-12 09:52:32 +01:00
|
|
|
pub fn get_mut(&mut self, id: &NodeId) -> Option<&mut Node> {
|
|
|
|
self.nodes.get_mut(id)
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-07-19 15:44:08 +02:00
|
|
|
/// Check if a node exists in the table.
|
|
|
|
pub fn contains(&self, id: &NodeId) -> bool {
|
|
|
|
self.nodes.contains_key(id)
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-02-14 01:03:48 +01:00
|
|
|
/// Apply table changes coming from discovery
|
2016-07-14 10:38:53 +02:00
|
|
|
pub fn update(&mut self, mut update: TableUpdates, reserved: &HashSet<NodeId>) {
|
2016-02-13 22:57:39 +01:00
|
|
|
for (_, node) in update.added.drain() {
|
2018-05-31 13:38:46 +02:00
|
|
|
let entry = self
|
|
|
|
.nodes
|
|
|
|
.entry(node.id)
|
|
|
|
.or_insert_with(|| Node::new(node.id, node.endpoint.clone()));
|
2016-02-13 22:57:39 +01:00
|
|
|
entry.endpoint = node.endpoint;
|
|
|
|
}
|
2016-02-12 09:52:32 +01:00
|
|
|
for r in update.removed {
|
2016-07-14 10:38:53 +02:00
|
|
|
if !reserved.contains(&r) {
|
|
|
|
self.nodes.remove(&r);
|
2016-02-12 09:52:32 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-07 13:11:12 +02:00
|
|
|
/// Set last contact as failure for a node
|
2016-02-14 01:03:48 +01:00
|
|
|
pub fn note_failure(&mut self, id: &NodeId) {
|
|
|
|
if let Some(node) = self.nodes.get_mut(id) {
|
2018-05-07 13:11:12 +02:00
|
|
|
node.last_contact = Some(NodeContact::failure());
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
|
|
|
|
2018-05-07 13:11:12 +02:00
|
|
|
/// Set last contact as success for a node
|
|
|
|
pub fn note_success(&mut self, id: &NodeId) {
|
|
|
|
if let Some(node) = self.nodes.get_mut(id) {
|
|
|
|
node.last_contact = Some(NodeContact::success());
|
2016-02-14 01:03:48 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
|
|
|
|
2018-01-31 09:50:01 +01:00
|
|
|
/// Mark as useless, no further attempts to connect until next call to `clear_useless`.
|
2016-05-02 14:48:30 +02:00
|
|
|
pub fn mark_as_useless(&mut self, id: &NodeId) {
|
|
|
|
self.useless_nodes.insert(id.clone());
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-08-17 16:04:03 +02:00
|
|
|
/// Attempt to connect to useless nodes again.
|
2016-05-02 14:48:30 +02:00
|
|
|
pub fn clear_useless(&mut self) {
|
|
|
|
self.useless_nodes.clear();
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-10-11 18:42:20 +02:00
|
|
|
/// Save the nodes.json file.
|
|
|
|
pub fn save(&self) {
|
2018-01-31 09:50:01 +01:00
|
|
|
let mut path = match self.path {
|
|
|
|
Some(ref path) => PathBuf::from(path),
|
|
|
|
None => return,
|
|
|
|
};
|
|
|
|
if let Err(e) = fs::create_dir_all(&path) {
|
2018-09-14 22:18:03 +02:00
|
|
|
warn!(target: "network", "Error creating node table directory: {:?}", e);
|
2018-01-31 09:50:01 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
path.push(NODES_FILE);
|
2018-05-31 13:38:46 +02:00
|
|
|
let node_ids = self.nodes(&IpFilter::default());
|
2018-01-31 09:50:01 +01:00
|
|
|
let nodes = node_ids
|
|
|
|
.into_iter()
|
|
|
|
.map(|id| {
|
|
|
|
self.nodes
|
|
|
|
.get(&id)
|
|
|
|
.expect("self.nodes() only returns node IDs from self.nodes")
|
2020-08-05 06:08:03 +02:00
|
|
|
})
|
2018-01-31 09:50:01 +01:00
|
|
|
.take(MAX_NODES)
|
|
|
|
.map(Into::into)
|
|
|
|
.collect();
|
|
|
|
let table = json::NodeTable { nodes };
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-01-31 09:50:01 +01:00
|
|
|
match fs::File::create(&path) {
|
|
|
|
Ok(file) => {
|
|
|
|
if let Err(e) = serde_json::to_writer_pretty(file, &table) {
|
2018-09-14 22:18:03 +02:00
|
|
|
warn!(target: "network", "Error writing node table file: {:?}", e);
|
2016-02-15 11:54:38 +01:00
|
|
|
}
|
2018-01-31 09:50:01 +01:00
|
|
|
}
|
|
|
|
Err(e) => {
|
2018-09-14 22:18:03 +02:00
|
|
|
warn!(target: "network", "Error creating node table file: {:?}", e);
|
2016-02-15 11:54:38 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-15 11:54:38 +01:00
|
|
|
fn load(path: Option<String>) -> HashMap<NodeId, Node> {
|
2018-01-31 09:50:01 +01:00
|
|
|
let path = match path {
|
|
|
|
Some(path) => PathBuf::from(path).join(NODES_FILE),
|
|
|
|
None => return Default::default(),
|
|
|
|
};
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-01-31 09:50:01 +01:00
|
|
|
let file = match fs::File::open(&path) {
|
|
|
|
Ok(file) => file,
|
|
|
|
Err(e) => {
|
2018-09-14 22:18:03 +02:00
|
|
|
debug!(target: "network", "Error opening node table file: {:?}", e);
|
2018-01-31 09:50:01 +01:00
|
|
|
return Default::default();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let res: Result<json::NodeTable, _> = serde_json::from_reader(file);
|
|
|
|
match res {
|
|
|
|
Ok(table) => table
|
|
|
|
.nodes
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|n| n.into_node())
|
2018-05-31 13:38:46 +02:00
|
|
|
.map(|n| (n.id, n))
|
2018-01-31 09:50:01 +01:00
|
|
|
.collect(),
|
|
|
|
Err(e) => {
|
2018-09-14 22:18:03 +02:00
|
|
|
warn!(target: "network", "Error reading node table file: {:?}", e);
|
2018-01-31 09:50:01 +01:00
|
|
|
Default::default()
|
|
|
|
}
|
2016-02-15 11:54:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for NodeTable {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.save();
|
|
|
|
}
|
2016-02-14 01:03:48 +01:00
|
|
|
}
|
|
|
|
|
2016-02-19 16:34:31 +01:00
|
|
|
/// Check if node url is valid
|
2017-11-13 14:37:08 +01:00
|
|
|
pub fn validate_node_url(url: &str) -> Option<Error> {
|
2017-10-31 05:09:31 +01:00
|
|
|
match Node::from_str(url) {
|
|
|
|
Ok(_) => None,
|
|
|
|
Err(e) => Some(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-31 09:50:01 +01:00
|
|
|
mod json {
|
|
|
|
use super::*;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-01-31 09:50:01 +01:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct NodeTable {
|
|
|
|
pub nodes: Vec<Node>,
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-05-07 13:11:12 +02:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub enum NodeContact {
|
|
|
|
#[serde(rename = "success")]
|
|
|
|
Success(u64),
|
|
|
|
#[serde(rename = "failure")]
|
|
|
|
Failure(u64),
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-05-07 13:11:12 +02:00
|
|
|
impl NodeContact {
|
|
|
|
pub fn into_node_contact(self) -> super::NodeContact {
|
|
|
|
match self {
|
|
|
|
NodeContact::Success(s) => {
|
|
|
|
super::NodeContact::Success(time::UNIX_EPOCH + Duration::from_secs(s))
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
2018-05-07 13:11:12 +02:00
|
|
|
NodeContact::Failure(s) => {
|
|
|
|
super::NodeContact::Failure(time::UNIX_EPOCH + Duration::from_secs(s))
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
|
|
|
}
|
2018-05-07 13:11:12 +02:00
|
|
|
}
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-01-31 09:50:01 +01:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct Node {
|
|
|
|
pub url: String,
|
2018-05-07 13:11:12 +02:00
|
|
|
pub last_contact: Option<NodeContact>,
|
2018-01-31 09:50:01 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-01-31 09:50:01 +01:00
|
|
|
impl Node {
|
|
|
|
pub fn into_node(self) -> Option<super::Node> {
|
|
|
|
match super::Node::from_str(&self.url) {
|
|
|
|
Ok(mut node) => {
|
2018-05-07 13:11:12 +02:00
|
|
|
node.last_contact = self.last_contact.map(|c| c.into_node_contact());
|
2018-01-31 09:50:01 +01:00
|
|
|
Some(node)
|
|
|
|
}
|
|
|
|
_ => None,
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
2018-01-31 09:50:01 +01:00
|
|
|
}
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-01-31 09:50:01 +01:00
|
|
|
impl<'a> From<&'a super::Node> for Node {
|
|
|
|
fn from(node: &'a super::Node) -> Self {
|
2018-05-07 13:11:12 +02:00
|
|
|
let last_contact = node.last_contact.and_then(|c| match c {
|
|
|
|
super::NodeContact::Success(t) => t
|
|
|
|
.duration_since(time::UNIX_EPOCH)
|
|
|
|
.ok()
|
|
|
|
.map(|d| NodeContact::Success(d.as_secs())),
|
|
|
|
super::NodeContact::Failure(t) => t
|
|
|
|
.duration_since(time::UNIX_EPOCH)
|
|
|
|
.ok()
|
|
|
|
.map(|d| NodeContact::Failure(d.as_secs())),
|
|
|
|
});
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-01-31 09:50:01 +01:00
|
|
|
Node {
|
|
|
|
url: format!("{}", node),
|
2018-05-07 13:11:12 +02:00
|
|
|
last_contact,
|
2018-01-31 09:50:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-14 01:03:48 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2018-01-10 13:35:18 +01:00
|
|
|
use ethereum_types::H512;
|
2016-11-28 17:05:37 +01:00
|
|
|
use ipnetwork::IpNetwork;
|
|
|
|
use std::{
|
|
|
|
net::{Ipv4Addr, SocketAddr, SocketAddrV4},
|
2016-02-14 01:03:48 +01:00
|
|
|
str::FromStr,
|
|
|
|
};
|
2017-10-20 12:11:34 +02:00
|
|
|
use tempdir::TempDir;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-02-14 01:03:48 +01:00
|
|
|
#[test]
|
|
|
|
fn endpoint_parse() {
|
|
|
|
let endpoint = NodeEndpoint::from_str("123.99.55.44:7770");
|
|
|
|
assert!(endpoint.is_ok());
|
|
|
|
let v4 = match endpoint.unwrap().address {
|
|
|
|
SocketAddr::V4(v4address) => v4address,
|
2018-05-09 15:58:02 +02:00
|
|
|
_ => panic!("should be v4 address"),
|
2016-02-14 01:03:48 +01:00
|
|
|
};
|
|
|
|
assert_eq!(SocketAddrV4::new(Ipv4Addr::new(123, 99, 55, 44), 7770), v4);
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-05-09 15:58:02 +02:00
|
|
|
#[test]
|
|
|
|
fn endpoint_parse_empty_ip_string_returns_error() {
|
|
|
|
let endpoint = NodeEndpoint::from_str("");
|
|
|
|
assert!(endpoint.is_err());
|
|
|
|
assert_matches!(endpoint.unwrap_err().kind(), &ErrorKind::AddressParse);
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-05-09 15:58:02 +02:00
|
|
|
#[test]
|
|
|
|
fn endpoint_parse_invalid_ip_string_returns_error() {
|
|
|
|
let endpoint = NodeEndpoint::from_str("beef");
|
|
|
|
assert!(endpoint.is_err());
|
|
|
|
assert_matches!(endpoint.unwrap_err().kind(), &ErrorKind::AddressParse);
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-05-09 15:58:02 +02:00
|
|
|
#[test]
|
|
|
|
fn endpoint_parse_valid_ip_without_port_returns_error() {
|
|
|
|
let endpoint = NodeEndpoint::from_str("123.123.123.123");
|
|
|
|
assert!(endpoint.is_err());
|
|
|
|
assert_matches!(endpoint.unwrap_err().kind(), &ErrorKind::AddressParse);
|
|
|
|
let endpoint = NodeEndpoint::from_str("123.123.123.123:123");
|
|
|
|
assert!(endpoint.is_ok())
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-02-14 01:03:48 +01:00
|
|
|
#[test]
|
|
|
|
fn node_parse() {
|
2017-11-06 07:51:26 +01:00
|
|
|
assert!(validate_node_url("enode://a979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c@22.99.55.44:7770").is_none());
|
2016-02-14 01:03:48 +01:00
|
|
|
let node = Node::from_str("enode://a979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c@22.99.55.44:7770");
|
|
|
|
assert!(node.is_ok());
|
|
|
|
let node = node.unwrap();
|
|
|
|
let v4 = match node.endpoint.address {
|
|
|
|
SocketAddr::V4(v4address) => v4address,
|
|
|
|
_ => panic!("should ve v4 address"),
|
|
|
|
};
|
|
|
|
assert_eq!(SocketAddrV4::new(Ipv4Addr::new(22, 99, 55, 44), 7770), v4);
|
|
|
|
assert_eq!(
|
|
|
|
H512::from_str("a979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c").unwrap(),
|
|
|
|
node.id);
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-05-09 15:58:02 +02:00
|
|
|
#[test]
|
|
|
|
fn node_parse_fails_for_invalid_urls() {
|
|
|
|
let node = Node::from_str("foo");
|
|
|
|
assert!(node.is_err());
|
|
|
|
assert_matches!(node.unwrap_err().kind(), &ErrorKind::AddressParse);
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-05-09 15:58:02 +02:00
|
|
|
let node = Node::from_str("enode://foo@bar");
|
|
|
|
assert!(node.is_err());
|
|
|
|
assert_matches!(node.unwrap_err().kind(), &ErrorKind::AddressParse);
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-02-15 11:54:38 +01:00
|
|
|
#[test]
|
2018-05-07 13:11:12 +02:00
|
|
|
fn table_last_contact_order() {
|
2016-02-15 14:39:56 +01:00
|
|
|
let node1 = Node::from_str("enode://a979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c@22.99.55.44:7770").unwrap();
|
|
|
|
let node2 = Node::from_str("enode://b979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c@22.99.55.44:7770").unwrap();
|
|
|
|
let node3 = Node::from_str("enode://c979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c@22.99.55.44:7770").unwrap();
|
2018-01-31 09:50:01 +01:00
|
|
|
let node4 = Node::from_str("enode://d979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c@22.99.55.44:7770").unwrap();
|
2018-05-07 13:11:12 +02:00
|
|
|
let node5 = Node::from_str("enode://e979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c@22.99.55.44:7770").unwrap();
|
|
|
|
let node6 = Node::from_str("enode://f979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c@22.99.55.44:7770").unwrap();
|
2016-02-15 11:54:38 +01:00
|
|
|
let id1 = H512::from_str("a979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c").unwrap();
|
|
|
|
let id2 = H512::from_str("b979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c").unwrap();
|
2016-02-15 14:39:56 +01:00
|
|
|
let id3 = H512::from_str("c979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c").unwrap();
|
2018-01-31 09:50:01 +01:00
|
|
|
let id4 = H512::from_str("d979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c").unwrap();
|
2018-05-07 13:11:12 +02:00
|
|
|
let id5 = H512::from_str("e979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c").unwrap();
|
|
|
|
let id6 = H512::from_str("f979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c").unwrap();
|
2016-02-15 11:54:38 +01:00
|
|
|
let mut table = NodeTable::new(None);
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-02-15 14:39:56 +01:00
|
|
|
table.add_node(node1);
|
|
|
|
table.add_node(node2);
|
2018-01-31 09:50:01 +01:00
|
|
|
table.add_node(node3);
|
|
|
|
table.add_node(node4);
|
2018-05-07 13:11:12 +02:00
|
|
|
table.add_node(node5);
|
|
|
|
table.add_node(node6);
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-05-07 13:11:12 +02:00
|
|
|
// failures - nodes 1 & 2
|
2016-02-15 14:39:56 +01:00
|
|
|
table.note_failure(&id1);
|
|
|
|
table.note_failure(&id2);
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-05-07 13:11:12 +02:00
|
|
|
// success - nodes 3 & 4
|
|
|
|
table.note_success(&id3);
|
|
|
|
table.note_success(&id4);
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-05-07 13:11:12 +02:00
|
|
|
// success - node 5 (old contact)
|
|
|
|
table.get_mut(&id5).unwrap().last_contact = Some(NodeContact::Success(time::UNIX_EPOCH));
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-05-07 13:11:12 +02:00
|
|
|
// unknown - node 6
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-09-24 22:51:16 +02:00
|
|
|
// nodes are also ordered according to their addition time
|
|
|
|
//
|
|
|
|
// nanosecond precision lost since mac os x high sierra update so let's not compare their order
|
|
|
|
// https://github.com/paritytech/parity-ethereum/issues/9632
|
2018-05-31 13:38:46 +02:00
|
|
|
let r = table.nodes(&IpFilter::default());
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-09-24 22:51:16 +02:00
|
|
|
// most recent success
|
|
|
|
assert!((r[0] == id4 && r[1] == id3) || (r[0] == id3 && r[1] == id4));
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-05-07 13:11:12 +02:00
|
|
|
// unknown (old contacts and new nodes), randomly shuffled
|
2018-09-24 22:51:16 +02:00
|
|
|
assert!((r[2] == id5 && r[3] == id6) || (r[2] == id6 && r[3] == id5));
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-05-07 13:11:12 +02:00
|
|
|
// oldest failure
|
2018-09-24 22:51:16 +02:00
|
|
|
assert!((r[4] == id1 && r[5] == id2) || (r[4] == id2 && r[5] == id1));
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-02-15 14:39:56 +01:00
|
|
|
fn table_save_load() {
|
2018-05-07 13:11:12 +02:00
|
|
|
let tempdir = TempDir::new("").unwrap();
|
2016-02-15 14:39:56 +01:00
|
|
|
let node1 = Node::from_str("enode://a979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c@22.99.55.44:7770").unwrap();
|
|
|
|
let node2 = Node::from_str("enode://b979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c@22.99.55.44:7770").unwrap();
|
2018-09-24 22:51:16 +02:00
|
|
|
let node3 = Node::from_str("enode://c979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c@22.99.55.44:7770").unwrap();
|
|
|
|
let id1 = H512::from_str("a979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c").unwrap();
|
|
|
|
let id2 = H512::from_str("b979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c").unwrap();
|
|
|
|
let id3 = H512::from_str("c979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c").unwrap();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-02-15 14:39:56 +01:00
|
|
|
{
|
2017-10-20 12:11:34 +02:00
|
|
|
let mut table = NodeTable::new(Some(tempdir.path().to_str().unwrap().to_owned()));
|
2016-02-15 14:39:56 +01:00
|
|
|
table.add_node(node1);
|
|
|
|
table.add_node(node2);
|
2018-05-07 13:11:12 +02:00
|
|
|
table.add_node(node3);
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-02-15 14:39:56 +01:00
|
|
|
table.note_success(&id2);
|
2018-05-07 13:11:12 +02:00
|
|
|
table.note_failure(&id3);
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
|
|
|
|
2016-02-15 14:39:56 +01:00
|
|
|
{
|
2017-10-20 12:11:34 +02:00
|
|
|
let table = NodeTable::new(Some(tempdir.path().to_str().unwrap().to_owned()));
|
2016-02-15 14:39:56 +01:00
|
|
|
let r = table.nodes(&IpFilter::default());
|
|
|
|
assert_eq!(r[0][..], id2[..]); // latest success
|
2018-05-07 13:11:12 +02:00
|
|
|
assert_eq!(r[1][..], id1[..]); // unknown
|
|
|
|
assert_eq!(r[2][..], id3[..]); // oldest failure
|
2016-02-15 14:39:56 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2017-10-20 12:11:34 +02:00
|
|
|
fn custom_allow() {
|
|
|
|
let filter = IpFilter {
|
|
|
|
predefined: AllowIP::None,
|
|
|
|
custom_allow: vec![
|
|
|
|
IpNetwork::from_str(&"10.0.0.0/8").unwrap(),
|
|
|
|
IpNetwork::from_str(&"1.0.0.0/8").unwrap(),
|
2020-08-05 06:08:03 +02:00
|
|
|
],
|
2017-10-20 12:11:34 +02:00
|
|
|
custom_block: vec![],
|
|
|
|
};
|
2018-05-31 13:38:46 +02:00
|
|
|
assert!(!NodeEndpoint::from_str("123.99.55.44:7770")
|
|
|
|
.unwrap()
|
|
|
|
.is_allowed(&filter));
|
2018-05-07 13:11:12 +02:00
|
|
|
assert!(NodeEndpoint::from_str("10.0.0.1:7770")
|
|
|
|
.unwrap()
|
|
|
|
.is_allowed(&filter));
|
|
|
|
assert!(NodeEndpoint::from_str("1.0.0.55:5550")
|
2020-08-05 06:08:03 +02:00
|
|
|
.unwrap()
|
2018-05-07 13:11:12 +02:00
|
|
|
.is_allowed(&filter));
|
2016-02-15 11:54:38 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-07-28 19:06:39 +02:00
|
|
|
#[test]
|
|
|
|
fn custom_block() {
|
|
|
|
let filter = IpFilter {
|
|
|
|
predefined: AllowIP::All,
|
|
|
|
custom_allow: vec![],
|
|
|
|
custom_block: vec![
|
|
|
|
IpNetwork::from_str(&"10.0.0.0/8").unwrap(),
|
|
|
|
IpNetwork::from_str(&"1.0.0.0/8").unwrap(),
|
|
|
|
],
|
|
|
|
};
|
|
|
|
assert!(NodeEndpoint::from_str("123.99.55.44:7770")
|
|
|
|
.unwrap()
|
|
|
|
.is_allowed(&filter));
|
|
|
|
assert!(!NodeEndpoint::from_str("10.0.0.1:7770")
|
|
|
|
.unwrap()
|
|
|
|
.is_allowed(&filter));
|
|
|
|
assert!(!NodeEndpoint::from_str("1.0.0.55:5550")
|
|
|
|
.unwrap()
|
|
|
|
.is_allowed(&filter));
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-07-28 19:06:39 +02:00
|
|
|
#[test]
|
|
|
|
fn custom_allow_ipv6() {
|
|
|
|
let filter = IpFilter {
|
|
|
|
predefined: AllowIP::None,
|
|
|
|
custom_allow: vec![IpNetwork::from_str(&"fc00::/8").unwrap()],
|
|
|
|
custom_block: vec![],
|
|
|
|
};
|
|
|
|
assert!(NodeEndpoint::from_str("[fc00::]:5550")
|
|
|
|
.unwrap()
|
|
|
|
.is_allowed(&filter));
|
|
|
|
assert!(!NodeEndpoint::from_str("[fd00::]:5550")
|
|
|
|
.unwrap()
|
|
|
|
.is_allowed(&filter));
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-07-28 19:06:39 +02:00
|
|
|
#[test]
|
|
|
|
fn custom_block_ipv6() {
|
|
|
|
let filter = IpFilter {
|
|
|
|
predefined: AllowIP::All,
|
|
|
|
custom_allow: vec![],
|
|
|
|
custom_block: vec![IpNetwork::from_str(&"fc00::/8").unwrap()],
|
|
|
|
};
|
|
|
|
assert!(!NodeEndpoint::from_str("[fc00::]:5550")
|
|
|
|
.unwrap()
|
|
|
|
.is_allowed(&filter));
|
|
|
|
assert!(NodeEndpoint::from_str("[fd00::]:5550")
|
|
|
|
.unwrap()
|
|
|
|
.is_allowed(&filter));
|
|
|
|
}
|
2016-02-12 09:52:32 +01:00
|
|
|
}
|