Skip to content

Commit

Permalink
Several small optimizations.
Browse files Browse the repository at this point in the history
  • Loading branch information
Revertron committed Apr 18, 2021
1 parent 8cbc26f commit 9ca952e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "alfis"
version = "0.4.11"
version = "0.4.12"
authors = ["Revertron <[email protected]>"]
edition = "2018"
build = "build.rs"
Expand Down
6 changes: 6 additions & 0 deletions src/blockchain/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ impl Chain {
None => { return None; }
Some(ref block) => { block.clone() }
};
// TODO maybe make some config option to mine signing blocks above?
let sign_count = self.height() - block.index;
if sign_count >= BLOCK_SIGNERS_MIN {
trace!("Block {} has enough signing blocks", block.index);
return None;
}
let keystore = keystore.clone().unwrap().clone();
let signers: HashSet<Bytes> = self.get_block_signers(&block).into_iter().collect();
if signers.contains(&keystore.get_public()) {
Expand Down
13 changes: 11 additions & 2 deletions src/p2p/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ impl Network {
let mut ui_timer = Instant::now();
let mut log_timer = Instant::now();
let mut bootstrap_timer = Instant::now();
let mut last_events_time = Instant::now();
loop {
if peers.get_peers_count() == 0 && bootstrap_timer.elapsed().as_secs() > 60 {
// Starting peer connections to bootstrap nodes
Expand Down Expand Up @@ -136,6 +137,9 @@ impl Network {
}
}
}
if !events.is_empty() {
last_events_time = Instant::now();
}
events.clear();

if ui_timer.elapsed().as_millis() > UI_REFRESH_DELAY_MS {
Expand All @@ -144,11 +148,16 @@ impl Network {
let mut context = context.lock().unwrap();
let height = context.chain.height();
let nodes = peers.get_peers_active_count();
let banned = peers.get_peers_banned_count();
if nodes > 0 {
context.bus.post(crate::event::Event::NetworkStatus { nodes, blocks: height });
}
if log_timer.elapsed().as_secs() > LOG_REFRESH_DELAY_SEC {
info!("Active nodes count: {}, blocks count: {}", nodes, height);
info!("Active nodes count: {}, banned count: {}, blocks count: {}", nodes, banned, height);
let elapsed = last_events_time.elapsed().as_secs();
if elapsed >= 10 {
warn!("Last network events time {} seconds ago", elapsed);
}
log_timer = Instant::now();
let keystore = context.keystore.clone();
if let Some(event) = context.chain.update(&keystore) {
Expand Down Expand Up @@ -433,7 +442,7 @@ fn handle_message(context: Arc<Mutex<Context>>, message: Message, peers: &mut Pe
State::message(Message::GetBlock { index: my_height })
} else {
if random::<u8>() < 10 {
debug!("Requesting more peers");
debug!("Requesting more peers from {}", peer.get_addr().ip());
State::message(Message::GetPeers)
} else {
State::idle()
Expand Down
4 changes: 4 additions & 0 deletions src/p2p/peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ impl Peers {
count
}

pub fn get_peers_banned_count(&self) -> usize {
self.ignored.len()
}

pub fn ignore_peer(&mut self, registry: &Registry, token: &Token) {
let peer = self.peers.get_mut(token).unwrap();
peer.set_state(State::Banned);
Expand Down

0 comments on commit 9ca952e

Please sign in to comment.