Skip to content

Commit

Permalink
Merge branch 'feat/ibc-eureka' into bznein/7466/MsgRecvPacketValidate…
Browse files Browse the repository at this point in the history
…Basic
  • Loading branch information
bznein authored Oct 15, 2024
2 parents 14ef24a + 1682792 commit 6512e83
Show file tree
Hide file tree
Showing 14 changed files with 241 additions and 170 deletions.
13 changes: 7 additions & 6 deletions modules/core/04-channel/v2/client/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (

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

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

// GetQueryCmd returns the query commands for the IBC packet-server.
// GetQueryCmd returns the query commands for the IBC channel/v2.
func GetQueryCmd() *cobra.Command {
queryCmd := &cobra.Command{
Use: types.SubModuleName,
Short: "IBC packet-server query subcommands",
Short: "IBC channel/v2 query subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
Expand All @@ -25,18 +25,19 @@ func GetQueryCmd() *cobra.Command {
return queryCmd
}

// NewTxCmd returns the command to submit transactions defined for the IBC packet-server.
// NewTxCmd returns the command to submit transactions defined for IBC channel/v2.
func NewTxCmd() *cobra.Command {
txCmd := &cobra.Command{
Use: types.SubModuleName,
Short: "IBC packet-server transaction subcommands",
Short: "IBC channel/v2 transaction subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}

txCmd.AddCommand(
newProvideCounterpartyCmd(),
newCreateChannelTxCmd(),
newProvideCounterpartyTxCmd(),
)

return txCmd
Expand Down
50 changes: 49 additions & 1 deletion modules/core/04-channel/v2/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cli

import (
"encoding/hex"
"fmt"
"strings"

"github.com/spf13/cobra"

Expand All @@ -11,11 +13,42 @@ import (
"github.com/cosmos/cosmos-sdk/version"

"github.com/cosmos/ibc-go/v9/modules/core/04-channel/v2/types"
commitmenttypesv2 "github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types/v2"
"github.com/cosmos/ibc-go/v9/modules/core/exported"
)

// newCreateChannelTxCmd defines the command to create an IBC channel/v2.
func newCreateChannelTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create-channel [client-identifier] [merkle-path-prefix]",
Args: cobra.ExactArgs(2),
Short: "create an IBC channel/v2",
Long: `Creates an IBC channel/v2 using the client identifier representing the counterparty chain and the hex-encoded merkle path prefix under which the counterparty stores packet flow information.`,
Example: fmt.Sprintf("%s tx %s %s create-channel 07-tendermint-0 696263,657572656b61", version.AppName, exported.ModuleName, types.SubModuleName),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

clientID := args[0]
merklePathPrefix, err := parseMerklePathPrefix(args[2])
if err != nil {
return err
}

msg := types.NewMsgCreateChannel(clientID, merklePathPrefix, clientCtx.GetFromAddress().String())

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)
return cmd
}

// newProvideCounterpartyCmd defines the command to provide the counterparty channel identifier to an IBC channel.
func newProvideCounterpartyCmd() *cobra.Command {
func newProvideCounterpartyTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "provide-counterparty [channel-identifier] [counterparty-channel-identifier]",
Args: cobra.ExactArgs(2),
Expand Down Expand Up @@ -43,3 +76,18 @@ func newProvideCounterpartyCmd() *cobra.Command {
flags.AddTxFlagsToCmd(cmd)
return cmd
}

// parseMerklePathPrefix parses a comma-separated list of hex-encoded strings into a MerklePath.
func parseMerklePathPrefix(merklePathPrefixString string) (commitmenttypesv2.MerklePath, error) {
var keyPath [][]byte
hexPrefixes := strings.Split(merklePathPrefixString, ",")
for _, hexPrefix := range hexPrefixes {
prefix, err := hex.DecodeString(hexPrefix)
if err != nil {
return commitmenttypesv2.MerklePath{}, fmt.Errorf("invalid hex merkle path prefix: %w", err)
}
keyPath = append(keyPath, prefix)
}

return commitmenttypesv2.MerklePath{KeyPath: keyPath}, nil
}
2 changes: 1 addition & 1 deletion modules/core/04-channel/v2/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

var _ types.QueryServer = (*queryServer)(nil)

// queryServer implements the packet-server types.QueryServer interface.
// queryServer implements the channel/v2 types.QueryServer interface.
type queryServer struct {
*Keeper
}
Expand Down
25 changes: 2 additions & 23 deletions modules/core/04-channel/v2/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
channeltypesv1 "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types"
channeltypesv2 "github.com/cosmos/ibc-go/v9/modules/core/04-channel/v2/types"
ibcerrors "github.com/cosmos/ibc-go/v9/modules/core/errors"
internalerrors "github.com/cosmos/ibc-go/v9/modules/core/internal/errors"
telemetryv2 "github.com/cosmos/ibc-go/v9/modules/core/internal/v2/telemetry"
coretypes "github.com/cosmos/ibc-go/v9/modules/core/types"
)

var _ channeltypesv2.MsgServer = &Keeper{}
Expand Down Expand Up @@ -127,7 +127,7 @@ func (k *Keeper) RecvPacket(ctx context.Context, msg *channeltypesv2.MsgRecvPack
writeFn()
} else {
// Modify events in cached context to reflect unsuccessful acknowledgement
sdkCtx.EventManager().EmitEvents(convertToErrorEvents(cacheCtx.EventManager().Events()))
sdkCtx.EventManager().EmitEvents(internalerrors.ConvertToErrorEvents(cacheCtx.EventManager().Events()))
}

ack.AcknowledgementResults = append(ack.AcknowledgementResults, channeltypesv2.AcknowledgementResult{
Expand Down Expand Up @@ -233,24 +233,3 @@ func (k *Keeper) ProvideCounterparty(goCtx context.Context, msg *channeltypesv2.

return &channeltypesv2.MsgProvideCounterpartyResponse{}, nil
}

// convertToErrorEvents converts all events to error events by appending the
// error attribute prefix to each event's attribute key.
// TODO: https://github.com/cosmos/ibc-go/issues/7436
func convertToErrorEvents(events sdk.Events) sdk.Events {
if events == nil {
return nil
}

newEvents := make(sdk.Events, len(events))
for i, event := range events {
newAttributes := make([]sdk.Attribute, len(event.Attributes))
for j, attribute := range event.Attributes {
newAttributes[j] = sdk.NewAttribute(coretypes.ErrorAttributeKeyPrefix+attribute.Key, attribute.Value)
}

newEvents[i] = sdk.NewEvent(coretypes.ErrorAttributeKeyPrefix+event.Type, newAttributes...)
}

return newEvents
}
Loading

0 comments on commit 6512e83

Please sign in to comment.