Optimized p2p connections.
Added a limit for 10 active connections to save bandwidth and CPU.
This commit is contained in:
+1
-1
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "alfis"
|
||||
version = "0.4.24"
|
||||
version = "0.4.25"
|
||||
authors = ["Revertron <alfis@revertron.com>"]
|
||||
edition = "2018"
|
||||
build = "build.rs"
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use std::time::Duration;
|
||||
|
||||
pub const DB_VERSION: u32 = 0;
|
||||
pub const CHAIN_VERSION: u32 = 0;
|
||||
|
||||
@@ -38,3 +40,8 @@ pub const ALFIS_DEBUG: &str = "ALFIS_DEBUG";
|
||||
pub const LISTEN_PORT: u16 = 4244;
|
||||
pub const UI_REFRESH_DELAY_MS: u128 = 250;
|
||||
pub const LOG_REFRESH_DELAY_SEC: u64 = 60;
|
||||
|
||||
pub const POLL_TIMEOUT: Option<Duration> = Some(Duration::from_millis(250));
|
||||
pub const MAX_PACKET_SIZE: usize = 1 * 1024 * 1024; // 1 Mb
|
||||
pub const MAX_READ_BLOCK_TIME: u128 = 500;
|
||||
pub const MAX_NODES: usize = 10;
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
pub enum Event {
|
||||
MinerStarted,
|
||||
MinerStopped { success: bool, full: bool },
|
||||
MinerStats { thread: usize, speed: u64, max_diff: u32, aim_diff: u32 },
|
||||
MinerStats { thread: usize, speed: u64, max_diff: u32, target_diff: u32 },
|
||||
KeyGeneratorStarted,
|
||||
KeyGeneratorStopped,
|
||||
KeyCreated { path: String, public: String, hash: String },
|
||||
|
||||
+3
-3
@@ -224,7 +224,7 @@ impl Miner {
|
||||
}
|
||||
|
||||
fn find_hash(context: Arc<Mutex<Context>>, mut block: Block, running: Arc<AtomicBool>, thread: usize) -> Option<Block> {
|
||||
let difficulty = block.difficulty;
|
||||
let target_diff = block.difficulty;
|
||||
let full = block.transaction.is_some();
|
||||
let mut digest = Blakeout::new();
|
||||
let mut max_diff = 0;
|
||||
@@ -261,7 +261,7 @@ fn find_hash(context: Arc<Mutex<Context>>, mut block: Block, running: Arc<Atomic
|
||||
digest.reset();
|
||||
digest.update(&block.as_bytes());
|
||||
let diff = hash_difficulty(digest.result());
|
||||
if diff >= difficulty {
|
||||
if diff >= target_diff {
|
||||
block.hash = Bytes::from_bytes(digest.result());
|
||||
return Some(block);
|
||||
}
|
||||
@@ -276,7 +276,7 @@ fn find_hash(context: Arc<Mutex<Context>>, mut block: Block, running: Arc<Atomic
|
||||
let speed = (nonce - prev_nonce) / (elapsed as u64 / 1000);
|
||||
//debug!("Mining speed {} H/s, max difficulty {}", speed, max_diff);
|
||||
if let Ok(mut context) = context.try_lock() {
|
||||
context.bus.post(Event::MinerStats { thread, speed, max_diff, aim_diff: difficulty })
|
||||
context.bus.post(Event::MinerStats { thread, speed, max_diff, target_diff })
|
||||
}
|
||||
time = Instant::now();
|
||||
prev_nonce = nonce;
|
||||
|
||||
+8
-12
@@ -2,6 +2,7 @@ extern crate serde;
|
||||
extern crate serde_json;
|
||||
|
||||
use std::{io, thread};
|
||||
use std::cmp::max;
|
||||
use std::io::{Read, Write};
|
||||
use std::net::{IpAddr, Shutdown, SocketAddr, SocketAddrV4};
|
||||
use std::sync::{Arc, Mutex};
|
||||
@@ -19,12 +20,8 @@ use rand::random;
|
||||
use crate::{Block, Context, p2p::Message, p2p::Peer, p2p::Peers, p2p::State};
|
||||
use crate::blockchain::types::BlockQuality;
|
||||
use crate::commons::*;
|
||||
use std::cmp::max;
|
||||
|
||||
const SERVER: Token = Token(0);
|
||||
const POLL_TIMEOUT: Option<Duration> = Some(Duration::from_millis(250));
|
||||
const MAX_PACKET_SIZE: usize = 1 * 1024 * 1024; // 1 Mb
|
||||
const MAX_READ_BLOCK_TIME: u128 = 500;
|
||||
|
||||
pub struct Network {
|
||||
context: Arc<Mutex<Context>>
|
||||
@@ -147,7 +144,6 @@ impl Network {
|
||||
if !events.is_empty() {
|
||||
last_events_time = Instant::now();
|
||||
}
|
||||
events.clear();
|
||||
|
||||
if ui_timer.elapsed().as_millis() > UI_REFRESH_DELAY_MS {
|
||||
// Send pings to idle peers
|
||||
@@ -167,15 +163,15 @@ impl Network {
|
||||
}
|
||||
log_timer = Instant::now();
|
||||
}
|
||||
if nodes < MAX_NODES && connect_timer.elapsed().as_secs() >= 10 {
|
||||
peers.connect_new_peers(poll.registry(), &mut unique_token, yggdrasil_only);
|
||||
connect_timer = Instant::now();
|
||||
}
|
||||
(height, context.chain.get_last_hash())
|
||||
};
|
||||
peers.update(poll.registry(), height, hash);
|
||||
ui_timer = Instant::now();
|
||||
}
|
||||
if connect_timer.elapsed().as_secs() >= 10 {
|
||||
peers.connect_new_peers(poll.registry(), &mut unique_token, yggdrasil_only);
|
||||
connect_timer = Instant::now();
|
||||
}
|
||||
}
|
||||
if !running.load(Ordering::SeqCst) {
|
||||
info!("Network loop finished");
|
||||
@@ -401,7 +397,7 @@ fn handle_message(context: Arc<Mutex<Context>>, message: Message, peers: &mut Pe
|
||||
return State::Banned;
|
||||
}
|
||||
if ok {
|
||||
let active_count = peers.get_peers_active_count();
|
||||
let nodes = peers.get_peers_active_count();
|
||||
let peer = peers.get_mut_peer(token).unwrap();
|
||||
peer.set_height(height);
|
||||
peer.set_active(true);
|
||||
@@ -412,7 +408,7 @@ fn handle_message(context: Arc<Mutex<Context>>, message: Message, peers: &mut Pe
|
||||
let event = crate::event::Event::Syncing { have: my_height, height: max(height, my_height) };
|
||||
context.bus.post(event);
|
||||
}
|
||||
if active_count < 5 || random::<u8>() < 10 {
|
||||
if nodes < MAX_NODES && random::<bool>() {
|
||||
debug!("Requesting more peers from {}", peer.get_addr().ip());
|
||||
State::message(Message::GetPeers)
|
||||
} else {
|
||||
@@ -453,7 +449,7 @@ fn handle_message(context: Arc<Mutex<Context>>, message: Message, peers: &mut Pe
|
||||
info!("My hash: {:?}, their hash: {:?}", &my_hash, &hash);
|
||||
State::message(Message::GetBlock { index: my_height })
|
||||
} else {
|
||||
if active_count < 5 || random::<u8>() < 10 {
|
||||
if active_count < MAX_NODES && random::<u8>() < 50 {
|
||||
debug!("Requesting more peers from {}", peer.get_addr().ip());
|
||||
State::message(Message::GetPeers)
|
||||
} else {
|
||||
|
||||
+4
-3
@@ -213,14 +213,15 @@ impl Peers {
|
||||
}
|
||||
|
||||
pub fn update(&mut self, registry: &Registry, height: u64, hash: Bytes) {
|
||||
let nodes = self.get_peers_active_count();
|
||||
|
||||
let random_time = random::<u64>() % PING_PERIOD;
|
||||
for (token, peer) in self.peers.iter_mut() {
|
||||
match peer.get_state() {
|
||||
State::Idle { from } => {
|
||||
let random_time = random::<u64>() % PING_PERIOD;
|
||||
if from.elapsed().as_secs() >= PING_PERIOD + random_time {
|
||||
// Sometimes we check for new peers instead of pinging
|
||||
let random: u8 = random();
|
||||
let message = if random < 10 {
|
||||
let message = if nodes < MAX_NODES && random::<bool>() {
|
||||
Message::GetPeers
|
||||
} else {
|
||||
Message::ping(height, hash.clone())
|
||||
|
||||
+2
-2
@@ -243,13 +243,13 @@ fn action_loaded(context: &Arc<Mutex<Context>>, web_view: &mut WebView<()>) {
|
||||
}
|
||||
s
|
||||
}
|
||||
Event::MinerStats { thread, speed, max_diff, aim_diff } => {
|
||||
Event::MinerStats { thread, speed, max_diff, target_diff } => {
|
||||
if status.max_diff < max_diff {
|
||||
status.max_diff = max_diff;
|
||||
}
|
||||
status.set_thread_speed(thread, speed);
|
||||
if thread == threads - 1 {
|
||||
format!("setLeftStatusBarText('Mining speed {} H/s, max found difficulty {}/{}.'); showMiningIndicator(true, false);", status.get_speed(), status.max_diff, aim_diff)
|
||||
format!("setLeftStatusBarText('Mining speed {} H/s, max found difficulty {}/{}.'); showMiningIndicator(true, false);", status.get_speed(), status.max_diff, target_diff)
|
||||
} else {
|
||||
String::new()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user