Skip to content

Commit

Permalink
Merge branch 'main' into neitdung/test-redeem-tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
ThanhNhann authored Oct 26, 2023
2 parents 9e530ab + 64173b8 commit 780e184
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# CODEOWNERS: https://help.github.com/articles/about-codeowners/

# Primary repo maintainers
* @joe-bowman @ajansari95 @muku314115 @ThanhNhann @faddat @sontrinh16 @anhductn2001
* @joe-bowman @ThanhNhann @faddat @sontrinh16 @anhductn2001
106 changes: 106 additions & 0 deletions x/interchainstaking/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper_test

import (
"errors"
"fmt"
"time"

Expand All @@ -10,8 +11,10 @@ import (
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"

clienttypes "github.com/cosmos/ibc-go/v5/modules/core/02-client/types"
connectiontypes "github.com/cosmos/ibc-go/v5/modules/core/03-connection/types"
channeltypes "github.com/cosmos/ibc-go/v5/modules/core/04-channel/types"
tmclienttypes "github.com/cosmos/ibc-go/v5/modules/light-clients/07-tendermint/types"

"github.com/quicksilver-zone/quicksilver/utils/addressutils"
icskeeper "github.com/quicksilver-zone/quicksilver/x/interchainstaking/keeper"
Expand Down Expand Up @@ -584,3 +587,106 @@ func (suite *KeeperTestSuite) TestGovCloseChannel() {
})
}
}

func (suite *KeeperTestSuite) TestGovReopenChannel() {
testCase := []struct {
name string
malleate func(suite *KeeperTestSuite) *icstypes.MsgGovReopenChannel
expecErr error
}{
{
name: "invalid connection id",
malleate: func(suite *KeeperTestSuite) *icstypes.MsgGovReopenChannel {
return &icstypes.MsgGovReopenChannel{
ConnectionId: "",
PortId: "",
Authority: "",
}
},
expecErr: fmt.Errorf("unable to obtain chain id: invalid connection id, \"%s\" not found", ""),
},
{
name: "chainID / connectsionID mismatch",
malleate: func(suite *KeeperTestSuite) *icstypes.MsgGovReopenChannel {
return &icstypes.MsgGovReopenChannel{
ConnectionId: suite.path.EndpointA.ConnectionID,
PortId: "",
Authority: "",
}
},
expecErr: fmt.Errorf("chainID / connectionID mismatch. Connection: %s, Port: %s", "testchain2", ""),
},
{
name: "existing active channel",
malleate: func(suite *KeeperTestSuite) *icstypes.MsgGovReopenChannel {
k := suite.GetQuicksilverApp(suite.chainA).InterchainstakingKeeper
ctx := suite.chainA.GetContext()
channels := suite.GetQuicksilverApp(suite.chainA).IBCKeeper.ChannelKeeper.GetAllChannels(ctx)
return &icstypes.MsgGovReopenChannel{
ConnectionId: suite.path.EndpointA.ConnectionID,
PortId: channels[0].PortId,
Authority: sdk.MustBech32ifyAddressBytes(sdk.GetConfig().GetBech32AccountAddrPrefix(), k.AccountKeeper.GetModuleAddress(govtypes.ModuleName)),
}
},
expecErr: errors.New("existing active channel channel-7 for portID icacontroller-testchain2.delegate on connection connection-0 for owner testchain2.delegate: active channel already set for this owner"),
},
{
name: "pass",
malleate: func(suite *KeeperTestSuite) *icstypes.MsgGovReopenChannel {
quicksilver := suite.GetQuicksilverApp(suite.chainA)
ctx := suite.chainA.GetContext()
connectionID := "connection-1"
portID := "icacontroller-testchain2.delegate"
channelID := "channel-9"

version := []*connectiontypes.Version{
{Identifier: "1", Features: []string{"ORDER_ORDERED", "ORDER_UNORDERED"}},
}
connectionEnd := connectiontypes.ConnectionEnd{ClientId: "09-tendermint-1", State: connectiontypes.OPEN, Versions: version}
quicksilver.IBCKeeper.ConnectionKeeper.SetConnection(ctx, connectionID, connectionEnd)

_, f := quicksilver.IBCKeeper.ConnectionKeeper.GetConnection(ctx, connectionID)
suite.True(f)

channelSet := channeltypes.Channel{
State: channeltypes.TRYOPEN,
Ordering: channeltypes.NONE,
Counterparty: channeltypes.NewCounterparty(portID, channelID),
ConnectionHops: []string{connectionID},
}
quicksilver.IBCKeeper.ChannelKeeper.SetChannel(ctx, portID, channelID, channelSet)

quicksilver.IBCKeeper.ClientKeeper.SetClientState(ctx, connectionEnd.ClientId, &tmclienttypes.ClientState{ChainId: suite.chainB.ChainID, TrustingPeriod: time.Hour, LatestHeight: clienttypes.Height{RevisionNumber: 1, RevisionHeight: 100}})

return &icstypes.MsgGovReopenChannel{
ConnectionId: connectionID,
PortId: portID,
Authority: sdk.MustBech32ifyAddressBytes(sdk.GetConfig().GetBech32AccountAddrPrefix(), quicksilver.InterchainstakingKeeper.AccountKeeper.GetModuleAddress(govtypes.ModuleName)),
}
},
expecErr: nil,
},
}
for _, tc := range testCase {
suite.Run(tc.name, func() {
suite.SetupTest()
suite.setupTestZones()

msg := tc.malleate(suite)
msgSrv := icskeeper.NewMsgServerImpl(*suite.GetQuicksilverApp(suite.chainA).InterchainstakingKeeper)
ctx := suite.chainA.GetContext()

_, err := msgSrv.GovReopenChannel(ctx, msg)
if tc.expecErr != nil {
suite.Equal(tc.expecErr.Error(), err.Error())
return
}
suite.NoError(err)

// Check connection for port has been set
conn, err := suite.GetQuicksilverApp(suite.chainA).InterchainstakingKeeper.GetConnectionForPort(ctx, msg.PortId)
suite.NoError(err)
suite.Equal(conn, msg.ConnectionId)
})
}
}
63 changes: 63 additions & 0 deletions x/interchainstaking/keeper/receipt_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper_test

