Skip to content

Commit

Permalink
refactor(osmoutils): use Dec for additive tolerance instead of Int (#…
Browse files Browse the repository at this point in the history
…3711)

* refactor(osmoutils): use Dec for additive tolerance instead of Int

* changelog
  • Loading branch information
p0mvn authored Dec 14, 2022
1 parent 1bbc41a commit 5ab7ebf
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* [#3611](https://github.com/osmosis-labs/osmosis/pull/3611),[#3647](https://github.com/osmosis-labs/osmosis/pull/3647) Introduce osmocli, to automate thousands of lines of CLI boilerplate
* [#3634](https://github.com/osmosis-labs/osmosis/pull/3634) (Makefile) Ensure correct golang version in make build and make install. (Thank you @jhernandezb )
* [#3711](https://github.com/osmosis-labs/osmosis/pull/3711) Use Dec instead of Int for additive `ErrTolerace` in `osmoutils`.

## v13.0.0

Expand Down
8 changes: 4 additions & 4 deletions osmoutils/binary_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
// RoundingDir = RoundUnconstrained.
// Note that if AdditiveTolerance == 0, then this is equivalent to a standard compare.
type ErrTolerance struct {
AdditiveTolerance sdk.Int
AdditiveTolerance sdk.Dec
MultiplicativeTolerance sdk.Dec
RoundingDir osmomath.RoundingDirection
}
Expand All @@ -33,7 +33,7 @@ type ErrTolerance struct {
// returns 1 if not, and expected > actual.
// returns -1 if not, and expected < actual
func (e ErrTolerance) Compare(expected sdk.Int, actual sdk.Int) int {
diff := expected.Sub(actual).Abs()
diff := expected.ToDec().Sub(actual.ToDec()).Abs()

comparisonSign := 0
if expected.GT(actual) {
Expand Down Expand Up @@ -71,7 +71,7 @@ func (e ErrTolerance) Compare(expected sdk.Int, actual sdk.Int) int {
}
// Check multiplicative tolerance equations
if !e.MultiplicativeTolerance.IsNil() && !e.MultiplicativeTolerance.IsZero() {
errTerm := diff.ToDec().Quo(sdk.MinInt(expected.Abs(), actual.Abs()).ToDec())
errTerm := diff.Quo(sdk.MinInt(expected.Abs(), actual.Abs()).ToDec())
if errTerm.GT(e.MultiplicativeTolerance) {
return comparisonSign
}
Expand Down Expand Up @@ -117,7 +117,7 @@ func (e ErrTolerance) CompareBigDec(expected osmomath.BigDec, actual osmomath.Bi
}
}

if diff.GT(osmomath.BigDecFromSDKDec(e.AdditiveTolerance.ToDec())) {
if diff.GT(osmomath.BigDecFromSDKDec(e.AdditiveTolerance)) {
return comparisonSign
}
}
Expand Down
30 changes: 15 additions & 15 deletions osmoutils/binary_search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

var (
withinOne = ErrTolerance{AdditiveTolerance: sdk.OneInt()}
withinOne = ErrTolerance{AdditiveTolerance: sdk.OneDec()}
withinFactor8 = ErrTolerance{MultiplicativeTolerance: sdk.NewDec(8)}
zero = osmomath.ZeroDec()
)
Expand All @@ -29,10 +29,10 @@ func TestBinarySearch(t *testing.T) {
output := sdk.Int(result)
return output, nil
}
noErrTolerance := ErrTolerance{AdditiveTolerance: sdk.ZeroInt()}
testErrToleranceAdditive := ErrTolerance{AdditiveTolerance: sdk.NewInt(1 << 20)}
testErrToleranceMultiplicative := ErrTolerance{AdditiveTolerance: sdk.ZeroInt(), MultiplicativeTolerance: sdk.NewDec(10)}
testErrToleranceBoth := ErrTolerance{AdditiveTolerance: sdk.NewInt(1 << 20), MultiplicativeTolerance: sdk.NewDec(1 << 3)}
noErrTolerance := ErrTolerance{AdditiveTolerance: sdk.ZeroDec()}
testErrToleranceAdditive := ErrTolerance{AdditiveTolerance: sdk.NewDec(1 << 20)}
testErrToleranceMultiplicative := ErrTolerance{AdditiveTolerance: sdk.ZeroDec(), MultiplicativeTolerance: sdk.NewDec(10)}
testErrToleranceBoth := ErrTolerance{AdditiveTolerance: sdk.NewDec(1 << 20), MultiplicativeTolerance: sdk.NewDec(1 << 3)}
tests := map[string]struct {
f func(sdk.Int) (sdk.Int, error)
lowerbound sdk.Int
Expand Down Expand Up @@ -153,9 +153,9 @@ var fnMap = map[string]searchFn{"line": lineF, "cubic": cubicF, "neg_cubic": neg
// This function tests that any value in a given range can be reached within expected num iterations.
func TestIterationDepthRandValue(t *testing.T) {
tests := map[string]binarySearchTestCase{}
exactEqual := ErrTolerance{AdditiveTolerance: sdk.ZeroInt()}
withinOne := ErrTolerance{AdditiveTolerance: sdk.OneInt()}
within32 := ErrTolerance{AdditiveTolerance: sdk.OneInt().MulRaw(32)}
exactEqual := ErrTolerance{AdditiveTolerance: sdk.ZeroDec()}
withinOne := ErrTolerance{AdditiveTolerance: sdk.OneDec()}
within32 := ErrTolerance{AdditiveTolerance: sdk.OneDec().Mul(sdk.NewDec(32))}

createRandInput := func(fnName string, lowerbound, upperbound int64,
errTolerance ErrTolerance, maxNumIters int, errToleranceName string) {
Expand Down Expand Up @@ -227,8 +227,8 @@ func runBinarySearchTestCases(t *testing.T, tests map[string]binarySearchTestCas
}

func TestBinarySearchBigDec(t *testing.T) {
testErrToleranceAdditive := ErrTolerance{AdditiveTolerance: sdk.NewInt(1 << 30)}
errToleranceBoth := ErrTolerance{AdditiveTolerance: sdk.NewInt(1 << 30), MultiplicativeTolerance: sdk.NewDec(1 << 3)}
testErrToleranceAdditive := ErrTolerance{AdditiveTolerance: sdk.NewDec(1 << 30)}
errToleranceBoth := ErrTolerance{AdditiveTolerance: sdk.NewDec(1 << 30), MultiplicativeTolerance: sdk.NewDec(1 << 3)}

twoTo50 := osmomath.NewBigDec(1 << 50)
twoTo25PlusOne := osmomath.NewBigDec(1 + (1 << 25))
Expand Down Expand Up @@ -279,7 +279,7 @@ func TestBinarySearchBigDec(t *testing.T) {
}

func TestBinarySearchRoundingBehavior(t *testing.T) {
withinTwoTo30 := ErrTolerance{AdditiveTolerance: sdk.NewInt(1 << 30)}
withinTwoTo30 := ErrTolerance{AdditiveTolerance: sdk.NewDec(1 << 30)}

twoTo50 := osmomath.NewBigDec(1 << 50)
// twoTo25PlusOne := osmomath.NewBigDec(1 + (1 << 25))
Expand Down Expand Up @@ -318,10 +318,10 @@ func TestBinarySearchRoundingBehavior(t *testing.T) {
}

func TestErrTolerance_Compare(t *testing.T) {
ZeroErrTolerance := ErrTolerance{AdditiveTolerance: sdk.ZeroInt(), MultiplicativeTolerance: sdk.Dec{}}
NonZeroErrAdditive := ErrTolerance{AdditiveTolerance: sdk.NewInt(10), MultiplicativeTolerance: sdk.Dec{}}
NonZeroErrMultiplicative := ErrTolerance{AdditiveTolerance: sdk.Int{}, MultiplicativeTolerance: sdk.NewDec(10)}
NonZeroErrBoth := ErrTolerance{AdditiveTolerance: sdk.NewInt(1), MultiplicativeTolerance: sdk.NewDec(10)}
ZeroErrTolerance := ErrTolerance{AdditiveTolerance: sdk.ZeroDec(), MultiplicativeTolerance: sdk.Dec{}}
NonZeroErrAdditive := ErrTolerance{AdditiveTolerance: sdk.NewDec(10), MultiplicativeTolerance: sdk.Dec{}}
NonZeroErrMultiplicative := ErrTolerance{AdditiveTolerance: sdk.Dec{}, MultiplicativeTolerance: sdk.NewDec(10)}
NonZeroErrBoth := ErrTolerance{AdditiveTolerance: sdk.NewDec(1), MultiplicativeTolerance: sdk.NewDec(10)}
tests := []struct {
name string
tol ErrTolerance
Expand Down
2 changes: 1 addition & 1 deletion x/gamm/pool-models/balancer/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ func (suite *BalancerTestSuite) TestBalancerCalculateAmountOutAndIn_InverseRelat
suite.Require().NotNil(pool)

errTolerance := osmoutils.ErrTolerance{
AdditiveTolerance: sdk.OneInt(), MultiplicativeTolerance: sdk.Dec{}}
AdditiveTolerance: sdk.OneDec(), MultiplicativeTolerance: sdk.Dec{}}
sut := func() {
test_helpers.TestCalculateAmountOutAndIn_InverseRelationship(suite.T(), ctx, pool, poolAssetIn.Token.Denom, poolAssetOut.Token.Denom, tc.initialCalcOut, swapFeeDec, errTolerance)
}
Expand Down
2 changes: 1 addition & 1 deletion x/gamm/pool-models/internal/cfmm_common/lp.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func BinarySearchSingleAssetJoin(
}

// We accept an additive tolerance of 1 LP share error and round down
errTolerance := osmoutils.ErrTolerance{AdditiveTolerance: sdk.OneInt(), MultiplicativeTolerance: sdk.Dec{}, RoundingDir: osmomath.RoundDown}
errTolerance := osmoutils.ErrTolerance{AdditiveTolerance: sdk.OneDec(), MultiplicativeTolerance: sdk.Dec{}, RoundingDir: osmomath.RoundDown}

numLPShares, err = osmoutils.BinarySearch(
estimateCoinOutGivenShares,
Expand Down
2 changes: 1 addition & 1 deletion x/gamm/pool-models/stableswap/amm.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ func solveCFMMBinarySearchMulti(xReserve, yReserve, wSumSquares, yIn osmomath.Bi
maxIterations := 256

// we use a geometric error tolerance that guarantees approximately 10^-12 precision on outputs
errTolerance := osmoutils.ErrTolerance{AdditiveTolerance: sdk.Int{}, MultiplicativeTolerance: sdk.NewDecWithPrec(1, 12)}
errTolerance := osmoutils.ErrTolerance{AdditiveTolerance: sdk.Dec{}, MultiplicativeTolerance: sdk.NewDecWithPrec(1, 12)}

// if yIn is positive, we want to under-estimate the amount of xOut.
// This means, we want x_out to be rounded down, as x_out = x_init - x_final, for x_init > x_final.
Expand Down
2 changes: 1 addition & 1 deletion x/gamm/pool-models/stableswap/amm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ func (suite *StableSwapTestSuite) Test_StableSwap_CalculateAmountOutAndIn_Invers
pool := createTestPool(suite.T(), tc.poolLiquidity, swapFeeDec, exitFeeDec, tc.scalingFactors)
suite.Require().NotNil(pool)
errTolerance := osmoutils.ErrTolerance{
AdditiveTolerance: sdk.Int{}, MultiplicativeTolerance: sdk.NewDecWithPrec(1, 12)}
AdditiveTolerance: sdk.Dec{}, MultiplicativeTolerance: sdk.NewDecWithPrec(1, 12)}
test_helpers.TestCalculateAmountOutAndIn_InverseRelationship(suite.T(), ctx, pool, tc.denomIn, tc.denomOut, tc.initialCalcOut, swapFeeDec, errTolerance)
})
}
Expand Down

0 comments on commit 5ab7ebf

Please sign in to comment.