Skip to content

Commit

Permalink
generalized stake_flags
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoranYi committed Jun 14, 2023
1 parent 08ce3fb commit 329c5aa
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 195 deletions.
4 changes: 2 additions & 2 deletions account-decoder/src/parse_stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl From<Delegation> for UiDelegation {

#[cfg(test)]
mod test {
use {super::*, bincode::serialize, solana_sdk::stake::state::DeactivationFlag};
use {super::*, bincode::serialize, solana_sdk::stake::state::StakeFlags};

#[test]
fn test_parse_stake() {
Expand Down Expand Up @@ -194,7 +194,7 @@ mod test {
credits_observed: 10,
};

let stake_state = StakeState::Stake(meta, stake, DeactivationFlag::Empty);
let stake_state = StakeState::Stake(meta, stake, StakeFlags::empty());
let stake_data = serialize(&stake_state).unwrap();
assert_eq!(
parse_stake(&stake_data).unwrap(),
Expand Down
77 changes: 35 additions & 42 deletions programs/stake/src/stake_instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,9 +526,7 @@ mod tests {
set_lockup_checked, AuthorizeCheckedWithSeedArgs, AuthorizeWithSeedArgs,
LockupArgs, StakeError,
},
state::{
Authorized, DeactivationFlag, Lockup, StakeActivationStatus, StakeAuthorize,
},
state::{Authorized, Lockup, StakeActivationStatus, StakeAuthorize, StakeFlags},
MINIMUM_DELINQUENT_EPOCHS_FOR_DEACTIVATION,
},
stake_history::{StakeHistory, StakeHistoryEntry},
Expand Down Expand Up @@ -672,7 +670,7 @@ mod tests {
},
..Stake::default()
},
DeactivationFlag::Empty,
StakeFlags::empty(),
)
}

Expand Down Expand Up @@ -4808,11 +4806,7 @@ mod tests {

for split_to_state in &[
StakeState::Initialized(Meta::default()),
StakeState::Stake(
Meta::default(),
Stake::default(),
DeactivationFlag::default(),
),
StakeState::Stake(Meta::default(), Stake::default(), StakeFlags::default()),
StakeState::RewardsPool,
] {
let split_to_account = AccountSharedData::new_data_with_space(
Expand Down Expand Up @@ -4978,7 +4972,7 @@ mod tests {
);

// verify no stake leakage in the case of a stake
if let StakeState::Stake(meta, stake, deactivation_flag) = state {
if let StakeState::Stake(meta, stake, stake_flags) = state {
assert_eq!(
accounts[1].state(),
Ok(StakeState::Stake(
Expand All @@ -4990,7 +4984,7 @@ mod tests {
},
..*stake
},
*deactivation_flag,
*stake_flags,
))
);
assert_eq!(accounts[0].lamports(), *minimum_balance,);
Expand Down Expand Up @@ -5081,7 +5075,7 @@ mod tests {
stake_lamports + initial_balance,
);

if let StakeState::Stake(meta, stake, deactivation_flag) = state {
if let StakeState::Stake(meta, stake, stake_flags) = state {
let expected_stake =
stake_lamports / 2 - (rent_exempt_reserve.saturating_sub(initial_balance));
assert_eq!(
Expand All @@ -5095,7 +5089,7 @@ mod tests {
},
..stake
},
deactivation_flag
stake_flags
)),
accounts[1].state(),
);
Expand All @@ -5115,7 +5109,7 @@ mod tests {
},
..stake
},
deactivation_flag,
stake_flags,
)),
accounts[0].state(),
);
Expand Down Expand Up @@ -5206,7 +5200,7 @@ mod tests {
stake_lamports + initial_balance
);

