Forráskód Böngészése

Update cashierd configs and options

Janus 5 éve
szülő
commit
878f3eb1dc
6 módosított fájl, 89 hozzáadás és 50 törlés
  1. 13 39
      src/bin/cashierd.rs
  2. 23 0
      src/cli/cashierd_cli.rs
  3. 40 0
      src/cli/cli_config.rs
  4. 2 0
      src/cli/mod.rs
  5. 10 10
      src/service/bitcoin_bridge.rs
  6. 1 1
      src/service/mod.rs

+ 13 - 39
src/bin/cashierd.rs

@@ -4,11 +4,11 @@ use std::sync::Arc;
 
 use std::fs::OpenOptions;
 use std::io::Read;
-use std::{fs, path::PathBuf};
+use std::{fs, path::Path, path::PathBuf};
 use toml;
 
 use drk::blockchain::{rocks::columns, Rocks, RocksColumn};
-use drk::cli::{ServiceCli, CashierdConfig};
+use drk::cli::{CashierdCli, CashierdConfig};
 use drk::service::CashierService;
 
 use drk::util::join_config_path;
@@ -43,49 +43,23 @@ async fn start(executor: Arc<Executor<'_>>, config: Arc<&CashierdConfig>) -> Res
     Ok(())
 }
 
-fn set_default() -> Result<CashierdConfig> {
-    let config_file = CashierdConfig {
-        accept_url: String::from("127.0.0.1:7777"),
-        database_path: String::from("cashierd.db"),
-        log_path: String::from("/tmp/cashierd.log"),
-    };
-    Ok(config_file)
-}
-
 fn main() -> Result<()> {
     use simplelog::*;
 
     let ex = Arc::new(Executor::new());
     let (signal, shutdown) = async_channel::unbounded::<()>();
 
-    let config_path = PathBuf::from("cashierd.toml");
-    let path = join_config_path(&config_path).unwrap();
+    let path = join_config_path(&PathBuf::from("cashierd.toml")).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 config: CashierdConfig = if Path::new(&path).exists() {
+        CashierdConfig::load(path)?
+    } else {
+        CashierdConfig::load_default(path)?
+    };
 
-    // read from config file
-    let config: CashierdConfig = toml::from_str(str_buff)?;
-    let config_pointer = Arc::new(&config);
+    let config_ptr = Arc::new(&config);
 
-    let options = ServiceCli::load()?;
+    let options = CashierdCli::load()?;
 
     let logger_config = ConfigBuilder::new().set_time_format_str("%T%.6f").build();
 
@@ -96,7 +70,6 @@ fn main() -> Result<()> {
     };
 
     let log_path = config.log_path.clone();
-
     CombinedLogger::init(vec![
         TermLogger::new(debug_level, logger_config, TerminalMode::Mixed).unwrap(),
         WriteLogger::new(
@@ -105,7 +78,8 @@ fn main() -> Result<()> {
             std::fs::File::create(log_path).unwrap(),
         ),
     ])
-        .unwrap();
+    .unwrap();
+
 
     let ex2 = ex.clone();
 
@@ -115,7 +89,7 @@ fn main() -> Result<()> {
         // Run the main future on the current thread.
         .finish(|| {
             smol::future::block_on(async move {
-                start(ex2, config_pointer).await?;
+                start(ex2, config_ptr).await?;
                 drop(signal);
                 Ok::<(), drk::Error>(())
             })

+ 23 - 0
src/cli/cashierd_cli.rs

@@ -0,0 +1,23 @@
+use crate::Result;
+
+pub struct CashierdCli {
+    pub verbose: bool,
+}
+
+impl CashierdCli {
+    pub fn load() -> Result<Self> {
+        let app = clap_app!(dfi =>
+            (version: "0.1.0")
+            (author: "Amir Taaki <amir@dyne.org>")
+            (about: "run service daemon")
+            (@arg VERBOSE: -v --verbose "Increase verbosity")
+        )
+        .get_matches();
+
+        let verbose = app.is_present("VERBOSE");
+
+        Ok(Self {
+            verbose,
+        })
+    }
+}

+ 40 - 0
src/cli/cli_config.rs

@@ -160,3 +160,43 @@ impl Default for GatewaydConfig {
         }
     }
 }
+
+#[derive(Serialize, Deserialize, Debug)]
+pub struct CashierdConfig {
+    #[serde(default)]
+    #[serde(rename = "connect_url")]
+    pub accept_url: String,
+
+    #[serde(default)]
+    #[serde(rename = "database_path")]
+    pub database_path: String,
+
+    #[serde(default)]
+    #[serde(rename = "log_path")]
+    pub log_path: String,
+}
+
+impl CashierdConfig {
+    pub fn load(path: PathBuf) -> Result<Self> {
+        let toml = fs::read(&path)?;
+        let str_buff = str::from_utf8(&toml)?;
+        let config: Self = toml::from_str(str_buff)?;
+        Ok(config)
+    }
+    pub fn load_default(path: PathBuf) -> Result<Self> {
+        let toml = Self::default();
+        let config_file = toml::to_string(&toml)?;
+        fs::write(&path, &config_file)?;
+        let config = Self::load(path)?;
+        Ok(config)
+    }
+}
+
+impl Default for CashierdConfig {
+    fn default() -> Self {
+        let accept_url = String::from("127.0.0.1:7777");
+        let database_path = String::from("cashierd.db");
+        let log_path = String::from("/tmp/cashierd.log");
+        Self { accept_url, database_path, log_path }
+    }
+}

+ 2 - 0
src/cli/mod.rs

@@ -2,9 +2,11 @@ pub mod cli_config;
 pub mod darkfid_cli;
 pub mod drk_cli;
 pub mod gatewayd_cli;
+pub mod cashierd_cli;
 
 pub use cli_config::{DarkfidConfig, DrkConfig, CashierdConfig, GatewaydConfig};
 pub use darkfid_cli::DarkfidCli;
 pub use drk_cli::DrkCli;
 pub use drk_cli::Transfer;
 pub use gatewayd_cli::GatewaydCli;
+pub use cashierd_cli::CashierdCli;

+ 10 - 10
src/service/bitcoin_bridge.rs

@@ -26,16 +26,16 @@ enum CashierCommand {
     GetBTC,
 }
 
-pub struct CashierKeys {
+pub struct BitcoinKeys {
     secret_key: SecretKey,
     bitcoin_private_key: PrivateKey,
     pub bitcoin_public_key: BitcoinPubKey,
     pub pub_address: Address,
 }
-impl CashierKeys {
+impl BitcoinKeys {
     pub fn new(
 
-    ) -> Result<CashierKeys> {
+    ) -> Result<BitcoinKeys> {
 
         let context = secp256k1::Secp256k1::new();
 
@@ -55,12 +55,12 @@ impl CashierKeys {
 
         //let public_key = PublicKey::from_secret_key(&context, &secret_key);
 
-        // Use mainnet
-        let bitcoin_private_key = PrivateKey::new(secret_key, Network::Bitcoin);
+        // Use Testnet
+        let bitcoin_private_key = PrivateKey::new(secret_key, Network::Testnet);
 
         let bitcoin_public_key = BitcoinPubKey::from_private_key(&context, &bitcoin_private_key);
 
-        let pub_address = Address::p2pkh(&bitcoin_public_key, Network::Bitcoin);
+        let pub_address = Address::p2pkh(&bitcoin_public_key, Network::Testnet);
 
         Ok(Self {
             secret_key,
@@ -148,13 +148,13 @@ impl CashierService {
                 let zkpub = request.get_payload();
 
                 // Generate bitcoin Address
-                let btc_keys = CashierKeys::new().unwrap();
+                let btc_keys = BitcoinKeys::new().unwrap();
                 let deposit_address = btc_keys.get_deposit_address();
 
                 let mut reply = Reply::from(&request, CashierError::NoError as u32, vec![]);
-                //if let None = error {
-                //    reply.set_error(CashierError::UpdateIndex as u32);
-                //}
+                // if let None = error {
+                //     reply.set_error(CashierError::UpdateIndex as u32);
+                // }
                 // send reply
                 send_queue.send((peer, reply)).await?;
 

+ 1 - 1
src/service/mod.rs

@@ -4,4 +4,4 @@ pub mod bitcoin_bridge;
 
 pub use gateway::{GatewayClient, GatewayService, GatewaySlabsSubscriber};
 
-pub use bitcoin_bridge::{CashierKeys, CashierService, CashierClient};
+pub use bitcoin_bridge::{BitcoinKeys, CashierService, CashierClient};