Skip to content
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

Arham/auto register host zone #1784

Merged
merged 4 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 116 additions & 1 deletion x/participationrewards/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import (
"encoding/json"

"errors"
"fmt"
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
sdk "github.com/cosmos/cosmos-sdk/types"
transfertypes "github.com/cosmos/ibc-go/v6/modules/apps/transfer/types"
"github.com/quicksilver-zone/quicksilver/utils"

epochstypes "github.com/quicksilver-zone/quicksilver/x/epochs/types"
icstypes "github.com/quicksilver-zone/quicksilver/x/interchainstaking/types"
Expand Down Expand Up @@ -146,6 +149,118 @@
Data: localDenomBytes,
})

// channel for the host chain
channel, found := k.IBCKeeper.ChannelKeeper.GetChannel(ctx, transfertypes.PortID, zone.TransferChannel)
if !found {
return fmt.Errorf("channel not found: %s", zone.TransferChannel)
}

Check warning on line 156 in x/participationrewards/keeper/hooks.go

View check run for this annotation

Codecov / codecov/patch

x/participationrewards/keeper/hooks.go#L155-L156

Added lines #L155 - L156 were not covered by tests

// Create LiquidAllowedDenomProtocolData for the host zone
hostZoneDenom := types.LiquidAllowedDenomProtocolData{
ChainID: zone.ChainId,
RegisteredZoneChainID: zone.ChainId,
IbcDenom: utils.DeriveIbcDenom(transfertypes.PortID, channel.Counterparty.ChannelId, channel.Counterparty.PortId, zone.TransferChannel, zone.LocalDenom),
QAssetDenom: zone.LocalDenom,
}
if err := hostZoneDenom.ValidateBasic(); err != nil {
return err
}

Check warning on line 167 in x/participationrewards/keeper/hooks.go

View check run for this annotation

Codecov / codecov/patch

x/participationrewards/keeper/hooks.go#L166-L167

Added lines #L166 - L167 were not covered by tests
hostZoneDenomBytes, err := json.Marshal(hostZoneDenom)
if err != nil {
return err
}

Check warning on line 171 in x/participationrewards/keeper/hooks.go

View check run for this annotation

Codecov / codecov/patch

x/participationrewards/keeper/hooks.go#L170-L171

Added lines #L170 - L171 were not covered by tests
k.SetProtocolData(ctx, hostZoneDenom.GenerateKey(), &types.ProtocolData{
Type: types.ProtocolDataType_name[int32(types.ProtocolDataTypeLiquidToken)],
Data: hostZoneDenomBytes,
})

// Fetch OsmosisParamsProtocolData and create LiquidAllowedDenomProtocolData for Osmosis
osmosisParamsData, found := k.GetProtocolData(ctx, types.ProtocolDataTypeOsmosisParams, types.OsmosisParamsKey)
if found {
osmosisParams, err := types.UnmarshalProtocolData(types.ProtocolDataTypeOsmosisParams, osmosisParamsData.Data)
if err != nil {
return err
}

Check warning on line 183 in x/participationrewards/keeper/hooks.go

View check run for this annotation

Codecov / codecov/patch

x/participationrewards/keeper/hooks.go#L182-L183

Added lines #L182 - L183 were not covered by tests
osmosisParamsData, ok := osmosisParams.(*types.OsmosisParamsProtocolData)
if !ok {
return errors.New("error unmarshalling protocol data for osmosis chain")
}

Check warning on line 187 in x/participationrewards/keeper/hooks.go

View check run for this annotation

Codecov / codecov/patch

x/participationrewards/keeper/hooks.go#L186-L187

Added lines #L186 - L187 were not covered by tests

_, tt, err := GetAndUnmarshalProtocolData[*types.ConnectionProtocolData](ctx, k, osmosisParamsData.ChainID, types.ProtocolDataTypeConnection)
if err != nil {
k.Logger(ctx).Error("Error unmarshalling protocol data for osmosis chain")
return err
}

Check warning on line 193 in x/participationrewards/keeper/hooks.go

View check run for this annotation

Codecov / codecov/patch

x/participationrewards/keeper/hooks.go#L191-L193

Added lines #L191 - L193 were not covered by tests
osmosisChannel := tt.TransferChannel

// channel for the osmosis chain
channel, found := k.IBCKeeper.ChannelKeeper.GetChannel(ctx, transfertypes.PortID, osmosisChannel)
if !found {
return errors.New("channel not found: " + osmosisChannel)
}

Check warning on line 200 in x/participationrewards/keeper/hooks.go

View check run for this annotation

Codecov / codecov/patch

x/participationrewards/keeper/hooks.go#L199-L200

Added lines #L199 - L200 were not covered by tests
osmosisDenom := types.LiquidAllowedDenomProtocolData{
ChainID: osmosisParamsData.ChainID,
RegisteredZoneChainID: zone.ChainId,
IbcDenom: utils.DeriveIbcDenom(transfertypes.PortID, channel.Counterparty.ChannelId, transfertypes.PortID, osmosisChannel, zone.LocalDenom),
QAssetDenom: zone.LocalDenom,
}
if err := osmosisDenom.ValidateBasic(); err != nil {
return err
}

Check warning on line 209 in x/participationrewards/keeper/hooks.go

View check run for this annotation

Codecov / codecov/patch

x/participationrewards/keeper/hooks.go#L208-L209

Added lines #L208 - L209 were not covered by tests
osmosisDenomBytes, err := json.Marshal(osmosisDenom)
if err != nil {
return err
}

Check warning on line 213 in x/participationrewards/keeper/hooks.go

View check run for this annotation

Codecov / codecov/patch

x/participationrewards/keeper/hooks.go#L212-L213

Added lines #L212 - L213 were not covered by tests
k.SetProtocolData(ctx, osmosisDenom.GenerateKey(), &types.ProtocolData{
Type: types.ProtocolDataType_name[int32(types.ProtocolDataTypeLiquidToken)],
Data: osmosisDenomBytes,
})
}

