Просмотр исходного кода

fixed toml load logic error and simplified code into structs

lunar-mining 5 лет назад
Родитель
Сommit
5e09f11922
6 измененных файлов с 32 добавлено и 126 удалено
  1. 9 45
      src/bin/darkfid.rs
  2. 10 38
      src/bin/drk.rs
  3. 9 40
      src/bin/gatewayd.rs
  4. 1 1
      src/cli/mod.rs
  5. 2 2
      src/lib.rs
  6. 1 0
      src/util.rs

+ 9 - 45
src/bin/darkfid.rs

@@ -11,17 +11,10 @@ use drk::crypto::{
 use drk::serial::Decodable;
 use drk::serial::Decodable;
 use drk::service::{GatewayClient, GatewaySlabsSubscriber};
 use drk::service::{GatewayClient, GatewaySlabsSubscriber};
 use drk::state::{state_transition, ProgramState, StateUpdate};
 use drk::state::{state_transition, ProgramState, StateUpdate};
-use drk::util;
 use drk::util::join_config_path;
 use drk::util::join_config_path;
 use drk::wallet::{WalletDb, WalletPtr};
 use drk::wallet::{WalletDb, WalletPtr};
 use drk::{tx, Result};
 use drk::{tx, Result};
 use log::*;
 use log::*;
-use std::fs;
-use std::str;
-//use std::fs::File;
-use std::fs::OpenOptions;
-use toml;
-//use drk::rpc::
 use drk::rpc::adapter::RpcAdapter;
 use drk::rpc::adapter::RpcAdapter;
 use drk::rpc::jsonserver;
 use drk::rpc::jsonserver;
 
 
@@ -34,7 +27,6 @@ use rand::rngs::OsRng;
 use rusqlite::Connection;
 use rusqlite::Connection;
 
 
 use async_std::sync::Arc;
 use async_std::sync::Arc;
-use std::io::Read;
 use std::net::SocketAddr;
 use std::net::SocketAddr;
 use std::path::Path;
 use std::path::Path;
 use std::path::PathBuf;
 use std::path::PathBuf;
@@ -228,48 +220,20 @@ async fn start(executor: Arc<Executor<'_>>, config: Arc<&DarkfidConfig>) -> Resu
     Ok(())
     Ok(())
 }
 }
 
 
