Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
[devp2p] Fix warnings and re-org imports (#10710)
Browse files Browse the repository at this point in the history
* Run cargo fix

* Optimize imports

* Reorg imports

* Put TODO back in place
  • Loading branch information
dvdplm authored Jun 3, 2019
1 parent dae5d75 commit 425dcd4
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 145 deletions.
28 changes: 16 additions & 12 deletions util/network-devp2p/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,28 @@
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.

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 bytes::{Buf, BufMut};
use ethereum_types::{H128, H256, H512};
use hash::{keccak, write_keccak};
use mio::{Token, Ready, PollOpt};
use mio::deprecated::{Handler, EventLoop, TryRead, TryWrite};
use mio::{PollOpt, Ready, Token};
use mio::deprecated::{EventLoop, Handler, 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::blockmodes::*;
use rcrypto::buffer::*;
use rcrypto::symmetriccipher::*;
use rlp::{Rlp, RlpStream};
use tiny_keccak::Keccak;
use bytes::{Buf, BufMut};

use ethkey::crypto;
use handshake::Handshake;
use io::{IoContext, StreamToken};
use network::{Error, ErrorKind};

const ENCRYPTED_HEADER_LEN: usize = 32;
Expand Down Expand Up @@ -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 {
Expand Down
29 changes: 16 additions & 13 deletions util/network-devp2p/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,22 @@
// You should have received a copy of the GNU General Public License
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.

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 hash::keccak;
use lru_cache::LruCache;
use parity_bytes::Bytes;
use rlp::{Rlp, RlpStream};
use node_table::*;

use ethkey::{KeyPair, recover, Secret, sign};
use network::{Error, ErrorKind};
use ethkey::{Secret, KeyPair, sign, recover};
use network::IpFilter;

use node_table::*;
use PROTOCOL_VERSION;

const ADDRESS_BYTES_SIZE: usize = 32; // Size of address type in bytes.
Expand Down Expand Up @@ -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() {
Expand Down
35 changes: 20 additions & 15 deletions util/network-devp2p/src/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.

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 {
Expand Down Expand Up @@ -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;

fn check_auth(h: &Handshake, version: u64) {
use std::str::FromStr;

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(),
Expand Down
45 changes: 23 additions & 22 deletions util/network-devp2p/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,42 @@
// You should have received a copy of the GNU General Public License
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.

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 parity_path::restrict_permissions_owner;
use parking_lot::{Mutex, RwLock};
use rlp::{Encodable, RlpStream};
use rustc_hex::ToHex;

use session::{Session, SessionData};
use connection::PAYLOAD_SOFT_LIMIT;
use discovery::{Discovery, MAX_DATAGRAM_SIZE, NodeEntry, TableUpdates};
use ethkey::{Generator, KeyPair, Random, Secret};
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 connection::PAYLOAD_SOFT_LIMIT;
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<T> = ::slab::Slab<T, usize>;

Expand Down
19 changes: 11 additions & 8 deletions util/network-devp2p/src/ip_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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};

use libc::{AF_INET, AF_INET6};
use libc::{freeifaddrs, getifaddrs, ifaddrs, sockaddr, sockaddr_in, sockaddr_in6};

fn convert_sockaddr(sa: *mut sockaddr) -> Option<IpAddr> {
fn convert_sockaddr(sa: *mut sockaddr) -> Option<IpAddr> {
if sa.is_null() { return None; }

let (addr, _) = match i32::from(unsafe { *sa }.sa_family) {
Expand Down
66 changes: 32 additions & 34 deletions util/network-devp2p/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,45 +60,49 @@
//TODO: use Poll from mio
#![allow(deprecated)]

//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 parity_bytes;
extern crate parity_crypto as crypto;
extern crate ethcore_network as network;
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 igd;
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;
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;

#[cfg(test)]
extern crate env_logger;
extern crate serde_json;
extern crate slab;
#[cfg(test)]
extern crate tempdir;
#[cfg(test)] #[macro_use]
extern crate assert_matches;
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;
Expand All @@ -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;
Loading

0 comments on commit 425dcd4

Please sign in to comment.