main.rs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. use std::{path::PathBuf, sync::Arc};
  2. use async_executor::Executor;
  3. use clap::{IntoApp, Parser};
  4. use easy_parallel::Parallel;
  5. use log::debug;
  6. use simplelog::{ColorChoice, LevelFilter, TermLogger, TerminalMode};
  7. use darkfi::{
  8. blockchain::{rocks::columns, Rocks, RocksColumn},
  9. cli::{cli_config::spawn_config, CliGatewayd, Config, GatewaydConfig},
  10. node::service::gateway::GatewayService,
  11. util::{expand_path, join_config_path},
  12. Result,
  13. };
  14. const CONFIG_FILE_CONTENTS: &[u8] = include_bytes!("../gatewayd_config.toml");
  15. async fn start(executor: Arc<Executor<'_>>, config: &GatewaydConfig) -> Result<()> {
  16. let rocks = Rocks::new(&expand_path(&config.database_path)?)?;
  17. let rocks_slabstore_column = RocksColumn::<columns::Slabs>::new(rocks);
  18. let gateway = GatewayService::new(
  19. config.protocol_listen_address,
  20. config.publisher_listen_address,
  21. rocks_slabstore_column,
  22. )?;
  23. Ok(gateway.start(executor.clone()).await?)
  24. }
  25. #[async_std::main]
  26. async fn main() -> Result<()> {
  27. let args = CliGatewayd::parse();
  28. let matches = CliGatewayd::into_app().get_matches();
  29. let config_path = if args.config.is_some() {
  30. expand_path(&args.config.unwrap())?
  31. } else {
  32. join_config_path(&PathBuf::from("gatewayd.toml"))?
  33. };
  34. // Spawn config file if it's not in place already.
  35. spawn_config(&config_path, CONFIG_FILE_CONTENTS)?;
  36. let mut verbosity_level = 0;
  37. verbosity_level += matches.occurrences_of("verbose");
  38. let loglevel = match verbosity_level {
  39. 0 => LevelFilter::Info,
  40. 1 => LevelFilter::Debug,
  41. _ => LevelFilter::Trace,
  42. };
  43. TermLogger::init(
  44. loglevel,
  45. simplelog::Config::default(),
  46. TerminalMode::Mixed,
  47. ColorChoice::Auto,
  48. )?;
  49. let config: GatewaydConfig = Config::<GatewaydConfig>::load(config_path)?;
  50. let ex = Arc::new(Executor::new());
  51. let (signal, shutdown) = async_channel::unbounded::<()>();
  52. let ex2 = ex.clone();
  53. let nthreads = num_cpus::get();
  54. debug!(target: "GATEWAY DAEMON", "Run {} executor threads", nthreads);
  55. let (_, result) = Parallel::new()
  56. .each(0..nthreads, |_| smol::future::block_on(ex.run(shutdown.recv())))
  57. // Run the main future on the current thread.
  58. .finish(|| {
  59. smol::future::block_on(async move {
  60. start(ex2, &config).await?;
  61. drop(signal);
  62. Ok::<(), darkfi::Error>(())
  63. })
  64. });
  65. result
  66. }