Merge pull request #96 from ethcore/shutdown_gracefully

shutdown gracefully on incompatible db open
This commit is contained in:
Svyatoslav Nikolsky 2016-11-07 13:27:30 +03:00 committed by GitHub
commit 725120c126
3 changed files with 9 additions and 7 deletions

View File

@ -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);

View File

@ -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,

View File

@ -23,13 +23,15 @@ pub fn node_table_path() -> PathBuf {
node_table
}
pub fn init_db(cfg: &Config, db: &Arc<db::Store>) {
pub fn init_db(cfg: &Config, db: &Arc<db::Store>) -> 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(())
}
}
}