# When `stakerCountInTree` Increases, Some Users May Receive Less Interest

In LayerEdgeStaking, when stakerCountInTree increases, a tier-3 user who should be promoted to tier 2 is not, causing them to receive less interest.

- Payout: Audit contest
- Severity: high
- Program: Layeredge
- Found by: Cybrid
- Disclosed: 2025-05-21
- Write-up quality: 7/10
- Original: https://github.com/sherlock-audit/2025-05-layeredge-judging/issues/230
- Library page: https://bugbountylibrary.com/r/sherlock%3A2025-05-layeredge%3AH-2
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:sherlock sev:high tech:solidity technique:source-review vuln:accounting-error vuln:business-logic

**Target:** LayerEdgeStaking.sol _checkBoundariesAndRecord
**Root cause:** When stakerCountInTree increases and tier-2 count stays the same, the code only calls _findAndRecordTierChange for the old boundary (old_t1+old_t2) instead of the new boundary (new_t1+new_t2), so the user at the new boundary is not promoted from tier 3 to tier 2.
**Steps:**
1. Deploy LayerEdgeStaking with 20 users each holding 10,000 tokens.
2. Have users[1..15] each stake MIN_STAKE, growing stakerCountInTree from 14 to 15.
3. Tier distribution shifts from (2,4,8) to (3,4,8); the 7th-ranked user should move from tier 3 to tier 2.
4. Call staking.getUserAPY(users[7]) and observe it reports 35% APY.
5. Warp 365 days, call staking.claimInterest() as users[7], and measure actual received interest: only 20% APY.
**How found:** The auditor traced the boundary-update logic in _checkBoundariesAndRecord and noticed that in the non-removal branch where new_t2 == old_t2, only the old boundary rank is updated, leaving the new boundary rank unprocessed.
**Exploitation:** No attacker action; the flaw is a logic error that silently underpays interest to a user who should have been promoted to a higher tier when the staker count grows.
**Insight:** When a function updates tier boundaries after a count change, verify that both the old and new boundary ranks are processed in every branch, especially the 'count unchanged' branch where the boundary still shifts.

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

**Foundry test proving tier-3 user underpaid interest**

```
function test_LayerEdgeStaking_checkBoundariesAndRecord() public {
        vm.startPrank(admin);
        uint256 userAmount = 10_000 * 1e18;
        address[20] memory users;
        for (uint256 i = 0; i < 20; i++) {
            users[i] = makeAddr(string(abi.encodePacked("users", toString(i)))); 
            token.transfer(users[i], userAmount);
        } 
        vm.stopPrank();
        for (uint256 i = 1; i <= 15; i++) {
            vm.startPrank(users[i]);
            token.approve(address(staking), MIN_STAKE);
            staking.stake(MIN_STAKE);
            vm.stopPrank();
        }
        address user = users[7];
        vm.warp(block.timestamp + 365 days);
        uint256 userAPY = staking.getUserAPY(user);
        vm.startPrank(user);
        uint256 beforeUserAmount = token.balanceOf(user);
        staking.claimInterest();
        uint256 afterUserAmount = token.balanceOf(user);
        console2.log("Procotol User   APY : %d", userAPY);
        console2.log("Actual Received APY : %d", 100e18 * (afterUserAmount - beforeUserAmount) / MIN_STAKE);
        vm.stopPrank();
    }
```

