Code reformatting.
This commit is contained in:
+3
-2
@@ -2,12 +2,13 @@ extern crate serde;
|
||||
extern crate serde_json;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::Bytes;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub enum Message {
|
||||
Error,
|
||||
Hand { app_version: String, origin: String, version: u32, public: bool, rand_id: String, },
|
||||
Hand { app_version: String, origin: String, version: u32, public: bool, rand_id: String },
|
||||
Shake { app_version: String, origin: String, version: u32, public: bool, rand_id: String, height: u64 },
|
||||
Ping { height: u64, hash: Bytes },
|
||||
Pong { height: u64, hash: Bytes },
|
||||
@@ -16,7 +17,7 @@ pub enum Message {
|
||||
GetPeers,
|
||||
Peers { peers: Vec<String> },
|
||||
GetBlock { index: u64 },
|
||||
Block { index: u64, block: Vec<u8> },
|
||||
Block { index: u64, block: Vec<u8> }
|
||||
}
|
||||
|
||||
impl Message {
|
||||
|
||||
+4
-5
@@ -1,12 +1,11 @@
|
||||
pub mod network;
|
||||
pub mod message;
|
||||
pub mod state;
|
||||
pub mod network;
|
||||
pub mod peer;
|
||||
pub mod peers;
|
||||
pub mod state;
|
||||
|
||||
pub use network::Network;
|
||||
pub use message::Message;
|
||||
pub use state::State;
|
||||
pub use network::Network;
|
||||
pub use peer::Peer;
|
||||
pub use peers::Peers;
|
||||
|
||||
pub use state::State;
|
||||
|
||||
+43
-43
@@ -1,30 +1,31 @@
|
||||
extern crate serde;
|
||||
extern crate serde_json;
|
||||
|
||||
use std::{io, thread};
|
||||
use std::cmp::max;
|
||||
use std::io::{Read, Write, Error, ErrorKind};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::io::{Error, ErrorKind, Read, Write};
|
||||
use std::net::{IpAddr, Shutdown, SocketAddr, SocketAddrV4};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::Instant;
|
||||
use std::{io, thread};
|
||||
|
||||
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
|
||||
#[allow(unused_imports)]
|
||||
use log::{debug, error, info, trace, warn};
|
||||
use mio::{Events, Interest, Poll, Registry, Token};
|
||||
use mio::event::Event;
|
||||
use mio::net::{TcpListener, TcpStream};
|
||||
use rand::{random, RngCore, Rng};
|
||||
use mio::{Events, Interest, Poll, Registry, Token};
|
||||
use rand::{random, Rng, RngCore};
|
||||
use rand_old::prelude::thread_rng;
|
||||
use x25519_dalek::{StaticSecret, PublicKey};
|
||||
use x25519_dalek::{PublicKey, StaticSecret};
|
||||
|
||||
use crate::{Block, Context, p2p::Message, p2p::Peer, p2p::Peers, p2p::State};
|
||||
use crate::blockchain::types::BlockQuality;
|
||||
use crate::commons::*;
|
||||
use crate::eventbus::{register, post};
|
||||
use crate::crypto::Chacha;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use crate::eventbus::{post, register};
|
||||
use crate::p2p::{Message, Peer, Peers, State};
|
||||
use crate::{Block, Context};
|
||||
|
||||
const SERVER: Token = Token(0);
|
||||
|
||||
@@ -36,7 +37,7 @@ pub struct Network {
|
||||
// States of peer connections, and some data to send when sockets become writable
|
||||
peers: Peers,
|
||||
// Orphan blocks from future
|
||||
future_blocks: HashMap<u64, Block>,
|
||||
future_blocks: HashMap<u64, Block>
|
||||
}
|
||||
|
||||
impl Network {
|
||||
@@ -116,7 +117,9 @@ impl Network {
|
||||
|
||||
if yggdrasil_only && !is_yggdrasil(&address.ip()) {
|
||||
debug!("Dropping connection from Internet");
|
||||
stream.shutdown(Shutdown::Both).unwrap_or_else(|e|{ warn!("Error in shutdown, {}", e); });
|
||||
stream.shutdown(Shutdown::Both).unwrap_or_else(|e| {
|
||||
warn!("Error in shutdown, {}", e);
|
||||
});
|
||||
let _ = poll.registry().reregister(&mut server, SERVER, Interest::READABLE);
|
||||
continue;
|
||||
}
|
||||
@@ -137,8 +140,8 @@ impl Network {
|
||||
if !self.handle_connection_event(&poll.registry(), &event) {
|
||||
let _ = self.peers.close_peer(poll.registry(), &token);
|
||||
let blocks = self.context.lock().unwrap().chain.get_height();
|
||||
let keys = self.context.lock().unwrap().chain.get_users_count();
|
||||
let domains = self.context.lock().unwrap().chain.get_domains_count();
|
||||
let keys = self.context.lock().unwrap().chain.get_users_count();
|
||||
let domains = self.context.lock().unwrap().chain.get_domains_count();
|
||||
post(crate::event::Event::NetworkStatus { blocks, domains, keys, nodes: self.peers.get_peers_active_count() });
|
||||
}
|
||||
}
|
||||
@@ -165,8 +168,8 @@ impl Network {
|
||||
let nodes = self.peers.get_peers_active_count();
|
||||
let banned = self.peers.get_peers_banned_count();
|
||||
|
||||
let keys = context.chain.get_users_count();
|
||||
let domains = context.chain.get_domains_count();
|
||||
let keys = context.chain.get_users_count();
|
||||
let domains = context.chain.get_domains_count();
|
||||
post(crate::event::Event::NetworkStatus { blocks, domains, keys, nodes });
|
||||
|
||||
if log_timer.elapsed().as_secs() > LOG_REFRESH_DELAY_SEC {
|
||||
@@ -237,7 +240,7 @@ impl Network {
|
||||
debug!("Error reading client handshake from {}.", peer.get_addr());
|
||||
false
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
State::ServerHandshake => {
|
||||
let mut stream = peer.get_stream();
|
||||
@@ -264,7 +267,7 @@ impl Network {
|
||||
debug!("Error reading client handshake from {}", peer.get_addr());
|
||||
false
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
_ => {
|
||||
let mut stream = peer.get_stream();
|
||||
@@ -281,9 +284,7 @@ impl Network {
|
||||
Some(peer) => {
|
||||
let data = data.unwrap();
|
||||
match decode_message(&data, peer.get_cipher()) {
|
||||
Ok(data) => {
|
||||
data
|
||||
}
|
||||
Ok(data) => data,
|
||||
Err(_) => {
|
||||
vec![]
|
||||
}
|
||||
@@ -344,8 +345,8 @@ impl Network {
|
||||
} else {
|
||||
let error = data.err().unwrap();
|
||||
let addr = match self.peers.get_peer(&event.token()) {
|
||||
None => { String::from("unknown") }
|
||||
Some(peer) => { peer.get_addr().to_string() }
|
||||
None => String::from("unknown"),
|
||||
Some(peer) => peer.get_addr().to_string()
|
||||
};
|
||||
debug!("Error reading message from {}, error = {}", addr, error);
|
||||
return false;
|
||||
@@ -475,7 +476,7 @@ impl Network {
|
||||
State::idle()
|
||||
}
|
||||
}
|
||||
Message::Error => { State::Error }
|
||||
Message::Error => State::Error,
|
||||
Message::Ping { height, hash } => {
|
||||
let peer = self.peers.get_mut_peer(token).unwrap();
|
||||
peer.set_height(height);
|
||||
@@ -546,7 +547,7 @@ impl Network {
|
||||
Ok(block) => block,
|
||||
Err(e) => {
|
||||
warn!("Error deserializing block! {}", e);
|
||||
return State::Banned
|
||||
return State::Banned;
|
||||
}
|
||||
};
|
||||
if index != block.index {
|
||||
@@ -555,8 +556,8 @@ impl Network {
|
||||
info!("Received block {} with hash {:?}", block.index, &block.hash);
|
||||
self.handle_block(token, block)
|
||||
}
|
||||
Message::Twin => { State::Twin }
|
||||
Message::Loop => { State::Loop }
|
||||
Message::Twin => State::Twin,
|
||||
Message::Loop => State::Loop
|
||||
};
|
||||
answer
|
||||
}
|
||||
@@ -658,13 +659,10 @@ fn subscribe_to_bus(running: Arc<AtomicBool>) {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
fn encode_bytes(data: &Vec<u8>, cipher: &Option<Chacha>) -> Result<Vec<u8>, chacha20poly1305::aead::Error> {
|
||||
match cipher {
|
||||
None => { Ok(data.clone()) }
|
||||
Some(chacha) => {
|
||||
chacha.encrypt(data.as_slice())
|
||||
}
|
||||
None => Ok(data.clone()),
|
||||
Some(chacha) => chacha.encrypt(data.as_slice())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -691,10 +689,8 @@ fn encode_message(message: &Message, cipher: &Option<Chacha>) -> Result<Vec<u8>,
|
||||
|
||||
fn decode_message(data: &Vec<u8>, cipher: &Option<Chacha>) -> Result<Vec<u8>, chacha20poly1305::aead::Error> {
|
||||
match cipher {
|
||||
None => { Ok(data.clone()) }
|
||||
Some(chacha) => {
|
||||
chacha.decrypt(data.as_slice())
|
||||
}
|
||||
None => Ok(data.clone()),
|
||||
Some(chacha) => chacha.decrypt(data.as_slice())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -727,22 +723,24 @@ fn send_client_handshake(stream: &mut TcpStream, public_key: &[u8]) -> io::Resul
|
||||
fn read_client_handshake(stream: &mut TcpStream) -> Result<Vec<u8>, Error> {
|
||||
// First, we read garbage size
|
||||
let data_size = match stream.read_u8() {
|
||||
Ok(size) => { (size ^ 0xA) as usize }
|
||||
Ok(size) => (size ^ 0xA) as usize,
|
||||
Err(e) => {
|
||||
error!("Error reading from socket! {}", e);
|
||||
return Err(e)
|
||||
return Err(e);
|
||||
}
|
||||
};
|
||||
// Read the garbage
|
||||
let mut buf = vec![0u8; data_size];
|
||||
match stream.read_exact(&mut buf) {
|
||||
Ok(_) => {}
|
||||
Err(e) => { return Err(e); }
|
||||
Err(e) => {
|
||||
return Err(e);
|
||||
}
|
||||
}
|
||||
// Then we have public key for ECDH
|
||||
let mut buf = vec![0u8; 32];
|
||||
match stream.read_exact(&mut buf) {
|
||||
Ok(_) => { Ok(buf) }
|
||||
Ok(_) => Ok(buf),
|
||||
Err(e) => {
|
||||
warn!("Error reading handshake!");
|
||||
Err(e)
|
||||
@@ -773,22 +771,24 @@ fn send_server_handshake(peer: &mut Peer, public_key: &[u8]) -> io::Result<()> {
|
||||
fn read_server_handshake(stream: &mut TcpStream) -> Result<Vec<u8>, Error> {
|
||||
// First, we read garbage size
|
||||
let data_size = match stream.read_u8() {
|
||||
Ok(size) => { (size ^ 0xA) as usize }
|
||||
Ok(size) => (size ^ 0xA) as usize,
|
||||
Err(e) => {
|
||||
error!("Error reading from socket! {}", e);
|
||||
return Err(e)
|
||||
return Err(e);
|
||||
}
|
||||
};
|
||||
// Read the garbage
|
||||
let mut buf = vec![0u8; data_size];
|
||||
match stream.read_exact(&mut buf) {
|
||||
Ok(_) => {}
|
||||
Err(e) => { return Err(e); }
|
||||
Err(e) => {
|
||||
return Err(e);
|
||||
}
|
||||
}
|
||||
// Then we have public key for ECDH, plus nonce 12 bytes
|
||||
let mut buf = vec![0u8; 32 + 12];
|
||||
match stream.read_exact(&mut buf) {
|
||||
Ok(_) => { Ok(buf) }
|
||||
Ok(_) => Ok(buf),
|
||||
Err(e) => {
|
||||
warn!("Error reading handshake!");
|
||||
Err(e)
|
||||
|
||||
+6
-4
@@ -1,9 +1,11 @@
|
||||
use std::net::SocketAddr;
|
||||
use std::collections::HashMap;
|
||||
use std::net::SocketAddr;
|
||||
|
||||
use mio::net::TcpStream;
|
||||
|
||||
use crate::crypto::Chacha;
|
||||
use crate::p2p::State;
|
||||
use crate::Block;
|
||||
use crate::crypto::Chacha;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Peer {
|
||||
@@ -49,8 +51,8 @@ impl Peer {
|
||||
|
||||
pub fn get_nonce(&self) -> &[u8; 12] {
|
||||
match &self.cipher {
|
||||
None => { &crate::crypto::ZERO_NONCE }
|
||||
Some(chacha) => { chacha.get_nonce() }
|
||||
None => &crate::crypto::ZERO_NONCE,
|
||||
Some(chacha) => chacha.get_nonce()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+8
-8
@@ -1,19 +1,19 @@
|
||||
use std::cmp::min;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::io;
|
||||
use std::net::{IpAddr, Shutdown, SocketAddr, ToSocketAddrs};
|
||||
|
||||
use chrono::Utc;
|
||||
#[allow(unused_imports)]
|
||||
use log::{debug, error, info, trace, warn};
|
||||
use mio::{Interest, Registry, Token};
|
||||
use mio::net::TcpStream;
|
||||
use mio::{Interest, Registry, Token};
|
||||
use rand::random;
|
||||
use rand::seq::IteratorRandom;
|
||||
|
||||
use crate::{Bytes, commons};
|
||||
use crate::commons::*;
|
||||
use crate::p2p::{Message, Peer, State};
|
||||
use std::io;
|
||||
use std::cmp::min;
|
||||
use crate::{commons, Bytes};
|
||||
|
||||
const PING_PERIOD: u64 = 30;
|
||||
|
||||
@@ -22,7 +22,7 @@ pub struct Peers {
|
||||
new_peers: Vec<SocketAddr>,
|
||||
ignored: HashSet<IpAddr>,
|
||||
my_id: String,
|
||||
behind_ping_sent_time: i64,
|
||||
behind_ping_sent_time: i64
|
||||
}
|
||||
|
||||
impl Peers {
|
||||
@@ -383,7 +383,7 @@ impl Peers {
|
||||
for peer in peers_addrs.iter() {
|
||||
info!("Resolving address {}", peer);
|
||||
let mut addresses: Vec<SocketAddr> = match peer.to_socket_addrs() {
|
||||
Ok(peers) => { peers.collect() }
|
||||
Ok(peers) => peers.collect(),
|
||||
Err(_) => { error!("Can't resolve address {}", &peer); continue; }
|
||||
};
|
||||
info!("Got addresses: {:?}", &addresses);
|
||||
@@ -424,7 +424,7 @@ impl Peers {
|
||||
}
|
||||
trace!("Connecting to peer {}", &addr);
|
||||
match TcpStream::connect(addr.clone()) {
|
||||
Ok(mut stream ) => {
|
||||
Ok(mut stream) => {
|
||||
//stream.set_nodelay(true)?;
|
||||
let token = next(unique_token);
|
||||
trace!("Created connection {}, to peer {}", &token.0, &addr);
|
||||
@@ -434,7 +434,7 @@ impl Peers {
|
||||
self.peers.insert(token, peer);
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => { Err(e) }
|
||||
Err(e) => Err(e)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+13
-12
@@ -1,4 +1,5 @@
|
||||
use std::time::Instant;
|
||||
|
||||
use crate::p2p::Message;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
@@ -14,7 +15,7 @@ pub enum State {
|
||||
SendLoop,
|
||||
Loop,
|
||||
Twin,
|
||||
Offline { from: Instant },
|
||||
Offline { from: Instant }
|
||||
}
|
||||
|
||||
impl State {
|
||||
@@ -33,34 +34,34 @@ impl State {
|
||||
|
||||
pub fn is_idle(&self) -> bool {
|
||||
match self {
|
||||
State::Idle { .. } => { true }
|
||||
_ => { false }
|
||||
State::Idle { .. } => true,
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_loop(&self) -> bool {
|
||||
match self {
|
||||
State::Loop { .. } => { true }
|
||||
State::SendLoop { .. } => { true }
|
||||
_ => { false }
|
||||
State::Loop { .. } => true,
|
||||
State::SendLoop { .. } => true,
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
|
||||
pub fn disabled(&self) -> bool {
|
||||
match self {
|
||||
State::Error => { true }
|
||||
State::Banned => { true }
|
||||
State::Offline { from} => {
|
||||
State::Error => true,
|
||||
State::Banned => true,
|
||||
State::Offline { from } => {
|
||||
from.elapsed().as_secs() < 60 // We check offline peers to become online every 5 minutes
|
||||
}
|
||||
_ => { false }
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
|
||||
pub fn need_reconnect(&self) -> bool {
|
||||
match self {
|
||||
State::Offline { from } => { from.elapsed().as_secs() > 60 }
|
||||
_ => { false }
|
||||
State::Offline { from } => from.elapsed().as_secs() > 60,
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user