From b1453335ededc637f71711f38435c502fd70f93c Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Wed, 1 Jun 2022 16:39:22 +0200 Subject: [PATCH] fix: running a tx with --dry-run returns an error (#12095) --- CHANGELOG.md | 1 + client/tx/factory.go | 51 +++++++++++++++++++++------------ x/bank/client/testutil/suite.go | 34 ++++++++++++++++++++++ 3 files changed, 67 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0bb227e5b8..9b9d5d76f9 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 diff --git a/client/tx/factory.go b/client/tx/factory.go index b65264d667..ab6ab79437 100644 --- a/client/tx/factory.go +++ b/client/tx/factory.go @@ -317,25 +317,9 @@ 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 - - var ( - ok bool - pk cryptotypes.PubKey = &secp256k1.PubKey{} // use default public key type - ) - - if 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") - } - - // take the first record just for simulation purposes - pk, ok = records[0].PubKey.GetCachedValue().(cryptotypes.PubKey) - if !ok { - return nil, errors.New("cannot build signature for simulation, failed to convert proto Any to public key") - } + pk, err := f.getSimPK() + if err != nil { + return nil, err } // Create an empty signature literal as the ante handler will populate with a @@ -354,6 +338,35 @@ 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, +// 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 + 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. + 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") + } + + // take the first record just for simulation purposes + pk, ok = records[0].PubKey.GetCachedValue().(cryptotypes.PubKey) + if !ok { + return nil, errors.New("cannot build signature for simulation, failed to convert proto Any to public key") + } + } + + return pk, nil +} + // Prepare ensures the account defined by ctx.GetFromAddress() exists and // if the account number and/or the account sequence number are zero (not set), // they will be queried for and set on the provided Factory. A new Factory with diff --git a/x/bank/client/testutil/suite.go b/x/bank/client/testutil/suite.go index 2c6c79316f..4afad13f5b 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]