From 425dcd45c23e3f257242b9de3342d779489555f5 Mon Sep 17 00:00:00 2001 From: David Date: Mon, 3 Jun 2019 19:21:29 +0200 Subject: [PATCH] [devp2p] Fix warnings and re-org imports (#10710) * Run cargo fix * Optimize imports * Reorg imports * Put TODO back in place --- util/network-devp2p/src/connection.rs | 38 ++++++------ util/network-devp2p/src/discovery.rs | 35 ++++++----- util/network-devp2p/src/handshake.rs | 33 ++++++----- util/network-devp2p/src/host.rs | 47 +++++++-------- util/network-devp2p/src/ip_utils.rs | 19 +++--- util/network-devp2p/src/lib.rs | 84 +++++++++++++-------------- util/network-devp2p/src/node_table.rs | 30 +++++----- util/network-devp2p/src/service.rs | 22 +++---- util/network-devp2p/src/session.rs | 29 ++++----- util/network-devp2p/tests/tests.rs | 18 +++--- 10 files changed, 189 insertions(+), 166 deletions(-) diff --git a/util/network-devp2p/src/connection.rs b/util/network-devp2p/src/connection.rs index 07b14622a..424e7e176 100644 --- a/util/network-devp2p/src/connection.rs +++ b/util/network-devp2p/src/connection.rs @@ -15,26 +15,28 @@ // along with Parity Ethereum. If not, see . use std::collections::VecDeque; +use std::io::{self, Cursor, Read, Write}; use std::net::SocketAddr; use std::sync::atomic::{AtomicBool, Ordering as AtomicOrdering}; use std::time::Duration; -use hash::{keccak, write_keccak}; -use mio::{Token, Ready, PollOpt}; -use mio::deprecated::{Handler, EventLoop, TryRead, TryWrite}; -use mio::tcp::*; -use ethereum_types::{H128, H256, H512}; -use parity_bytes::*; -use rlp::{Rlp, RlpStream}; -use std::io::{self, Cursor, Read, Write}; -use io::{IoContext, StreamToken}; -use handshake::Handshake; -use rcrypto::blockmodes::*; -use rcrypto::aessafe::*; -use rcrypto::symmetriccipher::*; -use rcrypto::buffer::*; -use tiny_keccak::Keccak; + use bytes::{Buf, BufMut}; +use ethereum_types::{H128, H256, H512}; +use hash::{keccak, write_keccak}; +use mio::{PollOpt, Ready, Token}; +use mio::deprecated::{EventLoop, Handler, TryRead, TryWrite}; +use mio::tcp::*; +use parity_bytes::*; +use rcrypto::aessafe::*; +use rcrypto::blockmodes::*; +use rcrypto::buffer::*; +use rcrypto::symmetriccipher::*; +use rlp::{Rlp, RlpStream}; +use tiny_keccak::Keccak; + use ethkey::crypto; +use handshake::Handshake; +use io::{IoContext, StreamToken}; use network::{Error, ErrorKind}; const ENCRYPTED_HEADER_LEN: usize = 32; @@ -513,12 +515,14 @@ pub fn test_encryption() { mod tests { use std::cmp; use std::collections::VecDeque; - use std::io::{Read, Write, Cursor, ErrorKind, Result, Error}; + use std::io::{Cursor, Error, ErrorKind, Read, Result, Write}; use std::sync::atomic::AtomicBool; - use mio::{Ready}; + use mio::Ready; use parity_bytes::Bytes; + use io::*; + use super::*; pub struct TestSocket { diff --git a/util/network-devp2p/src/discovery.rs b/util/network-devp2p/src/discovery.rs index 262730bfd..2777505b3 100644 --- a/util/network-devp2p/src/discovery.rs +++ b/util/network-devp2p/src/discovery.rs @@ -14,21 +14,22 @@ // You should have received a copy of the GNU General Public License // along with Parity Ethereum. If not, see . -use parity_bytes::Bytes; -use std::net::SocketAddr; -use std::collections::{HashSet, HashMap, VecDeque}; +use std::collections::{HashMap, HashSet, VecDeque}; use std::collections::hash_map::Entry; use std::default::Default; +use std::net::SocketAddr; use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH}; -use lru_cache::LruCache; -use hash::keccak; -use ethereum_types::{H256, H520}; -use rlp::{Rlp, RlpStream}; -use node_table::*; -use network::{Error, ErrorKind}; -use ethkey::{Secret, KeyPair, sign, recover}; -use network::IpFilter; +use ethereum_types::{H256, H520}; +use hash::keccak; +use lru_cache::LruCache; +use parity_bytes::Bytes; +use rlp::{Rlp, RlpStream}; + +use ethkey::{KeyPair, recover, Secret, sign}; +use network::{Error, ErrorKind}; +use network::IpFilter; +use node_table::*; use PROTOCOL_VERSION; const ADDRESS_BYTES_SIZE: usize = 32; // Size of address type in bytes. @@ -878,13 +879,15 @@ where #[cfg(test)] mod tests { - use super::*; - use std::net::{IpAddr,Ipv4Addr}; - use node_table::{Node, NodeId, NodeEndpoint}; - + use std::net::{IpAddr, Ipv4Addr}; use std::str::FromStr; + use rustc_hex::FromHex; - use ethkey::{Random, Generator}; + + use ethkey::{Generator, Random}; + use node_table::{Node, NodeEndpoint, NodeId}; + + use super::*; #[test] fn find_node() { diff --git a/util/network-devp2p/src/handshake.rs b/util/network-devp2p/src/handshake.rs index e07b609b1..aee73d0e8 100644 --- a/util/network-devp2p/src/handshake.rs +++ b/util/network-devp2p/src/handshake.rs @@ -15,19 +15,21 @@ // along with Parity Ethereum. If not, see . use std::time::Duration; -use rand::random; + +use ethereum_types::{H256, H520}; use hash::write_keccak; use mio::tcp::*; -use ethereum_types::{H256, H520}; use parity_bytes::Bytes; +use rand::random; use rlp::{Rlp, RlpStream}; + use connection::Connection; -use node_table::NodeId; -use io::{IoContext, StreamToken}; -use ethkey::{KeyPair, Public, Secret, recover, sign, Generator, Random}; +use ethkey::{Generator, KeyPair, Public, Random, recover, Secret, sign}; use ethkey::crypto::{ecdh, ecies}; -use network::{Error, ErrorKind}; use host::HostInfo; +use io::{IoContext, StreamToken}; +use network::{Error, ErrorKind}; +use node_table::NodeId; #[derive(PartialEq, Eq, Debug)] enum HandshakeState { @@ -318,15 +320,18 @@ impl Handshake { #[cfg(test)] mod test { - use rustc_hex::FromHex; - use super::*; - use ethereum_types::{H256, H512}; - use io::*; - use mio::tcp::TcpStream; - use ethkey::Public; - use std::str::FromStr; + use std::str::FromStr; - fn check_auth(h: &Handshake, version: u64) { + use ethereum_types::{H256, H512}; + use mio::tcp::TcpStream; + use rustc_hex::FromHex; + + use ethkey::Public; + use io::*; + + use super::*; + + fn check_auth(h: &Handshake, version: u64) { assert_eq!( h.id, H512::from_str("fda1cff674c90c9a197539fe3dfb53086ace64f83ed7c6eabec741f7f381cc803e52ab2cd55d5569bce4347107a310dfd5f88a010cd2ffd1005ca406f1842877").unwrap(), diff --git a/util/network-devp2p/src/host.rs b/util/network-devp2p/src/host.rs index 0383aa993..cf9f6ac5b 100644 --- a/util/network-devp2p/src/host.rs +++ b/util/network-devp2p/src/host.rs @@ -14,41 +14,42 @@ // You should have received a copy of the GNU General Public License // along with Parity Ethereum. If not, see . -use std::net::{SocketAddr, SocketAddrV4, Ipv4Addr}; +use std::cmp::{max, min}; use std::collections::{HashMap, HashSet}; +use std::fs; +use std::io::{self, Read, Write}; +use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4}; +use std::ops::*; +use std::path::{Path, PathBuf}; use std::str::FromStr; use std::sync::Arc; use std::sync::atomic::{AtomicBool, Ordering as AtomicOrdering}; -use std::ops::*; -use std::cmp::{min, max}; -use std::path::{Path, PathBuf}; -use std::io::{Read, Write, self}; -use std::fs; use std::time::Duration; -use ethkey::{KeyPair, Secret, Random, Generator}; + +use ethereum_types::H256; use hash::keccak; use mio::*; -use mio::deprecated::{EventLoop}; +use mio::deprecated::EventLoop; use mio::tcp::*; use mio::udp::*; -use ethereum_types::H256; -use rlp::{RlpStream, Encodable}; -use rustc_hex::ToHex; - -use session::{Session, SessionData}; -use io::*; -use PROTOCOL_VERSION; -use node_table::*; -use network::{NetworkConfiguration, NetworkIoMessage, ProtocolId, PeerId, PacketId}; -use network::{NonReservedPeerMode, NetworkContext as NetworkContextTrait}; -use network::{SessionInfo, Error, ErrorKind, DisconnectReason, NetworkProtocolHandler}; -use discovery::{Discovery, TableUpdates, NodeEntry, MAX_DATAGRAM_SIZE}; -use network::client_version::ClientVersion; -use ip_utils::{map_external_address, select_public_address}; use parity_path::restrict_permissions_owner; use parking_lot::{Mutex, RwLock}; -use network::{ConnectionFilter, ConnectionDirection}; +use rlp::{Encodable, RlpStream}; +use rustc_hex::ToHex; + use connection::PAYLOAD_SOFT_LIMIT; +use discovery::{Discovery, MAX_DATAGRAM_SIZE, NodeEntry, TableUpdates}; +use ethkey::{Generator, KeyPair, Random, Secret}; +use io::*; +use ip_utils::{map_external_address, select_public_address}; +use network::{NetworkConfiguration, NetworkIoMessage, PacketId, PeerId, ProtocolId}; +use network::{NetworkContext as NetworkContextTrait, NonReservedPeerMode}; +use network::{DisconnectReason, Error, ErrorKind, NetworkProtocolHandler, SessionInfo}; +use network::{ConnectionDirection, ConnectionFilter}; +use network::client_version::ClientVersion; +use node_table::*; +use PROTOCOL_VERSION; +use session::{Session, SessionData}; type Slab = ::slab::Slab; diff --git a/util/network-devp2p/src/ip_utils.rs b/util/network-devp2p/src/ip_utils.rs index 4b8473cec..75ac0f530 100644 --- a/util/network-devp2p/src/ip_utils.rs +++ b/util/network-devp2p/src/ip_utils.rs @@ -16,13 +16,15 @@ // Based on original work by David Levy https://raw.githubusercontent.com/dlevy47/rust-interfaces -use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6}; use std::io; -use igd::{PortMappingProtocol, search_gateway_from_timeout}; +use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6}; use std::time::Duration; -use node_table::NodeEndpoint; + +use igd::{PortMappingProtocol, search_gateway_from_timeout}; use ipnetwork::IpNetwork; +use node_table::NodeEndpoint; + /// Socket address extension for rustc beta. To be replaces with now unstable API pub trait SocketAddrExt { /// Returns true if the address appears to be globally routable. @@ -212,12 +214,13 @@ impl SocketAddrExt for IpAddr { #[cfg(not(any(windows, target_os = "android")))] mod getinterfaces { - use std::{mem, io}; - use libc::{AF_INET, AF_INET6}; - use libc::{getifaddrs, freeifaddrs, ifaddrs, sockaddr, sockaddr_in, sockaddr_in6}; - use std::net::{Ipv4Addr, Ipv6Addr, IpAddr}; + use std::{io, mem}; + use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; - fn convert_sockaddr(sa: *mut sockaddr) -> Option { + use libc::{AF_INET, AF_INET6}; + use libc::{freeifaddrs, getifaddrs, ifaddrs, sockaddr, sockaddr_in, sockaddr_in6}; + + fn convert_sockaddr(sa: *mut sockaddr) -> Option { if sa.is_null() { return None; } let (addr, _) = match i32::from(unsafe { *sa }.sa_family) { diff --git a/util/network-devp2p/src/lib.rs b/util/network-devp2p/src/lib.rs index 3f1999bc6..a0ded7d6b 100644 --- a/util/network-devp2p/src/lib.rs +++ b/util/network-devp2p/src/lib.rs @@ -60,45 +60,49 @@ //TODO: use Poll from mio #![allow(deprecated)] -extern crate ethcore_io as io; -extern crate parity_bytes; -extern crate parity_crypto as crypto; -extern crate ethereum_types; -extern crate parking_lot; -extern crate mio; -extern crate tiny_keccak; -extern crate crypto as rcrypto; -extern crate rand; -extern crate ansi_term; //TODO: remove this -extern crate rustc_hex; -extern crate igd; -extern crate libc; -extern crate slab; -extern crate ethkey; -extern crate rlp; -extern crate bytes; -extern crate parity_path; -extern crate ethcore_network as network; -extern crate ipnetwork; -extern crate keccak_hash as hash; -extern crate serde; -extern crate serde_json; -extern crate parity_snappy as snappy; -extern crate lru_cache; - -#[macro_use] -extern crate error_chain; -#[macro_use] -extern crate log; -#[macro_use] -extern crate serde_derive; - -#[cfg(test)] -extern crate env_logger; -#[cfg(test)] -extern crate tempdir; +//TODO: remove this +extern crate ansi_term; #[cfg(test)] #[macro_use] extern crate assert_matches; +extern crate bytes; +extern crate crypto as rcrypto; +#[cfg(test)] +extern crate env_logger; +#[macro_use] +extern crate error_chain; +extern crate ethcore_io as io; +extern crate ethcore_network as network; +extern crate ethereum_types; +extern crate ethkey; +extern crate igd; +extern crate ipnetwork; +extern crate keccak_hash as hash; +extern crate libc; +#[macro_use] +extern crate log; +extern crate lru_cache; +extern crate mio; +extern crate parity_bytes; +extern crate parity_crypto as crypto; +extern crate parity_path; +extern crate parity_snappy as snappy; +extern crate parking_lot; +extern crate rand; +extern crate rlp; +extern crate rustc_hex; +extern crate serde; +#[macro_use] +extern crate serde_derive; +extern crate serde_json; +extern crate slab; +#[cfg(test)] +extern crate tempdir; +extern crate tiny_keccak; + +pub use host::NetworkContext; +pub use io::TimerToken; +pub use node_table::{MAX_NODES_IN_TABLE, NodeId, validate_node_url}; +pub use service::NetworkService; mod host; mod connection; @@ -109,10 +113,4 @@ mod service; mod node_table; mod ip_utils; -pub use service::NetworkService; -pub use host::NetworkContext; - -pub use io::TimerToken; -pub use node_table::{validate_node_url, NodeId, MAX_NODES_IN_TABLE}; - const PROTOCOL_VERSION: u32 = 5; diff --git a/util/network-devp2p/src/node_table.rs b/util/network-devp2p/src/node_table.rs index 12f5a0b66..739babfe0 100644 --- a/util/network-devp2p/src/node_table.rs +++ b/util/network-devp2p/src/node_table.rs @@ -14,22 +14,24 @@ // You should have received a copy of the GNU General Public License // along with Parity Ethereum. If not, see . -use discovery::{TableUpdates, NodeEntry}; -use ethereum_types::H512; -use ip_utils::*; -use network::{Error, ErrorKind, AllowIP, IpFilter}; -use rlp::{Rlp, RlpStream, DecoderError}; -use serde_json; +use std::{fs, slice}; use std::collections::{HashMap, HashSet}; use std::fmt::{self, Display, Formatter}; use std::hash::{Hash, Hasher}; use std::iter::FromIterator; -use std::net::{SocketAddr, ToSocketAddrs, SocketAddrV4, SocketAddrV6, Ipv4Addr, Ipv6Addr}; +use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6, ToSocketAddrs}; use std::path::PathBuf; use std::str::FromStr; -use std::{fs, slice}; use std::time::{self, Duration, SystemTime}; + +use ethereum_types::H512; use rand::{self, Rng}; +use rlp::{DecoderError, Rlp, RlpStream}; +use serde_json; + +use discovery::{NodeEntry, TableUpdates}; +use ip_utils::*; +use network::{AllowIP, Error, ErrorKind, IpFilter}; /// Node public key pub type NodeId = H512; @@ -595,14 +597,16 @@ mod json { #[cfg(test)] mod tests { - use super::*; - use std::net::{SocketAddr, SocketAddrV4, Ipv4Addr}; - use ethereum_types::H512; + use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4}; + use std::str::FromStr; use std::thread::sleep; use std::time::Duration; - use std::str::FromStr; - use tempdir::TempDir; + + use ethereum_types::H512; use ipnetwork::IpNetwork; + use tempdir::TempDir; + + use super::*; #[test] fn endpoint_parse() { diff --git a/util/network-devp2p/src/service.rs b/util/network-devp2p/src/service.rs index dfacec4be..3bf9b1da1 100644 --- a/util/network-devp2p/src/service.rs +++ b/util/network-devp2p/src/service.rs @@ -14,15 +14,17 @@ // You should have received a copy of the GNU General Public License // along with Parity Ethereum. If not, see . -use network::{Error, NetworkConfiguration, NetworkProtocolHandler, NonReservedPeerMode}; -use network::{NetworkContext, PeerId, ProtocolId, NetworkIoMessage}; -use host::Host; -use io::*; -use parking_lot::RwLock; use std::net::SocketAddr; use std::ops::RangeInclusive; use std::sync::Arc; + use ansi_term::Colour; +use parking_lot::RwLock; + +use host::Host; +use io::*; +use network::{Error, NetworkConfiguration, NetworkProtocolHandler, NonReservedPeerMode}; +use network::{NetworkContext, NetworkIoMessage, PeerId, ProtocolId}; use network::ConnectionFilter; struct HostHandler { @@ -49,12 +51,12 @@ pub struct NetworkService { host: RwLock>>, host_handler: Arc, config: NetworkConfiguration, - filter: Option>, + filter: Option>, } impl NetworkService { /// Starts IO event loop - pub fn new(config: NetworkConfiguration, filter: Option>) -> Result { + pub fn new(config: NetworkConfiguration, filter: Option>) -> Result { let host_handler = Arc::new(HostHandler { public_url: RwLock::new(None) }); let io_service = IoService::::start()?; @@ -71,7 +73,7 @@ impl NetworkService { /// Register a new protocol handler with the event loop. pub fn register_protocol( &self, - handler: Arc, + handler: Arc, protocol: ProtocolId, // version id + packet count versions: &[(u8, u8)] @@ -178,7 +180,7 @@ impl NetworkService { } /// Executes action in the network context - pub fn with_context(&self, protocol: ProtocolId, action: F) where F: FnOnce(&NetworkContext) { + pub fn with_context(&self, protocol: ProtocolId, action: F) where F: FnOnce(&dyn NetworkContext) { let io = IoContext::new(self.io_service.channel(), 0); let host = self.host.read(); if let Some(ref host) = host.as_ref() { @@ -187,7 +189,7 @@ impl NetworkService { } /// Evaluates function in the network context - pub fn with_context_eval(&self, protocol: ProtocolId, action: F) -> Option where F: FnOnce(&NetworkContext) -> T { + pub fn with_context_eval(&self, protocol: ProtocolId, action: F) -> Option where F: FnOnce(&dyn NetworkContext) -> T { let io = IoContext::new(self.io_service.channel(), 0); let host = self.host.read(); host.as_ref().map(|ref host| host.with_context_eval(protocol, &io, action)) diff --git a/util/network-devp2p/src/session.rs b/util/network-devp2p/src/session.rs index 6cecaf361..8087342a1 100644 --- a/util/network-devp2p/src/session.rs +++ b/util/network-devp2p/src/session.rs @@ -14,26 +14,27 @@ // You should have received a copy of the GNU General Public License // along with Parity Ethereum. If not, see . -use std::{str, io}; -use std::net::SocketAddr; +use std::{io, str}; use std::collections::HashMap; +use std::net::SocketAddr; use std::time::{Duration, Instant}; -use mio::*; -use mio::deprecated::{Handler, EventLoop}; -use mio::tcp::*; use ethereum_types::H256; -use rlp::{Rlp, RlpStream, EMPTY_LIST_RLP}; -use connection::{EncryptedConnection, Packet, Connection, MAX_PAYLOAD_SIZE}; -use handshake::Handshake; -use io::{IoContext, StreamToken}; -use network::{Error, ErrorKind, DisconnectReason, SessionInfo, ProtocolId, PeerCapabilityInfo}; -use network::SessionCapabilityInfo; -use network::client_version::ClientVersion; -use host::*; -use node_table::NodeId; +use mio::*; +use mio::deprecated::{EventLoop, Handler}; +use mio::tcp::*; +use rlp::{EMPTY_LIST_RLP, Rlp, RlpStream}; use snappy; +use connection::{Connection, EncryptedConnection, MAX_PAYLOAD_SIZE, Packet}; +use handshake::Handshake; +use host::*; +use io::{IoContext, StreamToken}; +use network::{DisconnectReason, Error, ErrorKind, PeerCapabilityInfo, ProtocolId, SessionInfo}; +use network::client_version::ClientVersion; +use network::SessionCapabilityInfo; +use node_table::NodeId; + // Timeout must be less than (interval - 1). const PING_TIMEOUT: Duration = Duration::from_secs(60); const PING_INTERVAL: Duration = Duration::from_secs(120); diff --git a/util/network-devp2p/tests/tests.rs b/util/network-devp2p/tests/tests.rs index 00f811e46..74094cc46 100644 --- a/util/network-devp2p/tests/tests.rs +++ b/util/network-devp2p/tests/tests.rs @@ -22,15 +22,17 @@ extern crate ethkey; extern crate parity_bytes; extern crate parking_lot; -use std::sync::atomic::{AtomicBool, Ordering as AtomicOrdering}; use std::sync::Arc; +use std::sync::atomic::{AtomicBool, Ordering as AtomicOrdering}; use std::thread; use std::time::*; -use parking_lot::Mutex; + use parity_bytes::Bytes; +use parking_lot::Mutex; + use ethcore_network::*; use ethcore_network_devp2p::NetworkService; -use ethkey::{Random, Generator}; +use ethkey::{Generator, Random}; use io::TimerToken; pub struct TestProtocol { @@ -70,16 +72,16 @@ impl TestProtocol { } impl NetworkProtocolHandler for TestProtocol { - fn initialize(&self, io: &NetworkContext) { + fn initialize(&self, io: &dyn NetworkContext) { io.register_timer(0, Duration::from_millis(10)).unwrap(); } - fn read(&self, _io: &NetworkContext, _peer: &PeerId, packet_id: u8, data: &[u8]) { + fn read(&self, _io: &dyn NetworkContext, _peer: &PeerId, packet_id: u8, data: &[u8]) { assert_eq!(packet_id, 33); self.packet.lock().extend(data); } - fn connected(&self, io: &NetworkContext, peer: &PeerId) { + fn connected(&self, io: &dyn NetworkContext, peer: &PeerId) { assert!(io.peer_client_version(*peer).to_string().contains("Parity")); if self.drop_session { io.disconnect_peer(*peer) @@ -88,12 +90,12 @@ impl NetworkProtocolHandler for TestProtocol { } } - fn disconnected(&self, _io: &NetworkContext, _peer: &PeerId) { + fn disconnected(&self, _io: &dyn NetworkContext, _peer: &PeerId) { self.got_disconnect.store(true, AtomicOrdering::Relaxed); } /// Timer function called after a timeout created with `NetworkContext::timeout`. - fn timeout(&self, _io: &NetworkContext, timer: TimerToken) { + fn timeout(&self, _io: &dyn NetworkContext, timer: TimerToken) { assert_eq!(timer, 0); self.got_timeout.store(true, AtomicOrdering::Relaxed); }