Files
solana/sdk/src/sysvar/stake_history.rs

113 lines
3.5 KiB
Rust
Raw Normal View History

2019-08-09 12:31:56 -07:00
//! named accounts for synthesized data accounts for bank state, etc.
//!
//! this account carries history about stake activations and de-activations
//!
pub use crate::clock::Epoch;
use crate::{account::Account, sysvar::Sysvar};
2019-08-09 12:31:56 -07:00
use std::ops::Deref;
crate::declare_sysvar_id!("SysvarStakeHistory1111111111111111111111111", StakeHistory);
2019-08-09 12:31:56 -07:00
pub const MAX_ENTRIES: usize = 512; // it should never take as many as 512 epochs to warm up or cool down
2019-08-09 12:31:56 -07:00
#[derive(Debug, Serialize, Deserialize, PartialEq, Default, Clone)]
2019-08-09 12:31:56 -07:00
pub struct StakeHistoryEntry {
pub effective: u64, // effective stake at this epoch
pub activating: u64, // sum of portion of stakes not fully warmed up
pub deactivating: u64, // requested to be cooled down, not fully deactivated yet
2019-08-09 12:31:56 -07:00
}
#[repr(C)]
#[derive(Debug, Serialize, Deserialize, PartialEq, Default, Clone)]
pub struct StakeHistory(Vec<(Epoch, StakeHistoryEntry)>);
2019-08-09 12:31:56 -07:00
impl Sysvar for StakeHistory {
// override
fn size_of() -> usize {
// hard-coded so that we don't have to construct an empty
16392 // golden, update if MAX_ENTRIES changes
2019-08-09 12:31:56 -07:00
}
}
2019-08-09 12:31:56 -07:00
impl StakeHistory {
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn get(&self, epoch: &Epoch) -> Option<&StakeHistoryEntry> {
self.binary_search_by(|probe| epoch.cmp(&probe.0))
.ok()
.map(|index| &self[index].1)
}
pub fn add(&mut self, epoch: Epoch, entry: StakeHistoryEntry) {
match self.binary_search_by(|probe| epoch.cmp(&probe.0)) {
Ok(index) => (self.0)[index] = (epoch, entry),
Err(index) => (self.0).insert(index, (epoch, entry)),
2019-08-09 12:31:56 -07:00
}
(self.0).truncate(MAX_ENTRIES);
2019-08-09 12:31:56 -07:00
}
}
impl Deref for StakeHistory {
type Target = Vec<(Epoch, StakeHistoryEntry)>;
2019-08-09 12:31:56 -07:00
fn deref(&self) -> &Self::Target {
&self.0
2019-08-09 12:31:56 -07:00
}
}
pub fn create_account(lamports: u64, stake_history: &StakeHistory) -> Account {
stake_history.create_account(lamports)
2019-08-09 12:31:56 -07:00
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_size_of() {
assert_eq!(
bincode::serialized_size(&StakeHistory(vec![
(0, StakeHistoryEntry::default());
MAX_ENTRIES
]))
.unwrap() as usize,
StakeHistory::size_of()
);
}
2019-08-09 12:31:56 -07:00
#[test]
fn test_create_account() {
let lamports = 42;
let account = StakeHistory::default().create_account(lamports);
2019-08-09 12:31:56 -07:00
assert_eq!(account.data.len(), StakeHistory::size_of());
let stake_history = StakeHistory::from_account(&account);
2019-08-09 12:31:56 -07:00
assert_eq!(stake_history, Some(StakeHistory::default()));
let mut stake_history = stake_history.unwrap();
for i in 0..MAX_ENTRIES as u64 + 1 {
2019-08-09 12:31:56 -07:00
stake_history.add(
i,
StakeHistoryEntry {
activating: i,
..StakeHistoryEntry::default()
},
);
}
assert_eq!(stake_history.len(), MAX_ENTRIES);
assert_eq!(stake_history.iter().map(|entry| entry.0).min().unwrap(), 1);
assert_eq!(stake_history.get(&0), None);
assert_eq!(
stake_history.get(&1),
Some(&StakeHistoryEntry {
activating: 1,
..StakeHistoryEntry::default()
})
);
// verify the account can hold a full instance
assert_eq!(
StakeHistory::from_account(&stake_history.create_account(lamports)),
Some(stake_history)
);
2019-08-09 12:31:56 -07:00
}
}