ソースを参照

More general cleanups and better config variable naming.

parazyd 4 年 前
コミット
f33f2c30be

+ 9 - 6
example/config/cashierd.toml

@@ -4,7 +4,7 @@
 ## your daemon properly.
 
 # The endpoint where cashierd will bind its RPC socket
-listen_url = "127.0.0.1:9000"
+rpc_listen_address = "127.0.0.1:9000"
 
 # Whether to listen with TLS or plain TCP
 serve_tls = false
@@ -12,19 +12,22 @@ serve_tls = false
 # Path to DER-formatted PKCS#12 archive. (Unused if serve_tls=false)
 # This can be created using openssl:
 # openssl pkcs12 -export -out identity.pfx -inkey key.pem -in cert.pem -certfile chain_certs.pem
-tls_identity_path = "~/.config/darkfi/darkfid_identity.pfx"
+tls_identity_path = "~/.config/darkfi/cashierd_identity.pfx"
 
 # Password for the created TLS identity. (Unused if serve_tls=false)
 tls_identity_password = "FOOBAR"
 
-gateway_url = "127.0.0.1:3333"
-gateway_subscriber_url = "127.0.0.1:4444"
+# The endpoint to a gatewayd protocol API
+gateway_protocol_url = "127.0.0.1:3333"
+
+# The endpoint to a gatewayd publisher API
+gateway_publisher_url = "127.0.0.1:4444"
 
 # Path to mint.params
-mint_params = "~/.config/darkfi/cashierd_mint.params"
+mint_params_path = "~/.config/darkfi/cashierd_mint.params"
 
 # Path to spend.params
-spend_params = "~/.config/darkfi/cashierd_spend.params"
+spend_params_path = "~/.config/darkfi/cashierd_spend.params"
 
 # Path to cashierd wallet
 cashier_wallet_path = "~/.config/darkfi/cashier_wallet.db"

+ 3 - 3
example/config/darkfid.toml

@@ -4,7 +4,7 @@
 ## your daemon properly.
 
 # The address where darkfid should bind its RPC socket
-listen_address = "127.0.0.1:8000"
+rpc_listen_address = "127.0.0.1:8000"
 
 # Whether to listen with TLS or plain TCP
 serve_tls = false
@@ -18,8 +18,8 @@ tls_identity_path = "~/.config/darkfi/darkfid_identity.pfx"
 tls_identity_password = "FOOBAR"
 
 # The RPC endpoint for a selected cashier
-cashier_url = "tcp://127.0.0.1:9000"
-#cashier_url = "tls://127.0.0.1:9000"
+cashier_rpc_url = "tcp://127.0.0.1:9000"
+#cashier_rpc_url = "tls://127.0.0.1:9000"
 
 # Path to the client database
 database_path = "~/.config/darkfi/darkfid_client.db"

+ 2 - 2
example/config/drk.toml

@@ -4,5 +4,5 @@
 ## your client properly.
 
 # The RPC endpoint where darkfid is listening on
-darkfid_url = "tcp://127.0.0.1:8000"
-#darkfid_url = "tls://127.0.0.1:8000"
+darkfid_rpc_url = "tcp://127.0.0.1:8000"
+#darkfid_rpc_url = "tls://127.0.0.1:8000"

+ 24 - 2
example/config/gatewayd.toml

@@ -1,2 +1,24 @@
-accept_url = "127.0.0.1:3333"
-publisher_url = "127.0.0.1:4444"
+## gatewayd configuration file
+##
+## Please make sure you go through all the settings so you can configure
+## your daemon properly.
+
+# The endpoint where gatewayd will serve its protocol API
+protocol_listen_address = "127.0.0.1:3333"
+
+# The endpoint where gatewayd will serve its publisher API
+publisher_listen_address = "127.0.0.1:4444"
+
+# Whether to listen with TLS or plain TCP
+serve_tls = false
+
+# Path to DER-formatted PKCS#12 archive. (Unused if serve_tls=false)
+# This can be created using openssl:
+# openssl pkcs12 -export -out identity.pfx -inkey key.pem -in cert.pem -certfiles chain_certs.pem
+tls_identity_path = "~/.config/darkfi/gatewayd_identity.pfx"
+
+# Password for the created TLS identity. (Unused if serve_tls=false)
+tls_identity_password = "FOOBAR"
+
+# Path to database
+database_path = "~/.config/darkfi/gatewayd.db"

+ 18 - 20
src/bin/cashierd.rs

