main.rs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 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 async_std::{stream::StreamExt, sync::Arc};
  19. use log::info;
  20. use structopt_toml::{serde::Deserialize, structopt::StructOpt, StructOptToml};
  21. use darkfi::{
  22. async_daemonize,
  23. blockchain::BlockInfo,
  24. cli_desc,
  25. util::time::TimeKeeper,
  26. validator::{Validator, ValidatorConfig, ValidatorPtr},
  27. Result,
  28. };
  29. use darkfi_contract_test_harness::vks;
  30. #[cfg(test)]
  31. mod tests;
  32. const CONFIG_FILE: &str = "darkfid_config.toml";
  33. const CONFIG_FILE_CONTENTS: &str = include_str!("../darkfid_config.toml");
  34. #[derive(Clone, Debug, Deserialize, StructOpt, StructOptToml)]
  35. #[serde(default)]
  36. #[structopt(name = "darkfid", about = cli_desc!())]
  37. struct Args {
  38. #[structopt(short, long)]
  39. /// Configuration file to use
  40. config: Option<String>,
  41. #[structopt(long)]
  42. /// Enable testing mode for local testing
  43. testing_mode: bool,
  44. #[structopt(short, long)]
  45. /// Set log file to ouput into
  46. log: Option<String>,
  47. #[structopt(short, parse(from_occurrences))]
  48. /// Increase verbosity (-vvv supported)
  49. verbose: u8,
  50. }
  51. pub struct Darkfid {
  52. _validator: ValidatorPtr,
  53. }
  54. impl Darkfid {
  55. pub async fn new(_validator: ValidatorPtr) -> Self {
  56. Self { _validator }
  57. }
  58. }
  59. async_daemonize!(realmain);
  60. async fn realmain(args: Args, _ex: Arc<smol::Executor<'_>>) -> Result<()> {
  61. info!("Initializing DarkFi node...");
  62. // NOTE: everything is dummy for now
  63. // Initialize or open sled database
  64. let sled_db = sled::Config::new().temporary(true).open()?;
  65. vks::inject(&sled_db)?;
  66. // Initialize validator configuration
  67. let genesis_block = BlockInfo::default();
  68. let time_keeper = TimeKeeper::new(genesis_block.header.timestamp, 10, 90, 0);
  69. let config = ValidatorConfig::new(time_keeper, genesis_block, vec![], args.testing_mode);
  70. if args.testing_mode {
  71. info!("Node is configured to run in testing mode!");
  72. }
  73. // Initialize validator
  74. let validator = Validator::new(&sled_db, config).await?;
  75. // Initialize node
  76. let _darkfid = Darkfid::new(validator).await;
  77. info!("Node initialized successfully!");
  78. // Signal handling for graceful termination.
  79. let (signals_handler, signals_task) = SignalHandler::new()?;
  80. signals_handler.wait_termination(signals_task).await?;
  81. info!("Caught termination signal, cleaning up and exiting...");
  82. Ok(())
  83. }