Submitted by rileyholterhus, also found by MrPotatoMagic
The GaugeV3 contract distributes rewards based on the proportion of liquidity that each position had in range over each 1-week period. This is calculated by cachePeriodEarned() in the gauge, which calls positionPeriodSecondsInRange() on the RamsesV3Pool. A key part of this calculation is the periodCumulativesInside() function, which computes the total seconds per liquidity within a tick range for that period.
In periodCumulativesInside(), one sub-case occurs when the period is in the past, and the tick range was active at the end of that period:
Notice that this sub-case relies on $.periods[period].endSecondsPerLiquidityPeriodX128, which is meant to represent the total seconds per liquidity when the period ended. However, this value is actually more accurately described as the seconds per liquidity at the start of the next period, which can be seen in how _advancePeriod() and newPeriod() are implemented:
So, this means that if a period is skipped (meaning no activity happens in the pool during the period), the next time _advancePeriod() is called, the endSecondsPerLiquidityPeriodX128 for the last period will be set to just before the start of the period after the skipped period. This is effectively one week after the last period actually ended. This adds extra time to the sub-case mentioned above, which leads to inflated rewards for users.
If period p has gauge rewards and period p+1 has no pool activity, the reward calculation for period p will be inflated, allowing users to claim more tokens than they should.
The following test file can be added to test/v3/.
By running npx hardhat test --grep "Code4rena Contest", the following output can be seen:
This output shows that the attacker can claim double their intended rewards at the expense of other another user.
One initial idea for a fix might be to modify _advancePeriod() and newPeriod() to only extrapolate the seconds per liquidity up to the end of the period, rather than to the start of the next period:
However, the current behavior of endSecondsPerLiquidityPeriodX128 is actually important to maintain for the following logic in periodCumulativesInside():
So, it is instead recommended to introduce separate startSecondsPerLiquidityPeriodX128 and endSecondsPerLiquidityPeriodX128 variables  in the PeriodInfo struct, so that the code can correctly distinguish between the two different time points in the calculations.
gzeon (judge) commented:
keccakdog (Ramses) commented:
gzeon (judge) decreased severity to Low/Non-Critical
rileyholterhus (warden) commented:
keccakdog (Ramses) commented:
rileyholterhus (warden) commented:
gzeon (judge) commented:
rileyholterhus (warden) commented:
gzeon (judge) commented:
rileyholterhus (warden) commented:
gzeon (judge) increased severity to Medium and commented:
