forks.rs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 darkfi::{
  19. blockchain::{BlockInfo, Blockchain, HeaderHash},
  20. validator::{consensus::Fork, pow::PoWModule},
  21. Result,
  22. };
  23. #[test]
  24. fn forks() -> Result<()> {
  25. smol::block_on(async {
  26. // Dummy records we will insert
  27. let record1 = HeaderHash::new(blake3::hash(b"Let there be dark!").into());
  28. let record2 = HeaderHash::new(blake3::hash(b"Never skip brain day.").into());
  29. // Create a temporary blockchain and a PoW module
  30. let blockchain = Blockchain::new(&sled::Config::new().temporary(true).open()?)?;
  31. let module = PoWModule::new(blockchain.clone(), 90, None)?;
  32. // Generate and insert default genesis block
  33. let genesis_block = BlockInfo::default();
  34. blockchain.add_block(&genesis_block)?;
  35. let genesis_block_hash = genesis_block.hash();
  36. // Create a fork
  37. let fork = Fork::new(blockchain.clone(), module).await?;
  38. // Add a dummy record to fork
  39. fork.overlay.lock().unwrap().blocks.insert_order(&[1], &[record1])?;
  40. // Verify blockchain doesn't contain the record
  41. assert_eq!(blockchain.blocks.get_order(&[0, 1], false)?, [Some(genesis_block_hash), None]);
  42. assert_eq!(
  43. fork.overlay.lock().unwrap().blocks.get_order(&[0, 1], true)?,
  44. [Some(genesis_block_hash), Some(record1)]
  45. );
  46. // Now we are going to clone the fork
  47. let fork_clone = fork.full_clone()?;
  48. // Verify it contains the original records
  49. assert_eq!(
  50. fork_clone.overlay.lock().unwrap().blocks.get_order(&[0, 1], true)?,
  51. [Some(genesis_block_hash), Some(record1)]
  52. );
  53. // Add another dummy record to cloned fork
  54. fork_clone.overlay.lock().unwrap().blocks.insert_order(&[2], &[record2])?;
  55. // Verify blockchain and original fork don't contain the second record
  56. assert_eq!(
  57. blockchain.blocks.get_order(&[0, 1, 2], false)?,
  58. [Some(genesis_block_hash), None, None]
  59. );
  60. assert_eq!(
  61. fork.overlay.lock().unwrap().blocks.get_order(&[0, 1, 2], false)?,
  62. [Some(genesis_block_hash), Some(record1), None]
  63. );
  64. assert_eq!(
  65. fork_clone.overlay.lock().unwrap().blocks.get_order(&[0, 1, 2], true)?,
  66. [Some(genesis_block_hash), Some(record1), Some(record2)]
  67. );
  68. Ok(())
  69. })
  70. }