Skip to content

Commit

Permalink
Merge PR cosmos#4499: Remove Client Codec Redundancy
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderbez authored Jun 6, 2019
1 parent 5b78a7b commit 3180e68
Show file tree
Hide file tree
Showing 49 changed files with 390 additions and 456 deletions.
2 changes: 2 additions & 0 deletions .pending/breaking/sdk/4479-Remove-codec-ar
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#4479 Remove codec argument redundency in client usage where
the CLIContext's codec should be used instead.
2 changes: 0 additions & 2 deletions client/lcd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ type RestServer struct {
Mux *mux.Router
CliCtx context.CLIContext
KeyBase keybase.Keybase
Cdc *codec.Codec

log log.Logger
listener net.Listener
Expand All @@ -45,7 +44,6 @@ func NewRestServer(cdc *codec.Codec) *RestServer {
return &RestServer{
Mux: r,
CliCtx: cliCtx,
Cdc: cdc,
log: logger,
}
}
Expand Down
14 changes: 6 additions & 8 deletions client/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,16 @@ import (
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/utils"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/x/auth"
)

//-----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// Building / Sending utilities

// WriteGenerateStdTxResponse writes response for the generate only mode.
func WriteGenerateStdTxResponse(w http.ResponseWriter, cdc *codec.Codec,
cliCtx context.CLIContext, br rest.BaseReq, msgs []sdk.Msg) {

func WriteGenerateStdTxResponse(w http.ResponseWriter, cliCtx context.CLIContext, br rest.BaseReq, msgs []sdk.Msg) {
gasAdj, ok := rest.ParseFloat64OrReturnBadRequest(w, br.GasAdjustment, flags.DefaultGasAdjustment)
if !ok {
return
Expand All @@ -32,7 +29,7 @@ func WriteGenerateStdTxResponse(w http.ResponseWriter, cdc *codec.Codec,
}

txBldr := auth.NewTxBuilder(
utils.GetTxEncoder(cdc), br.AccountNumber, br.Sequence, gas, gasAdj,
utils.GetTxEncoder(cliCtx.Codec), br.AccountNumber, br.Sequence, gas, gasAdj,
br.Simulate, br.ChainID, br.Memo, br.Fees, br.GasPrices,
)

Expand All @@ -49,7 +46,7 @@ func WriteGenerateStdTxResponse(w http.ResponseWriter, cdc *codec.Codec,
}

if br.Simulate {
rest.WriteSimulationResponse(w, cdc, txBldr.Gas())
rest.WriteSimulationResponse(w, cliCtx.Codec, txBldr.Gas())
return
}
}
Expand All @@ -60,7 +57,7 @@ func WriteGenerateStdTxResponse(w http.ResponseWriter, cdc *codec.Codec,
return
}

output, err := cdc.MarshalJSON(auth.NewStdTx(stdMsg.Msgs, stdMsg.Fee, nil, stdMsg.Memo))
output, err := cliCtx.Codec.MarshalJSON(auth.NewStdTx(stdMsg.Msgs, stdMsg.Fee, nil, stdMsg.Memo))
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
Expand All @@ -70,5 +67,6 @@ func WriteGenerateStdTxResponse(w http.ResponseWriter, cdc *codec.Codec,
if _, err := w.Write(output); err != nil {
log.Printf("could not write response: %v", err)
}

return
}
5 changes: 2 additions & 3 deletions client/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"github.com/gorilla/mux"

"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
)

// Register routes
func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec) {
func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router) {
RegisterRPCRoutes(cliCtx, r)
RegisterTxRoutes(cliCtx, r, cdc)
RegisterTxRoutes(cliCtx, r)
}
4 changes: 2 additions & 2 deletions client/rpc/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func BlockRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
rest.PostProcessResponse(w, codec.Cdc, output, cliCtx.Indent)
rest.PostProcessResponse(w, cliCtx, output)
}
}

Expand All @@ -144,6 +144,6 @@ func LatestBlockRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return
}

rest.PostProcessResponse(w, codec.Cdc, output, cliCtx.Indent)
rest.PostProcessResponse(w, cliCtx, output)
}
}
2 changes: 1 addition & 1 deletion client/rpc/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func NodeInfoRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
}

nodeInfo := status.NodeInfo
rest.PostProcessResponse(w, codec.Cdc, nodeInfo, cliCtx.Indent)
rest.PostProcessResponse(w, cliCtx, nodeInfo)
}
}

Expand Down
4 changes: 2 additions & 2 deletions client/rpc/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func ValidatorSetRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
rest.PostProcessResponse(w, codec.Cdc, output, cliCtx.Indent)
rest.PostProcessResponse(w, cliCtx, output)
}
}

Expand All @@ -189,6 +189,6 @@ func LatestValidatorSetRequestHandlerFn(cliCtx context.CLIContext) http.HandlerF
return
}

