# Delegators can lose their rewards when a delegator has removed a delegatee and claims all of his rewards before delegating again to a previous removed delegatee.

In Morphl2 Distribute, claimAll shrinks the delegatees AddressSet mid-loop and re-delegation resets unclaimedStart, permanently stranding unclaimed staking rewards.

- Payout: Audit contest
- Severity: medium
- Program: Morphl2
- Found by: 0xStalin
- Disclosed: 2024-08-28
- Write-up quality: 7/10
- Original: https://github.com/sherlock-audit/2024-08-morphl2-judging/issues/157
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-08-morphl2%3AM-7
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:sherlock sev:medium tech:solidity technique:chained technique:source-review vuln:accounting-error vuln:business-logic

**Target:** Distribute.sol staking reward distribution contract
**Root cause:** claimAll iterates over a live AddressSet that _claim mutates by removing undelegated delegatees, so the loop exits early; separately notifyDelegation overwrites unclaimedStart on re-delegation without claiming pending rewards.
**Steps:**
1. Delegator delegates to stakers A, B, C and accrues rewards across epochs.
2. Delegator undelegates A and C (effective next epoch).
3. Delegator calls `l2Staking.claimReward(address(0), 0)`; _claim removes A from the AddressSet, shrinking length so C is never reached.
4. Delegator calls `l2Staking.claimUndelegation()`.
5. Delegator re-delegates to C via `l2Staking.delegateStake(thirdStaker, 1 ether)`; notifyDelegation sets `unclaimedStart[C] = effectiveEpoch`, discarding pre-existing unclaimed rewards.
6. queryAllUnclaimed total drops; stranded rewards remain in Distribute.
**How found:** Code review of claimAll's for-loop bound against the AddressSet mutation inside _claim, plus noticing notifyDelegation unconditionally resets unclaimedStart without checking pending rewards; confirmed with a Foundry PoC.
**Exploitation:** No attacker profit; a legitimate delegator following normal claim/undelegate/re-delegate flow loses unclaimable rewards that stay stuck in the Distribute contract.
**Insight:** When a loop iterates a mutable EnumerableSet whose elements are removed inside the body, the index outruns the shrinking length and skips elements—snapshot the set to a memory array first. Also check state-reset paths (unclaimedStart) for unflushed pending balances.

## Proof of concept (copied exactly from the original report)

**Standalone claimAll shrink-loop reproduction test**

```
contract BugWhenClaimingAllRewards is Test {
  function test_claimingAllRewardsReproducingBug() public {
    SimpleDelegations delegations = new SimpleDelegations();

    delegations.addDelegatee(address(1));
    delegations.addDelegatee(address(2));
    delegations.addDelegatee(address(3));

    delegations.undelegate(address(1));
    delegations.undelegate(address(3));

    //@audit => Should claim 30 of rewards! There are 3 delegatees and each delegatee gives 10 of rewards
    uint256 rewards = delegations.claimAll();
    console2.log("Total rewards: ", rewards);

    console2.log("delegatees list after claiming");
    address[] memory delegatees = delegations.getDelegatees();
    for(uint i = 0; i < delegatees.length; i++) {
      console2.log("delegatee: ", delegatees[i]);
    }

    //@audit => If all delegatees would have been claimed, then, delegatees 1 & 3 would have been deleted from the `unclaimed.delegatees` AddressSet, but because the delegatee 3 was never reached, (and the rewards of this delegatee were not claimed), this delegatee is still part of the list of delegatees for the delegator with unclaimed rewards!
  }
}
```

