Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Prevent withrawing Initialized stake account to rent-exempt reserve #17366

Merged

Conversation

CriesofCarrots
Copy link
Contributor

@CriesofCarrots CriesofCarrots commented May 21, 2021

Problem

It is possible to end up with a Delegated stake account with 0 delegated stake by withdrawing an Initialized account to exactly the rent-exempt reserve and then delegating. This is confusing to users (is it activating? can it be redelegated?), and defies our intention that every stake account have more lamports than the rent-exempt reserve.

Summary of Changes

Bump reserve for Initialized stake accounts by 1 lamport
Also flesh out testing of withdrawals from Initialized accounts

@CriesofCarrots
Copy link
Contributor Author

@t-nelson , I did examine all the places in the Stake program where we ensure resulting accounts have balances > rent-exempt-reserve, as we discussed. But I didn't see a great way to unify the logic. This seemed low churn and the most consistent with the withdraw handling for Delegated account.

@CriesofCarrots CriesofCarrots added the CI Pull Request is ready to enter CI label May 21, 2021
@solana-grimes solana-grimes removed the CI Pull Request is ready to enter CI label May 21, 2021
Copy link
Contributor

@t-nelson t-nelson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! lgtm

@CriesofCarrots CriesofCarrots added the automerge Merge this Pull Request automatically once CI passes label May 21, 2021
@codecov
Copy link

codecov bot commented May 21, 2021

Codecov Report

Merging #17366 (2fb588f) into master (a1a0d6f) will increase coverage by 0.0%.
The diff coverage is 98.0%.

@@           Coverage Diff           @@
##           master   #17366   +/-   ##
=======================================
  Coverage    82.7%    82.7%           
=======================================
  Files         424      424           
  Lines      118353   118402   +49     
=======================================
+ Hits        97906    97957   +51     
+ Misses      20447    20445    -2     

@mergify mergify bot merged commit 91f2b61 into solana-labs:master May 21, 2021
mergify bot pushed a commit that referenced this pull request May 21, 2021
(cherry picked from commit 91f2b61)

# Conflicts:
#	programs/stake/src/stake_instruction.rs
@jon-chuang
Copy link
Contributor

jon-chuang commented May 21, 2021

I think a similar problem is faced with splitting accounts.

To combat this - rather than go for the somewhat confusing reliance on credits_observed as a indirect proxy to check if balances are too small, one can ensure that split results in accounts with at least 1 SOL staked.

Having at least 1 SOL staked would combat stake fragmentation too. Not sure if this direction is desirable and should be applied uniformly or would cause churn in current deployment.

@CriesofCarrots
Copy link
Contributor Author

I think a similar problem is faced with splitting accounts.

@jon-chuang I do not think it is possible to generate a 0-stake account via StakeAccount::split(), but if you have a test or STR that proves me wrong, I would love to see it 🙏

@jon-chuang
Copy link
Contributor

I think I've probably made a mistake - the 1 in the code is 1 lamport, not 1 SOL - there are plenty of stakers presently w/ < 1 SOL.

Also you're probably right but I'll go and double check.

mergify bot added a commit that referenced this pull request May 21, 2021
…backport #17366) (#17370)

* Prevent withrawing Initialized stake account to zero stake (#17366)

(cherry picked from commit 91f2b61)

# Conflicts:
#	programs/stake/src/stake_instruction.rs

* Fix conflicts

Co-authored-by: Tyera Eulberg <teulberg@gmail.com>
Co-authored-by: Tyera Eulberg <tyera@solana.com>
@joncinque
Copy link
Contributor

Nice! I had been wondering about this for some time

@CriesofCarrots CriesofCarrots deleted the stake-prevent-withdraw-to-zero branch July 28, 2021 22:32
@brooksprumo brooksprumo mentioned this pull request Aug 23, 2021
@@ -1248,8 +1250,13 @@ impl<'a> StakeAccount for KeyedAccount<'a> {
StakeState::Initialized(meta) => {
meta.authorized
.check(&signers, StakeAuthorize::Withdrawer)?;
let reserve = if prevent_withdraw_to_zero {
checked_add(meta.rent_exempt_reserve, 1)? // stake accounts must have a balance > rent_exempt_reserve
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here

@github-actions
Copy link
Contributor

This PR has been automatically locked since there has not been any activity in past 14 days after it was merged.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 30, 2022
@mergify mergify bot removed the automerge Merge this Pull Request automatically once CI passes label Mar 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants