darkfid.rs 3.4 KB

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