mithril_common/entities/
cardano_chain_point.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use std::cmp::Ordering;
use std::fmt::{Display, Formatter};

use serde::{Deserialize, Serialize};

use crate::entities::{BlockNumber, SlotNumber};

/// Hash of a Cardano Block
pub type BlockHash = String;

/// The Cardano chain point which is used to identify a specific point in the Cardano chain.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ChainPoint {
    /// The [slot number](https://docs.cardano.org/learn/cardano-node/#slotsandepochs)
    pub slot_number: SlotNumber,

    ///  The block number
    pub block_number: BlockNumber,

    /// The hex encoded block hash
    pub block_hash: BlockHash,
}

impl ChainPoint {
    /// [ChainPoint] factory
    pub fn new<T: Into<BlockHash>>(
        slot_number: SlotNumber,
        block_number: BlockNumber,
        block_hash: T,
    ) -> ChainPoint {
        ChainPoint {
            slot_number,
            block_number,
            block_hash: block_hash.into(),
        }
    }

    cfg_test_tools! {
        /// Create a dummy ChainPoint
        pub fn dummy() -> Self {
            Self {
                slot_number: SlotNumber(100),
                block_number: BlockNumber(0),
                block_hash: "block_hash-50".to_string(),
            }
        }
    }
}

impl Display for ChainPoint {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "ChainPoint (slot_number: {}, block_number: {}, block_hash: {})",
            self.slot_number, self.block_number, self.block_hash
        )
    }
}

impl PartialOrd for ChainPoint {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for ChainPoint {
    fn cmp(&self, other: &Self) -> Ordering {
        self.block_number
            .cmp(&other.block_number)
            .then(self.slot_number.cmp(&other.slot_number))
            .then(self.block_hash.cmp(&other.block_hash))
    }
}

#[cfg(test)]
mod tests {
    use std::cmp::Ordering;

    use super::*;

    #[test]
    fn chain_point_ord_cmp_block_number_take_precedence_over_other_fields() {
        let chain_point1 = ChainPoint {
            slot_number: SlotNumber(15),
            block_number: BlockNumber(10),
            block_hash: "hash2".to_string(),
        };
        let chain_point2 = ChainPoint {
            slot_number: SlotNumber(5),
            block_number: BlockNumber(20),
            block_hash: "hash1".to_string(),
        };

        assert_eq!(Ordering::Less, chain_point1.cmp(&chain_point2));
    }

    #[test]
    fn chain_point_ord_cmp_if_block_number_equals_then_compare_slot_numbers() {
        let chain_point1 = ChainPoint {
            slot_number: SlotNumber(15),
            block_number: BlockNumber(0),
            block_hash: "hash2".to_string(),
        };
        let chain_point2 = ChainPoint {
            slot_number: SlotNumber(5),
            block_number: BlockNumber(0),
            block_hash: "hash1".to_string(),
        };

        assert_eq!(Ordering::Greater, chain_point1.cmp(&chain_point2));
    }

    #[test]
    fn chain_point_ord_cmp_if_block_number_and_slot_number_equals_then_compare_block_hash() {
        let chain_point1 = ChainPoint {
            slot_number: SlotNumber(5),
            block_number: BlockNumber(10),
            block_hash: "hash1".to_string(),
        };
        let chain_point2 = ChainPoint {
            slot_number: SlotNumber(5),
            block_number: BlockNumber(10),
            block_hash: "hash2".to_string(),
        };

        assert_eq!(Ordering::Less, chain_point1.cmp(&chain_point2));
    }
}