gemachain_program/
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::clock::Epoch;
6
7use std::ops::Deref;
8
9pub const MAX_ENTRIES: usize = 512; // it should never take as many as 512 epochs to warm up or cool down
10
11#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Default, Clone, AbiExample)]
12pub struct StakeHistoryEntry {
13    pub effective: u64,    // effective stake at this epoch
14    pub activating: u64,   // sum of portion of stakes not fully warmed up
15    pub deactivating: u64, // requested to be cooled down, not fully deactivated yet
16}
17
18#[repr(C)]
19#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Default, Clone, AbiExample)]
20pub struct StakeHistory(Vec<(Epoch, StakeHistoryEntry)>);
21
22impl StakeHistory {
23    #[allow(clippy::trivially_copy_pass_by_ref)]
24    pub fn get(&self, epoch: &Epoch) -> Option<&StakeHistoryEntry> {
25        self.binary_search_by(|probe| epoch.cmp(&probe.0))
26            .ok()
27            .map(|index| &self[index].1)
28    }
29
30    pub fn add(&mut self, epoch: Epoch, entry: StakeHistoryEntry) {
31        match self.binary_search_by(|probe| epoch.cmp(&probe.0)) {
32            Ok(index) => (self.0)[index] = (epoch, entry),
33            Err(index) => (self.0).insert(index, (epoch, entry)),
34        }
35        (self.0).truncate(MAX_ENTRIES);
36    }
37}
38
39impl Deref for StakeHistory {
40    type Target = Vec<(Epoch, StakeHistoryEntry)>;
41    fn deref(&self) -> &Self::Target {
42        &self.0
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn test_stake_history() {
52        let mut stake_history = StakeHistory::default();
53
54        for i in 0..MAX_ENTRIES as u64 + 1 {
55            stake_history.add(
56                i,
57                StakeHistoryEntry {
58                    activating: i,
59                    ..StakeHistoryEntry::default()
60                },
61            );
62        }
63        assert_eq!(stake_history.len(), MAX_ENTRIES);
64        assert_eq!(stake_history.iter().map(|entry| entry.0).min().unwrap(), 1);
65        assert_eq!(stake_history.get(&0), None);
66        assert_eq!(
67            stake_history.get(&1),
68            Some(&StakeHistoryEntry {
69                activating: 1,
70                ..StakeHistoryEntry::default()
71            })
72        );
73    }
74}