Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
A short summary of the changes.
Context
#1305
Math
Vault is healthy if:
LS / TVS <= M / B
or
LS * B <= TVS * M
Rebalance does the following
LS - X
rebalance(roundUp(X))
and
TVS - Y
Normally,
X == Y
BUT
because rebalance rounds UP, in the worst case scenario
Y = X + 1
thus, we can derive the post-rebalance condition:
(LS - X) * B <= (TVS - X - 1) * M
OR (using distributive property)
LS * B - X * B <= TVS * M - X * M - M
add
X * M
to both sidesLS * B - X * B + X * M <= TVS * M - M
subtract
LS * B
- X * B + X * M <= TVS * M - M - LS * B
or factor X
X (M - B) <= TVS * M - M - LS * B
because
T = M - B
X * (-T) <= TVS * M - M - LS * B
divide both sides by -T and flip the sign
X >= (TVS * M - M - LS * B) / (-T)
move minus to numerator
X >= -(TVS * M - M - LS * B) / T
factor -1
X >= (-TVS * M + M + L * B) / T
rearrange and get the final inequality
X >= (L * B - TVS * M + M) / T
AND after all of that we need to use ceiling division because the final inequality also includes division
X >= (L * B - TVS * M + M) + (T - 1) / T