if let StakeState::Stake(meta, stake, deactivation_flag) = state {
if let StakeState::Stake(meta, stake, stake_flags) = state {
let expected_split_meta = Meta {
authorized: Authorized::auto(&stake_address),
rent_exempt_reserve: split_rent_exempt_reserve,
Expand All @@ -5225,7 +5219,7 @@ mod tests {
},
..stake
},
deactivation_flag,
stake_flags,
)),
accounts[1].state()
);
Expand All @@ -5245,7 +5239,7 @@ mod tests {
},
..stake
},
deactivation_flag,
stake_flags,
)),
accounts[0].state()
);
Expand Down Expand Up @@ -5401,7 +5395,7 @@ mod tests {
assert_eq!(Ok(*state), accounts[1].state());
assert_eq!(Ok(StakeState::Uninitialized), accounts[0].state());
}
StakeState::Stake(meta, stake, deactivation_flag) => {
StakeState::Stake(meta, stake, stake_flags) => {
assert_eq!(
Ok(StakeState::Stake(
*meta,
Expand All @@ -5412,7 +5406,7 @@ mod tests {
},
..*stake
},
*deactivation_flag
*stake_flags
)),
accounts[1].state()
);
Expand Down Expand Up @@ -5497,7 +5491,7 @@ mod tests {
stake_lamports + initial_balance
);

if let StakeState::Stake(meta, stake, deactivation_flag) = state {
if let StakeState::Stake(meta, stake, stake_flags) = state {
assert_eq!(
Ok(StakeState::Stake(
meta,
Expand All @@ -5508,7 +5502,7 @@ mod tests {
},
..stake
},
deactivation_flag,
stake_flags,
)),
accounts[1].state()
);
Expand Down Expand Up @@ -5620,7 +5614,7 @@ mod tests {
);
assert_eq!(Ok(StakeState::Uninitialized), accounts[0].state());
}
StakeState::Stake(_meta, stake, deactivation_flag) => {
StakeState::Stake(_meta, stake, stake_flags) => {
// Expected stake should reflect original stake amount so that extra lamports
// from the rent_exempt_reserve inequality do not magically activate
let expected_stake = stake_lamports - source_rent_exempt_reserve;
Expand All @@ -5635,7 +5629,7 @@ mod tests {
},
..*stake
},
*deactivation_flag,
*stake_flags,
)),
accounts[1].state()
);
Expand Down Expand Up @@ -5750,7 +5744,7 @@ mod tests {
StakeState::Initialized(meta) => {
assert_eq!(accounts[0].state(), Ok(StakeState::Initialized(*meta)),);
}
StakeState::Stake(meta, stake, deactivation_flag) => {
StakeState::Stake(meta, stake, stake_flags) => {
let expected_stake = stake.delegation.stake
+ merge_from_state
.stake()
Expand All @@ -5770,7 +5764,7 @@ mod tests {
},
..*stake
},
*deactivation_flag,
*stake_flags,
)),
);
}
Expand Down Expand Up @@ -5804,7 +5798,7 @@ mod tests {
};
let stake_account = AccountSharedData::new_data_with_space(
stake_lamports,
&StakeState::Stake(meta, stake, DeactivationFlag::Empty),
&StakeState::Stake(meta, stake, StakeFlags::empty()),
StakeState::size_of(),
&id(),
)
Expand Down Expand Up @@ -6131,7 +6125,7 @@ mod tests {
};
let stake_account = AccountSharedData::new_data_with_space(
stake_lamports,
&StakeState::Stake(meta, stake, DeactivationFlag::Empty),
&StakeState::Stake(meta, stake, StakeFlags::empty()),
StakeState::size_of(),
&id(),
)
Expand All @@ -6147,7 +6141,7 @@ mod tests {
};
let merge_from_account = AccountSharedData::new_data_with_space(
merge_from_lamports,
&StakeState::Stake(meta, merge_from_stake, DeactivationFlag::Empty),
&StakeState::Stake(meta, merge_from_stake, StakeFlags::empty()),
StakeState::size_of(),
&id(),
)
Expand Down Expand Up @@ -6303,7 +6297,7 @@ mod tests {
};
transaction_accounts[0]
.1
.set_state(&StakeState::Stake(meta, stake, DeactivationFlag::Empty))
.set_state(&StakeState::Stake(meta, stake, StakeFlags::empty()))
.unwrap();
}
if clock.epoch == merge_from_deactivation_epoch {
Expand All @@ -6320,7 +6314,7 @@ mod tests {
.set_state(&StakeState::Stake(
meta,
merge_from_stake,
DeactivationFlag::Empty,
StakeFlags::empty(),
))
.unwrap();
}
Expand Down Expand Up @@ -6532,7 +6526,7 @@ mod tests {
1, /* activation_epoch */
&stake_config::Config::default(),
),
DeactivationFlag::Empty,
StakeFlags::empty(),
);

let stake_account = AccountSharedData::new_data_with_space(
Expand Down Expand Up @@ -6739,7 +6733,7 @@ mod tests {
1, /* activation_epoch */
&stake_config::Config::default(),
),
DeactivationFlag::Empty,
StakeFlags::empty(),
))
.unwrap();

Expand Down Expand Up @@ -6833,7 +6827,7 @@ mod tests {
activation_epoch,
&stake_config::Config::default(),
),
DeactivationFlag::Empty,
StakeFlags::empty(),
);

if let Some(expected_stake_activation_status) = expected_stake_activation_status {
Expand Down Expand Up @@ -7142,14 +7136,14 @@ mod tests {
//
let mut stake_account_over_allocated =
prepare_stake_account(0 /*activation_epoch:*/, None);
if let StakeState::Stake(mut meta, stake, deactivation_flag) = stake_account_over_allocated
if let StakeState::Stake(mut meta, stake, stake_flags) = stake_account_over_allocated
.borrow_mut()
.deserialize_data()
.unwrap()
{
meta.rent_exempt_reserve += 42;
stake_account_over_allocated
.set_state(&StakeState::Stake(meta, stake, deactivation_flag))
.set_state(&StakeState::Stake(meta, stake, stake_flags))
.unwrap();
}
stake_account_over_allocated
Expand Down Expand Up @@ -7343,7 +7337,7 @@ mod tests {
);

deactivating_stake_account
.set_state(&StakeState::Stake(meta, stake, DeactivationFlag::Empty))
.set_state(&StakeState::Stake(meta, stake, StakeFlags::empty()))
.unwrap();
}
let _ = process_instruction_redelegate(
Expand All @@ -7362,19 +7356,18 @@ mod tests {
// (less than `minimum_delegation + rent_exempt_reserve`)
//
let mut stake_account_too_few_lamports = stake_account.clone();
if let StakeState::Stake(meta, mut stake, deactivation_flag) =
stake_account_too_few_lamports
.borrow_mut()
.deserialize_data()
.unwrap()
if let StakeState::Stake(meta, mut stake, stake_flags) = stake_account_too_few_lamports
.borrow_mut()
.deserialize_data()
.unwrap()
{
stake.delegation.stake -= 1;
assert_eq!(
stake.delegation.stake,
minimum_delegation + rent_exempt_reserve - 1
);
stake_account_too_few_lamports
.set_state(&StakeState::Stake(meta, stake, deactivation_flag))
.set_state(&StakeState::Stake(meta, stake, stake_flags))
.unwrap();
} else {
panic!("Invalid stake_account");
Expand Down
Loading

0 comments on commit 329c5aa

Please sign in to comment.