Skip to content

Commit

Permalink
fix CLI JSON param unmarshaling (#5431)
Browse files Browse the repository at this point in the history
  • Loading branch information
fedekunze authored Jan 2, 2020
1 parent 9ec532c commit 73a7e5d
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 129 deletions.
22 changes: 11 additions & 11 deletions x/ibc/02-client/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"os"
"strings"

"github.com/pkg/errors"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client/context"
Expand Down Expand Up @@ -43,14 +45,13 @@ $ %s tx ibc client create [client-id] [path/to/consensus_state.json] --from node

var state exported.ConsensusState
if err := cdc.UnmarshalJSON([]byte(args[1]), &state); err != nil {
fmt.Fprintf(os.Stderr, "failed to unmarshall input into struct, checking for file...")

// check for file path if JSON input is not provided
contents, err := ioutil.ReadFile(args[1])
if err != nil {
return fmt.Errorf("error opening proof file: %v", err)
return errors.New("neither JSON input nor path to .json file were provided")
}
if err := cdc.UnmarshalJSON(contents, &state); err != nil {
return fmt.Errorf("error unmarshalling consensus state file: %v", err)
return errors.Wrap(err, "error unmarshalling consensus state file")
}
}

Expand Down Expand Up @@ -91,14 +92,13 @@ $ %s tx ibc client update [client-id] [path/to/header.json] --from node0 --home

var header exported.Header
if err := cdc.UnmarshalJSON([]byte(args[1]), &header); err != nil {
fmt.Fprintf(os.Stderr, "failed to unmarshall input into struct, checking for file...")

// check for file path if JSON input is not provided
contents, err := ioutil.ReadFile(args[1])
if err != nil {
return fmt.Errorf("error opening proof file: %v", err)
return errors.New("neither JSON input nor path to .json file were provided")
}
if err := cdc.UnmarshalJSON(contents, &header); err != nil {
return fmt.Errorf("error unmarshalling header file: %v", err)
return errors.Wrap(err, "error unmarshalling header file")
}
}

Expand Down Expand Up @@ -137,13 +137,13 @@ $ %s tx ibc client misbehaviour [client-id] [path/to/evidence.json] --from node0
var evidence evidenceexported.Evidence
if err := cdc.UnmarshalJSON([]byte(args[1]), &evidence); err != nil {
fmt.Fprintf(os.Stderr, "failed to unmarshall input into struct, checking for file...")

// check for file path if JSON input is not provided
contents, err := ioutil.ReadFile(args[1])
if err != nil {
return fmt.Errorf("error opening proof file: %v", err)
return errors.New("neither JSON input nor path to .json file were provided")
}
if err := cdc.UnmarshalJSON(contents, &evidence); err != nil {
return fmt.Errorf("error unmarshalling evidence file: %v", err)
return errors.Wrap(err, "error unmarshalling evidence file")
}
}

Expand Down
75 changes: 16 additions & 59 deletions x/ibc/03-connection/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ package cli
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"strings"

"github.com/pkg/errors"

"github.com/spf13/cobra"
"github.com/spf13/viper"

Expand All @@ -18,9 +14,9 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/auth/client/utils"
authutils "github.com/cosmos/cosmos-sdk/x/auth/client/utils"
"github.com/cosmos/cosmos-sdk/x/ibc/03-connection/client/utils"
"github.com/cosmos/cosmos-sdk/x/ibc/03-connection/types"
commitment "github.com/cosmos/cosmos-sdk/x/ibc/23-commitment"
)

// Connection Handshake flags
Expand Down Expand Up @@ -50,24 +46,19 @@ $ %s tx ibc connection open-init [connection-id] [client-id] \
Args: cobra.ExactArgs(5),
RunE: func(cmd *cobra.Command, args []string) error {
inBuf := bufio.NewReader(cmd.InOrStdin())
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(authutils.GetTxEncoder(cdc))
cliCtx := context.NewCLIContextWithInput(inBuf).WithCodec(cdc)

connectionID := args[0]
clientID := args[1]
counterpartyConnectionID := args[2]
counterpartyClientID := args[3]

bz, err := ioutil.ReadFile(args[4])
counterpartyPrefix, err := utils.ParsePrefix(cliCtx.Codec, args[4])
if err != nil {
return err
}

var counterpartyPrefix commitment.Prefix
if err := cdc.UnmarshalJSON(bz, &counterpartyPrefix); err != nil {
return err
}

msg := types.NewMsgConnectionOpenInit(
connectionID, clientID, counterpartyConnectionID, counterpartyClientID,
counterpartyPrefix, cliCtx.GetFromAddress(),
Expand All @@ -77,7 +68,7 @@ $ %s tx ibc connection open-init [connection-id] [client-id] \
return err
}

return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
return authutils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
return cmd
Expand All @@ -103,7 +94,7 @@ $ %s tx ibc connection open-try connection-id] [client-id] \
Args: cobra.ExactArgs(7),
RunE: func(cmd *cobra.Command, args []string) error {
inBuf := bufio.NewReader(cmd.InOrStdin())
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(authutils.GetTxEncoder(cdc))
cliCtx := context.NewCLIContextWithInput(inBuf).
WithCodec(cdc).
WithHeight(viper.GetInt64(flags.FlagHeight))
Expand All @@ -113,29 +104,19 @@ $ %s tx ibc connection open-try connection-id] [client-id] \
counterpartyConnectionID := args[2]
counterpartyClientID := args[3]

prefixBz, err := ioutil.ReadFile(args[4])
counterpartyPrefix, err := utils.ParsePrefix(cliCtx.Codec, args[4])
if err != nil {
return err
}

