Skip to content

Add delegation scheduled requests summary#3680

Merged
manuelmauro merged 17 commits intomasterfrom
manuel/delegation-scheduled-requests-summary-map
Feb 25, 2026
Merged

Add delegation scheduled requests summary#3680
manuelmauro merged 17 commits intomasterfrom
manuel/delegation-scheduled-requests-summary-map

Conversation

@manuelmauro
Copy link
Contributor

What does it do?

Introduces a new DelegationScheduledRequestsSummaryMap (StorageDoubleMap<collator, delegator, DelegationAction>) that maintains a pre-aggregated summary of pending actions per (collator, delegator)
pair:

  • Revoke(bond) — stored when a revocation is pending.
  • Decrease(total) — aggregated sum of all pending decrease amounts.

This summary map is kept in sync on every schedule, cancel, and execute operation in delegation_requests.rs. Round-transition functions (get_rewardable_delegators, delegation_request_revoke_exists)
now perform O(1) lookups per delegator into the summary map instead of iterating the full requests list.

…stsSummaryMap

Replace the boolean PendingRevocations flag (StorageDoubleMap storing ())
with DelegationScheduledRequestsSummaryMap storing DelegationAction<BalanceOf<T>>.
This tracks both pending revocations (Revoke(bond)) and the aggregated
total of pending decreases (Decrease(total)) per (collator, delegator) pair.

get_rewardable_delegators now reads only fixed-size summary values instead
of variable-length request queues, while the execution queue storage
(DelegationScheduledRequests) is preserved for timing and ordering.
@manuelmauro manuelmauro marked this pull request as draft February 24, 2026 13:18
@coderabbitai
Copy link

coderabbitai bot commented Feb 24, 2026

📝 Walkthrough

Walkthrough

Introduces DelegationScheduledRequestsSummaryMap storage item to track aggregated pending delegation actions (Revoke/Decrease) per (collator, delegator) pair. Updates delegation request operations and reward calculation logic to populate and read from this summary map instead of performing per-delegator aggregation at calculation time.

Changes

Cohort / File(s) Summary
Core delegation request logic
pallets/parachain-staking/src/delegation_requests.rs
Implements new DelegationScheduledRequestsSummaryMap storage item with insert/mutate/remove operations synchronized across delegation_schedule_revoke, delegation_schedule_bond_decrease, delegation_cancel_request, and delegation_execute_scheduled_request paths. Updates delegation_request_revoke_exists to check summary map instead of direct scheduled requests inspection.
Reward calculation refactoring
pallets/parachain-staking/src/lib.rs
Replaces in-memory aggregation logic in get_rewardable_delegators with direct lookups into DelegationScheduledRequestsSummaryMap. Updates round transition cleanup to clear summary map prefixes alongside existing scheduled requests removal.
Benchmark setup
pallets/parachain-staking/src/benchmarks.rs
Populates DelegationScheduledRequestsSummaryMap entries with Revoke actions during benchmark setup. Removes an uncounted_stake assertion and exposes summary map in public exports.
Test updates
pallets/parachain-staking/src/tests.rs
Adds DelegationScheduledRequestsSummaryMap import and setup. Renames test function and inserts Revoke entries into summary map during test state initialization to verify reduced reward behavior.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Add delegation scheduled requests summary' directly and accurately reflects the main change: introducing a new DelegationScheduledRequestsSummaryMap storage item.
Description check ✅ Passed The description clearly explains the purpose, implementation, and benefits of the new DelegationScheduledRequestsSummaryMap, directly relating to all changes in the changeset.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch manuel/delegation-scheduled-requests-summary-map

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@manuelmauro manuelmauro self-assigned this Feb 24, 2026
@manuelmauro manuelmauro added B7-runtimenoteworthy Changes should be noted in any runtime-upgrade release notes D5-nicetohaveaudit⚠️ PR contains trivial changes to logic that should be properly reviewed. not-breaking Does not need to be mentioned in breaking changes labels Feb 24, 2026
@manuelmauro manuelmauro changed the title Manuel/delegation scheduled requests summary map Delegation scheduled requests summary map Feb 24, 2026
@manuelmauro manuelmauro changed the title Delegation scheduled requests summary map Add delegation scheduled requests summary map Feb 24, 2026
@manuelmauro manuelmauro changed the title Add delegation scheduled requests summary map Add delegation scheduled requests summary Feb 24, 2026
@manuelmauro manuelmauro marked this pull request as ready for review February 24, 2026 15:40
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (2)
pallets/parachain-staking/src/tests.rs (1)

