From dca9dcd2764896bcaffd71686911c8fa60801bc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?colin=20axn=C3=A9r?= <25233464+colin-axner@users.noreply.github.com> Date: Wed, 28 Oct 2020 14:31:22 +0100 Subject: [PATCH] Fix panic in context when setting nodeURI (#7699) * Fix panic in context Move URI parsing outside of context. WithNodeURI will only set the nodeURI. To set the Client in the context, WithClient must be called. * add changelog * add deleted space back --- CHANGELOG.md | 1 + client/cmd.go | 8 ++++++++ client/context.go | 7 ------- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1003e6bd00..fd6ba83dd4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -60,6 +60,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Bug Fixes * (kvstore) [\#7415](https://github.com/cosmos/cosmos-sdk/pull/7415) Allow new stores to be registered during on-chain upgrades. +* (client) [\#7699](https://github.com/cosmos/cosmos-sdk/pull/7699) Fix panic in context when setting invalid nodeURI. `WithNodeURI` does not set the `Client` in the context. ## [v0.40.0-rc0](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.40.0-rc0) - 2020-10-13 diff --git a/client/cmd.go b/client/cmd.go index 712a792740..8395fb49b5 100644 --- a/client/cmd.go +++ b/client/cmd.go @@ -8,6 +8,7 @@ import ( "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/tendermint/tendermint/libs/cli" + rpchttp "github.com/tendermint/tendermint/rpc/client/http" "github.com/cosmos/cosmos-sdk/client/flags" sdk "github.com/cosmos/cosmos-sdk/types" @@ -131,6 +132,13 @@ func ReadPersistentCommandFlags(clientCtx Context, flagSet *pflag.FlagSet) (Cont rpcURI, _ := flagSet.GetString(flags.FlagNode) if rpcURI != "" { clientCtx = clientCtx.WithNodeURI(rpcURI) + + client, err := rpchttp.New(rpcURI, "/websocket") + if err != nil { + return clientCtx, err + } + + clientCtx = clientCtx.WithClient(client) } } diff --git a/client/context.go b/client/context.go index 96c909478b..7afddcf431 100644 --- a/client/context.go +++ b/client/context.go @@ -10,7 +10,6 @@ import ( "github.com/gogo/protobuf/proto" "github.com/pkg/errors" rpcclient "github.com/tendermint/tendermint/rpc/client" - rpchttp "github.com/tendermint/tendermint/rpc/client/http" "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" @@ -95,12 +94,6 @@ func (ctx Context) WithOutputFormat(format string) Context { // WithNodeURI returns a copy of the context with an updated node URI. func (ctx Context) WithNodeURI(nodeURI string) Context { ctx.NodeURI = nodeURI - client, err := rpchttp.New(nodeURI, "/websocket") - if err != nil { - panic(err) - } - - ctx.Client = client return ctx }