gemachain_program/sysvar/
stake_history.rs

1//! named accounts for synthesized data accounts for bank state, etc.
2//!
3//! this account carries history about stake activations and de-activations
4//!
5pub use crate::stake_history::StakeHistory;
6
7use crate::sysvar::Sysvar;
8
9crate::declare_sysvar_id!("SysvarStakeHistory1111111111111111111111111", StakeHistory);
10
11impl Sysvar for StakeHistory {
12    // override
13    fn size_of() -> usize {
14        // hard-coded so that we don't have to construct an empty
15        16392 // golden, update if MAX_ENTRIES changes
16    }
17}
18
19#[cfg(test)]
20mod tests {
21    use super::*;
22    use crate::stake_history::*;
23
24    #[test]
25    fn test_size_of() {
26        let mut stake_history = StakeHistory::default();
27        for i in 0..MAX_ENTRIES as u64 {
28            stake_history.add(
29                i,
30                StakeHistoryEntry {
31                    activating: i,
32                    ..StakeHistoryEntry::default()
33                },
34            );
35        }
36
37        assert_eq!(
38            bincode::serialized_size(&stake_history).unwrap() as usize,
39            StakeHistory::size_of()
40        );
41    }
42
43    #[test]
44    fn test_create_account() {
45        let mut stake_history = StakeHistory::default();
46        for i in 0..MAX_ENTRIES as u64 + 1 {
47            stake_history.add(
48                i,
49                StakeHistoryEntry {
50                    activating: i,
51                    ..StakeHistoryEntry::default()
52                },
53            );
54        }
55        assert_eq!(stake_history.len(), MAX_ENTRIES);
56        assert_eq!(stake_history.iter().map(|entry| entry.0).min().unwrap(), 1);
57        assert_eq!(stake_history.get(&0), None);
58        assert_eq!(
59            stake_history.get(&1),
60            Some(&StakeHistoryEntry {
61                activating: 1,
62                ..StakeHistoryEntry::default()
63            })
64        );
65    }
66}