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

Rework configuration structs for new reference config files.

parazyd 4 лет назад
Родитель
Сommit
2c9fd99d98
4 измененных файлов с 79 добавлено и 97 удалено
  1. 17 18
      src/bin/cashierd.rs
  2. 11 10
      src/bin/darkfid.rs
  3. 1 1
      src/bin/drk.rs
  4. 50 68
      src/cli/cli_config.rs

+ 17 - 18
src/bin/cashierd.rs

@@ -29,7 +29,6 @@ use std::path::PathBuf;
 
 #[derive(Clone)]
 struct Cashierd {
-    verbose: bool,
     config: CashierdConfig,
     bridge: Arc<Bridge>,
     cashier_wallet: Arc<CashierDb>,
@@ -61,22 +60,20 @@ impl RequestHandler for Cashierd {
 }
 
 impl Cashierd {
-    fn new(verbose: bool, executor: Arc<Executor<'static>>, config_path: PathBuf) -> Result<Self> {
-        let mint_params_path = join_config_path(&PathBuf::from("cashier_mint.params"))?;
-        let spend_params_path = join_config_path(&PathBuf::from("cashier_spend.params"))?;
-
+    fn new(executor: Arc<Executor<'static>>, config_path: PathBuf) -> Result<Self> {
         let config: CashierdConfig = Config::<CashierdConfig>::load(config_path)?;
 
-        let cashier_wallet_path = join_config_path(&PathBuf::from("cashier_wallet.db"))?;
-
-        let client_wallet_path = join_config_path(&PathBuf::from("cashier_client_wallet.db"))?;
-
-        let cashier_wallet = CashierDb::new(&cashier_wallet_path, config.password.clone())?;
-        let client_wallet = WalletDb::new(&client_wallet_path.clone(), config.password.clone())?;
+        let cashier_wallet = CashierDb::new(
+            &PathBuf::from(config.cashier_wallet_path.clone()),
+            config.cashier_wallet_password.clone(),
+        )?;
 
-        let database_path = join_config_path(&PathBuf::from("cashier_database.db"))?;
+        let client_wallet = WalletDb::new(
+            &PathBuf::from(config.client_wallet_path.clone()),
+            config.client_wallet_password.clone(),
+        )?;
 
-        let rocks = Rocks::new(&database_path)?;
+        let rocks = Rocks::new(&PathBuf::from(config.database_path.clone()))?;
 
         let client = Client::new(
             rocks,
@@ -84,7 +81,10 @@ impl Cashierd {
                 config.gateway_url.parse()?,
                 config.gateway_subscriber_url.parse()?,
             ),
-            (mint_params_path, spend_params_path),
+            (
+                PathBuf::from(config.mint_params.clone()),
+                PathBuf::from(config.spend_params.clone()),
+            ),
             client_wallet.clone(),
         )?;
 
@@ -99,7 +99,6 @@ impl Cashierd {
         let bridge = bridge::Bridge::new();
 
         Ok(Self {
-            verbose,
             config: config.clone(),
             bridge,
             cashier_wallet,
@@ -170,8 +169,8 @@ impl Cashierd {
         });
 
         let cfg = RpcServerConfig {
-            socket_addr: self.config.clone().rpc_url,
-            use_tls: self.config.use_tls,
+            socket_addr: self.config.clone().listen_url,
+            use_tls: self.config.serve_tls,
             identity_path: self.config.clone().tls_identity_path,
             identity_pass: self.config.clone().tls_identity_password,
         };
@@ -382,6 +381,6 @@ async fn main() -> Result<()> {
 
     simple_logger::init_with_level(loglevel)?;
     let ex = Arc::new(Executor::new());
-    let cashierd = Cashierd::new(args.clone().is_present("verbose"), ex.clone(), config_path)?;
+    let cashierd = Cashierd::new(ex.clone(), config_path)?;
     cashierd.start().await
 }

+ 11 - 10
src/bin/darkfid.rs

@@ -23,9 +23,6 @@ struct Darkfid {
     config: DarkfidConfig,
     wallet: Arc<WalletDb>,
     tokenlist: Value,
-    // clientdb:
-    // mint_params:
-    // spend_params:
 }
 
 #[async_trait]
@@ -56,7 +53,11 @@ impl RequestHandler for Darkfid {
 impl Darkfid {
     fn new(config_path: PathBuf) -> Result<Self> {
         let config: DarkfidConfig = Config::<DarkfidConfig>::load(config_path)?;
-        let wallet = WalletDb::new(&PathBuf::from(&config.wallet_path), config.password.clone())?;
+        let wallet = WalletDb::new(
+            &PathBuf::from(&config.wallet_path),
+            config.wallet_password.clone(),
+        )?;
+        // TODO: FIXME
         let file_contents = std::fs::read_to_string("token/solanatokenlist.json")?;
         let tokenlist: Value = serde_json::from_str(&file_contents)?;
 
@@ -297,16 +298,16 @@ async fn main() -> Result<()> {
 
     simple_logger::init_with_level(loglevel)?;
 
-    let dfi = Darkfid::new(config_path)?;
+    let darkfid = Darkfid::new(config_path)?;
 
     let server_config = RpcServerConfig {
-        socket_addr: dfi.config.clone().rpc_url,
-        use_tls: dfi.config.use_tls,
-        identity_path: dfi.config.clone().tls_identity_path,
-        identity_pass: dfi.config.clone().tls_identity_password,
+        socket_addr: darkfid.config.clone().listen_address,
+        use_tls: darkfid.config.serve_tls,
+        identity_path: darkfid.config.clone().tls_identity_path,
+        identity_pass: darkfid.config.clone().tls_identity_password,
     };
 
-    listen_and_serve(server_config, dfi).await
+    listen_and_serve(server_config, darkfid).await
 }
 
 mod tests {

+ 1 - 1
src/bin/drk.rs

@@ -115,7 +115,7 @@ impl Drk {
 }
 
 async fn start(config: &DrkConfig, options: ArgMatches<'_>) -> Result<()> {
-    let client = Drk::new(config.rpc_url.clone());
+    let client = Drk::new(config.darkfid_url.clone());
 
     if options.is_present("hello") {
         let reply = client.say_hello().await?;

+ 50 - 68
src/cli/cli_config.rs

@@ -1,14 +1,14 @@
-use crate::{Error, Result};
-
 use serde::de::DeserializeOwned;
 use serde::{Deserialize, Serialize};
-use std::marker::PhantomData;
 use std::{
     fs,
+    marker::PhantomData,
     path::{Path, PathBuf},
     str,
 };
 
+use crate::{Error, Result};
+
 #[derive(Clone, Default)]
 pub struct Config<T> {
     config: PhantomData<T>,
@@ -22,101 +22,83 @@ impl<T: Serialize + DeserializeOwned> Config<T> {
             let config: T = toml::from_str(str_buff)?;
             Ok(config)
         } else {
-            println!("No config files were found in .config/darkfi. Please follow the instructions in the README and add default configs.");
+            println!("Could not parse configuration");
+            println!("Please follow the instructions in the README");
             Err(Error::ConfigNotFound)
         }
     }
 }
 
-#[derive(Serialize, Deserialize, Debug)]
+/// The configuration for drk
+#[derive(Clone, Serialize, Deserialize, Debug)]
 pub struct DrkConfig {
-    pub rpc_url: String,
-    pub log_path: String,
+    /// The URL where darkfid is listening on.
+    pub darkfid_url: String,
 }
 
+/// The configuration for darkfid
 #[derive(Clone, Serialize, Deserialize, Debug)]
 pub struct DarkfidConfig {
-    #[serde(rename = "connect_url")]
-    pub connect_url: String,
-
-    #[serde(rename = "subscriber_url")]
-    pub subscriber_url: String,
-
-    #[serde(rename = "cashier_url")]
-    pub cashier_url: String,
-
-    #[serde(rename = "rpc_url")]
-    pub rpc_url: String,
-
-    #[serde(rename = "use_tls")]
-    pub use_tls: bool,
-
-    #[serde(rename = "tls_identity_path")]
+    /// The address where darkfid should bind its RPC socket
+    pub listen_address: String,
+    /// Whether to listen with TLS or plain TCP
+    pub serve_tls: bool,
+    /// Path to DER-formatted PKCS#12 archive. (Unused if serve_tls=false)
     pub tls_identity_path: String,
-
-    #[serde(rename = "tls_identity_password")]
+    /// Password for the TLS identity. (Unused if serve_tls=false)
     pub tls_identity_password: String,
-
-    //TODO: reimplement this
-    //#[serde(rename = "database_path")]
-    //pub database_path: String,
-    #[serde(rename = "wallet_path")]
+    /// The RPC endpoint for a selected cashier
+    pub cashier_url: String,
+    /// Path to the client database
+    pub database_path: String,
+    /// Path to the wallet database
     pub wallet_path: String,
-
-    #[serde(rename = "log_path")]
-    pub log_path: String,
-
-    #[serde(rename = "password")]
-    pub password: String,
+    /// The wallet password
+    pub wallet_password: String,
 }
 
 #[derive(Serialize, Deserialize, Debug)]
 pub struct GatewaydConfig {
-    #[serde(rename = "connect_url")]
     pub accept_url: String,
-
-    #[serde(rename = "publisher_url")]
     pub publisher_url: String,
-
-    #[serde(rename = "log_path")]
-    pub log_path: String,
 }
 
 #[derive(Clone, Debug, Serialize, Deserialize)]
 pub struct FeatureNetwork {
+    /// Network name
     pub name: String,
+    /// Blockchain (mainnet/testnet/etc.)
     pub blockchain: String,
 }
 
 #[derive(Clone, Serialize, Deserialize, Debug)]
 pub struct CashierdConfig {
-    #[serde(rename = "rpc_url")]
-    pub rpc_url: String,
-
-    #[serde(rename = "gateway_url")]
-    pub gateway_url: String,
-
-    #[serde(rename = "gateway_subscriber_url")]
-    pub gateway_subscriber_url: String,
-
-    #[serde(rename = "log_path")]
-    pub log_path: String,
-
-    #[serde(rename = "password")]
-    pub password: String,
-
-    #[serde(rename = "use_tls")]
-    pub use_tls: bool,
-
-    #[serde(rename = "tls_identity_path")]
+    /// The endpoint where cashierd will bind its RPC socket
+    pub listen_url: String,
+    /// Whether to listen with TLS or plain TCP
+    pub serve_tls: bool,
+    /// Path to DER-formatted PKCS#12 archive. (Unused if serve_tls=false)
     pub tls_identity_path: String,
-
-    #[serde(rename = "tls_identity_password")]
+    /// Password for the TLS identity. (Unused if serve_tls=false)
     pub tls_identity_password: String,
-
-    #[serde(rename = "client_password")]
-    pub client_password: String,
-
-    #[serde(rename = "networks")]
+    /// ?
+    pub gateway_url: String,
+    /// ?
+    pub gateway_subscriber_url: String,
+    /// Path to mint.params
+    pub mint_params: String,
+    /// Path to spend.params
+    pub spend_params: String,
+    /// Path to cashierd wallet
+    pub cashier_wallet_path: String,
+    /// Password for cashierd wallet
+    pub cashier_wallet_password: String,
+    /// Path to client wallet
+    pub client_wallet_path: String,
+    /// Password for client wallet
+    pub client_wallet_password: String,
+    /// Path to database
+    pub database_path: String,
+    /// The configured networks to use
     pub networks: Vec<FeatureNetwork>,
 }