fix: running a tx with --dry-run returns an error (#12095)

This commit is contained in:
Julien Robert 2022-06-01 16:39:22 +02:00 committed by GitHub
parent c4934b7bab
commit b1453335ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 67 additions and 19 deletions

View File

@ -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. * (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. * (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 ## [v0.46.0-rc1](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.46.0-rc1) - 2022-05-23

View File

@ -317,25 +317,9 @@ func (f Factory) BuildSimTx(msgs ...sdk.Msg) ([]byte, error) {
return nil, err return nil, err
} }
// use the first element from the list of keys in order to generate a valid pk, err := f.getSimPK()
// pubkey that supports multiple algorithms if err != nil {
return nil, err
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")
}
} }
// Create an empty signature literal as the ante handler will populate with a // 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()) 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 // Prepare ensures the account defined by ctx.GetFromAddress() exists and
// if the account number and/or the account sequence number are zero (not set), // 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 // they will be queried for and set on the provided Factory. A new Factory with

View File

@ -2,6 +2,8 @@ package testutil
import ( import (
"fmt" "fmt"
"io/ioutil"
"os"
"github.com/gogo/protobuf/proto" "github.com/gogo/protobuf/proto"
"github.com/stretchr/testify/suite" "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()) 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() { func (s *IntegrationTestSuite) TestNewSendTxCmd() {
val := s.network.Validators[0] val := s.network.Validators[0]