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