From 0d944ca1dcd10128d97cba19b4a6ddab85938a77 Mon Sep 17 00:00:00 2001 From: Revertron Date: Fri, 23 Apr 2021 01:09:38 +0200 Subject: [PATCH] Fixed a problem with allowed mining while waiting for signers. Sped up initial blocks downloading. Changed block consensus a bit. --- Cargo.toml | 2 +- src/blockchain/block.rs | 17 ++--------------- src/miner.rs | 17 ++++++++--------- src/p2p/network.rs | 21 ++++++++++++++------- src/p2p/peers.rs | 33 +++++++++------------------------ 5 files changed, 34 insertions(+), 56 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6454969..6f2f452 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "alfis" -version = "0.4.23" +version = "0.4.24" authors = ["Revertron "] edition = "2018" build = "build.rs" diff --git a/src/blockchain/block.rs b/src/blockchain/block.rs index 4ad0f88..b821996 100644 --- a/src/blockchain/block.rs +++ b/src/blockchain/block.rs @@ -73,24 +73,11 @@ impl Block { if self.transaction.is_some() && other.transaction.is_none() { return true; } - let my_diff = hash_difficulty(self.hash.as_slice()); - let it_diff = hash_difficulty(other.hash.as_slice()); + let my_diff = hash_difficulty(self.hash.as_slice()) + key_hash_difficulty(self.hash.as_slice()); + let it_diff = hash_difficulty(other.hash.as_slice()) + key_hash_difficulty(other.hash.as_slice()); if my_diff > it_diff { return true; } - let my_diff = key_hash_difficulty(self.hash.as_slice()); - let it_diff = key_hash_difficulty(other.hash.as_slice()); - if my_diff > it_diff { - return true; - } - let my_diff = hash_difficulty(self.signature.as_slice()); - let it_diff = hash_difficulty(other.signature.as_slice()); - if my_diff > it_diff { - return true; - } - if self.nonce < other.nonce { - return true; - } false } diff --git a/src/miner.rs b/src/miner.rs index d2c6f71..b057d06 100644 --- a/src/miner.rs +++ b/src/miner.rs @@ -231,25 +231,24 @@ fn find_hash(context: Arc>, mut block: Block, running: Arc block.index { - if !running.load(Ordering::Relaxed) { - return None; - } + if !running.load(Ordering::Relaxed) { + return None; + } + if full && waiting_signers { //trace!("Mining full block is not allowed until previous is not signed"); // We can't mine now, as we need to wait for block to be signed thread::sleep(Duration::from_millis(5000)); continue; } + debug!("Mining block {}", serde_json::to_string(&block).unwrap()); let mut time = Instant::now(); let mut prev_nonce = 0; diff --git a/src/p2p/network.rs b/src/p2p/network.rs index af432d6..1f7876e 100644 --- a/src/p2p/network.rs +++ b/src/p2p/network.rs @@ -22,7 +22,7 @@ use crate::commons::*; use std::cmp::max; const SERVER: Token = Token(0); -const POLL_TIMEOUT: Option = Some(Duration::from_millis(500)); +const POLL_TIMEOUT: Option = Some(Duration::from_millis(250)); const MAX_PACKET_SIZE: usize = 1 * 1024 * 1024; // 1 Mb const MAX_READ_BLOCK_TIME: u128 = 500; @@ -66,6 +66,7 @@ impl Network { let mut ui_timer = Instant::now(); let mut log_timer = Instant::now(); let mut bootstrap_timer = Instant::now(); + let mut connect_timer = Instant::now(); let mut last_events_time = Instant::now(); loop { if peers.get_peers_count() == 0 && bootstrap_timer.elapsed().as_secs() > 60 { @@ -169,9 +170,12 @@ impl Network { (height, context.chain.get_last_hash()) }; peers.update(poll.registry(), height, hash); - peers.connect_new_peers(poll.registry(), &mut unique_token, yggdrasil_only); 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"); @@ -214,7 +218,7 @@ fn handle_connection_event(context: Arc>, peers: &mut Peers, regi if event.is_read_closed() { //debug!("Spurious wakeup for connection {}, ignoring", token.0); if peer.spurious() >= 3 { - //debug!("Disconnecting socket on 3 spurious wakeups"); + debug!("Disconnecting socket for 3 spurious wakeups {}", peer.get_addr().ip()); return false; } let interest = if let State::Message{..} = peer.get_state() { @@ -483,18 +487,21 @@ fn handle_message(context: Arc>, message: Message, peers: &mut Pe Message::Block { index, block } => { let peer = peers.get_mut_peer(token).unwrap(); peer.set_active(true); - info!("Received block {}", index); let block: Block = match serde_json::from_str(&block) { Ok(block) => block, - Err(_) => return State::Error + Err(_) => return State::Banned }; - process_new_block(context, peers, token, block) + if index != block.index { + return State::Banned; + } + info!("Received block {} with hash {:?}", block.index, &block.hash); + handle_block(context, peers, token, block) } }; answer } -fn process_new_block(context: Arc>, peers: &mut Peers, token: &Token, block: Block) -> State { +fn handle_block(context: Arc>, peers: &mut Peers, token: &Token, block: Block) -> State { let peers_count = peers.get_peers_active_count(); let peer = peers.get_mut_peer(token).unwrap(); peer.set_received_block(block.index); diff --git a/src/p2p/peers.rs b/src/p2p/peers.rs index 22f5701..0ee11e3 100644 --- a/src/p2p/peers.rs +++ b/src/p2p/peers.rs @@ -22,7 +22,6 @@ pub struct Peers { new_peers: Vec, ignored: HashSet, my_id: String, - block_asked_time: i64, behind_ping_sent_time: i64, } @@ -33,7 +32,6 @@ impl Peers { new_peers: Vec::new(), ignored: HashSet::new(), my_id: commons::random_string(6), - block_asked_time: 0, behind_ping_sent_time: 0 } } @@ -59,23 +57,23 @@ impl Peers { let _ = registry.deregister(stream); match peer.get_state() { State::Connecting => { - error!("Peer connection {} to {:?} has timed out", &token.0, &peer.get_addr()); + info!("Peer connection {} to {:?} has timed out", &token.0, &peer.get_addr()); } State::Connected => { - error!("Peer connection {} to {:?} disconnected", &token.0, &peer.get_addr()); + info!("Peer connection {} to {:?} disconnected", &token.0, &peer.get_addr()); } State::Idle { .. } | State::Message { .. } => { - error!("Peer connection {} to {:?} disconnected", &token.0, &peer.get_addr()); + info!("Peer connection {} to {:?} disconnected", &token.0, &peer.get_addr()); } State::Error => { - error!("Peer connection {} to {:?} has shut down on error", &token.0, &peer.get_addr()); + info!("Peer connection {} to {:?} has shut down on error", &token.0, &peer.get_addr()); } State::Banned => { - error!("Peer connection {} to {:?} has shut down, banned", &token.0, &peer.get_addr()); + info!("Peer connection {} to {:?} has shut down, banned", &token.0, &peer.get_addr()); self.ignored.insert(peer.get_addr().ip().clone()); } State::Offline { .. } => { - error!("Peer connection {} to {:?} is offline", &token.0, &peer.get_addr()); + info!("Peer connection {} to {:?} is offline", &token.0, &peer.get_addr()); } } @@ -238,24 +236,19 @@ impl Peers { } // If someone has more blocks we sync - if self.need_ask_block() { + { let mut rng = rand::thread_rng(); - let mut asked = false; match self.peers .iter_mut() .filter_map(|(token, peer)| if peer.has_more_blocks(height) { Some((token, peer)) } else { None }) .choose(&mut rng) { None => {} Some((token, peer)) => { - debug!("Found some peer higher than we are, requesting block {}, from {}", height + 1, &peer.get_addr().ip()); + debug!("Peer {} is higher than we are, requesting block {}", &peer.get_addr().ip(), height + 1); registry.reregister(peer.get_stream(), token.clone(), Interest::WRITABLE).unwrap(); peer.set_state(State::message(Message::GetBlock { index: height + 1 })); - asked = true; } } - if asked { - self.update_asked_block_time(); - } } // If someone has less blocks (we mined a new block) we send a ping with our height @@ -267,7 +260,7 @@ impl Peers { .choose(&mut rng) { None => {} Some((token, peer)) => { - debug!("Found some peer lower than we are, sending ping"); + debug!("Peer {} is behind, sending ping", &peer.get_addr().ip()); registry.reregister(peer.get_stream(), token.clone(), Interest::WRITABLE).unwrap(); peer.set_state(State::message(Message::Ping { height, hash })); self.update_behind_ping_time(); @@ -379,14 +372,6 @@ impl Peers { } } - pub fn update_asked_block_time(&mut self) { - self.block_asked_time = Utc::now().timestamp(); - } - - pub fn need_ask_block(&self) -> bool { - self.block_asked_time + 5 < Utc::now().timestamp() - } - pub fn update_behind_ping_time(&mut self) { self.behind_ping_sent_time = Utc::now().timestamp(); }