Skip to main content

clone_solana_hard_forks/
lib.rs

1//! The list of slot boundaries at which a hard fork should
2//! occur.
3
4#![cfg_attr(feature = "frozen-abi", feature(min_specialization))]
5
6#[cfg_attr(
7    feature = "frozen-abi",
8    derive(clone_solana_frozen_abi_macro::AbiExample)
9)]
10#[cfg_attr(
11    feature = "serde",
12    derive(serde_derive::Deserialize, serde_derive::Serialize)
13)]
14#[derive(Clone, Debug, Default, Eq, PartialEq)]
15pub struct HardForks {
16    hard_forks: Vec<(u64, usize)>,
17}
18impl HardForks {
19    // Register a fork to occur at all slots >= `slot` with a parent slot < `slot`
20    pub fn register(&mut self, new_slot: u64) {
21        if let Some(i) = self
22            .hard_forks
23            .iter()
24            .position(|(slot, _)| *slot == new_slot)
25        {
26            self.hard_forks[i] = (new_slot, self.hard_forks[i].1.saturating_add(1));
27        } else {
28            self.hard_forks.push((new_slot, 1));
29        }
30        #[allow(clippy::stable_sort_primitive)]
31        self.hard_forks.sort();
32    }
33
34    // Returns a sorted-by-slot iterator over the registered hark forks
35    pub fn iter(&self) -> std::slice::Iter<(u64, usize)> {
36        self.hard_forks.iter()
37    }
38
39    // Returns `true` is there are currently no registered hard forks
40    pub fn is_empty(&self) -> bool {
41        self.hard_forks.is_empty()
42    }
43
44    // Returns data to include in the bank hash for the given slot if a hard fork is scheduled
45    pub fn get_hash_data(&self, slot: u64, parent_slot: u64) -> Option<[u8; 8]> {
46        // The expected number of hard forks in a cluster is small.
47        // If this turns out to be false then a more efficient data
48        // structure may be needed here to avoid this linear search
49        let fork_count: usize = self
50            .hard_forks
51            .iter()
52            .map(|(fork_slot, fork_count)| {
53                if parent_slot < *fork_slot && slot >= *fork_slot {
54                    *fork_count
55                } else {
56                    0
57                }
58            })
59            .sum();
60
61        (fork_count > 0).then(|| (fork_count as u64).to_le_bytes())
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68
69    #[test]
70    fn iter_is_sorted() {
71        let mut hf = HardForks::default();
72        hf.register(30);
73        hf.register(20);
74        hf.register(10);
75        hf.register(20);
76
77        assert_eq!(hf.hard_forks, vec![(10, 1), (20, 2), (30, 1)]);
78    }
79
80    #[test]
81    fn multiple_hard_forks_since_parent() {
82        let mut hf = HardForks::default();
83        hf.register(10);
84        hf.register(20);
85
86        assert_eq!(hf.get_hash_data(9, 0), None);
87        assert_eq!(hf.get_hash_data(10, 0), Some([1, 0, 0, 0, 0, 0, 0, 0,]));
88        assert_eq!(hf.get_hash_data(19, 0), Some([1, 0, 0, 0, 0, 0, 0, 0,]));
89        assert_eq!(hf.get_hash_data(20, 0), Some([2, 0, 0, 0, 0, 0, 0, 0,]));
90        assert_eq!(hf.get_hash_data(20, 10), Some([1, 0, 0, 0, 0, 0, 0, 0,]));
91        assert_eq!(hf.get_hash_data(20, 11), Some([1, 0, 0, 0, 0, 0, 0, 0,]));
92        assert_eq!(hf.get_hash_data(21, 11), Some([1, 0, 0, 0, 0, 0, 0, 0,]));
93        assert_eq!(hf.get_hash_data(21, 20), None);
94    }
95}