-fn set_default() -> Result<DarkfidConfig> {
-    let config_file = DarkfidConfig {
-        connect_url: String::from("127.0.0.1:3333"),
-        subscriber_url: String::from("127.0.0.1:4444"),
-        rpc_url: String::from("127.0.0.1:8000"),
-        database_path: String::from("darkfid.db"),
-        log_path: String::from("/tmp/darkfid.log"),
-        password: String::from(""),
-    };
-    Ok(config_file)
-}
-
 fn main() -> Result<()> {
 fn main() -> Result<()> {
     use simplelog::*;
     use simplelog::*;
 
 
     let options = Arc::new(DarkfidCli::load()?);
     let options = Arc::new(DarkfidCli::load()?);
-    let config_path = PathBuf::from("darkfid.toml");
-    let path = util::join_config_path(&config_path).unwrap();
-
-    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() {
-        // set the default setting
-        let config_file = set_default()?;
-        let config_file = toml::to_string(&config_file)?;
-        fs::write(&path, &config_file)?;
-    }
 
 
-    // reload the config
-    let toml = fs::read(&path)?;
-    let str_buff = str::from_utf8(&toml)?;
+    let path = join_config_path(&PathBuf::from("darkfid.toml")).unwrap();
+
+    let config: DarkfidConfig = if Path::new(&path).exists() {
+        DarkfidConfig::load(path)?
+    } else {
+        DarkfidConfig::load_default(path)?
+    };
 
 
-    // read from config file
-    let config: DarkfidConfig = toml::from_str(str_buff)?;
-    let config_pointer = Arc::new(&config);
+    let config_ptr = Arc::new(&config);
 
 
     let ex = Arc::new(Executor::new());
     let ex = Arc::new(Executor::new());
     let (signal, shutdown) = async_channel::unbounded::<()>();
     let (signal, shutdown) = async_channel::unbounded::<()>();
@@ -301,7 +265,7 @@ fn main() -> Result<()> {
         // Run the main future on the current thread.
         // Run the main future on the current thread.
         .finish(|| {
         .finish(|| {
             smol::future::block_on(async move {
             smol::future::block_on(async move {
-                start(ex2, config_pointer).await?;
+                start(ex2, config_ptr).await?;
                 drop(signal);
                 drop(signal);
                 Ok::<(), drk::Error>(())
                 Ok::<(), drk::Error>(())
             })
             })

+ 10 - 38
src/bin/drk.rs

@@ -1,15 +1,11 @@
 use drk::cli::{DrkCli, DrkConfig};
 use drk::cli::{DrkCli, DrkConfig};
-use drk::util;
+use drk::util::join_config_path;
 use drk::Result;
 use drk::Result;
 use log::*;
 use log::*;
-use std::fs::OpenOptions;
-use std::io::Read;
-use std::str;
-use toml;
 
 
 use async_std::sync::Arc;
 use async_std::sync::Arc;
 use std::collections::HashMap;
 use std::collections::HashMap;
-use std::{fs, path::PathBuf};
+use std::path::{Path, PathBuf};
 
 
 type Payload = HashMap<String, String>;
 type Payload = HashMap<String, String>;
 
 
@@ -109,44 +105,20 @@ async fn start(config: Arc<&DrkConfig>, options: Arc<DrkCli>) -> Result<()> {
     Ok(())
     Ok(())
 }
 }
 
 
-fn set_default() -> Result<DrkConfig> {
-    let config_file = DrkConfig {
-        rpc_url: String::from("http://127.0.0.1:8000"),
-        log_path: String::from("/tmp/drk.log"),
-    };
-    Ok(config_file)
-}
-
 fn main() -> Result<()> {
 fn main() -> Result<()> {
     use simplelog::*;
     use simplelog::*;
 
 
     let options = Arc::new(DrkCli::load()?);
     let options = Arc::new(DrkCli::load()?);
 
 
-    let config_path = PathBuf::from("drk.toml");
-    let path = util::join_config_path(&config_path).unwrap();
-
-    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() {
-        // set the default setting
-        let config_file = set_default()?;
-        let config_file = toml::to_string(&config_file)?;
-        fs::write(&path, &config_file)?;
-    }
+    let path = join_config_path(&PathBuf::from("drk.toml")).unwrap();
 
 
-    // reload the config
-    let toml = fs::read(&path)?;
-    let str_buff = str::from_utf8(&toml)?;
+    let config: DrkConfig = if Path::new(&path).exists() {
+        DrkConfig::load(path)?
+    } else {
+        DrkConfig::load_default(path)?
+    };
 
 
-    // read from config file
-    let config: DrkConfig = toml::from_str(str_buff)?;
-    let config_pointer = Arc::new(&config);
+    let config_ptr = Arc::new(&config);
 
 
     let logger_config = ConfigBuilder::new().set_time_format_str("%T%.6f").build();
     let logger_config = ConfigBuilder::new().set_time_format_str("%T%.6f").build();
 
 
@@ -167,7 +139,7 @@ fn main() -> Result<()> {
     ])
     ])
     .unwrap();
     .unwrap();
 
 
-    futures::executor::block_on(start(config_pointer, options))?;
+    futures::executor::block_on(start(config_ptr, options))?;
 
 
     Ok(())
     Ok(())
 }
 }

+ 9 - 40
src/bin/gatewayd.rs

@@ -1,16 +1,12 @@
 use std::net::SocketAddr;
 use std::net::SocketAddr;
-use std::str;
 use std::sync::Arc;
 use std::sync::Arc;
 
 
-use std::fs::OpenOptions;
-use std::io::Read;
-use std::{fs, path::PathBuf};
-use toml;
 use drk::blockchain::{rocks::columns, Rocks, RocksColumn};
 use drk::blockchain::{rocks::columns, Rocks, RocksColumn};
 use drk::cli::{GatewaydCli, GatewaydConfig};
 use drk::cli::{GatewaydCli, GatewaydConfig};
 use drk::service::GatewayService;
 use drk::service::GatewayService;
 use drk::util::join_config_path;
 use drk::util::join_config_path;
 use drk::Result;
 use drk::Result;
+use std::path::{Path, PathBuf};
 
 
 extern crate clap;
 extern crate clap;
 use async_executor::Executor;
 use async_executor::Executor;
@@ -31,48 +27,21 @@ async fn start(executor: Arc<Executor<'_>>, config: Arc<&GatewaydConfig>) -> Res
     Ok(())
     Ok(())
 }
 }
 
 
-fn set_default() -> Result<GatewaydConfig> {
-    let config_file = GatewaydConfig {
-        accept_url: String::from("127.0.0.1:3333"),
-        publisher_url: String::from("127.0.0.1:4444"),
-        database_path: String::from("gatewayd.db"),
-        log_path: String::from("/tmp/gatewayd.log"),
-    };
-    Ok(config_file)
-}
-
 fn main() -> Result<()> {
 fn main() -> Result<()> {
     use simplelog::*;
     use simplelog::*;
 
 
     let ex = Arc::new(Executor::new());
     let ex = Arc::new(Executor::new());
     let (signal, shutdown) = async_channel::unbounded::<()>();
     let (signal, shutdown) = async_channel::unbounded::<()>();
 
 
-    let config_path = PathBuf::from("gatewayd.toml");
-    let path = join_config_path(&config_path).unwrap();
-
-    let mut file = OpenOptions::new()
-        .read(true)
-        .write(true)
-        .create(true)
-        .open(&path)?;
+    let path = join_config_path(&PathBuf::from("gatewayd.toml")).unwrap();
 
 
-    let mut buffer: Vec<u8> = vec![];
-    file.read_to_end(&mut buffer)?;
-
-    if buffer.is_empty() {
-        // set the default setting
-        let config_file = set_default()?;
-        let config_file = toml::to_string(&config_file)?;
-        fs::write(&path, &config_file)?;
-    }
-
-    // reload the config
-    let toml = fs::read(&path)?;
-    let str_buff = str::from_utf8(&toml)?;
+    let config: GatewaydConfig = if Path::new(&path).exists() {
+        GatewaydConfig::load(path)?
+    } else {
+        GatewaydConfig::load_default(path)?
+    };
 
 
-    // read from config file
-    let config: GatewaydConfig = toml::from_str(str_buff)?;
-    let config_pointer = Arc::new(&config);
+    let config_ptr = Arc::new(&config);
 
 
     let options = GatewaydCli::load()?;
     let options = GatewaydCli::load()?;
 
 
@@ -103,7 +72,7 @@ fn main() -> Result<()> {
         // Run the main future on the current thread.
         // Run the main future on the current thread.
         .finish(|| {
         .finish(|| {
             smol::future::block_on(async move {
             smol::future::block_on(async move {
-                start(ex2, config_pointer).await?;
+                start(ex2, config_ptr).await?;
                 drop(signal);
                 drop(signal);
                 Ok::<(), drk::Error>(())
                 Ok::<(), drk::Error>(())
             })
             })

+ 1 - 1
src/cli/mod.rs

@@ -3,7 +3,7 @@ pub mod darkfid_cli;
 pub mod drk_cli;
 pub mod drk_cli;
 pub mod gatewayd_cli;
 pub mod gatewayd_cli;
 
 
-pub use cli_config::{DarkfidConfig, GatewaydConfig, DrkConfig};
+pub use cli_config::{DarkfidConfig, DrkConfig, GatewaydConfig};
 pub use darkfid_cli::DarkfidCli;
 pub use darkfid_cli::DarkfidCli;
 pub use drk_cli::DrkCli;
 pub use drk_cli::DrkCli;
 pub use drk_cli::Transfer;
 pub use drk_cli::Transfer;

+ 2 - 2
src/lib.rs

@@ -29,8 +29,8 @@ pub use crate::error::{Error, Result};
 pub use crate::net::p2p::P2p;
 pub use crate::net::p2p::P2p;
 pub use crate::serial::{Decodable, Encodable};
 pub use crate::serial::{Decodable, Encodable};
 pub use crate::vm::{
 pub use crate::vm::{
-    AllocType, ConstraintInstruction, CryptoOperation, VariableIndex, VariableRef, ZkVmCircuit,
-    ZkVirtualMachine,
+    AllocType, ConstraintInstruction, CryptoOperation, VariableIndex, VariableRef,
+    ZkVirtualMachine, ZkVmCircuit,
 };
 };
 
 
 pub type Bytes = Vec<u8>;
 pub type Bytes = Vec<u8>;

+ 1 - 0
src/util.rs

@@ -11,3 +11,4 @@ pub fn join_config_path(file: &PathBuf) -> Result<PathBuf> {
     path.push(file);
     path.push(file);
     Ok(path)
     Ok(path)
 }
 }
+