sync_forks.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 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, 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 = 90;
  30. let pow_fixed_difficulty = Some(BigUint::one());
  31. let config = HarnessConfig {
  32. pow_target,
  33. pow_fixed_difficulty: pow_fixed_difficulty.clone(),
  34. finalization_threshold: 6,
  35. alice_initial: 1000,
  36. bob_initial: 500,
  37. };
  38. let th = Harness::new(config, true, &ex).await?;
  39. // Retrieve genesis block and generate 3 forks
  40. let previous = th.alice.validator.blockchain.last_block()?;
  41. // Generate a fork with 3 blocks
  42. let block1 = th.generate_next_block(&previous).await?;
  43. let block2 = th.generate_next_block(&block1).await?;
  44. let block3 = th.generate_next_block(&block2).await?;
  45. th.add_blocks(&vec![block1, block2, block3]).await?;
  46. // Generate a fork with 1 block
  47. let block4 = th.generate_next_block(&previous).await?;
  48. th.add_blocks(&vec![block4.clone()]).await?;
  49. // Generate a fork with 1 block
  50. let block5 = th.generate_next_block(&previous).await?;
  51. th.add_blocks(&vec![block5.clone()]).await?;
  52. // Check nodes have all the forks
  53. th.validate_fork_chains(3, vec![3, 1, 1]).await;
  54. // We are going to create a third node and try to sync from Bob
  55. let mut sync_settings =
  56. Settings { localnet: true, inbound_connections: 3, ..Default::default() };
  57. let charlie_url = Url::parse("tcp+tls://127.0.0.1:18342")?;
  58. sync_settings.inbound_addrs = vec![charlie_url];
  59. let bob_url = th.bob.sync_p2p.settings().inbound_addrs[0].clone();
  60. sync_settings.peers = vec![bob_url];
  61. let charlie =
  62. generate_node(&th.vks, &th.validator_config, &sync_settings, None, &ex, false).await?;
  63. // Verify node synced the big(best) fork
  64. let charlie_forks = charlie.validator.consensus.forks.read().await;
  65. assert_eq!(charlie_forks.len(), 1);
  66. assert_eq!(charlie_forks[0].proposals.len(), 3);
  67. drop(charlie_forks);
  68. // Extend the small fork sequences and add it to nodes
  69. let block6 = th.generate_next_block(&block4).await?;
  70. th.add_blocks(&vec![block6]).await?;
  71. let block7 = th.generate_next_block(&block5).await?;
  72. th.add_blocks(&vec![block7]).await?;
  73. // Check charlie has all the forks
  74. let charlie_forks = charlie.validator.consensus.forks.read().await;
  75. assert_eq!(charlie_forks.len(), 3);
  76. assert_eq!(charlie_forks[0].proposals.len(), 3);
  77. assert_eq!(charlie_forks[1].proposals.len(), 2);
  78. assert_eq!(charlie_forks[2].proposals.len(), 2);
  79. drop(charlie_forks);
  80. // Thanks for reading
  81. Ok(())
  82. }
  83. #[test]
  84. #[ignore]
  85. fn sync_forks() -> Result<()> {
  86. let ex = Arc::new(Executor::new());
  87. let (signal, shutdown) = smol::channel::unbounded::<()>();
  88. easy_parallel::Parallel::new().each(0..4, |_| smol::block_on(ex.run(shutdown.recv()))).finish(
  89. || {
  90. smol::block_on(async {
  91. sync_forks_real(ex.clone()).await.unwrap();
  92. drop(signal);
  93. })
  94. },
  95. );
  96. Ok(())
  97. }