shutdown gracefully on incompatible db open
This commit is contained in:
parent
2248dc2c35
commit
9e501fe5d0
|
@ -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);
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
12
pbtc/util.rs
12
pbtc/util.rs
|
@ -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(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue