atlas_hard_forks/
lib.rs

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