solana_epoch_rewards/
lib.rs1#![no_std]
10#![cfg_attr(docsrs, feature(doc_auto_cfg))]
11#![cfg_attr(feature = "frozen-abi", feature(min_specialization))]
12
13#[cfg(feature = "sysvar")]
14pub mod sysvar;
15
16#[cfg(feature = "std")]
17extern crate std;
18#[cfg(feature = "serde")]
19use serde_derive::{Deserialize, Serialize};
20use {solana_hash::Hash, solana_sdk_macro::CloneZeroed};
21
22#[repr(C, align(16))]
23#[cfg_attr(feature = "frozen-abi", derive(solana_frozen_abi_macro::AbiExample))]
24#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
25#[derive(Debug, PartialEq, Eq, Default, CloneZeroed)]
26pub struct EpochRewards {
27 pub distribution_starting_block_height: u64,
30
31 pub num_partitions: u64,
34
35 pub parent_blockhash: Hash,
38
39 pub total_points: u128,
43
44 pub total_rewards: u64,
48
49 pub distributed_rewards: u64,
51
52 pub active: bool,
55}
56
57impl EpochRewards {
58 pub fn distribute(&mut self, amount: u64) {
59 let new_distributed_rewards = self.distributed_rewards.saturating_add(amount);
60 assert!(new_distributed_rewards <= self.total_rewards);
61 self.distributed_rewards = new_distributed_rewards;
62 }
63}
64
65#[cfg(test)]
66mod tests {
67 use super::*;
68
69 impl EpochRewards {
70 pub fn new(
71 total_rewards: u64,
72 distributed_rewards: u64,
73 distribution_starting_block_height: u64,
74 ) -> Self {
75 Self {
76 total_rewards,
77 distributed_rewards,
78 distribution_starting_block_height,
79 ..Self::default()
80 }
81 }
82 }
83
84 #[test]
85 fn test_epoch_rewards_new() {
86 let epoch_rewards = EpochRewards::new(100, 0, 64);
87
88 assert_eq!(epoch_rewards.total_rewards, 100);
89 assert_eq!(epoch_rewards.distributed_rewards, 0);
90 assert_eq!(epoch_rewards.distribution_starting_block_height, 64);
91 }
92
93 #[test]
94 fn test_epoch_rewards_distribute() {
95 let mut epoch_rewards = EpochRewards::new(100, 0, 64);
96 epoch_rewards.distribute(100);
97
98 assert_eq!(epoch_rewards.total_rewards, 100);
99 assert_eq!(epoch_rewards.distributed_rewards, 100);
100 }
101
102 #[test]
103 #[should_panic(expected = "new_distributed_rewards <= self.total_rewards")]
104 fn test_epoch_rewards_distribute_panic() {
105 let mut epoch_rewards = EpochRewards::new(100, 0, 64);
106 epoch_rewards.distribute(200);
107 }
108}