import (
"fmt"
"time"

"cosmossdk.io/math"
Expand All @@ -11,6 +12,8 @@ import (
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

transfertypes "github.com/cosmos/ibc-go/v5/modules/apps/transfer/types"

"github.com/quicksilver-zone/quicksilver/utils/addressutils"
"github.com/quicksilver-zone/quicksilver/utils/randomutils"
"github.com/quicksilver-zone/quicksilver/x/interchainstaking/types"
Expand Down Expand Up @@ -314,3 +317,63 @@ func (suite *KeeperTestSuite) TestReceiptStore() {

suite.Equal(&now, receipt.Completed)
}

func (suite *KeeperTestSuite) TestSendTokenIBC() {
suite.Run("test", func() {
suite.SetupTest()

// setup transfer channel
suite.path.EndpointA.ChannelConfig.Version = transfertypes.Version
suite.path.EndpointB.ChannelConfig.Version = transfertypes.Version
suite.coordinator.CreateTransferChannels(suite.path)

suite.setupTestZones()

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

zone, found := quicksilver.InterchainstakingKeeper.GetZone(ctx, suite.chainB.ChainID)
suite.True(found)

sender := suite.chainA.SenderAccount.GetAddress()
receiver := addressutils.GenerateAddressForTestWithPrefix("cosmos")

amount := sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100))
err := quicksilver.BankKeeper.MintCoins(ctx, types.ModuleName, sdk.NewCoins(amount))
suite.NoError(err)
err = quicksilver.BankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, sender, sdk.NewCoins(amount))
suite.NoError(err)

// Try to send native token but wrong connection id on zone
wrongZone := zone
wrongZone.ConnectionId = "connection-10"
err = quicksilver.InterchainstakingKeeper.SendTokenIBC(ctx, sender, receiver, &wrongZone, amount)
suite.ErrorContains(err, "unable to find remote transfer connection")

// Try to send the native token
err = quicksilver.InterchainstakingKeeper.SendTokenIBC(ctx, sender, receiver, &zone, amount)
suite.NoError(err)

portID := types.TransferPort
channelID := suite.path.EndpointA.ChannelID

ibcAmount := transfertypes.GetTransferCoin(portID, channelID, sdk.DefaultBondDenom, sdk.NewInt(100))

err = quicksilver.BankKeeper.MintCoins(ctx, types.ModuleName, sdk.NewCoins(ibcAmount))
suite.NoError(err)
err = quicksilver.BankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, sender, sdk.NewCoins(ibcAmount))
suite.NoError(err)

quicksilver.TransferKeeper.SetDenomTrace(
ctx,
transfertypes.DenomTrace{
Path: fmt.Sprintf("%s/%s", portID, channelID),
BaseDenom: sdk.DefaultBondDenom,
},
)

// Try to send the ibc token
err = quicksilver.InterchainstakingKeeper.SendTokenIBC(ctx, sender, receiver, &zone, ibcAmount)
suite.NoError(err)
})
}
58 changes: 58 additions & 0 deletions x/interchainstaking/keeper/redelegation_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,64 @@ func (suite *KeeperTestSuite) TestGCCompletedRedelegations() {
suite.False(found)
}

