main.rs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2025 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 smol::{stream::StreamExt, Executor};
  20. use structopt_toml::{serde::Deserialize, structopt::StructOpt, StructOptToml};
  21. use tracing::info;
  22. use darkfi::{async_daemonize, cli_desc, rpc::settings::RpcSettingsOpt, Error, Result};
  23. use minerd::Minerd;
  24. const CONFIG_FILE: &str = "minerd.toml";
  25. const CONFIG_FILE_CONTENTS: &str = include_str!("../minerd.toml");
  26. #[derive(Clone, Debug, Deserialize, StructOpt, StructOptToml)]
  27. #[serde(default)]
  28. #[structopt(name = "minerd", about = cli_desc!())]
  29. struct Args {
  30. #[structopt(short, long)]
  31. /// Configuration file to use
  32. config: Option<String>,
  33. #[structopt(flatten)]
  34. /// JSON-RPC settings
  35. rpc: RpcSettingsOpt,
  36. #[structopt(short, long, default_value = "4")]
  37. /// PoW miner number of threads to use
  38. threads: usize,
  39. #[structopt(long, default_value = "0")]
  40. /// Refuse mining at given height (0 mines forever)
  41. stop_at_height: u32,
  42. #[structopt(short, long)]
  43. /// Set log file to ouput into
  44. log: Option<String>,
  45. #[structopt(short, parse(from_occurrences))]
  46. /// Increase verbosity (-vvv supported)
  47. verbose: u8,
  48. }
  49. async_daemonize!(realmain);
  50. async fn realmain(args: Args, ex: Arc<Executor<'static>>) -> Result<()> {
  51. info!(target: "minerd", "Starting DarkFi Mining Daemon...");
  52. let daemon = Minerd::init(args.threads, args.stop_at_height);
  53. daemon.start(&ex, &args.rpc.into());
  54. // Signal handling for graceful termination.
  55. let (signals_handler, signals_task) = SignalHandler::new(ex)?;
  56. signals_handler.wait_termination(signals_task).await?;
  57. info!(target: "minerd", "Caught termination signal, cleaning up and exiting");
  58. daemon.stop().await?;
  59. info!(target: "minerd", "Shut down successfully");
  60. Ok(())
  61. }