// Fetch UmeeParamsProtocolData and create LiquidAllowedDenomProtocolData for Umee
umeeParamsData, found := k.GetProtocolData(ctx, types.ProtocolDataTypeUmeeParams, types.UmeeParamsKey)
if found {
umeeParams, err := types.UnmarshalProtocolData(types.ProtocolDataTypeUmeeParams, umeeParamsData.Data)
if err != nil {
return err
}

Check warning on line 226 in x/participationrewards/keeper/hooks.go

View check run for this annotation

Codecov / codecov/patch

x/participationrewards/keeper/hooks.go#L225-L226

Added lines #L225 - L226 were not covered by tests

umeeParamsData, ok := umeeParams.(*types.UmeeParamsProtocolData)
if !ok {
return errors.New("error unmarshalling protocol data for umee chain")
}

Check warning on line 231 in x/participationrewards/keeper/hooks.go

View check run for this annotation

Codecov / codecov/patch

x/participationrewards/keeper/hooks.go#L230-L231

Added lines #L230 - L231 were not covered by tests

_, tt, err := GetAndUnmarshalProtocolData[*types.ConnectionProtocolData](ctx, k, umeeParamsData.ChainID, types.ProtocolDataTypeConnection)
if err != nil {
k.Logger(ctx).Error("Error unmarshalling protocol data for umee chain")
return err
}

Check warning on line 237 in x/participationrewards/keeper/hooks.go

View check run for this annotation

Codecov / codecov/patch

x/participationrewards/keeper/hooks.go#L235-L237

Added lines #L235 - L237 were not covered by tests
umeeChannel := tt.TransferChannel

// channel for the umee chain
channel, found := k.IBCKeeper.ChannelKeeper.GetChannel(ctx, transfertypes.PortID, umeeChannel)
if !found {
return errors.New("channel not found: " + umeeChannel)
}

Check warning on line 244 in x/participationrewards/keeper/hooks.go

View check run for this annotation

Codecov / codecov/patch

x/participationrewards/keeper/hooks.go#L243-L244

Added lines #L243 - L244 were not covered by tests
umeeDenom := types.LiquidAllowedDenomProtocolData{
ChainID: umeeParamsData.ChainID,
RegisteredZoneChainID: zone.ChainId,
IbcDenom: utils.DeriveIbcDenom(transfertypes.PortID, channel.Counterparty.ChannelId, transfertypes.PortID, umeeChannel, zone.LocalDenom),
QAssetDenom: zone.LocalDenom,
}
if err := umeeDenom.ValidateBasic(); err != nil {
return err
}

Check warning on line 253 in x/participationrewards/keeper/hooks.go

View check run for this annotation

Codecov / codecov/patch

x/participationrewards/keeper/hooks.go#L252-L253

Added lines #L252 - L253 were not covered by tests
umeeDenomBytes, err := json.Marshal(umeeDenom)
if err != nil {
return err
}

Check warning on line 257 in x/participationrewards/keeper/hooks.go

View check run for this annotation

Codecov / codecov/patch

x/participationrewards/keeper/hooks.go#L256-L257

Added lines #L256 - L257 were not covered by tests
k.SetProtocolData(ctx, umeeDenom.GenerateKey(), &types.ProtocolData{
Type: types.ProtocolDataType_name[int32(types.ProtocolDataTypeLiquidToken)],
Data: umeeDenomBytes,
})
}

