From edec631c3964423a81ddd16ba4c8c4bb298c04f5 Mon Sep 17 00:00:00 2001 From: Revertron Date: Wed, 21 Apr 2021 14:38:37 +0200 Subject: [PATCH] Fixed database truncation. --- src/blockchain/chain.rs | 26 +++++++++++++++++++------- src/main.rs | 6 +++--- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/blockchain/chain.rs b/src/blockchain/chain.rs index 3f86630..b16a800 100644 --- a/src/blockchain/chain.rs +++ b/src/blockchain/chain.rs @@ -31,7 +31,9 @@ const SQL_REPLACE_BLOCK: &str = "UPDATE blocks SET timestamp = ?, version = ?, d const SQL_GET_LAST_BLOCK: &str = "SELECT * FROM blocks ORDER BY id DESC LIMIT 1;"; const SQL_GET_FIRST_BLOCK_FOR_KEY: &str = "SELECT id FROM blocks WHERE pub_key = ? LIMIT 1;"; const SQL_DELETE_BLOCK_AND_TRANSACTIONS: &str = "DELETE FROM blocks WHERE id = ?; DELETE FROM domains WHERE id = ?; DELETE FROM zones WHERE id = ?;"; -const SQL_TRUNCATE_DB_FROM_BLOCK: &str = "DELETE FROM blocks WHERE id >= ?; DELETE FROM domains WHERE id >= ?; DELETE FROM zones WHERE id >= ?;"; +const SQL_TRUNCATE_BLOCKS: &str = "DELETE FROM blocks WHERE id >= ?;"; +const SQL_TRUNCATE_DOMAINS: &str = "DELETE FROM domains WHERE id >= ?;"; +const SQL_TRUNCATE_ZONES: &str = "DELETE FROM zones WHERE id >= ?;"; const SQL_ADD_DOMAIN: &str = "INSERT INTO domains (id, timestamp, identity, confirmation, data, pub_key) VALUES (?, ?, ?, ?, ?, ?)"; const SQL_ADD_ZONE: &str = "INSERT INTO zones (id, timestamp, identity, confirmation, data, pub_key) VALUES (?, ?, ?, ?, ?, ?)"; @@ -114,12 +116,16 @@ impl Chain { if self.check_new_block(&block) != BlockQuality::Good { error!("Block {} is bad:\n{:?}", block.index, &block); info!("Truncating database from block {}...", block.index); - if self.truncate_db_from_block(block.index).is_err() { - panic!("Error truncating database! Please, delete 'blockchain.db' and restart."); + match self.truncate_db_from_block(block.index) { + Ok(_) => {} + Err(e) => { + error!("{}", e); + panic!("Error truncating database! Please, delete 'blockchain.db' and restart."); + } } break; } - info!("Block {} is good!", block.index); + info!("Block {} with hash {:?} is good!", block.index, &block.hash); if block.transaction.is_some() { self.last_full_block = Some(block.clone()); } @@ -143,10 +149,16 @@ impl Chain { #[allow(dead_code)] fn truncate_db_from_block(&mut self, index: u64) -> sqlite::Result { - let mut statement = self.db.prepare(SQL_TRUNCATE_DB_FROM_BLOCK)?; + let mut statement = self.db.prepare(SQL_TRUNCATE_BLOCKS)?; + statement.bind(1, index as i64)?; + statement.next()?; + + let mut statement = self.db.prepare(SQL_TRUNCATE_DOMAINS)?; + statement.bind(1, index as i64)?; + statement.next()?; + + let mut statement = self.db.prepare(SQL_TRUNCATE_ZONES)?; statement.bind(1, index as i64)?; - statement.bind(2, index as i64)?; - statement.bind(3, index as i64)?; statement.next() } diff --git a/src/main.rs b/src/main.rs index 92f0b96..7428e02 100644 --- a/src/main.rs +++ b/src/main.rs @@ -91,13 +91,13 @@ fn main() { #[cfg(not(feature = "webgui"))] let no_gui = true; + if let Some(path) = opt_matches.opt_str("w") { + env::set_current_dir(Path::new(&path)).expect(&format!("Unable to change working directory to '{}'", &path)); + } let config_name = match opt_matches.opt_str("c") { None => { SETTINGS_FILENAME.to_owned() } Some(path) => { path } }; - if let Some(path) = opt_matches.opt_str("w") { - env::set_current_dir(Path::new(&path)).expect(&format!("Unable to change working directory to '{}'", &path)); - } setup_logger(&opt_matches); info!(target: LOG_TARGET_MAIN, "Starting ALFIS {}", env!("CARGO_PKG_VERSION"));