From ea5d6a9dbc79592abc501ed451088c41161910b7 Mon Sep 17 00:00:00 2001 From: alpo <62043214+AlpinYukseloglu@users.noreply.github.com> Date: Sun, 18 Jun 2023 16:39:11 -0700 Subject: [PATCH] extract invariant and range test changes from bugfix PR (#5561) --- x/concentrated-liquidity/invariant_test.go | 20 +++++++++++--------- x/concentrated-liquidity/range_test.go | 12 ++++++------ 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/x/concentrated-liquidity/invariant_test.go b/x/concentrated-liquidity/invariant_test.go index 6453401d0ec..b1c750ae806 100644 --- a/x/concentrated-liquidity/invariant_test.go +++ b/x/concentrated-liquidity/invariant_test.go @@ -87,11 +87,12 @@ func (s *KeeperTestSuite) assertTotalRewardsInvariant() { totalCollectedIncentives = totalCollectedIncentives.Add(collectedIncentives...) } - // We allow for an additive tolerance of 1 per position in the pool, since this is the maximum that can be truncated - // when collecting spread rewards. + // For global invariant checks, we simply ensure that any rounding error was in the pool's favor. + // This is to allow for cases where we slightly overround, which would otherwise fail here. + // TODO: create ErrTolerance type that allows for additive OR multiplicative tolerance to allow for + // tightening this check further. errTolerance := osmomath.ErrTolerance{ - AdditiveTolerance: sdk.NewDec(int64(len(allPositions))), - RoundingDir: osmomath.RoundDown, + RoundingDir: osmomath.RoundDown, } // Assert total collected spread rewards and incentives equal to expected @@ -144,15 +145,16 @@ func (s *KeeperTestSuite) assertWithdrawAllInvariant() { totalWithdrawn = totalWithdrawn.Add(withdrawn...) } - // We allow for an additive tolerance of 1 per position in the pool, since this is the maximum that can be truncated - // when collecting spread rewards. + // For global invariant checks, we simply ensure that any rounding error was in the pool's favor. + // This is to allow for cases where we slightly overround, which would otherwise fail here. + // TODO: create ErrTolerance type that allows for additive OR multiplicative tolerance to allow for + // tightening this check further. errTolerance := osmomath.ErrTolerance{ - AdditiveTolerance: sdk.NewDec(int64(len(allPositions))), - RoundingDir: osmomath.RoundDown, + RoundingDir: osmomath.RoundDown, } // Assert total withdrawn assets equal to expected - s.Require().True(errTolerance.EqualCoins(expectedTotalWithdrawn, totalWithdrawn)) + s.Require().True(errTolerance.EqualCoins(expectedTotalWithdrawn, totalWithdrawn), "expected withdrawn vs. actual: %s vs. %s", expectedTotalWithdrawn, totalWithdrawn) // Refetch total pool balances across all pools remainingPositions, finalTotalPoolAssets, remainingTotalSpreadRewards, remainingTotalIncentives := s.getAllPositionsAndPoolBalances(cachedCtx) diff --git a/x/concentrated-liquidity/range_test.go b/x/concentrated-liquidity/range_test.go index 4983760d907..e733c6265ce 100644 --- a/x/concentrated-liquidity/range_test.go +++ b/x/concentrated-liquidity/range_test.go @@ -47,7 +47,7 @@ type RangeTestParams struct { var ( DefaultRangeTestParams = RangeTestParams{ // Base amounts - baseNumPositions: 1000, + baseNumPositions: 10, baseAssets: sdk.NewCoins(sdk.NewCoin(ETH, sdk.NewInt(5000000000)), sdk.NewCoin(USDC, sdk.NewInt(5000000000))), baseTimeBetweenJoins: time.Hour, baseSwapAmount: sdk.NewInt(10000000), @@ -109,7 +109,8 @@ func (s *KeeperTestSuite) setupRangesAndAssertInvariants(pool types.Concentrated // Set up assets for new position curAssets := getRandomizedAssets(testParams.baseAssets, testParams.fuzzAssets) - s.FundAcc(curAddr, curAssets) + roundingError := sdk.NewCoins(sdk.NewCoin(pool.GetToken0(), sdk.OneInt()), sdk.NewCoin(pool.GetToken1(), sdk.OneInt())) + s.FundAcc(curAddr, curAssets.Add(roundingError...)) // Set up position curPositionId, actualAmt0, actualAmt1, curLiquidity, actualLowerTick, actualUpperTick, err := s.clk.CreatePosition(s.Ctx, pool.GetId(), curAddr, curAssets, sdk.ZeroInt(), sdk.ZeroInt(), ranges[curRange][0], ranges[curRange][1]) @@ -122,7 +123,6 @@ func (s *KeeperTestSuite) setupRangesAndAssertInvariants(pool types.Concentrated // Let time elapse after join if applicable timeElapsed := s.addRandomizedBlockTime(testParams.baseTimeBetweenJoins, testParams.fuzzTimeBetweenJoins) - s.assertGlobalInvariants() // Execute swap against pool if applicable swappedIn, swappedOut := s.executeRandomizedSwap(pool, swapAddresses, testParams.baseSwapAmount, testParams.fuzzSwapAmounts) @@ -149,7 +149,8 @@ func (s *KeeperTestSuite) setupRangesAndAssertInvariants(pool types.Concentrated // Ensure the pool balance is exactly equal to the assets added + amount swapped in - amount swapped out poolAssets := s.App.BankKeeper.GetAllBalances(s.Ctx, pool.GetAddress()) - s.Require().Equal(totalAssets, poolAssets) + poolSpreadRewards := s.App.BankKeeper.GetAllBalances(s.Ctx, pool.GetSpreadRewardsAddress()) + s.Require().Equal(totalAssets, poolAssets.Add(poolSpreadRewards...)) } // numPositionSlice prepares a slice tracking the number of positions to create on each range, fuzzing the number at each step if applicable. @@ -187,7 +188,7 @@ func (s *KeeperTestSuite) prepareNumPositionSlice(ranges [][]int64, baseNumPosit // TODO: Make swaps that target getting to a tick boundary exactly func (s *KeeperTestSuite) executeRandomizedSwap(pool types.ConcentratedPoolExtension, swapAddresses []sdk.AccAddress, baseSwapAmount sdk.Int, fuzzSwap bool) (sdk.Coin, sdk.Coin) { // Quietly skip if no swap assets or swap addresses provided - if baseSwapAmount.Equal(sdk.Int{}) || len(swapAddresses) == 0 { + if (baseSwapAmount == sdk.Int{}) || len(swapAddresses) == 0 { return sdk.Coin{}, sdk.Coin{} } @@ -250,7 +251,6 @@ func (s *KeeperTestSuite) addRandomizedBlockTime(baseTimeToAdd time.Duration, fu } s.AddBlockTime(timeToAdd) - } return baseTimeToAdd