return nil
}

Expand Down
38 changes: 38 additions & 0 deletions x/participationrewards/keeper/hooks_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper_test

import (
"github.com/quicksilver-zone/quicksilver/utils"
icstypes "github.com/quicksilver-zone/quicksilver/x/interchainstaking/types"
"github.com/quicksilver-zone/quicksilver/x/participationrewards/types"
)
Expand All @@ -15,6 +16,8 @@ func (suite *KeeperTestSuite) TestAfterZoneCreated() {
TransferChannel: "channel-1",
}
suite.Run("ProtocolData", func() {
suite.setupChannelForHookTest()
suite.setupTestProtocolData()
k := suite.GetQuicksilverApp(suite.chainA).ParticipationRewardsKeeper
ctx := suite.chainA.GetContext()
suite.NoError(k.Hooks().AfterZoneCreated(ctx, zone))
Expand All @@ -24,9 +27,44 @@ func (suite *KeeperTestSuite) TestAfterZoneCreated() {
suite.True(found)
upd, err := types.UnmarshalProtocolData(types.ProtocolDataTypeLiquidToken, pd.Data)
suite.NoError(err)

lpd := upd.(*types.LiquidAllowedDenomProtocolData)
suite.Equal(zone.ChainId, lpd.RegisteredZoneChainID)
suite.Equal(zone.LocalDenom, lpd.QAssetDenom)
suite.Equal(ctx.ChainID(), lpd.ChainID)

// check host chain
hostChainIBCDenom := utils.DeriveIbcDenom("transfer", counterpartyTestzoneChannel, "", "", "uqtst")
pd, found = k.GetProtocolData(ctx, types.ProtocolDataTypeLiquidToken, zone.ChainId+"_"+hostChainIBCDenom)
suite.True(found)
suite.NotNil(pd)
upd, err = types.UnmarshalProtocolData(types.ProtocolDataTypeLiquidToken, pd.Data)
suite.NoError(err)
lpd = upd.(*types.LiquidAllowedDenomProtocolData)
suite.Equal(zone.ChainId, lpd.RegisteredZoneChainID)
suite.Equal(zone.LocalDenom, lpd.QAssetDenom)
suite.Equal(hostChainIBCDenom, lpd.IbcDenom)

// check osmosis
osmosisIBCDenom := utils.DeriveIbcDenom("transfer", counterpartyOsmosisChannel, "", "", "uqtst")
pd, found = k.GetProtocolData(ctx, types.ProtocolDataTypeLiquidToken, osmosisTestChain+"_"+osmosisIBCDenom)
suite.True(found)
upd, err = types.UnmarshalProtocolData(types.ProtocolDataTypeLiquidToken, pd.Data)
suite.NoError(err)
lpd = upd.(*types.LiquidAllowedDenomProtocolData)
suite.Equal(zone.ChainId, lpd.RegisteredZoneChainID)
suite.Equal(zone.LocalDenom, lpd.QAssetDenom)
suite.Equal(osmosisIBCDenom, lpd.IbcDenom)

// check umee
umeeIBCDenom := utils.DeriveIbcDenom("transfer", counterpartyUmeeChannel, "", "", "uqtst")
pd, found = k.GetProtocolData(ctx, types.ProtocolDataTypeLiquidToken, umeeTestChain+"_"+umeeIBCDenom)
suite.True(found)
upd, err = types.UnmarshalProtocolData(types.ProtocolDataTypeLiquidToken, pd.Data)
suite.NoError(err)
lpd = upd.(*types.LiquidAllowedDenomProtocolData)
suite.Equal(zone.ChainId, lpd.RegisteredZoneChainID)
suite.Equal(zone.LocalDenom, lpd.QAssetDenom)
suite.Equal(umeeIBCDenom, lpd.IbcDenom)
})
}
36 changes: 33 additions & 3 deletions x/participationrewards/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,19 @@
umeeTestChain = "umee-types-1"
umeeBaseDenom = "uumee"

osmosisTestChain = "osmosis-1"
osmosisBaseDenom = "uosmo"

Check failure on line 39 in x/participationrewards/keeper/keeper_test.go

View workflow job for this annotation

GitHub Actions / lint

var `osmosisBaseDenom` is unused (unused)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Unused variable triggers pipeline error.
osmosisBaseDenom is reported as unused and fails the lint check. Consider removing it if it's not needed.

Apply this diff to remove the variable:

- osmosisBaseDenom = "uosmo"
🧰 Tools
🪛 golangci-lint (1.62.2)

