Fixed #86 - shuffling domain zones list.

This commit is contained in:
Revertron
2021-04-27 17:10:05 +02:00
parent daf6c23967
commit f60e42eb2e
5 changed files with 45 additions and 5 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
[package] [package]
name = "alfis" name = "alfis"
version = "0.4.31" version = "0.4.32"
authors = ["Revertron <alfis@revertron.com>"] authors = ["Revertron <alfis@revertron.com>"]
edition = "2018" edition = "2018"
build = "build.rs" build = "build.rs"
+23
View File
@@ -63,6 +63,22 @@ impl Transaction {
} }
None None
} }
/// Gets a type of transaction
pub fn get_type(what: &Option<Transaction>) -> 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::<ZoneData>(&transaction.data) {
return TransactionType::Zone;
}
TransactionType::Unknown
}
}
}
} }
impl fmt::Debug for Transaction { 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)] #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct DomainData { pub struct DomainData {
pub domain: Bytes, pub domain: Bytes,
+1
View File
@@ -8,6 +8,7 @@ pub enum Event {
KeyCreated { path: String, public: String, hash: String }, KeyCreated { path: String, public: String, hash: String },
KeyLoaded { path: String, public: String, hash: String }, KeyLoaded { path: String, public: String, hash: String },
KeySaved { path: String, public: String, hash: String }, KeySaved { path: String, public: String, hash: String },
ZonesChanged,
NewBlockReceived, NewBlockReceived,
BlockchainChanged { index: u64 }, BlockchainChanged { index: u64 },
ActionStopMining, ActionStopMining,
+10 -1
View File
@@ -17,7 +17,8 @@ use mio::event::Event;
use mio::net::{TcpListener, TcpStream}; use mio::net::{TcpListener, TcpStream};
use rand::random; 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::blockchain::types::BlockQuality;
use crate::commons::*; use crate::commons::*;
@@ -538,9 +539,13 @@ fn handle_block(context: Arc<Mutex<Context>>, peers: &mut Peers, token: &Token,
let max_height = context.chain.max_height(); let max_height = context.chain.max_height();
match context.chain.check_new_block(&block) { match context.chain.check_new_block(&block) {
BlockQuality::Good => { BlockQuality::Good => {
let zone = matches!(Transaction::get_type(&block.transaction), TransactionType::Zone);
context.chain.add_block(block); context.chain.add_block(block);
let my_height = context.chain.get_height(); let my_height = context.chain.get_height();
context.bus.post(crate::event::Event::BlockchainChanged { index: my_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 it was the last block to sync
if my_height == max_height { if my_height == max_height {
context.bus.post(crate::event::Event::SyncFinished); context.bus.post(crate::event::Event::SyncFinished);
@@ -568,9 +573,13 @@ fn handle_block(context: Arc<Mutex<Context>>, peers: &mut Peers, token: &Token,
debug!("Got forked block {} with hash {:?}", block.index, block.hash); debug!("Got forked block {} with hash {:?}", block.index, block.hash);
let last_block = context.chain.last_block().unwrap(); let last_block = context.chain.last_block().unwrap();
if block.is_better_than(&last_block) { 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"); context.chain.replace_block(block).expect("Error replacing block with fork");
let index = context.chain.get_height(); let index = context.chain.get_height();
context.bus.post(crate::event::Event::BlockchainChanged { index }); context.bus.post(crate::event::Event::BlockchainChanged { index });
if zone {
context.bus.post(crate::event::Event::ZonesChanged);
}
} }
let height = context.chain.get_height(); let height = context.chain.get_height();
context.chain.update_max_height(height); context.chain.update_max_height(height);
+10 -3
View File
@@ -292,9 +292,8 @@ fn action_loaded(context: &Arc<Mutex<Context>>, web_view: &mut WebView<()>) {
format!("setLeftStatusBarText('Idle'); setRightStatusBarText('Nodes: {}, Blocks: {}')", nodes, blocks) format!("setLeftStatusBarText('Idle'); setRightStatusBarText('Nodes: {}, Blocks: {}')", nodes, blocks)
} }
} }
Event::BlockchainChanged {index} => { Event::ZonesChanged => {
debug!("Current blockchain height is {}", index); info!("New zone arrived");
event_handle_info(&handle, &format!("Blockchain changed, current block count is {} now.", index));
if let Ok(zones) = serde_json::to_string(&context.chain.get_zones()) { if let Ok(zones) = serde_json::to_string(&context.chain.get_zones()) {
let _ = handle.dispatch(move |web_view|{ let _ = handle.dispatch(move |web_view|{
web_view.eval(&format!("zonesChanged('{}');", &zones)) web_view.eval(&format!("zonesChanged('{}');", &zones))
@@ -302,6 +301,11 @@ fn action_loaded(context: &Arc<Mutex<Context>>, web_view: &mut WebView<()>) {
} }
String::new() // Nothing 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() } _ => { String::new() }
}; };
@@ -322,6 +326,9 @@ fn action_loaded(context: &Arc<Mutex<Context>>, web_view: &mut WebView<()>) {
} }
let index = c.chain.get_height(); let index = c.chain.get_height();
c.bus.post(Event::BlockchainChanged { index }); 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"); event_info(web_view, "Application loaded");
} }