From 9e501fe5d0e030791628c07480f25291913b3f34 Mon Sep 17 00:00:00 2001 From: debris Date: Mon, 7 Nov 2016 11:20:27 +0100 Subject: [PATCH] shutdown gracefully on incompatible db open --- pbtc/commands/import.rs | 2 +- pbtc/commands/start.rs | 2 +- pbtc/util.rs | 12 +++++++----- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/pbtc/commands/import.rs b/pbtc/commands/import.rs index 5653ef99..39b045cf 100644 --- a/pbtc/commands/import.rs +++ b/pbtc/commands/import.rs @@ -6,7 +6,7 @@ use util::{open_db, init_db}; pub fn import(cfg: Config, matches: &ArgMatches) -> Result<(), String> { let db = open_db(&cfg); // TODO: this might be unnecessary here! - init_db(&cfg, &db); + try!(init_db(&cfg, &db)); let mut writer = create_sync_blocks_writer(db); diff --git a/pbtc/commands/start.rs b/pbtc/commands/start.rs index 1bf3ef44..5860893d 100644 --- a/pbtc/commands/start.rs +++ b/pbtc/commands/start.rs @@ -8,7 +8,7 @@ pub fn start(cfg: config::Config) -> Result<(), String> { let mut el = p2p::event_loop(); let db = open_db(&cfg); - init_db(&cfg, &db); + try!(init_db(&cfg, &db)); let p2p_cfg = p2p::Config { threads: 4, diff --git a/pbtc/util.rs b/pbtc/util.rs index 112d6723..768f247d 100644 --- a/pbtc/util.rs +++ b/pbtc/util.rs @@ -23,13 +23,15 @@ pub fn node_table_path() -> PathBuf { node_table } -pub fn init_db(cfg: &Config, db: &Arc) { +pub fn init_db(cfg: &Config, db: &Arc) -> Result<(), String> { // insert genesis block if db is empty let genesis_block = cfg.magic.genesis_block(); match db.block_hash(0) { - Some(ref db_genesis_block_hash) if db_genesis_block_hash != &genesis_block.hash() => panic!("Trying to open database with incompatible genesis block"), - Some(_) => (), - None => db.insert_block(&genesis_block) - .expect("Failed to insert genesis block to the database"), + Some(ref db_genesis_block_hash) if db_genesis_block_hash != &genesis_block.hash() => Err("Trying to open database with incompatible genesis block".into()), + Some(_) => Ok(()), + None => { + db.insert_block(&genesis_block).expect("Failed to insert genesis block to the database"); + Ok(()) + } } }