39-39: var osmosisBaseDenom is unused

(unused)

🪛 GitHub Check: lint

[failure] 39-39:
var osmosisBaseDenom is unused (unused)

🪛 GitHub Actions: golangci-lint

[error] 39-39: Unused variable 'osmosisBaseDenom'


cosmosIBCDenom = "ibc/3020922B7576FC75BBE057A0290A9AEEFF489BB1113E6E365CE472D4BFB7FFA3"
osmosisIBCDenom = "ibc/15E9C5CF5969080539DB395FA7D9C0868265217EFC528433671AAF9B1912D159"
)

var (
counterpartyUmeeChannel = "channel-2000"
counterpartyOsmosisChannel = "channel-1000"
counterpartyTestzoneChannel = "channel-500"
)

func init() {
ibctesting.DefaultTestingAppInit = app.SetupTestingApp
}
Expand Down Expand Up @@ -106,7 +115,7 @@

akpd = quicksilver.ParticipationRewardsKeeper.AllKeyedProtocolDatas(suite.chainA.GetContext())
// added 19 in setupTestProtocolData
suite.Equal(14, len(akpd))
suite.Equal(15, len(akpd))

// advance the chains
suite.coordinator.CommitNBlocks(suite.chainA, 1)
Expand Down Expand Up @@ -405,7 +414,28 @@
return ibcModule.OnChanOpenAck(suite.chainA.GetContext(), portID, channelID, "", "")
}

func (suite *KeeperTestSuite) setupChannelForHookTest() {
quicksilver := suite.GetQuicksilverApp(suite.chainA)

// create a channel for the host chain, channel-1 <-> channel-500
quicksilver.IBCKeeper.ChannelKeeper.SetChannel(suite.chainA.GetContext(), ibctesting.TransferPort, "channel-1", channeltypes.Channel{State: channeltypes.OPEN, Ordering: channeltypes.ORDERED, Counterparty: channeltypes.Counterparty{PortId: ibctesting.TransferPort, ChannelId: counterpartyTestzoneChannel}, ConnectionHops: []string{suite.path.EndpointA.ConnectionID}})

// create a channel for the osmosis chain, channel-2 <-> channel-1000
quicksilver.IBCKeeper.ChannelKeeper.SetChannel(suite.chainA.GetContext(), ibctesting.TransferPort, "channel-2", channeltypes.Channel{State: channeltypes.OPEN, Ordering: channeltypes.ORDERED, Counterparty: channeltypes.Counterparty{PortId: ibctesting.TransferPort, ChannelId: counterpartyOsmosisChannel}, ConnectionHops: []string{suite.path.EndpointA.ConnectionID}})

// create a channel channel-3 <-> channel-1500
quicksilver.IBCKeeper.ChannelKeeper.SetChannel(suite.chainA.GetContext(), ibctesting.TransferPort, "channel-3", channeltypes.Channel{State: channeltypes.OPEN, Ordering: channeltypes.ORDERED, Counterparty: channeltypes.Counterparty{PortId: ibctesting.TransferPort, ChannelId: "channel-1500"}, ConnectionHops: []string{suite.path.EndpointA.ConnectionID}})

// create a channel for the umee-types chain, channel-5 <-> channel-2000
quicksilver.IBCKeeper.ChannelKeeper.SetChannel(suite.chainA.GetContext(), ibctesting.TransferPort, "channel-5", channeltypes.Channel{State: channeltypes.OPEN, Ordering: channeltypes.ORDERED, Counterparty: channeltypes.Counterparty{PortId: ibctesting.TransferPort, ChannelId: counterpartyUmeeChannel}, ConnectionHops: []string{suite.path.EndpointA.ConnectionID}})
}

