Skip to main content

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