Skip to content

Commit

Permalink
fix: Set limit to LSM deposit filtering (#731)
Browse files Browse the repository at this point in the history
* set limit to LSM deposit filtering

* changelog
  • Loading branch information
kruspy authored Jan 19, 2024
1 parent 8ef9742 commit 869a03e
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Bug Fixes

- [731](https://github.com/persistenceOne/pstake-native/pull/731) Set limit to LSM deposit filtering.
- [730](https://github.com/persistenceOne/pstake-native/pull/730) Fix deposit validate.
- [728](https://github.com/persistenceOne/pstake-native/pull/728) Fix prevent users from liquid-staking funds by
removing the Deposit entry.
Expand Down
2 changes: 1 addition & 1 deletion x/liquidstakeibc/keeper/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ func (k *Keeper) handleUnsuccessfulAck(
)
}
case sdk.MsgTypeURL(&stakingtypes.MsgRedeemTokensForShares{}):
deposits := k.FilterLSMDeposits(
deposits := k.FilterLSMDepositsWithLimit(
ctx,
func(d types.LSMDeposit) bool {
return d.IbcSequenceId == k.GetTransactionSequenceID(channel, sequence)
Expand Down
29 changes: 26 additions & 3 deletions x/liquidstakeibc/keeper/lsm_deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (k *Keeper) GetLSMDepositsFromIbcDenom(ctx sdk.Context, ibcDenom string) []
}

func (k *Keeper) GetLSMDepositsFromIbcSequenceID(ctx sdk.Context, ibcSequenceID string) []*liquidstakeibctypes.LSMDeposit {
return k.FilterLSMDeposits(
return k.FilterLSMDepositsWithLimit(
ctx,
func(d liquidstakeibctypes.LSMDeposit) bool {
return d.IbcSequenceId == ibcSequenceID
Expand All @@ -45,7 +45,7 @@ func (k *Keeper) GetLSMDepositsFromIbcSequenceID(ctx sdk.Context, ibcSequenceID
}

func (k *Keeper) GetTransferableLSMDeposits(ctx sdk.Context, chainID string) []*liquidstakeibctypes.LSMDeposit {
return k.FilterLSMDeposits(
return k.FilterLSMDepositsWithLimit(
ctx,
func(d liquidstakeibctypes.LSMDeposit) bool {
return d.ChainId == chainID && d.State == liquidstakeibctypes.LSMDeposit_DEPOSIT_PENDING
Expand All @@ -54,7 +54,7 @@ func (k *Keeper) GetTransferableLSMDeposits(ctx sdk.Context, chainID string) []*
}

func (k *Keeper) GetRedeemableLSMDeposits(ctx sdk.Context, chainID string) []*liquidstakeibctypes.LSMDeposit {
return k.FilterLSMDeposits(
return k.FilterLSMDepositsWithLimit(
ctx,
func(d liquidstakeibctypes.LSMDeposit) bool {
return d.ChainId == chainID && d.State == liquidstakeibctypes.LSMDeposit_DEPOSIT_RECEIVED
Expand Down Expand Up @@ -112,6 +112,29 @@ func (k *Keeper) FilterLSMDeposits(
return deposits
}

func (k *Keeper) FilterLSMDepositsWithLimit(
ctx sdk.Context,
filter func(d liquidstakeibctypes.LSMDeposit) bool,
) []*liquidstakeibctypes.LSMDeposit {
store := prefix.NewStore(ctx.KVStore(k.storeKey), liquidstakeibctypes.LSMDepositKey)
iterator := sdk.KVStorePrefixIterator(store, nil)
defer iterator.Close()

deposits := make([]*liquidstakeibctypes.LSMDeposit, 0)
for ; iterator.Valid(); iterator.Next() {
deposit := liquidstakeibctypes.LSMDeposit{}
k.cdc.MustUnmarshal(iterator.Value(), &deposit)
if filter(deposit) {
deposits = append(deposits, &deposit)
}
if len(deposits) == liquidstakeibctypes.LSMDepositFilterLimit {
return deposits
}
}

return deposits
}

func (k *Keeper) GetLSMDepositAmountUntokenized(ctx sdk.Context, chainID string) math.Int {
amount := sdk.ZeroInt()

Expand Down
100 changes: 90 additions & 10 deletions x/liquidstakeibc/keeper/lsm_deposit_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package keeper_test

import (
"strconv"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/persistenceOne/pstake-native/v2/x/liquidstakeibc/types"
)

const (
TestDelegatorAddress = "persistence1234"
TestLSMDenom = "cosmosvaloper1234/1"
TestDelegatorAddress = "persistence1234"
TestLSMDenom = "cosmosvaloper1234/1"
TestLSMDenomIncomplete = "cosmosvaloper1234/"
LSMDepositsTestAmount = 15000
)

func (suite *IntegrationTestSuite) TestGetSetLSMDeposit() {
Expand Down Expand Up @@ -140,21 +144,39 @@ func (suite *IntegrationTestSuite) TestGetLSMDepositsFromIbcSequenceID() {
},
)

for i := 2; i < LSMDepositsTestAmount; i++ {
suite.app.LiquidStakeIBCKeeper.SetLSMDeposit(
suite.ctx,
&types.LSMDeposit{
ChainId: suite.chainB.ChainID,
DelegatorAddress: TestDelegatorAddress,
Denom: TestLSMDenomIncomplete + strconv.Itoa(i),
IbcSequenceId: "2",
},
)
}

tc := []struct {
name string
ibcSequence string
expectedLen int
found bool
}{
{
name: "Success",
name: "Success Limit Not Hit",
ibcSequence: "1",
expectedLen: 1,
found: true,
},
{
name: "NotFound",
name: "Success Limit Hit",
ibcSequence: "2",
expectedLen: types.LSMDepositFilterLimit,
found: true,
},
{
name: "NotFound",
ibcSequence: "3",
expectedLen: 0,
found: false,
},
Expand Down Expand Up @@ -187,21 +209,39 @@ func (suite *IntegrationTestSuite) TestGetTransferableLSMDeposits() {
},
)

for i := 2; i < LSMDepositsTestAmount; i++ {
suite.app.LiquidStakeIBCKeeper.SetLSMDeposit(
suite.ctx,
&types.LSMDeposit{
ChainId: suite.chainA.ChainID,
DelegatorAddress: TestDelegatorAddress,
Denom: TestLSMDenomIncomplete + strconv.Itoa(i),
State: types.LSMDeposit_DEPOSIT_PENDING,
},
)
}

tc := []struct {
name string
chainID string
expectedLen int
found bool
}{
{
name: "Success",
name: "Success Limit Not Hit",
chainID: suite.chainB.ChainID,
expectedLen: 1,
found: true,
},
{
name: "NotFound",
name: "Success Limit Not Hit",
chainID: suite.chainA.ChainID,
expectedLen: types.LSMDepositFilterLimit,
found: true,
},
{
name: "NotFound",
chainID: "non-valid-chain-id",
expectedLen: 0,
found: false,
},
Expand All @@ -214,7 +254,7 @@ func (suite *IntegrationTestSuite) TestGetTransferableLSMDeposits() {
suite.Require().Equal(t.expectedLen, len(deposits))
if t.found {
suite.Require().Equal(
suite.chainB.ChainID, deposits[0].ChainId,
t.chainID, deposits[0].ChainId,
TestDelegatorAddress, deposits[0].DelegatorAddress,
TestLSMDenom, deposits[0].Denom,
)
Expand All @@ -234,21 +274,39 @@ func (suite *IntegrationTestSuite) TestGetRedeemableLSMDeposits() {
},
)

for i := 2; i < LSMDepositsTestAmount; i++ {
suite.app.LiquidStakeIBCKeeper.SetLSMDeposit(
suite.ctx,
&types.LSMDeposit{
ChainId: suite.chainA.ChainID,
DelegatorAddress: TestDelegatorAddress,
Denom: TestLSMDenomIncomplete + strconv.Itoa(i),
State: types.LSMDeposit_DEPOSIT_RECEIVED,
},
)
}

tc := []struct {
name string
chainID string
expectedLen int
found bool
}{
{
name: "Success",
name: "Success Limit Not Hit",
chainID: suite.chainB.ChainID,
expectedLen: 1,
found: true,
},
{
name: "NotFound",
name: "Success Limit Hit",
chainID: suite.chainA.ChainID,
expectedLen: types.LSMDepositFilterLimit,
found: true,
},
{
name: "NotFound",
chainID: "non-valid-chain-id",
expectedLen: 0,
found: false,
},
Expand All @@ -261,7 +319,7 @@ func (suite *IntegrationTestSuite) TestGetRedeemableLSMDeposits() {
suite.Require().Equal(t.expectedLen, len(deposits))
if t.found {
suite.Require().Equal(
suite.chainB.ChainID, deposits[0].ChainId,
t.chainID, deposits[0].ChainId,
TestDelegatorAddress, deposits[0].DelegatorAddress,
TestLSMDenom, deposits[0].Denom,
)
Expand Down Expand Up @@ -396,6 +454,28 @@ func (suite *IntegrationTestSuite) TestFilterLSMDeposits() {
suite.Require().Equal("cosmosvaloper1234/1", deposits[0].Denom)
}

func (suite *IntegrationTestSuite) TestFilterLSMDepositsWithLimit() {
for i := 2; i < LSMDepositsTestAmount; i++ {
suite.app.LiquidStakeIBCKeeper.SetLSMDeposit(
suite.ctx,
&types.LSMDeposit{
ChainId: suite.chainB.ChainID,
DelegatorAddress: TestDelegatorAddress,
Denom: TestLSMDenomIncomplete + strconv.Itoa(i),
},
)
}

deposits := suite.app.LiquidStakeIBCKeeper.FilterLSMDepositsWithLimit(
suite.ctx,
func(d types.LSMDeposit) bool {
return d.ChainId == suite.chainB.ChainID
},
)

suite.Require().Equal(types.LSMDepositFilterLimit, len(deposits))
}

func (suite *IntegrationTestSuite) TestGetLSMDepositAmountUntokenized() {
deposits := []*types.LSMDeposit{
{
Expand Down
2 changes: 2 additions & 0 deletions x/liquidstakeibc/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ const (
IBCPrefix = transfertypes.DenomPrefix + "/"

UnbondingStateEpochLimit = 4

LSMDepositFilterLimit = 10000
)

// Consts for KV updates, update host chain
Expand Down

0 comments on commit 869a03e

Please sign in to comment.