diff --git a/x/interchainstaking/keeper/keeper_test.go b/x/interchainstaking/keeper/keeper_test.go index 97d7dada8..1ad9a87bb 100644 --- a/x/interchainstaking/keeper/keeper_test.go +++ b/x/interchainstaking/keeper/keeper_test.go @@ -1,6 +1,8 @@ package keeper_test import ( + "context" + "errors" "testing" "time" @@ -20,9 +22,11 @@ import ( ibctesting "github.com/cosmos/ibc-go/v5/testing" "github.com/quicksilver-zone/quicksilver/app" + "github.com/quicksilver-zone/quicksilver/utils" "github.com/quicksilver-zone/quicksilver/utils/addressutils" "github.com/quicksilver-zone/quicksilver/utils/randomutils" ics "github.com/quicksilver-zone/quicksilver/x/interchainstaking" + interchainstakingkeeper "github.com/quicksilver-zone/quicksilver/x/interchainstaking/keeper" icstypes "github.com/quicksilver-zone/quicksilver/x/interchainstaking/types" ) @@ -664,3 +668,78 @@ func (suite *KeeperTestSuite) TestOverrideRedemptionRateNoCap() { suite.Equal(sdk.NewDecWithPrec(676666666666666667, 18), zone.RedemptionRate) } + +func (suite *KeeperTestSuite) TestGetChainIDFromContext() { + testCase := []struct { + name string + setup func() (*interchainstakingkeeper.Keeper, sdk.Context) + wantErr bool + expectedErr error + expectedChainID string + }{ + { + name: "connectionID not in context", + setup: func() (*interchainstakingkeeper.Keeper, sdk.Context) { + suite.SetupTest() + suite.setupTestZones() + return suite.GetQuicksilverApp(suite.chainA).InterchainstakingKeeper, suite.chainA.GetContext() + }, + wantErr: true, + expectedErr: errors.New("connectionID not in context"), + }, + { + name: "get chainID success", + setup: func() (*interchainstakingkeeper.Keeper, sdk.Context) { + suite.SetupTest() + suite.setupTestZones() + ctx := suite.chainA.GetContext() + + ctx = ctx.WithContext(context.WithValue(ctx.Context(), utils.ContextKey("connectionID"), suite.path.EndpointA.ConnectionID)) + return suite.GetQuicksilverApp(suite.chainA).InterchainstakingKeeper, ctx + }, + wantErr: false, + expectedErr: nil, + expectedChainID: "testchain2", + }, + } + for _, tc := range testCase { + suite.Run(tc.name, func() { + keeper, ctx := tc.setup() + + chainID, err := keeper.GetChainIDFromContext(ctx) + if tc.wantErr { + suite.Equal(tc.expectedErr, err) + return + } + suite.NoError(err) + suite.Equal(tc.expectedChainID, chainID) + }) + } +} + +func (suite *KeeperTestSuite) TestIteratePortConnection() { + suite.SetupTest() + suite.setupTestZones() + + quicksilver := suite.GetQuicksilverApp(suite.chainA) + ctx := suite.chainA.GetContext() + icsKeeper := quicksilver.InterchainstakingKeeper + zone, found := icsKeeper.GetZone(ctx, suite.chainB.ChainID) + suite.True(found) + // After setup, there are 4 port connections available + pcs := icsKeeper.AllPortConnections(ctx) + suite.Equal(4, len(pcs)) + // set add 4 port connections + icsKeeper.SetConnectionForPort(ctx, "connection-1", zone.ChainId+"."+"deposit") + icsKeeper.SetConnectionForPort(ctx, "connection-2", zone.ChainId+"."+"withdrawal") + icsKeeper.SetConnectionForPort(ctx, "connection-3", zone.ChainId+"."+"performance") + icsKeeper.SetConnectionForPort(ctx, "connection-4", zone.ChainId+"."+"delegate") + + // iterate + var portConnection []icstypes.PortConnectionTuple + icsKeeper.IteratePortConnections(ctx, func(pc icstypes.PortConnectionTuple) (stop bool) { + portConnection = append(portConnection, pc) + return false + }) + suite.Equal(8, len(portConnection)) +} diff --git a/x/interchainstaking/keeper/redelegation_record_test.go b/x/interchainstaking/keeper/redelegation_record_test.go index 69e1feec3..1fb3662b4 100644 --- a/x/interchainstaking/keeper/redelegation_record_test.go +++ b/x/interchainstaking/keeper/redelegation_record_test.go @@ -3,6 +3,8 @@ package keeper_test import ( "time" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/quicksilver-zone/quicksilver/utils/addressutils" "github.com/quicksilver-zone/quicksilver/x/interchainstaking/types" ) @@ -166,3 +168,99 @@ func (suite *KeeperTestSuite) TestDeleteRedelegationRecordByKey() { records = quicksilver.InterchainstakingKeeper.AllRedelegationRecords(ctx) suite.Equal(0, len(records)) } + +func (suite *KeeperTestSuite) TestGCCompletedUnbondings() { + 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") + } + records := quicksilver.InterchainstakingKeeper.AllWithdrawalRecords(ctx) + suite.Equal(0, len(records)) + + vals := quicksilver.InterchainstakingKeeper.GetValidators(ctx, zone.ChainId) + currentTime := ctx.BlockTime() + + record1 := types.WithdrawalRecord{ + ChainId: suite.chainB.ChainID, + Delegator: zone.DelegationAddress.Address, + Distribution: []*types.Distribution{ + { + Valoper: vals[0].ValoperAddress, + Amount: 500, + }, + { + Valoper: vals[1].ValoperAddress, + Amount: 500, + }, + }, + Recipient: user1.String(), + Amount: sdk.NewCoins(sdk.NewCoin(zone.BaseDenom, sdk.NewInt(1000))), + BurnAmount: sdk.NewCoin(zone.LocalDenom, sdk.NewInt(1000)), + Txhash: "1613D2E8FBF7C7294A4D2247B55EE89FB22FC68C62D61050B944F1191DF092BD", + Status: types.WithdrawStatusCompleted, + CompletionTime: currentTime.Add(-25 * time.Hour).UTC(), + } + quicksilver.InterchainstakingKeeper.SetWithdrawalRecord(ctx, record1) + + record2 := types.WithdrawalRecord{ + ChainId: suite.chainB.ChainID, + Delegator: zone.DelegationAddress.Address, + Distribution: []*types.Distribution{ + { + Valoper: vals[0].ValoperAddress, + Amount: 500, + }, + { + Valoper: vals[1].ValoperAddress, + Amount: 500, + }, + }, + Recipient: user2.String(), + Amount: sdk.NewCoins(sdk.NewCoin(zone.BaseDenom, sdk.NewInt(1000))), + BurnAmount: sdk.NewCoin(zone.LocalDenom, sdk.NewInt(1000)), + Txhash: "91DF093BD1613D2E8FBF7C7294A4D2247B55EE89FB22FC68C62D61050B944F11", + Status: types.WithdrawStatusUnbond, + CompletionTime: currentTime.Add(25 * time.Hour).UTC(), + } + quicksilver.InterchainstakingKeeper.SetWithdrawalRecord(ctx, record2) + + record3 := types.WithdrawalRecord{ + ChainId: suite.chainB.ChainID, + Delegator: zone.DelegationAddress.Address, + Distribution: []*types.Distribution{ + { + Valoper: vals[0].ValoperAddress, + Amount: 500, + }, + { + Valoper: vals[1].ValoperAddress, + Amount: 500, + }, + }, + Recipient: user2.String(), + Amount: sdk.NewCoins(sdk.NewCoin(zone.BaseDenom, sdk.NewInt(1000))), + BurnAmount: sdk.NewCoin(zone.LocalDenom, sdk.NewInt(1000)), + Txhash: "2247B55EE89FB22FC68C62D61050B944F1191DF093BD1613D2E8FBF7C7294A4D", + Status: types.WithdrawStatusUnbond, + CompletionTime: time.Time{}, + } + quicksilver.InterchainstakingKeeper.SetWithdrawalRecord(ctx, record3) + + records = quicksilver.InterchainstakingKeeper.AllWithdrawalRecords(ctx) + suite.Equal(3, len(records)) + + err := quicksilver.InterchainstakingKeeper.GCCompletedUnbondings(ctx, &zone) + suite.NoError(err) + + records = quicksilver.InterchainstakingKeeper.AllWithdrawalRecords(ctx) + suite.Equal(2, len(records)) + + _, found = quicksilver.InterchainstakingKeeper.GetWithdrawalRecord(ctx, record1.ChainId, record1.Txhash, record1.Status) + suite.False(found) +}