From 5ec3fa888e4973eef6a2cadb3cd87d6530fcf8fc Mon Sep 17 00:00:00 2001 From: ghassmo Date: Wed, 30 Jun 2021 21:28:04 +0300 Subject: [PATCH] use join_config_path function for both sqlite and rocksdb databases --- src/bin/darkfid.rs | 37 +++++++++++++++++++++++-------------- src/bin/gatewayd.rs | 6 ++++-- src/blockchain/rocks.rs | 6 +++--- src/wallet/walletdb.rs | 17 +++++------------ 4 files changed, 35 insertions(+), 31 deletions(-) diff --git a/src/bin/darkfid.rs b/src/bin/darkfid.rs index ca9f6dd9b..5134a25e1 100644 --- a/src/bin/darkfid.rs +++ b/src/bin/darkfid.rs @@ -1,6 +1,6 @@ use async_std::sync::Arc; //use drk::rpc:: -use drk::rpc::adapter::{RpcAdapter}; +use drk::rpc::adapter::RpcAdapter; use drk::rpc::jsonserver; //use drk::rpc::options::ProgramOptions; use rand::rngs::OsRng; @@ -20,6 +20,7 @@ use drk::service::{ClientProgramOptions, GatewayClient, GatewaySlabsSubscriber}; use drk::state::{state_transition, ProgramState, StateUpdate}; use drk::wallet::{WalletDB, WalletPtr}; use drk::{tx, Result}; +use drk::util::join_config_path; use rusqlite::Connection; use async_executor::Executor; @@ -160,9 +161,10 @@ pub async fn subscribe(gateway_slabs_sub: GatewaySlabsSubscriber, mut state: Sta async fn start(executor: Arc>, options: Arc) -> Result<()> { let connect_addr: SocketAddr = setup_addr(options.connect_addr, "127.0.0.1:3333".parse()?); let sub_addr: SocketAddr = setup_addr(options.sub_addr, "127.0.0.1:4444".parse()?); - let database_path = options.database_path.as_path(); + let database_path = options.database_path.clone(); - let rocks = Rocks::new(database_path)?; + let database_path = join_config_path(&(*database_path))?; + let rocks = Rocks::new(&database_path)?; let slabstore = RocksColumn::::new(rocks.clone()); @@ -285,10 +287,12 @@ mod test { use std::net::SocketAddr; use std::path::Path; + use std::path::PathBuf; use std::sync::Arc; use drk::blockchain::{rocks::columns, Rocks, RocksColumn, Slab}; use drk::service::{GatewayClient, GatewaySlabsSubscriber}; + use drk::util::join_config_path; use async_executor::Executor; use easy_parallel::Parallel; @@ -343,8 +347,9 @@ mod test { let rnd: u32 = rng.gen(); let path_str = format!("database_{}.db", rnd); - let database_path = Path::new(path_str.as_str()); - let rocks = Rocks::new(database_path.clone()).unwrap(); + let database_path = PathBuf::from(path_str.as_str()); + let database_path = join_config_path(&database_path).unwrap(); + let rocks = Rocks::new(&database_path).unwrap(); let slabstore = RocksColumn::::new(rocks.clone()); @@ -390,8 +395,9 @@ mod test { let rnd: u32 = rng.gen(); let path_str = format!("database_{}.db", rnd); - let database_path = Path::new(path_str.as_str()); - let rocks = Rocks::new(database_path.clone()).unwrap(); + let database_path = PathBuf::from(path_str.as_str()); + let database_path = join_config_path(&database_path).unwrap(); + let rocks = Rocks::new(&database_path).unwrap(); let slabstore = RocksColumn::::new(rocks.clone()); @@ -401,7 +407,7 @@ mod test { // start gateway client client.start().await.unwrap(); - let slab = Slab::new( rnd.to_le_bytes().to_vec()); + let slab = Slab::new(rnd.to_le_bytes().to_vec()); client.put_slab(slab.clone()).await.unwrap(); client.put_slab(slab.clone()).await.unwrap(); @@ -435,8 +441,9 @@ mod test { let rnd: u32 = rng.gen(); let path_str = format!("database_{}.db", rnd); - let database_path = Path::new(path_str.as_str()); - let rocks = Rocks::new(database_path.clone()).unwrap(); + let database_path = PathBuf::from(path_str.as_str()); + let database_path = join_config_path(&database_path).unwrap(); + let rocks = Rocks::new(&database_path).unwrap(); let slabstore = RocksColumn::::new(rocks.clone()); @@ -485,8 +492,9 @@ mod test { let rnd: u32 = rng.gen(); let path_str = format!("database_{}.db", rnd); - let database_path = Path::new(path_str.as_str()); - let rocks = Rocks::new(database_path.clone()).unwrap(); + let database_path = PathBuf::from(path_str.as_str()); + let database_path = join_config_path(&database_path).unwrap(); + let rocks = Rocks::new(&database_path).unwrap(); let slabstore = RocksColumn::::new(rocks.clone()); @@ -530,8 +538,9 @@ mod test { let rnd: u32 = rng.gen(); let path_str = format!("database_{}.db", rnd); - let database_path = Path::new(path_str.as_str()); - let rocks = Rocks::new(database_path.clone()).unwrap(); + let database_path = PathBuf::from(path_str.as_str()); + let database_path = join_config_path(&database_path).unwrap(); + let rocks = Rocks::new(&database_path).unwrap(); let slabstore = RocksColumn::::new(rocks.clone()); diff --git a/src/bin/gatewayd.rs b/src/bin/gatewayd.rs index 40a09f9cf..388c13a40 100644 --- a/src/bin/gatewayd.rs +++ b/src/bin/gatewayd.rs @@ -4,6 +4,7 @@ use std::sync::Arc; use drk::blockchain::{rocks::columns, Rocks, RocksColumn}; use drk::service::{GatewayService, ProgramOptions}; use drk::Result; +use drk::util::join_config_path; extern crate clap; use async_executor::Executor; @@ -19,9 +20,10 @@ fn setup_addr(address: Option, default: SocketAddr) -> SocketAddr { async fn start(executor: Arc>, options: ProgramOptions) -> Result<()> { let accept_addr: SocketAddr = setup_addr(options.accept_addr, "127.0.0.1:3333".parse()?); let pub_addr: SocketAddr = setup_addr(options.pub_addr, "127.0.0.1:4444".parse()?); - let database_path = options.database_path.as_path(); + let database_path = options.database_path.clone(); - let rocks = Rocks::new(database_path)?; + let database_path = join_config_path(&(*database_path))?; + let rocks = Rocks::new(&database_path)?; let rocks_slabstore_column = RocksColumn::::new(rocks); let gateway = GatewayService::new(accept_addr, pub_addr, rocks_slabstore_column)?; diff --git a/src/blockchain/rocks.rs b/src/blockchain/rocks.rs index 8c4605c68..54357d809 100644 --- a/src/blockchain/rocks.rs +++ b/src/blockchain/rocks.rs @@ -1,6 +1,6 @@ use async_std::sync::Arc; use std::marker::PhantomData; -use std::path::Path; +use std::path::PathBuf; use crate::serial::{deserialize, serialize, Decodable, Encodable}; use crate::{Error, Result}; @@ -39,7 +39,7 @@ pub struct Rocks { } impl Rocks { - pub fn new(path: &Path) -> Result> { + pub fn new(path: &PathBuf) -> Result> { // column family options let cf_opts = Options::default(); @@ -99,7 +99,7 @@ impl Rocks { self.db.iterator_cf(cf, iterator_mode) } - pub fn destroy(path: &Path) -> Result<()> { + pub fn destroy(path: &PathBuf) -> Result<()> { DB::destroy(&Options::default(), path)?; Ok(()) } diff --git a/src/wallet/walletdb.rs b/src/wallet/walletdb.rs index 55c7f6fac..553dc870c 100644 --- a/src/wallet/walletdb.rs +++ b/src/wallet/walletdb.rs @@ -1,14 +1,15 @@ use crate::crypto::{coin::Coin, merkle::IncrementalWitness, merkle_node::MerkleNode, note::Note}; use crate::serial; use crate::serial::{deserialize, serialize, Decodable, Encodable}; -use crate::Error; use crate::Result; +use crate::util::join_config_path; + use async_std::sync::{Arc, Mutex}; use ff::Field; use log::*; use rand::rngs::OsRng; use rusqlite::{named_params, Connection}; -use std::path::{PathBuf}; +use std::path::PathBuf; pub type WalletPtr = Arc; @@ -26,7 +27,7 @@ pub struct WalletDB { impl WalletDB { pub fn new(wallet: &str) -> Result { debug!(target: "walletdb", "new() Constructor called"); - let path = Self::create_path(wallet)?; + let path = join_config_path(&PathBuf::from(wallet))?; let cashier_secret = jubjub::Fr::random(&mut OsRng); let secret = jubjub::Fr::random(&mut OsRng); let public = zcash_primitives::constants::SPENDING_KEY_GENERATOR * secret; @@ -88,15 +89,7 @@ impl WalletDB { Ok(()) } - fn create_path(wallet: &str) -> Result { - let mut path = dirs::home_dir() - .ok_or(Error::PathNotFound)? - .as_path() - .join(".config/darkfi/"); - path.push(wallet); - debug!(target: "walletdb", "CREATE PATH {:?}", path); - Ok(path) - } + pub async fn key_gen(&self) -> (Vec, Vec) { debug!(target: "key_gen", "Generating keys...");