From efd0bcdc286cb07e1a578fe107c3564cf6eabe01 Mon Sep 17 00:00:00 2001 From: Evan Batsell Date: Tue, 3 Dec 2024 18:55:45 -0500 Subject: [PATCH] tests for new epoch credits methods --- tests/tests/validator_history/mod.rs | 2 + tests/tests/validator_history/test_state.rs | 74 +++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 tests/tests/validator_history/test_state.rs diff --git a/tests/tests/validator_history/mod.rs b/tests/tests/validator_history/mod.rs index 1ac5cac7..b801bd03 100644 --- a/tests/tests/validator_history/mod.rs +++ b/tests/tests/validator_history/mod.rs @@ -4,3 +4,5 @@ mod test_initialize; mod test_mev_commission; mod test_stake; mod test_vote_account; + +mod test_state; diff --git a/tests/tests/validator_history/test_state.rs b/tests/tests/validator_history/test_state.rs new file mode 100644 index 00000000..9428bc11 --- /dev/null +++ b/tests/tests/validator_history/test_state.rs @@ -0,0 +1,74 @@ + +se validator_history::constants::TVC_MULTIPLIER; +use validator_history::state::CircBuf; +use validator_history::ValidatorHistoryEntry; + +const MAX_ITEMS: usize = 512; +#[test] +fn test_normalized_epoch_credits_latest() { + let mut circ_buf = CircBuf { + idx: 4, + is_empty: 0, + padding: [0; 7], + arr: [ValidatorHistoryEntry::default(); MAX_ITEMS], + }; + + for i in 0..5 { + circ_buf.arr[i] = ValidatorHistoryEntry { + epoch_credits: 1000, + epoch: i as u16, + ..ValidatorHistoryEntry::default() + }; + } + + // Test normalizing an epoch before tvc activation + assert_eq!( + circ_buf.epoch_credits_latest_normalized(3, 4), + Some(1000 * TVC_MULTIPLIER) + ); + + // Test normalizing an epoch after tvc activation + assert_eq!(circ_buf.epoch_credits_latest_normalized(3, 3), Some(1000)); +} + +#[test] +fn test_epoch_credits_range_normalized() { + let mut circ_buf = CircBuf { + idx: 4, + is_empty: 0, + padding: [0; 7], + arr: [ValidatorHistoryEntry::default(); MAX_ITEMS], + }; + + for i in 0..5 { + circ_buf.arr[i] = ValidatorHistoryEntry { + epoch_credits: 1000, + epoch: i as u16, + ..ValidatorHistoryEntry::default() + }; + } + + // Test normalizing epochs before tvc activation + assert_eq!( + circ_buf.epoch_credits_range_normalized(0, 4, 5), + vec![ + Some(1000 * TVC_MULTIPLIER), + Some(1000 * TVC_MULTIPLIER), + Some(1000 * TVC_MULTIPLIER), + Some(1000 * TVC_MULTIPLIER), + Some(1000 * TVC_MULTIPLIER) + ] + ); + + // Test normalizing epochs with tvc activation in middle of range + assert_eq!( + circ_buf.epoch_credits_range_normalized(0, 4, 2), + vec![ + Some(1000 * TVC_MULTIPLIER), + Some(1000 * TVC_MULTIPLIER), + Some(1000), + Some(1000), + Some(1000) + ] + ); +}