crypsinous.rs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2022 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 clap::Parser;
  20. use easy_parallel::Parallel;
  21. use log::info;
  22. use smol::Executor;
  23. use url::Url;
  24. use darkfi::{
  25. consensus::{
  26. constants::{TESTNET_GENESIS_HASH_BYTES, TESTNET_GENESIS_TIMESTAMP},
  27. ouroboros::{EpochConsensus, Stakeholder},
  28. proto::{ProtocolSync, ProtocolTx},
  29. ValidatorState,
  30. },
  31. net,
  32. net::Settings,
  33. util::{path::expand_path, time::Timestamp},
  34. wallet::walletdb::init_wallet,
  35. Result,
  36. };
  37. #[derive(Parser)]
  38. struct NetCli {
  39. #[clap(long, value_parser)]
  40. addr: Vec<String>,
  41. #[clap(long, value_parser, default_value = "/tmp/db")]
  42. path: String,
  43. #[clap(long, value_parser)]
  44. peers: Vec<String>,
  45. #[clap(long, value_parser)]
  46. seeds: Vec<String>,
  47. #[clap(long, value_parser, default_value = "0")]
  48. slots: u32,
  49. #[clap(long, value_parser)]
  50. wallet_path: String,
  51. #[clap(long, value_parser)]
  52. wallet_pass: String,
  53. }
  54. #[async_std::main]
  55. async fn main() -> Result<()> {
  56. env_logger::init();
  57. let args = NetCli::parse();
  58. let (signal, shutdown) = smol::channel::unbounded::<()>();
  59. let ex = Arc::new(Executor::new());
  60. let ex2 = ex.clone();
  61. let ex3 = ex2.clone();
  62. let (_, result) = Parallel::new()
  63. .each(0..4, |_| smol::future::block_on(ex2.run(shutdown.recv())))
  64. .finish(|| {
  65. smol::future::block_on(async move {
  66. start(args, ex3).await?;
  67. drop(signal);
  68. Ok(())
  69. })
  70. });
  71. result
  72. }
  73. async fn start(args: NetCli, ex: Arc<Executor<'_>>) -> Result<()> {
  74. let mut addr = vec![];
  75. for i in 0..args.addr.len() {
  76. addr.push(Url::parse(args.addr[i].as_str()).unwrap());
  77. }
  78. let mut peers = vec![];
  79. for i in 0..args.peers.len() {
  80. peers.push(Url::parse(args.peers[i].as_str()).unwrap());
  81. }
  82. let mut seeds = vec![];
  83. for i in 0..args.seeds.len() {
  84. seeds.push(Url::parse(args.seeds[i].as_str()).unwrap());
  85. }
  86. // initialize n stakeholders
  87. let settings = Settings {
  88. inbound: addr.clone(),
  89. outbound_connections: args.slots,
  90. manual_attempt_limit: 0,
  91. seed_query_timeout_seconds: 8,
  92. connect_timeout_seconds: 10,
  93. channel_handshake_seconds: 4,
  94. channel_heartbeat_seconds: 10,
  95. external_addr: addr,
  96. peers,
  97. seeds,
  98. ..Default::default()
  99. };
  100. let p2p = net::P2p::new(settings.clone()).await;
  101. //////////////////////////////
  102. // Initialize or load wallet
  103. let wallet = init_wallet(&args.wallet_path, &args.wallet_pass).await?;
  104. // Initialize or open sled database
  105. let db_path = format!("{}/{}", expand_path(&args.path)?.to_str().unwrap(), "testnet");
  106. let sled_db = sled::open(&db_path)?;
  107. // Initialize validator state
  108. let (genesis_ts, genesis_data) = (*TESTNET_GENESIS_TIMESTAMP, *TESTNET_GENESIS_HASH_BYTES);
  109. // Parse faucet addresses (not needed here probably)
  110. let faucet_pubkeys = vec![];
  111. // Initialize validator state
  112. let state =
  113. ValidatorState::new(&sled_db, genesis_ts, genesis_data, wallet.clone(), faucet_pubkeys)
  114. .await?;
  115. let registry = p2p.protocol_registry();
  116. info!("Registering block sync P2P protocols...");
  117. let _state = state.clone();
  118. registry
  119. .register(net::SESSION_ALL, move |channel, p2p| {
  120. let state = _state.clone();
  121. async move { ProtocolSync::init(channel, state, p2p, false).await.unwrap() }
  122. })
  123. .await;
  124. let _state = state.clone();
  125. registry
  126. .register(net::SESSION_ALL, move |channel, p2p| {
  127. let state = _state.clone();
  128. async move { ProtocolTx::init(channel, state, p2p).await.unwrap() }
  129. })
  130. .await;
  131. //////////////////////////////
  132. let ex2 = ex.clone();
  133. p2p.clone().start(ex.clone()).await?;
  134. ex2.spawn(p2p.clone().run(ex.clone())).detach();
  135. let slots = 3;
  136. let epochs = 3;
  137. let ticks = 3;
  138. let reward = 1;
  139. let epoch_consensus = EpochConsensus::new(Some(slots), Some(epochs), Some(ticks), Some(reward));
  140. //proof's number of rows
  141. let k: u32 = 11;
  142. let path = args.path.clone();
  143. let id = Timestamp::current_time().0;
  144. let mut stakeholder =
  145. Stakeholder::new(epoch_consensus, p2p.clone(), settings.to_owned(), &path, id, Some(k))
  146. .await?;
  147. stakeholder.background(Some(100)).await;
  148. p2p.stop().await;
  149. Ok(())
  150. }