rest.PostProcessResponse(w, codec.Cdc, output, cliCtx.Indent)
rest.PostProcessResponse(w, cliCtx, output)
}
}
8 changes: 4 additions & 4 deletions client/tx/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type BroadcastReq struct {
// BroadcastTxRequest implements a tx broadcasting handler that is responsible
// for broadcasting a valid and signed tx to a full node. The tx can be
// broadcasted via a sync|async|block mechanism.
func BroadcastTxRequest(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
func BroadcastTxRequest(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req BroadcastReq

Expand All @@ -36,13 +36,13 @@ func BroadcastTxRequest(cliCtx context.CLIContext, cdc *codec.Codec) http.Handle
return
}

err = cdc.UnmarshalJSON(body, &req)
err = cliCtx.Codec.UnmarshalJSON(body, &req)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

txBytes, err := cdc.MarshalBinaryLengthPrefixed(req.Tx)
txBytes, err := cliCtx.Codec.MarshalBinaryLengthPrefixed(req.Tx)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
Expand All @@ -56,7 +56,7 @@ func BroadcastTxRequest(cliCtx context.CLIContext, cdc *codec.Codec) http.Handle
return
}

rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
rest.PostProcessResponse(w, cliCtx, res)
}
}

Expand Down
6 changes: 3 additions & 3 deletions client/tx/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type (
// EncodeTxRequestHandlerFn returns the encode tx REST handler. In particular,
// it takes a json-formatted transaction, encodes it to the Amino wire protocol,
// and responds with base64-encoded bytes.
func EncodeTxRequestHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
func EncodeTxRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req auth.StdTx

Expand All @@ -38,7 +38,7 @@ func EncodeTxRequestHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.
return
}

err = cdc.UnmarshalJSON(body, &req)
err = cliCtx.Codec.UnmarshalJSON(body, &req)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
Expand All @@ -55,7 +55,7 @@ func EncodeTxRequestHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.
txBytesBase64 := base64.StdEncoding.EncodeToString(txBytes)

response := EncodeResp{Tx: txBytesBase64}
rest.PostProcessResponse(w, cdc, response, cliCtx.Indent)
rest.PostProcessResponse(w, cliCtx, response)
}
}

Expand Down
18 changes: 9 additions & 9 deletions client/tx/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ $ <appcli> query txs --tags '<tag1>:<value1>&<tag2>:<value2>' --page 1 --limit 3
limit := viper.GetInt(flagLimit)

cliCtx := context.NewCLIContext().WithCodec(cdc)
txs, err := SearchTxs(cliCtx, cdc, tmTags, page, limit)
txs, err := SearchTxs(cliCtx, tmTags, page, limit)
if err != nil {
return err
}
Expand Down Expand Up @@ -114,7 +114,7 @@ func QueryTxCmd(cdc *codec.Codec) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)

output, err := queryTx(cdc, cliCtx, args[0])
output, err := queryTx(cliCtx, args[0])
if err != nil {
return err
}
Expand All @@ -141,7 +141,7 @@ func QueryTxCmd(cdc *codec.Codec) *cobra.Command {

// QueryTxsByTagsRequestHandlerFn implements a REST handler that searches for
// transactions by tags.
func QueryTxsByTagsRequestHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
func QueryTxsByTagsRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var (
tags []string
Expand All @@ -157,7 +157,7 @@ func QueryTxsByTagsRequestHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec)
}

if len(r.Form) == 0 {
rest.PostProcessResponse(w, cdc, txs, cliCtx.Indent)
rest.PostProcessResponse(w, cliCtx, txs)
return
}

Expand All @@ -167,24 +167,24 @@ func QueryTxsByTagsRequestHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec)
return
}

searchResult, err := SearchTxs(cliCtx, cdc, tags, page, limit)
searchResult, err := SearchTxs(cliCtx, tags, page, limit)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

rest.PostProcessResponse(w, cdc, searchResult, cliCtx.Indent)
rest.PostProcessResponse(w, cliCtx, searchResult)
}
}

// QueryTxRequestHandlerFn implements a REST handler that queries a transaction
// by hash in a committed block.
func QueryTxRequestHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
func QueryTxRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
hashHexStr := vars["hash"]

output, err := queryTx(cdc, cliCtx, hashHexStr)
output, err := queryTx(cliCtx, hashHexStr)
if err != nil {
if strings.Contains(err.Error(), hashHexStr) {
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
Expand All @@ -198,6 +198,6 @@ func QueryTxRequestHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.H
rest.WriteErrorResponse(w, http.StatusNotFound, fmt.Sprintf("no transaction found with hash %s", hashHexStr))
}

rest.PostProcessResponse(w, cdc, output, cliCtx.Indent)
rest.PostProcessResponse(w, cliCtx, output)
}
}
11 changes: 5 additions & 6 deletions client/tx/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"github.com/gorilla/mux"

"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
)

