Implemented purging old disconnected peers.
This commit is contained in:
@@ -7,7 +7,7 @@ use log::{debug, error, info, trace, warn};
|
||||
use sqlite::{Connection, State, Statement};
|
||||
|
||||
use crate::{Block, Bytes, Keystore, Transaction, check_domain, get_domain_zone};
|
||||
use crate::blockchain::constants::*;
|
||||
use crate::commons::constants::*;
|
||||
use crate::blockchain::enums::{BlockQuality, MineResult};
|
||||
use crate::blockchain::enums::BlockQuality::*;
|
||||
use crate::blockchain::hash_utils::*;
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
pub use block::Block;
|
||||
pub use chain::Chain;
|
||||
pub use transaction::Transaction;
|
||||
|
||||
pub mod transaction;
|
||||
pub mod block;
|
||||
pub mod chain;
|
||||
pub mod filter;
|
||||
pub mod constants;
|
||||
pub mod hash_utils;
|
||||
pub mod enums;
|
||||
|
||||
pub use transaction::Transaction;
|
||||
pub use block::Block;
|
||||
pub use chain::Chain;
|
||||
pub use constants::*;
|
||||
@@ -13,4 +13,5 @@ pub const LOCKER_BLOCK_INTERVAL: u64 = 50;
|
||||
|
||||
pub const FULL_BLOCKS_INTERVAL: i64 = 86400; // One day in seconds
|
||||
|
||||
pub const ZONE_MAX_LENGTH: usize = 10;
|
||||
pub const ZONE_MAX_LENGTH: usize = 10;
|
||||
pub const MAX_RECONNECTS: u32 = 5;
|
||||
+4
-1
@@ -3,6 +3,9 @@ use rand::Rng;
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
use thread_priority::*;
|
||||
|
||||
pub mod constants;
|
||||
pub use constants::*;
|
||||
|
||||
/// Convert bytes array to HEX format
|
||||
pub fn to_hex(buf: &[u8]) -> String {
|
||||
let mut result = String::new();
|
||||
@@ -111,4 +114,4 @@ mod test {
|
||||
assert!(!check_domain(".ab.c", true));
|
||||
assert!(!check_domain("ab.c-", true));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -19,7 +19,7 @@ use serde::{Deserialize, Serialize};
|
||||
use crate::blockchain::hash_utils::*;
|
||||
use crate::{Context, setup_miner_thread};
|
||||
use crate::event::Event;
|
||||
use crate::blockchain::KEYSTORE_DIFFICULTY;
|
||||
use crate::commons::KEYSTORE_DIFFICULTY;
|
||||
use crate::bytes::Bytes;
|
||||
use blakeout::Blakeout;
|
||||
use self::crypto::digest::Digest;
|
||||
|
||||
+1
-1
@@ -16,7 +16,7 @@ use simple_logger::SimpleLogger;
|
||||
use winapi::um::wincon::{ATTACH_PARENT_PROCESS, AttachConsole, FreeConsole};
|
||||
|
||||
use alfis::{Block, Bytes, Chain, Miner, Context, Network, Settings, dns_utils, Keystore};
|
||||
use alfis::blockchain::BLOCK_DIFFICULTY;
|
||||
use alfis::commons::BLOCK_DIFFICULTY;
|
||||
|
||||
#[cfg(feature = "webgui")]
|
||||
mod web_ui;
|
||||
|
||||
+1
-1
@@ -10,7 +10,7 @@ use log::{debug, error, info, trace, warn};
|
||||
use num_cpus;
|
||||
|
||||
use crate::{Block, Bytes, Context, setup_miner_thread};
|
||||
use crate::blockchain::{CHAIN_VERSION, LOCKER_DIFFICULTY, KEYSTORE_DIFFICULTY};
|
||||
use crate::commons::{CHAIN_VERSION, LOCKER_DIFFICULTY, KEYSTORE_DIFFICULTY};
|
||||
use crate::blockchain::enums::BlockQuality;
|
||||
use crate::blockchain::hash_utils::*;
|
||||
use crate::keys::check_public_key_strength;
|
||||
|
||||
+2
-1
@@ -17,7 +17,7 @@ use std::net::{SocketAddr, IpAddr, SocketAddrV4, Shutdown};
|
||||
use std::collections::HashSet;
|
||||
use crate::{Context, Block, p2p::Message, p2p::State, p2p::Peer, p2p::Peers, Bytes};
|
||||
use crate::blockchain::enums::BlockQuality;
|
||||
use crate::blockchain::CHAIN_VERSION;
|
||||
use crate::commons::CHAIN_VERSION;
|
||||
|
||||
const SERVER: Token = Token(0);
|
||||
const POLL_TIMEOUT: Option<Duration> = Some(Duration::from_millis(3000));
|
||||
@@ -304,6 +304,7 @@ fn handle_message(context: Arc<Mutex<Context>>, message: Message, peers: &mut Pe
|
||||
let peer = peers.get_mut_peer(token).unwrap();
|
||||
peer.set_height(height);
|
||||
peer.set_active(true);
|
||||
peer.reset_reconnects();
|
||||
let mut context = context.lock().unwrap();
|
||||
let blocks_count = context.chain.height();
|
||||
context.bus.post(crate::event::Event::NetworkStatus { nodes: active_count + 1, blocks: blocks_count });
|
||||
|
||||
+26
-1
@@ -14,13 +14,26 @@ pub struct Peer {
|
||||
inbound: bool,
|
||||
public: bool,
|
||||
active: bool,
|
||||
reconnects: u32,
|
||||
received_block: u64,
|
||||
fork: HashMap<u64, Block>
|
||||
}
|
||||
|
||||
impl Peer {
|
||||
pub fn new(addr: SocketAddr, stream: TcpStream, state: State, inbound: bool) -> Self {
|
||||
Peer { addr, stream, state, rand: commons::random_string(6), height: 0, inbound, public: false, active: false, received_block: 0, fork: HashMap::new() }
|
||||
Peer {
|
||||
addr,
|
||||
stream,
|
||||
state,
|
||||
rand: commons::random_string(6),
|
||||
height: 0,
|
||||
inbound,
|
||||
public: false,
|
||||
active: false,
|
||||
reconnects: 0,
|
||||
received_block: 0,
|
||||
fork: HashMap::new()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_addr(&self) -> SocketAddr {
|
||||
@@ -83,6 +96,18 @@ impl Peer {
|
||||
self.active
|
||||
}
|
||||
|
||||
pub fn reconnects(&self) -> u32 {
|
||||
self.reconnects
|
||||
}
|
||||
|
||||
pub fn inc_reconnects(&mut self) {
|
||||
self.reconnects += 1;
|
||||
}
|
||||
|
||||
pub fn reset_reconnects(&mut self) {
|
||||
self.reconnects = 0;
|
||||
}
|
||||
|
||||
pub fn disabled(&self) -> bool {
|
||||
self.state.disabled()
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ use rand::seq::IteratorRandom;
|
||||
#[allow(unused_imports)]
|
||||
use log::{trace, debug, info, warn, error};
|
||||
use crate::Bytes;
|
||||
use crate::commons::MAX_RECONNECTS;
|
||||
|
||||
pub struct Peers {
|
||||
peers: HashMap<Token, Peer>,
|
||||
@@ -229,6 +230,9 @@ impl Peers {
|
||||
}
|
||||
}
|
||||
|
||||
// Remove all peers that are offline for a long time
|
||||
self.peers.retain(|_, p| { !(p.get_state().need_reconnect() && p.reconnects() >= MAX_RECONNECTS) });
|
||||
|
||||
for (token, peer) in self.peers.iter_mut() {
|
||||
if peer.get_state().need_reconnect() {
|
||||
let addr = peer.get_addr();
|
||||
@@ -237,6 +241,7 @@ impl Peers {
|
||||
info!("Created connection to peer {}", &addr);
|
||||
registry.register(&mut stream, token.clone(), Interest::WRITABLE).unwrap();
|
||||
peer.set_state(State::Connecting);
|
||||
peer.inc_reconnects();
|
||||
peer.set_stream(stream);
|
||||
}
|
||||
Err(e) => {
|
||||
|
||||
+1
-1
@@ -17,7 +17,7 @@ use alfis::miner::Miner;
|
||||
use alfis::{keys, check_domain};
|
||||
use alfis::event::Event;
|
||||
use alfis::dns::protocol::DnsRecord;
|
||||
use alfis::blockchain::{ZONE_MAX_LENGTH, ZONE_DIFFICULTY};
|
||||
use alfis::commons::{ZONE_MAX_LENGTH, ZONE_DIFFICULTY};
|
||||
use Cmd::*;
|
||||
use alfis::blockchain::transaction::{DomainData, ZoneData};
|
||||
use self::web_view::WebView;
|
||||
|
||||
Reference in New Issue
Block a user