Submitted by hansfriese
The important states including long0Balance, long1Balance, long1FeeGrowth, long1ProtocolFees are wrongly calculated and it breaks the pools invariant.
The protocol provides a rebalancing functionality and the main logic is implemented in the library Pool.sol.
If param.isLong0ToLong1 is true and the transaction is TimeswapV2PoolRebalance.GivenLong1, the protocol calculates the long1AmountAdjustFees first and the actual long0Amount, longFees and the final long1Balance is decided accordingly.
The problem is it is using the wrong parameter pool.long0Balance while it is supposed to use pool.long1Balance in the line L679.
This leads to wrong state calculation in the following logic. (especially L685 is setting the long1Balance to zero).
Furthermore, the protocol is designed as a permission-less one and anyone can call TimeswapV2Pool.rebalance().
An attacker can abuse this to break the pools invariant and take profit leveraging that.
Fix the L679 as below.
vhawk19 (Timeswap) confirmed and resolved:
