Skip to content

Commit

Permalink
chore: add pluming necessary for testing CreateChannel (#7406)
Browse files Browse the repository at this point in the history
* chore: add pluming necessary for testings.

* chore: docustring for event func.

* chore: wire up call to emit in msg_server

* chore: fix linter mess.

* chore: review touch ups.
  • Loading branch information
DimitrisJim authored Oct 8, 2024
1 parent 6a19337 commit 9264b1c
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 2 deletions.
3 changes: 3 additions & 0 deletions modules/core/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
porttypes "github.com/cosmos/ibc-go/v9/modules/core/05-port/types"
ibcerrors "github.com/cosmos/ibc-go/v9/modules/core/errors"
"github.com/cosmos/ibc-go/v9/modules/core/internal/telemetry"
packetserverkeeper "github.com/cosmos/ibc-go/v9/modules/core/packet-server/keeper"
packetservertypes "github.com/cosmos/ibc-go/v9/modules/core/packet-server/types"
coretypes "github.com/cosmos/ibc-go/v9/modules/core/types"
)
Expand Down Expand Up @@ -155,6 +156,8 @@ func (k *Keeper) CreateChannel(goCtx context.Context, msg *packetservertypes.Msg

k.ClientKeeper.SetCreator(ctx, channelID, msg.Signer)

packetserverkeeper.EmitCreateChannelEvent(goCtx, channelID)

return &packetservertypes.MsgCreateChannelResponse{ChannelId: channelID}, nil
}

Expand Down
25 changes: 25 additions & 0 deletions modules/core/packet-server/keeper/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package keeper

import (
"context"

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

"github.com/cosmos/ibc-go/v9/modules/core/packet-server/types"
)

// EmitCreateChannelEvent emits a channel create event.
func EmitCreateChannelEvent(ctx context.Context, channelID string) {
sdkCtx := sdk.UnwrapSDKContext(ctx)

sdkCtx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeCreateChannel,
sdk.NewAttribute(types.AttributeKeyChannelID, channelID),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
),
})
}
19 changes: 19 additions & 0 deletions modules/core/packet-server/types/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package types

import (
"fmt"

ibcexported "github.com/cosmos/ibc-go/v9/modules/core/exported"
)

// IBC channel events
const (
AttributeKeyChannelID = "channel_id"
)

// IBC channel events vars
var (
EventTypeCreateChannel = "create_channel"

AttributeValueCategory = fmt.Sprintf("%s_%s", ibcexported.ModuleName, SubModuleName)
)
16 changes: 16 additions & 0 deletions testing/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,22 @@ func (endpoint *Endpoint) ProvideCounterparty() (err error) {
return err
}

// CreateChannel will construct and execute a new MsgCreateChannel on the associated endpoint.
func (endpoint *Endpoint) CreateChannel() (err error) {
msg := packetservertypes.NewMsgCreateChannel(endpoint.ClientID, merklePath, endpoint.Chain.SenderAccount.GetAddress().String())

// create channel
res, err := endpoint.Chain.SendMsgs(msg)
if err != nil {
return err
}

endpoint.ChannelID, err = ParseChannelIDFromEvents(res.Events)
require.NoError(endpoint.Chain.TB, err)

return nil
}

// UpgradeChain will upgrade a chain's chainID to the next revision number.
// It will also update the counterparty client.
// TODO: implement actual upgrade chain functionality via scheduling an upgrade
Expand Down
5 changes: 3 additions & 2 deletions testing/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types"
connectiontypes "github.com/cosmos/ibc-go/v9/modules/core/03-connection/types"
channeltypes "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types"
packetservertypes "github.com/cosmos/ibc-go/v9/modules/core/packet-server/types"
)

// ParseClientIDFromEvents parses events emitted from a MsgCreateClient and returns the
Expand Down Expand Up @@ -44,10 +45,10 @@ func ParseConnectionIDFromEvents(events []abci.Event) (string, error) {
}

// ParseChannelIDFromEvents parses events emitted from a MsgChannelOpenInit or
// MsgChannelOpenTry and returns the channel identifier.
// MsgChannelOpenTry or a MsgCreateChannel and returns the channel identifier.
func ParseChannelIDFromEvents(events []abci.Event) (string, error) {
for _, ev := range events {
if ev.Type == channeltypes.EventTypeChannelOpenInit || ev.Type == channeltypes.EventTypeChannelOpenTry {
if ev.Type == packetservertypes.EventTypeCreateChannel || ev.Type == channeltypes.EventTypeChannelOpenInit || ev.Type == channeltypes.EventTypeChannelOpenTry {
if attribute, found := attributeByKey(ev.Attributes, channeltypes.AttributeKeyChannelID); found {
return attribute.Value, nil
}
Expand Down
16 changes: 16 additions & 0 deletions testing/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ func (path *Path) Setup() {
// This is all that is necessary for path setup with the Eureka (V2) protocol
func (path *Path) SetupV2() {
path.SetupClients()

// path.CreateChannelsV2()

path.SetupCounterparties()
}

Expand Down Expand Up @@ -252,6 +255,19 @@ func (path *Path) CreateChannels() {
}
}

// CreateChannelsV2 initializes two channel endpoints by executing CreateChannel on both chainA and chainB.
func (path *Path) CreateChannelsV2() {
err := path.EndpointA.CreateChannel()
if err != nil {
panic(err)
}

err = path.EndpointB.CreateChannel()
if err != nil {
panic(err)
}
}

// EnableFeeOnPath enables fee on a channel given a path.
func EnableFeeOnPath(path *Path) *Path {
path.EndpointA.ChannelConfig.Version = ibcmock.MockFeeVersion
Expand Down
1 change: 1 addition & 0 deletions testing/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,5 @@ var (
prefix = commitmenttypes.NewMerklePrefix([]byte("ibc"))
// unusedHash is a placeholder hash used for testing.
unusedHash = tmhash.Sum([]byte{0x00})
merklePath = commitmenttypes.NewMerklePath([]byte("ibc"), []byte(""))
)

0 comments on commit 9264b1c

Please sign in to comment.