forks.rs 3.1 KB

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