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