From 0cb6164d90fa099c3717b39d2cae427f139ceae0 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Tue, 31 May 2022 14:41:40 +0200 Subject: [PATCH 1/5] fix: running a tx with --dry-run returns an error --- x/bank/client/testutil/suite.go | 34 +++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/x/bank/client/testutil/suite.go b/x/bank/client/testutil/suite.go index 2c6c79316ffe..4afad13f5b3c 100644 --- a/x/bank/client/testutil/suite.go +++ b/x/bank/client/testutil/suite.go @@ -2,6 +2,8 @@ package testutil import ( "fmt" + "io/ioutil" + "os" "github.com/gogo/protobuf/proto" "github.com/stretchr/testify/suite" @@ -387,6 +389,38 @@ func (s *IntegrationTestSuite) TestNewSendTxCmdGenOnly() { s.Require().Equal([]sdk.Msg{types.NewMsgSend(from, to, amount)}, tx.GetMsgs()) } +func (s *IntegrationTestSuite) TestNewSendTxCmdDryRun() { + val := s.network.Validators[0] + + clientCtx := val.ClientCtx + + from := val.Address + to := val.Address + amount := sdk.NewCoins( + sdk.NewCoin(fmt.Sprintf("%stoken", val.Moniker), sdk.NewInt(10)), + sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)), + ) + args := []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.FlagDryRun), + } + + oldSterr := os.Stderr + r, w, _ := os.Pipe() + os.Stderr = w + + _, err := MsgSendExec(clientCtx, from, to, amount, args...) + s.Require().NoError(err) + + w.Close() + out, _ := ioutil.ReadAll(r) + os.Stderr = oldSterr + + s.Require().Regexp("gas estimate: [0-9]+", string(out)) +} + func (s *IntegrationTestSuite) TestNewSendTxCmd() { val := s.network.Validators[0] From 7d26ea27c405d2e2e290d73d44e5c6fa1cb133ec Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Tue, 31 May 2022 15:27:01 +0200 Subject: [PATCH 2/5] add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0bb227e5b8f2..9b9d5d76f982 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (migrations) [#12028](https://github.com/cosmos/cosmos-sdk/pull/12028) Fix v0.45->v0.46 in-place store migrations. * (baseapp) [#12089](https://github.com/cosmos/cosmos-sdk/pull/12089) Include antehandler and runMsgs events in SimulateTx. +* (cli) [#12095](https://github.com/cosmos/cosmos-sdk/pull/12095) Fix running a tx with --dry-run returns an error ## [v0.46.0-rc1](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.46.0-rc1) - 2022-05-23 From e0293eed4d128f5cf86d1bb5d567435ab4f4b945 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Tue, 31 May 2022 16:58:06 +0200 Subject: [PATCH 3/5] add fix --- client/tx/factory.go | 46 ++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/client/tx/factory.go b/client/tx/factory.go index b65264d6671f..8a89e80142dd 100644 --- a/client/tx/factory.go +++ b/client/tx/factory.go @@ -317,15 +317,40 @@ func (f Factory) BuildSimTx(msgs ...sdk.Msg) ([]byte, error) { return nil, err } - // use the first element from the list of keys in order to generate a valid - // pubkey that supports multiple algorithms + pk, err := f.getSimPK() + if err != nil { + return nil, err + } + // Create an empty signature literal as the ante handler will populate with a + // sentinel pubkey. + sig := signing.SignatureV2{ + PubKey: pk, + Data: &signing.SingleSignatureData{ + SignMode: f.signMode, + }, + Sequence: f.Sequence(), + } + if err := txb.SetSignatures(sig); err != nil { + return nil, err + } + + return f.txConfig.TxEncoder()(txb.GetTx()) +} + +// getSimPK gets the public key to use for building a simulation tx +// note we should only check for keys in the keybase if we are in simulate and execute mode +// (f.e. when using --gas=auto - ref #11283) +// when using --dry-run, we are is simulation mode only and should not check the keybase +func (f Factory) getSimPK() (cryptotypes.PubKey, error) { var ( ok bool pk cryptotypes.PubKey = &secp256k1.PubKey{} // use default public key type ) - if f.keybase != nil { + // use the first element from the list of keys in order to generate a valid + // pubkey that supports multiple algorithms + if f.simulateAndExecute && f.keybase != nil { records, _ := f.keybase.List() if len(records) == 0 { return nil, errors.New("cannot build signature for simulation, key records slice is empty") @@ -338,20 +363,7 @@ func (f Factory) BuildSimTx(msgs ...sdk.Msg) ([]byte, error) { } } - // Create an empty signature literal as the ante handler will populate with a - // sentinel pubkey. - sig := signing.SignatureV2{ - PubKey: pk, - Data: &signing.SingleSignatureData{ - SignMode: f.signMode, - }, - Sequence: f.Sequence(), - } - if err := txb.SetSignatures(sig); err != nil { - return nil, err - } - - return f.txConfig.TxEncoder()(txb.GetTx()) + return pk, nil } // Prepare ensures the account defined by ctx.GetFromAddress() exists and From ec4f7f64902f6fe03c044c926ac720eead505c97 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Wed, 1 Jun 2022 00:35:31 +0200 Subject: [PATCH 4/5] Update client/tx/factory.go Co-authored-by: Aleksandr Bezobchuk --- client/tx/factory.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/client/tx/factory.go b/client/tx/factory.go index 8a89e80142dd..2c9bf8f1701d 100644 --- a/client/tx/factory.go +++ b/client/tx/factory.go @@ -338,10 +338,11 @@ func (f Factory) BuildSimTx(msgs ...sdk.Msg) ([]byte, error) { return f.txConfig.TxEncoder()(txb.GetTx()) } -// getSimPK gets the public key to use for building a simulation tx -// note we should only check for keys in the keybase if we are in simulate and execute mode -// (f.e. when using --gas=auto - ref #11283) -// when using --dry-run, we are is simulation mode only and should not check the keybase +// getSimPK gets the public key to use for building a simulation tx. +// Note, we should only check for keys in the keybase if we are in simulate and execute mode, +// e.g. when using --gas=auto. +// When using --dry-run, we are is simulation mode only and should not check the keybase. +// Ref: https://github.com/cosmos/cosmos-sdk/issues/11283 func (f Factory) getSimPK() (cryptotypes.PubKey, error) { var ( ok bool From 31ffb0df9e2ae4a3edda2b6ee7a9a5a8e2baeba2 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Wed, 1 Jun 2022 00:35:37 +0200 Subject: [PATCH 5/5] Update client/tx/factory.go Co-authored-by: Aleksandr Bezobchuk --- client/tx/factory.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/tx/factory.go b/client/tx/factory.go index 2c9bf8f1701d..ab6ab7943793 100644 --- a/client/tx/factory.go +++ b/client/tx/factory.go @@ -349,8 +349,8 @@ func (f Factory) getSimPK() (cryptotypes.PubKey, error) { pk cryptotypes.PubKey = &secp256k1.PubKey{} // use default public key type ) - // use the first element from the list of keys in order to generate a valid - // pubkey that supports multiple algorithms + // Use the first element from the list of keys in order to generate a valid + // pubkey that supports multiple algorithms. if f.simulateAndExecute && f.keybase != nil { records, _ := f.keybase.List() if len(records) == 0 {