Remove support for stake redelegation (#7995)
* Remove support for stake redelegation * fixup
This commit is contained in:
@ -26,6 +26,7 @@ pub mod rpc_port;
|
||||
pub mod short_vec;
|
||||
pub mod slot_hashes;
|
||||
pub mod slot_history;
|
||||
pub mod stake_history;
|
||||
pub mod system_instruction;
|
||||
pub mod system_program;
|
||||
pub mod sysvar;
|
||||
|
74
sdk/src/stake_history.rs
Normal file
74
sdk/src/stake_history.rs
Normal file
@ -0,0 +1,74 @@
|
||||
//! 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 std::ops::Deref;
|
||||
|
||||
pub const MAX_ENTRIES: usize = 512; // it should never take as many as 512 epochs to warm up or cool down
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, PartialEq, Default, Clone)]
|
||||
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
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Serialize, Deserialize, PartialEq, Default, Clone)]
|
||||
pub struct StakeHistory(Vec<(Epoch, StakeHistoryEntry)>);
|
||||
|
||||
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)),
|
||||
}
|
||||
(self.0).truncate(MAX_ENTRIES);
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for StakeHistory {
|
||||
type Target = Vec<(Epoch, StakeHistoryEntry)>;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_stake_history() {
|
||||
let mut stake_history = StakeHistory::default();
|
||||
|
||||
for i in 0..MAX_ENTRIES as u64 + 1 {
|
||||
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()
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
@ -2,26 +2,12 @@
|
||||
//!
|
||||
//! this account carries history about stake activations and de-activations
|
||||
//!
|
||||
pub use crate::clock::Epoch;
|
||||
pub use crate::stake_history::StakeHistory;
|
||||
|
||||
use crate::{account::Account, sysvar::Sysvar};
|
||||
use std::ops::Deref;
|
||||
|
||||
crate::declare_sysvar_id!("SysvarStakeHistory1111111111111111111111111", StakeHistory);
|
||||
|
||||
pub const MAX_ENTRIES: usize = 512; // it should never take as many as 512 epochs to warm up or cool down
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, PartialEq, Default, Clone)]
|
||||
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
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Serialize, Deserialize, PartialEq, Default, Clone)]
|
||||
pub struct StakeHistory(Vec<(Epoch, StakeHistoryEntry)>);
|
||||
|
||||
impl Sysvar for StakeHistory {
|
||||
// override
|
||||
fn size_of() -> usize {
|
||||
@ -30,30 +16,6 @@ impl Sysvar for StakeHistory {
|
||||
}
|
||||
}
|
||||
|
||||
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)),
|
||||
}
|
||||
(self.0).truncate(MAX_ENTRIES);
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for StakeHistory {
|
||||
type Target = Vec<(Epoch, StakeHistoryEntry)>;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_account(lamports: u64, stake_history: &StakeHistory) -> Account {
|
||||
stake_history.create_account(lamports)
|
||||
}
|
||||
@ -61,15 +23,23 @@ pub fn create_account(lamports: u64, stake_history: &StakeHistory) -> Account {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::stake_history::*;
|
||||
|
||||
#[test]
|
||||
fn test_size_of() {
|
||||
let mut stake_history = StakeHistory::default();
|
||||
for i in 0..MAX_ENTRIES as u64 {
|
||||
stake_history.add(
|
||||
i,
|
||||
StakeHistoryEntry {
|
||||
activating: i,
|
||||
..StakeHistoryEntry::default()
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
assert_eq!(
|
||||
bincode::serialized_size(&StakeHistory(vec![
|
||||
(0, StakeHistoryEntry::default());
|
||||
MAX_ENTRIES
|
||||
]))
|
||||
.unwrap() as usize,
|
||||
bincode::serialized_size(&stake_history).unwrap() as usize,
|
||||
StakeHistory::size_of()
|
||||
);
|
||||
}
|
||||
|
Reference in New Issue
Block a user