Skip to content

Commit

Permalink
cosmos#2926 Add TxEncoder to client TxBuilder.
Browse files Browse the repository at this point in the history
  • Loading branch information
arturalbov committed Dec 12, 2018
1 parent 582ca8e commit bf8365d
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 11 deletions.
7 changes: 3 additions & 4 deletions client/utils/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ func ParseFloat64OrReturnBadRequest(w http.ResponseWriter, s string, defaultIfEm
}

// WriteGenerateStdTxResponse writes response for the generate_only mode.
func WriteGenerateStdTxResponse(w http.ResponseWriter, txBldr authtxb.TxBuilder, msgs []sdk.Msg) {
func WriteGenerateStdTxResponse(w http.ResponseWriter, cdc *codec.Codec, txBldr authtxb.TxBuilder, msgs []sdk.Msg) {
stdMsg, err := txBldr.Build(msgs)
if err != nil {
WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

output, err := txBldr.Codec.MarshalJSON(auth.NewStdTx(stdMsg.Msgs, stdMsg.Fee, nil, stdMsg.Memo))
output, err := cdc.MarshalJSON(auth.NewStdTx(stdMsg.Msgs, stdMsg.Fee, nil, stdMsg.Memo))
if err != nil {
WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
Expand Down Expand Up @@ -198,7 +198,6 @@ func CompleteAndBroadcastTxREST(w http.ResponseWriter, r *http.Request, cliCtx c
}

txBldr := authtxb.TxBuilder{
Codec: cdc,
Gas: gas,
GasAdjustment: adjustment,
SimulateGas: simulateGas,
Expand All @@ -223,7 +222,7 @@ func CompleteAndBroadcastTxREST(w http.ResponseWriter, r *http.Request, cliCtx c
}

if baseReq.GenerateOnly {
WriteGenerateStdTxResponse(w, txBldr, msgs)
WriteGenerateStdTxResponse(w, cdc, txBldr, msgs)
return
}

Expand Down
2 changes: 1 addition & 1 deletion client/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func PrintUnsignedStdTx(w io.Writer, txBldr authtxb.TxBuilder, cliCtx context.CL
if err != nil {
return
}
json, err := txBldr.Codec.MarshalJSON(stdTx)
json, err := cliCtx.Codec.MarshalJSON(stdTx)
if err == nil {
fmt.Fprintf(w, "%s\n", json)
}
Expand Down
3 changes: 3 additions & 0 deletions types/tx_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ type Tx interface {
// TxDecoder unmarshals transaction bytes
type TxDecoder func(txBytes []byte) (Tx, Error)

// TxEncoder marshals transaction to bytes
type TxEncoder func(tx Tx) ([]byte, error)

//__________________________________________________________

var _ Msg = (*TestMsg)(nil)
Expand Down
11 changes: 5 additions & 6 deletions x/auth/client/txbuilder/txbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package context
import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"

Expand All @@ -13,7 +12,7 @@ import (

// TxBuilder implements a transaction context created in SDK modules.
type TxBuilder struct {
Codec *codec.Codec
Encoder sdk.TxEncoder
AccountNumber uint64
Sequence uint64
Gas uint64
Expand Down Expand Up @@ -49,8 +48,8 @@ func NewTxBuilderFromCLI() TxBuilder {
}

// WithCodec returns a copy of the context with an updated codec.
func (bldr TxBuilder) WithCodec(cdc *codec.Codec) TxBuilder {
bldr.Codec = cdc
func (bldr TxBuilder) WithTxEncoder(encoder sdk.TxEncoder) TxBuilder {
bldr.Encoder = encoder
return bldr
}

Expand Down Expand Up @@ -126,7 +125,7 @@ func (bldr TxBuilder) Sign(name, passphrase string, msg StdSignMsg) ([]byte, err
return nil, err
}

return bldr.Codec.MarshalBinaryLengthPrefixed(auth.NewStdTx(msg.Msgs, msg.Fee, []auth.StdSignature{sig}, msg.Memo))
return bldr.Encoder(auth.NewStdTx(msg.Msgs, msg.Fee, []auth.StdSignature{sig}, msg.Memo))
}

// BuildAndSign builds a single message to be signed, and signs a transaction
Expand Down Expand Up @@ -165,7 +164,7 @@ func (bldr TxBuilder) BuildWithPubKey(name string, msgs []sdk.Msg) ([]byte, erro
PubKey: info.GetPubKey(),
}}

return bldr.Codec.MarshalBinaryLengthPrefixed(auth.NewStdTx(msg.Msgs, msg.Fee, sigs, msg.Memo))
return bldr.Encoder(auth.NewStdTx(msg.Msgs, msg.Fee, sigs, msg.Memo))
}

// SignStdTx appends a signature to a StdTx and returns a copy of a it. If append
Expand Down
6 changes: 6 additions & 0 deletions x/auth/stdtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,9 @@ func DefaultTxDecoder(cdc *codec.Codec) sdk.TxDecoder {
return tx, nil
}
}

func DefaultTxEncoder(cdc *codec.Codec) sdk.TxEncoder {
return func(tx sdk.Tx) ([]byte, error) {
return cdc.MarshalBinaryLengthPrefixed(tx)
}
}

0 comments on commit bf8365d

Please sign in to comment.