Fixed database truncation.
This commit is contained in:
+19
-7
@@ -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_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_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_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_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 (?, ?, ?, ?, ?, ?)";
|
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 {
|
if self.check_new_block(&block) != BlockQuality::Good {
|
||||||
error!("Block {} is bad:\n{:?}", block.index, &block);
|
error!("Block {} is bad:\n{:?}", block.index, &block);
|
||||||
info!("Truncating database from block {}...", block.index);
|
info!("Truncating database from block {}...", block.index);
|
||||||
if self.truncate_db_from_block(block.index).is_err() {
|
match self.truncate_db_from_block(block.index) {
|
||||||
panic!("Error truncating database! Please, delete 'blockchain.db' and restart.");
|
Ok(_) => {}
|
||||||
|
Err(e) => {
|
||||||
|
error!("{}", e);
|
||||||
|
panic!("Error truncating database! Please, delete 'blockchain.db' and restart.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
info!("Block {} is good!", block.index);
|
info!("Block {} with hash {:?} is good!", block.index, &block.hash);
|
||||||
if block.transaction.is_some() {
|
if block.transaction.is_some() {
|
||||||
self.last_full_block = Some(block.clone());
|
self.last_full_block = Some(block.clone());
|
||||||
}
|
}
|
||||||
@@ -143,10 +149,16 @@ impl Chain {
|
|||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
fn truncate_db_from_block(&mut self, index: u64) -> sqlite::Result<State> {
|
fn truncate_db_from_block(&mut self, index: u64) -> sqlite::Result<State> {
|
||||||
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(1, index as i64)?;
|
||||||
statement.bind(2, index as i64)?;
|
|
||||||
statement.bind(3, index as i64)?;
|
|
||||||
statement.next()
|
statement.next()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+3
-3
@@ -91,13 +91,13 @@ fn main() {
|
|||||||
#[cfg(not(feature = "webgui"))]
|
#[cfg(not(feature = "webgui"))]
|
||||||
let no_gui = true;
|
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") {
|
let config_name = match opt_matches.opt_str("c") {
|
||||||
None => { SETTINGS_FILENAME.to_owned() }
|
None => { SETTINGS_FILENAME.to_owned() }
|
||||||
Some(path) => { path }
|
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);
|
setup_logger(&opt_matches);
|
||||||
info!(target: LOG_TARGET_MAIN, "Starting ALFIS {}", env!("CARGO_PKG_VERSION"));
|
info!(target: LOG_TARGET_MAIN, "Starting ALFIS {}", env!("CARGO_PKG_VERSION"));
|
||||||
|
|||||||
Reference in New Issue
Block a user