main.rs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use std::sync::Arc;
  19. use log::info;
  20. use smol::{stream::StreamExt, Executor};
  21. use structopt_toml::{serde::Deserialize, structopt::StructOpt, StructOptToml};
  22. use url::Url;
  23. use darkfi::{async_daemonize, cli_desc, Result};
  24. use minerd::Minerd;
  25. const CONFIG_FILE: &str = "minerd.toml";
  26. const CONFIG_FILE_CONTENTS: &str = include_str!("../minerd.toml");
  27. #[derive(Clone, Debug, Deserialize, StructOpt, StructOptToml)]
  28. #[serde(default)]
  29. #[structopt(name = "minerd", about = cli_desc!())]
  30. struct Args {
  31. #[structopt(short, long)]
  32. /// Configuration file to use
  33. config: Option<String>,
  34. #[structopt(short, long, default_value = "tcp://127.0.0.1:28467")]
  35. /// JSON-RPC listen URL
  36. rpc_listen: Url,
  37. #[structopt(short, long, default_value = "4")]
  38. /// PoW miner number of threads to use
  39. threads: usize,
  40. #[structopt(short, long)]
  41. /// Set log file to ouput into
  42. log: Option<String>,
  43. #[structopt(short, parse(from_occurrences))]
  44. /// Increase verbosity (-vvv supported)
  45. verbose: u8,
  46. }
  47. async_daemonize!(realmain);
  48. async fn realmain(args: Args, ex: Arc<Executor<'static>>) -> Result<()> {
  49. info!(target: "minerd", "Starting DarkFi Mining Daemon...");
  50. let daemon = Minerd::init(args.threads);
  51. daemon.start(&ex, &args.rpc_listen);
  52. // Signal handling for graceful termination.
  53. let (signals_handler, signals_task) = SignalHandler::new(ex)?;
  54. signals_handler.wait_termination(signals_task).await?;
  55. info!(target: "minerd", "Caught termination signal, cleaning up and exiting");
  56. daemon.stop().await?;
  57. info!(target: "minerd", "Shut down successfully");
  58. Ok(())
  59. }