Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}