@@ -1,3 +1,14 @@
+use async_executor::Executor;
+use async_std::sync::{Arc, Mutex};
+use async_trait::async_trait;
+use clap::clap_app;
+use ff::Field;
+use log::{debug, warn};
+use rand::rngs::OsRng;
+use serde_json::{json, Value};
+use std::collections::HashMap;
+use std::path::PathBuf;
+
 use drk::{
     blockchain::Rocks,
     cli::{CashierdConfig, Config},
@@ -14,19 +25,6 @@ use drk::{
     Error, Result,
 };
 
-use clap::clap_app;
-use log::*;
-use serde_json::{json, Value};
-
-use async_executor::Executor;
-use ff::Field;
-use rand::rngs::OsRng;
-
-use async_std::sync::{Arc, Mutex};
-use async_trait::async_trait;
-use std::collections::HashMap;
-use std::path::PathBuf;
-
 fn handle_bridge_error(error_code: u32) -> Result<()> {
     match error_code {
         1 => Err(Error::BridgeError("Not Supported Client".into())),
@@ -51,7 +49,7 @@ impl RequestHandler for Cashierd {
             return JsonResult::Err(jsonerr(InvalidParams, None, req.id));
         }
 
-        debug!(target: "RPC", "--> {:#?}", serde_json::to_string(&req).unwrap());
+        debug!(target: "RPC", "--> {}", serde_json::to_string(&req).unwrap());
 
         match req.method.as_str() {
             Some("deposit") => return self.deposit(req.id, req.params).await,
@@ -84,12 +82,12 @@ impl Cashierd {
         let client = Client::new(
             rocks,
             (
-                config.gateway_url.parse()?,
-                config.gateway_subscriber_url.parse()?,
+                config.gateway_protocol_url.parse()?,
+                config.gateway_publisher_url.parse()?,
             ),
             (
-                expand_path(&config.mint_params.clone())?,
-                expand_path(&config.spend_params.clone())?,
+                expand_path(&config.mint_params_path.clone())?,
+                expand_path(&config.spend_params_path.clone())?,
             ),
             client_wallet.clone(),
         )?;
@@ -171,10 +169,10 @@ impl Cashierd {
         });
 
         let cfg = RpcServerConfig {
-            socket_addr: self.config.clone().listen_url,
+            socket_addr: self.config.rpc_listen_address.clone(),
             use_tls: self.config.serve_tls,
             identity_path: expand_path(&self.config.clone().tls_identity_path)?,
-            identity_pass: self.config.clone().tls_identity_password,
+            identity_pass: self.config.tls_identity_password.clone(),
         };
 
         listen_and_serve(cfg, self.clone()).await?;

+ 5 - 5
src/bin/darkfid.rs

@@ -148,7 +148,7 @@ impl Darkfid {
         // TODO: return a dictionary of features
         let req = jsonreq(json!("features"), json!([]));
         let rep: JsonResult;
-        match send_request(&self.config.cashier_url, json!(req)).await {
+        match send_request(&self.config.cashier_rpc_url, json!(req)).await {
             Ok(v) => rep = v,
             Err(e) => {
                 return JsonResult::Err(jsonerr(ServerError(-32004), Some(e.to_string()), id))
@@ -205,7 +205,7 @@ impl Darkfid {
         // If not, an error is returned, and forwarded to the method caller.
         let req = jsonreq(json!("deposit"), json!([network, token, pubkey]));
         let rep: JsonResult;
-        match send_request(&self.config.cashier_url, json!(req)).await {
+        match send_request(&self.config.cashier_rpc_url, json!(req)).await {
             Ok(v) => rep = v,
             Err(e) => {
                 return JsonResult::Err(jsonerr(ServerError(-32004), Some(e.to_string()), id))
@@ -301,10 +301,10 @@ async fn main() -> Result<()> {
     let darkfid = Darkfid::new(config_path)?;
 
     let server_config = RpcServerConfig {
-        socket_addr: darkfid.config.clone().listen_address,
+        socket_addr: darkfid.config.rpc_listen_address.clone(),
         use_tls: darkfid.config.serve_tls,
-        identity_path: expand_path(&darkfid.config.clone().tls_identity_path)?,
-        identity_pass: darkfid.config.clone().tls_identity_password,
+        identity_path: expand_path(&darkfid.config.tls_identity_path.clone())?,
+        identity_pass: darkfid.config.tls_identity_password.clone(),
     };
 
     listen_and_serve(server_config, darkfid).await

+ 5 - 5
src/bin/drk.rs

@@ -18,7 +18,7 @@ impl Drk {
 
     async fn request(&self, r: jsonrpc::JsonRequest) -> Result<Value> {
         let reply: JsonResult;
-        debug!(target: "DRK", "--> {}", serde_json::to_string(&r)?);
+        debug!(target: "RPC", "--> {}", serde_json::to_string(&r)?);
         match jsonrpc::send_request(&self.url, json!(r)).await {
             Ok(v) => reply = v,
             Err(e) => return Err(e),
@@ -26,17 +26,17 @@ impl Drk {
 
         match reply {
             JsonResult::Resp(r) => {
-                debug!(target: "DRK", "<-- {}", serde_json::to_string(&r)?);
+                debug!(target: "RPC", "<-- {}", serde_json::to_string(&r)?);
                 return Ok(r.result);
             }
 
             JsonResult::Err(e) => {
-                debug!(target: "DRK", "<-- {}", serde_json::to_string(&e)?);
+                debug!(target: "RPC", "<-- {}", serde_json::to_string(&e)?);
                 return Err(Error::JsonRpcError(e.error.message.to_string()));
             }
 
             JsonResult::Notif(n) => {
-                debug!(target: "DRK", "<-- {}", serde_json::to_string(&n)?);
+                debug!(target: "RPC", "<-- {}", serde_json::to_string(&n)?);
                 return Err(Error::JsonRpcError("Unexpected reply".to_string()));
             }
         }
@@ -115,7 +115,7 @@ impl Drk {
 }
 
 async fn start(config: &DrkConfig, options: ArgMatches<'_>) -> Result<()> {
-    let client = Drk::new(config.darkfid_url.clone());
+    let client = Drk::new(config.darkfid_rpc_url.clone());
 
     if options.is_present("hello") {
         let reply = client.say_hello().await?;

+ 7 - 8
src/bin/gatewayd.rs

@@ -1,7 +1,6 @@
 use async_executor::Executor;
 use clap::clap_app;
 use easy_parallel::Parallel;
-use std::net::SocketAddr;
 use std::path::PathBuf;
 use std::sync::Arc;
 
@@ -9,19 +8,19 @@ use drk::{
     blockchain::{rocks::columns, Rocks, RocksColumn},
     cli::{Config, GatewaydConfig},
     service::GatewayService,
-    util::join_config_path,
+    util::{expand_path, join_config_path},
     Result,
 };
 
 async fn start(executor: Arc<Executor<'_>>, config: Arc<&GatewaydConfig>) -> Result<()> {
-    let accept_addr: SocketAddr = config.accept_url.parse()?;
-    let pub_addr: SocketAddr = config.publisher_url.parse()?;
-    let database_path = join_config_path(&PathBuf::from("gatewayd.db"))?;
-
-    let rocks = Rocks::new(&database_path)?;
+    let rocks = Rocks::new(&expand_path(&config.database_path)?)?;
     let rocks_slabstore_column = RocksColumn::<columns::Slabs>::new(rocks);
 
-    let gateway = GatewayService::new(accept_addr, pub_addr, rocks_slabstore_column)?;
+    let gateway = GatewayService::new(
+        config.protocol_listen_address,
+        config.publisher_listen_address,
+        rocks_slabstore_column,
+    )?;
 
     Ok(gateway.start(executor.clone()).await?)
 }

+ 25 - 13
src/cli/cli_config.rs

@@ -3,6 +3,7 @@ use serde::{Deserialize, Serialize};
 use std::{
     fs,
     marker::PhantomData,
+    net::SocketAddr,
     path::{Path, PathBuf},
     str,
 };
@@ -32,15 +33,15 @@ impl<T: Serialize + DeserializeOwned> Config<T> {
 /// The configuration for drk
 #[derive(Clone, Serialize, Deserialize, Debug)]
 pub struct DrkConfig {
-    /// The URL where darkfid is listening on.
-    pub darkfid_url: String,
+    /// The URL where darkfid RPC is listening on
+    pub darkfid_rpc_url: String,
 }
 
 /// The configuration for darkfid
 #[derive(Clone, Serialize, Deserialize, Debug)]
 pub struct DarkfidConfig {
     /// The address where darkfid should bind its RPC socket
-    pub listen_address: String,
+    pub rpc_listen_address: SocketAddr,
     /// Whether to listen with TLS or plain TCP
     pub serve_tls: bool,
     /// Path to DER-formatted PKCS#12 archive. (Unused if serve_tls=false)
@@ -48,7 +49,7 @@ pub struct DarkfidConfig {
     /// Password for the TLS identity. (Unused if serve_tls=false)
     pub tls_identity_password: String,
     /// The RPC endpoint for a selected cashier
-    pub cashier_url: String,
+    pub cashier_rpc_url: String,
     /// Path to the client database
     pub database_path: String,
     /// Path to the wallet database
@@ -57,10 +58,21 @@ pub struct DarkfidConfig {
     pub wallet_password: String,
 }
 
+/// The configuration for gatewayd
 #[derive(Serialize, Deserialize, Debug)]
 pub struct GatewaydConfig {
-    pub accept_url: String,
-    pub publisher_url: String,
+    /// The address where gatewayd should bind its protocol socket
+    pub protocol_listen_address: SocketAddr,
+    /// The address where gatewayd should bind its publisher socket
+    pub publisher_listen_address: SocketAddr,
+    /// 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,
+    /// Password for the TLS identity. (Unused if serve_tls=false)
+    pub tls_identity_password: String,
+    /// Path to the database
+    pub database_path: String,
 }
 
 #[derive(Clone, Debug, Serialize, Deserialize)]
@@ -74,21 +86,21 @@ pub struct FeatureNetwork {
 #[derive(Clone, Serialize, Deserialize, Debug)]
 pub struct CashierdConfig {
     /// The endpoint where cashierd will bind its RPC socket
-    pub listen_url: String,
+    pub rpc_listen_address: SocketAddr,
     /// 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,
     /// Password for the TLS identity. (Unused if serve_tls=false)
     pub tls_identity_password: String,
-    /// ?
-    pub gateway_url: String,
-    /// ?
-    pub gateway_subscriber_url: String,
+    /// The endpoint to a gatewayd protocol API
+    pub gateway_protocol_url: String,
+    /// The endpoint to a gatewayd publisher API
+    pub gateway_publisher_url: String,
     /// Path to mint.params
-    pub mint_params: String,
+    pub mint_params_path: String,
     /// Path to spend.params
-    pub spend_params: String,
+    pub spend_params_path: String,
     /// Path to cashierd wallet
     pub cashier_wallet_path: String,
     /// Password for cashierd wallet

+ 0 - 21
src/cli/gatewayd_cli.rs

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

+ 0 - 3
src/cli/mod.rs

@@ -1,5 +1,2 @@
 pub mod cli_config;
-pub mod gatewayd_cli;
-
 pub use cli_config::{CashierdConfig, Config, DarkfidConfig, DrkConfig, GatewaydConfig};
-pub use gatewayd_cli::GatewaydCli;

+ 0 - 2
src/lib.rs

@@ -1,5 +1,3 @@
-#[macro_use]
-extern crate clap;
 use bellman::groth16;
 use bls12_381::{Bls12, Scalar};
 use std::collections::{HashMap, HashSet};

+ 3 - 6
src/rpc/rpcserver.rs

@@ -1,7 +1,6 @@
 use std::net::{SocketAddr, TcpListener, TcpStream};
-use std::str::FromStr;
-use std::sync::Arc;
 use std::path::PathBuf;
+use std::sync::Arc;
 
 use async_native_tls::{Identity, TlsAcceptor};
 use async_trait::async_trait;
@@ -15,7 +14,7 @@ use crate::rpc::jsonrpc::{JsonRequest, JsonResult};
 use crate::Result;
 
 pub struct RpcServerConfig {
-    pub socket_addr: String,
+    pub socket_addr: SocketAddr,
     pub use_tls: bool,
     pub identity_path: PathBuf,
     pub identity_pass: String,
@@ -144,8 +143,6 @@ pub async fn listen_and_serve(
 ) -> Result<()> {
     let tls: Option<TlsAcceptor>;
 
-    let sockaddr = SocketAddr::from_str(&cfg.socket_addr)?;
-
     if cfg.use_tls {
         let ident_bytes = std::fs::read(cfg.identity_path)?;
         let identity = Identity::from_pkcs12(&ident_bytes, &cfg.identity_pass)?;
@@ -155,6 +152,6 @@ pub async fn listen_and_serve(
     }
 
     let rh = Arc::new(rh);
-    let listener = listen(Async::<TcpListener>::bind(sockaddr)?, tls, rh);
+    let listener = listen(Async::<TcpListener>::bind(cfg.socket_addr)?, tls, rh);
     listener.await
 }

+ 3 - 1
src/util.rs

@@ -11,10 +11,12 @@ use crate::{
 pub fn expand_path(path: &str) -> Result<PathBuf> {
     let ret: PathBuf;
 
-    if path.starts_with("~") {
+    if path.starts_with("~/") {
         let homedir = dirs::home_dir().unwrap();
         let remains = PathBuf::from(path.strip_prefix("~/").unwrap());
         ret = [homedir, remains].iter().collect();
+    } else if path.starts_with('~') {
+        ret = dirs::home_dir().unwrap();
     } else {
         ret = PathBuf::from(path);
     }