-
Notifications
You must be signed in to change notification settings - Fork 160
Use sol_get_sysvar instead of sol_get_<NAME>_sysvar, fixed #458
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
base: master
Are you sure you want to change the base?
Changes from 7 commits
2d0d5e6
08936c4
70cae6a
732d1e8
07e5fb1
a359358
6070ca0
201ffb1
ff3c1f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -156,15 +156,83 @@ | |
|
|
||
| #[cfg(feature = "bincode")] | ||
| use crate::SysvarSerialize; | ||
| use crate::{impl_sysvar_get, Sysvar}; | ||
| use crate::{get_sysvar_via_packed, Sysvar}; | ||
| pub use { | ||
| solana_epoch_rewards::EpochRewards, | ||
| solana_sdk_ids::sysvar::epoch_rewards::{check_id, id, ID}, | ||
| }; | ||
|
|
||
| #[repr(C, packed)] | ||
| #[derive(Clone, Copy)] | ||
| struct EpochRewardsPacked { | ||
| distribution_starting_block_height: u64, | ||
| num_partitions: u64, | ||
| parent_blockhash: [u8; 32], | ||
| total_points: u128, | ||
| total_rewards: u64, | ||
| distributed_rewards: u64, | ||
| active: u8, // bool as u8 | ||
| } | ||
|
|
||
| const _: () = assert!(core::mem::size_of::<EpochRewardsPacked>() == 81); | ||
|
|
||
| impl From<EpochRewardsPacked> for EpochRewards { | ||
| fn from(p: EpochRewardsPacked) -> Self { | ||
|
||
| // Ensure field parity at compile time | ||
| let EpochRewardsPacked { | ||
| distribution_starting_block_height, | ||
| num_partitions, | ||
| parent_blockhash, | ||
| total_points, | ||
| total_rewards, | ||
| distributed_rewards, | ||
| active, | ||
| } = p; | ||
|
|
||
| Self { | ||
| distribution_starting_block_height, | ||
| num_partitions, | ||
| parent_blockhash: solana_hash::Hash::new_from_array(parent_blockhash), | ||
| total_points, | ||
| total_rewards, | ||
| distributed_rewards, | ||
| active: active != 0, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Sysvar for EpochRewards { | ||
| impl_sysvar_get!(sol_get_epoch_rewards_sysvar); | ||
| fn get() -> Result<Self, solana_program_error::ProgramError> { | ||
| get_sysvar_via_packed::<Self, EpochRewardsPacked>(&id()) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(feature = "bincode")] | ||
| impl SysvarSerialize for EpochRewards {} | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use {super::*, crate::Sysvar, serial_test::serial}; | ||
|
|
||
| #[test] | ||
| #[serial] | ||
| #[cfg(feature = "bincode")] | ||
| fn test_epoch_rewards_get() { | ||
| let expected = EpochRewards { | ||
| distribution_starting_block_height: 42, | ||
| num_partitions: 7, | ||
| parent_blockhash: solana_hash::Hash::new_unique(), | ||
| total_points: 1234567890, | ||
| total_rewards: 100, | ||
| distributed_rewards: 10, | ||
| active: true, | ||
| }; | ||
|
|
||
| let data = bincode::serialize(&expected).unwrap(); | ||
| assert_eq!(data.len(), 81); | ||
|
|
||
| crate::tests::mock_get_sysvar_syscall(&data); | ||
| let got = EpochRewards::get().unwrap(); | ||
| assert_eq!(got, expected); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -121,15 +121,68 @@ | |
| //! ``` | ||
| #[cfg(feature = "bincode")] | ||
| use crate::SysvarSerialize; | ||
| use crate::{impl_sysvar_get, Sysvar}; | ||
| use crate::{get_sysvar_via_packed, Sysvar}; | ||
| pub use { | ||
| solana_epoch_schedule::EpochSchedule, | ||
| solana_sdk_ids::sysvar::epoch_schedule::{check_id, id, ID}, | ||
| }; | ||
|
|
||
| #[repr(C, packed)] | ||
| #[derive(Clone, Copy)] | ||
| struct EpochSchedulePacked { | ||
| slots_per_epoch: u64, | ||
| leader_schedule_slot_offset: u64, | ||
| warmup: u8, // bool as u8 | ||
| first_normal_epoch: u64, | ||
| first_normal_slot: u64, | ||
| } | ||
|
||
|
|
||
| const _: () = assert!(core::mem::size_of::<EpochSchedulePacked>() == 33); | ||
|
|
||
| impl From<EpochSchedulePacked> for EpochSchedule { | ||
| fn from(p: EpochSchedulePacked) -> Self { | ||
| // Ensure field parity at compile time | ||
| let EpochSchedulePacked { | ||
| slots_per_epoch, | ||
| leader_schedule_slot_offset, | ||
| warmup, | ||
| first_normal_epoch, | ||
| first_normal_slot, | ||
| } = p; | ||
|
|
||
| Self { | ||
| slots_per_epoch, | ||
| leader_schedule_slot_offset, | ||
| warmup: warmup != 0, | ||
| first_normal_epoch, | ||
| first_normal_slot, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Sysvar for EpochSchedule { | ||
| impl_sysvar_get!(sol_get_epoch_schedule_sysvar); | ||
| fn get() -> Result<Self, solana_program_error::ProgramError> { | ||
| get_sysvar_via_packed::<Self, EpochSchedulePacked>(&id()) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(feature = "bincode")] | ||
| impl SysvarSerialize for EpochSchedule {} | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use {super::*, crate::Sysvar, serial_test::serial}; | ||
|
|
||
| #[test] | ||
| #[serial] | ||
| #[cfg(feature = "bincode")] | ||
| fn test_epoch_schedule_get() { | ||
| let expected = EpochSchedule::custom(1234, 5678, false); | ||
| let data = bincode::serialize(&expected).unwrap(); | ||
| assert_eq!(data.len(), 33); | ||
|
|
||
| crate::tests::mock_get_sysvar_syscall(&data); | ||
| let got = EpochSchedule::get().unwrap(); | ||
| assert_eq!(got, expected); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Enabling
deprecatedto be passed through (as in the original sol_get_sysvar PR)