Add more comments in Stakes::store for its subtlety (#14065)

* Add more comments in Stakes::store for its subtlety

* more comment tweak
This commit is contained in:
Ryo Onodera 2020-12-11 18:13:36 +09:00 committed by GitHub
parent 09bd412b13
commit 7078a6ac61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 0 deletions

View File

@ -123,7 +123,13 @@ impl Stakes {
fix_stake_deactivate: bool, fix_stake_deactivate: bool,
) -> Option<ArcVoteAccount> { ) -> Option<ArcVoteAccount> {
if solana_vote_program::check_id(&account.owner) { if solana_vote_program::check_id(&account.owner) {
// unconditionally remove existing at first; there is no dependent calculated state for
// votes, not like stakes (stake codepath maintains calculated stake value grouped by
// delegated vote pubkey)
let old = self.vote_accounts.remove(pubkey); let old = self.vote_accounts.remove(pubkey);
// when account is removed (lamports == 0), don't readd so that given
// `pubkey` can be used for any owner in the future, while not
// affecting Stakes.
if account.lamports != 0 { if account.lamports != 0 {
let stake = old.as_ref().map_or_else( let stake = old.as_ref().map_or_else(
|| { || {
@ -162,6 +168,9 @@ impl Stakes {
fix_stake_deactivate, fix_stake_deactivate,
) )
} else { } else {
// when account is removed (lamports == 0), this special `else` clause ensures
// resetting cached stake value below, even if the account happens to be
// still staked for some (odd) reason
0 0
}, },
) )
@ -182,12 +191,19 @@ impl Stakes {
} }
if account.lamports == 0 { if account.lamports == 0 {
// when account is removed (lamports == 0), remove it from Stakes as well
// so that given `pubkey` can be used for any owner in the future, while not
// affecting Stakes.
self.stake_delegations.remove(pubkey); self.stake_delegations.remove(pubkey);
} else if let Some(delegation) = delegation { } else if let Some(delegation) = delegation {
self.stake_delegations.insert(*pubkey, delegation); self.stake_delegations.insert(*pubkey, delegation);
} }
None None
} else { } else {
// there is no need to remove possibly existing Stakes cache entries with given
// `pubkey` because this isn't possible, first of all.
// Runtime always enforces an intermediary write of account.lamports == 0,
// when not-System111-owned account.owner is swapped.
None None
} }
} }