Skip to content

Commit

Permalink
Ensure that StakeDelegations and StakeHistory serde (#21640) (#21653)
Browse files Browse the repository at this point in the history
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 da4015a)

Co-authored-by: Brooks Prumo <[email protected]>
  • Loading branch information
mergify[bot] and brooksprumo authored Dec 7, 2021
1 parent 8a7106b commit 46935c0
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
42 changes: 42 additions & 0 deletions runtime/src/stake_delegations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type StakeDelegationsInner = HashMap<Pubkey, Delegation>;
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();
Expand Down Expand Up @@ -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);
}
}
}
35 changes: 35 additions & 0 deletions runtime/src/stake_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 46935c0

Please sign in to comment.