Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy solana-stake-interface into this repo #4535

Closed
wants to merge 11 commits into from
Prev Previous commit
Next Next commit
use stake-interface crate in sysvar and program crates
kevinheavey committed Jan 19, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 116f0322b8664fe1ad8bab2d1b10eec9714c92f0
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 20 additions & 1 deletion programs/sbf/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sdk/program/Cargo.toml
Original file line number Diff line number Diff line change
@@ -74,6 +74,7 @@ solana-short-vec = { workspace = true }
solana-slot-hashes = { workspace = true, features = ["serde", "sysvar"] }
solana-slot-history = { workspace = true, features = ["serde", "sysvar"] }
solana-stable-layout = { workspace = true }
solana-stake-interface = { workspace = true, features = ["bincode"] }
solana-system-interface = { workspace = true, features = ["bincode"] }
solana-sysvar = { workspace = true, features = ["bincode", "bytemuck"] }
solana-sysvar-id = { workspace = true }
13 changes: 13 additions & 0 deletions sdk/program/src/stake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#[deprecated(since = "2.2.0", note = "Use solana-stake-interface instead")]
pub use solana_stake_interface::{
config, stake_flags, state, tools, MINIMUM_DELINQUENT_EPOCHS_FOR_DEACTIVATION,
};

pub mod instruction {
#[deprecated(since = "2.2.0", note = "Use solana-stake-interface instead")]
pub use solana_stake_interface::{error::StakeError, instruction::*};
}

pub mod program {
pub use solana_sdk_ids::stake::{check_id, id, ID};
}
32 changes: 0 additions & 32 deletions sdk/program/src/stake/config.rs

This file was deleted.

954 changes: 0 additions & 954 deletions sdk/program/src/stake/instruction.rs

This file was deleted.

18 changes: 0 additions & 18 deletions sdk/program/src/stake/mod.rs

This file was deleted.

140 changes: 0 additions & 140 deletions sdk/program/src/stake/stake_flags.rs

This file was deleted.

1,301 changes: 0 additions & 1,301 deletions sdk/program/src/stake/state.rs

This file was deleted.

155 changes: 0 additions & 155 deletions sdk/program/src/stake/tools.rs

This file was deleted.

3 changes: 2 additions & 1 deletion sdk/sysvar/Cargo.toml
Original file line number Diff line number Diff line change
@@ -36,6 +36,7 @@ solana-sdk-ids = { workspace = true }
solana-sdk-macro = { workspace = true }
solana-slot-hashes = { workspace = true, features = ["sysvar"] }
solana-slot-history = { workspace = true, features = ["sysvar"] }
solana-stake-interface = { workspace = true }
solana-sysvar-id = { workspace = true }

[target.'cfg(not(target_os = "solana"))'.dependencies]
@@ -56,7 +57,7 @@ solana-sysvar = { path = ".", features = ["dev-context-only-utils"] }
test-case = { workspace = true }

[features]
bincode = ["dep:bincode", "serde"]
bincode = ["dep:bincode", "serde", "solana-stake-interface/bincode"]
bytemuck = ["dep:bytemuck", "dep:bytemuck_derive"]
dev-context-only-utils = ["bincode", "bytemuck", "solana-instructions-sysvar/dev-context-only-utils"]
frozen-abi = ["dep:solana-frozen-abi", "dep:solana-frozen-abi-macro"]
135 changes: 8 additions & 127 deletions sdk/sysvar/src/stake_history.rs
Original file line number Diff line number Diff line change
@@ -48,109 +48,15 @@
#[cfg(feature = "bincode")]
use crate::Sysvar;
#[cfg(feature = "serde")]
use serde_derive::{Deserialize, Serialize};
pub use solana_sdk_ids::sysvar::stake_history::{check_id, id, ID};
use {
crate::get_sysvar,
solana_clock::Epoch,
solana_sysvar_id::{impl_sysvar_id, SysvarId},
std::ops::Deref,
#[deprecated(
since = "2.2.0",
note = "Use solana_stake_interface::stake_history instead"
)]
pub use solana_stake_interface::stake_history::{
StakeHistory, StakeHistoryEntry, StakeHistoryGetEntry, MAX_ENTRIES,
};

pub const MAX_ENTRIES: usize = 512; // it should never take as many as 512 epochs to warm up or cool down

#[repr(C)]
#[cfg_attr(feature = "frozen-abi", derive(solana_frozen_abi_macro::AbiExample))]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[derive(Debug, PartialEq, Eq, 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
}

impl StakeHistoryEntry {
pub fn with_effective(effective: u64) -> Self {
Self {
effective,
..Self::default()
}
}

pub fn with_effective_and_activating(effective: u64, activating: u64) -> Self {
Self {
effective,
activating,
..Self::default()
}
}

pub fn with_deactivating(deactivating: u64) -> Self {
Self {
effective: deactivating,
deactivating,
..Self::default()
}
}
}

impl std::ops::Add for StakeHistoryEntry {
type Output = StakeHistoryEntry;
fn add(self, rhs: StakeHistoryEntry) -> Self::Output {
Self {
effective: self.effective.saturating_add(rhs.effective),
activating: self.activating.saturating_add(rhs.activating),
deactivating: self.deactivating.saturating_add(rhs.deactivating),
}
}
}

/// A type to hold data for the [`StakeHistory` sysvar][sv].
///
/// [sv]: https://docs.solanalabs.com/runtime/sysvars#stakehistory
#[repr(C)]
#[cfg_attr(feature = "frozen-abi", derive(solana_frozen_abi_macro::AbiExample))]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[derive(Debug, PartialEq, Eq, Default, Clone)]
pub struct StakeHistory(Vec<(Epoch, StakeHistoryEntry)>);

impl StakeHistory {
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 trait StakeHistoryGetEntry {
fn get_entry(&self, epoch: Epoch) -> Option<StakeHistoryEntry>;
}

impl StakeHistoryGetEntry for StakeHistory {
fn get_entry(&self, epoch: Epoch) -> Option<StakeHistoryEntry> {
self.binary_search_by(|probe| epoch.cmp(&probe.0))
.ok()
.map(|index| self[index].1.clone())
}
}

impl_sysvar_id!(StakeHistory);
use {crate::get_sysvar, solana_clock::Epoch};

#[cfg(feature = "bincode")]
impl Sysvar for StakeHistory {
@@ -193,7 +99,7 @@ impl StakeHistoryGetEntry for StakeHistorySysvar {
let mut entry_buf = [0; EPOCH_AND_ENTRY_SERIALIZED_SIZE as usize];
let result = get_sysvar(
&mut entry_buf,
&StakeHistory::id(),
&id(),
offset,
EPOCH_AND_ENTRY_SERIALIZED_SIZE,
);
@@ -224,31 +130,6 @@ impl StakeHistoryGetEntry for StakeHistorySysvar {
mod tests {
use {super::*, crate::tests::mock_get_sysvar_syscall, serial_test::serial};

#[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()
})
);
}

#[test]
fn test_size_of() {
let mut stake_history = StakeHistory::default();
21 changes: 20 additions & 1 deletion svm/examples/Cargo.lock