|
156 | 156 |
|
157 | 157 | #[cfg(feature = "bincode")] |
158 | 158 | use crate::SysvarSerialize; |
159 | | -use crate::{impl_sysvar_get, Sysvar}; |
| 159 | +use crate::{get_sysvar_via_packed, sysvar_packed_struct, Sysvar}; |
160 | 160 | pub use { |
161 | 161 | solana_epoch_rewards::EpochRewards, |
162 | 162 | solana_sdk_ids::sysvar::epoch_rewards::{check_id, id, ID}, |
163 | 163 | }; |
164 | 164 |
|
| 165 | +sysvar_packed_struct! { |
| 166 | + struct EpochRewardsPacked(81) { |
| 167 | + distribution_starting_block_height: u64, |
| 168 | + num_partitions: u64, |
| 169 | + parent_blockhash: [u8; 32], |
| 170 | + total_points: u128, |
| 171 | + total_rewards: u64, |
| 172 | + distributed_rewards: u64, |
| 173 | + active: u8, // bool as u8 |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +impl From<EpochRewardsPacked> for EpochRewards { |
| 178 | + fn from(p: EpochRewardsPacked) -> Self { |
| 179 | + Self { |
| 180 | + distribution_starting_block_height: p.distribution_starting_block_height, |
| 181 | + num_partitions: p.num_partitions, |
| 182 | + parent_blockhash: solana_hash::Hash::new_from_array(p.parent_blockhash), |
| 183 | + total_points: p.total_points, |
| 184 | + total_rewards: p.total_rewards, |
| 185 | + distributed_rewards: p.distributed_rewards, |
| 186 | + active: p.active != 0, |
| 187 | + } |
| 188 | + } |
| 189 | +} |
| 190 | + |
165 | 191 | impl Sysvar for EpochRewards { |
166 | | - impl_sysvar_get!(sol_get_epoch_rewards_sysvar); |
| 192 | + fn get() -> Result<Self, solana_program_error::ProgramError> { |
| 193 | + get_sysvar_via_packed::<Self, EpochRewardsPacked>(&id()) |
| 194 | + } |
167 | 195 | } |
168 | 196 |
|
169 | 197 | #[cfg(feature = "bincode")] |
170 | 198 | impl SysvarSerialize for EpochRewards {} |
| 199 | + |
| 200 | +#[cfg(test)] |
| 201 | +mod tests { |
| 202 | + use {super::*, crate::Sysvar, serial_test::serial}; |
| 203 | + |
| 204 | + #[test] |
| 205 | + fn test_epoch_rewards_packed_size() { |
| 206 | + assert_eq!(core::mem::size_of::<EpochRewardsPacked>(), 81); |
| 207 | + } |
| 208 | + |
| 209 | + #[test] |
| 210 | + #[serial] |
| 211 | + #[cfg(feature = "bincode")] |
| 212 | + fn test_epoch_rewards_get() { |
| 213 | + use { |
| 214 | + crate::program_stubs::{set_syscall_stubs, SyscallStubs}, |
| 215 | + solana_program_entrypoint::SUCCESS, |
| 216 | + }; |
| 217 | + |
| 218 | + let expected = EpochRewards { |
| 219 | + distribution_starting_block_height: 42, |
| 220 | + num_partitions: 7, |
| 221 | + parent_blockhash: solana_hash::Hash::new_unique(), |
| 222 | + total_points: 1234567890, |
| 223 | + total_rewards: 100, |
| 224 | + distributed_rewards: 10, |
| 225 | + active: true, |
| 226 | + }; |
| 227 | + |
| 228 | + let data = bincode::serialize(&expected).unwrap(); |
| 229 | + assert_eq!(data.len(), 81); |
| 230 | + |
| 231 | + struct MockSyscall { |
| 232 | + data: Vec<u8>, |
| 233 | + } |
| 234 | + impl SyscallStubs for MockSyscall { |
| 235 | + fn sol_get_sysvar( |
| 236 | + &self, |
| 237 | + _sysvar_id_addr: *const u8, |
| 238 | + var_addr: *mut u8, |
| 239 | + offset: u64, |
| 240 | + length: u64, |
| 241 | + ) -> u64 { |
| 242 | + unsafe { |
| 243 | + let slice = core::slice::from_raw_parts_mut(var_addr, length as usize); |
| 244 | + slice.copy_from_slice(&self.data[offset as usize..(offset + length) as usize]); |
| 245 | + } |
| 246 | + SUCCESS |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + set_syscall_stubs(Box::new(MockSyscall { data })); |
| 251 | + let got = EpochRewards::get().unwrap(); |
| 252 | + assert_eq!(got, expected); |
| 253 | + } |
| 254 | +} |
0 commit comments