Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit c7a1e98

Browse files
authored
stake-pool: Check transient stake state (#3987)
1 parent c6c8398 commit c7a1e98

File tree

4 files changed

+51
-7
lines changed

4 files changed

+51
-7
lines changed

stake-pool/program/src/processor.rs

+48-4
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,42 @@ fn stake_is_inactive_without_history(stake: &stake::state::Stake, epoch: Epoch)
222222
&& stake.delegation.deactivation_epoch == epoch)
223223
}
224224

225+
/// Roughly checks if a stake account is deactivating or inactive
226+
fn check_if_stake_is_deactivating_or_inactive(
227+
account_info: &AccountInfo,
228+
vote_account_address: &Pubkey,
229+
) -> Result<(), ProgramError> {
230+
let (_, stake) = get_stake_state(account_info)?;
231+
if stake.delegation.deactivation_epoch == Epoch::MAX {
232+
msg!(
233+
"Existing stake {} delegated to {} is activating or active",
234+
account_info.key,
235+
vote_account_address
236+
);
237+
Err(StakePoolError::WrongStakeState.into())
238+
} else {
239+
Ok(())
240+
}
241+
}
242+
243+
/// Roughly checks if a stake account is activating or active
244+
fn check_if_stake_is_activating_or_active(
245+
account_info: &AccountInfo,
246+
vote_account_address: &Pubkey,
247+
) -> Result<(), ProgramError> {
248+
let (_, stake) = get_stake_state(account_info)?;
249+
if stake.delegation.deactivation_epoch != Epoch::MAX {
250+
msg!(
251+
"Existing stake {} delegated to {} is deactivating or inactive",
252+
account_info.key,
253+
vote_account_address
254+
);
255+
Err(StakePoolError::WrongStakeState.into())
256+
} else {
257+
Ok(())
258+
}
259+
}
260+
225261
/// Check that the stake state is correct: usable by the pool and delegated to
226262
/// the expected validator
227263
fn check_stake_state(
@@ -1337,8 +1373,10 @@ impl Processor {
13371373
);
13381374
return Err(ProgramError::InvalidSeeds);
13391375
}
1340-
// Let the runtime check to see if the merge is valid, so there's no
1341-
// explicit check here that the transient stake is decreasing
1376+
check_if_stake_is_deactivating_or_inactive(
1377+
transient_stake_account_info,
1378+
&vote_account_address,
1379+
)?;
13421380
}
13431381

13441382
let stake_minimum_delegation = stake::tools::get_minimum_delegation()?;
@@ -1586,8 +1624,10 @@ impl Processor {
15861624
);
15871625
return Err(ProgramError::InvalidSeeds);
15881626
}
1589-
// Let the runtime check to see if the merge is valid, so there's no
1590-
// explicit check here that the transient stake is increasing
1627+
check_if_stake_is_activating_or_active(
1628+
transient_stake_account_info,
1629+
vote_account_address,
1630+
)?;
15911631
}
15921632

15931633
check_validator_stake_account(
@@ -2037,6 +2077,10 @@ impl Processor {
20372077
destination_transient_stake_seed,
20382078
&stake_pool.lockup,
20392079
)?;
2080+
check_if_stake_is_activating_or_active(
2081+
destination_transient_stake_account_info,
2082+
vote_account_address,
2083+
)?;
20402084
Self::stake_merge(
20412085
stake_pool_info.key,
20422086
ephemeral_stake_account_info.clone(),

stake-pool/program/tests/decrease.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ async fn fail_additional_with_increasing() {
586586
error,
587587
TransactionError::InstructionError(
588588
0,
589-
InstructionError::Custom(stake::instruction::StakeError::MergeTransientStake as u32)
589+
InstructionError::Custom(StakePoolError::WrongStakeState as u32)
590590
)
591591
);
592592
}

stake-pool/program/tests/increase.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ async fn fail_additional_with_decreasing() {
585585
error,
586586
TransactionError::InstructionError(
587587
0,
588-
InstructionError::Custom(StakeError::MergeTransientStake as u32)
588+
InstructionError::Custom(StakePoolError::WrongStakeState as u32)
589589
)
590590
);
591591
}

stake-pool/program/tests/redelegate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ async fn fail_with_decreasing_stake() {
657657
error,
658658
TransactionError::InstructionError(
659659
0,
660-
InstructionError::Custom(StakeError::MergeTransientStake as u32)
660+
InstructionError::Custom(StakePoolError::WrongStakeState as u32)
661661
)
662662
);
663663
}

0 commit comments

Comments
 (0)