func (suite *KeeperTestSuite) setupTestProtocolData() {
// // connection type for ibc testsuite chainB
suite.addProtocolData(
types.ProtocolDataTypeConnection,
[]byte(fmt.Sprintf("{\"connectionid\": %q,\"chainid\": %q,\"lastepoch\": %d,\"transferchannel\": %q}", suite.path.EndpointB.ConnectionID, "testzone-1", 0, "channel-3")),
)
// connection type for ibc testsuite chainB
suite.addProtocolData(
types.ProtocolDataTypeConnection,
Expand All @@ -419,7 +449,7 @@
// umee-types test chain
suite.addProtocolData(
types.ProtocolDataTypeConnection,
[]byte(fmt.Sprintf("{\"connectionid\": %q,\"chainid\": %q,\"lastepoch\": %d}", umeeTestConnection, umeeTestChain, 0)),
[]byte(fmt.Sprintf("{\"connectionid\": %q,\"chainid\": %q,\"lastepoch\": %d,\"transferchannel\": %q}", umeeTestConnection, umeeTestChain, 0, "channel-5")),
)
// umee-types test reserves
upd, _ := json.Marshal(types.UmeeReservesProtocolData{UmeeProtocolData: types.UmeeProtocolData{Denom: umeeBaseDenom}})
Expand Down Expand Up @@ -459,7 +489,7 @@
// osmosis test chain
suite.addProtocolData(
types.ProtocolDataTypeConnection,
[]byte(fmt.Sprintf("{\"connectionid\": %q,\"chainid\": %q,\"lastepoch\": %d}", "connection-77002", "osmosis-1", 0)),
[]byte(fmt.Sprintf("{\"connectionid\": %q,\"chainid\": %q,\"lastepoch\": %d,\"transferchannel\": %q}", "connection-77002", "osmosis-1", 0, "channel-2")),
)
// osmosis test pool
suite.addProtocolData(
Expand Down
28 changes: 8 additions & 20 deletions x/participationrewards/keeper/proposal_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package keeper_test

import (
"encoding/base64"
"encoding/json"
"fmt"

"github.com/quicksilver-zone/quicksilver/x/participationrewards/keeper"
Expand Down Expand Up @@ -111,47 +110,36 @@ func (suite *KeeperTestSuite) TestHandleAddProtocolDataProposal() {

func (suite *KeeperTestSuite) TestHandleRemoveProtocolDataProposal() {
appA := suite.GetQuicksilverApp(suite.chainA)
ctx := suite.chainA.GetContext()
k := appA.ParticipationRewardsKeeper

// set the protocol data
pd := types.ConnectionProtocolData{
ConnectionID: suite.path.EndpointB.ConnectionID,
ChainID: suite.chainB.ChainID,
LastEpoch: 0,
Prefix: "cosmos",
}

pdString, err := json.Marshal(pd)
err := keeper.MarshalAndSetProtocolData(ctx, k, types.ProtocolDataTypeConnection, &pd)
suite.NoError(err)

ctx := suite.chainA.GetContext()

prop := types.ProtocolData{
Type: types.ProtocolDataType_name[int32(types.ProtocolDataTypeConnection)],
Data: pdString,
}

k := appA.ParticipationRewardsKeeper

k.SetProtocolData(ctx, pd.GenerateKey(), &prop)
// set the protocol data

// check if the protocol data is set
_, found := k.GetProtocolData(ctx, types.ProtocolDataTypeConnection, string(pd.GenerateKey()))
suite.True(found)

msgServer := keeper.NewMsgServerImpl(k)

// submit proposal

// submit proposal to remove the protocol data
proposalMsg := types.MsgGovRemoveProtocolData{
Title: "remove chain B connection string",
Description: "remove the protocol data",
Key: base64.StdEncoding.EncodeToString(pd.GenerateKey()),
Key: base64.StdEncoding.EncodeToString(types.GetProtocolDataKey(types.ProtocolDataTypeConnection, pd.GenerateKey())),
Authority: k.GetGovAuthority(ctx),
}

_, err = msgServer.GovRemoveProtocolData(ctx, &proposalMsg)

suite.NoError(err)

_, found = k.GetProtocolData(ctx, types.ProtocolDataTypeConnection, string(pd.GenerateKey()))
suite.True(found)
suite.False(found)
}
14 changes: 14 additions & 0 deletions x/participationrewards/keeper/protocol_data.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
"encoding/json"
"fmt"

"github.com/cosmos/cosmos-sdk/store/prefix"
Expand Down Expand Up @@ -107,3 +108,16 @@
})
return out
}

// MarshalAndSetProtocolData marshals and sets protocol data given a protocol data type and protocol data.
// It returns an error if the protocol data cannot be marshalled, and panic if can not set the protocol data.
func MarshalAndSetProtocolData(ctx sdk.Context, k *Keeper, datatype types.ProtocolDataType, pd types.ProtocolDataI) error {
pdString, err := json.Marshal(pd)
if err != nil {
k.Logger(ctx).Error("Error marshalling protocol data", "error", err)
return err
}

Check warning on line 119 in x/participationrewards/keeper/protocol_data.go

View check run for this annotation

Codecov / codecov/patch

x/participationrewards/keeper/protocol_data.go#L117-L119

Added lines #L117 - L119 were not covered by tests
storedProtocolData := types.NewProtocolData(datatype.String(), pdString)
k.SetProtocolData(ctx, pd.GenerateKey(), storedProtocolData)
return nil
}
Loading