forked from solana-labs/solana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract sysvar crate, including program_stubs.rs (solana-labs#3680)
* minimise solana_program usage in sysvar and program_stubs modules * move solana_program::stake_history contents into solana_program::sysvar * extract sysvar crate, including program_stubs.rs * remove trailing whitespace * update program_stubs path in nits.sh * missing re-export * missing frozen-abi support * feature activation fixes * remove unused dev dep * update digest * fix sbf build * fix re-export * missing ; * fmt * inline re-export for cleaner docs * make serde and bincode optional in solana-sysvar * make bytemuck optional in sysvar crate * Apply suggestions Co-authored-by: Jon C <me@jonc.dev> * remove superfluous :: * remove unnecessary feature gating * move frozen-abi activation to the right file * remove message dep from the tests that were moved * move those tests back * add deprecation * remove unnecessary lifetimes --------- Co-authored-by: Jon C <me@jonc.dev>
- Loading branch information
1 parent
926c009
commit aeb5518
Showing
28 changed files
with
808 additions
and
471 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,127 +1,6 @@ | ||
//! A type to hold data for the [`StakeHistory` sysvar][sv]. | ||
//! | ||
//! [sv]: https://docs.solanalabs.com/runtime/sysvars#stakehistory | ||
//! | ||
//! The sysvar ID is declared in [`sysvar::stake_history`]. | ||
//! | ||
//! [`sysvar::stake_history`]: crate::sysvar::stake_history | ||
pub use solana_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 | ||
|
||
#[repr(C)] | ||
#[cfg_attr(feature = "frozen-abi", derive(AbiExample))] | ||
#[derive(Debug, Serialize, Deserialize, 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), | ||
} | ||
} | ||
} | ||
|
||
#[repr(C)] | ||
#[cfg_attr(feature = "frozen-abi", derive(AbiExample))] | ||
#[derive(Debug, Serialize, Deserialize, 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()) | ||
} | ||
} | ||
|
||
#[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() | ||
}) | ||
); | ||
} | ||
} | ||
pub use { | ||
crate::sysvar::stake_history::{ | ||
StakeHistory, StakeHistoryEntry, StakeHistoryGetEntry, MAX_ENTRIES, | ||
}, | ||
solana_clock::Epoch, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#[deprecated(since = "2.1.0", note = "Use `solana-sysvar-id` crate instead")] | ||
pub use solana_sysvar_id::{declare_deprecated_sysvar_id, declare_sysvar_id, SysvarId}; | ||
#[deprecated(since = "2.2.0", note = "Use `solana-sysvar` crate instead")] | ||
#[allow(deprecated)] | ||
pub use { | ||
solana_sdk_ids::sysvar::{check_id, id, ID}, | ||
solana_sysvar::{ | ||
clock, epoch_rewards, epoch_schedule, fees, instructions, is_sysvar_id, last_restart_slot, | ||
recent_blockhashes, rent, rewards, slot_hashes, slot_history, stake_history, Sysvar, | ||
ALL_IDS, | ||
}, | ||
}; |
Oops, something went wrong.