From f60e42eb2e24f03b3ded6485d801c13f32c44a1b Mon Sep 17 00:00:00 2001 From: Revertron Date: Tue, 27 Apr 2021 17:10:05 +0200 Subject: [PATCH] Fixed #86 - shuffling domain zones list. --- Cargo.toml | 2 +- src/blockchain/transaction.rs | 23 +++++++++++++++++++++++ src/event.rs | 1 + src/p2p/network.rs | 11 ++++++++++- src/web_ui.rs | 13 ++++++++++--- 5 files changed, 45 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e71bbbf..7c35d92 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "alfis" -version = "0.4.31" +version = "0.4.32" authors = ["Revertron "] edition = "2018" build = "build.rs" diff --git a/src/blockchain/transaction.rs b/src/blockchain/transaction.rs index eba04ec..632350e 100644 --- a/src/blockchain/transaction.rs +++ b/src/blockchain/transaction.rs @@ -63,6 +63,22 @@ impl Transaction { } None } + + /// Gets a type of transaction + pub fn get_type(what: &Option) -> TransactionType { + match what { + None => { TransactionType::Signing } + Some(transaction) => { + if let Some(_) = transaction.get_domain_data() { + return TransactionType::Domain; + } + if let Ok(_) = serde_json::from_str::(&transaction.data) { + return TransactionType::Zone; + } + TransactionType::Unknown + } + } + } } impl fmt::Debug for Transaction { @@ -89,6 +105,13 @@ impl Serialize for Transaction { } } +pub enum TransactionType { + Unknown, + Signing, + Domain, + Zone, +} + #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] pub struct DomainData { pub domain: Bytes, diff --git a/src/event.rs b/src/event.rs index fd0327c..021c6e5 100644 --- a/src/event.rs +++ b/src/event.rs @@ -8,6 +8,7 @@ pub enum Event { KeyCreated { path: String, public: String, hash: String }, KeyLoaded { path: String, public: String, hash: String }, KeySaved { path: String, public: String, hash: String }, + ZonesChanged, NewBlockReceived, BlockchainChanged { index: u64 }, ActionStopMining, diff --git a/src/p2p/network.rs b/src/p2p/network.rs index b2d4d61..0d5e20d 100644 --- a/src/p2p/network.rs +++ b/src/p2p/network.rs @@ -17,7 +17,8 @@ use mio::event::Event; use mio::net::{TcpListener, TcpStream}; use rand::random; -use crate::{Block, Context, p2p::Message, p2p::Peer, p2p::Peers, p2p::State}; +use crate::{Block, Context, p2p::Message, p2p::Peer, p2p::Peers, p2p::State, Transaction}; +use crate::blockchain::transaction::TransactionType; use crate::blockchain::types::BlockQuality; use crate::commons::*; @@ -538,9 +539,13 @@ fn handle_block(context: Arc>, peers: &mut Peers, token: &Token, let max_height = context.chain.max_height(); match context.chain.check_new_block(&block) { BlockQuality::Good => { + let zone = matches!(Transaction::get_type(&block.transaction), TransactionType::Zone); context.chain.add_block(block); let my_height = context.chain.get_height(); context.bus.post(crate::event::Event::BlockchainChanged { index: my_height }); + if zone { + context.bus.post(crate::event::Event::ZonesChanged); + } // If it was the last block to sync if my_height == max_height { context.bus.post(crate::event::Event::SyncFinished); @@ -568,9 +573,13 @@ fn handle_block(context: Arc>, peers: &mut Peers, token: &Token, debug!("Got forked block {} with hash {:?}", block.index, block.hash); let last_block = context.chain.last_block().unwrap(); if block.is_better_than(&last_block) { + let zone = matches!(Transaction::get_type(&block.transaction), TransactionType::Zone); context.chain.replace_block(block).expect("Error replacing block with fork"); let index = context.chain.get_height(); context.bus.post(crate::event::Event::BlockchainChanged { index }); + if zone { + context.bus.post(crate::event::Event::ZonesChanged); + } } let height = context.chain.get_height(); context.chain.update_max_height(height); diff --git a/src/web_ui.rs b/src/web_ui.rs index f393369..ff28f8d 100644 --- a/src/web_ui.rs +++ b/src/web_ui.rs @@ -292,9 +292,8 @@ fn action_loaded(context: &Arc>, web_view: &mut WebView<()>) { format!("setLeftStatusBarText('Idle'); setRightStatusBarText('Nodes: {}, Blocks: {}')", nodes, blocks) } } - Event::BlockchainChanged {index} => { - debug!("Current blockchain height is {}", index); - event_handle_info(&handle, &format!("Blockchain changed, current block count is {} now.", index)); + Event::ZonesChanged => { + info!("New zone arrived"); if let Ok(zones) = serde_json::to_string(&context.chain.get_zones()) { let _ = handle.dispatch(move |web_view|{ web_view.eval(&format!("zonesChanged('{}');", &zones)) @@ -302,6 +301,11 @@ fn action_loaded(context: &Arc>, web_view: &mut WebView<()>) { } String::new() // Nothing } + Event::BlockchainChanged {index} => { + debug!("Current blockchain height is {}", index); + event_handle_info(&handle, &format!("Blockchain changed, current block count is {} now.", index)); + String::new() // Nothing + } _ => { String::new() } }; @@ -322,6 +326,9 @@ fn action_loaded(context: &Arc>, web_view: &mut WebView<()>) { } let index = c.chain.get_height(); c.bus.post(Event::BlockchainChanged { index }); + if let Ok(zones) = serde_json::to_string(&c.chain.get_zones()) { + let _ = web_view.eval(&format!("zonesChanged('{}');", &zones)); + } event_info(web_view, "Application loaded"); }