diff --git a/src/main.rs b/src/main.rs index 60fad22..d70920a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,7 +9,7 @@ use std::env; use std::sync::{Arc, Mutex}; use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use std::thread; -use std::time::Duration; +use std::time::{Duration, Instant}; #[cfg(windows)] use winapi::um::wincon::{AttachConsole, FreeConsole, ATTACH_PARENT_PROCESS}; @@ -165,7 +165,7 @@ fn run_interface(context: Arc>, miner: Arc>) { let scripts = inline_script(include_str!("webview/scripts.js")); let html = Content::Html(file_content.to_owned().replace("{styles}", &styles).replace("{scripts}", &scripts)); - web_view::builder() + let mut interface = web_view::builder() .title("ALFIS 0.1.0") .content(html) .size(1024, 720) @@ -282,8 +282,36 @@ fn run_interface(context: Arc>, miner: Arc>) { //dbg!(&signature); Ok(()) }) - .run() - .unwrap(); + .build() + .expect("Error building GUI"); + + // We use this ugly loop to lower CPU usage a lot. + // If we use .run() or only .step() in a loop without sleeps it will try + // to support 60FPS and uses more CPU than it should. + let pause = Duration::from_millis(25); + let mut start = Instant::now(); + loop { + match interface.step() { + None => { + info!("Interface closed, exiting"); + break; + } + Some(result) => { + match result { + Ok(_) => {} + Err(_) => { + error!("Something wrong with webview, exiting"); + break; + } + } + } + } + if start.elapsed().as_millis() > 1 { + thread::sleep(pause); + start = Instant::now(); + } + } + interface.exit(); } fn create_genesis>(miner: Arc>, name: S, keystore: &Keystore, difficulty: u16) { diff --git a/src/p2p/peers.rs b/src/p2p/peers.rs index ff5592a..9a98b3d 100644 --- a/src/p2p/peers.rs +++ b/src/p2p/peers.rs @@ -1,4 +1,4 @@ -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; use std::net::{SocketAddr, Shutdown}; use mio::{Token, Interest, Registry}; use mio::net::TcpStream; @@ -54,12 +54,27 @@ impl Peers { } pub fn add_peers_from_exchange(&mut self, peers: Vec) { - info!("Got peers: {:?}", &peers); + let peers: HashSet = peers + .iter() + .fold(HashSet::new(), |mut peers, peer| { + peers.insert(peer.to_owned()); + peers + }); + debug!("Got {} peers: {:?}", peers.len(), &peers); // TODO make it return error if these peers are wrong and seem like an attack for peer in peers.iter() { let addr: SocketAddr = peer.parse().expect(&format!("Error parsing peer {}", peer)); + + if self.peers + .iter() + .find(|(token, peer)| peer.get_addr() == addr) + .is_some() { + debug!("Skipping address from exchange: {}", &addr); + continue; + } + if skip_addr(&addr) { - info!("Skipping address from exchange: {}", &addr); + debug!("Skipping address from exchange: {}", &addr); continue; // Return error in future } let mut found = false; diff --git a/src/webview/scripts.js b/src/webview/scripts.js index 50585aa..dcf3e1f 100644 --- a/src/webview/scripts.js +++ b/src/webview/scripts.js @@ -97,7 +97,7 @@ function openTab(element, tabName) { // Get all elements with class="content" and hide them tabContent = document.getElementsByClassName("content"); for (i = 0; i < tabContent.length; i++) { - tabContent[i].className = "context is-hidden"; + tabContent[i].className = "content is-hidden"; } // Get all elements with class="tablinks" and remove the class "active"