From a054e46a0fdc950bf0dc617a5c55ad4e12d92b32 Mon Sep 17 00:00:00 2001 From: Rob Walker Date: Sat, 11 Jul 2020 21:31:16 -0700 Subject: [PATCH] test typical values --- programs/stake/src/stake_state.rs | 43 ++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/programs/stake/src/stake_state.rs b/programs/stake/src/stake_state.rs index a193b2cbdb..8e2cf7e142 100644 --- a/programs/stake/src/stake_state.rs +++ b/programs/stake/src/stake_state.rs @@ -1013,7 +1013,7 @@ pub fn create_account( mod tests { use super::*; use crate::id; - use solana_sdk::{account::Account, pubkey::Pubkey, system_program}; + use solana_sdk::{account::Account, native_token, pubkey::Pubkey, system_program}; use solana_vote_program::vote_state; use std::cell::RefCell; @@ -2264,6 +2264,47 @@ mod tests { assert_eq!(stake.credits_observed, 2); } + #[test] + fn test_stake_state_calculate_points_with_typical_values() { + let mut vote_state = VoteState::default(); + + // bootstrap means fully-vested stake at epoch 0 with + // 10_000_000 SOL is a big but not unreasaonable stake + let stake = Stake::new( + native_token::sol_to_lamports(10_000_000f64), + &Pubkey::default(), + &vote_state, + std::u64::MAX, + &Config::default(), + ); + + // this one can't collect now, credits_observed == vote_state.credits() + assert_eq!( + None, + stake.calculate_rewards( + &PointValue { + rewards: 1_000_000_000, + points: 1 + }, + &vote_state, + None + ) + ); + + let epoch_slots: u128 = 14 * 24 * 3600 * 160; + // put 193,536,000 credits in at epoch 0, typical for a 14-day epoch + // this loop takes a few seconds... + for _ in 0..epoch_slots { + vote_state.increment_credits(0); + } + + // no overflow on points + assert_eq!( + u128::from(stake.delegation.stake) * epoch_slots, + stake.calculate_points(&vote_state, None) + ); + } + #[test] fn test_stake_state_calculate_rewards() { let mut vote_state = VoteState::default();