From 21e6f7ce7be9f9941ee71d7de9e8a2fd92d3d457 Mon Sep 17 00:00:00 2001 From: likhita-809 <78951027+likhita-809@users.noreply.github.com> Date: Fri, 10 Dec 2021 17:46:12 +0530 Subject: [PATCH] feat: tx with generate-only and offline shouldn't use chain id (#10710) ## Description chain-id shouldn't be required for creating a tx with --generate-only and --offline ref: #9407 --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable) --- CHANGELOG.md | 1 + client/tx/factory.go | 10 +++++++++- x/bank/client/testutil/suite.go | 17 +++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d420a893187b..da992685c617 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. * [\#10703](https://github.com/cosmos/cosmos-sdk/pull/10703) Create a new grantee account, if the grantee of an authorization does not exist. * [\#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. diff --git a/client/tx/factory.go b/client/tx/factory.go index 6d62d6264135..79fe2b45f9f2 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,7 +224,11 @@ 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 == "" { + 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") } diff --git a/x/bank/client/testutil/suite.go b/x/bank/client/testutil/suite.go index e486de8e91f2..f37d1f24c4d0 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), + }, + true, 0, &sdk.TxResponse{}, + }, { "not enough fees", val.Address,