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