Skip to content

Commit

Permalink
chore: revert TxFactory changes (cosmos#10053)
Browse files Browse the repository at this point in the history
* Revert "refactor: Move some methods inside TX Factory (backport cosmos#9421) (cosmos#10039)"

This reverts commit 0155244.

* remove conflict marks from changelog

* update changelog
  • Loading branch information
robert-zaremba authored and Eengineer1 committed Aug 25, 2022
1 parent aeadd74 commit 00d343d
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 14 deletions.
8 changes: 3 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,10 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Bug Fixes

+ [\#9965](https://github.com/cosmos/cosmos-sdk/pull/9965) Fixed `simd version` command output to report the right release tag.
+
### API Breaking Changes
* (client/tx) [\#9421](https://github.com/cosmos/cosmos-sdk/pull/9421/) `BuildUnsignedTx`, `BuildSimTx`, `PrintUnsignedStdTx` functions are moved to
the Tx Factory as methods.
* [\#9965](https://github.com/cosmos/cosmos-sdk/pull/9965) Fixed `simd version` command output to report the right release tag.

### Client Breaking Changes

* [\#10041](https://github.com/cosmos/cosmos-sdk/pull/10041) Remove broadcast & encode legacy REST endpoints. Please see the [REST Endpoints Migration guide](https://docs.cosmos.network/master/migrations/rest.html) to migrate to the new REST endpoints.

## [v0.43.0](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.43.0) - 2021-08-10
Expand Down Expand Up @@ -150,6 +147,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/capability) [\#9836](https://github.com/cosmos/cosmos-sdk/pull/9836) Removed `InitializeAndSeal(ctx sdk.Context)` and replaced with `Seal()`. App must add x/capability module to the begin blockers which will assure that the x/capability keeper is properly initialized. The x/capability begin blocker must be run before any other module which uses x/capability.



### State Machine Breaking

* (x/{bank,distrib,gov,slashing,staking}) [\#8363](https://github.com/cosmos/cosmos-sdk/issues/8363) Store keys have been modified to allow for variable-length addresses.
Expand Down
71 changes: 67 additions & 4 deletions client/tx/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ 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 @@ -53,17 +54,49 @@ func GenerateOrBroadcastTxWithFactory(clientCtx client.Context, txf Factory, msg
}

if clientCtx.GenerateOnly {
return txf.PrintUnsignedTx(clientCtx, msgs...)
return GenerateTx(clientCtx, txf, msgs...)
}

return BroadcastTx(clientCtx, txf, msgs...)
}

// GenerateTx 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 GenerateTx(clientCtx client.Context, txf Factory, msgs ...sdk.Msg) error {
if txf.SimulateAndExecute() {
if clientCtx.Offline {
return errors.New("cannot estimate gas in offline mode")
}

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

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

tx, err := BuildUnsignedTx(txf, msgs...)
if err != nil {
return err
}

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

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

// BroadcastTx attempts to generate, sign and broadcast a transaction with the
// given set of messages. It will also simulate gas requirements if necessary.
// It will return an error upon failure.
func BroadcastTx(clientCtx client.Context, txf Factory, msgs ...sdk.Msg) error {
txf, err := txf.Prepare(clientCtx)
txf, err := prepareFactory(clientCtx, txf)
if err != nil {
return err
}
Expand All @@ -82,7 +115,7 @@ func BroadcastTx(clientCtx client.Context, txf Factory, msgs ...sdk.Msg) error {
return nil
}

tx, err := txf.BuildUnsignedTx(msgs...)
tx, err := BuildUnsignedTx(txf, msgs...)
if err != nil {
return err
}
Expand Down Expand Up @@ -130,7 +163,7 @@ func BroadcastTx(clientCtx client.Context, txf Factory, msgs ...sdk.Msg) error {
func CalculateGas(
clientCtx gogogrpc.ClientConn, txf Factory, msgs ...sdk.Msg,
) (*tx.SimulateResponse, uint64, error) {
txBytes, err := txf.BuildSimTx(msgs...)
txBytes, err := BuildSimTx(txf, msgs...)
if err != nil {
return nil, 0, err
}
Expand All @@ -146,6 +179,36 @@ func CalculateGas(
return simRes, uint64(txf.GasAdjustment() * float64(simRes.GasInfo.GasUsed)), nil
}

// prepareFactory 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 prepareFactory(clientCtx client.Context, txf Factory) (Factory, error) {
from := clientCtx.GetFromAddress()

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

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

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

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

return txf, nil
}

// SignWithPrivKey signs a given tx with the given private key, and returns the
// corresponding SignatureV2 if the signing is successful.
func SignWithPrivKey(
Expand Down
8 changes: 4 additions & 4 deletions client/tx/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func TestBuildSimTx(t *testing.T) {
WithKeybase(kb)

msg := banktypes.NewMsgSend(sdk.AccAddress("from"), sdk.AccAddress("to"), nil)
bz, err := txf.BuildSimTx(msg)
bz, err := tx.BuildSimTx(txf, msg)
require.NoError(t, err)
require.NotNil(t, bz)
}
Expand All @@ -149,7 +149,7 @@ func TestBuildUnsignedTx(t *testing.T) {
WithKeybase(kb)

msg := banktypes.NewMsgSend(sdk.AccAddress("from"), sdk.AccAddress("to"), nil)
tx, err := txf.BuildUnsignedTx(msg)
tx, err := tx.BuildUnsignedTx(txf, msg)
require.NoError(t, err)
require.NotNil(t, tx)

Expand Down Expand Up @@ -205,9 +205,9 @@ func TestSign(t *testing.T) {
msg2 := banktypes.NewMsgSend(addr2, sdk.AccAddress("to"), nil)
txb, err := txfNoKeybase.BuildUnsignedTx(msg1, msg2)
requireT.NoError(err)
txb2, err := txfNoKeybase.BuildUnsignedTx(msg1, msg2)
txb2, err := tx.BuildUnsignedTx(txfNoKeybase, msg1, msg2)
requireT.NoError(err)
txbSimple, err := txfNoKeybase.BuildUnsignedTx(msg2)
txbSimple, err := tx.BuildUnsignedTx(txfNoKeybase, msg2)
requireT.NoError(err)

testCases := []struct {
Expand Down
6 changes: 6 additions & 0 deletions x/auth/client/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ func (gr GasEstimateResponse) String() string {
return fmt.Sprintf("gas estimate: %d", gr.GasEstimate)
}

// PrintUnsignedStdTx builds an unsigned StdTx and prints it to os.Stdout.
func PrintUnsignedStdTx(txBldr tx.Factory, clientCtx client.Context, msgs []sdk.Msg) error {
err := tx.GenerateTx(clientCtx, txBldr, msgs...)
return err
}

// SignTx signs a transaction managed by the TxBuilder using a `name` key stored in Keybase.
// The new signature is appended to the TxBuilder when overwrite=false or overwritten otherwise.
// Don't perform online validation or lookups if offline is true.
Expand Down
2 changes: 1 addition & 1 deletion x/genutil/client/cli/gentx.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ $ %s gentx my-key-name 1000000stake --home=/path/to/home/dir --keyring-backend=o

if key.GetType() == keyring.TypeOffline || key.GetType() == keyring.TypeMulti {
cmd.PrintErrln("Offline key passed in. Use `tx sign` command to sign.")
return txBldr.PrintUnsignedTx(clientCtx, msg)
return authclient.PrintUnsignedStdTx(txBldr, clientCtx, []sdk.Msg{msg})
}

// write the unsigned transaction to the buffer
Expand Down

0 comments on commit 00d343d

Please sign in to comment.