Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R4R: various: code clean up, refactoring #3580

Merged
merged 23 commits into from
Feb 14, 2019
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ BREAKING CHANGES
* Gaia

* SDK
* \#3580 Migrate HTTP request/response types and utilities to types/rest.
* \#3592 Drop deprecated keybase implementation's New() constructor in
favor of a new crypto/keys.New(string, string) implementation that
returns a lazy keybase instance. Remove client.MockKeyBase,
superseded by crypto/keys.NewInMemory()

* Tendermint

Expand Down
5 changes: 3 additions & 2 deletions client/keys/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/cosmos/cosmos-sdk/cmd/gaia/app"
"github.com/cosmos/cosmos-sdk/crypto/keys"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"

"errors"

Expand Down Expand Up @@ -399,7 +400,7 @@ func AddNewKeyRequestHandler(indent bool) http.HandlerFunc {

keyOutput.Mnemonic = mnemonic

PostProcessResponse(w, cdc, keyOutput, indent)
rest.PostProcessResponse(w, cdc, keyOutput, indent)
}
}

Expand Down Expand Up @@ -488,6 +489,6 @@ func RecoverRequestHandler(indent bool) http.HandlerFunc {
return
}

PostProcessResponse(w, cdc, keyOutput, indent)
rest.PostProcessResponse(w, cdc, keyOutput, indent)
}
}
5 changes: 3 additions & 2 deletions client/keys/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keys
import (
"net/http"

"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -49,7 +50,7 @@ func QueryKeysRequestHandler(indent bool) http.HandlerFunc {
}
// an empty list will be JSONized as null, but we want to keep the empty list
if len(infos) == 0 {
PostProcessResponse(w, cdc, []string{}, indent)
rest.PostProcessResponse(w, cdc, []string{}, indent)
return
}
keysOutput, err := Bech32KeysOutput(infos)
Expand All @@ -58,6 +59,6 @@ func QueryKeysRequestHandler(indent bool) http.HandlerFunc {
_, _ = w.Write([]byte(err.Error()))
return
}
PostProcessResponse(w, cdc, keysOutput, indent)
rest.PostProcessResponse(w, cdc, keysOutput, indent)
}
}
3 changes: 2 additions & 1 deletion client/keys/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/tendermint/tendermint/crypto"

"github.com/cosmos/cosmos-sdk/crypto/keys"
"github.com/cosmos/cosmos-sdk/types/rest"

"errors"

Expand Down Expand Up @@ -188,6 +189,6 @@ func GetKeyRequestHandler(indent bool) http.HandlerFunc {
return
}

PostProcessResponse(w, cdc, keyOutput, indent)
rest.PostProcessResponse(w, cdc, keyOutput, indent)
}
}
25 changes: 0 additions & 25 deletions client/keys/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package keys

import (
"fmt"
"net/http"
"path/filepath"

"github.com/spf13/viper"
"github.com/tendermint/tendermint/libs/cli"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys"
sdk "github.com/cosmos/cosmos-sdk/types"
)
Expand Down Expand Up @@ -217,26 +215,3 @@ func printPubKey(info keys.Info, bechKeyOut bechKeyOutFn) {

fmt.Println(ko.PubKey)
}

