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

feat: add cli commands for client v2 counterparty #7997

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions modules/core/02-client/client/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func GetQueryCmd() *cobra.Command {

queryCmd.AddCommand(
GetCmdQueryClientStates(),
GetCmdQueryCounterpartyInfo(),
GetCmdQueryClientState(),
GetCmdQueryClientStatus(),
GetCmdQueryConsensusStates(),
Expand All @@ -45,6 +46,7 @@ func NewTxCmd() *cobra.Command {

txCmd.AddCommand(
newCreateClientCmd(),
newAddCounterpartyCmd(),
newUpdateClientCmd(),
newSubmitMisbehaviourCmd(), // Deprecated
newUpgradeClientCmd(),
Expand Down
33 changes: 33 additions & 0 deletions modules/core/02-client/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/cosmos/ibc-go/v10/modules/core/02-client/client/utils"
"github.com/cosmos/ibc-go/v10/modules/core/02-client/types"
clienttypesv2 "github.com/cosmos/ibc-go/v10/modules/core/02-client/v2/types"
ibcexported "github.com/cosmos/ibc-go/v10/modules/core/exported"
)

Expand Down Expand Up @@ -58,6 +59,38 @@ func GetCmdQueryClientStates() *cobra.Command {
return cmd
}

func GetCmdQueryCounterpartyInfo() *cobra.Command {
cmd := &cobra.Command{
Use: "counterparty-info [client-id]",
Short: "Query a client's counterparty info",
Long: "Query a client's counterparty info",
Example: fmt.Sprintf("%s query %s %s counterparty-info [client-id]", version.AppName, ibcexported.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]

queryClient := clienttypesv2.NewQueryClient(clientCtx)
req := &clienttypesv2.QueryCounterpartyInfoRequest{
ClientId: clientID,
}
counterpartyRes, err := queryClient.CounterpartyInfo(cmd.Context(), req)
if err != nil {
return err
}

return clientCtx.PrintProto(counterpartyRes)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}

// GetCmdQueryClientState defines the command to query the state of a client with
// a given id as defined in https://github.com/cosmos/ibc/tree/master/spec/core/ics-002-client-semantics#query
func GetCmdQueryClientState() *cobra.Command {
Expand Down
36 changes: 36 additions & 0 deletions modules/core/02-client/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"encoding/base64"
"fmt"
"os"
"strconv"
Expand All @@ -20,6 +21,7 @@ import (
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"

"github.com/cosmos/ibc-go/v10/modules/core/02-client/types"
clienttypesv2 "github.com/cosmos/ibc-go/v10/modules/core/02-client/v2/types"
"github.com/cosmos/ibc-go/v10/modules/core/exported"
)

Expand Down Expand Up @@ -87,6 +89,40 @@ func newCreateClientCmd() *cobra.Command {
return cmd
}

func newAddCounterpartyCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "add-counterparty [client-id] [counterparty-client-id] [merkle-prefix...]",
Short: "add counterparty to client",
Example: fmt.Sprintf("%s tx ibc %s add-counterparty 07-tendermint-0 client-0 \"aWJj\" \"\"", version.AppName, types.SubModuleName),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is kinda ugly, open to alternative ways to do this...

Args: cobra.MinimumNArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

clientID := args[0]
counterpartyClientID := args[1]

var merklePrefix [][]byte
for _, base64EncodedPathPart := range args[2:] {
pathPart, err := base64.StdEncoding.DecodeString(base64EncodedPathPart)
if err != nil {
return err
}
merklePrefix = append(merklePrefix, pathPart)
}

msg := clienttypesv2.NewMsgRegisterCounterparty(clientID, merklePrefix, counterpartyClientID, clientCtx.GetFromAddress().String())

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

flags.AddTxFlagsToCmd(cmd)
return cmd
}

// newUpdateClientCmd defines the command to update an IBC client.
func newUpdateClientCmd() *cobra.Command {
cmd := &cobra.Command{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func GetSimApp(chain *ibctesting.TestChain) *simapp.SimApp {
// setupTestingApp provides the duplicated simapp which is specific to the 08-wasm module on chain creation.
func setupTestingApp() (ibctesting.TestingApp, map[string]json.RawMessage) {
db := dbm.NewMemDB()
app := simapp.NewSimApp(log.NewNopLogger(), db, nil, true, simtestutil.EmptyAppOptions{}, nil)
app := simapp.NewUnitTestSimApp(log.NewNopLogger(), db, nil, true, simtestutil.EmptyAppOptions{}, nil)
return app, app.DefaultGenesis()
}

Expand Down
4 changes: 2 additions & 2 deletions modules/light-clients/08-wasm/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func init() {
// setupTestingApp provides the duplicated simapp which is specific to the 08-wasm module on chain creation.
func setupTestingApp() (ibctesting.TestingApp, map[string]json.RawMessage) {
db := dbm.NewMemDB()
app := simapp.NewSimApp(log.NewNopLogger(), db, nil, true, simtestutil.EmptyAppOptions{}, nil)
app := simapp.NewUnitTestSimApp(log.NewNopLogger(), db, nil, true, simtestutil.EmptyAppOptions{}, nil)
return app, app.DefaultGenesis()
}

Expand Down Expand Up @@ -113,7 +113,7 @@ func (suite *KeeperTestSuite) setupWasmWithMockVM() (ibctesting.TestingApp, map[
})

db := dbm.NewMemDB()
app := simapp.NewSimApp(log.NewNopLogger(), db, nil, true, simtestutil.EmptyAppOptions{}, suite.mockVM)
app := simapp.NewUnitTestSimApp(log.NewNopLogger(), db, nil, true, simtestutil.EmptyAppOptions{}, suite.mockVM)

// reset DefaultTestingAppInit to its original value
ibctesting.DefaultTestingAppInit = setupTestingApp
Expand Down
32 changes: 30 additions & 2 deletions modules/light-clients/08-wasm/testing/simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"math/rand"
"os"
"path"
"path/filepath"
"strconv"

Expand Down Expand Up @@ -230,14 +231,41 @@ func init() {
DefaultNodeHome = filepath.Join(userHomeDir, ".simapp")
}

func NewUnitTestSimApp(
logger log.Logger,
db dbm.DB,
traceStore io.Writer,
loadLatest bool,
appOpts servertypes.AppOptions,
mockVM wasmtypes.WasmEngine,
baseAppOptions ...func(*baseapp.BaseApp),
) *SimApp {
wasmDir := path.Join("ibc_08-wasm_client_data", strconv.Itoa(rand.Intn(10000)))
return newSimApp(logger, db, traceStore, loadLatest, appOpts, mockVM, wasmDir, baseAppOptions...)
}

func NewBinarySimApp(
logger log.Logger,
db dbm.DB,
traceStore io.Writer,
loadLatest bool,
appOpts servertypes.AppOptions,
mockVM wasmtypes.WasmEngine,
baseAppOptions ...func(*baseapp.BaseApp),
) *SimApp {
wasmDir := "ibc_08-wasm_client_data"
return newSimApp(logger, db, traceStore, loadLatest, appOpts, mockVM, wasmDir, baseAppOptions...)
}

// NewSimApp returns a reference to an initialized SimApp.
func NewSimApp(
func newSimApp(
logger log.Logger,
db dbm.DB,
traceStore io.Writer,
loadLatest bool,
appOpts servertypes.AppOptions,
mockVM wasmtypes.WasmEngine,
wasmDir string,
baseAppOptions ...func(*baseapp.BaseApp),
) *SimApp {
interfaceRegistry, _ := types.NewInterfaceRegistryWithOptions(types.InterfaceRegistryOptions{
Expand Down Expand Up @@ -425,7 +453,7 @@ func NewSimApp(
// different VM instances running in the same data directory. In production environments, the
// appended random string is not needed.
wasmConfig := wasmtypes.WasmConfig{
DataDir: filepath.Join(homePath, "ibc_08-wasm_client_data", strconv.Itoa(rand.Intn(10000))),
DataDir: filepath.Join(homePath, wasmDir),
SupportedCapabilities: []string{"iterator"},
ContractDebugMode: false,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ import (
func NewRootCmd() *cobra.Command {
// we "pre"-instantiate the application for getting the injected/configured encoding configuration
// note, this is not necessary when using app wiring, as depinject can be directly used (see root_v2.go)
tempApp := simapp.NewSimApp(log.NewNopLogger(), dbm.NewMemDB(), nil, true, simtestutil.NewAppOptionsWithFlagHome(tempDir()), nil)
tempApp := simapp.NewBinarySimApp(log.NewNopLogger(), dbm.NewMemDB(), nil, true, simtestutil.NewAppOptionsWithFlagHome(tempDir()), nil)
encodingConfig := params.EncodingConfig{
InterfaceRegistry: tempApp.InterfaceRegistry(),
Codec: tempApp.AppCodec(),
Expand Down Expand Up @@ -313,7 +313,7 @@ func newApp(
) servertypes.Application {
baseappOptions := server.DefaultBaseappOptions(appOpts)

return simapp.NewSimApp(
return simapp.NewBinarySimApp(
logger, db, traceStore, true,
appOpts, nil,
baseappOptions...,
Expand Down Expand Up @@ -350,13 +350,13 @@ func appExport(
appOpts = viperAppOpts

if height != -1 {
simApp = simapp.NewSimApp(logger, db, traceStore, false, appOpts, nil)
simApp = simapp.NewBinarySimApp(logger, db, traceStore, false, appOpts, nil)

if err := simApp.LoadHeight(height); err != nil {
return servertypes.ExportedApp{}, err
}
} else {
simApp = simapp.NewSimApp(logger, db, traceStore, true, appOpts, nil)
simApp = simapp.NewBinarySimApp(logger, db, traceStore, true, appOpts, nil)
}

return simApp.ExportAppStateAndValidators(forZeroHeight, jailAllowedAddrs, modulesToExport)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func setup(tb testing.TB, chainID string, withGenesis bool, invCheckPeriod uint,
appOptions := make(simtestutil.AppOptionsMap, 0)
appOptions[flags.FlagHome] = nodeHome // ensure unique folder
appOptions[server.FlagInvCheckPeriod] = invCheckPeriod
app := NewSimApp(log.NewNopLogger(), db, nil, true, appOptions, mockVM, bam.SetChainID(chainID), bam.SetSnapshot(snapshotStore, snapshottypes.SnapshotOptions{KeepRecent: 2}))
app := NewUnitTestSimApp(log.NewNopLogger(), db, nil, true, appOptions, mockVM, bam.SetChainID(chainID), bam.SetSnapshot(snapshotStore, snapshottypes.SnapshotOptions{KeepRecent: 2}))

if withGenesis {
return app, app.DefaultGenesis()
Expand Down
2 changes: 1 addition & 1 deletion modules/light-clients/08-wasm/types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ func GetSimApp(chain *ibctesting.TestChain) *simapp.SimApp {
// setupTestingApp provides the duplicated simapp which is specific to the 08-wasm module on chain creation.
func setupTestingApp() (ibctesting.TestingApp, map[string]json.RawMessage) {
db := dbm.NewMemDB()
app := simapp.NewSimApp(log.NewNopLogger(), db, nil, true, simtestutil.EmptyAppOptions{}, nil)
app := simapp.NewUnitTestSimApp(log.NewNopLogger(), db, nil, true, simtestutil.EmptyAppOptions{}, nil)
return app, app.DefaultGenesis()
}
4 changes: 2 additions & 2 deletions modules/light-clients/08-wasm/wasm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func GetSimApp(chain *ibctesting.TestChain) *simapp.SimApp {
// setupTestingApp provides the duplicated simapp which is specific to the 08-wasm module on chain creation.
func setupTestingApp() (ibctesting.TestingApp, map[string]json.RawMessage) {
db := dbm.NewMemDB()
app := simapp.NewSimApp(log.NewNopLogger(), db, nil, true, simtestutil.EmptyAppOptions{}, nil)
app := simapp.NewUnitTestSimApp(log.NewNopLogger(), db, nil, true, simtestutil.EmptyAppOptions{}, nil)
return app, app.DefaultGenesis()
}

Expand Down Expand Up @@ -109,7 +109,7 @@ func (suite *WasmTestSuite) setupWasmWithMockVM() (ibctesting.TestingApp, map[st
})

db := dbm.NewMemDB()
app := simapp.NewSimApp(log.NewNopLogger(), db, nil, true, simtestutil.EmptyAppOptions{}, suite.mockVM)
app := simapp.NewUnitTestSimApp(log.NewNopLogger(), db, nil, true, simtestutil.EmptyAppOptions{}, suite.mockVM)

// reset DefaultTestingAppInit to its original value
ibctesting.DefaultTestingAppInit = setupTestingApp
Expand Down
Loading