var counterpartyPrefix commitment.Prefix
if err := cdc.UnmarshalJSON(prefixBz, &counterpartyPrefix); err != nil {
return err
}

// TODO: parse strings?
counterpartyVersions := args[5]

proofBz, err := ioutil.ReadFile(args[6])
proofInit, err := utils.ParseProof(cliCtx.Codec, args[1])
if err != nil {
return err
}

var proofInit commitment.Proof
if err := cdc.UnmarshalJSON(proofBz, &proofInit); err != nil {
return err
}

proofHeight := uint64(cliCtx.Height)
consensusHeight, err := lastHeight(cliCtx)
if err != nil {
Expand All @@ -152,7 +133,7 @@ $ %s tx ibc connection open-try connection-id] [client-id] \
return err
}

return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
return authutils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
return cmd
Expand All @@ -174,17 +155,13 @@ $ %s tx ibc connection open-ack [connection-id] [path/to/proof_try.json] [versio
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
inBuf := bufio.NewReader(cmd.InOrStdin())
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(authutils.GetTxEncoder(cdc))
cliCtx := context.NewCLIContextWithInput(inBuf).WithCodec(cdc)

connectionID := args[0]
proofBz, err := ioutil.ReadFile(args[1])
if err != nil {
return err
}

var proofTry commitment.Proof
if err := cdc.UnmarshalJSON(proofBz, &proofTry); err != nil {
proofTry, err := utils.ParseProof(cliCtx.Codec, args[1])
if err != nil {
return err
}

Expand All @@ -205,7 +182,7 @@ $ %s tx ibc connection open-ack [connection-id] [path/to/proof_try.json] [versio
return err
}

return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
return authutils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
return cmd
Expand All @@ -227,23 +204,18 @@ $ %s tx ibc connection open-confirm [connection-id] [path/to/proof_ack.json]
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
inBuf := bufio.NewReader(cmd.InOrStdin())
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(authutils.GetTxEncoder(cdc))
cliCtx := context.NewCLIContextWithInput(inBuf).
WithCodec(cdc).
WithHeight(viper.GetInt64(flags.FlagHeight))

connectionID := args[0]

proofBz, err := ioutil.ReadFile(args[1])
proofAck, err := utils.ParseProof(cliCtx.Codec, args[1])
if err != nil {
return err
}

var proofAck commitment.Proof
if err := cdc.UnmarshalJSON(proofBz, &proofAck); err != nil {
return err
}

proofHeight := uint64(cliCtx.Height)

msg := types.NewMsgConnectionOpenConfirm(
Expand All @@ -254,7 +226,7 @@ $ %s tx ibc connection open-confirm [connection-id] [path/to/proof_ack.json]
return err
}

return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
return authutils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
return cmd
Expand All @@ -274,18 +246,3 @@ func lastHeight(cliCtx context.CLIContext) (uint64, error) {

return uint64(info.Response.LastBlockHeight), nil
}

func parsePath(cdc *codec.Codec, arg string) (commitment.Prefix, error) {
var path commitment.Prefix
if err := cdc.UnmarshalJSON([]byte(arg), &path); err != nil {
fmt.Fprintf(os.Stderr, "failed to unmarshall input into struct, checking for file...")
contents, err := ioutil.ReadFile(arg)
if err != nil {
return path, errors.Wrap(err, "error opening path file")
}
if err := cdc.UnmarshalJSON(contents, &path); err != nil {
return path, errors.Wrap(err, "error unmarshalling path file")
}
}
return path, nil
}
39 changes: 39 additions & 0 deletions x/ibc/03-connection/client/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ package utils

import (
"fmt"
"io/ioutil"

"github.com/pkg/errors"

abci "github.com/tendermint/tendermint/abci/types"

"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/x/ibc/03-connection/types"
commitment "github.com/cosmos/cosmos-sdk/x/ibc/23-commitment"
)

// QueryAllConnections returns all the connections. It _does not_ return
Expand Down Expand Up @@ -82,3 +87,37 @@ func QueryClientConnections(
connPathsRes := types.NewClientConnectionsResponse(clientID, paths, res.Proof, res.Height)
return connPathsRes, nil
}

// ParsePrefix unmarshals an cmd input argument from a JSON string to a commitment
// Prefix. If the input is not a JSON, it looks for a path to the JSON file.
func ParsePrefix(cdc *codec.Codec, arg string) (commitment.Prefix, error) {
var prefix commitment.Prefix
if err := cdc.UnmarshalJSON([]byte(arg), &prefix); err != nil {
// check for file path if JSON input is not provided
contents, err := ioutil.ReadFile(arg)
if err != nil {
return commitment.Prefix{}, errors.New("neither JSON input nor path to .json file were provided")
}
if err := cdc.UnmarshalJSON(contents, &prefix); err != nil {
return commitment.Prefix{}, errors.Wrap(err, "error unmarshalling commitment prefix")
}
}
return prefix, nil
}

// ParseProof unmarshals an cmd input argument from a JSON string to a commitment
// Proof. If the input is not a JSON, it looks for a path to the JSON file.
func ParseProof(cdc *codec.Codec, arg string) (commitment.Proof, error) {
var proof commitment.Proof
if err := cdc.UnmarshalJSON([]byte(arg), &proof); err != nil {
// check for file path if JSON input is not provided
contents, err := ioutil.ReadFile(arg)
if err != nil {
return commitment.Proof{}, errors.New("neither JSON input nor path to .json file were provided")
}
if err := cdc.UnmarshalJSON(contents, &proof); err != nil {
return commitment.Proof{}, errors.Wrap(err, "error unmarshalling commitment proof")
}
}
return proof, nil
}
Loading

0 comments on commit 73a7e5d

Please sign in to comment.