-
Notifications
You must be signed in to change notification settings - Fork 607
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[CL] Changes from audit results(1/2) #5568
Changes from all commits
e6f552d
c223e07
440bea8
2474c54
3400a02
1ed3bb2
d56b619
7044f88
5d0c6e8
3775150
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -972,7 +972,7 @@ func (suite *AccumTestSuite) TestRemoveFromPosition() { | |
suite.Require().Equal(tc.startingUnclaimedRewards.Add(tc.expAccumDelta.MulDec(tc.startingNumShares)...), newPosition.UnclaimedRewardsTotal) | ||
|
||
// Ensure address's position properly reflects new number of shares | ||
if (tc.startingNumShares.Sub(tc.removedShares)).Equal(sdk.ZeroDec()) { | ||
if (tc.startingNumShares.Sub(tc.removedShares)).IsZero() { | ||
suite.Require().Equal(emptyDec, newPosition.NumShares) | ||
} else { | ||
suite.Require().Equal(tc.startingNumShares.Sub(tc.removedShares), newPosition.NumShares) | ||
|
@@ -1163,7 +1163,7 @@ func (suite *AccumTestSuite) TestGetPositionSize() { | |
// Get position size from valid address (or from nonexistant if address does not exist) | ||
positionSize, err := curAccum.GetPositionSize(positionName) | ||
|
||
if tc.changedShares.GT(sdk.ZeroDec()) { | ||
if tc.changedShares.IsPositive() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto for GT(sdk.ZeroDec) or LT(sdk.ZeroDec), using IsPositive or IsNegative methods avoid creating unnecessasry sdk.Dec object |
||
accumPackage.InitOrUpdatePosition(curAccum, curAccum.GetValue(), positionName, tc.numShares.Add(tc.changedShares), sdk.NewDecCoins(), nil) | ||
} | ||
|
||
|
@@ -1613,14 +1613,14 @@ func (suite *AccumTestSuite) TestGetTotalShares() { | |
|
||
// Half the time, we add to the new position | ||
addAmt := baseAmt.Mul(addShares) | ||
if addAmt.GT(sdk.ZeroDec()) { | ||
if addAmt.IsPositive() { | ||
err = curAccum.AddToPosition(curAddr, addAmt) | ||
suite.Require().NoError(err) | ||
} | ||
|
||
// Half the time, we remove one share from the position | ||
amtToRemove := sdk.OneDec().Mul(removeShares) | ||
if amtToRemove.GT(sdk.ZeroDec()) { | ||
if amtToRemove.IsPositive() { | ||
err = curAccum.RemoveFromPosition(curAddr, amtToRemove) | ||
suite.Require().NoError(err) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -403,27 +403,24 @@ func (k Keeper) updateGivenPoolUptimeAccumulatorsToNow(ctx sdk.Context, pool typ | |
|
||
// We optimistically assume that all liquidity on the active tick qualifies and handle | ||
// uptime-related checks in forfeiting logic. | ||
|
||
// If there is no share to be incentivized for the current uptime accumulator, we leave it unchanged | ||
qualifyingLiquidity := pool.GetLiquidity().Add(qualifyingBalancerShares) | ||
if !qualifyingLiquidity.LT(sdk.OneDec()) { | ||
for uptimeIndex := range uptimeAccums { | ||
// Get relevant uptime-level values | ||
curUptimeDuration := types.SupportedUptimes[uptimeIndex] | ||
incentivesToAddToCurAccum, updatedPoolRecords, err := calcAccruedIncentivesForAccum(ctx, curUptimeDuration, qualifyingLiquidity, timeElapsedSec, poolIncentiveRecords) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for uptimeIndex := range uptimeAccums { | ||
// Get relevant uptime-level values | ||
curUptimeDuration := types.SupportedUptimes[uptimeIndex] | ||
// Emit incentives to current uptime accumulator | ||
uptimeAccums[uptimeIndex].AddToAccumulator(incentivesToAddToCurAccum) | ||
|
||
// If there is no share to be incentivized for the current uptime accumulator, we leave it unchanged | ||
if qualifyingLiquidity.LT(sdk.OneDec()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change mainly is from here, right now, we are entering loop checking if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This LGTM |
||
continue | ||
// Update pool records (stored in state after loop) | ||
poolIncentiveRecords = updatedPoolRecords | ||
} | ||
|
||
incentivesToAddToCurAccum, updatedPoolRecords, err := calcAccruedIncentivesForAccum(ctx, curUptimeDuration, qualifyingLiquidity, timeElapsedSec, poolIncentiveRecords) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Emit incentives to current uptime accumulator | ||
uptimeAccums[uptimeIndex].AddToAccumulator(incentivesToAddToCurAccum) | ||
|
||
// Update pool records (stored in state after loop) | ||
poolIncentiveRecords = updatedPoolRecords | ||
} | ||
|
||
// Update pool incentive records and LastLiquidityUpdate time in state to reflect emitted incentives | ||
|
@@ -538,7 +535,7 @@ func (k Keeper) setIncentiveRecord(ctx sdk.Context, incentiveRecord types.Incent | |
// In all other cases, we update the record in state | ||
if store.Has(key) && incentiveRecordBody.RemainingCoin.IsZero() { | ||
store.Delete(key) | ||
} else if incentiveRecordBody.RemainingCoin.Amount.GT(sdk.ZeroDec()) { | ||
} else if incentiveRecordBody.RemainingCoin.Amount.IsPositive() { | ||
osmoutils.MustSet(store, key, &incentiveRecordBody) | ||
} | ||
|
||
|
@@ -972,9 +969,11 @@ func (k Keeper) collectIncentives(ctx sdk.Context, sender sdk.AccAddress, positi | |
return sdk.Coins{}, sdk.Coins{}, err | ||
} | ||
|
||
err = k.ensurePositionOwner(ctx, sender, position.PoolId, positionId) | ||
if err != nil { | ||
return sdk.Coins{}, sdk.Coins{}, err | ||
if sender.String() != position.Address { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Directly checking this instead of using |
||
return sdk.Coins{}, sdk.Coins{}, types.NotPositionOwnerError{ | ||
PositionId: positionId, | ||
Address: sender.String(), | ||
} | ||
} | ||
|
||
// Claim all incentives for the position. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -663,9 +663,6 @@ func (s *KeeperTestSuite) TestWithdrawPosition() { | |
s.Require().ErrorAs(err, &types.PositionIdNotFoundError{PositionId: config.positionId}) | ||
s.Require().Equal(clmodel.Position{}, position) | ||
|
||
err = concentratedLiquidityKeeper.EnsurePositionOwner(s.Ctx, owner, config.poolId, config.positionId) | ||
s.Require().Error(err, types.NotPositionOwnerError{PositionId: config.positionId, Address: owner.String()}) | ||
Comment on lines
-666
to
-667
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ensurePositionOwner method is now deleted, nor do we need this check in this test case |
||
|
||
// Since the positionLiquidity is deleted, retrieving it should return an error. | ||
positionLiquidity, err := s.App.ConcentratedLiquidityKeeper.GetPositionLiquidity(s.Ctx, config.positionId) | ||
s.Require().Error(err) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,22 +99,6 @@ func (k Keeper) HasAnyPositionForPool(ctx sdk.Context, poolId uint64) (bool, err | |
return osmoutils.HasAnyAtPrefix(store, poolPositionKey, parse) | ||
} | ||
|
||
func (k Keeper) ensurePositionOwner(ctx sdk.Context, sender sdk.AccAddress, poolId uint64, positionId uint64) error { | ||
isOwner := k.isPositionOwner(ctx, sender, poolId, positionId) | ||
if !isOwner { | ||
return types.NotPositionOwnerError{ | ||
PositionId: positionId, | ||
Address: sender.String(), | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// isPositionOwner returns true if the given positionId is owned by the given sender inside the given pool. | ||
func (k Keeper) isPositionOwner(ctx sdk.Context, sender sdk.AccAddress, poolId uint64, positionId uint64) bool { | ||
return ctx.KVStore(k.storeKey).Has(types.KeyAddressPoolIdPositionId(sender, poolId, positionId)) | ||
} | ||
|
||
Comment on lines
-102
to
-117
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not used any more, thus deletion |
||
// GetAllPositionsForPoolId gets all the position for a specific poolId and store prefix. | ||
func (k Keeper) GetAllPositionIdsForPoolId(ctx sdk.Context, prefix []byte, poolId uint64) ([]uint64, error) { | ||
store := ctx.KVStore(k.storeKey) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -313,7 +313,7 @@ func (s *KeeperTestSuite) TestInitOrUpdatePosition() { | |
if test.positionExists { | ||
// Track how much the current uptime accum has grown by | ||
actualUptimeAccumDelta[uptimeIndex] = newUptimeAccumValues[uptimeIndex].Sub(initUptimeAccumValues[uptimeIndex]) | ||
if timeElapsedSec.GT(sdk.ZeroDec()) { | ||
if timeElapsedSec.IsPositive() { | ||
expectedGrowthCurAccum, _, err := cl.CalcAccruedIncentivesForAccum(s.Ctx, uptime, test.param.liquidityDelta, timeElapsedSec, expectedIncentiveRecords) | ||
s.Require().NoError(err) | ||
expectedUptimeAccumValueGrowth[uptimeIndex] = expectedGrowthCurAccum | ||
|
@@ -410,103 +410,6 @@ type positionOwnershipTest struct { | |
poolId uint64 | ||
} | ||
|
||
func (s *KeeperTestSuite) runIsPositionOwnerTest(test positionOwnershipTest) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method that was being tested is now deleted thus test is removed as well |
||
s.SetupTest() | ||
p := s.PrepareConcentratedPool() | ||
for i, owner := range test.setupPositions { | ||
err := s.App.ConcentratedLiquidityKeeper.InitOrUpdatePosition(s.Ctx, p.GetId(), owner, DefaultLowerTick, DefaultUpperTick, DefaultLiquidityAmt, DefaultJoinTime, uint64(i)) | ||
s.Require().NoError(err) | ||
} | ||
err := s.App.ConcentratedLiquidityKeeper.EnsurePositionOwner(s.Ctx, test.queryPositionOwner, test.poolId, test.queryPositionId) | ||
if test.expPass { | ||
s.Require().NoError(err) | ||
} else { | ||
s.Require().Error(err) | ||
} | ||
|
||
} | ||
|
||
func (s *KeeperTestSuite) TestIsPositionOwnerMultiPosition() { | ||
tenAddrOneAddr := []sdk.AccAddress{} | ||
for i := 0; i < 10; i++ { | ||
tenAddrOneAddr = append(tenAddrOneAddr, s.TestAccs[0]) | ||
} | ||
tenAddrOneAddr = append(tenAddrOneAddr, s.TestAccs[1]) | ||
tests := map[string]positionOwnershipTest{ | ||
"prefix malleability (prior bug)": { | ||
queryPositionOwner: s.TestAccs[1], | ||
queryPositionId: 1, expPass: false, | ||
setupPositions: tenAddrOneAddr, | ||
}, | ||
"things work": { | ||
queryPositionOwner: s.TestAccs[1], | ||
queryPositionId: 10, expPass: true, | ||
setupPositions: tenAddrOneAddr, | ||
}, | ||
} | ||
for name, test := range tests { | ||
s.Run(name, func() { | ||
test.poolId = 1 | ||
s.runIsPositionOwnerTest(test) | ||
}) | ||
} | ||
} | ||
|
||
func (s *KeeperTestSuite) TestIsPositionOwner() { | ||
actualOwner := s.TestAccs[0] | ||
nonOwner := s.TestAccs[1] | ||
|
||
tests := []struct { | ||
name string | ||
ownerToQuery sdk.AccAddress | ||
poolId uint64 | ||
positionId uint64 | ||
isOwner bool | ||
}{ | ||
{ | ||
name: "Happy path", | ||
ownerToQuery: actualOwner, | ||
poolId: 1, | ||
positionId: DefaultPositionId, | ||
isOwner: true, | ||
}, | ||
{ | ||
name: "query non owner", | ||
ownerToQuery: nonOwner, | ||
poolId: 1, | ||
positionId: DefaultPositionId, | ||
isOwner: false, | ||
}, | ||
{ | ||
name: "different pool ID, not the owner", | ||
ownerToQuery: actualOwner, | ||
poolId: 2, | ||
positionId: DefaultPositionId, | ||
isOwner: false, | ||
}, | ||
{ | ||
name: "different position ID, not the owner", | ||
ownerToQuery: actualOwner, | ||
poolId: 1, | ||
positionId: DefaultPositionId + 1, | ||
isOwner: false, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
s.Run(test.name, func() { | ||
s.runIsPositionOwnerTest(positionOwnershipTest{ | ||
queryPositionOwner: test.ownerToQuery, | ||
queryPositionId: test.positionId, | ||
expPass: test.isOwner, | ||
// positions 0 and 1 are owned by actualOwner | ||
setupPositions: []sdk.AccAddress{actualOwner, actualOwner}, | ||
poolId: test.poolId, | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
func (s *KeeperTestSuite) TestGetUserPositions() { | ||
s.Setup() | ||
defaultAddress := s.TestAccs[0] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using IsZero directly avoids creating and using a new sdk.Dec object