func (suite *KeeperTestSuite) TestDeleteRedelegationRecordByKey() {
quicksilver := suite.GetQuicksilverApp(suite.chainA)
ctx := suite.chainA.GetContext()

testValidatorOne := addressutils.GenerateAddressForTestWithPrefix("cosmosvaloper")
testValidatorTwo := addressutils.GenerateAddressForTestWithPrefix("cosmosvaloper")
testValidatorThree := addressutils.GenerateAddressForTestWithPrefix("cosmosvaloper")

suite.SetupTest()

// Currently there are 0 records
records := quicksilver.InterchainstakingKeeper.AllRedelegationRecords(ctx)
suite.Equal(0, len(records))

// Set 3 records
currentTime := ctx.BlockTime()

record := types.RedelegationRecord{
ChainId: "cosmoshub-4",
EpochNumber: 1,
Source: testValidatorOne,
Destination: testValidatorTwo,
Amount: 3000,
CompletionTime: currentTime.Add(time.Hour).UTC(),
}
quicksilver.InterchainstakingKeeper.SetRedelegationRecord(ctx, record)

record = types.RedelegationRecord{
ChainId: "cosmoshub-4",
EpochNumber: 1,
Source: testValidatorOne,
Destination: testValidatorThree,
Amount: 3000,
CompletionTime: currentTime.Add(-time.Hour).UTC(),
}
quicksilver.InterchainstakingKeeper.SetRedelegationRecord(ctx, record)
record = types.RedelegationRecord{
ChainId: "cosmoshub-4",
EpochNumber: 1,
Source: testValidatorThree,
Destination: testValidatorTwo,
Amount: 3000,
CompletionTime: time.Time{},
}
quicksilver.InterchainstakingKeeper.SetRedelegationRecord(ctx, record)
// Check set 3 records
records = quicksilver.InterchainstakingKeeper.AllRedelegationRecords(ctx)
suite.Equal(3, len(records))
// Handle DeleteRedelegationRecordByKey for 3 records
quicksilver.InterchainstakingKeeper.IterateRedelegationRecords(ctx, func(idx int64, key []byte, redelegation types.RedelegationRecord) bool {
quicksilver.InterchainstakingKeeper.DeleteRedelegationRecordByKey(ctx, append(types.KeyPrefixRedelegationRecord, key...))
return false
})
// Check DeleteRedelegationRecordByKey 3 records to 0 records
records = quicksilver.InterchainstakingKeeper.AllRedelegationRecords(ctx)
suite.Equal(0, len(records))
}

func (suite *KeeperTestSuite) TestGCCompletedUnbondings() {
suite.SetupTest()
suite.setupTestZones()
Expand Down

0 comments on commit 780e184

Please sign in to comment.