Skip to content

Commit

Permalink
Merge PR cosmos#3580: Code cleanup of client types
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio authored and jackzampolin committed Feb 14, 2019
1 parent f72a7c3 commit 1aa6c19
Show file tree
Hide file tree
Showing 34 changed files with 325 additions and 357 deletions.
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()
* \#3621 staking.GenesisState.Bonds -> Delegations

* 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
57 changes: 20 additions & 37 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 @@ -130,7 +131,7 @@ func CreateAddrs(t *testing.T, kb crkeys.Keybase, numAddrs int) (addrs []sdk.Acc
password := "1234567890"
info, seed, err = kb.CreateMnemonic(name, crkeys.English, password, crkeys.Secp256k1)
require.NoError(t, err)
addrSeeds = append(addrSeeds, rest.AddrSeed{Address: sdk.AccAddress(info.GetPubKey().Address()), Seed: seed, Name: name, Password: password})
addrSeeds = append(addrSeeds, AddrSeed{Address: sdk.AccAddress(info.GetPubKey().Address()), Seed: seed, Name: name, Password: password})
}

sort.Sort(addrSeeds)
Expand All @@ -145,8 +146,16 @@ func CreateAddrs(t *testing.T, kb crkeys.Keybase, numAddrs int) (addrs []sdk.Acc
return addrs, seeds, names, passwords
}

// AddrSeed combines an Address with the mnemonic of the private key to that address
type AddrSeed struct {
Address sdk.AccAddress
Seed string
Name string
Password string
}

// AddrSeedSlice implements `Interface` in sort package.
type AddrSeedSlice []rest.AddrSeed
type AddrSeedSlice []AddrSeed

func (b AddrSeedSlice) Len() int {
return len(b)
Expand Down Expand Up @@ -653,7 +662,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 +715,7 @@ func doTransferWithGas(
generateOnly, simulate,
)

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

sr := rest.SendReq{
sr := bankrest.SendReq{
Amount: sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 1)},
BaseReq: baseReq,
}
Expand All @@ -751,11 +760,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 +845,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 +1078,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 +1101,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 +1166,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(sdk.DefaultBondDenom, amount)},
BaseReq: baseReq,
Expand All @@ -1190,12 +1185,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 +1214,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 +1233,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 +1340,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

0 comments on commit 1aa6c19

Please sign in to comment.