iuna

iuna - experimental devnet protocol
git clone https://iuna.jhx.app/git/iuna.git
Log | Files | Refs | README | LICENSE

fork.rs (3048B)


      1 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
      2 pub(super) struct ForkPoint {
      3     pub(super) common_ancestor_height: u64,
      4 }
      5 
      6 impl ForkPoint {
      7     pub(super) fn first_diverging_height(self) -> u64 {
      8         self.common_ancestor_height + 1
      9     }
     10 }
     11 
     12 #[derive(Clone, Debug, Eq, PartialEq)]
     13 pub(super) struct LeaderScore {
     14     pub(super) finalizer_mode_rank: u8,
     15     pub(super) finalizer_rank: u32,
     16     pub(super) proof_rank: String,
     17 }
     18 
     19 impl Ord for LeaderScore {
     20     fn cmp(&self, other: &Self) -> std::cmp::Ordering {
     21         self.finalizer_mode_rank
     22             .cmp(&other.finalizer_mode_rank)
     23             .then_with(|| self.finalizer_rank.cmp(&other.finalizer_rank))
     24             .then_with(|| self.proof_rank.cmp(&other.proof_rank))
     25     }
     26 }
     27 
     28 impl PartialOrd for LeaderScore {
     29     fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
     30         Some(self.cmp(other))
     31     }
     32 }
     33 
     34 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
     35 pub(super) enum ForkQuality {
     36     LocalBetter,
     37     RemoteBetter,
     38     Equal,
     39 }
     40 
     41 impl From<std::cmp::Ordering> for ForkQuality {
     42     fn from(ordering: std::cmp::Ordering) -> Self {
     43         match ordering {
     44             std::cmp::Ordering::Less => Self::LocalBetter,
     45             std::cmp::Ordering::Equal => Self::Equal,
     46             std::cmp::Ordering::Greater => Self::RemoteBetter,
     47         }
     48     }
     49 }
     50 
     51 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
     52 pub(super) enum ForkChoice {
     53     KeepLocal,
     54     SwitchToCandidate,
     55 }
     56 
     57 #[cfg(test)]
     58 mod tests {
     59     use super::{ForkPoint, ForkQuality, LeaderScore};
     60 
     61     #[test]
     62     fn fork_point_reports_first_diverging_height() {
     63         assert_eq!(
     64             ForkPoint {
     65                 common_ancestor_height: 41
     66             }
     67             .first_diverging_height(),
     68             42
     69         );
     70     }
     71 
     72     #[test]
     73     fn leader_score_orders_by_mode_rank_then_finalizer_rank_then_proof_rank() {
     74         let base = LeaderScore {
     75             finalizer_mode_rank: 0,
     76             finalizer_rank: 0,
     77             proof_rank: "b".to_string(),
     78         };
     79 
     80         assert!(
     81             base < LeaderScore {
     82                 finalizer_mode_rank: 1,
     83                 finalizer_rank: 0,
     84                 proof_rank: "a".to_string(),
     85             }
     86         );
     87         assert!(
     88             base < LeaderScore {
     89                 finalizer_mode_rank: 0,
     90                 finalizer_rank: 1,
     91                 proof_rank: "a".to_string(),
     92             }
     93         );
     94         assert!(
     95             base > LeaderScore {
     96                 finalizer_mode_rank: 0,
     97                 finalizer_rank: 0,
     98                 proof_rank: "a".to_string(),
     99             }
    100         );
    101     }
    102 
    103     #[test]
    104     fn fork_quality_maps_ordering_without_inversion() {
    105         assert_eq!(
    106             ForkQuality::from(std::cmp::Ordering::Less),
    107             ForkQuality::LocalBetter
    108         );
    109         assert_eq!(
    110             ForkQuality::from(std::cmp::Ordering::Equal),
    111             ForkQuality::Equal
    112         );
    113         assert_eq!(
    114             ForkQuality::from(std::cmp::Ordering::Greater),
    115             ForkQuality::RemoteBetter
    116         );
    117     }
    118 }