From 52695e0988142f8036df58a34b2d5c6e9001afb7 Mon Sep 17 00:00:00 2001 From: Revertron Date: Mon, 3 May 2021 12:33:22 +0200 Subject: [PATCH] Added a possibility to set owner for mined domain. Fixed small errors. --- src/blockchain/chain.rs | 33 +++++------ src/blockchain/transaction.rs | 16 +++++- src/web_ui.rs | 19 ++++--- src/webview/index.html | 61 +++++++++++++++++--- src/webview/scripts.js | 102 +++++++++++++++++++--------------- 5 files changed, 149 insertions(+), 82 deletions(-) diff --git a/src/blockchain/chain.rs b/src/blockchain/chain.rs index f4d3488..9b326af 100644 --- a/src/blockchain/chain.rs +++ b/src/blockchain/chain.rs @@ -229,13 +229,17 @@ impl Chain { debug!("Adding block:\n{:?}", &block); let index = block.index; let timestamp = block.timestamp; + let owner = block.pub_key.clone(); self.last_block = Some(block.clone()); if block.transaction.is_some() { self.last_full_block = Some(block.clone()); } let transaction = block.transaction.clone(); if self.add_block_to_table(block).is_ok() { - if let Some(transaction) = transaction { + if let Some(mut transaction) = transaction { + if transaction.owner.is_empty() { + transaction.owner = owner; + } self.add_transaction_to_table(index, timestamp, &transaction).expect("Error adding transaction"); } } @@ -321,6 +325,9 @@ impl Chain { } pub fn is_waiting_signers(&self) -> bool { + if self.get_height() < BLOCK_SIGNERS_START { + return false; + } if let Some(full_block) = &self.last_full_block { let sign_count = self.get_height() - full_block.index; if sign_count < BLOCK_SIGNERS_MIN { @@ -356,7 +363,8 @@ impl Chain { /// Adds transaction to transactions table fn add_transaction_to_table(&mut self, index: u64, timestamp: i64, t: &Transaction) -> sqlite::Result { let sql = match t.class.as_ref() { - "domain" => SQL_ADD_DOMAIN, + CLASS_DOMAIN => SQL_ADD_DOMAIN, + CLASS_ORIGIN => return Ok(State::Done), _ => return Err(sqlite::Error { code: None, message: None }) }; @@ -590,29 +598,18 @@ impl Chain { let mut statement = self.db.prepare(SQL_GET_DOMAINS_BY_KEY).unwrap(); statement.bind(1, &**pub_key).expect("Error in bind"); while let State::Row = statement.next().unwrap() { - let index = statement.read::(0).unwrap() as u64; + let _index = statement.read::(0).unwrap() as u64; let timestamp = statement.read::(1).unwrap(); let identity = Bytes::from_bytes(&statement.read::>(2).unwrap()); let confirmation = Bytes::from_bytes(&statement.read::>(3).unwrap()); - let class = String::from("domain"); + let class = String::from(CLASS_DOMAIN); let data = statement.read::(4).unwrap(); let owner = Bytes::from_bytes(&statement.read::>(5).unwrap()); let transaction = Transaction { identity: identity.clone(), confirmation: confirmation.clone(), class, data, owner }; - //debug!("Found transaction for domain {}: {:?}", domain, &transaction); + debug!("Found transaction for domain {:?}", &transaction); if let Some(data) = transaction.get_domain_data() { - let mut domain = keystore.decrypt(data.domain.as_slice(), &confirmation.as_slice()[..12]); - if domain.is_empty() { - // Legacy encryption scheme - for i in 1..=10 { - let b = self.get_block(index - i).unwrap(); - domain = keystore.decrypt(data.domain.as_slice(), &b.hash.as_slice()[..12]); - if !domain.is_empty() { - break; - } - } - } - - let mut domain = String::from_utf8(domain.to_vec()).unwrap(); + let decrypted = keystore.decrypt(data.domain.as_slice(), &confirmation.as_slice()[..12]); + let mut domain = String::from_utf8(decrypted.to_vec()).unwrap(); if domain.is_empty() { domain = String::from("unknown"); } diff --git a/src/blockchain/transaction.rs b/src/blockchain/transaction.rs index 2eab077..968c5f6 100644 --- a/src/blockchain/transaction.rs +++ b/src/blockchain/transaction.rs @@ -25,9 +25,20 @@ pub struct Transaction { } impl Transaction { - pub fn from_str(identity: String, method: String, data: String, owner: Bytes) -> Self { + pub fn from_str(identity: String, method: String, data: String, miner: Bytes, owner: Bytes) -> Self { let hash = hash_identity(&identity, None); - let confirmation = hash_identity(&identity, Some(&owner)); + let key = if owner.is_empty() { + &miner + } else { + &owner + }; + let confirmation = hash_identity(&identity, Some(key)); + // If the miner doesn't change owner, we don't include owner at all + let owner = if owner.is_empty() || owner == miner { + miner + } else { + owner + }; return Self::new(hash, confirmation, method, data, owner); } @@ -125,6 +136,7 @@ pub enum TransactionType { pub struct DomainData { pub domain: Bytes, pub zone: String, + #[serde(default, skip_serializing_if = "String::is_empty")] pub info: String, #[serde(default, skip_serializing_if = "Vec::is_empty")] pub records: Vec, diff --git a/src/web_ui.rs b/src/web_ui.rs index 8e286b5..b33584a 100644 --- a/src/web_ui.rs +++ b/src/web_ui.rs @@ -53,8 +53,8 @@ pub fn run_interface(context: Arc>, miner: Arc>) { SaveKey => { action_save_key(&context); } CheckRecord { data } => { action_check_record(web_view, data); } CheckDomain { name } => { action_check_domain(&context, web_view, name); } - MineDomain { name, data } => { - action_create_domain(Arc::clone(&context), Arc::clone(&miner), web_view, name, data); + MineDomain { name, data, owner } => { + action_create_domain(Arc::clone(&context), Arc::clone(&miner), web_view, name, data, owner); } TransferDomain { .. } => {} StopMining => { context.lock().unwrap().bus.post(Event::ActionStopMining); } @@ -332,7 +332,7 @@ fn load_domains(context: &mut MutexGuard, handle: &Handle<()>) { }); } -fn action_create_domain(context: Arc>, miner: Arc>, web_view: &mut WebView<()>, name: String, data: String) { +fn action_create_domain(context: Arc>, miner: Arc>, web_view: &mut WebView<()>, name: String, data: String, owner: String) { debug!("Creating domain with data: {}", &data); let c = Arc::clone(&context); let context = context.lock().unwrap(); @@ -358,6 +358,11 @@ fn action_create_domain(context: Arc>, miner: Arc>, return; } }; + let owner = if !owner.is_empty() { + Bytes::new(from_hex(&owner).unwrap()) + } else { + Bytes::default() + }; // Check if yggdrasil only quality of zone is not violated let zones = context.chain.get_zones(); for z in zones { @@ -376,7 +381,7 @@ fn action_create_domain(context: Arc>, miner: Arc>, match context.chain.can_mine_domain(context.chain.get_height(), &name, &pub_key) { MineResult::Fine => { std::mem::drop(context); - create_domain(c, miner, CLASS_DOMAIN, &name, data, DOMAIN_DIFFICULTY, &keystore); + create_domain(c, miner, CLASS_DOMAIN, &name, data, DOMAIN_DIFFICULTY, &keystore, owner); let _ = web_view.eval("domainMiningStarted();"); event_info(web_view, &format!("Mining of domain \\'{}\\' has started", &name)); } @@ -482,13 +487,13 @@ fn format_event_now(kind: &str, message: &str) -> String { format!("addEvent('{}', '{}', '{}');", kind, time.format("%d.%m.%y %X"), message) } -fn create_domain(_context: Arc>, miner: Arc>, class: &str, name: &str, mut data: DomainData, difficulty: u32, keystore: &Keystore) { +fn create_domain(_context: Arc>, miner: Arc>, class: &str, name: &str, mut data: DomainData, difficulty: u32, keystore: &Keystore, owner: Bytes) { let name = name.to_owned(); let confirmation = hash_identity(&name, Some(&keystore.get_public())); data.domain = keystore.encrypt(name.as_bytes(), &confirmation.as_slice()[..12]); let data = serde_json::to_string(&data).unwrap(); - let transaction = Transaction::from_str(name, class.to_owned(), data, keystore.get_public().clone()); + let transaction = Transaction::from_str(name, class.to_owned(), data, keystore.get_public().clone(), owner); let block = Block::new(Some(transaction), keystore.get_public(), Bytes::default(), difficulty); miner.lock().unwrap().add_block(block, keystore.clone()); } @@ -502,7 +507,7 @@ pub enum Cmd { SaveKey, CheckRecord { data: String }, CheckDomain { name: String }, - MineDomain { name: String, data: String }, + MineDomain { name: String, data: String, owner: String }, TransferDomain { name: String, owner: String }, StopMining, Open { link: String }, diff --git a/src/webview/index.html b/src/webview/index.html index 5486368..518c1b6 100644 --- a/src/webview/index.html +++ b/src/webview/index.html @@ -176,13 +176,33 @@ +
+ +
+
-
@@ -196,23 +216,46 @@ -