From a1d7baf473a62b19a3da9317a34334d3b93b10fe Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Thu, 9 Dec 2021 14:56:00 +0530 Subject: [PATCH 1/6] tx with generate-only and offline shouldn't use chain id --- client/tx/factory.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/client/tx/factory.go b/client/tx/factory.go index 6d62d6264135..8fd90b2844c3 100644 --- a/client/tx/factory.go +++ b/client/tx/factory.go @@ -28,6 +28,8 @@ type Factory struct { timeoutHeight uint64 gasAdjustment float64 chainID string + offline bool + generateOnly bool memo string fees sdk.Coins tip *tx.Tip @@ -64,6 +66,8 @@ func NewFactoryCLI(clientCtx client.Context, flagSet *pflag.FlagSet) Factory { accountRetriever: clientCtx.AccountRetriever, keybase: clientCtx.Keyring, chainID: clientCtx.ChainID, + offline: clientCtx.Offline, + generateOnly: clientCtx.GenerateOnly, gas: gasSetting.Gas, simulateAndExecute: gasSetting.Simulate, accountNumber: accNum, @@ -220,8 +224,14 @@ func (f Factory) WithTimeoutHeight(height uint64) Factory { // BuildUnsignedTx builds a transaction to be signed given a set of messages. // Once created, the fee, memo, and messages are set. func (f Factory) BuildUnsignedTx(msgs ...sdk.Msg) (client.TxBuilder, error) { - if f.chainID == "" { - return nil, fmt.Errorf("chain ID required but not specified") + if f.offline && f.generateOnly { + if f.chainID != "" { + return nil, fmt.Errorf("chain ID cannot be used when offline and generate-only flags are set") + } + } else { + if f.chainID == "" { + return nil, fmt.Errorf("chain ID required but not specified") + } } fees := f.fees From fbcb7dae2e210cc476cacdf1159ba4653ccfba3b Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Thu, 9 Dec 2021 15:02:10 +0530 Subject: [PATCH 2/6] add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e1a210fd063e..82c83ee577cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Features +* [\#10710](https://github.com/cosmos/cosmos-sdk/pull/10710) Chain-id shouldn't be required for creating a transaction with both --generate-only and --offline flags. * [\#10592](https://github.com/cosmos/cosmos-sdk/pull/10592) Add a `DecApproxEq` function that checks to see if `|d1 - d2| < tol` for some Dec `d1, d2, tol`. * [\#10393](https://github.com/cosmos/cosmos-sdk/pull/10393) Add `HasSupply` method to bank keeper to ensure that input denom actually exists on chain. * [\#9933](https://github.com/cosmos/cosmos-sdk/pull/9933) Introduces the notion of a Cosmos "Scalar" type, which would just be simple aliases that give human-understandable meaning to the underlying type, both in Go code and in Proto definitions. From 29e5106e05b929766c5fcd82e08d504d49cfcbbf Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Thu, 9 Dec 2021 15:52:47 +0530 Subject: [PATCH 3/6] add tests --- x/bank/client/testutil/suite.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/x/bank/client/testutil/suite.go b/x/bank/client/testutil/suite.go index e486de8e91f2..5e43a67532cc 100644 --- a/x/bank/client/testutil/suite.go +++ b/x/bank/client/testutil/suite.go @@ -414,6 +414,23 @@ func (s *IntegrationTestSuite) TestNewSendTxCmd() { }, false, 0, &sdk.TxResponse{}, }, + { + "chain-id shouldn't be used with offline and generate-only flags", + val.Address, + val.Address, + sdk.NewCoins( + sdk.NewCoin(fmt.Sprintf("%stoken", val.Moniker), sdk.NewInt(10)), + sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)), + ), + []string{ + fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), + fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()), + fmt.Sprintf("--%s=true", flags.FlagOffline), + fmt.Sprintf("--%s=true", flags.FlagGenerateOnly), + }, + false, 0, &sdk.TxResponse{}, + }, { "not enough fees", val.Address, From 3bd11f4cecf2394adec516df48e283f19cde5a80 Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Thu, 9 Dec 2021 16:10:18 +0530 Subject: [PATCH 4/6] wip tests --- x/bank/client/testutil/suite.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/x/bank/client/testutil/suite.go b/x/bank/client/testutil/suite.go index 5e43a67532cc..eab9e78633b9 100644 --- a/x/bank/client/testutil/suite.go +++ b/x/bank/client/testutil/suite.go @@ -429,7 +429,7 @@ func (s *IntegrationTestSuite) TestNewSendTxCmd() { fmt.Sprintf("--%s=true", flags.FlagOffline), fmt.Sprintf("--%s=true", flags.FlagGenerateOnly), }, - false, 0, &sdk.TxResponse{}, + true, 0, &sdk.TxResponse{}, }, { "not enough fees", @@ -482,6 +482,8 @@ func (s *IntegrationTestSuite) TestNewSendTxCmd() { s.Require().NoError(clientCtx.Codec.UnmarshalJSON(bz.Bytes(), tc.respType), bz.String()) txResp := tc.respType.(*sdk.TxResponse) + fmt.Println("----------") + fmt.Println(tc.expectedCode, txResp.Code) s.Require().Equal(tc.expectedCode, txResp.Code) } }) From ba78a2753a1d6f82cfae4293efe573b7b877088e Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Thu, 9 Dec 2021 16:23:48 +0530 Subject: [PATCH 5/6] remove logs --- x/bank/client/testutil/suite.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/x/bank/client/testutil/suite.go b/x/bank/client/testutil/suite.go index eab9e78633b9..f37d1f24c4d0 100644 --- a/x/bank/client/testutil/suite.go +++ b/x/bank/client/testutil/suite.go @@ -482,8 +482,6 @@ func (s *IntegrationTestSuite) TestNewSendTxCmd() { s.Require().NoError(clientCtx.Codec.UnmarshalJSON(bz.Bytes(), tc.respType), bz.String()) txResp := tc.respType.(*sdk.TxResponse) - fmt.Println("----------") - fmt.Println(tc.expectedCode, txResp.Code) s.Require().Equal(tc.expectedCode, txResp.Code) } }) From feeda6af01059bc65869b23934f0a895d8c708fb Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Thu, 9 Dec 2021 22:49:07 +0530 Subject: [PATCH 6/6] address review comment --- client/tx/factory.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/client/tx/factory.go b/client/tx/factory.go index 8fd90b2844c3..79fe2b45f9f2 100644 --- a/client/tx/factory.go +++ b/client/tx/factory.go @@ -228,10 +228,8 @@ func (f Factory) BuildUnsignedTx(msgs ...sdk.Msg) (client.TxBuilder, error) { if f.chainID != "" { return nil, fmt.Errorf("chain ID cannot be used when offline and generate-only flags are set") } - } else { - if f.chainID == "" { - return nil, fmt.Errorf("chain ID required but not specified") - } + } else if f.chainID == "" { + return nil, fmt.Errorf("chain ID required but not specified") } fees := f.fees