Skip to content

Commit

Permalink
stake: Add BorshSerialize trait to structs (solana-labs#20784)
Browse files Browse the repository at this point in the history
  • Loading branch information
joncinque authored Oct 19, 2021
1 parent 96c6ba6 commit dc1b8dd
Showing 1 changed file with 74 additions and 3 deletions.
77 changes: 74 additions & 3 deletions sdk/program/src/stake/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use {
},
stake_history::{StakeHistory, StakeHistoryEntry},
},
borsh::{maybestd::io, BorshDeserialize, BorshSchema},
borsh::{maybestd::io, BorshDeserialize, BorshSchema, BorshSerialize},
std::collections::HashSet,
};

Expand Down Expand Up @@ -49,6 +49,24 @@ impl BorshDeserialize for StakeState {
}
}

impl BorshSerialize for StakeState {
fn serialize<W: io::Write>(&self, writer: &mut W) -> io::Result<()> {
match self {
StakeState::Uninitialized => writer.write_all(&0u32.to_le_bytes()),
StakeState::Initialized(meta) => {
writer.write_all(&1u32.to_le_bytes())?;
meta.serialize(writer)
}
StakeState::Stake(meta, stake) => {
writer.write_all(&2u32.to_le_bytes())?;
meta.serialize(writer)?;
stake.serialize(writer)
}
StakeState::RewardsPool => writer.write_all(&3u32.to_le_bytes()),
}
}
}

impl Default for StakeState {
fn default() -> Self {
StakeState::Uninitialized
Expand Down Expand Up @@ -112,6 +130,7 @@ pub enum StakeAuthorize {
AbiExample,
BorshDeserialize,
BorshSchema,
BorshSerialize,
)]
pub struct Lockup {
/// UnixTimestamp at which this stake will allow withdrawal, unless the
Expand Down Expand Up @@ -145,6 +164,7 @@ impl Lockup {
AbiExample,
BorshDeserialize,
BorshSchema,
BorshSerialize,
)]
pub struct Authorized {
pub staker: Pubkey,
Expand Down Expand Up @@ -223,6 +243,7 @@ impl Authorized {
AbiExample,
BorshDeserialize,
BorshSchema,
BorshSerialize,
)]
pub struct Meta {
pub rent_exempt_reserve: u64,
Expand Down Expand Up @@ -297,7 +318,16 @@ impl Meta {
}

#[derive(
Debug, Serialize, Deserialize, PartialEq, Clone, Copy, AbiExample, BorshDeserialize, BorshSchema,
Debug,
Serialize,
Deserialize,
PartialEq,
Clone,
Copy,
AbiExample,
BorshDeserialize,
BorshSchema,
BorshSerialize,
)]
pub struct Delegation {
/// to whom the stake is delegated
Expand Down Expand Up @@ -533,6 +563,7 @@ impl Delegation {
AbiExample,
BorshDeserialize,
BorshSchema,
BorshSerialize,
)]
pub struct Stake {
pub delegation: Delegation,
Expand Down Expand Up @@ -587,8 +618,14 @@ mod test {
assert_eq!(stake, deserialized);
}

fn check_borsh_serialization(stake: StakeState) {
let bincode_serialized = serialize(&stake).unwrap();
let borsh_serialized = StakeState::try_to_vec(&stake).unwrap();
assert_eq!(bincode_serialized, borsh_serialized);
}

#[test]
fn bincode_vs_borsh() {
fn bincode_vs_borsh_deserialization() {
check_borsh_deserialization(StakeState::Uninitialized);
check_borsh_deserialization(StakeState::RewardsPool);
check_borsh_deserialization(StakeState::Initialized(Meta {
Expand Down Expand Up @@ -621,6 +658,40 @@ mod test {
));
}

#[test]
fn bincode_vs_borsh_serialization() {
check_borsh_serialization(StakeState::Uninitialized);
check_borsh_serialization(StakeState::RewardsPool);
check_borsh_serialization(StakeState::Initialized(Meta {
rent_exempt_reserve: u64::MAX,
authorized: Authorized {
staker: Pubkey::new_unique(),
withdrawer: Pubkey::new_unique(),
},
lockup: Lockup::default(),
}));
check_borsh_serialization(StakeState::Stake(
Meta {
rent_exempt_reserve: 1,
authorized: Authorized {
staker: Pubkey::new_unique(),
withdrawer: Pubkey::new_unique(),
},
lockup: Lockup::default(),
},
Stake {
delegation: Delegation {
voter_pubkey: Pubkey::new_unique(),
stake: u64::MAX,
activation_epoch: Epoch::MAX,
deactivation_epoch: Epoch::MAX,
warmup_cooldown_rate: f64::MAX,
},
credits_observed: 1,
},
));
}

#[test]
fn borsh_deserialization_live_data() {
let data = [
Expand Down

0 comments on commit dc1b8dd

Please sign in to comment.