Skip to content

Commit

Permalink
Test for HandleWithdrawRewards (quicksilver-zone#659)
Browse files Browse the repository at this point in the history
* add handle withdraw rewards test

* lint

* remove test case name

* change waitgroup=0 case

* lint

---------

Co-authored-by: Joe Bowman <[email protected]>
Co-authored-by: Nguyen Thanh Nhan <[email protected]>
  • Loading branch information
3 people authored Nov 2, 2023
1 parent 0ef4916 commit fd9a5a0
Showing 1 changed file with 165 additions and 0 deletions.
165 changes: 165 additions & 0 deletions x/interchainstaking/keeper/ibc_packet_handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ var TestChannel = channeltypes.Channel{
ConnectionHops: []string{"connection-0"},
}

const queryAllBalancesPath = "cosmos.bank.v1beta1.Query/AllBalances"

func (suite *KeeperTestSuite) TestHandleMsgTransferGood() {
nineDec := sdk.NewDecWithPrec(9, 2)

Expand Down Expand Up @@ -773,6 +775,169 @@ func (suite *KeeperTestSuite) TestHandleWithdrawForUserLSM() {
}
}

func (suite *KeeperTestSuite) TestHandleWithdrawRewards() {
val := addressutils.GenerateValAddressForTest().String()
user := addressutils.GenerateAddressForTestWithPrefix("quick")
tests := []struct {
name string
setup func(ctx sdk.Context, quicksilver *app.Quicksilver, zone *icstypes.Zone)
msg func(zone *icstypes.Zone) sdk.Msg
checks func(ctx sdk.Context, quicksilver *app.Quicksilver, zone *icstypes.Zone)
triggered bool
err bool
}{
{
name: "wrong msg",
setup: func(ctx sdk.Context, quicksilver *app.Quicksilver, zone *icstypes.Zone) {},
checks: func(ctx sdk.Context, quicksilver *app.Quicksilver, zone *icstypes.Zone) {},
msg: func(zone *icstypes.Zone) sdk.Msg {
return &distrtypes.MsgWithdrawValidatorCommission{
ValidatorAddress: val,
}
},
triggered: false,
err: true,
},
{
name: "wrong context",
setup: func(ctx sdk.Context, quicksilver *app.Quicksilver, zone *icstypes.Zone) {
zone.ConnectionId = ""
quicksilver.InterchainstakingKeeper.SetZone(ctx, zone)
},
checks: func(ctx sdk.Context, quicksilver *app.Quicksilver, zone *icstypes.Zone) {},
msg: func(zone *icstypes.Zone) sdk.Msg {
return &distrtypes.MsgWithdrawDelegatorReward{
DelegatorAddress: user,
ValidatorAddress: val,
}
},
triggered: false,
err: true,
},
// try to decrement when waitgroup = 0
{
name: "try to decrement when waitgroup = 0",
setup: func(ctx sdk.Context, quicksilver *app.Quicksilver, zone *icstypes.Zone) {
zone.WithdrawalWaitgroup = 0
quicksilver.InterchainstakingKeeper.SetZone(ctx, zone)
},
checks: func(ctx sdk.Context, quicksilver *app.Quicksilver, zone *icstypes.Zone) {},
msg: func(zone *icstypes.Zone) sdk.Msg {
return &distrtypes.MsgWithdrawDelegatorReward{
DelegatorAddress: user,
ValidatorAddress: val,
}
},
triggered: false,
err: true,
},
{
name: "valid case with balances != 0",
setup: func(ctx sdk.Context, quicksilver *app.Quicksilver, zone *icstypes.Zone) {
zone.WithdrawalWaitgroup = 1
balances := sdk.NewCoins(
sdk.NewCoin(
zone.BaseDenom,
math.NewInt(10_000_000),
),
)
zone.DelegationAddress.Balance = balances
quicksilver.InterchainstakingKeeper.SetZone(ctx, zone)
},
checks: func(ctx sdk.Context, quicksilver *app.Quicksilver, zone *icstypes.Zone) {},
msg: func(zone *icstypes.Zone) sdk.Msg {
return &distrtypes.MsgWithdrawDelegatorReward{
DelegatorAddress: user,
ValidatorAddress: val,
}
},
triggered: false,
err: false,
},
{
name: "valid case trigger redemption rate and check if delegatorAddress == performanceAddress",
setup: func(ctx sdk.Context, quicksilver *app.Quicksilver, zone *icstypes.Zone) {
zone.WithdrawalWaitgroup = 1
quicksilver.InterchainstakingKeeper.SetZone(ctx, zone)
},
checks: func(ctx sdk.Context, quicksilver *app.Quicksilver, zone *icstypes.Zone) {
newZone, found := quicksilver.InterchainstakingKeeper.GetZone(ctx, zone.ChainId)
suite.True(found)
suite.Zero(newZone.WithdrawalWaitgroup)
},
msg: func(zone *icstypes.Zone) sdk.Msg {
return &distrtypes.MsgWithdrawDelegatorReward{
DelegatorAddress: user,
ValidatorAddress: val,
}
},
triggered: true,
err: false,
},
{
name: "valid case trigger redemption rate and without checking if delegatorAddress == performanceAddress",
setup: func(ctx sdk.Context, quicksilver *app.Quicksilver, zone *icstypes.Zone) {
zone.WithdrawalWaitgroup = 0
quicksilver.InterchainstakingKeeper.SetZone(ctx, zone)
},
checks: func(ctx sdk.Context, quicksilver *app.Quicksilver, zone *icstypes.Zone) {},
msg: func(zone *icstypes.Zone) sdk.Msg {
return &distrtypes.MsgWithdrawDelegatorReward{
DelegatorAddress: zone.PerformanceAddress.Address,
ValidatorAddress: val,
}
},
triggered: true,
err: false,
},
}

for _, test := range tests {
suite.Run(test.name, func() {
suite.SetupTest()
suite.setupTestZones()

quicksilver := suite.GetQuicksilverApp(suite.chainA)
ctx := suite.chainA.GetContext()

zone, found := quicksilver.InterchainstakingKeeper.GetZone(ctx, suite.chainB.ChainID)
if !found {
suite.Fail("unable to retrieve zone for test")
}

test.setup(ctx, quicksilver, &zone)
prevAllBalancesQueryCnt := 0
for _, query := range quicksilver.InterchainQueryKeeper.AllQueries(ctx) {
if query.QueryType == queryAllBalancesPath {
prevAllBalancesQueryCnt++
}
}

ctx = ctx.WithContext(context.WithValue(ctx.Context(), utils.ContextKey("connectionID"), zone.ConnectionId))
err := quicksilver.InterchainstakingKeeper.HandleWithdrawRewards(ctx, test.msg(&zone))
if test.err {
suite.Error(err)
} else {
suite.NoError(err)
}

allBalancesQueryCnt := 0
for _, query := range quicksilver.InterchainQueryKeeper.AllQueries(ctx) {
if query.QueryType == queryAllBalancesPath {
allBalancesQueryCnt++
}
}
if test.triggered {
suite.Equal(prevAllBalancesQueryCnt+1, allBalancesQueryCnt)
} else {
suite.Equal(prevAllBalancesQueryCnt, allBalancesQueryCnt)
}

test.checks(ctx, quicksilver, &zone)
})
}
}

func (suite *KeeperTestSuite) TestHandleFailedUnbondSend() {
v1 := addressutils.GenerateValAddressForTest().String()
v2 := addressutils.GenerateValAddressForTest().String()
Expand Down

0 comments on commit fd9a5a0

Please sign in to comment.