main.rs 3.6 KB

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