7397-7401: Consider adding a Decrease-case summary entry in the negative test for delegation_request_revoke_exists.
This would explicitly validate that the new summary map returns false when the stored action is Decrease.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@pallets/parachain-staking/src/tests.rs` around lines 7397 - 7401, In the
negative test delegation_request_revoke_exists, add an explicit
DelegationScheduledRequestsSummaryMap::<Test>::insert(...) entry using
DelegationAction::Decrease (e.g., same keys used for the existing Revoke insert)
so the test also asserts that the summary map check for a revoke returns false
when the stored action is Decrease; update the assertions to ensure the
revoke-existence check remains false for that Decrease entry.
pallets/parachain-staking/src/delegation_requests.rs (1)

324-345: Consider a self-heal fallback if the summary entry is missing or not a Decrease.
Right now this branch is a no-op in that case, which can leave the summary map desynced from scheduled_requests if the map was not correctly backfilled. A lightweight fallback is to recompute the remaining decrease total from scheduled_requests when entry is absent or non-decrease.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@pallets/parachain-staking/src/delegation_requests.rs` around lines 324 - 345,
The current mutate_exists closure on DelegationScheduledRequestsSummaryMap
silently no-ops if the summary entry is missing or not a Decrease; add a
self-heal fallback that recomputes the remaining decrease total from
scheduled_requests and updates the summary entry accordingly. Specifically,
inside the closure for
DelegationScheduledRequestsSummaryMap::mutate_exists(&collator, &delegator,
|entry| { ... }), detect when entry.is_none() or entry is Some(...) but not
DelegationAction::Decrease, then iterate the scheduled_requests storage for
(collator, delegator) (the same scheduled_requests map used elsewhere in this
module) to sum all pending Decrease amounts, subtract the current requested
amount (the amount variable) using saturating arithmetic, and then set *entry to
None if the remaining total is zero or to
Some(DelegationAction::Decrease(remaining)) otherwise; keep the existing
behavior when entry is already a Decrease by applying the same saturating_sub
logic.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@pallets/parachain-staking/src/delegation_requests.rs`:
- Around line 324-345: The current mutate_exists closure on
DelegationScheduledRequestsSummaryMap silently no-ops if the summary entry is
missing or not a Decrease; add a self-heal fallback that recomputes the
remaining decrease total from scheduled_requests and updates the summary entry
accordingly. Specifically, inside the closure for
DelegationScheduledRequestsSummaryMap::mutate_exists(&collator, &delegator,
|entry| { ... }), detect when entry.is_none() or entry is Some(...) but not
DelegationAction::Decrease, then iterate the scheduled_requests storage for
(collator, delegator) (the same scheduled_requests map used elsewhere in this
module) to sum all pending Decrease amounts, subtract the current requested
amount (the amount variable) using saturating arithmetic, and then set *entry to
None if the remaining total is zero or to
Some(DelegationAction::Decrease(remaining)) otherwise; keep the existing
behavior when entry is already a Decrease by applying the same saturating_sub
logic.

In `@pallets/parachain-staking/src/tests.rs`:
- Around line 7397-7401: In the negative test delegation_request_revoke_exists,
add an explicit DelegationScheduledRequestsSummaryMap::<Test>::insert(...) entry
using DelegationAction::Decrease (e.g., same keys used for the existing Revoke
insert) so the test also asserts that the summary map check for a revoke returns
false when the stored action is Decrease; update the assertions to ensure the
revoke-existence check remains false for that Decrease entry.

ℹ️ Review info

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 05ce4c8 and a5f1dd7.

📒 Files selected for processing (4)
  • pallets/parachain-staking/src/benchmarks.rs
  • pallets/parachain-staking/src/delegation_requests.rs
  • pallets/parachain-staking/src/lib.rs
  • pallets/parachain-staking/src/tests.rs

@github-actions
Copy link
Contributor

Coverage Report

@@                                 Coverage Diff                                 @@
##           master   manuel/delegation-scheduled-requests-summary-map     +/-   ##
===================================================================================
  Coverage   77.10%                                             77.10%   0.00%     
  Files         389                                                389             
+ Lines       76943                                              76972     +29     
===================================================================================
+ Hits        59323                                              59349     +26     
+ Misses      17620                                              17623      +3     
Files Changed Coverage
/pallets/parachain-staking/src/delegation_requests.rs 87.13% (+0.36%) 🔼
/pallets/parachain-staking/src/lib.rs 97.03% (+0.04%) 🔼

Coverage generated Tue Feb 24 15:59:41 UTC 2026

@manuelmauro manuelmauro merged commit 30efcc1 into master Feb 25, 2026
71 of 74 checks passed
@manuelmauro manuelmauro deleted the manuel/delegation-scheduled-requests-summary-map branch February 25, 2026 13:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

B7-runtimenoteworthy Changes should be noted in any runtime-upgrade release notes D5-nicetohaveaudit⚠️ PR contains trivial changes to logic that should be properly reviewed. not-breaking Does not need to be mentioned in breaking changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants