lidofinance / core

Lido DAO smart contracts
https://lido.fi
GNU General Public License v3.0
382 stars 194 forks source link

Improve precision for the `PositiveTokenRebaseLimiter` library #835

Open TheDZhon opened 6 months ago

TheDZhon commented 6 months ago

tl;dr

Change internal math reference precision from 1e9 to 1e27 to make the PositiveTokenRebaseLimiter library more robust against unusual input data

Context:

As a part of the Repovation initiative the PositiveTokenRebase library (contracts/0.8.9/lib/PositiveTokenRebaseLimiter.sol) has been covered with an extensive fuzzing test suite.

What:

Turns out the internal math precision with a reference 1 == 1e9 is insufficient to maintain wide (or even unconstrainted) data boundaries, see the relevant code in https://github.com/lidofinance/core/pull/88, the method is testFuzz_getSharesToBurnLimit. When the values are out of range, the allowed theoretical positive rebase start diverging from the calculated one due to the weak precision math.

It should be possible to maintain the accuracy for the following assumptions:

Possible fix:

Implement getSharesToBurnLimit with the internal 1e27 precision model.