sync_forks.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2026 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 darkfi::{net::Settings, validator::utils::best_fork_index, Result};
  20. use darkfi_contract_test_harness::init_logger;
  21. use darkfi_sdk::num_traits::One;
  22. use num_bigint::BigUint;
  23. use smol::Executor;
  24. use url::Url;
  25. use crate::tests::{generate_node, Harness, HarnessConfig};
  26. async fn sync_forks_real(ex: Arc<Executor<'static>>) -> Result<()> {
  27. init_logger();
  28. // Initialize harness in testing mode
  29. let pow_target = 120;
  30. let pow_fixed_difficulty = Some(BigUint::one());
  31. let config = HarnessConfig {
  32. pow_target,
  33. pow_fixed_difficulty: pow_fixed_difficulty.clone(),
  34. confirmation_threshold: 6,
  35. alice_url: "tcp+tls://127.0.0.1:18440".to_string(),
  36. bob_url: "tcp+tls://127.0.0.1:18441".to_string(),
  37. };
  38. let th = Harness::new(config, true, &ex).await?;
  39. // Generate 3 forks
  40. let mut fork0 = th.alice.validator.consensus.forks.read().await[0].full_clone()?;
  41. let mut fork1 = fork0.full_clone()?;
  42. let mut fork2 = fork1.full_clone()?;
  43. // Extend first fork with 3 blocks
  44. th.add_blocks(&[
  45. th.generate_next_block(&mut fork0).await?,
  46. th.generate_next_block(&mut fork0).await?,
  47. th.generate_next_block(&mut fork0).await?,
  48. ])
  49. .await?;
  50. // Extend second fork with 1 block
  51. th.add_blocks(&[th.generate_next_block(&mut fork1).await?]).await?;
  52. // Extend third fork with 1 block
  53. th.add_blocks(&[th.generate_next_block(&mut fork2).await?]).await?;
  54. // Check nodes have all the forks
  55. th.validate_fork_chains(3, vec![3, 1, 1]).await;
  56. // We are going to create a third node and try to sync from Bob
  57. let mut settings = Settings { localnet: true, inbound_connections: 3, ..Default::default() };
  58. let charlie_url = Url::parse("tcp+tls://127.0.0.1:18442")?;
  59. settings.inbound_addrs = vec![charlie_url];
  60. let bob_url = th.bob.p2p_handler.p2p.settings().read().await.inbound_addrs[0].clone();
  61. settings.peers = vec![bob_url];
  62. let charlie = generate_node(&th.vks, &th.validator_config, &settings, &ex, false, None).await?;
  63. // Verify node synced the best fork
  64. let forks = th.alice.validator.consensus.forks.read().await;
  65. let best_fork = &forks[best_fork_index(&forks)?];
  66. let charlie_forks = charlie.validator.consensus.forks.read().await;
  67. assert_eq!(charlie_forks.len(), 1);
  68. assert_eq!(charlie_forks[0].proposals.len(), best_fork.proposals.len());
  69. let small_best = best_fork.proposals.len() == 1;
  70. drop(forks);
  71. drop(charlie_forks);
  72. // Extend the small fork sequences and add it to nodes
  73. th.add_blocks(&[th.generate_next_block(&mut fork1).await?]).await?;
  74. th.add_blocks(&[th.generate_next_block(&mut fork2).await?]).await?;
  75. // Check charlie has the correct forks
  76. let charlie_forks = charlie.validator.consensus.forks.read().await;
  77. if small_best {
  78. // If Charlie already had a small fork as its best,
  79. // it will have two forks with 2 blocks each.
  80. assert_eq!(charlie_forks.len(), 2);
  81. assert_eq!(charlie_forks[0].proposals.len(), 2);
  82. assert_eq!(charlie_forks[1].proposals.len(), 2);
  83. } else {
  84. // Charlie didn't originaly have the forks, but they
  85. // should be synced when their proposals were received
  86. assert_eq!(charlie_forks.len(), 3);
  87. assert_eq!(charlie_forks[0].proposals.len(), 3);
  88. assert_eq!(charlie_forks[1].proposals.len(), 2);
  89. assert_eq!(charlie_forks[2].proposals.len(), 2);
  90. }
  91. drop(charlie_forks);
  92. // Thanks for reading
  93. Ok(())
  94. }
  95. #[test]
  96. fn sync_forks() -> Result<()> {
  97. let ex = Arc::new(Executor::new());
  98. let (signal, shutdown) = smol::channel::unbounded::<()>();
  99. easy_parallel::Parallel::new().each(0..4, |_| smol::block_on(ex.run(shutdown.recv()))).finish(
  100. || {
  101. smol::block_on(async {
  102. sync_forks_real(ex.clone()).await.unwrap();
  103. drop(signal);
  104. })
  105. },
  106. );
  107. Ok(())
  108. }