2016-11-03 09:19:35 -07:00
|
|
|
use std::sync::Arc;
|
2016-11-04 04:08:58 -07:00
|
|
|
use std::path::PathBuf;
|
2016-11-03 09:19:35 -07:00
|
|
|
use app_dirs::{app_dir, AppDataType};
|
2016-11-07 01:15:15 -08:00
|
|
|
use chain::RepresentH256;
|
2016-11-03 09:19:35 -07:00
|
|
|
use {db, APP_INFO};
|
2016-11-07 01:15:15 -08:00
|
|
|
use config::Config;
|
2016-11-03 09:19:35 -07:00
|
|
|
|
2016-11-07 01:15:15 -08:00
|
|
|
pub fn open_db(cfg: &Config) -> Arc<db::Store> {
|
|
|
|
match cfg.use_disk_database {
|
2016-11-03 09:19:35 -07:00
|
|
|
true => {
|
|
|
|
let db_path = app_dir(AppDataType::UserData, &APP_INFO, "db").expect("Failed to get app dir");
|
|
|
|
Arc::new(db::Storage::new(db_path).expect("Failed to open database"))
|
|
|
|
},
|
|
|
|
false => {
|
|
|
|
Arc::new(db::TestStorage::default())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-04 04:08:58 -07:00
|
|
|
pub fn node_table_path() -> PathBuf {
|
|
|
|
let mut node_table = app_dir(AppDataType::UserData, &APP_INFO, "p2p").expect("Failed to get app dir");
|
|
|
|
node_table.push("nodes.csv");
|
|
|
|
node_table
|
|
|
|
}
|
|
|
|
|
2016-11-07 01:15:15 -08:00
|
|
|
pub fn init_db(cfg: &Config, db: &Arc<db::Store>) {
|
2016-11-03 09:19:35 -07:00
|
|
|
// insert genesis block if db is empty
|
2016-11-07 01:15:15 -08:00
|
|
|
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"),
|
2016-11-03 09:19:35 -07:00
|
|
|
}
|
|
|
|
}
|