// register REST routes
func RegisterTxRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec) {
r.HandleFunc("/txs/{hash}", QueryTxRequestHandlerFn(cdc, cliCtx)).Methods("GET")
r.HandleFunc("/txs", QueryTxsByTagsRequestHandlerFn(cliCtx, cdc)).Methods("GET")
r.HandleFunc("/txs", BroadcastTxRequest(cliCtx, cdc)).Methods("POST")
r.HandleFunc("/txs/encode", EncodeTxRequestHandlerFn(cdc, cliCtx)).Methods("POST")
func RegisterTxRoutes(cliCtx context.CLIContext, r *mux.Router) {
r.HandleFunc("/txs/{hash}", QueryTxRequestHandlerFn(cliCtx)).Methods("GET")
r.HandleFunc("/txs", QueryTxsByTagsRequestHandlerFn(cliCtx)).Methods("GET")
r.HandleFunc("/txs", BroadcastTxRequest(cliCtx)).Methods("POST")
r.HandleFunc("/txs/encode", EncodeTxRequestHandlerFn(cliCtx)).Methods("POST")
}
8 changes: 4 additions & 4 deletions client/tx/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
// SearchTxs performs a search for transactions for a given set of tags via
// Tendermint RPC. It returns a slice of Info object containing txs and metadata.
// An error is returned if the query fails.
func SearchTxs(cliCtx context.CLIContext, cdc *codec.Codec, tags []string, page, limit int) (*sdk.SearchTxsResult, error) {
func SearchTxs(cliCtx context.CLIContext, tags []string, page, limit int) (*sdk.SearchTxsResult, error) {
if len(tags) == 0 {
return nil, errors.New("must declare at least one tag to search")
}
Expand Down Expand Up @@ -59,7 +59,7 @@ func SearchTxs(cliCtx context.CLIContext, cdc *codec.Codec, tags []string, page,
return nil, err
}

txs, err := formatTxResults(cdc, resTxs.Txs, resBlocks)
txs, err := formatTxResults(cliCtx.Codec, resTxs.Txs, resBlocks)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -140,7 +140,7 @@ func parseTx(cdc *codec.Codec, txBytes []byte) (sdk.Tx, error) {
return tx, nil
}

func queryTx(cdc *codec.Codec, cliCtx context.CLIContext, hashHexStr string) (sdk.TxResponse, error) {
func queryTx(cliCtx context.CLIContext, hashHexStr string) (sdk.TxResponse, error) {
hash, err := hex.DecodeString(hashHexStr)
if err != nil {
return sdk.TxResponse{}, err
Expand All @@ -167,7 +167,7 @@ func queryTx(cdc *codec.Codec, cliCtx context.CLIContext, hashHexStr string) (sd
return sdk.TxResponse{}, err
}

out, err := formatTxResult(cdc, resTx, resBlocks[resTx.Height])
out, err := formatTxResult(cliCtx.Codec, resTx, resBlocks[resTx.Height])
if err != nil {
return out, err
}
Expand Down
8 changes: 3 additions & 5 deletions types/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type AppModuleBasic interface {
ValidateGenesis(json.RawMessage) error

// client functionality
RegisterRESTRoutes(context.CLIContext, *mux.Router, *codec.Codec)
RegisterRESTRoutes(context.CLIContext, *mux.Router)
GetTxCmd(*codec.Codec) *cobra.Command
GetQueryCmd(*codec.Codec) *cobra.Command
}
Expand Down Expand Up @@ -94,11 +94,9 @@ func (bm BasicManager) ValidateGenesis(genesis map[string]json.RawMessage) error
}

// RegisterRestRoutes registers all module rest routes
func (bm BasicManager) RegisterRESTRoutes(
ctx context.CLIContext, rtr *mux.Router, cdc *codec.Codec) {

func (bm BasicManager) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router) {
for _, b := range bm {
b.RegisterRESTRoutes(ctx, rtr, cdc)
b.RegisterRESTRoutes(ctx, rtr)
}
}

Expand Down
9 changes: 5 additions & 4 deletions types/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/tendermint/tendermint/types"

"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
)
Expand Down Expand Up @@ -197,7 +198,7 @@ func ParseFloat64OrReturnBadRequest(w http.ResponseWriter, s string, defaultIfEm
}

// PostProcessResponse performs post processing for a REST response.
func PostProcessResponse(w http.ResponseWriter, cdc *codec.Codec, response interface{}, indent bool) {
func PostProcessResponse(w http.ResponseWriter, cliCtx context.CLIContext, response interface{}) {
var output []byte

switch response.(type) {
Expand All @@ -206,10 +207,10 @@ func PostProcessResponse(w http.ResponseWriter, cdc *codec.Codec, response inter

default:
var err error
if indent {
output, err = cdc.MarshalJSONIndent(response, "", " ")
if cliCtx.Indent {
output, err = cliCtx.Codec.MarshalJSONIndent(response, "", " ")
} else {
output, err = cdc.MarshalJSON(response)
output, err = cliCtx.Codec.MarshalJSON(response)
}
if err != nil {
WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
Expand Down
Loading

0 comments on commit 3180e68

Please sign in to comment.