darkfid.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. use drk::blockchain::Rocks;
  2. use drk::cli::{Config, DarkfidCli, DarkfidConfig};
  3. use drk::util::join_config_path;
  4. use drk::wallet::WalletDb;
  5. use drk::Result;
  6. use drk::client::Client;
  7. use async_executor::Executor;
  8. use easy_parallel::Parallel;
  9. use async_std::sync::Arc;
  10. use std::net::SocketAddr;
  11. use std::path::PathBuf;
  12. async fn start(executor: Arc<Executor<'_>>, config: Arc<DarkfidConfig>) -> Result<()> {
  13. let connect_addr: SocketAddr = config.connect_url.parse()?;
  14. let sub_addr: SocketAddr = config.subscriber_url.parse()?;
  15. let cashier_addr: SocketAddr = config.cashier_url.parse()?;
  16. let database_path = config.database_path.clone();
  17. let walletdb_path = config.walletdb_path.clone();
  18. let rpc_url: std::net::SocketAddr = config.rpc_url.parse()?;
  19. let database_path = join_config_path(&PathBuf::from(database_path))?;
  20. let walletdb_path = join_config_path(&PathBuf::from(walletdb_path))?;
  21. let rocks = Rocks::new(&database_path)?;
  22. let wallet = WalletDb::new(&walletdb_path, config.password.clone())?;
  23. let mint_params_path = join_config_path(&PathBuf::from("mint.params"))?;
  24. let spend_params_path = join_config_path(&PathBuf::from("spend.params"))?;
  25. if let Err(_) = wallet.get_keypairs() {
  26. wallet.init_db()?;
  27. wallet.key_gen()?;
  28. }
  29. let mut client = Client::new(
  30. rocks,
  31. (connect_addr, sub_addr),
  32. (mint_params_path, spend_params_path),
  33. wallet.clone(),
  34. )?;
  35. client.start().await?;
  36. Client::connect_to_cashier(
  37. client,
  38. executor.clone(),
  39. cashier_addr.clone(),
  40. rpc_url.clone(),
  41. )
  42. .await?;
  43. Ok(())
  44. }
  45. fn main() -> Result<()> {
  46. let options = Arc::new(DarkfidCli::load()?);
  47. let config_path: PathBuf;
  48. match options.config.as_ref() {
  49. Some(path) => {
  50. config_path = path.to_owned();
  51. }
  52. None => {
  53. config_path = join_config_path(&PathBuf::from("darkfid.toml"))?;
  54. }
  55. }
  56. let config: DarkfidConfig = Config::<DarkfidConfig>::load(config_path)?;
  57. let config = Arc::new(config);
  58. let ex = Arc::new(Executor::new());
  59. let (signal, shutdown) = async_channel::unbounded::<()>();
  60. {
  61. use simplelog::*;
  62. let logger_config = ConfigBuilder::new().set_time_format_str("%T%.6f").build();
  63. let debug_level = if options.verbose {
  64. LevelFilter::Debug
  65. } else {
  66. LevelFilter::Off
  67. };
  68. let log_path = config.log_path.clone();
  69. CombinedLogger::init(vec![
  70. TermLogger::new(debug_level, logger_config, TerminalMode::Mixed).unwrap(),
  71. WriteLogger::new(
  72. LevelFilter::Debug,
  73. Config::default(),
  74. std::fs::File::create(log_path).unwrap(),
  75. ),
  76. ])
  77. .unwrap();
  78. }
  79. let ex2 = ex.clone();
  80. let (_, result) = Parallel::new()
  81. // Run four executor threads.
  82. .each(0..3, |_| smol::future::block_on(ex.run(shutdown.recv())))
  83. // Run the main future on the current thread.
  84. .finish(|| {
  85. smol::future::block_on(async move {
  86. start(ex2, config).await?;
  87. drop(signal);
  88. Ok::<(), drk::Error>(())
  89. })
  90. });
  91. result
  92. }