Skip to content

Commit

Permalink
chore: clean up ProvideCounterparty tx. (#7422)
Browse files Browse the repository at this point in the history
* chore: clean up msgprovidecounterparty.

* Update modules/core/keeper/msg_server_test.go

Co-authored-by: Aditya <[email protected]>

---------

Co-authored-by: Aditya <[email protected]>
  • Loading branch information
DimitrisJim and AdityaSripal authored Oct 9, 2024
1 parent 8e9b8cc commit b509c50
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 118 deletions.
2 changes: 1 addition & 1 deletion modules/core/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (k *Keeper) ProvideCounterparty(goCtx context.Context, msg *packetservertyp
return nil, errorsmod.Wrapf(packetservertypes.ErrInvalidCounterparty, "counterparty must exist for channel %s", msg.ChannelId)
}

counterparty.CounterpartyChannelId = msg.Counterparty.CounterpartyChannelId
counterparty.CounterpartyChannelId = msg.CounterpartyChannelId
k.PacketServerKeeper.SetCounterparty(ctx, msg.ChannelId, counterparty)
// Delete client creator from state as it is not needed after this point.
k.PacketServerKeeper.DeleteCreator(ctx, msg.ChannelId)
Expand Down
10 changes: 4 additions & 6 deletions modules/core/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
channeltypes "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types"
porttypes "github.com/cosmos/ibc-go/v9/modules/core/05-port/types"
commitmenttypes "github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types"
commitmenttypesv2 "github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types/v2"
host "github.com/cosmos/ibc-go/v9/modules/core/24-host"
ibcerrors "github.com/cosmos/ibc-go/v9/modules/core/errors"
"github.com/cosmos/ibc-go/v9/modules/core/exported"
Expand Down Expand Up @@ -1218,7 +1217,7 @@ func (suite *KeeperTestSuite) TestProvideCounterparty() {
"success",
func() {
// set it before handler
suite.chainA.App.GetIBCKeeper().PacketServerKeeper.SetCounterparty(suite.chainA.GetContext(), msg.ChannelId, msg.Counterparty)
suite.chainA.App.GetIBCKeeper().PacketServerKeeper.SetCounterparty(suite.chainA.GetContext(), msg.ChannelId, packetservertypes.NewCounterparty(path.EndpointA.ClientID, "", ibctesting.MerklePath))
},
nil,
},
Expand Down Expand Up @@ -1246,8 +1245,7 @@ func (suite *KeeperTestSuite) TestProvideCounterparty() {
suite.Require().NoError(path.EndpointA.CreateChannel())

signer := path.EndpointA.Chain.SenderAccount.GetAddress().String()
merklePrefix := commitmenttypesv2.NewMerklePath([]byte("mock-key"))
msg = packetservertypes.NewMsgProvideCounterparty(path.EndpointA.ChannelID, path.EndpointB.ChannelID, merklePrefix, signer)
msg = packetservertypes.NewMsgProvideCounterparty(path.EndpointA.ChannelID, path.EndpointB.ChannelID, signer)

tc.malleate()

Expand All @@ -1259,10 +1257,10 @@ func (suite *KeeperTestSuite) TestProvideCounterparty() {
suite.Require().NotNil(resp)
suite.Require().Nil(err)

// Assert counterparty set and creator deleted
// Assert counterparty channel id filled in and creator deleted
counterparty, found := suite.chainA.App.GetIBCKeeper().PacketServerKeeper.GetCounterparty(suite.chainA.GetContext(), path.EndpointA.ChannelID)
suite.Require().True(found)
suite.Require().Equal(counterparty, msg.Counterparty)
suite.Require().Equal(counterparty.CounterpartyChannelId, path.EndpointB.ChannelID)

_, found = suite.chainA.App.GetIBCKeeper().PacketServerKeeper.GetCreator(suite.chainA.GetContext(), path.EndpointA.ClientID)
suite.Require().False(found)
Expand Down
43 changes: 10 additions & 33 deletions modules/core/packet-server/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package cli

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

"github.com/spf13/cobra"

Expand All @@ -12,38 +10,32 @@ import (
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/version"

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

// newProvideCounterpartyCmd defines the command to provide the counterparty to an IBC client.
func newProvideCounterpartyCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "provide-counterparty [client-identifier] [counterparty-client-identifier] [counterparty-merkle-path-prefix]",
Args: cobra.ExactArgs(3),
Short: "provide the counterparty to an IBC client",
Long: `Provide the counterparty to an IBC client specified by its client ID.
Use: "provide-counterparty [channel-identifier] [counterparty-channel-identifier]",
Args: cobra.ExactArgs(2),
Short: "provide the counterparty channel to an IBC channel end",
Long: `Provide the counterparty to an IBC channel end specified by its channel ID.
The [counterparty-merkle-path-prefix] is a comma-separated list of hex-encoded strings.`,
Example: fmt.Sprintf("%s tx %s %s provide-counterparty 07-tendermint-0 07-tendermint-1 696263,657572656b61", version.AppName, exported.ModuleName, types.SubModuleName),
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
}

clientIdentifier := args[0]
counterpartyClientIdentifier := args[1]
counterpartyMerklePathPrefix, err := parseMerklePathPrefix(args[2])
if err != nil {
return err
}
channelID := args[0]
counterpartyChannelID := args[1]

counterparty := types.NewCounterparty(clientIdentifier, counterpartyClientIdentifier, counterpartyMerklePathPrefix)
msg := types.MsgProvideCounterparty{
ChannelId: clientIdentifier,
Counterparty: counterparty,
Signer: clientCtx.GetFromAddress().String(),
ChannelId: channelID,
CounterpartyChannelId: counterpartyChannelID,
Signer: clientCtx.GetFromAddress().String(),
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
},
Expand All @@ -52,18 +44,3 @@ The [counterparty-merkle-path-prefix] is a comma-separated list of hex-encoded s
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
}
12 changes: 5 additions & 7 deletions modules/core/packet-server/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ var (
)

// NewMsgProvideCounterparty creates a new MsgProvideCounterparty instance
func NewMsgProvideCounterparty(clientID, counterpartyChannelID string, merklePathPrefix commitmenttypes.MerklePath, signer string) *MsgProvideCounterparty {
counterparty := NewCounterparty(clientID, counterpartyChannelID, merklePathPrefix)

func NewMsgProvideCounterparty(channelID, counterpartyChannelID string, signer string) *MsgProvideCounterparty {
return &MsgProvideCounterparty{
Signer: signer,
ChannelId: clientID,
Counterparty: counterparty,
Signer: signer,
ChannelId: channelID,
CounterpartyChannelId: counterpartyChannelID,
}
}

Expand All @@ -36,7 +34,7 @@ func (msg *MsgProvideCounterparty) ValidateBasic() error {
return err
}

if err := msg.Counterparty.Validate(); err != nil {
if err := host.ChannelIdentifierValidator(msg.CounterpartyChannelId); err != nil {
return err
}

Expand Down
14 changes: 3 additions & 11 deletions modules/core/packet-server/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,25 @@ func (s *TypesTestSuite) TestMsgProvideCounterpartyValidateBasic() {
ibcerrors.ErrInvalidAddress,
},
{
"failure: invalid client ID",
"failure: invalid channel ID",
func() {
msg.ChannelId = ""
},
host.ErrInvalidID,
},
{
"failure: invalid counterparty client ID",
"failure: invalid counterparty channel ID",
func() {
msg.Counterparty.ClientId = ""
msg.CounterpartyChannelId = ""
},
host.ErrInvalidID,
},
{
"failure: empty key path of counterparty of merkle path prefix",
func() {
msg.Counterparty.MerklePathPrefix.KeyPath = nil
},
types.ErrInvalidCounterparty,
},
}

for _, tc := range testCases {
msg = types.NewMsgProvideCounterparty(
ibctesting.FirstClientID,
ibctesting.SecondClientID,
commitmenttypes.NewMerklePath([]byte("key")),
ibctesting.TestAccAddress,
)

Expand Down
102 changes: 50 additions & 52 deletions modules/core/packet-server/types/tx.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions proto/ibc/core/packetserver/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ option go_package = "github.com/cosmos/ibc-go/v9/modules/core/packet-server/type

import "gogoproto/gogo.proto";
import "ibc/core/commitment/v2/commitment.proto";
import "ibc/core/packetserver/v1/counterparty.proto";
import "cosmos/msg/v1/msg.proto";

// Msg defines the ibc/packetserver Msg service.
Expand All @@ -31,8 +30,8 @@ message MsgProvideCounterparty {

// unique identifier we will use to write all packet messages sent to counterparty
string channel_id = 1;
// counterparty client
Counterparty counterparty = 2 [(gogoproto.nullable) = false];
// counterparty channel identifier
string counterparty_channel_id = 2;
// signer address
string signer = 3;
}
Expand Down
6 changes: 2 additions & 4 deletions testing/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,7 @@ func (endpoint *Endpoint) FreezeClient() {

// ProvideCounterparty will construct and execute a MsgProvideCounterparty on the associated endpoint.
func (endpoint *Endpoint) ProvideCounterparty() (err error) {
merklePath := commitmenttypes.NewMerklePath([]byte("ibc"), []byte(""))

msg := packetservertypes.NewMsgProvideCounterparty(endpoint.ChannelID, endpoint.Counterparty.ChannelID, merklePath, endpoint.Chain.SenderAccount.GetAddress().String())
msg := packetservertypes.NewMsgProvideCounterparty(endpoint.ChannelID, endpoint.Counterparty.ChannelID, endpoint.Chain.SenderAccount.GetAddress().String())

// setup counterparty
_, err = endpoint.Chain.SendMsgs(msg)
Expand All @@ -190,7 +188,7 @@ func (endpoint *Endpoint) ProvideCounterparty() (err error) {

// 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())
msg := packetservertypes.NewMsgCreateChannel(endpoint.ClientID, MerklePath, endpoint.Chain.SenderAccount.GetAddress().String())

// create channel
res, err := endpoint.Chain.SendMsgs(msg)
Expand Down
2 changes: 1 addition & 1 deletion testing/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +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(""))
MerklePath = commitmenttypes.NewMerklePath([]byte("ibc"), []byte(""))
)

0 comments on commit b509c50

Please sign in to comment.