Skip to content

Commit

Permalink
Decouple client/tx from x/auth (#5992)
Browse files Browse the repository at this point in the history
As part of #5864 part 2), this PR decouples the new client/tx
package from x/auth (as an incremental step to deprecating
all the tx logic in x/auth) and adds StdFee, StdSignature,
StdSignMsgBase and StdTxBase to codec/std, while
deprecating the corresponding types in x/auth.
  • Loading branch information
aaronc authored Apr 15, 2020
1 parent aeee097 commit bb4642a
Show file tree
Hide file tree
Showing 14 changed files with 1,610 additions and 1,493 deletions.
56 changes: 47 additions & 9 deletions client/tx/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ import (
"strings"

"github.com/gogo/protobuf/jsonpb"
"github.com/tendermint/tendermint/crypto"

"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/input"
clientkeys "github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/auth/types"
)

type (
Expand All @@ -27,6 +27,20 @@ type (
// implement ClientTx.
Generator interface {
NewTx() ClientTx
NewFee() ClientFee
NewSignature() ClientSignature
}

ClientFee interface {
sdk.Fee
SetGas(uint64)
SetAmount(sdk.Coins)
}

ClientSignature interface {
sdk.Signature
SetPubKey(crypto.PubKey) error
SetSignature([]byte)
}

// ClientTx defines an interface which an application-defined concrete transaction
Expand All @@ -39,9 +53,9 @@ type (

SetMsgs(...sdk.Msg) error
GetSignatures() []sdk.Signature
SetSignatures(...sdk.Signature)
SetSignatures(...ClientSignature) error
GetFee() sdk.Fee
SetFee(sdk.Fee)
SetFee(ClientFee) error
GetMemo() string
SetMemo(string)

Expand Down Expand Up @@ -176,7 +190,7 @@ func WriteGeneratedTxResponse(

if br.Simulate || simAndExec {
if gasAdj < 0 {
rest.WriteErrorResponse(w, http.StatusBadRequest, types.ErrorInvalidGasAdjustment.Error())
rest.WriteErrorResponse(w, http.StatusBadRequest, sdkerrors.ErrorInvalidGasAdjustment.Error())
return
}

Expand Down Expand Up @@ -233,10 +247,20 @@ func BuildUnsignedTx(txf Factory, msgs ...sdk.Msg) (ClientTx, error) {
}
}

clientFee := txf.txGenerator.NewFee()
clientFee.SetAmount(fees)
clientFee.SetGas(txf.gas)

tx := txf.txGenerator.NewTx()
tx.SetFee(auth.NewStdFee(txf.gas, fees))
tx.SetMemo(txf.memo)
tx.SetSignatures()

if err := tx.SetFee(clientFee); err != nil {
return nil, err
}

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

if err := tx.SetMsgs(msgs...); err != nil {
return nil, err
Expand All @@ -256,7 +280,11 @@ func BuildSimTx(txf Factory, msgs ...sdk.Msg) ([]byte, error) {

// Create an empty signature literal as the ante handler will populate with a
// sentinel pubkey.
tx.SetSignatures(auth.NewStdSignature(nil, nil))
sig := txf.txGenerator.NewSignature()

if err := tx.SetSignatures(sig); err != nil {
return nil, err
}

return tx.Marshal()
}
Expand Down Expand Up @@ -337,7 +365,17 @@ func Sign(txf Factory, name, passphrase string, tx ClientTx) ([]byte, error) {
return nil, err
}

tx.SetSignatures(auth.NewStdSignature(pubkey, sigBytes))
sig := txf.txGenerator.NewSignature()
sig.SetSignature(sigBytes)

if err := sig.SetPubKey(pubkey); err != nil {
return nil, err
}

if err := tx.SetSignatures(sig); err != nil {
return nil, err
}

return tx.Marshal()
}

Expand Down
3 changes: 1 addition & 2 deletions client/tx/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/std"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
)

Expand Down Expand Up @@ -87,7 +86,7 @@ func TestBuildSimTx(t *testing.T) {

tx := &std.Transaction{}
require.NoError(t, tx.Unmarshal(bz))
require.Equal(t, []sdk.Signature{sdk.Signature(auth.StdSignature{})}, tx.GetSignatures())
require.Equal(t, []sdk.Signature{sdk.Signature(std.StdSignature{})}, tx.GetSignatures())
}

func TestBuildUnsignedTx(t *testing.T) {
Expand Down
Loading

0 comments on commit bb4642a

Please sign in to comment.