Skip to content

Commit

Permalink
Merge branch 'feat/ibc-eureka' into damian/7414-timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
damiannolan committed Oct 17, 2024
2 parents 990fa7e + 4074c58 commit 51fc8eb
Show file tree
Hide file tree
Showing 57 changed files with 2,641 additions and 2,109 deletions.
4 changes: 2 additions & 2 deletions e2e/testsuite/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,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"
channeltypesv2 "github.com/cosmos/ibc-go/v9/modules/core/04-channel/v2/types"
solomachine "github.com/cosmos/ibc-go/v9/modules/light-clients/06-solomachine"
ibctmtypes "github.com/cosmos/ibc-go/v9/modules/light-clients/07-tendermint"
ibctesting "github.com/cosmos/ibc-go/v9/testing"
Expand Down Expand Up @@ -70,7 +70,7 @@ func codecAndEncodingConfig() (*codec.ProtoCodec, testutil.TestEncodingConfig) {
connectiontypes.RegisterInterfaces(cfg.InterfaceRegistry)
ibctmtypes.RegisterInterfaces(cfg.InterfaceRegistry)
wasmtypes.RegisterInterfaces(cfg.InterfaceRegistry)
packetservertypes.RegisterInterfaces(cfg.InterfaceRegistry)
channeltypesv2.RegisterInterfaces(cfg.InterfaceRegistry)

// all other types
upgradetypes.RegisterInterfaces(cfg.InterfaceRegistry)
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@ import (
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/version"

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

// getCmdQueryChannel defines the command to query the client information (creator and channel) for the given client ID.
// getCmdQueryChannel defines the command to query the channel information (creator and channel) for the given channel ID.
func getCmdQueryChannel() *cobra.Command {
cmd := &cobra.Command{
Use: "client [client-id]",
Short: "Query the information of a client.",
Long: "Query the client information (creator and channel) for the provided client ID.",
Example: fmt.Sprintf("%s query %s %s client [client-id]", version.AppName, exported.ModuleName, types.SubModuleName),
Use: "channel [channel-id]",
Short: "Query the information of a channel.",
Long: "Query the channel information (creator and channel) for the provided channel ID.",
Example: fmt.Sprintf("%s query %s %s channel [channel-id]", version.AppName, exported.ModuleName, types.SubModuleName),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
clientID := args[0]
channelID := args[0]

queryClient := types.NewQueryClient(clientCtx)

req := &types.QueryChannelRequest{ChannelId: clientID}
req := &types.QueryChannelRequest{ChannelId: channelID}

res, err := queryClient.Channel(cmd.Context(), req)
if err != nil {
Expand Down
93 changes: 93 additions & 0 deletions modules/core/04-channel/v2/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package cli

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

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"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 newProvideCounterpartyTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "provide-counterparty [channel-identifier] [counterparty-channel-identifier]",
Args: cobra.ExactArgs(2),
Short: "provide the counterparty channel id to an IBC channel",
Long: `Provide the counterparty channel id to an IBC channel specified by its channel ID.`,
Example: fmt.Sprintf("%s tx %s %s provide-counterparty channel-0 channel-1", version.AppName, exported.ModuleName, types.SubModuleName),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

channelID := args[0]
counterpartyChannelID := args[1]

msg := types.MsgProvideCounterparty{
ChannelId: channelID,
CounterpartyChannelId: counterpartyChannelID,
Signer: clientCtx.GetFromAddress().String(),
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
},
}

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
}
23 changes: 23 additions & 0 deletions modules/core/04-channel/v2/keeper/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package keeper
import (
"context"

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

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

Expand All @@ -25,3 +27,24 @@ func EmitAcknowledgePacketEvents(ctx context.Context, packet channeltypesv2.Pack
func EmitTimeoutPacketEvents(ctx context.Context, packet channeltypesv2.Packet) {
// TODO: https://github.com/cosmos/ibc-go/issues/7386
}

// EmitWriteAcknowledgementEvents emits events for WriteAcknowledgement.
func EmitWriteAcknowledgementEvents(ctx context.Context, packet channeltypesv2.Packet, ack channeltypesv2.Acknowledgement) {
// TODO: https://github.com/cosmos/ibc-go/issues/7386
}

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

sdkCtx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
channeltypesv2.EventTypeCreateChannel,
sdk.NewAttribute(channeltypesv2.AttributeKeyChannelID, channelID),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, channeltypesv2.AttributeValueCategory),
),
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (

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

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

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
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/cosmos/ibc-go/v9/modules/core/04-channel/v2/keeper"
"github.com/cosmos/ibc-go/v9/modules/core/04-channel/v2/types"
commitmenttypes "github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types"
"github.com/cosmos/ibc-go/v9/modules/core/packet-server/keeper"
"github.com/cosmos/ibc-go/v9/modules/core/packet-server/types"
ibctesting "github.com/cosmos/ibc-go/v9/testing"
)

Expand All @@ -28,8 +28,8 @@ func (suite *KeeperTestSuite) TestQueryChannel() {
"success",
func() {
ctx := suite.chainA.GetContext()
suite.chainA.App.GetIBCKeeper().PacketServerKeeper.SetCreator(ctx, ibctesting.FirstChannelID, expCreator)
suite.chainA.App.GetIBCKeeper().PacketServerKeeper.SetChannel(ctx, ibctesting.FirstChannelID, expChannel)
suite.chainA.App.GetIBCKeeper().ChannelKeeperV2.SetCreator(ctx, ibctesting.FirstChannelID, expCreator)
suite.chainA.App.GetIBCKeeper().ChannelKeeperV2.SetChannel(ctx, ibctesting.FirstChannelID, expChannel)

req = &types.QueryChannelRequest{
ChannelId: ibctesting.FirstChannelID,
Expand All @@ -42,7 +42,7 @@ func (suite *KeeperTestSuite) TestQueryChannel() {
func() {
expCreator = ""

suite.chainA.App.GetIBCKeeper().PacketServerKeeper.SetChannel(suite.chainA.GetContext(), ibctesting.FirstChannelID, expChannel)
suite.chainA.App.GetIBCKeeper().ChannelKeeperV2.SetChannel(suite.chainA.GetContext(), ibctesting.FirstChannelID, expChannel)

req = &types.QueryChannelRequest{
ChannelId: ibctesting.FirstChannelID,
Expand All @@ -55,7 +55,7 @@ func (suite *KeeperTestSuite) TestQueryChannel() {
func() {
expChannel = types.Channel{}

suite.chainA.App.GetIBCKeeper().PacketServerKeeper.SetCreator(suite.chainA.GetContext(), ibctesting.FirstChannelID, expCreator)
suite.chainA.App.GetIBCKeeper().ChannelKeeperV2.SetCreator(suite.chainA.GetContext(), ibctesting.FirstChannelID, expCreator)

req = &types.QueryChannelRequest{
ChannelId: ibctesting.FirstChannelID,
Expand Down Expand Up @@ -100,7 +100,7 @@ func (suite *KeeperTestSuite) TestQueryChannel() {

tc.malleate()

queryServer := keeper.NewQueryServer(suite.chainA.GetSimApp().IBCKeeper.PacketServerKeeper)
queryServer := keeper.NewQueryServer(suite.chainA.GetSimApp().IBCKeeper.ChannelKeeperV2)
res, err := queryServer.Channel(suite.chainA.GetContext(), req)

expPass := tc.expError == nil
Expand Down
41 changes: 34 additions & 7 deletions modules/core/04-channel/v2/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,29 @@ func (k *Keeper) GetChannel(ctx context.Context, channelID string) (types.Channe
return channel, true
}

// GetCreator returns the creator of the channel.
func (k *Keeper) GetCreator(ctx context.Context, channelID string) (string, bool) {
sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/5917
bz := k.ChannelStore(sdkCtx, channelID).Get([]byte(types.CreatorKey))
if len(bz) == 0 {
return "", false
}

return string(bz), true
}

// SetCreator sets the creator of the channel.
func (k *Keeper) SetCreator(ctx context.Context, channelID, creator string) {
sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/5917
k.ChannelStore(sdkCtx, channelID).Set([]byte(types.CreatorKey), []byte(creator))
}

// DeleteCreator deletes the creator associated with the channel.
func (k *Keeper) DeleteCreator(ctx context.Context, channelID string) {
sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/5917
k.ChannelStore(sdkCtx, channelID).Delete([]byte(types.CreatorKey))
}

// GetPacketReceipt returns the packet receipt from the packet receipt path based on the channelID and sequence.
func (k *Keeper) GetPacketReceipt(ctx context.Context, channelID string, sequence uint64) ([]byte, bool) {
store := k.storeService.OpenKVStore(ctx)
Expand Down Expand Up @@ -112,6 +135,16 @@ func (k *Keeper) SetPacketReceipt(ctx context.Context, channelID string, sequenc
}
}

// GetPacketAcknowledgement fetches the packet acknowledgement from the store.
func (k *Keeper) GetPacketAcknowledgement(ctx context.Context, channelID string, sequence uint64) []byte {
store := k.storeService.OpenKVStore(ctx)
bz, err := store.Get(hostv2.PacketAcknowledgementKey(channelID, sequence))
if err != nil {
panic(err)
}
return bz
}

// SetPacketAcknowledgement writes the acknowledgement hash under the acknowledgement path
// This is a public path that is standardized by the IBC V2 specification.
func (k *Keeper) SetPacketAcknowledgement(ctx context.Context, channelID string, sequence uint64, ackHash []byte) {
Expand All @@ -123,13 +156,7 @@ func (k *Keeper) SetPacketAcknowledgement(ctx context.Context, channelID string,

// HasPacketAcknowledgement check if the packet ack hash is already on the store.
func (k *Keeper) HasPacketAcknowledgement(ctx context.Context, channelID string, sequence uint64) bool {
store := k.storeService.OpenKVStore(ctx)
found, err := store.Has(hostv2.PacketAcknowledgementKey(channelID, sequence))
if err != nil {
panic(err)
}

return found
return len(k.GetPacketAcknowledgement(ctx, channelID, sequence)) > 0
}

// GetPacketCommitment returns the packet commitment hash under the commitment path.
Expand Down
Loading

0 comments on commit 51fc8eb

Please sign in to comment.