Przeglądaj źródła

create ClientConfig trait & create DarkfiCliConfig and DarkfidCliConfig structs which implement ClientConfig trait

ghassmo 5 lat temu
rodzic
commit
98302660f5

+ 5 - 6
src/bin/darkfid.rs

@@ -1,5 +1,5 @@
 use drk::blockchain::{rocks::columns, Rocks, RocksColumn};
-use drk::cli::{cli_config, DarkfidCli};
+use drk::cli::{DarkfidCliConfig, DarkfidCli, ClientCliConfig};
 use drk::crypto::{
     load_params,
     merkle::{CommitmentTree, IncrementalWitness},
@@ -153,8 +153,7 @@ pub async fn subscribe(gateway_slabs_sub: GatewaySlabsSubscriber, mut state: Sta
 
 async fn start(
     executor: Arc<Executor<'_>>,
-    config: Arc<cli_config::Config>,
-    _options: Arc<DarkfidCli>,
+    config: Arc<DarkfidCliConfig>,
 ) -> Result<()> {
     let connect_addr: SocketAddr = config.connect_url.parse()?;
     let sub_addr: SocketAddr = config.subscriber_url.parse()?;
@@ -228,12 +227,12 @@ async fn start(
 fn main() -> Result<()> {
     use simplelog::*;
 
-    let mut config = cli_config::Config::load(PathBuf::from("darkfid_config_file"))?;
+    let mut config = DarkfidCliConfig::load(PathBuf::from("darkfid_config_file"))?;
     let options = Arc::new(DarkfidCli::load(&mut config)?);
 
     if options.change_config {
         config.save(PathBuf::from("darkfid_config_file"))?;
-        std::process::exit(-1);
+        return Ok(());
     }
 
     let config = Arc::new(config);
@@ -268,7 +267,7 @@ fn main() -> Result<()> {
         // Run the main future on the current thread.
         .finish(|| {
             smol::future::block_on(async move {
-                start(ex2, config, options).await?;
+                start(ex2, config).await?;
                 drop(signal);
                 Ok::<(), drk::Error>(())
             })

+ 76 - 48
src/cli/client_cli/cli_config.rs

@@ -4,7 +4,42 @@ use crate::Result;
 
 use std::{fs::OpenOptions, io::prelude::*, path::PathBuf};
 
-pub struct Config {
+pub trait ClientCliConfig: Encodable + Decodable + Default {
+    fn load(path: PathBuf) -> Result<Self> {
+        let path = join_config_path(&path)?;
+        let mut file = OpenOptions::new()
+            .read(true)
+            .write(true)
+            .create(true)
+            .open(path)?;
+
+        let mut buffer: Vec<u8> = vec![];
+        file.read_to_end(&mut buffer)?;
+        if !buffer.is_empty() {
+            let config: Self = deserialize(&buffer)?;
+            Ok(config)
+        } else {
+            Ok(Self::default())
+        }
+    }
+    fn save(&self, path: PathBuf) -> Result<()> {
+        let path = join_config_path(&path)?;
+        let mut file = OpenOptions::new().write(true).create(true).open(&path)?;
+        let serialized = serialize(self);
+        file.write_all(&serialized)?;
+        Ok(())
+    }
+}
+
+impl ClientCliConfig for DarkfiCliConfig {} 
+impl ClientCliConfig for DarkfidCliConfig {} 
+
+pub struct DarkfiCliConfig {
+    pub rpc_url: String,
+    pub log_path: String,
+}
+
+pub struct DarkfidCliConfig {
     pub connect_url: String,
     pub subscriber_url: String,
     pub rpc_url: String,
@@ -12,7 +47,43 @@ pub struct Config {
     pub log_path: String,
 }
 
-impl Default for Config {
+impl Default for DarkfiCliConfig {
+    fn default() -> Self {
+        let rpc_url = String::from("127.0.0.1:8000");
+        let log_path = String::from("/tmp/darkfi_cli.log");
+        Self {
+            rpc_url,
+            log_path,
+        }
+    }
+}
+
+impl Encodable for DarkfidCliConfig {
+    fn encode<S: std::io::Write>(&self, mut s: S) -> Result<usize> {
+        let mut len = 0;
+        len += self.connect_url.encode(&mut s)?;
+        len += self.subscriber_url.encode(&mut s)?;
+        len += self.rpc_url.encode(&mut s)?;
+        len += self.database_path.encode(&mut s)?;
+        len += self.log_path.encode(&mut s)?;
+        Ok(len)
+    }
+}
+
+impl Decodable for DarkfidCliConfig {
+    fn decode<D: std::io::Read>(mut d: D) -> Result<Self> {
+        Ok(Self {
+            connect_url: Decodable::decode(&mut d)?,
+            subscriber_url: Decodable::decode(&mut d)?,
+            rpc_url: Decodable::decode(&mut d)?,
+            database_path: Decodable::decode(&mut d)?,
+            log_path: Decodable::decode(&mut d)?,
+        })
+    }
+}
+
+
+impl Default for DarkfidCliConfig {
     fn default() -> Self {
         let connect_url = String::from("127.0.0.1:3333");
         let subscriber_url = String::from("127.0.0.1:4444");
@@ -38,64 +109,21 @@ impl Default for Config {
     }
 }
 
-impl Config {
-    pub fn load(path: PathBuf) -> Result<Config> {
-        let path = join_config_path(&path)?;
-        load_config_file(path)
-    }
-    pub fn save(&self, path: PathBuf) -> Result<()> {
-        let path = join_config_path(&path)?;
-        save_config_file(self, path)
-    }
-}
-
-pub fn load_config_file(config_file: PathBuf) -> Result<Config> {
-    let mut file = OpenOptions::new()
-        .read(true)
-        .write(true)
-        .create(true)
-        .open(config_file)?;
-
-    let mut buffer: Vec<u8> = vec![];
-    file.read_to_end(&mut buffer)?;
-    if !buffer.is_empty() {
-        let config: Config = deserialize(&buffer)?;
-        Ok(config)
-    } else {
-        Ok(Config::default())
-    }
-}
-
-pub fn save_config_file(config: &Config, config_file: PathBuf) -> Result<()> {
-    let mut file = OpenOptions::new()
-        .write(true)
-        .create(true)
-        .open(&config_file)?;
-    let serialized = serialize(config);
-    file.write_all(&serialized)?;
-    Ok(())
-}
-
-impl Encodable for Config {
+impl Encodable for DarkfiCliConfig {
     fn encode<S: std::io::Write>(&self, mut s: S) -> Result<usize> {
         let mut len = 0;
-        len += self.connect_url.encode(&mut s)?;
-        len += self.subscriber_url.encode(&mut s)?;
         len += self.rpc_url.encode(&mut s)?;
-        len += self.database_path.encode(&mut s)?;
         len += self.log_path.encode(&mut s)?;
         Ok(len)
     }
 }
 
-impl Decodable for Config {
+impl Decodable for DarkfiCliConfig {
     fn decode<D: std::io::Read>(mut d: D) -> Result<Self> {
         Ok(Self {
-            connect_url: Decodable::decode(&mut d)?,
-            subscriber_url: Decodable::decode(&mut d)?,
             rpc_url: Decodable::decode(&mut d)?,
-            database_path: Decodable::decode(&mut d)?,
             log_path: Decodable::decode(&mut d)?,
         })
     }
 }
+

+ 8 - 8
src/cli/client_cli/darkfid_cli.rs

@@ -1,4 +1,4 @@
-use crate::cli::cli_config;
+use super::cli_config::DarkfidCliConfig;
 use crate::Result;
 
 use clap::{App, AppSettings, Arg};
@@ -9,11 +9,11 @@ pub struct DarkfidCli {
 }
 
 impl DarkfidCli {
-    pub fn load(config: &mut cli_config::Config) -> Result<Self> {
-        let app = App::new("Wallet CLI")
+    pub fn load(config: &mut DarkfidCliConfig) -> Result<Self> {
+        let app = App::new("Darkfi Daemon CLI")
             .version("0.1.0")
             .author("Amir Taaki <amir@dyne.org>")
-            .about("Run Darkfi daemon")
+            .about("Run Darkfi Daemon")
             .arg(
                 Arg::new("verbose")
                     .short('v')
@@ -80,19 +80,19 @@ impl DarkfidCli {
                         }
                         if let Some(v) = c.value_of("subscriber_url") {
                             config.subscriber_url = v.to_string();
-                            println!("Change Subscriber Url To {}", config.connect_url);
+                            println!("Change Subscriber Url To {}", config.subscriber_url);
                         }
                         if let Some(v) = c.value_of("rpc_url") {
                             config.rpc_url = v.to_string();
-                            println!("Change RPC Url To {}", config.connect_url);
+                            println!("Change RPC Url To {}", config.rpc_url);
                         }
                         if let Some(v) = c.value_of("database_path") {
                             config.database_path = v.to_string();
-                            println!("Change Database Path To {}", config.connect_url);
+                            println!("Change Database Path To {}", config.database_path);
                         }
                         if let Some(v) = c.value_of("log_path") {
                             config.log_path = v.to_string();
-                            println!("Change Log Path To {}", config.connect_url);
+                            println!("Change Log Path To {}", config.log_path);
                         }
                     }
                     _ => {}

+ 1 - 0
src/cli/client_cli/mod.rs

@@ -1,2 +1,3 @@
 pub mod cli_config;
+pub mod darkfi_cli; 
 pub mod darkfid_cli;

+ 2 - 1
src/cli/mod.rs

@@ -1,5 +1,6 @@
 pub mod client_cli;
 pub mod service_cli;
 
-pub use client_cli::{cli_config, darkfid_cli::DarkfidCli};
+pub use client_cli::{darkfi_cli::DarkfiCli, darkfid_cli::DarkfidCli};
+pub use client_cli::cli_config::{DarkfidCliConfig, DarkfiCliConfig, ClientCliConfig};
 pub use service_cli::ServiceCli;

+ 2 - 2
src/rpc/jsonserver.rs

@@ -1,4 +1,4 @@
-use crate::cli::cli_config;
+use crate::cli::DarkfidCliConfig;
 use crate::rpc::adapter::RpcAdapter;
 use crate::{Error, Result};
 use async_executor::Executor;
@@ -73,7 +73,7 @@ pub async fn listen(
 
 pub async fn start(
     executor: Arc<Executor<'_>>,
-    config: Arc<cli_config::Config>,
+    config: Arc<DarkfidCliConfig>,
     adapter: RpcAdapter,
 ) -> Result<()> {
     let rpc = RpcInterface::new(adapter)?;