Fixed a problem with allowed mining while waiting for signers.

Sped up initial blocks downloading.
Changed block consensus a bit.
This commit is contained in:
Revertron
2021-04-23 01:09:38 +02:00
parent dee53887cb
commit 0d944ca1dc
5 changed files with 34 additions and 56 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "alfis"
version = "0.4.23"
version = "0.4.24"
authors = ["Revertron <alfis@revertron.com>"]
edition = "2018"
build = "build.rs"
+2 -15
View File
@@ -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
}
+8 -9
View File
@@ -231,25 +231,24 @@ fn find_hash(context: Arc<Mutex<Context>>, mut block: Block, running: Arc<Atomic
loop {
block.random = rand::random();
block.timestamp = Utc::now().timestamp();
let next_allowed_block = {
let waiting_signers = {
let context = context.lock().unwrap();
// We use this block to fill some fields of our block as well
block.index = context.chain.get_height() + 1;
if let Some(b) = context.chain.last_block() {
block.prev_block_hash = b.hash;
block.index = b.index + 1;
}
context.chain.next_allowed_full_block()
context.chain.is_waiting_signers()
};
if full && next_allowed_block > 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;
+14 -7
View File
@@ -22,7 +22,7 @@ use crate::commons::*;
use std::cmp::max;
const SERVER: Token = Token(0);
const POLL_TIMEOUT: Option<Duration> = Some(Duration::from_millis(500));
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;
@@ -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<Mutex<Context>>, 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<Mutex<Context>>, 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<Mutex<Context>>, peers: &mut Peers, token: &Token, block: Block) -> State {
fn handle_block(context: Arc<Mutex<Context>>, 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);
+9 -24
View File
@@ -22,7 +22,6 @@ pub struct Peers {
new_peers: Vec<SocketAddr>,
ignored: HashSet<IpAddr>,
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();
}