crypsinous.rs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. ouroboros::{EpochConsensus, Stakeholder},
  27. proto::{ProtocolSync, ProtocolTx},
  28. ValidatorState, TESTNET_GENESIS_HASH_BYTES, TESTNET_GENESIS_TIMESTAMP,
  29. },
  30. net,
  31. net::Settings,
  32. node::Client,
  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. // TODO: sqldb init cleanup
  110. // Initialize client
  111. let client = Arc::new(Client::new(wallet.clone()).await?);
  112. // Parse cashier addresses
  113. let cashier_pubkeys = vec![wallet.get_default_keypair().await?.public];
  114. // Parse faucet addresses
  115. let faucet_pubkeys = vec![wallet.get_default_keypair().await?.public];
  116. // Initialize validator state
  117. let state = ValidatorState::new(
  118. &sled_db,
  119. genesis_ts,
  120. genesis_data,
  121. client,
  122. cashier_pubkeys,
  123. faucet_pubkeys,
  124. )
  125. .await?;
  126. let registry = p2p.protocol_registry();
  127. info!("Registering block sync P2P protocols...");
  128. let _state = state.clone();
  129. registry
  130. .register(net::SESSION_ALL, move |channel, p2p| {
  131. let state = _state.clone();
  132. async move { ProtocolSync::init(channel, state, p2p, false).await.unwrap() }
  133. })
  134. .await;
  135. let _state = state.clone();
  136. registry
  137. .register(net::SESSION_ALL, move |channel, p2p| {
  138. let state = _state.clone();
  139. async move { ProtocolTx::init(channel, state, p2p).await.unwrap() }
  140. })
  141. .await;
  142. //////////////////////////////
  143. let ex2 = ex.clone();
  144. p2p.clone().start(ex.clone()).await?;
  145. ex2.spawn(p2p.clone().run(ex.clone())).detach();
  146. let slots = 3;
  147. let epochs = 3;
  148. let ticks = 3;
  149. let reward = 1;
  150. let epoch_consensus = EpochConsensus::new(Some(slots), Some(epochs), Some(ticks), Some(reward));
  151. //proof's number of rows
  152. let k: u32 = 11;
  153. let path = args.path.clone();
  154. let id = Timestamp::current_time().0;
  155. let mut stakeholder =
  156. Stakeholder::new(epoch_consensus, p2p.clone(), settings.to_owned(), &path, id, Some(k))
  157. .await?;
  158. stakeholder.background(Some(100)).await;
  159. p2p.stop().await;
  160. Ok(())
  161. }