|
154 | 154 | //! # Ok::<(), anyhow::Error>(()) |
155 | 155 | //! ``` |
156 | 156 |
|
| 157 | +use crate::impl_sysvar_from_repr; |
157 | 158 | #[cfg(feature = "bincode")] |
158 | 159 | use crate::SysvarSerialize; |
159 | | -use crate::{impl_sysvar_get, 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 | | -impl Sysvar for EpochRewards { |
166 | | - impl_sysvar_get!(sol_get_epoch_rewards_sysvar); |
| 165 | +/// Compact 1-byte-aligned representation of [`EpochRewards`] matching its serialized form. |
| 166 | +/// |
| 167 | +/// The on-chain encoding is 81 bytes: u64 + u64 + Hash(32) + u128 + u64 + u64 + bool. |
| 168 | +/// The canonical `EpochRewards` struct uses `#[repr(C)]` and contains padding, |
| 169 | +/// so we define this compact layout to avoid undefined behavior when loading. |
| 170 | +#[repr(C, packed)] |
| 171 | +#[derive(Clone, Copy, Default)] |
| 172 | +struct EpochRewardsRepr { |
| 173 | + distribution_starting_block_height: u64, |
| 174 | + num_partitions: u64, |
| 175 | + parent_blockhash: [u8; 32], |
| 176 | + total_points: u128, |
| 177 | + total_rewards: u64, |
| 178 | + distributed_rewards: u64, |
| 179 | + active: u8, // bool serialized as u8 |
167 | 180 | } |
168 | 181 |
|
| 182 | +impl From<EpochRewardsRepr> for EpochRewards { |
| 183 | + fn from(r: EpochRewardsRepr) -> Self { |
| 184 | + Self { |
| 185 | + distribution_starting_block_height: r.distribution_starting_block_height, |
| 186 | + num_partitions: r.num_partitions, |
| 187 | + parent_blockhash: solana_hash::Hash::new_from_array(r.parent_blockhash), |
| 188 | + total_points: r.total_points, |
| 189 | + total_rewards: r.total_rewards, |
| 190 | + distributed_rewards: r.distributed_rewards, |
| 191 | + active: r.active != 0, |
| 192 | + } |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +impl_sysvar_from_repr!(EpochRewards, EpochRewardsRepr, id()); |
| 197 | + |
169 | 198 | #[cfg(feature = "bincode")] |
170 | 199 | impl SysvarSerialize for EpochRewards {} |
| 200 | + |
| 201 | +#[cfg(test)] |
| 202 | +mod tests { |
| 203 | + use {super::*, crate::Sysvar, serial_test::serial}; |
| 204 | + |
| 205 | + #[test] |
| 206 | + fn test_epoch_rewards_repr_size() { |
| 207 | + assert_eq!(core::mem::size_of::<EpochRewardsRepr>(), 81); |
| 208 | + } |
| 209 | + |
| 210 | + #[test] |
| 211 | + fn test_epoch_rewards_repr_conversion() { |
| 212 | + let hash_bytes = [42u8; 32]; |
| 213 | + let repr = EpochRewardsRepr { |
| 214 | + distribution_starting_block_height: 42, |
| 215 | + num_partitions: 7, |
| 216 | + parent_blockhash: hash_bytes, |
| 217 | + total_points: 1234567890, |
| 218 | + total_rewards: 100, |
| 219 | + distributed_rewards: 10, |
| 220 | + active: 1, // true |
| 221 | + }; |
| 222 | + let rewards = EpochRewards::from(repr); |
| 223 | + assert_eq!(rewards.distribution_starting_block_height, 42); |
| 224 | + assert_eq!(rewards.num_partitions, 7); |
| 225 | + assert_eq!( |
| 226 | + rewards.parent_blockhash, |
| 227 | + solana_hash::Hash::new_from_array(hash_bytes) |
| 228 | + ); |
| 229 | + assert_eq!(rewards.total_points, 1234567890); |
| 230 | + assert_eq!(rewards.total_rewards, 100); |
| 231 | + assert_eq!(rewards.distributed_rewards, 10); |
| 232 | + assert_eq!(rewards.active, true); |
| 233 | + } |
| 234 | + |
| 235 | + #[test] |
| 236 | + #[serial] |
| 237 | + #[cfg(feature = "bincode")] |
| 238 | + fn test_epoch_rewards_get_with_bincode_serialized_data() { |
| 239 | + use { |
| 240 | + crate::program_stubs::{set_syscall_stubs, SyscallStubs}, |
| 241 | + solana_program_entrypoint::SUCCESS, |
| 242 | + }; |
| 243 | + |
| 244 | + let expected = EpochRewards { |
| 245 | + distribution_starting_block_height: 42, |
| 246 | + num_partitions: 7, |
| 247 | + parent_blockhash: solana_hash::Hash::new_unique(), |
| 248 | + total_points: 1234567890, |
| 249 | + total_rewards: 100, |
| 250 | + distributed_rewards: 10, |
| 251 | + active: true, |
| 252 | + }; |
| 253 | + |
| 254 | + let data = bincode::serialize(&expected).unwrap(); |
| 255 | + assert_eq!(data.len(), 81, "Bincode serialization should be 81 bytes"); |
| 256 | + |
| 257 | + struct MockSyscall { |
| 258 | + data: Vec<u8>, |
| 259 | + } |
| 260 | + impl SyscallStubs for MockSyscall { |
| 261 | + fn sol_get_sysvar( |
| 262 | + &self, |
| 263 | + _sysvar_id_addr: *const u8, |
| 264 | + var_addr: *mut u8, |
| 265 | + offset: u64, |
| 266 | + length: u64, |
| 267 | + ) -> u64 { |
| 268 | + unsafe { |
| 269 | + let slice = core::slice::from_raw_parts_mut(var_addr, length as usize); |
| 270 | + slice.copy_from_slice(&self.data[offset as usize..(offset + length) as usize]); |
| 271 | + } |
| 272 | + SUCCESS |
| 273 | + } |
| 274 | + } |
| 275 | + |
| 276 | + set_syscall_stubs(Box::new(MockSyscall { data })); |
| 277 | + let got = EpochRewards::get().unwrap(); |
| 278 | + assert_eq!(got, expected); |
| 279 | + } |
| 280 | +} |
0 commit comments