solana_sdk/
hard_forks.rs

1//! The list of slot boundaries at which a hard fork should
2//! occur.
3
4#![cfg(feature = "full")]
5
6use {
7    byteorder::{ByteOrder, LittleEndian},
8    solana_sdk::clock::Slot,
9};
10
11#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
12#[derive(Default, Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
13pub struct HardForks {
14    hard_forks: Vec<(Slot, 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: Slot) {
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<(Slot, 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: Slot, parent_slot: Slot) -> 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        if fork_count > 0 {
60            let mut buf = [0u8; 8];
61            LittleEndian::write_u64(&mut buf[..], fork_count as u64);
62            Some(buf)
63        } else {
64            None
65        }
66    }
67}
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72
73    #[test]
74    fn iter_is_sorted() {
75        let mut hf = HardForks::default();
76        hf.register(30);
77        hf.register(20);
78        hf.register(10);
79        hf.register(20);
80
81        assert_eq!(hf.hard_forks, vec![(10, 1), (20, 2), (30, 1)]);
82    }
83
84    #[test]
85    fn multiple_hard_forks_since_parent() {
86        let mut hf = HardForks::default();
87        hf.register(10);
88        hf.register(20);
89
90        assert_eq!(hf.get_hash_data(9, 0), None);
91        assert_eq!(hf.get_hash_data(10, 0), Some([1, 0, 0, 0, 0, 0, 0, 0,]));
92        assert_eq!(hf.get_hash_data(19, 0), Some([1, 0, 0, 0, 0, 0, 0, 0,]));
93        assert_eq!(hf.get_hash_data(20, 0), Some([2, 0, 0, 0, 0, 0, 0, 0,]));
94        assert_eq!(hf.get_hash_data(20, 10), Some([1, 0, 0, 0, 0, 0, 0, 0,]));
95        assert_eq!(hf.get_hash_data(20, 11), Some([1, 0, 0, 0, 0, 0, 0, 0,]));
96        assert_eq!(hf.get_hash_data(21, 11), Some([1, 0, 0, 0, 0, 0, 0, 0,]));
97        assert_eq!(hf.get_hash_data(21, 20), None);
98    }
99}