From 46935c022eb544033347cdfb86b31ba2a580171b Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Tue, 7 Dec 2021 01:44:49 +0000 Subject: [PATCH] Ensure that StakeDelegations and StakeHistory serde (#21640) (#21653) Add tests to StakeDelegations and StakeHistory to ensure that the outer types serialize and deserialize correctly to/from the inner types. (cherry picked from commit da4015a959e1ae318092d95933300d7fab356175) Co-authored-by: Brooks Prumo --- runtime/src/stake_delegations.rs | 42 ++++++++++++++++++++++++++++++++ runtime/src/stake_history.rs | 35 ++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/runtime/src/stake_delegations.rs b/runtime/src/stake_delegations.rs index aedd0272055a6a..5dc69b4b7b0920 100644 --- a/runtime/src/stake_delegations.rs +++ b/runtime/src/stake_delegations.rs @@ -35,6 +35,7 @@ type StakeDelegationsInner = HashMap; mod tests { use super::*; + /// Ensure that StakeDelegations is indeed clone-on-write #[test] fn test_stake_delegations_is_cow() { let voter_pubkey = Pubkey::new_unique(); @@ -83,4 +84,45 @@ mod tests { ); } } + + /// Ensure that StakeDelegations serializes and deserializes between the inner and outer types + #[test] + fn test_stake_delegations_serde() { + let voter_pubkey = Pubkey::new_unique(); + let stake = rand::random(); + let activation_epoch = rand::random(); + let warmup_cooldown_rate = rand::random(); + let delegation = + Delegation::new(&voter_pubkey, stake, activation_epoch, warmup_cooldown_rate); + + let pubkey = Pubkey::new_unique(); + + let mut stake_delegations_outer = StakeDelegations::default(); + stake_delegations_outer.insert(pubkey, delegation); + + let mut stake_delegations_inner = StakeDelegationsInner::default(); + stake_delegations_inner.insert(pubkey, delegation); + + // Test: Assert that serializing the outer and inner types produces the same data + assert_eq!( + bincode::serialize(&stake_delegations_outer).unwrap(), + bincode::serialize(&stake_delegations_inner).unwrap(), + ); + + // Test: Assert that serializing the outer type then deserializing to the inner type + // produces the same values + { + let data = bincode::serialize(&stake_delegations_outer).unwrap(); + let deserialized_inner: StakeDelegationsInner = bincode::deserialize(&data).unwrap(); + assert_eq!(&deserialized_inner, stake_delegations_outer.deref()); + } + + // Test: Assert that serializing the inner type then deserializing to the outer type + // produces the same values + { + let data = bincode::serialize(&stake_delegations_inner).unwrap(); + let deserialized_outer: StakeDelegations = bincode::deserialize(&data).unwrap(); + assert_eq!(deserialized_outer.deref(), &stake_delegations_inner); + } + } } diff --git a/runtime/src/stake_history.rs b/runtime/src/stake_history.rs index fbcf7ab9b55e72..27c7674008daf5 100644 --- a/runtime/src/stake_history.rs +++ b/runtime/src/stake_history.rs @@ -38,6 +38,7 @@ mod tests { } } + /// Ensure that StakeHistory is indeed clone-on-write #[test] fn test_stake_history_is_cow() { let mut stake_history = StakeHistory::default(); @@ -81,4 +82,38 @@ mod tests { ); } } + + /// Ensure that StakeHistory serializes and deserializes between the inner and outer types + #[test] + fn test_stake_history_serde() { + let mut stake_history_outer = StakeHistory::default(); + let mut stake_history_inner = StakeHistoryInner::default(); + (2134..).take(11).for_each(|epoch| { + let entry = rand_stake_history_entry(); + stake_history_outer.add(epoch, entry.clone()); + stake_history_inner.add(epoch, entry); + }); + + // Test: Assert that serializing the outer and inner types produces the same data + assert_eq!( + bincode::serialize(&stake_history_outer).unwrap(), + bincode::serialize(&stake_history_inner).unwrap(), + ); + + // Test: Assert that serializing the outer type then deserializing to the inner type + // produces the same values + { + let data = bincode::serialize(&stake_history_outer).unwrap(); + let deserialized_inner: StakeHistoryInner = bincode::deserialize(&data).unwrap(); + assert_eq!(&deserialized_inner, stake_history_outer.deref()); + } + + // Test: Assert that serializing the inner type then deserializing to the outer type + // produces the same values + { + let data = bincode::serialize(&stake_history_inner).unwrap(); + let deserialized_outer: StakeHistory = bincode::deserialize(&data).unwrap(); + assert_eq!(deserialized_outer.deref(), &stake_history_inner); + } + } }