Skip to content

Commit

Permalink
fix: remove hardcoded pubkey from tx simulation (backport cosmos#11282)…
Browse files Browse the repository at this point in the history
… (cosmos#11288)

* fix: remove hardcoded pubkey from tx simulation (cosmos#11282)

## Description

Closes: cosmos#11283

---

### 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...

- [x] 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
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [x] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [x] 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)

(cherry picked from commit e657190)

# Conflicts:
#	client/tx/factory.go
#	client/tx/tx_test.go

* fix conflicts

* fix

* fix build

* fix tests

Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>
Co-authored-by: Federico Kunze Küllmer <federico.kunze94@gmail.com>
  • Loading branch information
3 people authored and JeancarloBarrios committed Sep 28, 2024
1 parent a4fa189 commit 430369e
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 93 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/auth) [\#10880](https://github.com/cosmos/cosmos-sdk/pull/10880) Added a new query to the tx query service that returns a block with transactions fully decoded.
* [#11314](https://github.com/cosmos/cosmos-sdk/pull/11314) Add state rollback command.


### Bug Fixes

* (client) [\#11283](https://github.com/cosmos/cosmos-sdk/issues/11283) Support multiple keys for tx simulation and setting automatic gas for txs.
* (store) [\#11177](https://github.com/cosmos/cosmos-sdk/pull/11177) Update the prune `everything` strategy to store the last two heights.
* (store) [\#11117](https://github.com/cosmos/cosmos-sdk/pull/11117) Fix data race in store trace component
* (x/authz) [\#11252](https://github.com/cosmos/cosmos-sdk/pull/11252) Allow insufficient funds error for authz simulation
Expand Down
150 changes: 150 additions & 0 deletions client/tx/factory.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package tx

import (
"errors"
"fmt"
"os"

"github.com/spf13/pflag"

"cosmossdk.io/math"
Expand All @@ -9,6 +13,8 @@ import (
"github.com/cosmos/cosmos-sdk/client/flags"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
)
Expand Down Expand Up @@ -241,3 +247,147 @@ func (f Factory) WithTimeoutHeight(height uint64) Factory {
f.timeoutHeight = height
return f
}

// 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")
}

fees := f.fees

if !f.gasPrices.IsZero() {
if !fees.IsZero() {
return nil, errors.New("cannot provide both fees and gas prices")
}

glDec := sdk.NewDec(int64(f.gas))

// Derive the fees based on the provided gas prices, where
// fee = ceil(gasPrice * gasLimit).
fees = make(sdk.Coins, len(f.gasPrices))

for i, gp := range f.gasPrices {
fee := gp.Amount.Mul(glDec)
fees[i] = sdk.NewCoin(gp.Denom, fee.Ceil().RoundInt())
}
}

tx := f.txConfig.NewTxBuilder()

if err := tx.SetMsgs(msgs...); err != nil {
return nil, err
}

tx.SetMemo(f.memo)
tx.SetFeeAmount(fees)
tx.SetGasLimit(f.gas)
tx.SetTimeoutHeight(f.TimeoutHeight())

return tx, nil
}

// PrintUnsignedTx will generate an unsigned transaction and print it to the writer
// specified by ctx.Output. If simulation was requested, the gas will be
// simulated and also printed to the same writer before the transaction is
// printed.
func (f Factory) PrintUnsignedTx(clientCtx client.Context, msgs ...sdk.Msg) error {
if f.SimulateAndExecute() {
if clientCtx.Offline {
return errors.New("cannot estimate gas in offline mode")
}

_, adjusted, err := CalculateGas(clientCtx, f, msgs...)
if err != nil {
return err
}

f = f.WithGas(adjusted)
_, _ = fmt.Fprintf(os.Stderr, "%s\n", GasEstimateResponse{GasEstimate: f.Gas()})
}

unsignedTx, err := f.BuildUnsignedTx(msgs...)
if err != nil {
return err
}

json, err := clientCtx.TxConfig.TxJSONEncoder()(unsignedTx.GetTx())
if err != nil {
return err
}

return clientCtx.PrintString(fmt.Sprintf("%s\n", json))
}

// BuildSimTx creates an unsigned tx with an empty single signature and returns
// the encoded transaction or an error if the unsigned transaction cannot be
// built.
func (f Factory) BuildSimTx(msgs ...sdk.Msg) ([]byte, error) {
txb, err := f.BuildUnsignedTx(msgs...)
if err != nil {
return nil, err
}

// use the first element from the list of keys in order to generate a valid
// pubkey that supports multiple algorithms

var pk cryptotypes.PubKey = &secp256k1.PubKey{} // use default public key type

if f.keybase != nil {
infos, _ := f.keybase.List()
if len(infos) == 0 {
return nil, errors.New("cannot build signature for simulation, key infos slice is empty")
}

// take the first info record just for simulation purposes
pk = infos[0].GetPubKey()
}

// 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())
}

// 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
// the updated fields will be returned.
func (f Factory) Prepare(clientCtx client.Context) (Factory, error) {
fc := f

from := clientCtx.GetFromAddress()

if err := fc.accountRetriever.EnsureExists(clientCtx, from); err != nil {
return fc, err
}

initNum, initSeq := fc.accountNumber, fc.sequence
if initNum == 0 || initSeq == 0 {
num, seq, err := fc.accountRetriever.GetAccountNumberSequence(clientCtx, from)
if err != nil {
return fc, err
}

if initNum == 0 {
fc = fc.WithAccountNumber(num)
}

if initSeq == 0 {
fc = fc.WithSequence(seq)
}
}

return fc, nil
}
57 changes: 2 additions & 55 deletions client/tx/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/input"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand Down Expand Up @@ -235,66 +234,14 @@ func WriteGeneratedTxResponse(
// transaction is initially created via the provided factory's generator. Once
// created, the fee, memo, and messages are set.
func BuildUnsignedTx(txf Factory, msgs ...sdk.Msg) (client.TxBuilder, error) {
if txf.chainID == "" {
return nil, fmt.Errorf("chain ID required but not specified")
}

fees := txf.fees

if !txf.gasPrices.IsZero() {
if !fees.IsZero() {
return nil, errors.New("cannot provide both fees and gas prices")
}

glDec := sdk.NewDec(int64(txf.gas))

// Derive the fees based on the provided gas prices, where
// fee = ceil(gasPrice * gasLimit).
fees = make(sdk.Coins, len(txf.gasPrices))

for i, gp := range txf.gasPrices {
fee := gp.Amount.Mul(glDec)
fees[i] = sdk.NewCoin(gp.Denom, fee.Ceil().RoundInt())
}
}

tx := txf.txConfig.NewTxBuilder()

if err := tx.SetMsgs(msgs...); err != nil {
return nil, err
}

tx.SetMemo(txf.memo)
tx.SetFeeAmount(fees)
tx.SetGasLimit(txf.gas)
tx.SetTimeoutHeight(txf.TimeoutHeight())

return tx, nil
return txf.BuildUnsignedTx(msgs...)
}

// BuildSimTx creates an unsigned tx with an empty single signature and returns
// the encoded transaction or an error if the unsigned transaction cannot be
// built.
func BuildSimTx(txf Factory, msgs ...sdk.Msg) ([]byte, error) {
txb, err := BuildUnsignedTx(txf, msgs...)
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: &secp256k1.PubKey{},
Data: &signing.SingleSignatureData{
SignMode: txf.signMode,
},
Sequence: txf.Sequence(),
}
if err := txb.SetSignatures(sig); err != nil {
return nil, err
}

return txf.txConfig.TxEncoder()(txb.GetTx())
return txf.BuildSimTx(msgs...)
}

// CalculateGas simulates the execution of a transaction and returns the
Expand Down
73 changes: 36 additions & 37 deletions client/tx/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (m mockContext) Invoke(_ context.Context, _ string, _, reply interface{}, _
return nil
}

func (mockContext) NewStream(context.Context, *grpc.StreamDesc, string, ...grpc.CallOption) (grpc.ClientStream, error) {
func (mockContext) NewStream(gocontext.Context, *grpc.StreamDesc, string, ...grpc.CallOption) (grpc.ClientStream, error) {
panic("not implemented")
}

Expand Down Expand Up @@ -108,23 +108,25 @@ func TestCalculateGas(t *testing.T) {
}
}

func mockTxFactory(txCfg client.TxConfig) Factory {
return Factory{}.
func TestBuildSimTx(t *testing.T) {
txCfg := NewTestTxConfig()

kb, err := keyring.New(t.Name(), "test", t.TempDir(), nil)
require.NoError(t, err)

path := hd.CreateHDPath(118, 0, 0).String()
_, _, err = kb.NewMnemonic("test_key1", keyring.English, path, keyring.DefaultBIP39Passphrase, hd.Secp256k1)
require.NoError(t, err)

txf := tx.Factory{}.
WithTxConfig(txCfg).
WithAccountNumber(50).
WithSequence(23).
WithFees("50stake").
WithMemo("memo").
WithChainID("test-chain")
}

func TestBuildSimTx(t *testing.T) {
txCfg, cdc := newTestTxConfig()
defaultSignMode, err := signing.APISignModeToInternal(txCfg.SignModeHandler().DefaultMode())
require.NoError(t, err)

kb, err := keyring.New(t.Name(), "test", t.TempDir(), nil, cdc)
require.NoError(t, err)
WithChainID("test-chain").
WithSignMode(txCfg.SignModeHandler().DefaultMode()).
WithKeybase(kb)

msg := banktypes.NewMsgSend(sdk.AccAddress("from"), sdk.AccAddress("to"), nil)
bz, err := tx.BuildSimTx(txf, msg)
Expand All @@ -133,12 +135,22 @@ func TestBuildSimTx(t *testing.T) {
}

func TestBuildUnsignedTx(t *testing.T) {
txConfig, cdc := newTestTxConfig()
kb, err := keyring.New(t.Name(), "test", t.TempDir(), nil, cdc)
kb, err := keyring.New(t.Name(), "test", t.TempDir(), nil)
require.NoError(t, err)

path := hd.CreateHDPath(118, 0, 0).String()

_, _, err = kb.NewMnemonic("test_key1", keyring.English, path, keyring.DefaultBIP39Passphrase, hd.Secp256k1)
require.NoError(t, err)

txf := tx.Factory{}.
WithTxConfig(NewTestTxConfig()).
WithAccountNumber(50).
WithSequence(23).
WithFees("50stake").
WithMemo("memo").
WithChainID("test-chain")

msg := banktypes.NewMsgSend(sdk.AccAddress("from"), sdk.AccAddress("to"), nil)
tx, err := tx.BuildUnsignedTx(txf, msg)
require.NoError(t, err)
Expand Down Expand Up @@ -255,6 +267,7 @@ func TestSign(t *testing.T) {
WithSignMode(signingtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON)
msg1 := banktypes.NewMsgSend(info1.GetAddress(), sdk.AccAddress("to"), nil)
msg2 := banktypes.NewMsgSend(info2.GetAddress(), sdk.AccAddress("to"), nil)

txb, err := tx.BuildUnsignedTx(txfNoKeybase, msg1, msg2)
requireT.NoError(err)
txb2, err := tx.BuildUnsignedTx(txfNoKeybase, msg1, msg2)
Expand Down Expand Up @@ -317,45 +330,31 @@ func TestSign(t *testing.T) {
},

/**** test double sign Direct mode
signing transaction with 2 or more DIRECT signers should fail in DIRECT mode ****/
signing transaction with more than 2 signers should fail in DIRECT mode ****/
{
"direct: should append a DIRECT signature with existing AMINO",
// txb already has 1 AMINO signature
"direct: should fail to append a signature with different mode",
txfDirect, txb, from1, false,
[]cryptotypes.PubKey{pubKey2, pubKey1},
nil,
},
{
"direct: should add single DIRECT sig in multi-signers tx",
txfDirect, txb2, from1, false,
[]cryptotypes.PubKey{pubKey1},
nil,
},
{
"direct: should fail to append 2nd DIRECT sig in multi-signers tx",
txfDirect, txb2, from2, false,
[]cryptotypes.PubKey{},
nil,
},
{
"amino: should append 2nd AMINO sig in multi-signers tx with 1 DIRECT sig",
// txb2 already has 1 DIRECT signature
txfAmino, txb2, from2, false,
"direct: should fail to sign multi-signers tx",
txfDirect, txb2, from1, false,
[]cryptotypes.PubKey{},
nil,
},
{
"direct: should overwrite multi-signers tx with DIRECT sig",
"direct: should fail to overwrite multi-signers tx",
txfDirect, txb2, from1, true,
[]cryptotypes.PubKey{pubKey1},
[]cryptotypes.PubKey{},
nil,
},
}

var prevSigs []signingtypes.SignatureV2
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err = Sign(clientCtx, tc.txf, tc.from, tc.txb, tc.overwrite)
t.Run(tc.name, func(_ *testing.T) {
err = tx.Sign(tc.txf, tc.from, tc.txb, tc.overwrite)
if len(tc.expectedPKs) == 0 {
requireT.Error(err)
} else {
Expand Down

0 comments on commit 430369e

Please sign in to comment.