main.rs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. use std::net::SocketAddr;
  2. use async_executor::Executor;
  3. use async_std::sync::Arc;
  4. use clap::{IntoApp, Parser};
  5. use serde::{Deserialize, Serialize};
  6. use simplelog::{ColorChoice, TermLogger, TerminalMode};
  7. use darkfi::{
  8. rpc::rpcserver::{listen_and_serve, RpcServerConfig},
  9. util::{
  10. cli::{log_config, spawn_config, Config},
  11. expand_path,
  12. path::get_config_path,
  13. },
  14. Result,
  15. };
  16. use consensusd::service::ConsensusService;
  17. /// This struct represent the configuration parameters used by the Consensus daemon.
  18. #[derive(Debug, Clone, Deserialize, Serialize)]
  19. pub struct ConsensusdConfig {
  20. /// The endpoint where chaind will bind its RPC socket
  21. pub rpc_listen_address: SocketAddr,
  22. /// Whether to listen with TLS or plain TCP
  23. pub serve_tls: bool,
  24. /// Path to DER-formatted PKCS#12 archive. (Unused if serve_tls=false)
  25. pub tls_identity_path: String,
  26. /// Password for the TLS identity. (Unused if serve_tls=false)
  27. pub tls_identity_password: String,
  28. /// Path to the state file
  29. pub state_path: String,
  30. /// Node ID, used only for testing
  31. pub id: u64,
  32. }
  33. /// Chaind cli configuration.
  34. #[derive(Parser)]
  35. #[clap(name = "consensusd")]
  36. pub struct CliConsensusd {
  37. /// Sets a custom config file
  38. #[clap(short, long)]
  39. pub config: Option<String>,
  40. /// Increase verbosity
  41. #[clap(short, parse(from_occurrences))]
  42. pub verbose: u8,
  43. }
  44. /// Consensus service initialization.
  45. async fn start(executor: Arc<Executor<'_>>, config: ConsensusdConfig) -> Result<()> {
  46. let server_config = RpcServerConfig {
  47. socket_addr: config.rpc_listen_address,
  48. use_tls: config.serve_tls,
  49. identity_path: expand_path(&config.clone().tls_identity_path)?,
  50. identity_pass: config.tls_identity_password.clone(),
  51. };
  52. let state_path = expand_path(&config.state_path)?;
  53. let id = config.id;
  54. let chain_service = ConsensusService::new(id, state_path)?;
  55. listen_and_serve(server_config, chain_service, executor).await
  56. }
  57. const CONFIG_FILE_CONTENTS: &[u8] = include_bytes!("../consensusd_config.toml");
  58. /// Consensus daemon initialization.
  59. #[async_std::main]
  60. async fn main() -> Result<()> {
  61. let args = CliConsensusd::parse();
  62. let matches = CliConsensusd::command().get_matches();
  63. let verbosity_level = matches.occurrences_of("verbose");
  64. let (lvl, conf) = log_config(verbosity_level)?;
  65. TermLogger::init(lvl, conf, TerminalMode::Mixed, ColorChoice::Auto)?;
  66. let config_path = get_config_path(args.config, "consensusd_config.toml")?;
  67. spawn_config(&config_path, CONFIG_FILE_CONTENTS)?;
  68. let config: ConsensusdConfig = Config::<ConsensusdConfig>::load(config_path)?;
  69. let ex = Arc::new(Executor::new());
  70. smol::block_on(ex.run(start(ex.clone(), config)))
  71. }