use {crate::hash::Hash, solana_sdk_macro::CloneZeroed, std::ops::AddAssign};
#[repr(C, align(16))]
#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Default, CloneZeroed)]
pub struct EpochRewards {
pub distribution_starting_block_height: u64,
pub num_partitions: u64,
pub parent_blockhash: Hash,
pub total_points: u128,
pub total_rewards: u64,
pub distributed_rewards: u64,
pub active: bool,
}
impl EpochRewards {
pub fn distribute(&mut self, amount: u64) {
assert!(self.distributed_rewards.saturating_add(amount) <= self.total_rewards);
self.distributed_rewards.add_assign(amount);
}
}
#[cfg(test)]
mod tests {
use super::*;
impl EpochRewards {
pub fn new(
total_rewards: u64,
distributed_rewards: u64,
distribution_starting_block_height: u64,
) -> Self {
Self {
total_rewards,
distributed_rewards,
distribution_starting_block_height,
..Self::default()
}
}
}
#[test]
fn test_epoch_rewards_new() {
let epoch_rewards = EpochRewards::new(100, 0, 64);
assert_eq!(epoch_rewards.total_rewards, 100);
assert_eq!(epoch_rewards.distributed_rewards, 0);
assert_eq!(epoch_rewards.distribution_starting_block_height, 64);
}
#[test]
fn test_epoch_rewards_distribute() {
let mut epoch_rewards = EpochRewards::new(100, 0, 64);
epoch_rewards.distribute(100);
assert_eq!(epoch_rewards.total_rewards, 100);
assert_eq!(epoch_rewards.distributed_rewards, 100);
}
#[test]
#[should_panic(
expected = "self.distributed_rewards.saturating_add(amount) <= self.total_rewards"
)]
fn test_epoch_rewards_distribute_panic() {
let mut epoch_rewards = EpochRewards::new(100, 0, 64);
epoch_rewards.distribute(200);
}
}