Skip to content

SIMD-0599: Remove inactive stakes from stake delegations - #15078

Merged
2501babe merged 1 commit into
anza-xyz:masterfrom
2501babe:20260903_hana_inerts
Sep 21, 2026
Merged

2501babe merged 1 commit into
anza-xyz:masterfrom
2501babe:20260903_hana_inerts

Conversation

@2501babe

@2501babe 2501babe commented Sep 4, 2026 •

Copy link
Copy Markdown
Member

Problem

we previously tried to impl this (#14394) like so:

  1. when switching epochs, after building the list of stake delegations, delete any prior-epoch inactives
  2. use this pruned set for points and rewards
  3. drop all inactives from the stakes cache after building the rewards set and before starting the first block of the new epoch
  4. when upserting the stakes cache, ignore/drop inactives if they were also inactive last epoch

points 1 and 3 imposed unacceptable cost, and the pr was reverted (#15029). 1 required hashing all 1.65m stake pubkeys to check against the inactive hashset and pushes epoch time almost to 300ms. 3 would have had to remove 250k entries from imbl on first epoch, which takes 800ms

Summary of Changes

instead we take a new approach:

  1. when switching epochs, while building the list of stake delegations, gather the list of inactive keys for later dropping
  2. use the full set for points and rewards, skipping inactives by recomputing activation status directly
  3. drop up to 10k inactives. we must bound this because dropping all 250k would lag the epoch by more than a second. this number was chosen by eyeballing about 4k deactivations in an epoch and observing about half are later withdrawn to zero. this mechanism is not intended for clearing backlog, merely for keeping up with normal deactivations
  4. when upserting the stakes cache, ignore/drop inactives if we are not in the rewards period. if we are in the rewards period, drop if they were also inactive last epoch. this is not an essential change but is strictly superior
  5. we also drop current-and-prior-epoch inactives when loading from snapshot. this means we do not need new runtime logic to amortize the cost of clearing the 250k inactive backlog across thousands of blocks. instead, the next time a validator updates to the latest agave version after the feature is active, they will dump their entire backlog. because the feature gate also prevents existing inactives from having any impact on consensus, it doesnt matter that validators do this ad hoc

bench:

case master f339058 this branch
10 votes / 1.5M / 1 inactive 84.7 ms 260.2 ms 86.1 ms
1000 votes / 1.5M / 1 inactive 109.5 ms 285.3 ms 110.5 ms
1000 votes / 1.5M / 250k inactive 121.0 ms 1.217 s 144.7 ms

we incur another 8ms from the 250k inactives until we clear them. the extra 16ms is the 10k prune. we could turn it down to 5k but i wanted to leave a healthy margin so a backlog doesnt reaccumulate. this only needs to clear inactives that arent touched again: if someone withdraws, even partially, in the next epoch, the stake will be dropped in CAS because of the new epoch rewards logic in my point 4

Testing

we keep ibhatt's test_inert_delegation_is_not_partitioned_for_rewards(). i improve my test_inert_delegation_is_removed_from_cache() to cover in-rewards and out-of-rewards, as well as the drop vs decline to insert cases. and add test_inert_delegation_is_pruned_on_load() to test the snapshot prune path

@2501babe 2501babe self-assigned this Sep 4, 2026
@codecov-commenter

codecov-commenter commented Sep 4, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.03846% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.3%. Comparing base (cda12b7) to head (9a3e53c).

Additional details and impacted files
@@           Coverage Diff            @@
##           master   #15078    +/-   ##
========================================
  Coverage    82.3%    82.3%            
========================================
  Files         929      929            
  Lines      369989   370330   +341     
========================================
+ Hits       304743   305048   +305     
- Misses      65246    65282    +36     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@2501babe
2501babe marked this pull request as ready for review September 8, 2026 15:35
@2501babe
2501babe requested a review from joncinque September 8, 2026 15:35
@2501babe

2501babe commented Sep 8, 2026

Copy link
Copy Markdown
Member Author

cc @ibhatt-jumptrading

@2501babe
2501babe marked this pull request as draft September 9, 2026 07:05
@2501babe
2501babe force-pushed the 20260903_hana_inerts branch 3 times, most recently from 262b2a0 to 3ee6925 Compare September 9, 2026 14:18
@2501babe
2501babe marked this pull request as ready for review September 9, 2026 14:20
@2501babe

2501babe commented Sep 9, 2026

Copy link
Copy Markdown
Member Author

had to do a nontrivial rebase because fixed point stake math removal landed and this touches all the same stuff, should be good now

@2501babe
2501babe force-pushed the 20260903_hana_inerts branch from 3ee6925 to 44a6aba Compare September 10, 2026 08:07

@joncinque joncinque left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This looks really great to me! Dropping from the cache on startup is definitely the right approach.

Just a few small questions on my side

Comment thread runtime/src/stakes.rs Resolved
Comment thread runtime/src/stakes.rs Resolved
Comment thread runtime/src/stakes.rs Outdated
Comment on lines +582 to +593
let delegation = stake_account.delegation();
is_delegation_inert(
delegation,
self.epoch,
&self.stake_history,
new_rate_activation_epoch,
) && is_delegation_inert(
delegation,
self.epoch.saturating_sub(1),
&self.stake_history,
new_rate_activation_epoch,
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Sorry if I'm missing something here, but why isn't this check the same as in check_and_store? If we're out of the rewards period, it should always be safe to drop the inactives, right?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

because bank epoch_reward_status is Inactive here. we could read the sysvar directly, or i think we could move this after recalculate_partitioned_rewards_if_active() but it would break if anyone reordered it

@joncinque joncinque Sep 14, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Again, sorry if I'm missing something, but recalculate_partitioned_rewards_if_active fetches the sysvar account to see if it's active, which implies that active field of the sysvar needs to be correct by the time we get into initialize_after_snapshot_restore. That means we can trust it here, and ordering within initialize_after_snapshot_restore shouldn't matter.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

sorry, i meant ordering would matter if we used the bank field, like check_and_store() does. 323ada9 this is using the raw sysvar

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

oh gotcha, the newest commit looks great to me!

@2501babe
2501babe force-pushed the 20260903_hana_inerts branch 2 times, most recently from d3ea736 to 323ada9 Compare September 16, 2026 12:34
joncinque
joncinque previously approved these changes Sep 16, 2026

@joncinque joncinque left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Looks great to me! Do you want to get any other reviewers before merging this since we had issues last time?

@bw-solana bw-solana left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LGTM. left a couple performance related comments, but they don't need to block

Comment thread runtime/src/stakes.rs
&& activation_status.effective == 0
&& activation_status.activating == 0
{
inert_stake_delegations.push(**stake_pubkey);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

if we have a ton of inert stake delegations, seems silly to push way more than 10k here.
could probably do a CAS to detect when we've hit the limit, but that might be worse than the allocations... maybe some lazy CAS updated and checked every 100 entries or something.

but also, that starts sounding a little over-engineered.. if we measure this taking a long time in the field, we can revisit

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yea i didnt want to overdo this since we expect to never have over 10k after the backlog is cleared. even if more than 10k get deactivated in an epoch, they will naturally be cleared when people withdraw in the next epoch, because of my change to do "drop if inactive if not in rewards period, or if inactive now and last epoch" whereas the the previous pr was only the latter rule. so this only needs to cover:

  • deactivations where the persion decided to leave all sol in the account
  • deactivate delinquent
  • deactivations from rent increase

Comment thread runtime/src/stakes.rs
/// Removes inactive delegations, used when restoring from snapshot. As in
/// `upsert_stake_delegation`, during the rewards period we keep stakes that
/// were active last epoch in case they are owed rewards.
fn remove_inert_stake_delegations(

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

it would be cool to combine this removal with the stakes iteration we already do as part of the refresh_delegated_stakes / calculate_delegated_stakes path

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

this is only part of snapshot loading so i didnt want to get fancy, the iteration is like 80ms

@2501babe
2501babe force-pushed the 20260903_hana_inerts branch 2 times, most recently from ae97949 to e9458bb Compare September 19, 2026 17:09
@2501babe
2501babe requested a review from joncinque September 20, 2026 06:39
@2501babe

Copy link
Copy Markdown
Member Author

@joncinque re-reqed with no changes, i have to rebase this for merge conflicts like daily

@bw-solana should i take your comments to mean you are a full second reviewer? or should i get someone else? we really want another reviewer on this because it was a weeks-long design-by-committee by me jon and ibhatt, so the first version came out less than perfect because it was hard to review by any of us with fresh eyes

---------

Co-authored-by: Ishan Bhatt <ibhatt@jumptrading.com>
Co-authored-by: Jon C <me@jonc.dev>
@2501babe
2501babe force-pushed the 20260903_hana_inerts branch from e9458bb to 9a3e53c Compare September 20, 2026 15:41
@bw-solana

Copy link
Copy Markdown

@bw-solana should i take your comments to mean you are a full second reviewer? or should i get someone else? we really want another reviewer on this because it was a weeks-long design-by-committee by me jon and ibhatt, so the first version came out less than perfect because it was hard to review by any of us with fresh eyes

yes, can count me as the 2nd reviewer. LGTM 👍

@2501babe
2501babe added this pull request to the merge queue Sep 21, 2026
Merged via the queue into anza-xyz:master with commit 3f76270 Sep 21, 2026
47 checks passed
@2501babe
2501babe deleted the 20260903_hana_inerts branch September 21, 2026 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants