From 2d429731b3f30e9c6bf06ba0c4e26fd7585d273c Mon Sep 17 00:00:00 2001 From: Sunny Aggarwal Date: Tue, 23 Oct 2018 01:25:53 -0700 Subject: [PATCH] LCD and CLI --- PENDING.md | 2 ++ client/lcd/lcd_test.go | 17 +++++++++++++++- cmd/gaia/cli_test/cli_test.go | 13 ++++++++++++ x/stake/client/cli/query.go | 37 +++++++++++++++++++++++++++++++++++ x/stake/client/rest/query.go | 11 +++++++++++ 5 files changed, 79 insertions(+), 1 deletion(-) diff --git a/PENDING.md b/PENDING.md index 84a08a5ab..ab68635f1 100644 --- a/PENDING.md +++ b/PENDING.md @@ -23,6 +23,7 @@ FEATURES * [cli] [\#2569](https://github.com/cosmos/cosmos-sdk/pull/2569) Add commands to query validator unbondings and redelegations * [cli] [\#2524](https://github.com/cosmos/cosmos-sdk/issues/2524) Add support offline mode to `gaiacli tx sign`. Lookups are not performed if the flag `--offline` is on. * [cli] [\#2558](https://github.com/cosmos/cosmos-sdk/issues/2558) Rename --print-sigs to --validate-signatures. It now performs a complete set of sanity checks and reports to the user. Also added --print-signature-only to print the signature only, not the whole transaction. + * [stake][cli] [\#2027] Add CLI query command for getting all delegations to a specific validator. * Gaia @@ -46,6 +47,7 @@ IMPROVEMENTS - \#2660 [x/mock/simulation] Staking transactions get tested far more frequently - #2610 [x/stake] Block redelegation to and from the same validator - #2652 [x/auth] Add benchmark for get and set account + - \#2027 [x/stake] Add `Querier` for getting all delegations to a specific validator. * Tendermint diff --git a/client/lcd/lcd_test.go b/client/lcd/lcd_test.go index cb25721e8..deb1687b7 100644 --- a/client/lcd/lcd_test.go +++ b/client/lcd/lcd_test.go @@ -538,7 +538,7 @@ func TestBonding(t *testing.T) { require.Equal(t, int64(40), coins.AmountOf(denom).Int64()) - // query validator + // query delegation bond := getDelegation(t, port, addr, operAddrs[0]) require.Equal(t, amt, bond.Shares) @@ -546,6 +546,10 @@ func TestBonding(t *testing.T) { require.Len(t, delegatorDels, 1) require.Equal(t, amt, delegatorDels[0].Shares) + // query all delegations to validator + bonds := getValidatorDelegations(t, port, operAddrs[0]) + require.Len(t, bonds, 2) + bondedValidators := getDelegatorValidators(t, port, addr) require.Len(t, bondedValidators, 1) require.Equal(t, operAddrs[0], bondedValidators[0].OperatorAddr) @@ -1206,6 +1210,17 @@ func getValidator(t *testing.T, port string, validatorAddr sdk.ValAddress) stake return validator } +func getValidatorDelegations(t *testing.T, port string, validatorAddr sdk.ValAddress) []stake.Delegation { + res, body := Request(t, port, "GET", fmt.Sprintf("/stake/validators/%s/delegations", validatorAddr.String()), nil) + require.Equal(t, http.StatusOK, res.StatusCode, body) + + var delegations []stake.Delegation + err := cdc.UnmarshalJSON([]byte(body), &delegations) + require.Nil(t, err) + + return delegations +} + func getValidatorUnbondingDelegations(t *testing.T, port string, validatorAddr sdk.ValAddress) []stake.UnbondingDelegation { res, body := Request(t, port, "GET", fmt.Sprintf("/stake/validators/%s/unbonding_delegations", validatorAddr.String()), nil) require.Equal(t, http.StatusOK, res.StatusCode, body) diff --git a/cmd/gaia/cli_test/cli_test.go b/cmd/gaia/cli_test/cli_test.go index b4791bf12..76b5ad8eb 100644 --- a/cmd/gaia/cli_test/cli_test.go +++ b/cmd/gaia/cli_test/cli_test.go @@ -268,6 +268,10 @@ func TestGaiaCLICreateValidator(t *testing.T) { require.Equal(t, validator.OperatorAddr, sdk.ValAddress(barAddr)) require.True(sdk.DecEq(t, sdk.NewDec(2), validator.Tokens)) + validatorDelegations := executeGetValidatorDelegations(t, fmt.Sprintf("gaiacli query delegations-to %s --output=json %v", sdk.ValAddress(barAddr), flags)) + require.Len(t, validatorDelegations, 1) + require.NotZero(t, validatorDelegations[0].Shares) + // unbond a single share unbondStr := fmt.Sprintf("gaiacli tx unbond begin %v", flags) unbondStr += fmt.Sprintf(" --from=%s", "bar") @@ -720,6 +724,15 @@ func executeGetValidatorRedelegations(t *testing.T, cmdStr string) []stake.Redel return reds } +func executeGetValidatorDelegations(t *testing.T, cmdStr string) []stake.Delegation { + out, _ := tests.ExecuteT(t, cmdStr, "") + var delegations []stake.Delegation + cdc := app.MakeCodec() + err := cdc.UnmarshalJSON([]byte(out), &delegations) + require.NoError(t, err, "out %v\n, err %v", out, err) + return delegations +} + func executeGetPool(t *testing.T, cmdStr string) stake.Pool { out, _ := tests.ExecuteT(t, cmdStr, "") var pool stake.Pool diff --git a/x/stake/client/cli/query.go b/x/stake/client/cli/query.go index 28d09df8e..47bcd693a 100644 --- a/x/stake/client/cli/query.go +++ b/x/stake/client/cli/query.go @@ -290,6 +290,43 @@ func GetCmdQueryDelegations(storeName string, cdc *codec.Codec) *cobra.Command { return cmd } +// GetCmdQueryValidatorDelegations implements the command to query all the +// delegations to a specific validator. +func GetCmdQueryValidatorDelegations(storeName string, cdc *codec.Codec) *cobra.Command { + cmd := &cobra.Command{ + Use: "delegations-to [validator-addr]", + Short: "Query all delegations made to one validator", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + validatorAddr, err := sdk.ValAddressFromBech32(args[0]) + if err != nil { + return err + } + + params := stake.QueryValidatorParams{ + ValidatorAddr: validatorAddr, + } + + bz, err := cdc.MarshalJSON(params) + if err != nil { + return err + } + + cliCtx := context.NewCLIContext().WithCodec(cdc) + + res, err := cliCtx.QueryWithData("custom/stake/validatorDelegations", bz) + if err != nil { + return err + } + + fmt.Println(string(res)) + return nil + }, + } + + return cmd +} + // GetCmdQueryUnbondingDelegation implements the command to query a single // unbonding-delegation record. func GetCmdQueryUnbondingDelegation(storeName string, cdc *codec.Codec) *cobra.Command { diff --git a/x/stake/client/rest/query.go b/x/stake/client/rest/query.go index 935c0229f..1c73665a7 100644 --- a/x/stake/client/rest/query.go +++ b/x/stake/client/rest/query.go @@ -78,6 +78,12 @@ func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Co validatorHandlerFn(cliCtx, cdc), ).Methods("GET") + // Get all delegations to a validator + r.HandleFunc( + "/stake/validators/{validatorAddr}/delegations", + validatorDelegationsHandlerFn(cliCtx, cdc), + ).Methods("GET") + // Get all unbonding delegations from a validator r.HandleFunc( "/stake/validators/{validatorAddr}/unbonding_delegations", @@ -227,6 +233,11 @@ func validatorHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.Handle return queryValidator(cliCtx, cdc, "custom/stake/validator") } +// HTTP request handler to query all unbonding delegations from a validator +func validatorDelegationsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc { + return queryValidator(cliCtx, cdc, "custom/stake/validatorDelegations") +} + // HTTP request handler to query all unbonding delegations from a validator func validatorUnbondingDelegationsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc { return queryValidator(cliCtx, cdc, "custom/stake/validatorUnbondingDelegations")