diff --git a/consensus/cryptarchia-engine/src/lib.rs b/consensus/cryptarchia-engine/src/lib.rs index 0ab515f4..4bc05f61 100644 --- a/consensus/cryptarchia-engine/src/lib.rs +++ b/consensus/cryptarchia-engine/src/lib.rs @@ -22,6 +22,7 @@ pub struct Branches { } #[derive(Clone, Debug)] +#[cfg_attr(test, derive(PartialEq))] pub struct Branch { id: Id, parent: Id, @@ -131,6 +132,7 @@ where } #[derive(Debug, Clone, Error)] +#[cfg_attr(test, derive(PartialEq))] pub enum Error { #[error("Parent block: {0:?} is not know to this node")] ParentMissing(Id), @@ -220,7 +222,7 @@ where #[cfg(test)] pub mod tests { - use super::Cryptarchia; + use super::{Cryptarchia, Error, Slot}; use crate::Config; use std::hash::{DefaultHasher, Hash, Hasher}; @@ -320,4 +322,58 @@ pub mod tests { res[..8].copy_from_slice(&hash.to_be_bytes()); res } + + #[test] + fn test_getters() { + let engine = Cryptarchia::from_genesis([0; 32], config()); + let parent = engine.genesis(); + + // Get branch directly from HashMap + let branch1 = engine + .branches + .get(&parent) + .ok_or("At least one branch should be there"); + + let branches = engine.branches(); + + // Get branch using getter + let branch2 = branches + .get(&parent) + .ok_or("At least one branch should be there"); + + assert_eq!(branch1, branch2); + assert_eq!( + branch1.expect("id is not set").id(), + branch2.expect("id is not set").id() + ); + assert_eq!( + branch1.expect("parent is not set").parent(), + branch2.expect("parent is not set").parent() + ); + assert_eq!( + branch1.expect("slot is not set").slot(), + branch2.expect("slot is not set").slot() + ); + assert_eq!( + branch1.expect("length is not set").length(), + branch2.expect("length is not set").length() + ); + + let slot = Slot::genesis(); + + assert_eq!(slot + 10u64, Slot::from(10)); + + let strange_engine = Cryptarchia::from_genesis([100; 32], config()); + let not_a_parent = strange_engine.genesis(); + + _ = match branches + .get(¬_a_parent) + .ok_or(Error::ParentMissing(not_a_parent)) + { + Ok(_) => panic!("Parent should not be related to this branch"), + Err(e) => { + assert_ne!(e, Error::ParentMissing(parent)); + } + }; + } }