Skip to content

Commit

Permalink
refactor: serde stakes (#2519)
Browse files Browse the repository at this point in the history
  • Loading branch information
jstarry authored Aug 9, 2024
1 parent adbc39c commit 82a8dcc
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 27 deletions.
2 changes: 1 addition & 1 deletion runtime/src/bank/serde_snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ mod tests {
#[cfg_attr(
feature = "frozen-abi",
derive(AbiExample),
frozen_abi(digest = "DnUdXXELygo14vA8d6QoXo5bkJAQbTWqWW5Qf9RXXWgZ")
frozen_abi(digest = "HRBDXrGrHMZU4cNebKHT7jEmhrgd3h1c2qUMMywrGPiq")
)]
#[derive(Serialize)]
pub struct BankAbiTestWrapper {
Expand Down
4 changes: 2 additions & 2 deletions runtime/src/epoch_stakes.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use {
crate::{
stake_account::StakeAccount,
stakes::{Stakes, StakesEnum},
stakes::{serde_stakes_to_delegation_format, Stakes, StakesEnum},
},
serde::{Deserialize, Deserializer, Serialize, Serializer},
solana_sdk::{clock::Epoch, pubkey::Pubkey, stake::state::Stake},
Expand All @@ -24,7 +24,7 @@ pub struct NodeVoteAccounts {
#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
#[cfg_attr(feature = "dev-context-only-utils", derive(PartialEq))]
pub struct EpochStakes {
#[serde(with = "crate::stakes::serde_stakes_enum_compat")]
#[serde(with = "serde_stakes_to_delegation_format")]
stakes: Arc<StakesEnum>,
total_stake: u64,
node_id_to_vote_accounts: Arc<NodeIdToVoteAccounts>,
Expand Down
4 changes: 2 additions & 2 deletions runtime/src/serde_snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use {
runtime_config::RuntimeConfig,
serde_snapshot::storage::SerializableAccountStorageEntry,
snapshot_utils::{SnapshotError, StorageAndNextAccountsFileId},
stakes::{serde_stakes_enum_compat, Stakes, StakesEnum},
stakes::{serde_stakes_to_delegation_format, Stakes, StakesEnum},
},
bincode::{self, config::Options, Error},
log::*,
Expand Down Expand Up @@ -237,7 +237,7 @@ struct SerializableVersionedBank {
rent_collector: RentCollector,
epoch_schedule: EpochSchedule,
inflation: Inflation,
#[serde(serialize_with = "serde_stakes_enum_compat::serialize")]
#[serde(serialize_with = "serde_stakes_to_delegation_format::serialize")]
stakes: StakesEnum,
unused_accounts: UnusedAccounts,
epoch_stakes: HashMap<Epoch, EpochStakes>,
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/stakes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use {
};

mod serde_stakes;
pub(crate) use serde_stakes::serde_stakes_enum_compat;
pub(crate) use serde_stakes::serde_stakes_to_delegation_format;

#[derive(Debug, Error)]
pub enum Error {
Expand Down
42 changes: 21 additions & 21 deletions runtime/src/stakes/serde_stakes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use {

// In order to maintain backward compatibility, the StakesEnum in EpochStakes
// and SerializableVersionedBank should be serialized as Stakes<Delegation>.
pub(crate) mod serde_stakes_enum_compat {
pub(crate) mod serde_stakes_to_delegation_format {
use {
super::*,
serde::{Deserialize, Deserializer, Serialize, Serializer},
Expand All @@ -23,9 +23,9 @@ pub(crate) mod serde_stakes_enum_compat {
{
match stakes {
StakesEnum::Delegations(stakes) => stakes.serialize(serializer),
StakesEnum::Stakes(stakes) => serialize_stakes_as_delegations(stakes, serializer),
StakesEnum::Stakes(stakes) => serialize_stakes_to_delegation_format(stakes, serializer),
StakesEnum::Accounts(stakes) => {
serialize_stake_accounts_as_delegations(stakes, serializer)
serialize_stake_accounts_to_delegation_format(stakes, serializer)
}
}
}
Expand All @@ -39,21 +39,21 @@ pub(crate) mod serde_stakes_enum_compat {
}
}

fn serialize_stakes_as_delegations<S: Serializer>(
fn serialize_stakes_to_delegation_format<S: Serializer>(
stakes: &Stakes<Stake>,
serializer: S,
) -> Result<S::Ok, S::Error> {
SerdeStakeVariantStakes::from(stakes.clone()).serialize(serializer)
SerdeStakesToDelegationFormat::from(stakes.clone()).serialize(serializer)
}

fn serialize_stake_accounts_as_delegations<S: Serializer>(
fn serialize_stake_accounts_to_delegation_format<S: Serializer>(
stakes: &Stakes<StakeAccount>,
serializer: S,
) -> Result<S::Ok, S::Error> {
SerdeStakeAccountVariantStakes::from(stakes.clone()).serialize(serializer)
SerdeStakeAccountsToDelegationFormat::from(stakes.clone()).serialize(serializer)
}

impl From<Stakes<Stake>> for SerdeStakeVariantStakes {
impl From<Stakes<Stake>> for SerdeStakesToDelegationFormat {
fn from(stakes: Stakes<Stake>) -> Self {
let Stakes {
vote_accounts,
Expand All @@ -65,15 +65,15 @@ impl From<Stakes<Stake>> for SerdeStakeVariantStakes {

Self {
vote_accounts,
stake_delegations: SerdeStakeMapWrapper(stake_delegations),
stake_delegations: SerdeStakeMapToDelegationFormat(stake_delegations),
unused,
epoch,
stake_history,
}
}
}

impl From<Stakes<StakeAccount>> for SerdeStakeAccountVariantStakes {
impl From<Stakes<StakeAccount>> for SerdeStakeAccountsToDelegationFormat {
fn from(stakes: Stakes<StakeAccount>) -> Self {
let Stakes {
vote_accounts,
Expand All @@ -85,7 +85,7 @@ impl From<Stakes<StakeAccount>> for SerdeStakeAccountVariantStakes {

Self {
vote_accounts,
stake_delegations: SerdeStakeAccountMapWrapper(stake_delegations),
stake_delegations: SerdeStakeAccountMapToDelegationFormat(stake_delegations),
unused,
epoch,
stake_history,
Expand All @@ -95,27 +95,27 @@ impl From<Stakes<StakeAccount>> for SerdeStakeAccountVariantStakes {

#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
#[derive(Serialize)]
struct SerdeStakeVariantStakes {
struct SerdeStakesToDelegationFormat {
vote_accounts: VoteAccounts,
stake_delegations: SerdeStakeMapWrapper,
stake_delegations: SerdeStakeMapToDelegationFormat,
unused: u64,
epoch: Epoch,
stake_history: StakeHistory,
}

#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
#[derive(Serialize)]
struct SerdeStakeAccountVariantStakes {
struct SerdeStakeAccountsToDelegationFormat {
vote_accounts: VoteAccounts,
stake_delegations: SerdeStakeAccountMapWrapper,
stake_delegations: SerdeStakeAccountMapToDelegationFormat,
unused: u64,
epoch: Epoch,
stake_history: StakeHistory,
}

#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
struct SerdeStakeMapWrapper(ImHashMap<Pubkey, Stake>);
impl Serialize for SerdeStakeMapWrapper {
struct SerdeStakeMapToDelegationFormat(ImHashMap<Pubkey, Stake>);
impl Serialize for SerdeStakeMapToDelegationFormat {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
Expand All @@ -129,8 +129,8 @@ impl Serialize for SerdeStakeMapWrapper {
}

#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
struct SerdeStakeAccountMapWrapper(ImHashMap<Pubkey, StakeAccount>);
impl Serialize for SerdeStakeAccountMapWrapper {
struct SerdeStakeAccountMapToDelegationFormat(ImHashMap<Pubkey, StakeAccount>);
impl Serialize for SerdeStakeAccountMapToDelegationFormat {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
Expand All @@ -151,11 +151,11 @@ mod tests {
};

#[test]
fn test_serde_stakes_enum_compat() {
fn test_serde_stakes_to_delegation_format() {
#[derive(Debug, PartialEq, Deserialize, Serialize)]
struct Dummy {
head: String,
#[serde(with = "serde_stakes_enum_compat")]
#[serde(with = "serde_stakes_to_delegation_format")]
stakes: Arc<StakesEnum>,
tail: String,
}
Expand Down

0 comments on commit 82a8dcc

Please sign in to comment.