Skip to content

Commit

Permalink
Generic 02-client cli cmds, removes light client specific ones (#8259)
Browse files Browse the repository at this point in the history
* remove old light client cli cmds and replace with generic ones housed in client

* remove print and add client cli getter cmds

* use any as arguments

* revert back to single pub key

* fix tests

* typo

* remove todo

* add upgrade cli cmd cc @AdityaSripal

* register upgrade cmd
  • Loading branch information
colin-axner authored and dudong2 committed Mar 11, 2022
1 parent d36ab89 commit 640bb36
Show file tree
Hide file tree
Showing 11 changed files with 289 additions and 227 deletions.
18 changes: 14 additions & 4 deletions x/auth/client/rest/rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,10 +496,20 @@ func (s *IntegrationTestSuite) testQueryIBCTx(txRes sdk.TxResponse, cmd *cobra.C
func (s *IntegrationTestSuite) TestLegacyRestErrMessages() {
val := s.network.Validators[0]

// Write client state json to temp file, used for an IBC message.
// Generated by printing the result of cdc.MarshalIntefaceJSON on
// a solo machine client state
clientStateJSON := testutil.WriteToNewTempFile(
s.T(),
`{"@type":"/ibc.lightclients.solomachine.v1.ClientState","sequence":"1","frozen_sequence":"0","consensus_state":{"public_key":{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"AtK50+5pJOoaa04qqAqrnyAqsYrwrR/INnA6UPIaYZlp"},"diversifier":"testing","timestamp":"10"},"allow_update_after_proposal":false}`,
)

// Write consensus json to temp file, used for an IBC message.
// Generated by printing the result of cdc.MarshalIntefaceJSON on
// a solo machine consensus state
consensusJSON := testutil.WriteToNewTempFile(
s.T(),
`{"public_key":{"@type":"/lbm.crypto.secp256k1.PubKey","key":"A/3SXL2ONYaOkxpdR5P8tHTlSlPv1AwQwSFxKRee5JQW"},"diversifier":"diversifier","timestamp":"10"}`,
`{"@type":"/ibc.lightclients.solomachine.v1.ConsensusState","public_key":{"@type":"/lbm.crypto.secp256k1.PubKey","key":"AtK50+5pJOoaa04qqAqrnyAqsYrwrR/INnA6UPIaYZlp"},"diversifier":"testing","timestamp":"10"}`,
)

testCases := []struct {
Expand All @@ -525,10 +535,10 @@ func (s *IntegrationTestSuite) TestLegacyRestErrMessages() {
},
{
"Successful IBC message",
ibcsolomachinecli.NewCreateClientCmd(),
ibcclientcli.NewCreateClientCmd(),
[]string{
"1", // dummy sequence
consensusJSON.Name(), // path to consensus json,
clientStateJSON.Name(), // path to client state json
consensusJSON.Name(), // path to consensus json,
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
Expand Down
20 changes: 20 additions & 0 deletions x/ibc/core/02-client/client/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,23 @@ func GetQueryCmd() *cobra.Command {

return queryCmd
}

// NewTxCmd returns the command to create and handle IBC clients
func NewTxCmd() *cobra.Command {
txCmd := &cobra.Command{
Use: types.SubModuleName,
Short: "IBC client transaction subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}

txCmd.AddCommand(
NewCreateClientCmd(),
NewUpdateClientCmd(),
NewSubmitMisbehaviourCmd(),
NewUpgradeClientCmd(),
)

return txCmd
}
6 changes: 2 additions & 4 deletions x/ibc/core/02-client/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,11 @@ func GetCmdQueryHeader() *cobra.Command {
if err != nil {
return err
}
header, height, err := utils.QueryOstraconHeader(clientCtx)
header, _, err := utils.QueryOstraconHeader(clientCtx)
if err != nil {
return err
}

clientCtx = clientCtx.WithHeight(height)
return clientCtx.PrintProto(&header)
},
}
Expand All @@ -222,12 +221,11 @@ func GetCmdNodeConsensusState() *cobra.Command {
if err != nil {
return err
}
state, height, err := utils.QueryNodeConsensusState(clientCtx)
state, _, err := utils.QueryNodeConsensusState(clientCtx)
if err != nil {
return err
}

clientCtx = clientCtx.WithHeight(height)
return clientCtx.PrintProto(state)
},
}
Expand Down
246 changes: 246 additions & 0 deletions x/ibc/core/02-client/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
package cli

import (
"fmt"
"io/ioutil"

"github.com/pkg/errors"
"github.com/spf13/cobra"

"github.com/line/lbm-sdk/client"
"github.com/line/lbm-sdk/client/flags"
"github.com/line/lbm-sdk/client/tx"
"github.com/line/lbm-sdk/codec"
"github.com/line/lbm-sdk/version"
"github.com/line/lbm-sdk/x/ibc/core/02-client/types"
"github.com/line/lbm-sdk/x/ibc/core/exported"
)

// NewCreateClientCmd defines the command to create a new IBC light client.
func NewCreateClientCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create [path/to/client_state.json] [path/to/consensus_state.json]",
Short: "create new IBC client",
Long: `create a new IBC client with the specified client state and consensus state
- ClientState JSON example: {"@type":"/ibc.lightclients.solomachine.v1.ClientState","sequence":"1","frozen_sequence":"0","consensus_state":{"public_key":{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"AtK50+5pJOoaa04qqAqrnyAqsYrwrR/INnA6UPIaYZlp"},"diversifier":"testing","timestamp":"10"},"allow_update_after_proposal":false}
- ConsensusState JSON example: {"@type":"/ibc.lightclients.solomachine.v1.ConsensusState","public_key":{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"AtK50+5pJOoaa04qqAqrnyAqsYrwrR/INnA6UPIaYZlp"},"diversifier":"testing","timestamp":"10"}`,
Example: fmt.Sprintf("%s tx ibc %s create [path/to/client_state.json] [path/to/consensus_state.json] --from node0 --home ../node0/<app>cli --chain-id $CID", version.AppName, types.SubModuleName),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
cdc := codec.NewProtoCodec(clientCtx.InterfaceRegistry)

// attempt to unmarshal client state argument
var clientState exported.ClientState
clientContentOrFileName := args[0]
if err := cdc.UnmarshalInterfaceJSON([]byte(clientContentOrFileName), &clientState); err != nil {

// check for file path if JSON input is not provided
contents, err := ioutil.ReadFile(clientContentOrFileName)
if err != nil {
return errors.Wrap(err, "neither JSON input nor path to .json file for client state were provided")
}

if err := cdc.UnmarshalInterfaceJSON(contents, &clientState); err != nil {
return errors.Wrap(err, "error unmarshalling client state file")
}
}

// attempt to unmarshal consensus state argument
var consensusState exported.ConsensusState
consensusContentOrFileName := args[1]
if err := cdc.UnmarshalInterfaceJSON([]byte(consensusContentOrFileName), &consensusState); err != nil {

// check for file path if JSON input is not provided
contents, err := ioutil.ReadFile(consensusContentOrFileName)
if err != nil {
return errors.Wrap(err, "neither JSON input nor path to .json file for consensus state were provided")
}

if err := cdc.UnmarshalInterfaceJSON(contents, &consensusState); err != nil {
return errors.Wrap(err, "error unmarshalling consensus state file")
}
}

msg, err := types.NewMsgCreateClient(clientState, consensusState, clientCtx.GetFromAddress())
if err != nil {
return err
}

if err := msg.ValidateBasic(); err != nil {
return err
}

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 {
return &cobra.Command{
Use: "update [client-id] [path/to/header.json]",
Short: "update existing client with a header",
Long: "update existing client with a header",
Example: fmt.Sprintf("%s tx ibc %s update [client-id] [path/to/header.json] --from node0 --home ../node0/<app>cli --chain-id $CID", version.AppName, types.SubModuleName),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
clientID := args[0]

cdc := codec.NewProtoCodec(clientCtx.InterfaceRegistry)

var header exported.Header
headerContentOrFileName := args[1]
if err := cdc.UnmarshalInterfaceJSON([]byte(headerContentOrFileName), &header); err != nil {

// check for file path if JSON input is not provided
contents, err := ioutil.ReadFile(headerContentOrFileName)
if err != nil {
return errors.Wrap(err, "neither JSON input nor path to .json file for header were provided")
}

if err := cdc.UnmarshalInterfaceJSON(contents, &header); err != nil {
return errors.Wrap(err, "error unmarshalling header file")
}
}

msg, err := types.NewMsgUpdateClient(clientID, header, clientCtx.GetFromAddress())
if err != nil {
return err
}

if err := msg.ValidateBasic(); err != nil {
return err
}

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

// NewSubmitMisbehaviourCmd defines the command to submit a misbehaviour to prevent
// future updates.
func NewSubmitMisbehaviourCmd() *cobra.Command {
return &cobra.Command{
Use: "misbehaviour [path/to/misbehaviour.json]",
Short: "submit a client misbehaviour",
Long: "submit a client misbehaviour to prevent future updates",
Example: fmt.Sprintf("%s tx ibc %s misbehaviour [path/to/misbehaviour.json] --from node0 --home ../node0/<app>cli --chain-id $CID", version.AppName, types.SubModuleName),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
cdc := codec.NewProtoCodec(clientCtx.InterfaceRegistry)

var misbehaviour exported.Misbehaviour
misbehaviourContentOrFileName := args[0]
if err := cdc.UnmarshalInterfaceJSON([]byte(misbehaviourContentOrFileName), &misbehaviour); err != nil {

// check for file path if JSON input is not provided
contents, err := ioutil.ReadFile(misbehaviourContentOrFileName)
if err != nil {
return errors.Wrap(err, "neither JSON input nor path to .json file for misbehaviour were provided")
}

if err := cdc.UnmarshalInterfaceJSON(contents, misbehaviour); err != nil {
return errors.Wrap(err, "error unmarshalling misbehaviour file")
}
}

msg, err := types.NewMsgSubmitMisbehaviour(misbehaviour.GetClientID(), misbehaviour, clientCtx.GetFromAddress())
if err != nil {
return err
}

if err := msg.ValidateBasic(); err != nil {
return err
}

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

// NewUpgradeClientCmd defines the command to upgrade an IBC light client.
func NewUpgradeClientCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "upgrade [client-identifier] [path/to/client_state.json] [path/to/consensus_state.json] [upgrade-client-proof] [upgrade-consensus-state-proof]",
Short: "upgrade an IBC client",
Long: `upgrade the IBC client associated with the provided client identifier while providing proof committed by the counterparty chain to the new client and consensus states
- ClientState JSON example: {"@type":"/ibc.lightclients.solomachine.v1.ClientState","sequence":"1","frozen_sequence":"0","consensus_state":{"public_key":{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"AtK50+5pJOoaa04qqAqrnyAqsYrwrR/INnA6UPIaYZlp"},"diversifier":"testing","timestamp":"10"},"allow_update_after_proposal":false}
- ConsensusState JSON example: {"@type":"/ibc.lightclients.solomachine.v1.ConsensusState","public_key":{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"AtK50+5pJOoaa04qqAqrnyAqsYrwrR/INnA6UPIaYZlp"},"diversifier":"testing","timestamp":"10"}`,
Example: fmt.Sprintf("%s tx ibc %s upgrade [client-identifier] [path/to/client_state.json] [path/to/consensus_state.json] [client-state-proof] [consensus-state-proof] --from node0 --home ../node0/<app>cli --chain-id $CID", version.AppName, types.SubModuleName),
Args: cobra.ExactArgs(5),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
cdc := codec.NewProtoCodec(clientCtx.InterfaceRegistry)
clientID := args[0]

// attempt to unmarshal client state argument
var clientState exported.ClientState
clientContentOrFileName := args[1]
if err := cdc.UnmarshalInterfaceJSON([]byte(clientContentOrFileName), &clientState); err != nil {

// check for file path if JSON input is not provided
contents, err := ioutil.ReadFile(clientContentOrFileName)
if err != nil {
return errors.Wrap(err, "neither JSON input nor path to .json file for client state were provided")
}

if err := cdc.UnmarshalInterfaceJSON(contents, &clientState); err != nil {
return errors.Wrap(err, "error unmarshalling client state file")
}
}

// attempt to unmarshal consensus state argument
var consensusState exported.ConsensusState
consensusContentOrFileName := args[2]
if err := cdc.UnmarshalInterfaceJSON([]byte(consensusContentOrFileName), &consensusState); err != nil {

// check for file path if JSON input is not provided
contents, err := ioutil.ReadFile(consensusContentOrFileName)
if err != nil {
return errors.Wrap(err, "neither JSON input nor path to .json file for consensus state were provided")
}

if err := cdc.UnmarshalInterfaceJSON(contents, &consensusState); err != nil {
return errors.Wrap(err, "error unmarshalling consensus state file")
}
}

proofUpgradeClient := []byte(args[3])
proofUpgradeConsensus := []byte(args[4])

msg, err := types.NewMsgUpgradeClient(clientID, clientState, consensusState, proofUpgradeClient, proofUpgradeConsensus, clientCtx.GetFromAddress())
if err != nil {
return err
}

if err := msg.ValidateBasic(); err != nil {
return err
}

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

flags.AddTxFlagsToCmd(cmd)

return cmd
}
5 changes: 5 additions & 0 deletions x/ibc/core/02-client/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ func GetQueryCmd() *cobra.Command {
return cli.GetQueryCmd()
}

// GetTxCmd returns the root tx command for 02-client.
func GetTxCmd() *cobra.Command {
return cli.NewTxCmd()
}

// RegisterQueryService registers the gRPC query service for IBC client.
func RegisterQueryService(server grpc.Server, queryServer types.QueryServer) {
types.RegisterQueryServer(server, queryServer)
Expand Down
5 changes: 1 addition & 4 deletions x/ibc/core/client/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
connection "github.com/line/lbm-sdk/x/ibc/core/03-connection"
channel "github.com/line/lbm-sdk/x/ibc/core/04-channel"
host "github.com/line/lbm-sdk/x/ibc/core/24-host"
solomachine "github.com/line/lbm-sdk/x/ibc/light-clients/06-solomachine"
tendermint "github.com/line/lbm-sdk/x/ibc/light-clients/99-ostracon"
)

// GetTxCmd returns the transaction commands for this module
Expand All @@ -23,8 +21,7 @@ func GetTxCmd() *cobra.Command {
}

ibcTxCmd.AddCommand(
solomachine.GetTxCmd(),
tendermint.GetTxCmd(),
ibcclient.GetTxCmd(),
connection.GetTxCmd(),
channel.GetTxCmd(),
)
Expand Down
27 changes: 0 additions & 27 deletions x/ibc/light-clients/06-solomachine/client/cli/cli.go

This file was deleted.

Loading

0 comments on commit 640bb36

Please sign in to comment.