// PostProcessResponse performs post process for rest response
func PostProcessResponse(w http.ResponseWriter, cdc *codec.Codec, response interface{}, indent bool) {
var output []byte
switch response.(type) {
default:
var err error
if indent {
output, err = cdc.MarshalJSONIndent(response, "", " ")
} else {
output, err = cdc.MarshalJSON(response)
}
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(err.Error()))
return
}
case []byte:
output = response.([]byte)
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(output)
}
2 changes: 1 addition & 1 deletion client/lcd/lcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import (

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/client/rest"
"github.com/cosmos/cosmos-sdk/cmd/gaia/app"
"github.com/cosmos/cosmos-sdk/crypto/keys/mintkey"
"github.com/cosmos/cosmos-sdk/tests"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/auth"
authrest "github.com/cosmos/cosmos-sdk/x/auth/client/rest"
Expand Down
45 changes: 10 additions & 35 deletions client/lcd/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/client/rest"

"github.com/cosmos/cosmos-sdk/client/rpc"
"github.com/cosmos/cosmos-sdk/client/tx"
gapp "github.com/cosmos/cosmos-sdk/cmd/gaia/app"
Expand All @@ -29,6 +29,7 @@ import (
"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/tests"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/x/auth"
authrest "github.com/cosmos/cosmos-sdk/x/auth/client/rest"
txbuilder "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
Expand Down Expand Up @@ -653,7 +654,7 @@ func doSign(t *testing.T, port, name, password, chainID string, accnum, sequence

// POST /tx/broadcast Send a signed Tx
func doBroadcast(t *testing.T, port string, msg auth.StdTx) sdk.TxResponse {
tx := rest.BroadcastReq{Tx: msg, Return: "block"}
tx := authrest.BroadcastReq{Tx: msg, Return: "block"}
req, err := cdc.MarshalJSON(tx)
require.Nil(t, err)
res, body := Request(t, port, "POST", "/tx/broadcast", req)
Expand Down Expand Up @@ -706,7 +707,7 @@ func doTransferWithGas(
generateOnly, simulate,
)

sr := rest.SendReq{
sr := bankrest.SendReq{
Amount: sdk.Coins{sdk.NewInt64Coin(staking.DefaultBondDenom, 1)},
BaseReq: baseReq,
}
Expand Down Expand Up @@ -739,7 +740,7 @@ func doTransferWithGasAccAuto(
fmt.Sprintf("%f", gasAdjustment), 0, 0, fees, nil, generateOnly, simulate,
)

sr := rest.SendReq{
sr := bankrest.SendReq{
Amount: sdk.Coins{sdk.NewInt64Coin(staking.DefaultBondDenom, 1)},
BaseReq: baseReq,
}
Expand All @@ -751,11 +752,6 @@ func doTransferWithGasAccAuto(
return
}

type sendReq struct {
Amount sdk.Coins `json:"amount"`
BaseReq rest.BaseReq `json:"base_req"`
}

// ----------------------------------------------------------------------
// ICS 21 - Stake
// ----------------------------------------------------------------------
Expand Down Expand Up @@ -841,7 +837,7 @@ func doBeginRedelegation(t *testing.T, port, name, password string,
chainID := viper.GetString(client.FlagChainID)
baseReq := rest.NewBaseReq(name, password, "", chainID, "", "", accnum, sequence, fees, nil, false, false)

msg := rest.MsgBeginRedelegateInput{
msg := stakingrest.MsgBeginRedelegateInput{
BaseReq: baseReq,
DelegatorAddr: delAddr,
ValidatorSrcAddr: valSrcAddr,
Expand Down Expand Up @@ -1074,7 +1070,7 @@ func doSubmitProposal(t *testing.T, port, seed, name, password string, proposerA
chainID := viper.GetString(client.FlagChainID)
baseReq := rest.NewBaseReq(name, password, "", chainID, "", "", accnum, sequence, fees, nil, false, false)

pr := rest.PostProposalReq{
pr := govrest.PostProposalReq{
Title: "Test",
Description: "test",
ProposalType: "Text",
Expand All @@ -1097,15 +1093,6 @@ func doSubmitProposal(t *testing.T, port, seed, name, password string, proposerA
return results
}

type postProposalReq struct {
BaseReq rest.BaseReq `json:"base_req"`
Title string `json:"title"` // Title of the proposal
Description string `json:"description"` // Description of the proposal
ProposalType string `json:"proposal_type"` // Type of proposal. Initial set {PlainTextProposal, SoftwareUpgradeProposal}
Proposer sdk.AccAddress `json:"proposer"` // Address of the proposer
InitialDeposit sdk.Coins `json:"initial_deposit"` // Coins to add to the proposal's deposit
}

// GET /gov/proposals Query proposals
func getProposalsAll(t *testing.T, port string) []gov.Proposal {
res, body := Request(t, port, "GET", "/gov/proposals", nil)
Expand Down Expand Up @@ -1171,7 +1158,7 @@ func doDeposit(t *testing.T, port, seed, name, password string, proposerAddr sdk
chainID := viper.GetString(client.FlagChainID)
baseReq := rest.NewBaseReq(name, password, "", chainID, "", "", accnum, sequence, fees, nil, false, false)

dr := rest.DepositReq{
dr := govrest.DepositReq{
Depositor: proposerAddr,
Amount: sdk.Coins{sdk.NewCoin(staking.DefaultBondDenom, amount)},
BaseReq: baseReq,
Expand All @@ -1190,12 +1177,6 @@ func doDeposit(t *testing.T, port, seed, name, password string, proposerAddr sdk
return results
}

type depositReq struct {
BaseReq rest.BaseReq `json:"base_req"`
Depositor sdk.AccAddress `json:"depositor"` // Address of the depositor
Amount sdk.Coins `json:"amount"` // Coins to add to the proposal's deposit
}

// GET /gov/proposals/{proposalId}/deposits Query deposits
func getDeposits(t *testing.T, port string, proposalID uint64) []gov.Deposit {
res, body := Request(t, port, "GET", fmt.Sprintf("/gov/proposals/%d/deposits", proposalID), nil)
Expand Down Expand Up @@ -1225,7 +1206,7 @@ func doVote(t *testing.T, port, seed, name, password string, proposerAddr sdk.Ac
chainID := viper.GetString(client.FlagChainID)
baseReq := rest.NewBaseReq(name, password, "", chainID, "", "", accnum, sequence, fees, nil, false, false)

vr := rest.VoteReq{
vr := govrest.VoteReq{
Voter: proposerAddr,
Option: option,
BaseReq: baseReq,
Expand All @@ -1244,12 +1225,6 @@ func doVote(t *testing.T, port, seed, name, password string, proposerAddr sdk.Ac
return results
}

type voteReq struct {
BaseReq rest.BaseReq `json:"base_req"`
Voter sdk.AccAddress `json:"voter"` // address of the voter
Option string `json:"option"` // option from OptionSet chosen by the voter
}

// GET /gov/proposals/{proposalId}/votes Query voters
func getVotes(t *testing.T, port string, proposalID uint64) []gov.Vote {
res, body := Request(t, port, "GET", fmt.Sprintf("/gov/proposals/%d/votes", proposalID), nil)
Expand Down Expand Up @@ -1357,7 +1332,7 @@ func doUnjail(t *testing.T, port, seed, name, password string,
chainID := viper.GetString(client.FlagChainID)
baseReq := rest.NewBaseReq(name, password, "", chainID, "", "", 1, 1, fees, nil, false, false)

ur := rest.UnjailReq{
ur := slashingrest.UnjailReq{
BaseReq: baseReq,
}
req, err := cdc.MarshalJSON(ur)
Expand Down
Loading