forks.rs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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::Blockchain,
  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 record0 = blake3::hash(b"Let there be dark!");
  28. let record1 = blake3::hash(b"Never skip brain day.");
  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. // Create a fork
  33. let fork = Fork::new(&blockchain, module).await?;
  34. // Add a dummy record to fork
  35. fork.overlay.lock().unwrap().order.insert(&[0], &[record0])?;
  36. // Verify blockchain doesn't contain the record
  37. assert_eq!(blockchain.order.get(&[0], false)?, [None]);
  38. assert_eq!(fork.overlay.lock().unwrap().order.get(&[0], true)?, [Some(record0)]);
  39. // Now we are going to clone the fork
  40. let fork_clone = fork.full_clone()?;
  41. // Verify it cointains the original record
  42. assert_eq!(fork_clone.overlay.lock().unwrap().order.get(&[0], true)?, [Some(record0)]);
  43. // Add another dummy record to cloned fork
  44. fork_clone.overlay.lock().unwrap().order.insert(&[1], &[record1])?;
  45. // Verify blockchain and original fork don't contain the second record
  46. assert_eq!(blockchain.order.get(&[0, 1], false)?, [None, None]);
  47. assert_eq!(fork.overlay.lock().unwrap().order.get(&[0, 1], false)?, [Some(record0), None]);
  48. assert_eq!(
  49. fork_clone.overlay.lock().unwrap().order.get(&[0, 1], true)?,
  50. [Some(record0), Some(record1)]
  51. );
  52. Ok(())
  53. })
  54. }